activerecord-jdbc-adapter 0.9.0.1 → 0.9.1

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.
Files changed (49) hide show
  1. data/History.txt +31 -0
  2. data/Manifest.txt +7 -0
  3. data/README.txt +15 -2
  4. data/Rakefile +28 -30
  5. data/lib/active_record/connection_adapters/h2_adapter.rb +13 -1
  6. data/lib/active_record/connection_adapters/jdbc_adapter.rb +78 -96
  7. data/lib/jdbc_adapter/jdbc.rake +15 -5
  8. data/lib/jdbc_adapter/jdbc_adapter_internal.jar +0 -0
  9. data/lib/jdbc_adapter/jdbc_cachedb.rb +4 -4
  10. data/lib/jdbc_adapter/jdbc_db2.rb +5 -7
  11. data/lib/jdbc_adapter/jdbc_derby.rb +57 -30
  12. data/lib/jdbc_adapter/jdbc_firebird.rb +2 -2
  13. data/lib/jdbc_adapter/jdbc_hsqldb.rb +53 -46
  14. data/lib/jdbc_adapter/jdbc_informix.rb +4 -5
  15. data/lib/jdbc_adapter/jdbc_mimer.rb +2 -2
  16. data/lib/jdbc_adapter/jdbc_mssql.rb +25 -23
  17. data/lib/jdbc_adapter/jdbc_mysql.rb +20 -22
  18. data/lib/jdbc_adapter/jdbc_oracle.rb +115 -117
  19. data/lib/jdbc_adapter/jdbc_postgre.rb +129 -59
  20. data/lib/jdbc_adapter/jdbc_sqlite3.rb +149 -28
  21. data/lib/jdbc_adapter/jdbc_sybase.rb +13 -2
  22. data/lib/jdbc_adapter/missing_functionality_helper.rb +12 -3
  23. data/lib/jdbc_adapter/version.rb +1 -1
  24. data/src/java/jdbc_adapter/JdbcAdapterInternalService.java +6 -1101
  25. data/src/java/jdbc_adapter/JdbcDerbySpec.java +26 -23
  26. data/src/java/jdbc_adapter/JdbcMySQLSpec.java +79 -28
  27. data/src/java/jdbc_adapter/PostgresRubyJdbcConnection.java +35 -0
  28. data/src/java/jdbc_adapter/RubyJdbcConnection.java +1149 -0
  29. data/src/java/jdbc_adapter/SQLBlock.java +12 -3
  30. data/src/java/jdbc_adapter/Sqlite3RubyJdbcConnection.java +41 -0
  31. data/test/activerecord/connection_adapters/type_conversion_test.rb +1 -1
  32. data/test/db/derby.rb +0 -3
  33. data/test/db/h2.rb +0 -3
  34. data/test/db/hsqldb.rb +1 -4
  35. data/test/db/mysql.rb +1 -0
  36. data/test/db/oracle.rb +5 -0
  37. data/test/db/sqlite3.rb +7 -3
  38. data/test/derby_migration_test.rb +21 -0
  39. data/test/has_many_through.rb +11 -4
  40. data/test/jdbc_common.rb +13 -1
  41. data/test/models/data_types.rb +11 -1
  42. data/test/models/mixed_case.rb +20 -0
  43. data/test/mysql_multibyte_test.rb +4 -0
  44. data/test/oracle_simple_test.rb +1 -1
  45. data/test/postgres_mixed_case_test.rb +19 -0
  46. data/test/simple.rb +220 -41
  47. data/test/sqlite3_simple_test.rb +83 -0
  48. data/test/sybase_jtds_simple_test.rb +6 -0
  49. metadata +12 -10
@@ -6,13 +6,22 @@
6
6
  package jdbc_adapter;
7
7
 
8
8
  import java.sql.Connection;
9
+ import java.sql.ResultSet;
9
10
  import java.sql.SQLException;
10
- import org.jruby.runtime.builtin.IRubyObject;
11
+ import java.sql.Statement;
11
12
 
12
13
  /**
13
14
  *
14
15
  * @author nicksieger
15
16
  */
