activerecord-jdbc-adapter 1.2.5 → 1.2.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. data/.gitignore +1 -0
  2. data/.travis.yml +5 -1
  3. data/Appraisals +5 -5
  4. data/Gemfile +9 -1
  5. data/Gemfile.lock +44 -10
  6. data/History.txt +126 -2
  7. data/README.md +246 -0
  8. data/Rakefile +34 -25
  9. data/activerecord-jdbc-adapter.gemspec +1 -1
  10. data/gemfiles/rails23.gemfile +5 -3
  11. data/gemfiles/rails23.gemfile.lock +26 -18
  12. data/gemfiles/rails30.gemfile +4 -2
  13. data/gemfiles/rails30.gemfile.lock +16 -8
  14. data/gemfiles/rails31.gemfile +4 -2
  15. data/gemfiles/rails31.gemfile.lock +16 -9
  16. data/gemfiles/rails32.gemfile +4 -2
  17. data/gemfiles/rails32.gemfile.lock +15 -8
  18. data/lib/active_record/connection_adapters/db2_adapter.rb +1 -0
  19. data/lib/arel/visitors/sql_server.rb +3 -0
  20. data/lib/arjdbc.rb +3 -5
  21. data/lib/arjdbc/db2.rb +1 -0
  22. data/lib/arjdbc/db2/adapter.rb +302 -196
  23. data/lib/arjdbc/db2/connection_methods.rb +18 -0
  24. data/lib/arjdbc/derby/active_record_patch.rb +12 -0
  25. data/lib/arjdbc/derby/adapter.rb +180 -158
  26. data/lib/arjdbc/derby/connection_methods.rb +5 -1
  27. data/lib/arjdbc/firebird/adapter.rb +27 -19
  28. data/lib/arjdbc/h2/adapter.rb +162 -7
  29. data/lib/arjdbc/h2/connection_methods.rb +5 -1
  30. data/lib/arjdbc/hsqldb.rb +1 -1
  31. data/lib/arjdbc/hsqldb/adapter.rb +96 -61
  32. data/lib/arjdbc/hsqldb/connection_methods.rb +5 -1
  33. data/lib/arjdbc/hsqldb/explain_support.rb +35 -0
  34. data/lib/arjdbc/informix/adapter.rb +56 -55
  35. data/lib/arjdbc/jdbc/adapter.rb +173 -86
  36. data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
  37. data/lib/arjdbc/jdbc/column.rb +28 -23
  38. data/lib/arjdbc/jdbc/connection.rb +10 -6
  39. data/lib/arjdbc/jdbc/driver.rb +13 -5
  40. data/lib/arjdbc/jdbc/serialized_attributes_helper.rb +21 -0
  41. data/lib/arjdbc/mssql.rb +1 -1
  42. data/lib/arjdbc/mssql/adapter.rb +51 -53
  43. data/lib/arjdbc/mssql/connection_methods.rb +8 -1
  44. data/lib/arjdbc/mysql.rb +1 -1
  45. data/lib/arjdbc/mysql/adapter.rb +186 -150
  46. data/lib/arjdbc/mysql/connection_methods.rb +9 -9
  47. data/lib/arjdbc/mysql/explain_support.rb +85 -0
  48. data/lib/arjdbc/oracle.rb +1 -1
  49. data/lib/arjdbc/oracle/adapter.rb +232 -125
  50. data/lib/arjdbc/oracle/connection_methods.rb +2 -2
  51. data/lib/arjdbc/postgresql.rb +1 -1
  52. data/lib/arjdbc/postgresql/adapter.rb +134 -86
  53. data/lib/arjdbc/postgresql/connection_methods.rb +6 -4
  54. data/lib/arjdbc/postgresql/explain_support.rb +55 -0
  55. data/lib/arjdbc/sqlite3.rb +1 -1
  56. data/lib/arjdbc/sqlite3/adapter.rb +176 -108
  57. data/lib/arjdbc/sqlite3/connection_methods.rb +5 -5
  58. data/lib/arjdbc/sqlite3/explain_support.rb +32 -0
  59. data/lib/arjdbc/sybase/adapter.rb +7 -6
  60. data/lib/arjdbc/version.rb +1 -1
  61. data/pom.xml +1 -1
  62. data/rakelib/02-test.rake +9 -11
  63. data/rakelib/rails.rake +18 -10
  64. data/src/java/arjdbc/db2/DB2Module.java +70 -0
  65. data/src/java/arjdbc/derby/DerbyModule.java +24 -5
  66. data/src/java/arjdbc/hsqldb/HSQLDBModule.java +66 -0
  67. data/src/java/arjdbc/jdbc/AdapterJavaService.java +14 -7
  68. data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +111 -89
  69. data/src/java/arjdbc/mysql/MySQLModule.java +79 -70
  70. data/src/java/arjdbc/oracle/OracleModule.java +74 -0
  71. data/src/java/arjdbc/oracle/OracleRubyJdbcConnection.java +5 -10
  72. data/src/java/arjdbc/sqlite3/SQLite3Module.java +77 -0
  73. data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +127 -0
  74. data/src/java/arjdbc/sqlite3/Sqlite3RubyJdbcConnection.java +25 -111
  75. data/src/java/arjdbc/util/QuotingUtils.java +104 -0
  76. data/test/abstract_db_create.rb +6 -6
  77. data/test/activerecord/connection_adapters/type_conversion_test.rb +2 -2
  78. data/test/assets/flowers.jpg +0 -0
  79. data/test/binary.rb +67 -0
  80. data/test/db/db2.rb +30 -7
  81. data/test/db/jdbc.rb +4 -2
  82. data/test/db/oracle.rb +18 -27
  83. data/test/db2_binary_test.rb +6 -0
  84. data/test/db2_serialize_test.rb +6 -0
  85. data/test/db2_simple_test.rb +20 -25
  86. data/test/db2_test.rb +71 -0
  87. data/test/derby_binary_test.rb +6 -0
  88. data/test/derby_migration_test.rb +42 -35
  89. data/test/derby_reset_column_information_test.rb +1 -0
  90. data/test/derby_row_locking_test.rb +17 -0
  91. data/test/derby_schema_dump_test.rb +9 -0
  92. data/test/derby_serialize_test.rb +6 -0
  93. data/test/derby_simple_test.rb +59 -17
  94. data/test/generic_jdbc_connection_test.rb +112 -5
  95. data/test/h2_binary_test.rb +6 -0
  96. data/test/h2_change_column_test.rb +1 -1
  97. data/test/h2_schema_dump_test.rb +25 -0
  98. data/test/h2_serialize_test.rb +6 -0
  99. data/test/h2_simple_test.rb +23 -9
  100. data/test/has_many_through.rb +18 -4
  101. data/test/hsqldb_binary_test.rb +6 -0
  102. data/test/hsqldb_schema_dump_test.rb +15 -0
  103. data/test/hsqldb_serialize_test.rb +6 -0
  104. data/test/hsqldb_simple_test.rb +1 -0
  105. data/test/informix_simple_test.rb +1 -1
  106. data/test/jdbc/db2.rb +23 -0
  107. data/test/jdbc/oracle.rb +23 -0
  108. data/test/jdbc_common.rb +3 -110
  109. data/test/jndi_callbacks_test.rb +0 -2
  110. data/test/jndi_test.rb +2 -0
  111. data/test/models/binary.rb +18 -0
  112. data/test/models/custom_pk_name.rb +1 -0
  113. data/test/models/data_types.rb +11 -2
  114. data/test/models/entry.rb +1 -1
  115. data/test/models/string_id.rb +2 -2
  116. data/test/models/thing.rb +1 -1
  117. data/test/models/topic.rb +32 -0
  118. data/test/mssql_legacy_types_test.rb +1 -1
  119. data/test/mssql_limit_offset_test.rb +13 -3
  120. data/test/mssql_serialize_test.rb +6 -0
  121. data/test/mysql_binary_test.rb +6 -0
  122. data/test/mysql_schema_dump_test.rb +220 -0
  123. data/test/mysql_serialize_test.rb +6 -0
  124. data/test/mysql_simple_test.rb +22 -2
  125. data/test/mysql_test.rb +93 -0
  126. data/test/oracle_binary_test.rb +6 -0
  127. data/test/oracle_limit_test.rb +2 -1
  128. data/test/oracle_serialize_test.rb +6 -0
  129. data/test/oracle_simple_test.rb +61 -0
  130. data/test/oracle_specific_test.rb +77 -26
  131. data/test/postgres_binary_test.rb +6 -0
  132. data/test/postgres_native_type_mapping_test.rb +12 -11
  133. data/test/postgres_nonseq_pkey_test.rb +1 -0
  134. data/test/postgres_reserved_test.rb +1 -0
  135. data/test/postgres_reset_column_information_test.rb +1 -0
  136. data/test/postgres_row_locking_test.rb +21 -0
  137. data/test/postgres_schema_dump_test.rb +88 -0
  138. data/test/postgres_schema_search_path_test.rb +1 -0
  139. data/test/postgres_simple_test.rb +62 -89
  140. data/test/postgres_table_alias_length_test.rb +1 -0
  141. data/test/postgres_test.rb +31 -0
  142. data/test/postgres_type_conversion_test.rb +16 -16
  143. data/test/row_locking.rb +69 -64
  144. data/test/schema_dump.rb +168 -0
  145. data/test/serialize.rb +277 -0
  146. data/test/simple.rb +326 -122
  147. data/test/sqlite3_serialize_test.rb +6 -0
  148. data/test/sqlite3_simple_test.rb +51 -84
  149. data/test/sqlite3_type_conversion_test.rb +101 -0
  150. data/test/test_helper.rb +224 -0
  151. metadata +325 -366
  152. data/README.rdoc +0 -214
  153. data/test/db/logger.rb +0 -3
  154. data/test/derby_multibyte_test.rb +0 -11
  155. data/test/mysql_info_test.rb +0 -123
