Skip to content

Commit 59be77d

Browse files
Steve Halaszaeden
authored andcommitted
passing tests for DB=oracle_enhanced
1 parent 05646c1 commit 59be77d

File tree

8 files changed

+170
-8
lines changed

8 files changed

+170
-8
lines changed

lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def drop_table_with_cascade(table_name, options = {})
6969
# database documentation to determine what drop behaviors are available.
7070
def drop_view(name, options={})
7171
if supports_views?
72-
drop_sql = "DROP VIEW #{name}"
72+
drop_sql = "DROP VIEW #{quote_table_name(name)}"
7373
drop_sql << " #{options[:drop_behavior]}" if options[:drop_behavior]
7474
execute drop_sql
7575
end

lib/rails_sql_views/connection_adapters/oracleenhanced_adapter.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
module RailsSqlViews
22
module ConnectionAdapters
33
module OracleEnhancedAdapter
4+
def self.included(base)
5+
base.alias_method_chain :tables, :views_included
6+
end
47
# Returns true as this adapter supports views.
58
def supports_views?
69
true
710
end
811

12+
def tables_with_views_included(name = nil)
13+
tables = []
14+
sql = " SELECT TABLE_NAME FROM USER_TABLES
15+
UNION
16+
SELECT VIEW_NAME AS TABLE_NAME FROM USER_VIEWS"
17+
cursor = execute(sql, name)
18+
while row = cursor.fetch
19+
tables << row[0].downcase
20+
end
21+
tables
22+
end
23+
924
def base_tables(name = nil) #:nodoc:
1025
tables = []
1126
cursor = execute("SELECT TABLE_NAME FROM USER_TABLES", name)
1227
while row = cursor.fetch
13-
tables << row[0]
28+
tables << row[0].downcase
1429
end
1530
tables
1631
end
@@ -20,13 +35,14 @@ def views(name = nil) #:nodoc:
2035
views = []
2136
cursor = execute("SELECT VIEW_NAME FROM USER_VIEWS", name)
2237
while row = cursor.fetch
23-
views << row[0]
38+
views << row[0].downcase
2439
end
2540
views
2641
end
2742

2843
# Get the view select statement for the specified table.
2944
def view_select_statement(view, name=nil)
45+
view.upcase!
3046
cursor = execute("SELECT TEXT FROM USER_VIEWS WHERE VIEW_NAME = '#{view}'", name)
3147
if row = cursor.fetch
3248
return row[0]

lib/rails_sql_views/connection_adapters/postgresql_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module RailsSqlViews
22
module ConnectionAdapters
33
module PostgreSQLAdapter
44
def self.included(base)
5-
base.alias_method_chain :tables, :views_included unless method_defined?(:tables_with_views_included)
5+
base.alias_method_chain :tables, :views_included
66
end
77
# Returns true as this adapter supports views.
88
def supports_views?
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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!
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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", :precision => 38, :scale => 0
16+
end
17+
18+
create_table "items_people", :id => false, :force => true do |t|
19+
t.integer "person_id", :precision => 38, :scale => 0
20+
t.integer "item_id", :precision => 38, :scale => 0
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", :precision => 38, :scale => 0
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.string "address", :limit => 2000
38+
t.string "city"
39+
t.string "cstate"
40+
t.string "country", :limit => 2
41+
end
42+
43+
create_view "v_people", "select id, first_name, last_name, ssn, 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

test/schema_dumper_test.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33

44
class SchemaDumperTest < Test::Unit::TestCase
55
def setup
6-
ActiveRecord::Base.connection.execute('drop view if exists v_people')
7-
ActiveRecord::Base.connection.execute('drop view if exists v_profile')
6+
teardown
87
end
98
def teardown
10-
ActiveRecord::Base.connection.execute('drop view if exists v_people')
11-
ActiveRecord::Base.connection.execute('drop view if exists v_profile')
9+
['V_PEOPLE', 'V_PROFILE'].each do |view|
10+
if ActiveRecord::Base.connection.adapter_name == 'OracleEnhanced'
11+
ActiveRecord::Base.connection.execute("
12+
DECLARE
13+
CURSOR C1 is SELECT view_name FROM user_views where view_name = '#{view}';
14+
BEGIN
15+
FOR I IN C1 LOOP
16+
EXECUTE IMMEDIATE 'DROP VIEW '||I.view_name||'';
17+
END LOOP;
18+
END;
19+
");
20+
else
21+
ActiveRecord::Base.connection.execute("drop view if exists #{view}")
22+
end
23+
end
1224
end
1325
def test_view
1426
create_people_view

0 commit comments

Comments
 (0)