16
- public interface SQLBlock {
17
- IRubyObject call(Connection c) throws SQLException;
17
+ public abstract class SQLBlock {
18
+ abstract Object call(Connection c) throws SQLException;
19
+
20
+ public void close(Statement statement) {
21
+ RubyJdbcConnection.close(statement);
22
+ }
23
+
24
+ public void close(ResultSet resultSet) {
25
+ RubyJdbcConnection.close(resultSet);
26
+ }
18
27
  }
@@ -0,0 +1,41 @@
1
+ /*
2
+ * To change this template, choose Tools | Templates
3
+ * and open the template in the editor.
4
+ */
5
+
6
+ package jdbc_adapter;
7
+
8
+ import org.jruby.Ruby;
9
+ import org.jruby.RubyClass;
10
+ import org.jruby.runtime.ObjectAllocator;
11
+ import org.jruby.runtime.ThreadContext;
12
+ import org.jruby.runtime.builtin.IRubyObject;
13
+
14
+ /**
15
+ *
16
+ * @author enebo
17
+ */
18
+ public class Sqlite3RubyJdbcConnection extends RubyJdbcConnection {
19
+ protected Sqlite3RubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
20
+ super(runtime, metaClass);
21
+ }
22
+
23
+ public static RubyClass createSqlite3JdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
24
+ RubyClass clazz = RubyJdbcConnection.getConnectionAdapters(runtime).defineClassUnder("Sqlite3JdbcConnection",
25
+ jdbcConnection, SQLITE3_JDBCCONNECTION_ALLOCATOR);
26
+ clazz.defineAnnotatedMethods(Sqlite3RubyJdbcConnection.class);
27
+
28
+ return clazz;
29
+ }
30
+
31
+ private static ObjectAllocator SQLITE3_JDBCCONNECTION_ALLOCATOR = new ObjectAllocator() {
32
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
33
+ return new Sqlite3RubyJdbcConnection(runtime, klass);
34
+ }
35
+ };
36
+
37
+ @Override
38
+ protected IRubyObject tables(ThreadContext context, String catalog, String schemaPattern, String tablePattern, String[] types) {
39
+ return (IRubyObject) withConnectionAndRetry(context, tableLookupBlock(context.getRuntime(), catalog, schemaPattern, tablePattern, types, true));
40
+ }
41
+ }
@@ -7,7 +7,7 @@ require 'test/unit'
7
7
  JInteger = java.lang.Integer
8
8
 
9
9
  class TypeConversionTest < Test::Unit::TestCase
10
- TEST_TIME = Time.at(1169964202)
10
+ TEST_TIME = Time.at(1169964202).gmtime
11
11
  def setup
12
12
  DbTypeMigration.up