@@ -0,0 +1,6 @@
1
+ require 'db/sqlite3'
2
+ require 'serialize'
3
+
4
+ class SQLite3SerializeTest < Test::Unit::TestCase
5
+ include SerializeTestMethods
6
+ end
@@ -3,6 +3,12 @@ require 'db/sqlite3'
3
3
  require 'models/data_types'
4
4
  require 'models/validates_uniqueness_of_string'
5
5
 
6
+ #module Rails
7
+ # def self.logger
8
+ # ActiveRecord::Base.logger
9
+ # end
10
+ #end
11
+
6
12
  class SQLite3SimpleTest < Test::Unit::TestCase
7
13
  include SimpleTestMethods
8
14
  include ActiveRecord3TestMethods
@@ -11,22 +17,22 @@ class SQLite3SimpleTest < Test::Unit::TestCase
11
17
  include XmlColumnTests
12
18
 
13
19
  def test_recreate_database
14
- assert @connection.tables.include?(Entry.table_name)
15
- db = @connection.database_name
16
- @connection.recreate_database(db)
17
- assert !@connection.tables.include?(Entry.table_name)
20
+ assert connection.tables.include?(Entry.table_name)
21
+ db = connection.database_name
22
+ connection.recreate_database(db)
23
+ assert ! connection.tables.include?(Entry.table_name)
18
24
  self.setup # avoid teardown complaining
