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
@@ -25,7 +25,7 @@ end
25
25
  class CreateUsers < ActiveRecord::Migration
26
26
  def self.up
27
27
  create_table "users", :force => true do |t|
28
- t.column :login, :string, :limit => 100
28
+ t.column :login, :string, :limit => 100, :null => false
29
29
  end
30
30
  end
31
31
 
@@ -1,7 +1,7 @@
1
1
  class CreateStringIds < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table "string_ids", :force => true, :id => false do |t|
4
- t.string :id
4
+ t.string :id, :null => false
5
5
  end
6
6
  end
7
7
 
@@ -11,7 +11,7 @@ class CreateStringIds < ActiveRecord::Migration
11
11
  end
12
12
 
13
13
  class StringId < ActiveRecord::Base
14
- def self.table_name () "string_ids" end
14
+ def self.table_name; "string_ids"; end
15
15
  # Fake out a table without a primary key
16
16
  self.primary_key = "id"
17
17
  end
@@ -2,9 +2,9 @@ class CreateThings < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :things, :id => false do |t|
4
4
  t.string :name
5
-
6
5
  t.timestamps
7
6
  end
7
+ add_index :things, :name, :unique => true
8
8
  end
9
9
 
10
10
  def self.down
@@ -0,0 +1,32 @@
1
+ class Topic < ActiveRecord::Base
2
+ serialize :content
3
+ end
4
+
5
+ class ImportantTopic < Topic
6
+ serialize :important, Hash
7
+ end
8
+
9
+ class TopicMigration < ActiveRecord::Migration
10
+
11
+ def self.up
12
+ create_table :topics, :force => true do |t|
13
+ t.string :title
14
+ # use VARCHAR2(4000) instead of CLOB datatype as CLOB data type has many limitations in
15
+ # Oracle SELECT WHERE clause which causes many unit test failures
16
+ #if current_adapter?(:OracleAdapter)
17
+ #t.string :content, :limit => 4000
18
+ #t.string :important, :limit => 4000
19
+ #else
20
+ t.text :content
21
+ t.text :important
22
+ #end
23
+ t.string :type
24
+ t.timestamps
25
+ end
26
+ end
27
+
28
+ def self.down
29
+ drop_table :topics
30
+ end
31
+
32
+ end
@@ -29,7 +29,7 @@ class MsSQLLegacyTypesTest < Test::Unit::TestCase
29
29
 
30
30
  def setup
31
31
  CreateArticles.up
32
- @connection = ActiveRecord::Base.connection
32
+ ActiveRecord::Base.connection
33
33
  end
34
34
 
35
35
  def teardown
@@ -88,7 +88,7 @@ class MsSQLLimitOffsetTest < Test::Unit::TestCase
88
88
  CreateLongShips.up
89
89
  CreateVikings.up
90
90
  CreateNoIdVikings.up
91
- @connection = ActiveRecord::Base.connection
91
+ ActiveRecord::Base.connection
92
92
  end
93
93
 
94
94
  def teardown
@@ -102,7 +102,7 @@ class MsSQLLimitOffsetTest < Test::Unit::TestCase
102
102
  def test_limit_with_no_id_column_available
103
103
  NoIdViking.create!(:name => 'Erik')
104
104
  assert_nothing_raised(ActiveRecord::StatementInvalid) do
105
- NoIdViking.find(:first)
105
+ NoIdViking.first
106
106
  end
107
107
  end
108
108
 
@@ -162,5 +162,15 @@ class MsSQLLimitOffsetTest < Test::Unit::TestCase
162
162
  assert_equal(["Adam", "Carl", "Ben"], vikings.map(&:name))
163
163
 
164
164
  end
165
-
165
+
166
+ def test_offset_without_limit
167
+ %w(one two three four five six seven eight).each do |name|
168
+ LongShip.create!(:name => name)
169
+ end
170
+ error = assert_raise ActiveRecord::ActiveRecordError do
171
+ LongShip.find(:all, :offset => 2)
172
+ end
173
+ assert_equal("You must specify :limit with :offset.", error.message)
174
+ end
175
+
166
176
  end