13
13
  DbType.create(
data/test/db/derby.rb CHANGED
@@ -6,9 +6,6 @@ config = {
6
6
  }
7
7
 
8
8
  ActiveRecord::Base.establish_connection(config)
9
- logger = Logger.new 'derby-testdb.log'
10
- logger.level = Logger::DEBUG
11
- ActiveRecord::Base.logger = logger
12
9
 
13
10
  at_exit {
14
11
  # Clean up derby files
data/test/db/h2.rb CHANGED
@@ -4,9 +4,6 @@ config = {
4
4
  }
5
5
 
6
6
  ActiveRecord::Base.establish_connection(config)
7
- logger = Logger.new 'h2-testdb.log'
8
- logger.level = Logger::DEBUG
9
- ActiveRecord::Base.logger = logger
10
7
 
11
8
  at_exit {
12
9
  # Clean up hsqldb when done
data/test/db/hsqldb.rb CHANGED
@@ -4,12 +4,9 @@ config = {
4
4
  }
5
5
 
6
6
  ActiveRecord::Base.establish_connection(config)
7
- logger = Logger.new 'hsqldb-testdb.log'
8
- logger.level = Logger::DEBUG
9
- ActiveRecord::Base.logger = logger
10
7
 
11
8
  at_exit {
12
9
  # Clean up hsqldb when done
13
10
  Dir['test.db*'].each {|f| File.delete(f)}
14
- File.delete('hsqldb-testdb.log')
11
+ File.delete('hsqldb-testdb.log') rescue nil #can't delete on windows
15
12
  }
data/test/db/mysql.rb CHANGED
@@ -7,3 +7,4 @@ config = {
7
7
  }
8
8
 
9
9
  ActiveRecord::Base.establish_connection(config)
10
+
data/test/db/oracle.rb CHANGED
@@ -11,8 +11,13 @@ ActiveRecord::Base.establish_connection(config)
11
11
  # Here are some notes of things I had to do to get running on Oracle
12
12
  # XE.
13
13
  #
14
+ # ON Linux:
14
15
  # create tablespace weblog_development
15
16
  # datafile '/usr/lib/oracle/xe/oradata/XE/weblog_development.dbf';
17
+ # ON Windows XP:
18
+ # create tablespace weblog_development
19
+ # datafile 'C:\ORACLEXE\ORADATA\XE\WEBLOGD.DBF' size 16m;
20
+ #
16
21
  # create user blog identified by blog
17
22
  # default tablespace weblog_development;
18
23
  # grant all privileges to blog;
data/test/db/sqlite3.rb CHANGED
@@ -1,11 +1,15 @@
1
+ require 'jdbc/sqlite3' if jruby?
2
+
1
3
  config = {
2
- :adapter => 'sqlite3',
3
- :database => 'test.sqlite3'
4
+ :adapter => jruby? ? 'jdbcsqlite3' : 'sqlite3',
5
+ :dbfile => 'test.sqlite3.db'
6
+ # :url => 'jdbc:sqlite:test.sqlite3.db',
7
+ # :driver => 'org.sqlite.JDBC'
4
8
  }
5
9
 
6
10
  ActiveRecord::Base.establish_connection(config)
7
11
 
8
12
  at_exit {
9
- # Clean up hsqldb when done
13
+ # Clean up sqlite3 db when done
10
14
  Dir['test.sqlite3*'].each {|f| File.delete(f)}
11
15
  }
@@ -0,0 +1,21 @@
1
+ require 'jdbc_common'
2
+ require 'db/derby'
3
+
4
+ class CreateDummies < ActiveRecord::Migration
5
+ def self.up
6
+ create_table :dummies, :force => true do |t|
7
+ t.string :year, :default => "", :null => false
8
+ end
9
+ add_index :dummies, :year, :unique => true
10
+ end
11
+
12
+ end
13
+
14
+ class DerbyQuotingTest < Test::Unit::TestCase
15
+ include FixtureSetup
16
+
17
+ def test_create_table_column_quoting_vs_keywords
18
+ CreateDummies.up
19
+ end
20
+
21
+ end
@@ -19,6 +19,7 @@ class CreateRbac < ActiveRecord::Migration
19
19
  t.column :name, :string
20
20
  t.column :controller_name, :string
21
21
  t.column :actions, :string
22
+ t.column :hours, :float, :null => false
22
23
  end
23
24
  end
24
25
 
@@ -61,12 +62,18 @@ module HasManyThroughMethods
61
62
  end
62
63
 
63
64
  def test_has_many_through
64
- role_rights = Right.create( {:name => "Administrator - Full Access To Roles", :actions => "*", :controller_name => "Admin::RolesController"} )
65
- right_rights = Right.create( {:name => "Administrator - Full Access To Rights", :actions => "*", :controller_name => "Admin::RightsController"} )
66
-
67
65
  admin_role = Role.create( {:name => "Administrator", :description => "System defined super user - access to right and role management."} )
66
+ admin_role.save
67
+
68
+ assert_equal(0, admin_role.rights.sum(:hours))
69
+
70
+ role_rights = Right.create( {:name => "Administrator - Full Access To Roles", :actions => "*", :controller_name => "Admin::RolesController", :hours => 0} )
71
+ right_rights = Right.create( {:name => "Administrator - Full Access To Rights", :actions => "*", :controller_name => "Admin::RightsController", :hours => 1.5} )
72
+
68
73
  admin_role.rights << role_rights
69
74
  admin_role.rights << right_rights
70
75
  admin_role.save
76
+
77
+ assert_equal(1.5, admin_role.rights.sum(:hours))
71
78
  end
72
- end
79
+ end
data/test/jdbc_common.rb CHANGED
@@ -1,12 +1,24 @@
1
+ # Simple method to reduce the boilerplate
2
+ def jruby?
3
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
4
+ end
5
+
1
6
  require 'rubygems'
2
7
  # Specify version of activerecord with ENV['AR_VERSION'] if desired
3
8
  gem 'activerecord', ENV['AR_VERSION'] if ENV['AR_VERSION']
4
- require 'jdbc_adapter'
9
+ require 'active_record/version'
10
+ require 'jdbc_adapter' if jruby?
5
11
  puts "Using activerecord version #{ActiveRecord::VERSION::STRING}"
6
12
  puts "Specify version with AR_VERSION=={version} or RUBYLIB={path}"
7
13
  require 'models/auto_id'
8
14
  require 'models/entry'
15
+ require 'models/data_types'
9
16
  require 'models/add_not_null_column_to_table'
10
17
  require 'simple'
11
18
  require 'has_many_through'
12
19
  require 'test/unit'
20
+
21
+ # Comment/uncomment to enable logging to be loaded for any of the database adapters
22
+ # require 'db/logger'
23
+
24
+
@@ -5,7 +5,17 @@ class DbTypeMigration < ActiveRecord::Migration
5
5
  def self.up
6
6
  create_table "db_types", :force => true do |t|
7
7
  t.column :sample_timestamp, :timestamp
8
- t.column :sample_decimal, :decimal, :precision=> 15, :scale => 0
8
+ t.column :sample_datetime, :datetime
9
+ t.column :sample_date, :date
10
+ t.column :sample_time, :time
11
+ t.column :sample_decimal, :decimal, :precision => 15, :scale => 0
12
+ t.column :sample_small_decimal, :decimal, :precision => 3, :scale => 2
13
+ t.column :sample_float, :float
14
+ t.column :sample_binary, :binary
15
+ t.column :sample_boolean, :boolean
16
+ t.column :sample_string, :string, :default => ''
17
+ t.column :sample_integer, :integer, :limit => 5
18
+ t.column :sample_text, :text
9
19
  end
10
20
  end
11
21
 
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+
4
+ module Migration
5
+ class MixedCase < ActiveRecord::Migration
6
+
7
+ def self.up
8
+ create_table "mixed_cases" do |t|
9
+ t.column :SOME_value, :string
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table "mixed_cases"
15
+ end
16
+ end
17
+ end
18
+
19
+ class MixedCase < ActiveRecord::Base
20
+ end
@@ -4,3 +4,7 @@ require 'db/mysql'
4
4
  class MySQLMultibyteTest < Test::Unit::TestCase
5
5
  include MultibyteTestMethods
6
6
  end
7
+
8
+ class MySQLNonUTF8EncodingTest < Test::Unit::TestCase
9
+ include NonUTF8EncodingMethods
10
+ end
@@ -26,4 +26,4 @@ class OracleSpecificTest < Test::Unit::TestCase
26
26
  obj = klass.find(:first)
27
27
  assert_equal 0.076, obj.value
28
28
  end
29
- end
29
+ end if defined?(JRUBY_VERSION)
@@ -0,0 +1,19 @@
1
+ require 'jdbc_common'
2
+ require 'models/mixed_case'
3
+
4
+ class MixedCaseTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ Migration::MixedCase.up
8
+ end
9
+
10
+ def teardown
11
+ Migration::MixedCase.down
12
+ end
13
+
14
+ def test_create
15
+ mixed_case = MixedCase.create :SOME_value => 'some value'
16
+ assert_equal 'some value', mixed_case.SOME_value
17
+ end
18
+
19
+ end
data/test/simple.rb CHANGED
@@ -1,8 +1,13 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  ActiveRecord::Schema.verbose = false
3
+ ActiveRecord::Base.time_zone_aware_attributes = true if ActiveRecord::Base.respond_to?(:time_zone_aware_attributes)
4
+ ActiveRecord::Base.default_timezone = :utc
5
+ #just a random zone, unlikely to be local, and not utc
6
+ Time.zone = 'Moscow' if Time.respond_to?(:zone)
3
7
 
4
8
  module MigrationSetup
5
9
  def setup
10
+ DbTypeMigration.up
6
11
  CreateEntries.up
7
12
  CreateAutoIds.up
8
13
 
@@ -10,6 +15,7 @@ module MigrationSetup
10
15
  end
11
16
 
12
17
  def teardown
18
+ DbTypeMigration.down
13
19
  CreateEntries.down
14
20
  CreateAutoIds.down
15
21
  ActiveRecord::Base.clear_active_connections!
@@ -25,6 +31,7 @@ module FixtureSetup
25
31
  @new_title = "First post updated title"
26
32
  @rating = 205.76
27
33
  Entry.create :title => @title, :content => @content, :rating => @rating
34
+ DbType.create
28
35
  end
29
36
  end
30
37
 
@@ -40,6 +47,15 @@ module SimpleTestMethods
40
47
  assert_equal 0, Entry.count
41
48
  end
42
49
 
50
+ def test_insert_returns_id
51
+ unless ActiveRecord::Base.connection.adapter_name =~ /oracle/i
52
+ value = ActiveRecord::Base.connection.insert("INSERT INTO entries (title, content, rating) VALUES('insert_title', 'some content', 1)")
53
+ assert !value.nil?
54
+ entry = Entry.find_by_title('insert_title')
55
+ assert_equal value, entry.id
56
+ end
57
+ end
58
+
43
59
  def test_create_new_entry
44
60
  Entry.delete_all
45
61
 
@@ -78,15 +94,132 @@ module SimpleTestMethods
78
94
  assert_equal prev_count - 1, Entry.count
79
95
  end
80
96
 
97
+ if Time.respond_to?(:zone)
98
+ def test_save_time
99
+ t = Time.now
100
+ #precision will only be expected to the second.
101
+ time = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec)
102
+ e = DbType.find(:first)
103
+ e.sample_datetime = time
104
+ e.save!
105
+ e = DbType.find(:first)
106
+ assert_equal time.in_time_zone, e.sample_datetime
107
+ end
108
+
109
+ def test_save_date_time
110
+ t = Time.now
111
+ #precision will only be expected to the second.
112
+ time = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec)
113
+ datetime = time.to_datetime
114
+ e = DbType.find(:first)
115
+ e.sample_datetime = datetime
116
+ e.save!
117
+ e = DbType.find(:first)
118
+ assert_equal time, e.sample_datetime.localtime
119
+ end
120
+
121
+ def test_save_time_with_zone
122
+ t = Time.now
123
+ #precision will only be expected to the second.
124
+ original_time = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec)
125
+ time = original_time.in_time_zone
126
+ e = DbType.find(:first)
127
+ e.sample_datetime = time
128
+ e.save!
129
+ e = DbType.find(:first)
130
+ assert_equal time, e.sample_datetime
131
+ end
132
+ end
133
+
134
+ def test_save_float
135
+ e = DbType.find(:first)
136
+ e.sample_float = 12.0
137
+ e.save!
138
+
139
+ e = DbType.find(:first)
140
+ assert_equal(12.0, e.sample_float)
141
+ end
142
+
143
+ def test_save_date
144
+ date = Date.new(2007)
145
+ e = DbType.find(:first)
146
+ e.sample_date = date
147
+ e.save!
148
+ e = DbType.find(:first)
149
+ assert_equal date, e.sample_date
150
+ end
151
+
152
+ def test_boolean
153
+ # An unset boolean should default to nil
154
+ e = DbType.find(:first)
155
+ assert_equal(nil, e.sample_boolean)
156
+
157
+ e.sample_boolean = true
158
+ e.save!
159
+
160
+ e = DbType.find(:first)
161
+ assert_equal(true, e.sample_boolean)
162
+ end
163
+
164
+ def test_integer
165
+ # An unset boolean should default to nil
166
+ e = DbType.find(:first)
167
+ assert_equal(nil, e.sample_integer)
168
+
169
+ e.sample_integer = 10
170
+ e.save!
171
+
172
+ e = DbType.find(:first)
173
+ assert_equal(10, e.sample_integer)
174
+ end
175
+
176
+ def test_text
177
+ # An unset boolean should default to nil
178
+ e = DbType.find(:first)
179
+
180
+ # Oracle adapter initializes all CLOB fields with empty_clob() function,
181
+ # so they all have a initial value of an empty string ''
182
+ assert_equal(nil, e.sample_text) unless ActiveRecord::Base.connection.adapter_name =~ /oracle/i
183
+
184
+ e.sample_text = "ooop"
185
+ e.save!
186
+
187
+ e = DbType.find(:first)
188
+ assert_equal("ooop", e.sample_text)
189
+ end
190
+
191
+ def test_string
192
+ e = DbType.find(:first)
193
+
194
+ # An empty string is treated as a null value in Oracle: http://www.techonthenet.com/oracle/questions/empty_null.php
195
+ assert_equal('', e.sample_string) unless ActiveRecord::Base.connection.adapter_name =~ /oracle/i
196
+ e.sample_string = "ooop"
197
+ e.save!
198
+
199
+ e = DbType.find(:first)
200
+ assert_equal("ooop", e.sample_string)
201
+ end
202
+
203
+ def test_save_binary
204
+ #string is 60_000 bytes
205
+ binary_string = "\000ABCDEFGHIJKLMNOPQRSTUVWXYZ'\001\003"*1#2_000
206
+ e = DbType.find(:first)
207
+ e.sample_binary = binary_string
208
+ e.send(:write_attribute, :binary, binary_string)
209
+ e.save!
210
+ e = DbType.find(:first)
211
+ assert_equal binary_string, e.sample_binary
212
+ end
213
+
81
214
  def test_indexes