19
25
  end
20
26
 
21
27
  def test_execute_insert
22
28
  assert_equal 1, Entry.count
23
- id = @connection.execute "INSERT INTO entries (title, content) VALUES ('Execute Insert', 'This now works with SQLite3')"
29
+ id = connection.execute "INSERT INTO entries (title, content) VALUES ('Execute Insert', 'This now works with SQLite3')"
24
30
  assert_equal Entry.last.id, id
25
31
  assert_equal 2, Entry.count
26
32
  end
27
33
 
28
34
  def test_execute_update
29
- affected_rows = @connection.execute "UPDATE entries SET title = 'Execute Update' WHERE id = #{Entry.first.id}"
35
+ affected_rows = connection.execute "UPDATE entries SET title = 'Execute Update' WHERE id = #{Entry.first.id}"
30
36
  assert_equal 1, affected_rows
31
37
  assert_equal 'Execute Update', Entry.first.title
32
38
  end
@@ -79,7 +85,7 @@ class SQLite3SimpleTest < Test::Unit::TestCase
79
85
  end
80
86
 
81
87
  def test_rename_column_preserves_content
82
- post = Entry.find(:first)
88
+ post = Entry.first
83
89
  assert_equal @title, post.title
84
90
  assert_equal @content, post.content
85
91
  assert_equal @rating, post.rating
