diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-24 14:01:35 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-24 14:01:35 +0000 |
commit | 14ea312808599001674784a839bf7cf5a8eea872 (patch) | |
tree | 9875456d3eba2ceb2f86909ee280333e2dfefa94 /activerecord/lib/active_record | |
parent | 1ae0a53d861c43dff8883fc956a08404c23f180c (diff) | |
download | rails-14ea312808599001674784a839bf7cf5a8eea872.tar.gz rails-14ea312808599001674784a839bf7cf5a8eea872.tar.bz2 rails-14ea312808599001674784a839bf7cf5a8eea872.zip |
Made Oracle a first-class connection adapter by adhering closer to idiomatic Oracle style #1798 [The Gang of Oracles]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1912 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
8 files changed, 133 insertions, 33 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 20ccd9a623..84bf0cdcff 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -569,6 +569,11 @@ module ActiveRecord #:nodoc: "type" end + # Defines the sequence_name (for Oracle) -- can be overridden in subclasses. + def sequence_name + "#{table_name}_seq" + end + # Sets the table name to use to the given value, or (if the value # is nil or false) to the value returned by the given block. # @@ -612,6 +617,25 @@ module ActiveRecord #:nodoc: end alias :inheritance_column= :set_inheritance_column + # Sets the name of the sequence to use when generating ids to the given + # value, or (if the value is nil or false) to the value returned by the + # given block. Currently useful only when using Oracle, which requires + # explicit sequences. + # + # Setting the sequence name when using other dbs will have no effect. + # If a sequence name is not explicitly set when using Oracle, it will + # default to the commonly used pattern of: #{table_name}_seq + # + # Example: + # + # class Project < ActiveRecord::Base + # set_sequence_name "projectseq" # default would have been "project_seq" + # end + def set_sequence_name( value=nil, &block ) + define_attr_method :sequence_name, value, &block + end + alias :sequence_name= :set_sequence_name + # Turns the +table_name+ back into a class name following the reverse rules of +table_name+. def class_name(table_name = table_name) # :nodoc: # remove any prefix and/or suffix from the table name @@ -1193,7 +1217,7 @@ module ActiveRecord #:nodoc: "(#{quoted_column_names.join(', ')}) " + "VALUES(#{attributes_with_quotes.values.join(', ')})", "#{self.class.name} Create", - self.class.primary_key, self.id + self.class.primary_key, self.id, self.class.sequence_name ) @new_record = false diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index ff34c63fa1..7a680c3101 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -279,7 +279,7 @@ module ActiveRecord def columns(table_name, name = nil) end # Returns the last auto-generated ID from the affected table. - def insert(sql, name = nil, pk = nil, id_value = nil) end + def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) end # Executes the update statement and returns the number of rows affected. def update(sql, name = nil) end diff --git a/activerecord/lib/active_record/connection_adapters/db2_adapter.rb b/activerecord/lib/active_record/connection_adapters/db2_adapter.rb index a80a227a6d..b31e205fc8 100644 --- a/activerecord/lib/active_record/connection_adapters/db2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/db2_adapter.rb @@ -44,7 +44,7 @@ begin select(sql, name).first end - def insert(sql, name = nil, pk = nil, id_value = nil) + def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) execute(sql, name = nil) id_value || last_insert_id end diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 71329705b7..6dbf315396 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -109,7 +109,7 @@ module ActiveRecord columns end - def insert(sql, name = nil, pk = nil, id_value = nil) + def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) execute(sql, name = nil) id_value || @connection.insert_id end diff --git a/activerecord/lib/active_record/connection_adapters/oci_adapter.rb b/activerecord/lib/active_record/connection_adapters/oci_adapter.rb index 3988d1aa96..194e5d0062 100644 --- a/activerecord/lib/active_record/connection_adapters/oci_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/oci_adapter.rb @@ -15,7 +15,7 @@ # Do what you want with this code, at your own peril, but if any significant portion of my code # remains then please acknowledge my contribution. # Copyright 2005 Graham Jenkins -# $Revision: 1.2 $ + require 'active_record/connection_adapters/abstract_adapter' begin @@ -77,8 +77,8 @@ begin # It has also been tested against a 9i database. # # Usage notes: - # * Key generation uses a sequence "rails_sequence" for all tables. (I couldn't find a simple - # and safe way of passing table-specific sequence information to the adapter.) + # * Key generation assumes a "${table_name}_seq" sequence is available for all tables; the + # sequence name can be changed using ActiveRecord::Base.set_sequence_name # * Oracle uses DATE or TIMESTAMP datatypes for both dates and times. Consequently I have had to # resort to some hacks to get data converted to Date or Time in Ruby. # If the column_name ends in _time it's created as a Ruby Time. Else if the @@ -106,31 +106,82 @@ begin def quote(value, column = nil) if column and column.type == :binary then %Q{empty_#{ column.sql_type }()} else case value - when String then %Q{'#{quote_string(value)}'} + when String then %Q{'#{quote_string(value)}'} when NilClass then 'null' when TrueClass then '1' when FalseClass then '0' - when Numeric then value.to_s + when Numeric then value.to_s when Date, Time then %Q{'#{value.strftime("%Y-%m-%d %H:%M:%S")}'} - else %Q{'#{quote_string(value.to_yaml)}'} + else %Q{'#{quote_string(value.to_yaml)}'} + end + end + end + + # camelCase column names need to be quoted; not that anyone using Oracle + # would really do this, but handling this case means we pass the test... + def quote_column_name(name) + name =~ /[A-Z]/ ? "\"#{name}\"" : name + end + + def structure_dump + s = select_all("select sequence_name from user_sequences").inject("") do |structure, seq| + structure << "create sequence #{seq.to_a.first.last};\n\n" + end + + 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| + col = "#{row['column_name'].downcase} #{row['data_type'].downcase}" + 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 << ')' + elsif row['data_type'].include?('CHAR') + col << "(#{row['data_length'].to_i})" + end + col << " default #{row['data_default']}" if !row['data_default'].nil? + col << ' not null' if row['nullable'] == 'N' + col end + ddl << cols.join(",\n ") + ddl << ");\n\n" + structure << ddl + end + end + + def structure_drop + s = select_all("select sequence_name from user_sequences").inject("") do |drop, seq| + drop << "drop sequence #{seq.to_a.first.last};\n\n" + end + + select_all("select table_name from user_tables").inject(s) do |drop, table| + drop << "drop table #{table.to_a.first.last} cascade constraints;\n\n" end end def select_all(sql, name = nil) offset = sql =~ /OFFSET (\d+)$/ ? $1.to_i : 0 sql, limit = $1, $2.to_i if sql =~ /(.*)(?: LIMIT[= ](\d+))(\s*OFFSET \d+)?$/ + if limit sql = "select * from (select raw_sql_.*, rownum raw_rnum_ from (#{sql}) raw_sql_ where rownum <= #{offset+limit}) where raw_rnum_ > #{offset}" elsif offset > 0 sql = "select * from (select raw_sql_.*, rownum raw_rnum_ from (#{sql}) raw_sql_) where raw_rnum_ > #{offset}" end + cursor = log(sql, name) { @connection.exec sql } - cols = cursor.get_col_names.map { |x| x.downcase } + cols = cursor.get_col_names.map { |x| oci_downcase(x) } rows = [] + while row = cursor.fetch hash = Hash.new - cols.each_with_index { |col, i| + + cols.each_with_index do |col, i| hash[col] = case row[i] when OCI8::LOB name == 'Writable Large Object' ? row[i]: row[i].read @@ -139,9 +190,11 @@ begin row[i].to_date : row[i].to_time else row[i] end unless col == 'raw_rnum_' - } + end + rows << hash end + rows ensure cursor.close if cursor @@ -153,25 +206,34 @@ begin end def columns(table_name, name = nil) - cols = select_all(%Q{ + select_all(%Q{ select column_name, data_type, data_default, data_length, data_scale - from user_tab_columns where table_name = '#{table_name.upcase}'} - ).map { |row| - OCIColumn.new row['column_name'].downcase, row['data_default'], - row['data_length'], row['data_type'], row['data_scale'] - } - cols + from user_catalog cat, user_synonyms syn, all_tab_columns col + where cat.table_name = '#{table_name.upcase}' + and syn.synonym_name (+)= cat.table_name + and col.owner = nvl(syn.table_owner, user) + and col.table_name = nvl(syn.table_name, cat.table_name)} + ).map do |row| + OCIColumn.new( + oci_downcase(row['column_name']), + row['data_default'], + row['data_length'], + row['data_type'], + row['data_scale'] + ) + end end - def insert(sql, name = nil, pk = nil, id_value = nil) + def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) if pk.nil? # Who called us? What does the sql look like? No idea! execute sql, name elsif id_value # Pre-assigned id log(sql, name) { @connection.exec sql } else # Assume the sql contains a bind-variable for the id - id_value = select_one("select rails_sequence.nextval id from dual")['id'] + id_value = select_one("select #{sequence_name}.nextval id from dual")['id'] log(sql, name) { @connection.exec sql, id_value } end + id_value end @@ -201,18 +263,31 @@ begin def adapter_name() 'OCI' end + + private + # Oracle column names by default are case-insensitive, but treated as upcase; + # for neatness, we'll downcase within Rails. EXCEPT that folks CAN quote + # their column names when creating Oracle tables, which makes then case-sensitive. + # I don't know anybody who does this, but we'll handle the theoretical case of a + # camelCase column name. I imagine other dbs handle this different, since there's a + # unit test that's currently failing test_oci. + def oci_downcase(column_name) + column_name =~ /[a-z]/ ? column_name : column_name.downcase + end end end end module ActiveRecord class Base - def self.oci_connection(config) #:nodoc: - conn = OCI8.new config[:username], config[:password], config[:host] - conn.exec %q{alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'} - conn.exec %q{alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS'} - conn.autocommit = true - ConnectionAdapters::OCIAdapter.new conn, logger + class << self + def oci_connection(config) #:nodoc: + conn = OCI8.new config[:username], config[:password], config[:host] + conn.exec %q{alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'} + conn.exec %q{alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS'} + conn.autocommit = true + ConnectionAdapters::OCIAdapter.new conn, logger + end end alias :attributes_with_quotes_pre_oci :attributes_with_quotes #:nodoc: @@ -231,9 +306,10 @@ begin # After setting large objects to empty, select the OCI8::LOB and write back the data def write_lobs() #:nodoc: - if connection.class == ConnectionAdapters::OCIAdapter + if connection.is_a?(ConnectionAdapters::OCIAdapter) self.class.columns.select { |c| c.type == :binary }.each { |c| - break unless value = self[c.name] + value = self[c.name] + next if value.nil? || (value == '') lob = connection.select_one( "select #{ c.name} from #{ self.class.table_name } WHERE #{ self.class.primary_key} = #{quote(id)}", 'Writable Large Object' diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index b7ba2431c4..25539e2617 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -95,7 +95,7 @@ module ActiveRecord end end - def insert(sql, name = nil, pk = nil, id_value = nil) + def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) execute(sql, name) table = sql.split(" ", 4)[2] return id_value || last_insert_id(table, pk) diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 212e11de4e..5e3ef53c82 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -123,7 +123,7 @@ module ActiveRecord @connection.changes end - def insert(sql, name = nil, pk = nil, id_value = nil) + def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) execute(sql, name = nil) id_value || @connection.last_insert_row_id end diff --git a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb index 7b68283e57..7d4c00b85d 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb @@ -204,7 +204,7 @@ module ActiveRecord columns end - def insert(sql, name = nil, pk = nil, id_value = nil) + def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) begin table_name = get_table_name(sql) col = get_identity_column(table_name) |