82
215
  # Only test indexes if we have implemented it for the particular adapter
83
216
  if @connection.respond_to?(:indexes)
84
217
  indexes = @connection.indexes(:entries)
85
218
  assert_equal(0, indexes.size)
86
-
219
+
87
220
  index_name = "entries_index"
88
221
  @connection.add_index(:entries, :updated_on, :name => index_name)
89
-
222
+
90
223
  indexes = @connection.indexes(:entries)
91
224
  assert_equal(1, indexes.size)
92
225
  assert_equal "entries", indexes.first.table.to_s
@@ -123,17 +256,28 @@ module SimpleTestMethods
123
256
  assert_equal 1, Entry.count
124
257
  end
125
258
 
126
- def test_connection_valid
127
- assert_raises(ActiveRecord::ActiveRecordError) do
128
- @connection.raw_connection.with_connection_retry_guard do |c|
129
- begin
130
- stmt = c.createStatement
131
- stmt.execute "bogus sql"
132
- ensure
133
- stmt.close rescue nil
259
+ if jruby?
260
+ def test_connection_valid
261
+ assert_raises(ActiveRecord::ActiveRecordError) do
262
+ @connection.raw_connection.with_connection_retry_guard do |c|
263
+ begin
264
+ stmt = c.createStatement
265
+ stmt.execute "bogus sql"
266
+ ensure
267
+ stmt.close rescue nil
268
+ end
134
269
  end