@@ -90,14 +96,14 @@ class SQLite3SimpleTest < Test::Unit::TestCase
90
96
  end
91
97
  end
92
98
 
93
- post = Entry.find(:first)
99
+ post = Entry.first
94
100
  assert_equal @title, post.name
95
101
  assert_equal @content, post.content
96
102
  assert_equal @rating, post.rating
97
103
  end
98
104
 
99
105
  def test_rename_column_preserves_index
100
- assert_equal(0, @connection.indexes(:entries).size)
106
+ assert_equal(0, connection.indexes(:entries).size)
101
107
 
102
108
  index_name = "entries_index"
103
109
 
@@ -107,7 +113,7 @@ class SQLite3SimpleTest < Test::Unit::TestCase
107
113
  end
108
114
  end
109
115
 
110
- indexes = @connection.indexes(:entries)
116
+ indexes = connection.indexes(:entries)
111
117
  assert_equal(1, indexes.size)
112
118
  assert_equal "entries", indexes.first.table.to_s
113
119
  assert_equal index_name, indexes.first.name
@@ -120,7 +126,7 @@ class SQLite3SimpleTest < Test::Unit::TestCase
120
126
  end
121
127
  end
122
128
 
123
- indexes = @connection.indexes(:entries)
129
+ indexes = connection.indexes(:entries)
124
130
  assert_equal(1, indexes.size)
125
131
  assert_equal "entries", indexes.first.table.to_s
126
132
  assert_equal index_name, indexes.first.name
@@ -221,81 +227,42 @@ class SQLite3SimpleTest < Test::Unit::TestCase
221
227
  assert_equal 9, rating_column.precision
222
228
  assert_equal 7, rating_column.scale
223
229
  end
224
- end
225
-
226
- # assert_raise ActiveRecord::RecordInvalid do
227
-
228
- class SQLite3HasManyThroughTest < Test::Unit::TestCase
229
- include HasManyThroughMethods
230
- end
231
230
 
232
- if jruby?
233
- JInteger = java.lang.Integer
234
- else
235
- JInteger = Fixnum
236
- class Fixnum
237
- # Arbitrary value...we could pick
238
- MAX_VALUE = 2
231
+ def test_delete_sql
232
+ ActiveRecord::Base.connection.send :delete_sql, "DELETE FROM entries"
233
+ assert Entry.all.empty?
239
234
  end
