robdimarco_rails_sql_views 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/CHANGELOG +22 -0
  2. data/CONTRIB +8 -0
  3. data/LICENSE +7 -0
  4. data/README +51 -0
  5. data/Rakefile +41 -0
  6. data/TODO +2 -0
  7. data/init.rb +1 -0
  8. data/lib/active_record/view.rb +76 -0
  9. data/lib/core_ext/module.rb +13 -0
  10. data/lib/rails_sql_views.rb +51 -0
  11. data/lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb +63 -0
  12. data/lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb +81 -0
  13. data/lib/rails_sql_views/connection_adapters/abstract_adapter.rb +41 -0
  14. data/lib/rails_sql_views/connection_adapters/mysql2_adapter.rb +62 -0
  15. data/lib/rails_sql_views/connection_adapters/mysql_adapter.rb +66 -0
  16. data/lib/rails_sql_views/connection_adapters/oci_adapter.rb +33 -0
  17. data/lib/rails_sql_views/connection_adapters/oracle_adapter.rb +33 -0
  18. data/lib/rails_sql_views/connection_adapters/oracleenhanced_adapter.rb +39 -0
  19. data/lib/rails_sql_views/connection_adapters/oracleenhanced_adapter.rb.orig +72 -0
  20. data/lib/rails_sql_views/connection_adapters/postgresql_adapter.rb +65 -0
  21. data/lib/rails_sql_views/connection_adapters/postgresql_adapter.rb.orig +69 -0
  22. data/lib/rails_sql_views/connection_adapters/sqlite_adapter.rb +66 -0
  23. data/lib/rails_sql_views/connection_adapters/sqlserver_adapter.rb +43 -0
  24. data/lib/rails_sql_views/loader.rb +20 -0
  25. data/lib/rails_sql_views/schema_dumper.rb +113 -0
  26. data/lib/rails_sql_views/version.rb +9 -0
  27. data/rails/init.rb +1 -0
  28. data/test/README +63 -0
  29. data/test/adapter_test.rb +82 -0
  30. data/test/connection.example.yml +12 -0
  31. data/test/connection/native_mysql/connection.rb +32 -0
  32. data/test/connection/native_mysql/schema.sql +33 -0
  33. data/test/connection/native_mysql2/connection.rb +32 -0
  34. data/test/connection/native_mysql2/schema.sql +33 -0
  35. data/test/connection/native_postgresql/connection.rb +31 -0
  36. data/test/connection/native_postgresql/schema.sql +33 -0
  37. data/test/connection/oracle_enhanced/connection.rb +29 -0
  38. data/test/connection/oracle_enhanced/procedures.sql +15 -0
  39. data/test/connection/oracle_enhanced/schema.sql +39 -0
  40. data/test/models/item.rb +4 -0
  41. data/test/models/person.rb +5 -0
  42. data/test/models/person2.rb +3 -0
  43. data/test/models/place.rb +2 -0
  44. data/test/models/v_person.rb +4 -0
  45. data/test/models/v_profile.rb +3 -0
  46. data/test/schema.native_mysql.expected.rb +57 -0
  47. data/test/schema.native_mysql2.expected.rb +58 -0
  48. data/test/schema.native_postgresql.expected.rb +51 -0
  49. data/test/schema.oracle_enhanced.expected.rb +51 -0
  50. data/test/schema_dumper_test.rb +130 -0
  51. data/test/test_helper.rb +30 -0
  52. data/test/view_model_test.rb +63 -0
  53. data/test/view_operations_test.rb +36 -0
  54. metadata +246 -0