@@ -0,0 +1,6 @@
1
+ require 'db/mssql'
2
+ require 'serialize'
3
+
4
+ class MsSQLSerializeTest < Test::Unit::TestCase
5
+ include SerializeTestMethods
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'db/mysql'
2
+ require 'binary'
3
+
4
+ class MySQLBinaryTest < Test::Unit::TestCase
5
+ include BinaryTestMethods
6
+ end
@@ -0,0 +1,220 @@
1
+ require 'db/mysql'
2
+ require 'schema_dump'
3
+ begin; require 'active_support/core_ext/numeric/bytes'; rescue LoadError; end
4
+
5
+ class MysqlSchemaDumpTest < Test::Unit::TestCase
6
+ include SchemaDumpTestMethods
7
+
8
+ def self.startup
9
+ super
10
+ MigrationSetup.setup!
11
+ end
12
+
13
+ def self.shutdown
14
+ MigrationSetup.teardown!
15
+ super
16
+ end
17
+
18
+ def setup!; end # MigrationSetup#setup!
19
+ def teardown!; end # MigrationSetup#teardown!
20
+
21
+ ActiveRecord::Schema.define do
22
+ create_table :big_fields, :force => true do |t|
23
+ t.binary :tiny_blob, :limit => 255
24
+ t.binary :normal_blob, :limit => 65535
25
+ t.binary :medium_blob, :limit => 16777215
26
+ t.binary :long_blob, :limit => 2147483647
27
+ t.text :tiny_text, :limit => 255
28
+ t.text :normal_text, :limit => 65535
29
+ t.text :medium_text, :limit => 16777215
30
+ t.text :long_text, :limit => 2147483647
31
+ t.text :just_text, :null => false
32
+ # MySQL does not allow default values for blobs.
33
+ # Fake it out with a big varchar below.
34
+ t.string :string_col, :null => true, :default => '', :limit => 1024
35
+ end
36
+ end
37
+
38
+ def test_schema_dump_should_not_add_default_value_for_mysql_text_field
39
+ output = standard_dump
40
+ assert_match %r{t.text\s+"just_text",\s+:null => false$}, output
41
+ end
42
+
43
+ def test_schema_dump_includes_length_for_mysql_blob_and_text_fields
44
+ output = standard_dump
45
+ assert_match %r{t.binary\s+"tiny_blob",\s+:limit => 255$}, output
46
+ assert_match %r{t.binary\s+"normal_blob"$}, output
47
+ assert_match %r{t.binary\s+"medium_blob",\s+:limit => 16777215$}, output
48
+ assert_match %r{t.binary\s+"long_blob",\s+:limit => 2147483647$}, output
49
+ assert_match %r{t.text\s+"tiny_text",\s+:limit => 255$}, output
50
+ assert_match %r{t.text\s+"normal_text"$}, output
51
+ assert_match %r{t.text\s+"medium_text",\s+:limit => 16777215$}, output
52
+ assert_match %r{t.text\s+"long_text",\s+:limit => 2147483647$}, output
53
+ end
54
+
55
+ end
56
+
57
+ class MysqlInfoTest < Test::Unit::TestCase
58
+
59
+ class DBSetup < ActiveRecord::Migration
60
+
61
+ def self.up
62
+ create_table :books do |t|
63
+ t.string :title
64
+ t.timestamps
65
+ end
66
+
67
+ create_table :cars, :primary_key => 'legacy_id' do |t|
68
+ t.string :name
69
+ t.date :production_started_on
70
+ end
71
+
72
+ create_table :cats, :id => false do |t|
73
+ t.string :name
74
+ end
75
+
76
+ create_table :memos do |t|
77
+ t.text :text, :limit => 16.megabytes
78
+ end
79
+ end
80
+
81
+ def self.down
82
+ drop_table :books
83
+ drop_table :cars
84
+ drop_table :cats
85
+ drop_table :memos
86
+ end
87
+
88
+ end
89
+
90
+ def self.startup
91
+ super
92
+ DBSetup.up
93
+ end
94
+
95
+ def self.shutdown
96
+ DBSetup.down
97
+ super
98
+ end
99
+
100
+ ## primary_key
101
+ def test_should_return_the_primary_key_of_a_table
102
+ assert_equal 'id', connection.primary_key('books')
103
+ end
104
+
105
+ def test_should_be_able_to_return_a_custom_primary_key
106
+ assert_equal 'legacy_id', connection.primary_key('cars')
107
+ end
108
+
109
+ def test_should_return_nil_for_a_table_without_a_primary_key
110
+ assert_nil connection.primary_key('cats')
111
+ end
112
+
113
+ ## structure_dump
114
+ def test_should_include_the_tables_in_a_structure_dump
115
+ # TODO: Improve these tests, I added this one because no other tests exists for this method.
116
+ dump = connection.structure_dump
117
+ assert dump.include?('CREATE TABLE `books`')
118
+ assert dump.include?('CREATE TABLE `cars`')
119
+ assert dump.include?('CREATE TABLE `cats`')
120
+ assert dump.include?('CREATE TABLE `memos`')
121
+ end
122
+
123
+ def test_should_include_longtext_in_schema_dump
124
+ strio = StringIO.new
125
+ ActiveRecord::SchemaDumper::dump(connection, strio)
126
+ dump = strio.string
127
+ assert_match %r{t.text\s+"text",\s+:limit => 2147483647$}, dump
128
+ end
129
+
130
+ # JRUBY-5040
131
+ def test_schema_dump_should_not_have_limits_on_datetime
132
+ dump = schema_dump
133
+ dump.lines.grep(/datetime/).each {|line| assert line !~ /limit/ }
134
+ end
135
+
136
+ def test_schema_dump_should_not_have_limits_on_date
137
+ dump = schema_dump
138
+ dump.lines.grep(/date/).each {|line| assert line !~ /limit/ }
139
+ end
140
+
141
+ def test_should_include_limit
142
+ text_column = connection.columns('memos').find { |c| c.name == 'text' }
143
+ assert_equal 2147483647, text_column.limit
144
+ end
145
+
146
+ def test_should_set_sqltype_to_longtext
147
+ text_column = connection.columns('memos').find { |c| c.name == 'text' }
148
+ assert text_column.sql_type =~ /^longtext/i
149
+ end
150
+
151
+ def test_should_set_type_to_text
152
+ text_column = connection.columns('memos').find { |c| c.name == 'text' }
153
+ assert_equal :text, text_column.type
154
+ end
155
+
156
+ def test_verify_url_has_options
157
+ url = connection.config[:url]
158
+ assert url =~ /characterEncoding=utf8/
159
+ assert url =~ /useUnicode=true/
160
+ assert url =~ /zeroDateTimeBehavior=convertToNull/
161
+ end
162
+
163
+ def test_no_limits_for_some_data_types
164
+ DbTypeMigration.up
165
+ #
166
+ # AR-3.2 :
167
+ #
168
+ # create_table "db_types", :force => true do |t|
169
+ # t.datetime "sample_timestamp"
170
+ # t.datetime "sample_datetime"
171
+ # t.date "sample_date"
172
+ # t.time "sample_time"
173
+ # t.decimal "sample_decimal", :precision => 15, :scale => 0
174
+ # t.decimal "sample_small_decimal", :precision => 3, :scale => 2
175
+ # t.decimal "sample_default_decimal", :precision => 10, :scale => 0
176
+ # t.float "sample_float"
177
+ # t.binary "sample_binary"
178
+ # t.boolean "sample_boolean"
179
+ # t.string "sample_string", :default => ""
180
+ # t.integer "sample_integer", :limit => 8
181
+ # t.integer "sample_integer_with_limit_2", :limit => 2
182
+ # t.integer "sample_integer_with_limit_8", :limit => 8
183
+ # t.integer "sample_integer_no_limit"
184
+ # t.integer "sample_integer_neg_default", :default => -1
185
+ # t.text "sample_text"
186
+ # end
187
+ #
188
+ # AR-2.3 :
189
+ #
190
+ # create_table "db_types", :force => true do |t|
191
+ # t.datetime "sample_timestamp"
192
+ # t.datetime "sample_datetime"
193
+ # t.date "sample_date"
194
+ # t.time "sample_time"
195
+ # t.integer "sample_decimal", :limit => 15, :precision => 15, :scale => 0
196
+ # t.decimal "sample_small_decimal", :precision => 3, :scale => 2
197
+ # t.integer "sample_default_decimal", :limit => 10, :precision => 10, :scale => 0
198
+ # t.float "sample_float"
199
+ # t.binary "sample_binary"
200
+ # t.boolean "sample_boolean"
201
+ # t.string "sample_string", :default => ""
202
+ # t.integer "sample_integer", :limit => 8
203
+ # t.integer "sample_integer_with_limit_2", :limit => 2
204
+ # t.integer "sample_integer_with_limit_8", :limit => 8
205
+ # t.integer "sample_integer_no_limit"
206
+ # t.integer "sample_integer_neg_default", :default => -1
207
+ # t.text "sample_text"
208
+ # end
209
+ #
210
+ dump = schema_dump
211
+ if ar_version('3.0')
212
+ assert_nil dump.lines.detect {|l| l =~ /\.(float|date|datetime|integer|time|timestamp) .* :limit/ && l !~ /sample_integer/ }, dump
213
+ else
214
+ puts "test_no_limits_for_some_data_types assertion skipped on #{ActiveRecord::VERSION::STRING}"
215
+ end
216
+ ensure
217
+ DbTypeMigration.down
218
+ end
219
+
220
+ end
@@ -0,0 +1,6 @@
1
+ require 'db/mysql'
2
+ require 'serialize'
3
+
4
+ class MySQLSerializeTest < Test::Unit::TestCase
5
+ include SerializeTestMethods
6
+ end
@@ -48,9 +48,9 @@ class MysqlSimpleTest < Test::Unit::TestCase
48
48
  user_1 = User.create :login => 'user_1'