240
- end
241
-
242
- class SQLite3TypeConversionTest < Test::Unit::TestCase
243
- TEST_TIME = Time.at(1169964202)
244
- TEST_BINARY = "Some random binary data % \0 and then some"
245
- def setup
246
- DbTypeMigration.up
247
- DbType.create(
248
- :sample_timestamp => TEST_TIME,
249
- :sample_datetime => TEST_TIME,
250
- :sample_time => TEST_TIME,
251
- :sample_date => TEST_TIME,
252
- :sample_decimal => JInteger::MAX_VALUE + 1,
253
- :sample_small_decimal => 3.14,
254
- :sample_binary => TEST_BINARY)
255
- DbType.create(
256
- :sample_timestamp => TEST_TIME,
257
- :sample_datetime => TEST_TIME,
258
- :sample_time => TEST_TIME,
259
- :sample_date => TEST_TIME,
260
- :sample_decimal => JInteger::MAX_VALUE + 1,
261
- :sample_small_decimal => 1.0,
262
- :sample_binary => TEST_BINARY)
263
- end
264
-
265
- def teardown
266
- DbTypeMigration.down
267
- end
268
-
269
- def test_decimal
270
- types = DbType.find(:first)
271
- assert_equal((JInteger::MAX_VALUE + 1), types.sample_decimal)
272
- end
273
-
274
- def test_decimal_scale
275
- types = DbType.find(:first)
276
- assert_equal(2, DbType.columns_hash["sample_small_decimal"].scale)
277
- end
278
-
279
- def test_decimal_precision
280
- types = DbType.find(:first)
281
- assert_equal(3, DbType.columns_hash["sample_small_decimal"].precision)
282
- end
283
-
284
- def test_binary
285
- types = DbType.find(:first)
286
- assert_equal(TEST_BINARY, types.sample_binary)
287
- end
288
-
289
- def test_small_decimal
290
- types = DbType.find(:all, :order => ["sample_small_decimal desc"])
291
- assert_equal(3.14, types[0].sample_small_decimal)
292
- assert_equal(1.0, types[1].sample_small_decimal)
235
+
236
+ # #override
237
+ def test_big_decimal
238
+ #ActiveRecord::Base.logger.level = Logger::DEBUG
239
+
240
+ test_value = 1234567890.0 # FINE just like native adapter
241
+ db_type = DbType.create!(:big_decimal => test_value)
242
+ db_type = DbType.find(db_type.id)
243
+ assert_equal test_value, db_type.big_decimal
244
+
245
+ test_value = 1234567890_123456 # FINE just like native adapter
246
+ db_type = DbType.create!(:big_decimal => test_value)
247
+ db_type = DbType.find(db_type.id)
248
+ assert_equal test_value, db_type.big_decimal
249
+
250
+ # NOTE: this is getting f*cked up in the native adapter as well although
251
+ # differently and only when inserted manually - works with PSs (3.1+) :
252
+ test_value = 1234567890_1234567890.0
253
+ db_type = DbType.create!(:big_decimal => test_value)
254
+ db_type = DbType.find(db_type.id)
255
+ # TODO native gets us 12345678901234567000.0 JDBC gets us 1
256
+ #assert_equal test_value, db_type.big_decimal
257
+ #super
258
+ ensure
259
+ #ActiveRecord::Base.logger.level = Logger::WARN
293
260
  end
261
+
262
+ include ExplainSupportTestMethods if ar_version("3.1")
263
+
264
+ end
294
265
 
295
- def test_small_decimal_with_ordering
296
- types = DbType.find(:all, :order => ["sample_small_decimal asc"])
297
- types[1].sample_small_decimal
298
- assert_equal(1.0, types[0].sample_small_decimal)
299
- assert_equal(3.14, types[1].sample_small_decimal)
300
- end
266
+ class SQLite3HasManyThroughTest < Test::Unit::TestCase
267
+ include HasManyThroughMethods
301
268
  end
