Skip to content

Commit

Permalink
[oracle] should support WITH statements (as well)
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Jan 22, 2013
1 parent c15c477 commit 01b0c8f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
52 changes: 24 additions & 28 deletions lib/arjdbc/oracle/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def sql_literal?(value)
end

def ora_insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = []) #:nodoc:
if (id_value && !sql_literal?(id_value)) || pk.nil?
if (id_value && ! sql_literal?(id_value)) || pk.nil?
# Pre-assigned id or table without a primary key
# Presence of #to_sql means an Arel literal bind variable
# that should use #execute_id_insert below
Expand All @@ -206,15 +206,6 @@ def indexes(table, name = nil)
@connection.indexes(table, name, @connection.connection.meta_data.user_name)
end

def _execute(sql, name = nil)
case sql.strip
when /\A\(?\s*(select|show)/i then
@connection.execute_query(sql)
else
@connection.execute_update(sql)
end
end

def modify_types(tp)
tp[:primary_key] = "NUMBER(38) NOT NULL PRIMARY KEY"
tp[:integer] = { :name => "NUMBER", :limit => 38 }
Expand Down Expand Up @@ -277,19 +268,16 @@ def structure_dump #:nodoc:
select_all("select table_name from user_tables").inject(s) do |structure, table|
ddl = "create table #{table.to_a.first.last} (\n "
cols = select_all(%Q{
select column_name, data_type, data_length, data_precision, data_scale, data_default, nullable
from user_tab_columns
where table_name = '#{table.to_a.first.last}'
order by column_id
}).map do |row|
row = row.inject({}) do |h,args|
h[args[0].downcase] = args[1]
h
end
select column_name, data_type, data_length, data_precision, data_scale, data_default, nullable
from user_tab_columns
where table_name = '#{table.to_a.first.last}'
order by column_id
}).map do |row|
row = row.inject({}) { |h, args| h[ args[0].downcase ] = args[1]; h }
col = "#{row['column_name'].downcase} #{row['data_type'].downcase}"
if row['data_type'] =='NUMBER' and !row['data_precision'].nil?
if row['data_type'] == 'NUMBER' and ! row['data_precision'].nil?
col << "(#{row['data_precision'].to_i}"
col << ",#{row['data_scale'].to_i}" if !row['data_scale'].nil?
col << ",#{row['data_scale'].to_i}" if ! row['data_scale'].nil?
col << ')'
elsif row['data_type'].include?('CHAR')
col << "(#{row['data_length'].to_i})"
Expand Down Expand Up @@ -414,7 +402,22 @@ def quoted_false #:nodoc:
'0'
end

def _execute(sql, name = nil)
if self.class.select?(sql)
@connection.execute_query(sql)
else
@connection.execute_update(sql)
end
end

private

def select(sql, name = nil, binds = [])
records = execute(sql, name, binds)
records.each { |col| col.delete('raw_rnum_') }
records
end

# In Oracle, schemas are usually created under your username:
# http://www.oracle.com/technology/obe/2day_dba/schema/schema.htm
# But allow separate configuration as "schema:" anyway (GH #53)
Expand All @@ -426,13 +429,6 @@ def oracle_schema
end
end

def select(sql, name = nil, binds = [])
records = execute(sql, name, binds)
records.each do |col|
col.delete('raw_rnum_')
end
records
end
end
end

2 changes: 0 additions & 2 deletions test/db/oracle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@
end

ActiveRecord::Base.establish_connection(config)

#ActiveRecord::Base.connection.execute "SELECT 1"
17 changes: 17 additions & 0 deletions test/oracle_simple_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@ def test_sequences_are_not_cached
assert e1.id != e2.id
end
end

def test_find_by_sql_WITH_statement
user = User.create! :login => 'ferko'
Entry.create! :title => 'aaa', :user_id => user.id
entries = Entry.find_by_sql '' +
' WITH EntryLogin (title, login) AS ' +
' ( ' +
' SELECT e.title, u.login ' +
' FROM entries e INNER JOIN users u ON e.user_id = u.id ' +
' ) ' +
' ' +
' SELECT * FROM EntryLogin ORDER BY title ASC '
assert entries.first
assert entries.first.title
assert entries.first.login
end

end

0 comments on commit 01b0c8f

Please sign in to comment.