49
49
  user_2 = User.create :login => 'user_2'
50
50
 
51
- entry_1 = Entry.create :title => 'title_1', :content => 'content_1', :rating => 0,
51
+ Entry.create :title => 'title_1', :content => 'content_1', :rating => 0,
52
52
  :user_id => user_1.id
53
- entry_2 = Entry.create :title => 'title_2', :content => 'content_2', :rating => 1,
53
+ Entry.create :title => 'title_2', :content => 'content_2', :rating => 1,
54
54
  :user_id => user_2.id
55
55
 
56
56
  all_entries = Entry.joins(:user).where('users.id' => user_1.id).
@@ -103,6 +103,26 @@ class MysqlSimpleTest < Test::Unit::TestCase
103
103
  User.table_name = old_users_table_name
104
104
  end
105
105
  end
106
+
107
+ include ExplainSupportTestMethods if ar_version("3.1")
108
+
109
+ def test_update_sql_public_and_returns_rows_affected
110
+ ActiveRecord::Base.connection.update_sql "UPDATE entries SET title = NULL"
111
+
112
+ e1 = Entry.create! :title => 'a some', :content => 'brrrr', :rating => 10.8
113
+ e2 = Entry.create! :title => 'another', :content => 'meee', :rating => 40.2
114
+ rows_affected = ActiveRecord::Base.connection.update_sql "UPDATE entries " +
115
+ "SET content='updated content' WHERE rating > 10 AND title IS NOT NULL"
116
+ assert_equal 2, rows_affected
117
+ assert_equal 'updated content', e1.reload.content
118
+ assert_equal 'updated content', e2.reload.content
119
+ end
120
+
121
+ def test_reports_server_version
122
+ assert_instance_of Array, ActiveRecord::Base.connection.send(:version)
123
+ assert_equal 3, ActiveRecord::Base.connection.send(:version).size
124
+ end
125
+
106
126
  end
