forked from ctreatma/rails_sql_views
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/cogent/rails_sql_views in…
…to cogent
- Loading branch information
Showing
10 changed files
with
212 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module RailsSqlViews | ||
module ConnectionAdapters | ||
module Mysql2Adapter | ||
def self.included(base) | ||
if base.private_method_defined?(:supports_views?) | ||
base.send(:public, :supports_views?) | ||
end | ||
end | ||
|
||
# Returns true as this adapter supports views. | ||
def supports_views? | ||
true | ||
end | ||
|
||
def base_tables(name = nil) #:nodoc: | ||
tables = [] | ||
execute("SHOW FULL TABLES WHERE TABLE_TYPE='BASE TABLE'").each{|row| tables << row[0]} | ||
tables | ||
end | ||
alias nonview_tables base_tables | ||
|
||
def views(name = nil) #:nodoc: | ||
views = [] | ||
execute("SHOW FULL TABLES WHERE TABLE_TYPE='VIEW'").each{|row| views << row[0]} | ||
views | ||
end | ||
|
||
def tables_with_views_included(name = nil) | ||
nonview_tables(name) + views(name) | ||
end | ||
|
||
def structure_dump | ||
structure = "" | ||
base_tables.each do |table| | ||
structure += select_one("SHOW CREATE TABLE #{quote_table_name(table)}")["Create Table"] + ";\n\n" | ||
end | ||
|
||
views.each do |view| | ||
structure += select_one("SHOW CREATE VIEW #{quote_table_name(view)}")["Create View"] + ";\n\n" | ||
end | ||
|
||
return structure | ||
end | ||
|
||
# Get the view select statement for the specified table. | ||
def view_select_statement(view, name=nil) | ||
begin | ||
row = execute("SHOW CREATE VIEW #{view}", name).each do |row| | ||
return convert_statement(row[1]) if row[0] == view | ||
end | ||
rescue ActiveRecord::StatementInvalid => e | ||
raise "No view called #{view} found" | ||
end | ||
end | ||
|
||
private | ||
def convert_statement(s) | ||
s.gsub!(/.* AS (select .*)/, '\1') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
print "Using native MySQL2\n" | ||
|
||
adapter_name = 'mysql2' | ||
config = YAML.load_file(File.join(File.dirname(__FILE__), '/../../connection.yml'))[adapter_name] | ||
|
||
#require 'logger' | ||
#ActiveRecord::Base.logger = Logger.new("debug.log") | ||
|
||
ActiveRecord::Base.silence do | ||
ActiveRecord::Base.configurations = { | ||
config['database'] => { | ||
:adapter => adapter_name, | ||
:username => config['username'], | ||
:password => config['password'], | ||
:host => config['host'], | ||
:database => config['database'], | ||
:encoding => config['encoding'], | ||
:schema_file => config['schema_file'], | ||
} | ||
} | ||
|
||
ActiveRecord::Base.establish_connection config['database'] | ||
ActiveRecord::Migration.verbose = false | ||
|
||
puts "Resetting database" | ||
conn = ActiveRecord::Base.connection | ||
conn.recreate_database(conn.current_database) | ||
conn.reconnect! | ||
lines = open(File.join(File.dirname(__FILE__), ActiveRecord::Base.configurations[config['database']][:schema_file])).readlines | ||
lines.join.split(';').each { |line| conn.execute(line) } | ||
conn.reconnect! | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
drop table if exists people; | ||
create table people ( | ||
id int(11) DEFAULT NULL auto_increment PRIMARY KEY, | ||
first_name char(255), | ||
last_name char(255), | ||
ssn char(64), | ||
address_id integer | ||
); | ||
drop table if exists people2; | ||
create table people2 ( | ||
id int(11) DEFAULT NULL auto_increment PRIMARY KEY, | ||
first_name char(255), | ||
last_name char(255), | ||
ssn char(64) | ||
); | ||
drop table if exists places; | ||
create table places ( | ||
id int(11) DEFAULT NULL auto_increment PRIMARY KEY, | ||
address text, | ||
city char(255), | ||
cstate char(255), | ||
country char(2) | ||
); | ||
drop table if exists items; | ||
create table items ( | ||
id int(11) DEFAULT NULL auto_increment PRIMARY KEY, | ||
person_id int(11) | ||
); | ||
drop table if exists items_people; | ||
create table items_people ( | ||
person_id int(11), | ||
item_id int(11) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# This file is auto-generated from the current state of the database. Instead of editing this file, | ||
# please use the migrations feature of Active Record to incrementally modify your database, and | ||
# then regenerate this schema definition. | ||
# | ||
# Note that this schema.rb definition is the authoritative source for your database schema. If you need | ||
# to create the application database on another system, you should be using db:schema:load, not running | ||
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations | ||
# you'll amass, the slower it'll run and the greater likelihood for issues). | ||
# | ||
# It's strongly recommended to check this file into your version control system. | ||
|
||
ActiveRecord::Schema.define(:version => 0) do | ||
|
||
create_table "items", :force => true do |t| | ||
t.integer "person_id" | ||
end | ||
|
||
create_table "items_people", :id => false, :force => true do |t| | ||
t.integer "person_id" | ||
t.integer "item_id" | ||
end | ||
|
||
create_table "people", :force => true do |t| | ||
t.string "first_name" | ||
t.string "last_name" | ||
t.string "ssn", :limit => 64 | ||
t.integer "address_id" | ||
end | ||
|
||
create_table "people2", :force => true do |t| | ||
t.string "first_name" | ||
t.string "last_name" | ||
t.string "ssn", :limit => 64 | ||
end | ||
|
||
create_table "places", :force => true do |t| | ||
t.text "address" | ||
t.string "city" | ||
t.string "cstate" | ||
t.string "country", :limit => 2 | ||
end | ||
|
||
create_view "v_people", "select `people`.`id` AS `id`,`people`.`first_name` AS `f_name`,`people`.`last_name` AS `l_name`,`people`.`ssn` AS `social_security`,`people`.`address_id` AS `address_id` from `people`", :force => true do |v| | ||
v.column :id | ||
v.column :f_name | ||
v.column :l_name | ||
v.column :social_security | ||
v.column :address_id | ||
end | ||
|
||
create_view "v_profile", "select `people`.`first_name` AS `first_name`,`people`.`last_name` AS `last_name`,`people`.`ssn` AS `ssn` from `people` union select `people2`.`first_name` AS `first_name`,`people2`.`last_name` AS `last_name`,`people2`.`ssn` AS `ssn` from `people2`", :force => true do |v| | ||
v.column :first_name | ||
v.column :last_name | ||
v.column :ssn | ||
end | ||
|
||
end |