Permalink
Browse files

Renamed the "oci" adapter to "oracle", but kept the old name as an al…

…ias (closes #4017) [schoenm@earthlink.net]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3718 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 7056fc6 commit 631181ca18d2a21c3e72039b0c7f6c5a0b2fc05a @dhh dhh committed Mar 1, 2006
@@ -1,5 +1,7 @@
*SVN*
+* Renamed the "oci" adapter to "oracle", but kept the old name as an alias #4017 [schoenm@earthlink.net]
+
* Fixed that Base.save should always return false if the save didn't succeed, including if it has halted by before_save's #1861, #2477 [DHH]
* Speed up class -> connection caching and stale connection verification. #3979 [Stefan Kaes]
@@ -27,7 +27,7 @@ task :default => [ :test_mysql, :test_sqlite, :test_postgresql ]
# Run the unit tests
-for adapter in %w( mysql postgresql sqlite sqlite3 firebird sqlserver sqlserver_odbc db2 oci )
+for adapter in %w( mysql postgresql sqlite sqlite3 firebird sqlserver sqlserver_odbc db2 oracle )
Rake::TestTask.new("test_#{adapter}") { |t|
t.libs << "test" << "test/connections/native_#{adapter}"
t.pattern = "test/*_test{,_#{adapter}}.rb"
@@ -68,7 +68,7 @@
end
unless defined?(RAILS_CONNECTION_ADAPTERS)
- RAILS_CONNECTION_ADAPTERS = %w(mysql postgresql sqlite firebird sqlserver db2 oci)
+ RAILS_CONNECTION_ADAPTERS = %w(mysql postgresql sqlite firebird sqlserver db2 oracle)
end
RAILS_CONNECTION_ADAPTERS.each do |adapter|
@@ -1,4 +1,4 @@
-# oci_adapter.rb -- ActiveRecord adapter for Oracle 8i, 9i, 10g
+# oracle_adapter.rb -- ActiveRecord adapter for Oracle 8i, 9i, 10g
#
# Original author: Graham Jenkins
#
@@ -30,18 +30,24 @@
module ActiveRecord
class Base
- def self.oci_connection(config) #:nodoc:
+ def self.oracle_connection(config) #:nodoc:
# Use OCI8AutoRecover instead of normal OCI8 driver.
- ConnectionAdapters::OCIAdapter.new OCI8AutoRecover.new(config), logger
+ ConnectionAdapters::OracleAdapter.new OCI8AutoRecover.new(config), logger
+ end
+
+ # for backwards-compatibility
+ def self.oci_connection(config) #:nodoc:
+ config[:database] = config[:host]
+ self.oracle_connection(config)
end
# Enable the id column to be bound into the sql later, by the adapter's insert method.
# This is preferable to inserting the hard-coded value here, because the insert method
# needs to know the id value explicitly.
- alias :attributes_with_quotes_pre_oci :attributes_with_quotes #:nodoc:
+ alias :attributes_with_quotes_pre_oracle :attributes_with_quotes #:nodoc:
def attributes_with_quotes(creating = true) #:nodoc:
- aq = attributes_with_quotes_pre_oci creating
- if connection.class == ConnectionAdapters::OCIAdapter
+ aq = attributes_with_quotes_pre_oracle creating
+ if connection.class == ConnectionAdapters::OracleAdapter
aq[self.class.primary_key] = ":id" if creating && aq[self.class.primary_key].nil?
end
aq
@@ -51,7 +57,7 @@ def attributes_with_quotes(creating = true) #:nodoc:
# and write back the data.
after_save :write_lobs
def write_lobs() #:nodoc:
- if connection.is_a?(ConnectionAdapters::OCIAdapter)
+ if connection.is_a?(ConnectionAdapters::OracleAdapter)
self.class.columns.select { |c| c.type == :binary }.each { |c|
value = self[c.name]
next if value.nil? || (value == '')
@@ -68,7 +74,7 @@ def write_lobs() #:nodoc:
module ConnectionAdapters #:nodoc:
- class OCIColumn < Column #:nodoc:
+ class OracleColumn < Column #:nodoc:
attr_reader :sql_type
# overridden to add the concept of scale, required to differentiate
@@ -153,15 +159,15 @@ def guess_date_or_time(value)
# is supported in Oracle9i and later. You will need to use #finder_sql for
# has_and_belongs_to_many associations to run against Oracle8.
#
- # Options:
+ # Required parameters:
#
- # * <tt>:username</tt> -- Defaults to root
- # * <tt>:password</tt> -- Defaults to nothing
- # * <tt>:host</tt> -- Defaults to localhost
- class OCIAdapter < AbstractAdapter
+ # * <tt>:username</tt>
+ # * <tt>:password</tt>
+ # * <tt>:database</tt>
+ class OracleAdapter < AbstractAdapter
def adapter_name #:nodoc:
- 'OCI'
+ 'Oracle'
end
def supports_migrations? #:nodoc:
@@ -172,7 +178,7 @@ def native_database_types #:nodoc
{
:primary_key => "NUMBER(38) NOT NULL PRIMARY KEY",
:string => { :name => "VARCHAR2", :limit => 255 },
- :text => { :name => "LONG" },
+ :text => { :name => "BLOB" },
:integer => { :name => "NUMBER", :limit => 38 },
:float => { :name => "NUMBER" },
:datetime => { :name => "DATE" },
@@ -214,7 +220,8 @@ def quote(value, column = nil) #:nodoc:
end
- # CONNECTION MANAGEMENT ====================================#
+ # CONNECTION MANAGEMENT ====================================
+ #
# Returns true if the connection is active.
def active?
@@ -223,17 +230,23 @@ def active?
# last known state, which isn't good enough if the connection has
# gone stale since the last use.
@connection.ping
- rescue OCIError
+ rescue OCIException
false
end
# Reconnects to the database.
def reconnect!
@connection.reset!
- rescue OCIError => e
+ rescue OCIException => e
@logger.warn "#{adapter_name} automatic reconnection failed: #{e.message}"
end
+ # Disconnects from the database.
+ def disconnect!
+ @connection.logoff rescue nil
+ @connection.active = false
+ end
+
# DATABASE STATEMENTS ======================================
#
@@ -346,16 +359,17 @@ def columns(table_name, name = nil) #:nodoc:
from all_tab_columns
where owner = '#{table_info.schema}'
and table_name = '#{table_info.name}'
+ order by column_id
}
select_all(table_cols, name).map do |row|
row['data_default'].sub!(/^'(.*)'\s*$/, '\1') if row['data_default']
- OCIColumn.new(
- oci_downcase(row['column_name']),
+ OracleColumn.new(
+ oracle_downcase(row['column_name']),
row['data_default'],
row['data_type'],
- row['length'],
- row['scale'],
+ row['length'].to_i,
+ row['scale'].to_i,
row['nullable'] == 'Y'
)
end
@@ -444,7 +458,7 @@ def structure_drop #:nodoc:
def select(sql, name = nil)
cursor = log(sql, name) { @connection.exec sql }
- cols = cursor.get_col_names.map { |x| oci_downcase(x) }
+ cols = cursor.get_col_names.map { |x| oracle_downcase(x) }
rows = []
while row = cursor.fetch
@@ -476,7 +490,7 @@ def select(sql, name = nil)
# 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)
+ def oracle_downcase(column_name)
column_name =~ /[a-z]/ ? column_name : column_name.downcase
end
@@ -500,8 +514,8 @@ def define_a_column(i)
end
end
- # missing constant from oci8
- OCI_PTYPE_UNK = 0
+ # missing constant from oci8 < 0.1.14
+ OCI_PTYPE_UNK = 0 unless defined?(OCI_PTYPE_UNK)
def object_info(name)
OraObject.new describe(name.to_s, OCI_PTYPE_UNK)
@@ -529,11 +543,11 @@ def initialize(info)
end
- # The OCIConnectionFactory factors out the code necessary to connect and
- # configure an OCI connection.
- class OCIConnectionFactory #:nodoc:
- def new_connection(username, password, host)
- conn = OCI8.new username, password, host
+ # The OracleConnectionFactory factors out the code necessary to connect and
+ # configure an Oracle/OCI connection.
+ class OracleConnectionFactory #:nodoc:
+ def new_connection(username, password, database)
+ conn = OCI8.new username, password, database
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'} rescue nil
conn.autocommit = true
@@ -559,11 +573,11 @@ class << self
end
@@auto_retry = false
- def initialize(config, factory = OCIConnectionFactory.new)
+ def initialize(config, factory = OracleConnectionFactory.new)
@active = true
- @username, @password, @host = config[:username], config[:password], config[:host]
+ @username, @password, @database = config[:username], config[:password], config[:database]
@factory = factory
- @connection = @factory.new_connection @username, @password, @host
+ @connection = @factory.new_connection @username, @password, @database
super @connection
end
@@ -582,7 +596,7 @@ def ping
def reset!
logoff rescue nil
begin
- @connection = @factory.new_connection @username, @password, @host
+ @connection = @factory.new_connection @username, @password, @database
__setobj__ @connection
@active = true
rescue
@@ -605,7 +619,7 @@ def exec(sql, *bindvars)
begin
@connection.exec(sql, *bindvars)
- rescue OCIError => e
+ rescue OCIException => e
raise unless LOST_CONNECTION_ERROR_CODES.include?(e.code)
@active = false
raise unless should_retry
@@ -621,6 +635,10 @@ def exec(sql, *bindvars)
# OCI8 driver is unavailable.
module ActiveRecord # :nodoc:
class Base
+ def self.oracle_connection(config) # :nodoc:
+ # Set up a reasonable error message
+ raise LoadError, "Oracle/OCI libraries could not be loaded."
+ end
def self.oci_connection(config) # :nodoc:
# Set up a reasonable error message
raise LoadError, "Oracle/OCI libraries could not be loaded."
@@ -17,7 +17,7 @@ class MasterCreditCard < ActiveRecord::Base; end
class Post < ActiveRecord::Base; end
class Computer < ActiveRecord::Base; end
class NonExistentTable < ActiveRecord::Base; end
-class TestOCIDefault < ActiveRecord::Base; end
+class TestOracleDefault < ActiveRecord::Base; end
class LoosePerson < ActiveRecord::Base
attr_protected :credit_rating, :administrator
@@ -526,8 +526,8 @@ def test_default_values
# Oracle has some funky default handling, so it requires a bit of
# extra testing. See ticket #2788.
- if current_adapter?(:OCIAdapter)
- test = TestOCIDefault.new
+ if current_adapter?(:OracleAdapter)
+ test = TestOracleDefault.new
assert_equal "X", test.test_char
assert_equal "hello", test.test_string
assert_equal 3, test.test_int
@@ -536,7 +536,7 @@ def test_default_values
def test_utc_as_time_zone
# Oracle and SQLServer do not have a TIME datatype.
- return true if current_adapter?(:SQLServerAdapter) || current_adapter?(:OCIAdapter)
+ return true if current_adapter?(:SQLServerAdapter) || current_adapter?(:OracleAdapter)
Topic.default_timezone = :utc
attributes = { "bonus_time" => "5:42:00AM" }
@@ -704,7 +704,7 @@ def test_multiparameter_mass_assignment_protector
def test_attributes_on_dummy_time
# Oracle and SQL Server do not have a TIME datatype.
- return true if current_adapter?(:SQLServerAdapter) || current_adapter?(:OCIAdapter)
+ return true if current_adapter?(:SQLServerAdapter) || current_adapter?(:OracleAdapter)
attributes = {
"bonus_time" => "5:42:00AM"
@@ -20,7 +20,7 @@ def test_truth
# Without using prepared statements, it makes no sense to test
# BLOB data with DB2 or Firebird, because the length of a statement
# is limited to 32KB.
- unless %w(SQLServer DB2 OCI Firebird).include? ActiveRecord::Base.connection.adapter_name
+ unless %w(SQLServer DB2 Oracle Firebird).include? ActiveRecord::Base.connection.adapter_name
def test_load_save
bin = Binary.new
bin.data = @data
@@ -4,7 +4,7 @@
class TestColumnAlias < Test::Unit::TestCase
fixtures :topics
- QUERY = if 'OCI' == ActiveRecord::Base.connection.adapter_name
+ QUERY = if 'Oracle' == ActiveRecord::Base.connection.adapter_name
'SELECT id AS pk FROM topics WHERE ROWNUM < 2'
else
'SELECT id AS pk FROM topics'
@@ -1,25 +1,23 @@
-print "Using OCI Oracle\n"
+print "Using Oracle\n"
require_dependency 'fixtures/course'
require 'logger'
ActiveRecord::Base.logger = Logger.new STDOUT
ActiveRecord::Base.logger.level = Logger::WARN
-db1 = 'activerecord_unittest'
-db2 = 'activerecord_unittest2'
+# Set these to your database connection strings
+db = 'activerecord_unit_tests'
ActiveRecord::Base.establish_connection(
- :adapter => 'oci',
- :host => '', # can use an oracle SID
+ :adapter => 'oracle',
:username => 'arunit',
:password => 'arunit',
- :database => db1
+ :database => db
)
Course.establish_connection(
- :adapter => 'oci',
- :host => '', # can use an oracle SID
+ :adapter => 'oracle',
:username => 'arunit2',
:password => 'arunit2',
- :database => db2
+ :database => db
)
@@ -275,13 +275,13 @@ create table keyboards (
);
create sequence keyboards_seq minvalue 10000;
-create table test_oci_defaults (
+create table test_oracle_defaults (
id integer not null primary key,
test_char char(1) default 'X' not null,
test_string varchar2(20) default 'hello' not null,
test_int integer default 3 not null
);
-create sequence test_oci_defaults_seq minvalue 10000;
+create sequence test_oracle_defaults_seq minvalue 10000;
--This table has an altered lock_version column name.
create table legacy_things (
@@ -61,7 +61,7 @@ def test_inserts_with_pre_and_suffix
t.column :written_on, :datetime
t.column :bonus_time, :time
t.column :last_read, :date
- t.column :content, :text
+ t.column :content, :string
t.column :approved, :boolean, :default => true
t.column :replies_count, :integer, :default => 0
t.column :parent_id, :integer
@@ -78,16 +78,16 @@ def test_inserts_with_pre_and_suffix
topics = create_fixtures("topics")
- # Restore prefix/suffix to its previous values
- ActiveRecord::Base.table_name_prefix = old_prefix
- ActiveRecord::Base.table_name_suffix = old_suffix
-
firstRow = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'David'")
assert_equal("The First Topic", firstRow["title"])
secondRow = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'Mary'")
assert_nil(secondRow["author_email_address"])
ensure
+ # Restore prefix/suffix to its previous values
+ ActiveRecord::Base.table_name_prefix = old_prefix
+ ActiveRecord::Base.table_name_suffix = old_suffix
+
ActiveRecord::Base.connection.drop_table :prefix_topics_suffix rescue nil
end
end
Oops, something went wrong.

0 comments on commit 631181c

Please sign in to comment.