@@ -0,0 +1,101 @@
1
+ # encoding: utf-8
2
+ require 'jdbc_common'
3
+ require 'db/sqlite3'
4
+
5
+ class SQLite3TypeConversionTest < Test::Unit::TestCase
6
+
7
+ if defined?(JRUBY_VERSION)
8
+ JInteger = java.lang.Integer
9
+ else
10
+ JInteger = Fixnum
11
+ class Fixnum
12
+ # Arbitrary value...we could pick
13
+ MAX_VALUE = 2
14
+ end
15
+ end
16
+
17
+ TEST_TIME = Time.at(1169964202)
18
+ TEST_BINARY = "Some random binary data % \0 and then some"
19
+
20
+ def self.startup; DbTypeMigration.up; end
21
+ def self.shutdown; DbTypeMigration.down; end
22
+
23
+ def setup
24
+ super
25
+ DbType.delete_all
26
+ DbType.create(
27
+ :sample_timestamp => TEST_TIME,
28
+ :sample_datetime => TEST_TIME,
29
+ :sample_time => TEST_TIME,
30
+ :sample_date => TEST_TIME,
31
+ :sample_decimal => JInteger::MAX_VALUE + 1,
32
+ :sample_small_decimal => 3.14,
33
+ :sample_binary => TEST_BINARY)
34
+ DbType.create(
35
+ :sample_timestamp => TEST_TIME,
36
+ :sample_datetime => TEST_TIME,
37
+ :sample_time => TEST_TIME,
38
+ :sample_date => TEST_TIME,
39
+ :sample_decimal => JInteger::MAX_VALUE + 1,
40
+ :sample_small_decimal => 1.0,
41
+ :sample_binary => TEST_BINARY)
42
+ end
43
+
44
+ def test_decimal
45
+ types = DbType.first
46
+ assert_equal((JInteger::MAX_VALUE + 1), types.sample_decimal)
47
+ end
48
+
49
+ def test_decimal_scale
50
+ assert_equal(2, DbType.columns_hash["sample_small_decimal"].scale)
51
+ end
52
+
53
+ def test_decimal_precision
54
+ assert_equal(3, DbType.columns_hash["sample_small_decimal"].precision)
55
+ end
56
+
57
+ def test_small_decimal
58
+ types = DbType.all :order => "sample_small_decimal DESC"
59
+ assert_equal(3.14, types[0].sample_small_decimal)
60
+ assert_equal(1.0, types[1].sample_small_decimal)
61
+ end
62
+
63
+ def test_small_decimal_with_ordering
64
+ types = DbType.all :order => "sample_small_decimal ASC"
65
+ types[1].sample_small_decimal
66
+ assert_equal(1.0, types[0].sample_small_decimal)
67
+ assert_equal(3.14, types[1].sample_small_decimal)
68
+ end
69
+
70
+ def test_binary
71
+ types = DbType.first
72
+ assert_equal(TEST_BINARY, types.sample_binary)
73
+ end
74
+
75
+ class DualEncoding < ActiveRecord::Base
76
+ end
77
+
78
+ def test_quote_binary_column_escapes_it
79
+ DualEncoding.connection.execute(<<-eosql)
80
+ CREATE TABLE dual_encodings (
81
+ id integer PRIMARY KEY AUTOINCREMENT,
82
+ name string,
83
+ data binary
84
+ )
85
+ eosql
86
+ str = "01 \x80"
87
+ str.force_encoding('ASCII-8BIT') if str.respond_to?(:force_encoding)
88
+ binary = DualEncoding.new :name => '12ščťžýáííéúäô', :data => str
89
+ binary.save!
90
+ assert_equal str, binary.data
91
+ binary.reload
92
+ if str.respond_to?(:force_encoding)
93
+ assert_equal '12ščťžýáííéúäô'.force_encoding('UTF-8'), binary.name
94
+ assert_equal "01 \x80".force_encoding('ASCII-8BIT'), binary.data
95
+ else
96
+ assert_equal '12ščťžýáííéúäô', binary.name
97
+ assert_equal "01 \x80", binary.data
98
+ end
99
+ end
100
+
101
+ end
@@ -0,0 +1,224 @@
1
+ require 'helper'
2
+ require 'stringio'
3
+
4
+ begin
5
+ require 'bundler'
6
+ rescue LoadError => e
7
+ require('rubygems') && retry
8
+ raise e
9
+ end
10
+ Bundler.require(:default, :test)
11
+
12
+ require 'test/unit'
13
+ begin; require 'mocha/setup'; rescue LoadError; require 'mocha'; end
14
+
15
+ if ENV['COVERAGE']
16
+ require 'simplecov'
17
+ SimpleCov.start
18
+ puts 'started simplecov'
19
+ end
20
+
21
+ require 'arjdbc' if defined?(JRUBY_VERSION)
22
+
23
+ puts "Using ActiveRecord::VERSION = #{ActiveRecord::VERSION::STRING}"
24
+
25
+ # we always set-up logger (some tests fail if ActiveRecord::Base.logger is nil),
26
+ # but level is set to "warn" unless $DEBUG or ENV['DEBUG'] is set :
27
+ require 'logger'
28
+ ActiveRecord::Base.logger = Logger.new($stdout)
29
+ ActiveRecord::Base.logger.level = $DEBUG || ENV['DEBUG'] ? Logger::DEBUG : Logger::WARN
30
+
31
+ begin
32
+ require 'ruby-debug' if $DEBUG || ENV['DEBUG']
33
+ rescue LoadError
34
+ puts "ruby-debug missing thus won't be loaded"
35
+ end
36
+
37
+ # assert_queries and SQLCounter taken from rails active_record tests
38
+ class Test::Unit::TestCase
39
+
40
+ def self.ar_version(version)
41
+ match = version.match(/(\d+)\.(\d+)(?:\.(\d+))?/)
42
+ ActiveRecord::VERSION::MAJOR > match[1].to_i ||
43
+ (ActiveRecord::VERSION::MAJOR == match[1].to_i &&
44
+ ActiveRecord::VERSION::MINOR >= match[2].to_i)
45
+ end
46
+
47
+ def ar_version(version); self.class.ar_version(version); end
48
+
49
+ def with_java_connection(config = nil)
50
+ config ||= ActiveRecord::Base.connection.config
51
+ jdbc_driver = ActiveRecord::ConnectionAdapters::JdbcDriver.new(config[:driver])
52
+ begin
53
+ java_connection = jdbc_driver.connection(config[:url], config[:username], config[:password])
54
+ java_connection.setAutoCommit(true)
55
+ yield(java_connection)
56
+ ensure
57
+ java_connection.close rescue nil
58
+ end
59
+ end
60
+
61
+ def connection
62
+ @connection ||= ActiveRecord::Base.connection
63
+ end
64
+
65
+ def schema_dump
66
+ strio = StringIO.new
67
+ ActiveRecord::SchemaDumper::dump(connection, strio)
68
+ strio.string
69
+ end
70
+
71
+ # def teardown
72
+ # clear_active_connections!
73
+ # end
74
+ #
75
+ # def clear_active_connections!
76
+ # ActiveRecord::Base.clear_active_connections!
77
+ # end
78
+
79
+ protected
80
+
81
+ def assert_queries(count, matching = nil)
82
+ if ActiveRecord::SQLCounter.enabled?
83
+ log = ActiveRecord::SQLCounter.log = []
84
+ begin
85
+ yield
86
+ ensure
87
+ queries = queries = ( matching ? log.select { |s| s =~ matching } : log )
88
+ assert_equal count, queries.size,
89
+ "#{ queries.size } instead of #{ count } queries were executed." +
90
+ "#{ queries.size == 0 ? '' : "\nQueries:\n#{queries.join("\n")}" }"
91
+ end
92
+ else
93
+ warn "assert_queries skipped as SQLCounter is not enabled"
94
+ yield
95
+ end
96
+ end
97
+
98
+ # re-defined by Oracle
99
+ def assert_empty_string value
100
+ assert_equal '', value
101
+ end
102
+
103
+ # re-defined by Oracle
104
+ def assert_null_text value
105
+ assert_nil value
106
+ end
107
+
108
+ def assert_date_equal expected, actual
109
+ actual = actual_in_expected_time_zone(expected, actual)
110
+ actual = actual.to_date if actual.is_a?(Time)
111
+ assert_equal(expected.nil? ? nil : expected.to_date, actual)
112
+ end
113
+
114
+ def assert_time_equal expected, actual
115
+ actual = actual_in_expected_time_zone(expected, actual)
116
+ [ :hour, :min, :sec ].each do |method|
117
+ assert_equal expected.send(method), actual.send(method), "<#{expected}> but was <#{actual}> (differ at #{method.inspect})"
118
+ end
119
+ end
120
+
121
+ def assert_datetime_equal expected, actual
122
+ assert_date_equal expected, actual
123
+ assert_time_equal expected, actual
124
+ end
125
+
126
+ def assert_timestamp_equal expected, actual
127
+ e_utc = expected.utc; a_utc = actual.utc
128
+ [ :year, :month, :day, :hour, :min, :sec ].each do |method|
129
+ assert_equal e_utc.send(method), a_utc.send(method), "<#{expected}> but was <#{actual}> (differ at #{method.inspect})"
130
+ end
131
+ assert_equal e_utc.usec, a_utc.usec, "<#{expected}> but was <#{actual}> (differ at :usec)"
132
+ # :usec Ruby Time precision: 123_456 (although JRuby only supports ms with Time.now) :
133
+ #e_usec = ( e_utc.usec / 1000 ) * 1000
134
+ #a_usec = ( a_utc.usec / 1000 ) * 1000
135
+ #assert_equal e_usec, a_usec, "<#{expected}> but was <#{actual}> (differ at :usec / 1000)"
136
+ end
137
+
138
+ private
139
+
140
+ def actual_in_expected_time_zone(expected, actual)
141
+ if actual.is_a?(Time) && expected.respond_to?(:time_zone)
142
+ return actual.in_time_zone expected.time_zone
143
+ end
144
+ actual
145
+ end
146
+
147
+ end
148
+
149
+ module ActiveRecord
150
+ class SQLCounter
151
+
152
+ @@ignored_sql = [
153
+ /^PRAGMA (?!(table_info))/,
154
+ /^SELECT currval/,
155
+ /^SELECT CAST/,
156
+ /^SELECT @@IDENTITY/,
157
+ /^SELECT @@ROWCOUNT/,
158
+ /^SAVEPOINT/,
159
+ /^ROLLBACK TO SAVEPOINT/,
160
+ /^RELEASE SAVEPOINT/,
161
+ /^SHOW max_identifier_length/,
162
+ /^BEGIN/,
163
+ /^COMMIT/
164
+ ]
165
+ def self.ignored_sql; @@ignored_sql; end
166
+ def self.ignored_sql=(value)
167
+ @@ignored_sql = value || []
168
+ end
169
+
170
+ # FIXME: this needs to be refactored so specific database can add their own
171
+ # ignored SQL. This ignored SQL is for Oracle.
172
+ ignored_sql.concat [/^select .*nextval/i,
173
+ /^SAVEPOINT/,
174
+ /^ROLLBACK TO/,
175
+ /^\s*select .* from all_triggers/im
176
+ ]
177
+
178
+ @@log = []
179
+ def self.log; @@log; end
180
+ def self.log=(log); @@log = log;; end
181
+
182
+ def call(name, start, finish, message_id, values)
183
+ sql = values[:sql]
184
+
185
+ # FIXME: this seems bad. we should probably have a better way to indicate
186
+ # the query was cached
187
+ unless 'CACHE' == values[:name]
188
+ self.class.log << sql unless self.class.ignored_sql.any? { |r| sql =~ r }
189
+ end
190
+ end
191
+
192
+ @@enabled = true
193
+ def self.enabled?; @@enabled; end
194
+
195
+ begin
196
+ require 'active_support/notifications'
197
+ ActiveSupport::Notifications.subscribe('sql.active_record', SQLCounter.new)
198
+ rescue LoadError
199
+ @@enabled = false
200
+ end
201
+
202
+ end
203
+ end
204
+
205
+ # less (2.3) warnings otherwise the test console output is hard to read :
206
+ if ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR == 3
207
+ ActiveRecord::Migration.class_eval do
208
+ class << self # warning: instance variable @version not initialized
209
+ alias_method :_announce, :announce
210
+ def announce(message)
211
+ @version = nil unless defined?(@version)
212
+ _announce(message)
213
+ end
214
+ end
215
+ end
216
+ ActiveRecord::Base.class_eval do
217
+ def destroyed? # warning: instance variable @destroyed not initialized
218
+ defined?(@destroyed) && @destroyed # @destroyed
219
+ end
220
+ def new_record? # warning: instance variable @new_record not initialized
221
+ @new_record ||= false # @new_record || false
222
+ end
223
+ end
224
+ end