Usage
Configuration
Include in environment.rb
Rails::Initializer.run do |config| #... config.gem "activerecord-oracle_enhanced-adapter", :lib => "active_record/connection_adapters/oracle_enhanced_adapter" #... end
to ensure that Oracle enhanced adapter gem will be loaded.
In Rails config/database.yml file use oracle_enhanced as adapter name, e.g.:
development: adapter: oracle_enhanced database: xe username: hr password: hr
If JRuby/JDBC is used then specify also host and optionally also port (by default 1521) in database.yml (so that JDBC connection string could be constructed):
development: adapter: oracle_enhanced database: xe host: ubuntu username: hr password: hr
If necessary also custom JDBC url parameter could be specified in database.yml file.
Create config/initializers/oracle.rb file in your Rails application and put configuration options there, e.g.:
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do self.emulate_integers_by_column_name = true self.emulate_dates_by_column_name = true self.emulate_booleans_from_strings = true self.string_to_date_format = "%d.%m.%Y" self.string_to_time_format = "%d.%m.%Y %H:%M:%S" end
See further description of configuration options.
Date, integer and boolean data types
- set to true if columns with DATE in their name should be emulated as Date (and not as Time which is default)
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do self.emulate_dates_by_column_name = true end
- set to true if columns with ID at the end of column name should be emulated as Fixnum (and not as BigDecimal which is default)
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do self.emulate_integers_by_column_name = true end
- set to true if CHAR, VARCHAR2 columns or VARCHAR2 columns with FLAG or YN at the end of their name should be emulated as booleans (and do not use NUMBER as type for booleans which is default)
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do self.emulate_booleans_from_strings = true end
- specify other date and time formats that should be used when assigning string values to :date and :datetime columns, e.g.:
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do self..string_to_date_format = "%d.%m.%Y" self.string_to_time_format = "%d.%m.%Y %H:%M:%S" end
The following model class methods are available:
- specify which table columns should be with :date type (without time) or with :datetime type (with date and time) – this could be used
if emulation by column names is not working because of different naming conventions
set_date_columns :started_on, :finished_on set_datetime_columns :start_date_and_time, :end_date_and_time
- specify which table columns should be with :boolean type – this should be used together with boolean emulation from strings if
there are other boolean columns with different naming convention
set_boolean_columns :some_boolean_column
- specify which table columns should be ignored by ActiveRecord
ignore_table_columns :column1, :column2, :column3
Custom create, update and delete methods
- specify custom create, update and delete methods which should be used instead of Rails generated INSERT, UPDATE and DELETE statements
# ... example from model class definition ...
# should return ID of new record
set_create_method do
plsql.employees_pkg.create_employee(
:p_first_name => first_name,
:p_last_name => last_name,
:p_employee_id => nil
)[:p_employee_id]
end
set_update_method do
plsql.employees_pkg.update_employee(
:p_employee_id => id,
:p_first_name => first_name,
:p_last_name => last_name
)
end
set_delete_method do
plsql.employees_pkg.delete_employee(
:p_employee_id => id
)
end
ruby-plsql gem is required for custom create, update and delete methods.
Include in oracle.rb initializer file
plsql.activerecord_class = ActiveRecord::Base
to specify ActiveRecord connection to ruby-plsql gem.
Composite primary keys
Oracle enhanced adapter is also compatible with composite_primary_keys gem.
The following Rails and composite_primary_keys gem versions have been tested:
- Rails 2.0.2, composite_primary_keys 0.9.93
- Rails 2.1.2, composite_primary_keys 1.0.8
- Rails 2.2.2, composite_primary_keys 2.2.2
- Rails 2.3.2, probably composite_primary_keys 2.2.2 works?
Migrations: sequence creation
Oracle enhanced adapter for each table creates corresponding sequence for primary key values generation and by default this sequence start value is 10000. You can override this default starting value, e.g.:
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do self.default_sequence_start_value = 1 end
You can also specify individual starting value in table creation migration file:
create_table :users, :sequence_start_value => 100 do |t| # ... end
You can also specify other sequence definition additional parameters:
create_table :users, :sequence_start_value => "100 NOCACHE INCREMENT BY 10" do |t| # ... end
Migrations: table and column comments
It is possible to add table and column comments in table creation migration files:
create_table :employees, :comment => "Employees and contractors" do |t| t.string :first_name, :comment => "Given name" t.string :last_name, :comment => "Surname" end
This can be useful if Rails database schema is used also by other applications and such comments can be used to provide more detailed descriptions about meaning of data stored in tables and columns.
