pmacs-activerecord-oracle_enhanced-adapter 1.4.2.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.rspec +2 -0
  2. data/Gemfile +52 -0
  3. data/History.md +284 -0
  4. data/License.txt +20 -0
  5. data/README.md +403 -0
  6. data/RUNNING_TESTS.md +45 -0
  7. data/Rakefile +59 -0
  8. data/VERSION +1 -0
  9. data/lib/active_record/connection_adapters/emulation/oracle_adapter.rb +5 -0
  10. data/lib/active_record/connection_adapters/oracle_enhanced.rake +105 -0
  11. data/lib/active_record/connection_adapters/oracle_enhanced_activerecord_patches.rb +41 -0
  12. data/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb +1408 -0
  13. data/lib/active_record/connection_adapters/oracle_enhanced_base_ext.rb +118 -0
  14. data/lib/active_record/connection_adapters/oracle_enhanced_column.rb +141 -0
  15. data/lib/active_record/connection_adapters/oracle_enhanced_connection.rb +135 -0
  16. data/lib/active_record/connection_adapters/oracle_enhanced_context_index.rb +359 -0
  17. data/lib/active_record/connection_adapters/oracle_enhanced_core_ext.rb +25 -0
  18. data/lib/active_record/connection_adapters/oracle_enhanced_cpk.rb +21 -0
  19. data/lib/active_record/connection_adapters/oracle_enhanced_dirty.rb +44 -0
  20. data/lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb +565 -0
  21. data/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb +491 -0
  22. data/lib/active_record/connection_adapters/oracle_enhanced_procedures.rb +260 -0
  23. data/lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb +231 -0
  24. data/lib/active_record/connection_adapters/oracle_enhanced_schema_dumper.rb +257 -0
  25. data/lib/active_record/connection_adapters/oracle_enhanced_schema_statements.rb +397 -0
  26. data/lib/active_record/connection_adapters/oracle_enhanced_schema_statements_ext.rb +265 -0
  27. data/lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb +294 -0
  28. data/lib/active_record/connection_adapters/oracle_enhanced_tasks.rb +17 -0
  29. data/lib/active_record/connection_adapters/oracle_enhanced_version.rb +1 -0
  30. data/lib/pmacs-activerecord-oracle_enhanced-adapter.rb +25 -0
  31. data/pmacs-activerecord-oracle_enhanced-adapter.gemspec +131 -0
  32. data/spec/active_record/connection_adapters/oracle_enhanced_adapter_spec.rb +778 -0
  33. data/spec/active_record/connection_adapters/oracle_enhanced_connection_spec.rb +332 -0
  34. data/spec/active_record/connection_adapters/oracle_enhanced_context_index_spec.rb +427 -0
  35. data/spec/active_record/connection_adapters/oracle_enhanced_core_ext_spec.rb +19 -0
  36. data/spec/active_record/connection_adapters/oracle_enhanced_cpk_spec.rb +113 -0
  37. data/spec/active_record/connection_adapters/oracle_enhanced_data_types_spec.rb +1376 -0
  38. data/spec/active_record/connection_adapters/oracle_enhanced_dbms_output_spec.rb +69 -0
  39. data/spec/active_record/connection_adapters/oracle_enhanced_dirty_spec.rb +141 -0
  40. data/spec/active_record/connection_adapters/oracle_enhanced_emulate_oracle_adapter_spec.rb +25 -0
  41. data/spec/active_record/connection_adapters/oracle_enhanced_procedures_spec.rb +378 -0
  42. data/spec/active_record/connection_adapters/oracle_enhanced_schema_dump_spec.rb +438 -0
  43. data/spec/active_record/connection_adapters/oracle_enhanced_schema_statements_spec.rb +1280 -0
  44. data/spec/active_record/connection_adapters/oracle_enhanced_structure_dump_spec.rb +339 -0
  45. data/spec/spec_helper.rb +187 -0
  46. metadata +302 -0
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --backtrace
data/Gemfile ADDED
@@ -0,0 +1,52 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'jeweler', '~> 1.5.1'
5
+ gem 'rspec', '~> 2.4'
6
+ gem 'rdoc'
7
+
8
+ if ENV['RAILS_GEM_VERSION']
9
+ gem 'activerecord', "=#{ENV['RAILS_GEM_VERSION']}"
10
+ gem 'actionpack', "=#{ENV['RAILS_GEM_VERSION']}"
11
+ gem 'activesupport', "=#{ENV['RAILS_GEM_VERSION']}"
12
+ case ENV['RAILS_GEM_VERSION']
13
+ when /^2.0/
14
+ gem 'composite_primary_keys', '=0.9.93'
15
+ when /^2.1/
16
+ gem 'composite_primary_keys', '=1.0.8'
17
+ when /^2.2/
18
+ gem 'composite_primary_keys', '=2.2.2'
19
+ when /^2.3.3/
20
+ gem 'composite_primary_keys', '=2.3.2'
21
+ when /^3/
22
+ gem 'railties', "=#{ENV['RAILS_GEM_VERSION']}"
23
+ end
24
+ else
25
+ %w(activerecord activemodel activesupport actionpack railties).each do |gem_name|
26
+ if ENV['RAILS_GEM_PATH']
27
+ gem gem_name, :path => File.join(ENV['RAILS_GEM_PATH'], gem_name)
28
+ else
29
+ gem gem_name, :git => "git://github.com/rails/rails"
30
+ end
31
+ end
32
+
33
+ if ENV['AREL_GEM_PATH']
34
+ gem 'arel', :path => ENV['AREL_GEM_PATH']
35
+ else
36
+ gem 'arel', :git => "git://github.com/rails/arel"
37
+ end
38
+
39
+ if ENV['JOURNEY_GEM_PATH']
40
+ gem 'journey', :path => ENV['JOURNEY_GEM_PATH']
41
+ else
42
+ gem "journey", :git => "git://github.com/rails/journey"
43
+ end
44
+ end
45
+
46
+ gem 'ruby-plsql', '>=0.4.4'
47
+
48
+ platforms :ruby do
49
+ gem 'ruby-oci8', '>=2.0.4'
50
+ end
51
+
52
+ end
data/History.md ADDED
@@ -0,0 +1,284 @@
1
+ ### 1.4.2.rc1 / 2012-11-13
2
+
3
+ * Enhancements:
4
+ * Wordlist option for context index [#154]
5
+ * Fall back to directly connecting via OracleDriver on JRuby [#163]
6
+ * Allow slash-prefixed database name in database.yml for using a service [#201]
7
+ * Bug fixes:
8
+ * Fixed explain plans to work with JDBC and OCI8 [#146]
9
+ * Fixed various issues with virtual columns [#159]
10
+ * Fixed SQL structure dump with function indexes [#161]
11
+ * Fixed broken column remove inside a change_table block [#216]
12
+ * Dump indexes on virtual columns using the column's name instead of the column expression [#211]
13
+ * Don't update lobs that haven't changed or are attr_readonly [#212]
14
+ * Support dirty tracking with rails 3.2.9
15
+
16
+ ### 1.4.1 / 2012-01-27
17
+
18
+ * Enhancements:
19
+ * Support for Rails 3.2
20
+ * Support for ActiveRecord 3.2 explain plans [#116]
21
+ * Support for ActiveRecord 3.1 statement pool, to avoid `ORA-01000` maximum open cursors exceeded (default `statement_limit` is 250 and can be changed in `database.yml`) [#100]
22
+ * Added error handling for `rename_table` method in migrations [#137]
23
+ * Bug fixes:
24
+ * Store primary key as `nil` in cache at first time for table without primary key [#84]
25
+ * Fixed inserting records with decimal type columns (`ORA-01722` invalid number exceptions) [#130]
26
+ * Check virtual columns only in models that are using `oracle-enhanced` adapter, to avoid problems when using multiple database adapters [#85]
27
+ * Don't drop the user in rake `db:create` and `db:drop` tasks [#103]
28
+ * Don't add `db:create` and `db:drop` when ActiveRecord is not used as the primary datastore [#128]
29
+ * Quote column names in LOB statements to avoid `ORA-00936` errors [#91]
30
+ * Don't add the `RETURNING` clause if using `composite_primary_keys` gem [#132]
31
+ * Added `join_to_update` method that is necessary for ActiveRecord 3.1 to ensure that correct UPDATE statement is generated using `WHERE ... IN` subquery with offset condition
32
+
33
+ ### 1.4.0 / 2011-08-09
34
+
35
+ * Enhancements:
36
+ * Support for Rails 3.1
37
+ * Bind parameter support for exec_insert, exec_update and exec_delete (in ActiveRecord 3.1)
38
+ * Purge recyclebin on rake db:test:purge
39
+ * Support transactional context index
40
+ * Require ojdbc6.jar (on Java 6) or ojdbc5.jar (on Java 5) JDBC drivers
41
+ * Support for RAW data type
42
+ * rake db:create and db:drop tasks
43
+ * Support virtual columns (in Oracle 11g) in schema dump
44
+ * It is possible to specify default tablespaces for tables, indexes, CLOBs and BLOBs
45
+ * rename_index migrations method
46
+ * Search for JDBC driver in ./lib directory of Rails application
47
+ * Bug fixes:
48
+ * Fixed context index dump when definition is larger than 4000 bytes
49
+ * Fixed schema dump not to conflict with other database adapters that are used in the same application
50
+ * Allow $ in table name prefix or suffix
51
+
52
+ ### 1.3.2 / 2011-01-05
53
+
54
+ * Enhancements:
55
+ * If no :host or :port is provided then connect with :database name (do not default :host to localhost)
56
+ * Database connection pool support for JRuby on Tomcat and JBoss application servers
57
+ * NLS connection parameters support via environment variables or database.yml
58
+ * Support for Arel 2.0 and latest Rails master branch
59
+ * Support for Rails 3.1 prepared statements (implemented in not yet released Rails master branch version)
60
+ * Eager loading of included association with more than 1000 records (implemented in not yet released Rails master branch version)
61
+ * Bug fixes:
62
+ * Foreign keys are added after table definitions in schema dump to ensure correct order of schema statements
63
+ * Quote NCHAR and NVARCHAR2 type values with N'...'
64
+ * Numeric username and/or password in database.yml will be automatically converted to string
65
+
66
+ ### 1.3.1 / 2010-09-09
67
+
68
+ * Enhancements:
69
+ * Tested with Rails 3.0.0 release
70
+ * Lexer options for context index creation
71
+ * Added Bundler for running adapter specs, added RUNNING_TESTS.rdoc with description how to run specs
72
+ * Connection to database using :host, :port and :database options
73
+ * Improved loading of adapter in Rails 3 using railtie
74
+ * Bug fixes:
75
+ * Fix for custom context index procedure when indexing records with null values
76
+ * Quote table and column names in write_lobs callback
77
+ * Fix for incorrect column SQL types when two models use the same table and AR query cache is enabled
78
+ * Fixes for schema and scructure dump tasks
79
+ * Fix for handling of zero-length strings in BLOB and CLOB columns
80
+ * removed String.mb_chars upcase and downcase methods for Ruby 1.9 as Rails 3.0.0 already includes Unicode aware upcase and downcase methods for Ruby 1.9
81
+ * Fixes for latest ActiveRecord unit tests
82
+
83
+ ### 1.3.0 / 2010-06-21
84
+
85
+ * Enhancements:
86
+ * Rails 3.0.0.beta4 and Rails 2.3.x compatible
87
+ * When used with Rails 3 then works together with Oracle SQL compiler included in Arel gem (http://github.com/rails/arel)
88
+ * Rails 3: Better support for limit and offset (when possible adds just ROWNUM condition in WHERE clause without using subqueries)
89
+ * Table and column names are always quoted and in uppercase to avoid the need for checking Oracle reserved words
90
+ * Full text search index creation (add_context_index and remove_context_index methods in migrations and #contains method in ActiveRecord models)
91
+ * add_index and remove_index give just warnings on wrong index names (new expected behavior in Rails 2.3.8 and 3.0.0)
92
+ * :tablespace and :options options for create_table and add_index
93
+ * Workarounds:
94
+ * Rails 3: set ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.cache_columns = true in initializer file for all environments
95
+ (to avoid too many data dictionary queries from Arel)
96
+ * Rails 2.3: patch several ActiveRecord methods to work correctly with quoted table names in uppercase (see oracle_enhanced_activerecord_patches.rb).
97
+ These patches are already included in Rails 3.0.0.beta4.
98
+ * Bug fixes:
99
+ * Fixes for schema purge (drop correctly materialized views)
100
+ * Fixes for schema dump and structure dump (use correct statement separator)
101
+ * Only use Oracle specific schema dump for Oracle connections
102
+
103
+ ### 1.2.4 / 2010-02-23
104
+
105
+ * Enhancements:
106
+ * rake db:test:purge will drop all schema objects from test schema (including views, synonyms, packages, functions, procedures) -
107
+ they should be always reloaded before tests run if necessary
108
+ * added views, synonyms, packages, functions, procedures, indexes, triggers, types, primary, unique and foreign key constraints to structure dump
109
+ * added :temporary option for create_table to create temporary tables
110
+ * added :tablespace option for add_index
111
+ * support function based indexes in schema dump
112
+ * support JNDI database connections in JRuby
113
+ * check ruby-oci8 minimum version 2.0.3
114
+ * added savepoints support (nested ActiveRecord transactions)
115
+ * Bug fixes:
116
+ * typecast returned BigDecimal integer values to Fixnum or Bignum
117
+ (to avoid issues with _before_type_cast values for id attributes because _before_type_cast is used in form helpers)
118
+ * clear table columns cache after columns definition change in migrations
119
+
120
+ ### 1.2.3 / 2009-12-09
121
+
122
+ * Enhancements
123
+ * support fractional seconds in TIMESTAMP values
124
+ * support for ActiveRecord 2.3.5
125
+ * use ENV['TZ'] to set database session time zone
126
+ (as a result DATE and TIMESTAMP values are retrieved with correct time zone)
127
+ * added cache_columns adapter option
128
+ * added current_user adapter method
129
+ * added set_integer_columns and set_string_columns ActiveRecord model class methods
130
+ * Bug fixes:
131
+ * do not raise exception if ENV['PATH'] is nil
132
+ * do not add change_table behavior for ActiveRecord 2.0 (to avoid exception during loading)
133
+ * move foreign key definitions after definition of all tables in schema.rb
134
+ (to avoid definition of foreign keys before all tables are created)
135
+ * changed timestamp format mask to use ':' before fractional seconds
136
+ (workaround to avoid table detection in tables_in_string method in ActiveRecord associations.rb file)
137
+ * fixed custom create/update/delete methods with ActiveRecord 2.3+ and timestamps
138
+ * do not call oracle_enhanced specific schema dump methods when using other database adapters
139
+
140
+ ### 1.2.2 / 2009-09-28
141
+
142
+ * Enhancements
143
+ * improved RDoc documentation of public methods
144
+ * structure dump optionally (database.yml environment has db_stored_code: yes) extracts
145
+ packages, procedures, functions, views, triggers and synonyms
146
+ * automatically generated too long index names are shortened down to 30 characters
147
+ * create tables with primary key triggers
148
+ * use 'set_sequence_name :autogenerated' for inserting into legacy tables with trigger populated primary keys
149
+ * access to tables over database link (need to define local synonym to remote table and use local synonym in set_table_name)
150
+ * [JRuby] support JDBC connection using TNS_ADMIN environment variable and TNS database alias
151
+ * changed cursor_sharing option default from 'similar' to 'force'
152
+ * optional dbms_output logging to ActiveRecord log file (requires ruby-plsql gem)
153
+ * use add_foreign_key and remove_foreign_key to define foreign key constraints
154
+ (the same syntax as in http://github.com/matthuhiggins/foreigner and similar
155
+ to http://github.com/eyestreet/active_record_oracle_extensions)
156
+ * raise RecordNotUnique and InvalidForeignKey exceptions if caused by corresponding ORA errors
157
+ (these new exceptions are supported just by current ActiveRecord master branch)
158
+ * implemented disable_referential_integrity
159
+ (enables safe loading of fixtures in schema with foreign key constraints)
160
+ * use add_synonym and remove_synonym to define database synonyms
161
+ * add_foreign_key and add_synonym are also exported to schema.rb
162
+ * Bug fixes:
163
+ * [JRuby] do not raise LoadError if ojdbc14.jar cannot be required (rely on application server to add it to class path)
164
+ * [JRuby] 'execute' can be used to create triggers with :NEW reference
165
+ * support create_table without a block
166
+ * support create_table with Symbol table name
167
+ * use ActiveRecord functionality to do time zone conversion
168
+ * rake tasks such as db:test:clone are redefined only if oracle_enhanced is current adapter in use
169
+ * VARCHAR2 and CHAR column sizes are defined in characters and not in bytes (expected behavior from ActiveRecord)
170
+ * set_date_columns, set_datetime_columns, ignore_table_columns will work after reestablishing connection
171
+ * ignore :limit option for :text and :binary columns in migrations
172
+ * patches for ActiveRecord schema dumper to remove table prefixes and suffixes from schema.rb
173
+
174
+ ### 1.2.1 / 2009-06-07
175
+
176
+ * Enhancements
177
+ * caching of table indexes query which makes schema dump much faster
178
+ * Bug fixes:
179
+ * return Date (and not DateTime) values for :date column value before year 1970
180
+ * fixed after_create/update/destroy callbacks with plsql custom methods
181
+ * fixed creation of large integers in JRuby
182
+ * Made test tasks respect RAILS_ENV
183
+ * fixed support for composite primary keys for tables with LOBs
184
+
185
+ ### 1.2.0 / 2009-03-22
186
+
187
+ * Enhancements
188
+ * support for JRuby and JDBC
189
+ * support for Ruby 1.9.1 and ruby-oci8 2.0
190
+ * support for Rails 2.3
191
+ * quoting of Oracle reserved words in table names and column names
192
+ * emulation of OracleAdapter (for ActiveRecord unit tests)
193
+ * Bug fixes:
194
+ * several bug fixes that were identified during running of ActiveRecord unit tests
195
+
196
+ ### 1.1.9 / 2009-01-02
197
+
198
+ * Enhancements
199
+ * Added support for table and column comments in migrations
200
+ * Added support for specifying sequence start values
201
+ * Added :privilege option (e.g. :SYSDBA) to ActiveRecord::Base.establish_connection
202
+ * Bug fixes:
203
+ * Do not mark empty decimals, strings and texts (stored as NULL in database) as changed when reassigning them (starting from Rails 2.1)
204
+ * Create booleans as VARCHAR2(1) columns if emulate_booleans_from_strings is true
205
+
206
+ ### 1.1.8 / 2008-10-10
207
+
208
+ * Bug fixes:
209
+ * Fixed storing of serialized LOB columns
210
+ * Prevent from SQL injection in :limit and :offset
211
+ * Order by LOB columns (by replacing column with function which returns first 100 characters of LOB)
212
+ * Sequence creation for tables with non-default primary key in create_table block
213
+ * Do count distinct workaround only when composite_primary_keys gem is used
214
+ (otherwise count distinct did not work with ActiveRecord 2.1.1)
215
+ * Fixed rake db:test:clone_structure task
216
+ (see http://rsim.lighthouseapp.com/projects/11468/tickets/11-rake-dbtestclone_structure-fails-in-117)
217
+ * Fixed bug when ActiveRecord::Base.allow_concurrency = true
218
+ (see http://dev.rubyonrails.org/ticket/11134)
219
+
220
+ ### 1.1.7 / 2008-08-20
221
+
222
+ * Bug fixes:
223
+ * Fixed that adapter works without ruby-plsql gem (in this case just custom create/update/delete methods are not available)
224
+
225
+ ### 1.1.6 / 2008-08-19
226
+
227
+ * Enhancements:
228
+ * Added support for set_date_columns and set_datetime_columns
229
+ * Added support for set_boolean_columns
230
+ * Added support for schema prefix in set_table_name (removed table name quoting)
231
+ * Added support for NVARCHAR2 column type
232
+ * Bug fixes:
233
+ * Do not call write_lobs callback when custom create or update methods are defined
234
+
235
+ ### 1.1.5 / 2008-07-27
236
+
237
+ * Bug fixes:
238
+ * Fixed that write_lobs callback works with partial_updates enabled (added additional record lock before writing BLOB data to database)
239
+ * Enhancements:
240
+ * Changed SQL SELECT in indexes method so that it will execute faster on some large data dictionaries
241
+ * Support for other date and time formats when assigning string to :date or :datetime column
242
+
243
+ ### 1.1.4 / 2008-07-14
244
+
245
+ * Enhancements:
246
+ * Date/Time quoting changes to support composite_primary_keys
247
+ * Added additional methods that are used by composite_primary_keys
248
+
249
+ ### 1.1.3 / 2008-07-10
250
+
251
+ * Enhancements:
252
+ * Added support for custom create, update and delete methods when working with legacy databases where
253
+ PL/SQL API should be used for create, update and delete operations
254
+
255
+ ### 1.1.2 / 2008-07-08
256
+
257
+ * Bug fixes:
258
+ * Fixed after_save callback addition for session store in ActiveRecord version 2.0.2
259
+ * Changed date column name recognition - now should match regex /(^|_)date(_|$)/i
260
+ (previously "updated_at" was recognized as :date column and not as :datetime)
261
+
262
+ ### 1.1.1 / 2008-06-28
263
+
264
+ * Enhancements:
265
+ * Added ignore_table_columns option
266
+ * Added support for TIMESTAMP columns (without fractional seconds)
267
+ * NLS_DATE_FORMAT and NLS_TIMESTAMP_FORMAT independent DATE and TIMESTAMP columns support
268
+ * Bug fixes:
269
+ * Checks if CGI::Session::ActiveRecordStore::Session does not have enhanced_write_lobs callback before adding it
270
+ (Rails 2.0 does not add this callback, Rails 2.1 does)
271
+
272
+ ### 1.1.0 / 2008-05-05
273
+
274
+ * Forked from original activerecord-oracle-adapter-1.0.0.9216
275
+ * Renamed oracle adapter to oracle_enhanced adapter
276
+ * Added "enhanced" to method and class definitions so that oracle_enhanced and original oracle adapter
277
+ could be used simultaniously
278
+ * Added Rails rake tasks as a copy from original oracle tasks
279
+ * Enhancements:
280
+ * Improved perfomance of schema dump methods when used on large data dictionaries
281
+ * Added LOB writing callback for sessions stored in database
282
+ * Added emulate_dates_by_column_name option
283
+ * Added emulate_integers_by_column_name option
284
+ * Added emulate_booleans_from_strings option
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008-2011 Graham Jenkins, Michael Schoen, Raimonds Simanovskis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,403 @@
1
+ activerecord-oracle_enhanced-adapter
2
+ ====================================
3
+
4
+ Oracle enhanced adapter for ActiveRecord
5
+
6
+ DESCRIPTION
7
+ -----------
8
+
9
+ Oracle enhanced ActiveRecord adapter provides Oracle database access from Ruby on Rails applications. Oracle enhanced adapter can be used from Ruby on Rails versions 2.3.x and 3.x and it is working with Oracle database versions 10g and 11g.
10
+
11
+ INSTALLATION
12
+ ------------
13
+
14
+ ### Rails 3
15
+
16
+ When using Ruby on Rails version 3 then in Gemfile include
17
+
18
+ gem 'activerecord-oracle_enhanced-adapter', '~> 1.4.0'
19
+
20
+ where instead of 1.4.0 you can specify any other desired version. It is recommended to specify version with `~>` which means that use specified version or later patch versions (in this example any later 1.4.x version but not 1.5.x version). Oracle enhanced adapter maintains API backwards compatibility during patch version upgrades and therefore it is safe to always upgrade to latest patch version.
21
+
22
+ If you would like to use latest adapter version from github then specify
23
+
24
+ gem 'activerecord-oracle_enhanced-adapter', :git => 'git://github.com/rsim/oracle-enhanced.git'
25
+
26
+ If you are using MRI 1.8 or 1.9 Ruby implementation then you need to install ruby-oci8 gem as well as Oracle client, e.g. [Oracle Instant Client](http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html). Include in Gemfile also ruby-oci8:
27
+
28
+ gem 'ruby-oci8', '~> 2.1.0'
29
+
30
+ If you are using JRuby then you need to download latest [Oracle JDBC driver](http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html) - either ojdbc6.jar for Java 6 or ojdbc5.jar for Java 5. And copy this file to one of these locations:
31
+
32
+ * in `./lib` directory of Rails application
33
+ * in some directory which is in `PATH`
34
+ * in `JRUBY_HOME/lib` directory
35
+ * or include path to JDBC driver jar file in Java `CLASSPATH`
36
+
37
+ After specifying necessary gems in Gemfile run
38
+
39
+ bundle install
40
+
41
+ to install the adapter (or later run `bundle update` to force updating to latest version).
42
+
43
+ ### Rails 2.3
44
+
45
+ If you don't use Bundler in Rails 2 application then you need to specify gems in `config/environment.rb`, e.g.
46
+
47
+ Rails::Initializer.run do |config|
48
+ #...
49
+ config.gem 'activerecord-oracle_enhanced-adapter', :lib => "active_record/connection_adapters/oracle_enhanced_adapter"
50
+ config.gem 'ruby-oci8'
51
+ #...
52
+ end
53
+
54
+ But it is recommended to use Bundler for gem version management also for Rails 2.3 applications (search for instructions in Google).
55
+
56
+ ### Without Rails and Bundler
57
+
58
+ If you want to use ActiveRecord and Oracle enhanced adapter without Rails and Bundler then install it just as a gem:
59
+
60
+ gem install activerecord-oracle_enhanced-adapter
61
+
62
+ USAGE
63
+ -----
64
+
65
+ ### Database connection
66
+
67
+ In Rails application `config/database.yml` use oracle_enhanced as adapter name, e.g.
68
+
69
+ development:
70
+ adapter: oracle_enhanced
71
+ database: xe
72
+ username: user
73
+ password: secret
74
+
75
+ If you're connecting to a service name, indicate the service with a
76
+ leading slash on the database parameter:
77
+
78
+ development:
79
+ adapter: oracle_enhanced
80
+ database: /xe
81
+ username: user
82
+ password: secret
83
+
84
+ If `TNS_ADMIN` environment variable is pointing to directory where `tnsnames.ora` file is located then you can use TNS connection name in `database` parameter. Otherwise you can directly specify database host, port (defaults to 1521) and database name in the following way:
85
+
86
+ development:
87
+ adapter: oracle_enhanced
88
+ host: localhost
89
+ port: 1521
90
+ database: xe
91
+ username: user
92
+ password: secret
93
+
94
+ or you can use Oracle specific format in `database` parameter:
95
+
96
+ development:
97
+ adapter: oracle_enhanced
98
+ database: //localhost:1521/xe
99
+ username: user
100
+ password: secret
101
+
102
+ or you can even use Oracle specific TNS connection description:
103
+
104
+ development:
105
+ adapter: oracle_enhanced
106
+ database: "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=xe)))"
107
+ username: user
108
+ password: secret
109
+
110
+ If you deploy JRuby on Rails application in Java application server that supports JNDI connections then you can specify JNDI connection as well:
111
+
112
+ development:
113
+ adapter: oracle_enhanced
114
+ jndi: "jdbc/jndi_connection_name"
115
+
116
+ You can find other available database.yml connection parameters in [oracle_enhanced_adapter.rb](/rsim/oracle-enhanced/blob/master/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb). There are many NLS settings as well as some other Oracle session settings.
117
+
118
+ ### Adapter settings
119
+
120
+ If you want to change Oracle enhanced adapter default settings then create initializer file e.g. `config/initializers/oracle.rb` specify there necessary defaults, e.g.:
121
+
122
+ # It is recommended to set time zone in TZ environment variable so that the same timezone will be used by Ruby and by Oracle session
123
+ ENV['TZ'] = 'UTC'
124
+
125
+ ActiveSupport.on_load(:active_record) do
126
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do
127
+ # id columns and columns which end with _id will always be converted to integers
128
+ self.emulate_integers_by_column_name = true
129
+ # DATE columns which include "date" in name will be converted to Date, otherwise to Time
130
+ self.emulate_dates_by_column_name = true
131
+ # true and false will be stored as 'Y' and 'N'
132
+ self.emulate_booleans_from_strings = true
133
+ # start primary key sequences from 1 (and not 10000) and take just one next value in each session
134
+ self.default_sequence_start_value = "1 NOCACHE INCREMENT BY 1"
135
+ # other settings ...
136
+ end
137
+ end
138
+
139
+ In case of Rails 2 application you do not need to use `ActiveSupport.on_load(:active_record) do ... end` around settings code block.
140
+
141
+ See other adapter settings in [oracle_enhanced_adapter.rb](/rsim/oracle-enhanced/blob/master/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb).
142
+
143
+ ### Legacy schema support
144
+
145
+ If you want to put Oracle enhanced adapter on top of existing schema tables then there are several methods how to override ActiveRecord defaults, see example:
146
+
147
+ class Employee < ActiveRecord::Base
148
+ # specify schema and table name
149
+ set_table_name "hr.hr_employees"
150
+ # specify primary key name
151
+ set_primary_key "employee_id"
152
+ # specify sequence name
153
+ set_sequence_name "hr.hr_employee_s"
154
+ # set which DATE columns should be converted to Ruby Date
155
+ set_date_columns :hired_on, :birth_date_on
156
+ # set which DATE columns should be converted to Ruby Time
157
+ set_datetime_columns :last_login_time
158
+ # set which VARCHAR2 columns should be converted to true and false
159
+ set_boolean_columns :manager, :active
160
+ # set which columns should be ignored in ActiveRecord
161
+ ignore_table_columns :attribute1, :attribute2
162
+ end
163
+
164
+ You can also access remote tables over database link using
165
+
166
+ set_table_name "hr_employees@db_link"
167
+
168
+ ### Custom create, update and delete methods
169
+
170
+ If you have legacy schema and you are not allowed to do direct INSERTs, UPDATEs and DELETEs in legacy schema tables and need to use existing PL/SQL procedures for create, updated, delete operations then you should add `ruby-plsql` gem to your application and then define custom create, update and delete methods, see example:
171
+
172
+ class Employee < ActiveRecord::Base
173
+ # when defining create method then return ID of new record that will be assigned to id attribute of new object
174
+ set_create_method do
175
+ plsql.employees_pkg.create_employee(
176
+ :p_first_name => first_name,
177
+ :p_last_name => last_name,
178
+ :p_employee_id => nil
179
+ )[:p_employee_id]
180
+ end
181
+ set_update_method do
182
+ plsql.employees_pkg.update_employee(
183
+ :p_employee_id => id,
184
+ :p_first_name => first_name,
185
+ :p_last_name => last_name
186
+ )
187
+ end
188
+ set_delete_method do
189
+ plsql.employees_pkg.delete_employee(
190
+ :p_employee_id => id
191
+ )
192
+ end
193
+ end
194
+
195
+ In addition in `config/initializers/oracle.rb` initializer specify that ruby-plsql should use ActiveRecord database connection:
196
+
197
+ plsql.activerecord_class = ActiveRecord::Base
198
+
199
+ ### Oracle CONTEXT index support
200
+
201
+ Every edition of Oracle database includes [Oracle Text](http://www.oracle.com/technology/products/text/index.html) option for free which provides several full text indexing capabilities. Therefore in Oracle database case you don’t need external full text indexing and searching engines which can simplify your application deployment architecture.
202
+
203
+ To create simple single column index create migration with, e.g.
204
+
205
+ add_context_index :posts, :title
206
+
207
+ and you can remove context index with
208
+
209
+ remove_context_index :posts, :title
210
+
211
+ Include in class definition
212
+
213
+ has_context_index
214
+
215
+ and then you can do full text search with
216
+
217
+ Post.contains(:title, 'word')
218
+
219
+ You can create index on several columns (which will generate additional stored procedure for providing XML document with specified columns to indexer):
220
+
221
+ add_context_index :posts, [:title, :body]
222
+
223
+ And you can search either in all columns or specify in which column you want to search (as first argument you need to specify first column name as this is the column which is referenced during index creation):
224
+
225
+ Post.contains(:title, 'word')
226
+ Post.contains(:title, 'word within title')
227
+ Post.contains(:title, 'word within body')
228
+
229
+ See Oracle Text documentation for syntax that you can use in CONTAINS function in SELECT WHERE clause.
230
+
231
+ You can also specify some dummy main column name when creating multiple column index as well as specify to update index automatically after each commit (as otherwise you need to synchronize index manually or schedule periodic update):
232
+
233
+ add_context_index :posts, [:title, :body], :index_column => :all_text, :sync => 'ON COMMIT'
234
+
235
+ Post.contains(:all_text, 'word')
236
+
237
+ Or you can specify that index should be updated when specified columns are updated (e.g. in ActiveRecord you can specify to trigger index update when created_at or updated_at columns are updated). Otherwise index is updated only when main index column is updated.
238
+
239
+ add_context_index :posts, [:title, :body], :index_column => :all_text,
240
+ :sync => 'ON COMMIT', :index_column_trigger_on => [:created_at, :updated_at]
241
+
242
+ And you can even create index on multiple tables by providing SELECT statements which should be used to fetch necessary columns from related tables:
243
+
244
+ add_context_index :posts,
245
+ [:title, :body,
246
+ # specify aliases always with AS keyword
247
+ "SELECT comments.author AS comment_author, comments.body AS comment_body FROM comments WHERE comments.post_id = :id"
248
+ ],
249
+ :name => 'post_and_comments_index',
250
+ :index_column => :all_text,
251
+ :index_column_trigger_on => [:updated_at, :comments_count],
252
+ :sync => 'ON COMMIT'
253
+
254
+ # search in any table columns
255
+ Post.contains(:all_text, 'word')
256
+ # search in specified column
257
+ Post.contains(:all_text, "aaa within title")
258
+ Post.contains(:all_text, "bbb within comment_author")
259
+
260
+ ### Oracle virtual collumns support
261
+
262
+ Since version R11G1 Oracle database allows adding computed [Virtual Columns](http://www.oracle-base.com/articles/11g/virtual-columns-11gr1.php) to the table.
263
+ They can be used as normal fields in the queries, in the foreign key contstraints and to partitioning data.
264
+
265
+ To define virtual column you can use `virtual` method in the `create_table` block, providing column expression in the `:as` option:
266
+
267
+ create_table :mytable do |t|
268
+ t.decimal :price, :precision => 15, :scale => 2
269
+ t.decimal :quantity, :precision => 15, :scale => 2
270
+ t.virtual :amount, :as => 'price * quantity'
271
+ end
272
+
273
+ Oracle tries to predict type of the virtual column, based on its expression but sometimes it is necessary to state type explicitly.
274
+ This can be done by providing `:type` option to the `virtual` method:
275
+
276
+ ...
277
+ t.virtual :amount_2, :as => 'ROUND(price * quantity,2)', :type => :decimal, :precision => 15, :scale => 2
278
+ t.virtual :amount_str, :as => "TO_CHAR(quantity) || ' x ' || TO_CHAR(price) || ' USD = ' || TO_CHAR(quantity*price) || ' USD'",
279
+ :type => :string, :limit => 100
280
+ ...
281
+
282
+ It is possible to add virtual column to existing table:
283
+
284
+ add_column :mytable, :amount_4, :virtual, :as => 'ROUND(price * quantity,4)', :precision => 38, :scale => 4
285
+
286
+ You can use the same options here as in the `create_table` `virtual` method.
287
+
288
+ Changing virtual columns is also possible:
289
+
290
+ change_column :mytable, :amount, :virtual, :as => 'ROUND(price * quantity,0)', :type => :integer
291
+
292
+ Virtual columns allowed in the foreign key constraints.
293
+ For example it can be used to force foreign key constraint on polymorphic association:
294
+
295
+ create_table :comments do |t|
296
+ t.string :subject_type
297
+ t.integer :subject_id
298
+ t.virtual :subject_photo_id, :as => "CASE subject_type WHEN 'Photo' THEN subject_id END"
299
+ t.virtual :subject_event_id, :as => "CASE subject_type WHEN 'Event' THEN subject_id END"
300
+ end
301
+
302
+ add_foreign_key :comments, :photos, :column => :subject_photo_id
303
+ add_foreign_key :comments, :events, :column => :subject_event_id
304
+
305
+ For backward compatibility reasons it is possible to use `:default` option in the `create_table` instead of `:as` option.
306
+ But this is deprecated and may be removed in the future version.
307
+
308
+ ### Oracle specific schema statements and data types
309
+
310
+ There are several additional schema statements and data types available that you can use in database migrations:
311
+
312
+ * `add_foreign_key` and `remove_foreign_key` for foreign key definition (and they are also dumped in `db/schema.rb`)
313
+ * `add_synonym` and `remove_synonym` for synonym definition (and they are also dumped in `db/schema.rb`)
314
+ * You can create table with primary key trigger using `:primary_key_trigger => true` option for `create_table`
315
+ * You can define columns with `raw` type which maps to Oracle's `RAW` type
316
+ * You can add table and column comments with `:comment` option
317
+ * Default tablespaces can be specified for tables, indexes, clobs and blobs, for example:
318
+
319
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces =
320
+ {:clob => 'TS_LOB', :blob => 'TS_LOB', :index => 'TS_INDEX', :table => 'TS_DATA'}
321
+
322
+ TROUBLESHOOTING
323
+ ---------------
324
+
325
+ ### What to do if Oracle enhanced adapter is not working?
326
+
327
+ Please verify that
328
+
329
+ 1. Oracle Instant Client is installed correctly
330
+ Can you connect to database using sqlnet?
331
+
332
+ 2. ruby-oci8 is installed correctly
333
+ Try something like:
334
+
335
+ ruby -rubygems -e "require 'oci8'; OCI8.new('username','password','database').exec('select * from dual') do |r| puts r.join(','); end"
336
+
337
+ to verify that ruby-oci8 is working
338
+
339
+ 3. Verify that activerecord-oracle_enhanced-adapter is working from irb
340
+
341
+ require 'rubygems'
342
+ gem 'activerecord'
343
+ gem 'activerecord-oracle_enhanced-adapter'
344
+ require 'active_record'
345
+ ActiveRecord::Base.establish_connection(:adapter => "oracle_enhanced", :database => "database",:username => "user",:password => "password")
346
+
347
+ and see if it is successful (use your correct database, username and password)
348
+
349
+ ### What to do if Oracle enhanced adapter is not working with Phusion Passenger?
350
+
351
+ Oracle Instant Client and ruby-oci8 requires that several environment variables are set:
352
+
353
+ * `LD_LIBRARY_PATH` (on Linux) or `DYLD_LIBRARY_PATH` (on Mac) should point to Oracle Instant Client directory (where Oracle client shared libraries are located)
354
+ * `TNS_ADMIN` should point to directory where `tnsnames.ora` file is located
355
+ * `NLS_LANG` should specify which territory and language NLS settings to use and which character set to use (e.g. `"AMERICAN_AMERICA.UTF8"`)
356
+
357
+ If this continues to throw "OCI Library Initialization Error (OCIError)", you might also need
358
+
359
+ * `ORACLE_HOME` set to full Oracle client installation directory
360
+
361
+ When Apache with Phusion Passenger (mod_passenger or previously mod_rails) is used for Rails application deployment then by default Ruby is launched without environment variables that you have set in shell profile scripts (e.g. .profile). Therefore it is necessary to set environment variables in one of the following ways:
362
+
363
+ * Create wrapper script as described in [Phusion blog](http://blog.phusion.nl/2008/12/16/passing-environment-variables-to-ruby-from-phusion-passenger) or [RayApps::Blog](http://blog.rayapps.com/2008/05/21/using-mod_rails-with-rails-applications-on-oracle)
364
+ * Set environment variables in the file which is used by Apache before launching Apache worker processes - on Linux it typically is envvars file (look in apachectl or apache2ctl script where it is looking for envvars file) or /System/Library/LaunchDaemons/org.apache.httpd.plist on Mac OS X. See the following [discussion thread](http://groups.google.com/group/oracle-enhanced/browse_thread/thread/c5f64106569fadd0) for more hints.
365
+
366
+ RUNNING TESTS
367
+ -------------
368
+
369
+ See [RUNNING_TESTS.md](/rsim/oracle-enhanced/blob/master/RUNNING_TESTS.md) for information how to set up environment and run Oracle enhanced adapter unit tests.
370
+
371
+ LINKS
372
+ -----
373
+
374
+ * Source code: http://github.com/rsim/oracle-enhanced
375
+ * Bug reports / Feature requests / Pull requests: http://github.com/rsim/oracle-enhanced/issues
376
+ * Discuss at Oracle enhanced adapter group: http://groups.google.com/group/oracle-enhanced
377
+ * Blog posts about Oracle enhanced adapter can be found at http://blog.rayapps.com/category/oracle_enhanced
378
+
379
+ LICENSE
380
+ -------
381
+
382
+ (The MIT License)
383
+
384
+ Copyright (c) 2008-2011 Graham Jenkins, Michael Schoen, Raimonds Simanovskis
385
+
386
+ Permission is hereby granted, free of charge, to any person obtaining
387
+ a copy of this software and associated documentation files (the
388
+ 'Software'), to deal in the Software without restriction, including
389
+ without limitation the rights to use, copy, modify, merge, publish,
390
+ distribute, sublicense, and/or sell copies of the Software, and to
391
+ permit persons to whom the Software is furnished to do so, subject to
392
+ the following conditions:
393
+
394
+ The above copyright notice and this permission notice shall be
395
+ included in all copies or substantial portions of the Software.
396
+
397
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
398
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
399
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
400
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
401
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
402
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
403
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.