activerecord-oracle_enhanced-adapter 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of activerecord-oracle_enhanced-adapter might be problematic. Click here for more details.

@@ -0,0 +1,11 @@
1
+ # RSI: implementation idea taken from JDBC adapter
2
+ if defined?(Rake.application) && Rake.application
3
+ oracle_enhanced_rakefile = File.dirname(__FILE__) + "/oracle_enhanced.rake"
4
+ if Rake.application.lookup("environment")
5
+ # rails tasks already defined; load the override tasks now
6
+ load oracle_enhanced_rakefile
7
+ else
8
+ # rails tasks not loaded yet; load as an import
9
+ Rake.application.add_import(oracle_enhanced_rakefile)
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveRecord #:nodoc:
2
+ module ConnectionAdapters #:nodoc:
3
+ module OracleEnhancedVersion #:nodoc:
4
+ MAJOR = 1
5
+ MINOR = 1
6
+ TINY = 0
7
+
8
+ STRING = [MAJOR, MINOR, TINY].join('.')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,450 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe "OracleEnhancedAdapter establish connection" do
4
+
5
+ before(:all) do
6
+ ActiveRecord::Base.establish_connection(:adapter => "oracle_enhanced",
7
+ :database => "xe",
8
+ :username => "hr",
9
+ :password => "hr")
10
+ end
11
+
12
+ it "should connect to database" do
13
+ ActiveRecord::Base.connection.should_not be_nil
14
+ ActiveRecord::Base.connection.class.should == ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter
15
+ end
16
+
17
+ end
18
+
19
+ describe "OracleEnhancedAdapter schema dump" do
20
+
21
+ before(:all) do
22
+ @old_conn = ActiveRecord::Base.oracle_connection(
23
+ :database => "xe",
24
+ :username => "hr",
25
+ :password => "hr")
26
+ @old_conn.class.should == ActiveRecord::ConnectionAdapters::OracleAdapter
27
+ @new_conn = ActiveRecord::Base.oracle_enhanced_connection(
28
+ :database => "xe",
29
+ :username => "hr",
30
+ :password => "hr")
31
+ @new_conn.class.should == ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter
32
+ end
33
+
34
+ it "should return the same tables list as original oracle adapter" do
35
+ @new_conn.tables.should == @old_conn.tables
36
+ end
37
+
38
+ it "should return the same pk_and_sequence_for as original oracle adapter" do
39
+ @new_conn.tables.each do |t|
40
+ @new_conn.pk_and_sequence_for(t).should == @old_conn.pk_and_sequence_for(t)
41
+ end
42
+ end
43
+
44
+ it "should return the same structure dump as original oracle adapter" do
45
+ @new_conn.structure_dump.should == @old_conn.structure_dump
46
+ end
47
+
48
+ it "should return the same structure drop as original oracle adapter" do
49
+ @new_conn.structure_drop.should == @old_conn.structure_drop
50
+ end
51
+
52
+ end
53
+
54
+ describe "OracleEnhancedAdapter database session store" do
55
+ before(:all) do
56
+ ActiveRecord::Base.establish_connection(:adapter => "oracle_enhanced",
57
+ :database => "xe",
58
+ :username => "hr",
59
+ :password => "hr")
60
+ @conn = ActiveRecord::Base.connection
61
+ @conn.execute <<-SQL
62
+ CREATE TABLE sessions (
63
+ id NUMBER(38,0) NOT NULL,
64
+ session_id VARCHAR2(255) DEFAULT NULL,
65
+ data CLOB DEFAULT NULL,
66
+ updated_at DATE DEFAULT NULL,
67
+ PRIMARY KEY (ID)
68
+ )
69
+ SQL
70
+ @conn.execute <<-SQL
71
+ CREATE SEQUENCE sessions_seq MINVALUE 1 MAXVALUE 999999999999999999999999999
72
+ INCREMENT BY 1 START WITH 10040 CACHE 20 NOORDER NOCYCLE
73
+ SQL
74
+ end
75
+
76
+ after(:all) do
77
+ @conn.execute "DROP TABLE sessions"
78
+ @conn.execute "DROP SEQUENCE sessions_seq"
79
+ end
80
+
81
+ it "should create sessions table" do
82
+ ActiveRecord::Base.connection.tables.grep("sessions").should_not be_empty
83
+ end
84
+
85
+ it "should save session data" do
86
+ @session = CGI::Session::ActiveRecordStore::Session.new :session_id => "123456", :data => "something", :updated_at => Time.now
87
+ @session.save!
88
+ @session = CGI::Session::ActiveRecordStore::Session.find_by_session_id("123456")
89
+ @session.data.should == "something"
90
+ end
91
+
92
+ end
93
+
94
+ describe "OracleEnhancedAdapter date type detection based on column names" do
95
+ before(:all) do
96
+ ActiveRecord::Base.establish_connection(:adapter => "oracle_enhanced",
97
+ :database => "xe",
98
+ :username => "hr",
99
+ :password => "hr")
100
+ @conn = ActiveRecord::Base.connection
101
+ @conn.execute <<-SQL
102
+ CREATE TABLE test_employees (
103
+ employee_id NUMBER(6,0),
104
+ first_name VARCHAR2(20),
105
+ last_name VARCHAR2(25),
106
+ email VARCHAR2(25),
107
+ phone_number VARCHAR2(20),
108
+ hire_date DATE,
109
+ job_id NUMBER(6,0),
110
+ salary NUMBER(8,2),
111
+ commission_pct NUMBER(2,2),
112
+ manager_id NUMBER(6,0),
113
+ department_id NUMBER(4,0),
114
+ created_at DATE
115
+ )
116
+ SQL
117
+ @conn.execute <<-SQL
118
+ CREATE SEQUENCE test_employees_seq MINVALUE 1
119
+ INCREMENT BY 1 START WITH 10040 CACHE 20 NOORDER NOCYCLE
120
+ SQL
121
+ end
122
+
123
+ after(:all) do
124
+ @conn.execute "DROP TABLE test_employees"
125
+ @conn.execute "DROP SEQUENCE test_employees_seq"
126
+ end
127
+
128
+ it "should set DATE column type as datetime if emulate_dates_by_column_name is false" do
129
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates_by_column_name = false
130
+ columns = @conn.columns('test_employees')
131
+ column = columns.detect{|c| c.name == "hire_date"}
132
+ column.type.should == :datetime
133
+ end
134
+
135
+ it "should set DATE column type as date if column name contains 'date' and emulate_dates_by_column_name is true" do
136
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates_by_column_name = true
137
+ columns = @conn.columns('test_employees')
138
+ column = columns.detect{|c| c.name == "hire_date"}
139
+ column.type.should == :date
140
+ end
141
+
142
+ it "should set DATE column type as datetime if column name does not contain 'date' and emulate_dates_by_column_name is true" do
143
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates_by_column_name = true
144
+ columns = @conn.columns('test_employees')
145
+ column = columns.detect{|c| c.name == "created_at"}
146
+ column.type.should == :datetime
147
+ end
148
+
149
+ it "should return Time value from DATE column if emulate_dates_by_column_name is false" do
150
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates_by_column_name = false
151
+ columns = @conn.columns('test_employees')
152
+ column = columns.detect{|c| c.name == "hire_date"}
153
+ column.type_cast(Time.now).class.should == Time
154
+ end
155
+
156
+ it "should return Date value from DATE column if column name contains 'date' and emulate_dates_by_column_name is true" do
157
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates_by_column_name = true
158
+ columns = @conn.columns('test_employees')
159
+ column = columns.detect{|c| c.name == "hire_date"}
160
+ column.type_cast(Time.now).class.should == Date
161
+ end
162
+
163
+ describe "/ DATE values from ActiveRecord model" do
164
+ before(:all) do
165
+ class TestEmployee < ActiveRecord::Base
166
+ set_primary_key :employee_id
167
+ end
168
+ end
169
+
170
+ before(:each) do
171
+ @employee = TestEmployee.create(
172
+ :first_name => "First",
173
+ :last_name => "Last",
174
+ :hire_date => Date.today,
175
+ :created_at => Time.now
176
+ )
177
+ end
178
+
179
+ it "should return Time value from DATE column if emulate_dates_by_column_name is false" do
180
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates_by_column_name = false
181
+ @employee.reload
182
+ @employee.hire_date.class.should == Time
183
+ end
184
+
185
+ it "should return Date value from DATE column if column name contains 'date' and emulate_dates_by_column_name is true" do
186
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates_by_column_name = true
187
+ @employee.reload
188
+ @employee.hire_date.class.should == Date
189
+ end
190
+
191
+ it "should return Time value from DATE column if column name does not contain 'date' and emulate_dates_by_column_name is true" do
192
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates_by_column_name = true
193
+ @employee.reload
194
+ @employee.created_at.class.should == Time
195
+ end
196
+
197
+ end
198
+
199
+ end
200
+
201
+ describe "OracleEnhancedAdapter integer type detection based on column names" do
202
+ before(:all) do
203
+ ActiveRecord::Base.establish_connection(:adapter => "oracle_enhanced",
204
+ :database => "xe",
205
+ :username => "hr",
206
+ :password => "hr")
207
+ @conn = ActiveRecord::Base.connection
208
+ @conn.execute <<-SQL
209
+ CREATE TABLE test2_employees (
210
+ id NUMBER,
211
+ first_name VARCHAR2(20),
212
+ last_name VARCHAR2(25),
213
+ email VARCHAR2(25),
214
+ phone_number VARCHAR2(20),
215
+ hire_date DATE,
216
+ job_id NUMBER,
217
+ salary NUMBER,
218
+ commission_pct NUMBER(2,2),
219
+ manager_id NUMBER(6),
220
+ department_id NUMBER(4,0),
221
+ created_at DATE
222
+ )
223
+ SQL
224
+ @conn.execute <<-SQL
225
+ CREATE SEQUENCE test2_employees_seq MINVALUE 1
226
+ INCREMENT BY 1 START WITH 10040 CACHE 20 NOORDER NOCYCLE
227
+ SQL
228
+ end
229
+
230
+ after(:all) do
231
+ @conn.execute "DROP TABLE test2_employees"
232
+ @conn.execute "DROP SEQUENCE test2_employees_seq"
233
+ end
234
+
235
+ it "should set NUMBER column type as decimal if emulate_integers_by_column_name is false" do
236
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_integers_by_column_name = false
237
+ columns = @conn.columns('test2_employees')
238
+ column = columns.detect{|c| c.name == "job_id"}
239
+ column.type.should == :decimal
240
+ end
241
+
242
+ it "should set NUMBER column type as integer if emulate_integers_by_column_name is true" do
243
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_integers_by_column_name = true
244
+ columns = @conn.columns('test2_employees')
245
+ column = columns.detect{|c| c.name == "job_id"}
246
+ column.type.should == :integer
247
+ column = columns.detect{|c| c.name == "id"}
248
+ column.type.should == :integer
249
+ end
250
+
251
+ it "should set NUMBER column type as decimal if column name does not contain 'id' and emulate_integers_by_column_name is true" do
252
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_integers_by_column_name = true
253
+ columns = @conn.columns('test2_employees')
254
+ column = columns.detect{|c| c.name == "salary"}
255
+ column.type.should == :decimal
256
+ end
257
+
258
+ it "should return BigDecimal value from NUMBER column if emulate_integers_by_column_name is false" do
259
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_integers_by_column_name = false
260
+ columns = @conn.columns('test2_employees')
261
+ column = columns.detect{|c| c.name == "job_id"}
262
+ column.type_cast(1.0).class.should == BigDecimal
263
+ end
264
+
265
+ it "should return Fixnum value from NUMBER column if column name contains 'id' and emulate_integers_by_column_name is true" do
266
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_integers_by_column_name = true
267
+ columns = @conn.columns('test2_employees')
268
+ column = columns.detect{|c| c.name == "job_id"}
269
+ column.type_cast(1.0).class.should == Fixnum
270
+ end
271
+
272
+ describe "/ NUMBER values from ActiveRecord model" do
273
+ before(:each) do
274
+ class Test2Employee < ActiveRecord::Base
275
+ end
276
+ end
277
+
278
+ after(:each) do
279
+ Object.send(:remove_const, "Test2Employee")
280
+ end
281
+
282
+ def create_employee2
283
+ @employee2 = Test2Employee.create(
284
+ :first_name => "First",
285
+ :last_name => "Last",
286
+ :job_id => 1,
287
+ :salary => 1000
288
+ )
289
+ @employee2.reload
290
+ end
291
+
292
+ it "should return BigDecimal value from NUMBER column if emulate_integers_by_column_name is false" do
293
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_integers_by_column_name = false
294
+ create_employee2
295
+ @employee2.job_id.class.should == BigDecimal
296
+ end
297
+
298
+ it "should return Fixnum value from NUMBER column if column name contains 'id' and emulate_integers_by_column_name is true" do
299
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_integers_by_column_name = true
300
+ create_employee2
301
+ @employee2.job_id.class.should == Fixnum
302
+ end
303
+
304
+ it "should return BigDecimal value from NUMBER column if column name does not contain 'id' and emulate_integers_by_column_name is true" do
305
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_integers_by_column_name = true
306
+ create_employee2
307
+ @employee2.salary.class.should == BigDecimal
308
+ end
309
+
310
+ end
311
+
312
+ end
313
+
314
+ describe "OracleEnhancedAdapter boolean type detection based on string column types and names" do
315
+ before(:all) do
316
+ ActiveRecord::Base.establish_connection(:adapter => "oracle_enhanced",
317
+ :database => "xe",
318
+ :username => "hr",
319
+ :password => "hr")
320
+ @conn = ActiveRecord::Base.connection
321
+ @conn.execute <<-SQL
322
+ CREATE TABLE test3_employees (
323
+ id NUMBER,
324
+ first_name VARCHAR2(20),
325
+ last_name VARCHAR2(25),
326
+ email VARCHAR2(25),
327
+ phone_number VARCHAR2(20),
328
+ hire_date DATE,
329
+ job_id NUMBER,
330
+ salary NUMBER,
331
+ commission_pct NUMBER(2,2),
332
+ manager_id NUMBER(6),
333
+ department_id NUMBER(4,0),
334
+ created_at DATE,
335
+ has_email CHAR(1),
336
+ has_phone VARCHAR2(1),
337
+ active_flag VARCHAR2(2),
338
+ manager_yn VARCHAR2(3)
339
+ )
340
+ SQL
341
+ @conn.execute <<-SQL
342
+ CREATE SEQUENCE test3_employees_seq MINVALUE 1
343
+ INCREMENT BY 1 START WITH 10040 CACHE 20 NOORDER NOCYCLE
344
+ SQL
345
+ end
346
+
347
+ after(:all) do
348
+ @conn.execute "DROP TABLE test3_employees"
349
+ @conn.execute "DROP SEQUENCE test3_employees_seq"
350
+ end
351
+
352
+ it "should set CHAR/VARCHAR2 column type as string if emulate_booleans_from_strings is false" do
353
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans_from_strings = false
354
+ columns = @conn.columns('test3_employees')
355
+ %w(has_email has_phone active_flag manager_yn).each do |col|
356
+ column = columns.detect{|c| c.name == col}
357
+ column.type.should == :string
358
+ end
359
+ end
360
+
361
+ it "should set CHAR/VARCHAR2 column type as boolean if emulate_booleans_from_strings is true" do
362
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans_from_strings = true
363
+ columns = @conn.columns('test3_employees')
364
+ %w(has_email has_phone active_flag manager_yn).each do |col|
365
+ column = columns.detect{|c| c.name == col}
366
+ column.type.should == :boolean
367
+ end
368
+ end
369
+
370
+ it "should set VARCHAR2 column type as string if column name does not contain 'flag' or 'yn' and emulate_booleans_from_strings is true" do
371
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans_from_strings = true
372
+ columns = @conn.columns('test3_employees')
373
+ %w(phone_number email).each do |col|
374
+ column = columns.detect{|c| c.name == col}
375
+ column.type.should == :string
376
+ end
377
+ end
378
+
379
+ it "should return string value from VARCHAR2 boolean column if emulate_booleans_from_strings is false" do
380
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans_from_strings = false
381
+ columns = @conn.columns('test3_employees')
382
+ %w(has_email has_phone active_flag manager_yn).each do |col|
383
+ column = columns.detect{|c| c.name == col}
384
+ column.type_cast("Y").class.should == String
385
+ end
386
+ end
387
+
388
+ it "should return boolean value from VARCHAR2 boolean column if emulate_booleans_from_strings is true" do
389
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans_from_strings = true
390
+ columns = @conn.columns('test3_employees')
391
+ %w(has_email has_phone active_flag manager_yn).each do |col|
392
+ column = columns.detect{|c| c.name == col}
393
+ column.type_cast("Y").class.should == TrueClass
394
+ column.type_cast("N").class.should == FalseClass
395
+ end
396
+ end
397
+
398
+ describe "/ VARCHAR2 boolean values from ActiveRecord model" do
399
+ before(:each) do
400
+ class Test3Employee < ActiveRecord::Base
401
+ end
402
+ end
403
+
404
+ after(:each) do
405
+ Object.send(:remove_const, "Test3Employee")
406
+ end
407
+
408
+ def create_employee3
409
+ @employee3 = Test3Employee.create(
410
+ :first_name => "First",
411
+ :last_name => "Last",
412
+ :has_email => true,
413
+ :has_phone => false,
414
+ :active_flag => true,
415
+ :manager_yn => false
416
+ )
417
+ @employee3.reload
418
+ end
419
+
420
+ it "should return String value from VARCHAR2 boolean column if emulate_booleans_from_strings is false" do
421
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans_from_strings = false
422
+ create_employee3
423
+ %w(has_email has_phone active_flag manager_yn).each do |col|
424
+ @employee3.send(col.to_sym).class.should == String
425
+ end
426
+ end
427
+
428
+ it "should return boolean value from VARCHAR2 boolean column if emulate_booleans_from_strings is true" do
429
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans_from_strings = true
430
+ create_employee3
431
+ %w(has_email active_flag).each do |col|
432
+ @employee3.send(col.to_sym).class.should == TrueClass
433
+ @employee3.send((col+"_before_type_cast").to_sym).should == "Y"
434
+ end
435
+ %w(has_phone manager_yn).each do |col|
436
+ @employee3.send(col.to_sym).class.should == FalseClass
437
+ @employee3.send((col+"_before_type_cast").to_sym).should == "N"
438
+ end
439
+ end
440
+
441
+ it "should return string value from VARCHAR2 column if it is not boolean column and emulate_booleans_from_strings is true" do
442
+ ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans_from_strings = true
443
+ create_employee3
444
+ @employee3.first_name.class.should == String
445
+ end
446
+
447
+ end
448
+
449
+ end
450
+