From d19096028aa859656ad27c84e90275d2e5d36d5f Mon Sep 17 00:00:00 2001 From: kares Date: Wed, 20 Feb 2013 12:40:24 +0100 Subject: [PATCH] [oracle ] (MATRIALIZED) VIEWS/SYNONYMS should be usable table names on table_exists? - now works as expected due 5f2deb4d5c9d5ccd0e --- lib/arjdbc/oracle/adapter.rb | 7 ++- test/db/oracle/specific_test.rb | 78 ++++++++++++++++++++++++++------- 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/lib/arjdbc/oracle/adapter.rb b/lib/arjdbc/oracle/adapter.rb index c1cb06f31..e57499253 100644 --- a/lib/arjdbc/oracle/adapter.rb +++ b/lib/arjdbc/oracle/adapter.rb @@ -419,16 +419,19 @@ def extract_order_columns(order_by) end private :extract_order_columns - def tables + def tables # :nodoc: @connection.tables(nil, oracle_schema) end # NOTE: better to use current_schema instead of the configured one ?! - def columns(table_name, name = nil) # :nodoc: @connection.columns_internal(table_name.to_s, name, oracle_schema) end + def tablespace(table_name) + select_value "SELECT tablespace_name FROM user_tables WHERE table_name='#{table_name.to_s.upcase}'" + end + # QUOTING ================================================== # See ACTIVERECORD_JDBC-33 for details -- better to not quote diff --git a/test/db/oracle/specific_test.rb b/test/db/oracle/specific_test.rb index bc89af06f..a4bca9d1c 100644 --- a/test/db/oracle/specific_test.rb +++ b/test/db/oracle/specific_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -require 'jdbc_common' require 'db/oracle' +require 'simple' class OracleSpecificTest < Test::Unit::TestCase include MigrationSetup @@ -21,17 +21,29 @@ def self.startup java_connection.createStatement.execute " INSERT INTO DEFAULT_NUMBERS (ID, VALUE, DATUM, FPOINT, VALUE2) VALUES (1, 0.076, TIMESTAMP'2009-11-05 00:00:00', 1000.01, 1234)" - java_connection.createStatement.execute "CREATE SYNONYM POSTS FOR ENTRIES" MigrationSetup.setup! + + java_connection.createStatement.execute "CREATE SYNONYM POSTS FOR ENTRIES" + + java_connection.createStatement.execute "CREATE VIEW GOOD_ENTRIES AS SELECT * FROM entries WHERE rating >= 1.0" + + # MV will be populated IMMEDIATELY by default : + user = User.create! :login => 'sandokan' + Entry.create! :title => 'SANDOKAAAN!', :rating => 4.95, :user_id => user.id + + java_connection.createStatement.execute "CREATE MATERIALIZED VIEW USER_ENTRIES AS " + + "SELECT e.id, e.title, e.user_id FROM users u, entries e WHERE e.user_id = u.id " end def self.shutdown - MigrationSetup.teardown! - java_connection = @@java_connection java_connection.createStatement.execute "DROP TABLE DEFAULT_NUMBERS" java_connection.createStatement.execute "DROP SYNONYM POSTS" + java_connection.createStatement.execute "DROP VIEW GOOD_ENTRIES" + java_connection.createStatement.execute "DROP MATERIALIZED VIEW USER_ENTRIES" + + MigrationSetup.teardown! @@java_connection.close super @@ -91,26 +103,60 @@ def test_load_null_date obj = DefaultNumber.first assert obj.datum.nil? end + + test "synonym table exists" do + assert_true ActiveRecord::Base.connection.table_exists? 'posts' + assert_true ActiveRecord::Base.connection.table_exists? 'POSTS' + end + + test "view table exists (despite not being among reported tables)" do + assert_true ActiveRecord::Base.connection.table_exists? 'GOOD_ENTRIES' + assert_false ActiveRecord::Base.connection.tables.include?('GOOD_ENTRIES') + end - def test_model_access_by_synonym + test "materialized view table exists" do + assert_true ActiveRecord::Base.connection.table_exists? 'USER_ENTRIES' + end + + test 'model access by synonym' do @klass = Class.new(ActiveRecord::Base) @klass.table_name = "POSTS" # alias entry_columns = Entry.columns_hash - @klass.columns.each do |c| - assert ec = entry_columns[c.name] - assert_equal ec.sql_type, c.sql_type - assert_equal ec.type, c.type + @klass.columns.each do |column| + assert entry_column = entry_columns[column.name] + assert_equal entry_column.sql_type, column.sql_type + assert_equal entry_column.type, column.type end end - # - - def test_oracle_identifier_lengths - assert_equal 30, connection.table_alias_length - assert_equal 30, connection.table_name_length - assert_equal 30, connection.index_name_length - assert_equal 30, connection.column_name_length + class Post < ActiveRecord::Base; end + + test "post synonym" do + Entry.create! :title => 'first', :content => 'SOME CONTENT', :rating => 0.5 + assert_equal Entry.count, Post.count + assert_equal Entry.first.title, Post.first.title end + + test 'model access by materialized view' do + @klass = Class.new(ActiveRecord::Base) + @klass.table_name = "USER_ENTRIES" # MV + entry_columns = Entry.columns_hash + + assert_equal 3, @klass.columns.size + assert column = @klass.columns_hash['id'] + assert_equal entry_columns['id'].sql_type, column.sql_type + assert_equal entry_columns['id'].type, column.type + assert column = @klass.columns_hash['title'] + assert_equal entry_columns['title'].sql_type, column.sql_type + assert_equal entry_columns['title'].type, column.type + assert column = @klass.columns_hash['user_id'] + assert_equal entry_columns['user_id'].sql_type, column.sql_type + assert_equal entry_columns['user_id'].type, column.type + + assert @klass.first # sandokan was here ! + end + + # def test_current_user puts "ORA current_user: #{connection.current_user}"