@@ -0,0 +1,33 @@
1
+ drop table if exists people;
2
+ create table people (
3
+ id int(11) DEFAULT NULL auto_increment PRIMARY KEY,
4
+ first_name char(255),
5
+ last_name char(255),
6
+ ssn char(64),
7
+ address_id integer
8
+ );
9
+ drop table if exists people2;
10
+ create table people2 (
11
+ id int(11) DEFAULT NULL auto_increment PRIMARY KEY,
12
+ first_name char(255),
13
+ last_name char(255),
14
+ ssn char(64)
15
+ );
16
+ drop table if exists places;
17
+ create table places (
18
+ id int(11) DEFAULT NULL auto_increment PRIMARY KEY,
19
+ address text,
20
+ city char(255),
21
+ cstate char(255),
22
+ country char(2)
23
+ );
24
+ drop table if exists items;
25
+ create table items (
26
+ id int(11) DEFAULT NULL auto_increment PRIMARY KEY,
27
+ person_id int(11)
28
+ );
29
+ drop table if exists items_people;
30
+ create table items_people (
31
+ person_id int(11),
32
+ item_id int(11)
33
+ );
@@ -0,0 +1,32 @@
1
+ print "Using native MySQL2\n"
2
+
3
+ adapter_name = 'mysql2'
4
+ config = YAML.load_file(File.join(File.dirname(__FILE__), '/../../connection.yml'))[adapter_name]
5
+
6
+ #require 'logger'
7
+ #ActiveRecord::Base.logger = Logger.new("debug.log")
8
+
9
+ ActiveRecord::Base.silence do
10
+ ActiveRecord::Base.configurations = {
11
+ config['database'] => {
12
+ :adapter => adapter_name,
13
+ :username => config['username'],
14
+ :password => config['password'],
15
+ :host => config['host'],
16
+ :database => config['database'],
17
+ :encoding => config['encoding'],
18
+ :schema_file => config['schema_file'],
19
+ }
20
+ }
21
+
22
+ ActiveRecord::Base.establish_connection config['database']
23
+ ActiveRecord::Migration.verbose = false
24
+
25
+ puts "Resetting database"
26
+ conn = ActiveRecord::Base.connection
27
+ conn.recreate_database(conn.current_database)
28
+ conn.reconnect!
29
+ lines = open(File.join(File.dirname(__FILE__), ActiveRecord::Base.configurations[config['database']][:schema_file])).readlines
30
+ lines.join.split(';').each { |line| conn.execute(line) }
31
+ conn.reconnect!
32
+ end
@@ -0,0 +1,33 @@
1
+ drop table if exists people;
2
+ create table people (
3
+ id int(11) DEFAULT NULL auto_increment PRIMARY KEY,
4
+ first_name char(255),
5
+ last_name char(255),
6
+ ssn char(64),
7
+ address_id integer
8
+ );
9
+ drop table if exists people2;
10
+ create table people2 (
11
+ id int(11) DEFAULT NULL auto_increment PRIMARY KEY,
12
+ first_name char(255),
13
+ last_name char(255),
14
+ ssn char(64)
15
+ );
16
+ drop table if exists places;
17
+ create table places (
18
+ id int(11) DEFAULT NULL auto_increment PRIMARY KEY,
19
+ address text,
20
+ city char(255),
21
+ cstate char(255),
22
+ country char(2)
23
+ );
24
+ drop table if exists items;
25
+ create table items (
26
+ id int(11) DEFAULT NULL auto_increment PRIMARY KEY,
27
+ person_id int(11)
28
+ );
29
+ drop table if exists items_people;
30
+ create table items_people (
31
+ person_id int(11),
32
+ item_id int(11)
33
+ );
@@ -0,0 +1,31 @@
1
+ print "Using native PostgreSQL\n"
2
+
3
+ adapter_name = 'postgresql'
4
+ config = YAML.load_file(File.join(File.dirname(__FILE__), '/../../connection.yml'))[adapter_name]
5
+
6
+ #require 'logger'
7
+ #ActiveRecord::Base.logger = Logger.new("debug.log")
8
+
9
+ ActiveRecord::Base.silence do
10
+ ActiveRecord::Base.configurations = {
11
+ 'rails_sql_views_unittest' => {
12
+ :adapter => adapter_name,
13
+ :username => config['username'],
14
+ :password => config['password'],
15
+ :host => config['host'],
16
+ :database => config['database'],
17
+ :encoding => config['encoding'],
18
+ :schema_file => config['schema_file'],
19
+ }
20
+ }
21
+
22
+ ActiveRecord::Base.establish_connection config['database']
23
+
24
+ puts "Resetting database"
25
+ conn = ActiveRecord::Base.connection
26
+ #conn.recreate_database(conn.current_database)
27
+ conn.reconnect!
28
+ lines = open(File.join(File.dirname(__FILE__), ActiveRecord::Base.configurations[config['database']][:schema_file])).readlines
29
+ lines.join.split(';').each { |line| conn.execute(line) }
30
+ conn.reconnect!
31
+ end
@@ -0,0 +1,33 @@
1
+ drop table if exists people CASCADE;
2
+ create table people (
3
+ id serial primary key,
4
+ first_name char(255),
5
+ last_name char(255),
6
+ ssn char(64),
7
+ address_id integer
8
+ );
9
+ drop table if exists people2 CASCADE;
10
+ create table people2 (
11
+ id serial primary key,
12
+ first_name char(255),
13
+ last_name char(255),
14
+ ssn char(64)
15
+ );
16
+ drop table if exists places CASCADE;
17
+ create table places (
18
+ id serial primary key,
19
+ address text,
20
+ city char(255),
21
+ cstate char(255),
22
+ country char(2)
23
+ );
24
+ drop table if exists items CASCADE;
25
+ create table items (
26
+ id serial primary key,
27
+ person_id integer
28
+ );
29
+ drop table if exists items_people CASCADE;
30
+ create table items_people (
31
+ person_id integer,
32
+ item_id integer
33
+ );
@@ -0,0 +1,29 @@
1
+ print "Using Oracle Enhanced\n"
2
+
3
+ #require 'logger'
4
+ #ActiveRecord::Base.logger = Logger.new("debug.log")
5
+
6
+ ActiveRecord::Base.configurations = {
7
+ 'rails_sql_views_unittest' => {
8
+ :adapter => :oracle_enhanced,
9
+ :username => 'rails_sql_views_unittest',
10
+ :password => 'rails',
11
+ :host => 'localhost',
12
+ :database => 'mydev',
13
+ :encoding => 'utf8',
14
+ :procedures_file => 'procedures.sql',
15
+ :schema_file => 'schema.sql',
16
+ }
17
+ }
18
+
19
+ ActiveRecord::Base.establish_connection 'rails_sql_views_unittest'
20
+
21
+ puts "Resetting database"
22
+ conn = ActiveRecord::Base.connection
23
+ #conn.recreate_database(conn.current_database)
24
+ conn.reconnect!
25
+ [:procedures_file, :schema_file].each do |file|
26
+ lines = open(File.join(File.dirname(__FILE__), ActiveRecord::Base.configurations['rails_sql_views_unittest'][file])).readlines
27
+ conn.execute(lines.to_s)
28
+ end
29
+ conn.reconnect!
@@ -0,0 +1,15 @@
1
+ create or replace
2
+ procedure dropTable (tab_name varchar2) as
3
+ this_tab_name user_tables.table_name%type;
4
+ cursor table_cur is
5
+ select table_name
6
+ from user_tables
7
+ where table_name = tab_name;
8
+ begin
9
+ open table_cur;
10
+ loop
11
+ fetch table_cur into this_tab_name;
12
+ exit when table_cur%notfound;
13
+ execute immediate 'drop table "' || this_tab_name || '"';
14
+ end loop;
15
+ end;
@@ -0,0 +1,39 @@
1
+ BEGIN
2
+ dropTable('PEOPLE');
3
+ EXECUTE IMMEDIATE 'create table people (
4
+ id integer primary key,
5
+ first_name varchar2(255),
6
+ last_name varchar2(255),
7
+ ssn varchar2(64),
8
+ address_id integer
9
+ )';
10
+
11
+ dropTable('PEOPLE2');
12
+ EXECUTE IMMEDIATE 'create table people2 (
13
+ id integer primary key,
14
+ first_name varchar2(255),
15
+ last_name varchar2(255),
16
+ ssn varchar2(64)
17
+ )';
18
+
19
+ dropTable('PLACES');
20
+ EXECUTE IMMEDIATE 'create table places (
21
+ id integer primary key,
22
+ address varchar2(2000),
23
+ city varchar2(255),
24
+ cstate varchar2(255),
25
+ country char(2)
26
+ )';
27
+
28
+ dropTable('ITEMS');
29
+ EXECUTE IMMEDIATE 'create table items (
30
+ id integer primary key,
31
+ person_id integer
32
+ )';
33
+
34
+ dropTable('ITEMS_PEOPLE');
35
+ EXECUTE IMMEDIATE 'create table items_people (
36
+ person_id integer,
37
+ item_id integer
38
+ )';
39
+ END;
@@ -0,0 +1,4 @@
1
+ class Item < ActiveRecord::Base
2
+ belongs_to :person
3
+ has_and_belongs_to_many :people
4
+ end
@@ -0,0 +1,5 @@
1
+ class Person < ActiveRecord::Base
2
+ belongs_to :address, :class_name => 'Place', :foreign_key => :address_id
3
+ has_many :owned_items, :class_name => 'Item'
4
+ has_and_belongs_to_many :shared_items, :class_name => 'Item'
5
+ end
@@ -0,0 +1,3 @@
1
+ class Person2 < ActiveRecord::Base
2
+ set_table_name :people2
3
+ end
@@ -0,0 +1,2 @@
1
+ class Place < ActiveRecord::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_record/view'
2
+
3
+ class VPerson < ActiveRecord::View
4
+ end
@@ -0,0 +1,3 @@
1
+ class VProfile < ActiveRecord::Base
2
+
3
+ end
@@ -0,0 +1,57 @@
1
+ # This file is auto-generated from the current state of the database. Instead of editing this file,
2
+ # please use the migrations feature of Active Record to incrementally modify your database, and
3
+ # then regenerate this schema definition.
4
+ #
5
+ # Note that this schema.rb definition is the authoritative source for your database schema. If you need
6
+ # to create the application database on another system, you should be using db:schema:load, not running
7
+ # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
8
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
9
+ #
10
+ # It's strongly recommended to check this file into your version control system.
11
+
12
+ ActiveRecord::Schema.define(:version => 0) do
13
+
14
+ create_table "items", :force => true do |t|
15
+ t.integer "person_id"
16
+ end
17
+
18
+ create_table "items_people", :id => false, :force => true do |t|
19
+ t.integer "person_id"
20
+ t.integer "item_id"
21
+ end
22
+
23
+ create_table "people", :force => true do |t|
24
+ t.string "first_name"
25
+ t.string "last_name"
26
+ t.string "ssn", :limit => 64
27
+ t.integer "address_id"
28
+ end
29
+
30
+ create_table "people2", :force => true do |t|
31
+ t.string "first_name"
32
+ t.string "last_name"
33
+ t.string "ssn", :limit => 64
34
+ end
35
+
36
+ create_table "places", :force => true do |t|
37
+ t.text "address"
38
+ t.string "city"
39
+ t.string "cstate"
40
+ t.string "country", :limit => 2
41
+ end
42
+
43
+ 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|
44
+ v.column :id
45
+ v.column :f_name
46
+ v.column :l_name
47
+ v.column :social_security
48
+ v.column :address_id
49
+ end
50
+
51
+ 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|
52
+ v.column :first_name
53
+ v.column :last_name
54
+ v.column :ssn
55
+ end
56
+
57
+ end
@@ -0,0 +1,58 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # Note that this schema.rb definition is the authoritative source for your
6
+ # database schema. If you need to create the application database on another
7
+ # system, you should be using db:schema:load, not running all the migrations
8
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
9
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
10
+ #
11
+ # It's strongly recommended to check this file into your version control system.
12
+
13
+ ActiveRecord::Schema.define(:version => 0) do
14
+
15
+ create_table "items", :force => true do |t|
16
+ t.integer "person_id"
17
+ end
18
+
19
+ create_table "items_people", :id => false, :force => true do |t|
20
+ t.integer "person_id"
21
+ t.integer "item_id"
22
+ end
23
+
24
+ create_table "people", :force => true do |t|
25
+ t.string "first_name"
26
+ t.string "last_name"
27
+ t.string "ssn", :limit => 64
28
+ t.integer "address_id"
29
+ end
30
+
31
+ create_table "people2", :force => true do |t|
32
+ t.string "first_name"
33
+ t.string "last_name"
34
+ t.string "ssn", :limit => 64
35
+ end
36
+
37
+ create_table "places", :force => true do |t|
38
+ t.text "address"
39
+ t.string "city"
40
+ t.string "cstate"
41
+ t.string "country", :limit => 2
42
+ end
43
+
44
+ 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|
45
+ v.column :id
46
+ v.column :f_name
47
+ v.column :l_name
48
+ v.column :social_security
49
+ v.column :address_id
50
+ end
51
+
52
+ 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|
53
+ v.column :first_name
54
+ v.column :last_name
55
+ v.column :ssn
56
+ end
57
+
58
+ end
@@ -0,0 +1,51 @@
1
+ # This file is auto-generated from the current state of the database. Instead of editing this file,
2
+ # please use the migrations feature of Active Record to incrementally modify your database, and
3
+ # then regenerate this schema definition.
4
+ #
5
+ # Note that this schema.rb definition is the authoritative source for your database schema. If you need
6
+ # to create the application database on another system, you should be using db:schema:load, not running
7
+ # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
8
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
9
+ #
10
+ # It's strongly recommended to check this file into your version control system.
11
+
12
+ ActiveRecord::Schema.define(:version => 0) do
13
+
14
+ create_table "items", :force => true do |t|
15
+ t.integer "person_id"
16
+ end
17
+
18
+ create_table "items_people", :id => false, :force => true do |t|
19
+ t.integer "person_id"
20
+ t.integer "item_id"
21
+ end
22
+
23
+ create_table "people", :force => true do |t|
24
+ t.string "first_name"
25
+ t.string "last_name"
26
+ t.string "ssn", :limit => 64
27
+ t.integer "address_id"
28
+ end
29
+
30
+ create_table "people2", :force => true do |t|
31
+ t.string "first_name"
32
+ t.string "last_name"
33
+ t.string "ssn", :limit => 64
34
+ end
35
+
36
+ create_table "places", :force => true do |t|
37
+ t.text "address"
38
+ t.string "city"
39
+ t.string "cstate"
40
+ t.string "country", :limit => 2
41
+ end
42
+
43
+ create_view "v_people", "SELECT people.id, people.first_name AS f_name, people.last_name AS l_name, people.ssn AS social_security, people.address_id FROM people;", :force => true do |v|
44
+ v.column :id
45
+ v.column :f_name
46
+ v.column :l_name
47
+ v.column :social_security
48
+ v.column :address_id
49
+ end
50
+
51
+ end