Skip to content

Commit

Permalink
[oracle ] (MATRIALIZED) VIEWS/SYNONYMS should be usable table names
Browse files Browse the repository at this point in the history
on table_exists? - now works as expected due 5f2deb4
  • Loading branch information
kares committed Feb 20, 2013
1 parent 5f2deb4 commit d190960
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
7 changes: 5 additions & 2 deletions lib/arjdbc/oracle/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 62 additions & 16 deletions test/db/oracle/specific_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'
require 'jdbc_common'
require 'db/oracle'
require 'simple'

class OracleSpecificTest < Test::Unit::TestCase
include MigrationSetup
Expand All @@ -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
Expand Down Expand Up @@ -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}"
Expand Down

0 comments on commit d190960

Please sign in to comment.