135
270
  end
136
271
  end
272
+
273
+ class Animal < ActiveRecord::Base; end
274
+
275
+ # ENEBO: Is this really ar-jdbc-specific or a bug in our adapter?
276
+ def test_fetching_columns_for_nonexistent_table_should_raise
277
+ assert_raises(ActiveRecord::ActiveRecordError) do
278
+ Animal.columns
279
+ end
280
+ end
137
281
  end
138
282
 
139
283
  def test_disconnect
@@ -150,29 +294,57 @@ module SimpleTestMethods
150
294
  AddNotNullColumnToTable.down
151
295
  end
152
296
 
153
- class Animal < ActiveRecord::Base; end
154
- def test_fetching_columns_for_nonexistent_table_should_raise
155
- assert_raises(ActiveRecord::ActiveRecordError) do
156
- Animal.columns
297
+ class ChangeEntriesTable < ActiveRecord::Migration
298
+ def self.up
299
+ change_table :entries do |t|
300
+ t.string :author
301
+ end if respond_to?(:change_table)
157
302
  end
303
+ def self.down
304
+ change_table :entries do |t|
305
+ t.remove :author
306
+ end if respond_to?(:change_table)
307
+ end
308
+ end
309
+ def test_change_table
310
+ ChangeEntriesTable.up
311
+ ChangeEntriesTable.down
158
312
  end