107
127
 
108
128
  class MysqlHasManyThroughTest < Test::Unit::TestCase
@@ -0,0 +1,93 @@
1
+ # encoding: ASCII-8BIT
2
+ require 'test_helper'
3
+
4
+ class MySQLTest < Test::Unit::TestCase
5
+
6
+ class MySQLImpl
7
+ include ArJdbc::MySQL
8
+ def initialize; end
9
+ end
10
+ mySQL = MySQLImpl.new
11
+
12
+ test "quotes string value" do
13
+ {
14
+ "\x00" => "\\0",
15
+ "\n" => "\\n",
16
+ "\r" => "\\r",
17
+ "\x1A" => "\\Z",
18
+ '"' => "\\\"",
19
+ "'" => "\\'",
20
+ "\\" => "\\\\",
21
+ }.each do |s, q|
22
+ assert_equal q, mySQL.quote_string(s)
23
+ assert_equal "string #{q}", mySQL.quote_string(v = "string #{s}"), "while quoting #{v.inspect}"
24
+ assert_equal " #{q}", mySQL.quote_string(v = " #{s}"), "while quoting #{v.inspect}"
25
+ assert_equal "#{q}str", mySQL.quote_string(v = "#{s}str"), "while quoting #{v.inspect}"
26
+ end
27
+ end
28
+
29
+ test "quote string (with encoding mysql2 style)" do
30
+ s = "'' \\| \" \x00 \x01 \x1A \xA1 \r \n \\nOiZe \r\n'\x01\X\xA1\xA1\n"
31
+ q = "\\'\\' \\\\| \\\" \\0 \x01 \\Z \xA1 \\r \\n \\\\nOiZe \\r\\n\\'\x01X\xA1\xA1\\n"
32
+ if RUBY_VERSION > '1.9' # mysql2 compatibility
33
+ q2 = q.dup.force_encoding 'UTF-8'
34
+ assert_equal q2, mySQL.quote_string(s)
35
+ else
36
+ assert_equal q, mySQL.quote_string(s)
37
+ end
38
+
39
+ if s.respond_to?(:force_encoding)
40
+ s.force_encoding('UTF-8')
41
+ q.force_encoding('UTF-8')
42
+ v = mySQL.quote_string(s)
43
+ assert_equal q, v
44
+ assert_equal q.encoding, v.encoding
45
+
46
+ s.force_encoding('ASCII-8BIT')
47
+ v = mySQL.quote_string(s)
48
+ assert_equal q, v
49
+ # Mysql2::Client.new(...).escape(s) compatible :
50
+ assert_equal 'UTF-8', v.encoding.name
51
+ # Mysql.escape_string(s) returns ASCII-8BIT
52
+ end
53
+ end
54
+
55
+ test "quote string (utf-8)" do
56
+ s = "\x1Akôň ůň löw9876\r\nqűáéőú\n.éáű-mehehehehehehe0 \x01\x00\x00\\\x02\\'"
57
+ q = "\\Zkôň ůň löw9876\\r\\nqűáéőú\\n.éáű-mehehehehehehe0 \x01\\0\\0\\\\\x02\\\\\\'"
58
+ if RUBY_VERSION > '1.9' # mysql2 compatibility
59
+ q2 = q.dup.force_encoding 'UTF-8'
60
+ assert_equal q2, mySQL.quote_string(s.dup)
61
+
62
+ q.force_encoding('UTF-8')
63
+ s.force_encoding('UTF-8')
64
+ assert_equal q, mySQL.quote_string(s)
65
+ else
66
+ assert_equal q, mySQL.quote_string(s.dup)
67
+ end
68
+ assert_equal q, mySQL.quote_string(s)
69
+ end
70
+
71
+ test "quote string keeps original string" do
72
+ s = "mehehehehehehe0 \x01 \x02 nothing-to-quote-here ..."
73
+ assert_equal s, mySQL.quote_string(s.dup)
74
+
75
+ if s.respond_to?(:force_encoding)
76
+ s.force_encoding('UTF-8')
77
+ assert_equal s, mySQL.quote_string(s.dup)
78
+ end
79
+ assert_equal s, mySQL.quote_string(s)
80
+ end
81
+
82
+ test "quote string keeps original (utf-8)" do
83
+ s = "kôň ůň löw9876qűáéőú.éáű-mehehehehehehe0 \x01 \x02 nothing-to-quote"
84
+ assert_equal s, mySQL.quote_string(s.dup)
85
+
86
+ if s.respond_to?(:force_encoding)
87
+ s.force_encoding('UTF-8')
88
+ assert_equal s, mySQL.quote_string(s.dup)
89
+ end
90
+ assert_equal s, mySQL.quote_string(s)
91
+ end
92
+
93
+ end