159
313
  end
160
314
 
161
315
  module MultibyteTestMethods
162
316
  include MigrationSetup
163
317
 
164
- def setup
165
- super
166
- config = ActiveRecord::Base.connection.config
167
- jdbc_driver = ActiveRecord::ConnectionAdapters::JdbcDriver.new(config[:driver])
168
- jdbc_driver.load
169
- @java_con = jdbc_driver.connection(config[:url], config[:username], config[:password])
170
- @java_con.setAutoCommit(true)
171
- end
318
+ if defined?(JRUBY_VERSION)
319
+ def setup
320
+ super
321
+ config = ActiveRecord::Base.connection.config
322
+ jdbc_driver = ActiveRecord::ConnectionAdapters::JdbcDriver.new(config[:driver])
323
+ jdbc_driver.load
324
+ @java_con = jdbc_driver.connection(config[:url], config[:username], config[:password])
325
+ @java_con.setAutoCommit(true)
326
+ end
172
327
 
173
- def teardown
174
- @java_con.close
175
- super
328
+ def teardown
329
+ @java_con.close
330
+ super
331
+ end
332
+
333
+ def test_select_multibyte_string
334
+ @java_con.createStatement().execute("insert into entries (id, title, content) values (1, 'テスト', '本文')")
335
+ entry = Entry.find(:first)
336
+ assert_equal "テスト", entry.title
337
+ assert_equal "本文", entry.content
338
+ assert_equal entry, Entry.find_by_title("テスト")
339
+ end
340
+
341
+ def test_update_multibyte_string
342
+ Entry.create!(:title => "テスト", :content => "本文")
343
+ rs = @java_con.createStatement().executeQuery("select title, content from entries")
344
+ assert rs.next
345
+ assert_equal "テスト", rs.getString(1)
346
+ assert_equal "本文", rs.getString(2)
347
+ end
176
348
  end
177
349
 
178
350
  def test_multibyte_aliasing
@@ -186,23 +358,7 @@ module MultibyteTestMethods
186
358
  end
187
359
  end
188
360
  end
189
-
190
- def test_select_multibyte_string
191
- @java_con.createStatement().execute("insert into entries (id, title, content) values (1, 'テスト', '本文')")
192
- entry = Entry.find(:first)
193
- assert_equal "テスト", entry.title
194
- assert_equal "本文", entry.content
195
- assert_equal entry, Entry.find_by_title("テスト")
196
- end
197
361
 
198
- def test_update_multibyte_string
199
- Entry.create!(:title => "テスト", :content => "本文")
200
- rs = @java_con.createStatement().executeQuery("select title, content from entries")
201
- assert rs.next
202
- assert_equal "テスト", rs.getString(1)
203
- assert_equal "本文", rs.getString(2)
204
- end
205
-
206
362
  def test_chinese_word
207
363
  chinese_word = '中文'
208
364
  new_entry = Entry.create(:title => chinese_word)
@@ -210,3 +366,26 @@ module MultibyteTestMethods
210
366
  assert_equal chinese_word, new_entry.title
211
367
  end
212
368
  end
369
+
370
+ module NonUTF8EncodingMethods
371
+ def setup
372
+ @connection = ActiveRecord::Base.remove_connection
373
+ latin2_connection = @connection.dup
374
+ latin2_connection[:encoding] = 'latin2'
375
+ latin2_connection.delete(:url) # pre-gen url gets stashed; remove to re-gen
376
+ ActiveRecord::Base.establish_connection latin2_connection
377
+ CreateEntries.up
378
+ end
379
+
380
+ def teardown
381
+ CreateEntries.down
382
+ ActiveRecord::Base.establish_connection @connection
383
+ end
384
+
385
+ def test_nonutf8_encoding_in_entry
386
+ prague_district = 'hradčany'
387
+ new_entry = Entry.create :title => prague_district
388
+ new_entry.reload
389
+ assert_equal prague_district, new_entry.title
390
+ end
391
+ end