activerecord-jdbc-adapter 0.9.7-java → 1.0.0.beta1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/History.txt +11 -0
  2. data/Manifest.txt +71 -38
  3. data/lib/active_record/connection_adapters/cachedb_adapter.rb +1 -1
  4. data/lib/active_record/connection_adapters/derby_adapter.rb +1 -13
  5. data/lib/active_record/connection_adapters/h2_adapter.rb +1 -13
  6. data/lib/active_record/connection_adapters/hsqldb_adapter.rb +1 -13
  7. data/lib/active_record/connection_adapters/informix_adapter.rb +1 -1
  8. data/lib/active_record/connection_adapters/jdbc_adapter.rb +1 -661
  9. data/lib/active_record/connection_adapters/jndi_adapter.rb +1 -1
  10. data/lib/active_record/connection_adapters/mssql_adapter.rb +1 -13
  11. data/lib/active_record/connection_adapters/mysql_adapter.rb +1 -13
  12. data/lib/active_record/connection_adapters/oracle_adapter.rb +1 -1
  13. data/lib/active_record/connection_adapters/postgresql_adapter.rb +1 -13
  14. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -13
  15. data/lib/activerecord-jdbc-adapter.rb +2 -2
  16. data/lib/arjdbc.rb +29 -0
  17. data/lib/arjdbc/cachedb.rb +3 -0
  18. data/lib/arjdbc/cachedb/adapter.rb +20 -0
  19. data/lib/arjdbc/cachedb/connection_methods.rb +10 -0
  20. data/lib/arjdbc/db2.rb +2 -0
  21. data/lib/{jdbc_adapter/jdbc_db2.rb → arjdbc/db2/adapter.rb} +1 -17
  22. data/lib/arjdbc/derby.rb +7 -0
  23. data/lib/{jdbc_adapter/jdbc_derby.rb → arjdbc/derby/adapter.rb} +8 -26
  24. data/lib/arjdbc/derby/connection_methods.rb +18 -0
  25. data/lib/arjdbc/discover.rb +99 -0
  26. data/lib/arjdbc/firebird.rb +2 -0
  27. data/lib/{jdbc_adapter/jdbc_firebird.rb → arjdbc/firebird/adapter.rb} +12 -16
  28. data/lib/arjdbc/h2.rb +4 -0
  29. data/lib/arjdbc/h2/adapter.rb +15 -0
  30. data/lib/arjdbc/h2/connection_methods.rb +12 -0
  31. data/lib/arjdbc/hsqldb.rb +4 -0
  32. data/lib/{jdbc_adapter/jdbc_hsqldb.rb → arjdbc/hsqldb/adapter.rb} +6 -58
  33. data/lib/arjdbc/hsqldb/connection_methods.rb +14 -0
  34. data/lib/arjdbc/informix.rb +3 -0
  35. data/lib/{jdbc_adapter/jdbc_informix.rb → arjdbc/informix/adapter.rb} +6 -19
  36. data/lib/arjdbc/informix/connection_methods.rb +10 -0
  37. data/lib/arjdbc/jdbc.rb +2 -0
  38. data/lib/arjdbc/jdbc/adapter.rb +235 -0
  39. data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
  40. data/lib/arjdbc/jdbc/callbacks.rb +44 -0
  41. data/lib/arjdbc/jdbc/column.rb +38 -0
  42. data/lib/arjdbc/jdbc/compatibility.rb +51 -0
  43. data/lib/arjdbc/jdbc/connection.rb +97 -0
  44. data/lib/arjdbc/jdbc/connection_methods.rb +16 -0
  45. data/lib/arjdbc/jdbc/core_ext.rb +24 -0
  46. data/lib/arjdbc/jdbc/discover.rb +18 -0
  47. data/lib/arjdbc/jdbc/driver.rb +44 -0
  48. data/lib/arjdbc/jdbc/extension.rb +47 -0
  49. data/lib/arjdbc/jdbc/java.rb +14 -0
  50. data/lib/{jdbc_adapter → arjdbc/jdbc}/jdbc.rake +0 -0
  51. data/lib/{jdbc_adapter → arjdbc/jdbc}/missing_functionality_helper.rb +5 -5
  52. data/lib/arjdbc/jdbc/quoted_primary_key.rb +28 -0
  53. data/lib/{jdbc_adapter → arjdbc/jdbc}/railtie.rb +1 -1
  54. data/lib/{jdbc_adapter → arjdbc/jdbc}/rake_tasks.rb +0 -0
  55. data/lib/arjdbc/jdbc/require_driver.rb +16 -0
  56. data/lib/arjdbc/jdbc/type_converter.rb +119 -0
  57. data/lib/arjdbc/mimer.rb +2 -0
  58. data/lib/{jdbc_adapter/jdbc_mimer.rb → arjdbc/mimer/adapter.rb} +16 -19
  59. data/lib/arjdbc/mssql.rb +4 -0
  60. data/lib/{jdbc_adapter/jdbc_mssql.rb → arjdbc/mssql/adapter.rb} +19 -31
  61. data/lib/arjdbc/mssql/connection_methods.rb +13 -0
  62. data/lib/{jdbc_adapter → arjdbc/mssql}/tsql_helper.rb +1 -1
  63. data/lib/arjdbc/mysql.rb +4 -0
  64. data/lib/arjdbc/mysql/adapter.rb +388 -0
  65. data/lib/arjdbc/mysql/connection_methods.rb +26 -0
  66. data/lib/arjdbc/oracle.rb +3 -0
  67. data/lib/{jdbc_adapter/jdbc_oracle.rb → arjdbc/oracle/adapter.rb} +9 -17
  68. data/lib/arjdbc/oracle/connection_methods.rb +11 -0
  69. data/lib/arjdbc/postgresql.rb +4 -0
  70. data/lib/{jdbc_adapter/jdbc_postgre.rb → arjdbc/postgresql/adapter.rb} +7 -36
  71. data/lib/arjdbc/postgresql/connection_methods.rb +21 -0
  72. data/lib/arjdbc/sqlite3.rb +4 -0
  73. data/lib/{jdbc_adapter/jdbc_sqlite3.rb → arjdbc/sqlite3/adapter.rb} +106 -104
  74. data/lib/arjdbc/sqlite3/connection_methods.rb +33 -0
  75. data/lib/arjdbc/sybase.rb +2 -0
  76. data/lib/arjdbc/sybase/adapter.rb +46 -0
  77. data/lib/arjdbc/version.rb +8 -0
  78. data/lib/jdbc_adapter.rb +2 -27
  79. data/lib/jdbc_adapter/version.rb +3 -5
  80. data/rails_generators/templates/config/initializers/jdbc.rb +1 -1
  81. data/rakelib/compile.rake +3 -2
  82. data/rakelib/package.rake +3 -3
  83. data/src/java/{jdbc_adapter/JdbcDerbySpec.java → arjdbc/derby/DerbyModule.java} +32 -32
  84. data/src/java/{jdbc_adapter/JdbcAdapterInternalService.java → arjdbc/jdbc/AdapterJavaService.java} +13 -7
  85. data/src/java/{jdbc_adapter → arjdbc/jdbc}/JdbcConnectionFactory.java +6 -6
  86. data/src/java/{jdbc_adapter → arjdbc/jdbc}/RubyJdbcConnection.java +91 -16
  87. data/src/java/arjdbc/jdbc/SQLBlock.java +48 -0
  88. data/src/java/{jdbc_adapter → arjdbc/mssql}/MssqlRubyJdbcConnection.java +5 -2
  89. data/src/java/{jdbc_adapter/JdbcMySQLSpec.java → arjdbc/mysql/MySQLModule.java} +12 -12
  90. data/src/java/{jdbc_adapter/PostgresRubyJdbcConnection.java → arjdbc/postgresql/PostgresqlRubyJdbcConnection.java} +11 -9
  91. data/src/java/arjdbc/sqlite3/Sqlite3RubyJdbcConnection.java +64 -0
  92. data/test/abstract_db_create.rb +4 -1
  93. data/test/activerecord/connection_adapters/type_conversion_test.rb +1 -1
  94. data/test/db/cachedb.rb +0 -0
  95. data/test/db/derby.rb +12 -14
  96. data/test/db/hsqldb.rb +3 -2
  97. data/test/db/jndi_config.rb +4 -4
  98. data/test/db/sqlite3.rb +2 -6
  99. data/test/db2_simple_test.rb +23 -0
  100. data/test/derby_migration_test.rb +50 -3
  101. data/test/jdbc_common.rb +1 -1
  102. data/test/jndi_callbacks_test.rb +1 -0
  103. data/test/postgres_nonseq_pkey_test.rb +0 -2
  104. data/test/postgres_schema_search_path_test.rb +0 -2
  105. data/test/simple.rb +3 -3
  106. data/test/sybase_jtds_simple_test.rb +22 -0
  107. metadata +82 -46
  108. data/lib/active_record/connection_adapters/jdbc_adapter_spec.rb +0 -26
  109. data/lib/jdbc_adapter/jdbc_adapter_internal.jar +0 -0
  110. data/lib/jdbc_adapter/jdbc_cachedb.rb +0 -33
  111. data/lib/jdbc_adapter/jdbc_mysql.rb +0 -260
  112. data/lib/jdbc_adapter/jdbc_sybase.rb +0 -50
  113. data/src/java/jdbc_adapter/SQLBlock.java +0 -27
  114. data/src/java/jdbc_adapter/Sqlite3RubyJdbcConnection.java +0 -41
  115. data/test/jdbc_adapter/jdbc_db2_test.rb +0 -26
  116. data/test/jdbc_adapter/jdbc_sybase_test.rb +0 -33
  117. data/test/minirunit.rb +0 -109
  118. data/test/minirunit/testConnect.rb +0 -14
  119. data/test/minirunit/testH2.rb +0 -73
  120. data/test/minirunit/testHsqldb.rb +0 -73
  121. data/test/minirunit/testLoadActiveRecord.rb +0 -3
  122. data/test/minirunit/testMysql.rb +0 -83
  123. data/test/minirunit/testRawSelect.rb +0 -24
@@ -0,0 +1,26 @@
1
+ # Don't need to load native mysql adapter
2
+ $LOADED_FEATURES << "active_record/connection_adapters/mysql_adapter.rb"
3
+
4
+ class ActiveRecord::Base
5
+ class << self
6
+ def mysql_connection(config)
7
+ require "arjdbc/mysql"
8
+ config[:port] ||= 3306
9
+ url_options = "zeroDateTimeBehavior=convertToNull&jdbcCompliantTruncation=false&useUnicode=true&characterEncoding="
10
+ url_options << (config[:encoding] || 'utf8')
11
+ if config[:url]
12
+ config[:url] = config[:url]['?'] ? "#{config[:url]}&#{url_options}" : "#{config[:url]}?#{url_options}"
13
+ else
14
+ config[:url] = "jdbc:mysql://#{config[:host]}:#{config[:port]}/#{config[:database]}?#{url_options}"
15
+ end
16
+ config[:driver] ||= "com.mysql.jdbc.Driver"
17
+ config[:adapter_class] = ActiveRecord::ConnectionAdapters::MysqlAdapter
18
+ connection = jdbc_connection(config)
19
+ ::ArJdbc::MySQL.kill_cancel_timer(connection.raw_connection)
20
+ connection
21
+ end
22
+ alias_method :jdbcmysql_connection, :mysql_connection
23
+ end
24
+ end
25
+
26
+
@@ -0,0 +1,3 @@
1
+ require 'arjdbc/jdbc'
2
+ require 'arjdbc/oracle/connection_methods'
3
+ require 'arjdbc/oracle/adapter'
@@ -1,13 +1,8 @@
1
- module ::JdbcSpec
2
- module ActiveRecordExtensions
3
- def oracle_connection(config)
4
- config[:port] ||= 1521
5
- config[:url] ||= "jdbc:oracle:thin:@#{config[:host]}:#{config[:port]}:#{config[:database]}"
6
- config[:driver] ||= "oracle.jdbc.driver.OracleDriver"
7
- jdbc_connection(config)
8
- end
9
- end
1
+ module ActiveRecord::ConnectionAdapters
2
+ OracleAdapter = Class.new(AbstractAdapter) unless const_defined?(:OracleAdapter)
3
+ end
10
4
 
5
+ module ::ArJdbc
11
6
  module Oracle
12
7
  def self.extended(mod)
13
8
  unless @lob_callback_added
@@ -26,19 +21,16 @@ module ::JdbcSpec
26
21
  ActiveRecord::Base.after_save :after_save_with_oracle_lob
27
22
  @lob_callback_added = true
28
23
  end
29
- ActiveRecord::Base.extend JdbcSpec::QuotedPrimaryKeyExtension
24
+ require 'arjdbc/jdbc/quoted_primary_key'
25
+ ActiveRecord::Base.extend ArJdbc::QuotedPrimaryKeyExtension
30
26
  mod.class.class_eval do
31
27
  alias_chained_method :insert, :query_dirty, :insert
32
28
  alias_chained_method :columns, :query_cache, :columns
33
29
  end
34
30
  end
35
31
 
36
- def self.adapter_matcher(name, *)
37
- name =~ /oracle/i ? self : false
38
- end
39
-
40
32
  def self.column_selector
41
- [/oracle/i, lambda {|cfg,col| col.extend(::JdbcSpec::Oracle::Column)}]
33
+ [/oracle/i, lambda {|cfg,col| col.extend(::ArJdbc::Oracle::Column)}]
42
34
  end
43
35
 
44
36
  module Column
@@ -52,7 +44,7 @@ module ::JdbcSpec
52
44
  def type_cast(value)
53
45
  return nil if value.nil?
54
46
  case type
55
- when :datetime then JdbcSpec::Oracle::Column.string_to_time(value, self.class)
47
+ when :datetime then ArJdbc::Oracle::Column.string_to_time(value, self.class)
56
48
  else
57
49
  super
58
50
  end
@@ -60,7 +52,7 @@ module ::JdbcSpec
60
52
 
61
53
  def type_cast_code(var_name)
62
54
  case type
63
- when :datetime then "JdbcSpec::Oracle::Column.string_to_time(#{var_name}, self.class)"
55
+ when :datetime then "ArJdbc::Oracle::Column.string_to_time(#{var_name}, self.class)"
64
56
  else
65
57
  super
66
58
  end
@@ -0,0 +1,11 @@
1
+ class ActiveRecord::Base
2
+ class << self
3
+ def oracle_connection(config)
4
+ config[:port] ||= 1521
5
+ config[:url] ||= "jdbc:oracle:thin:@#{config[:host]}:#{config[:port]}:#{config[:database]}"
6
+ config[:driver] ||= "oracle.jdbc.driver.OracleDriver"
7
+ jdbc_connection(config)
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,4 @@
1
+ require 'arjdbc/jdbc'
2
+ jdbc_require_driver 'jdbc/postgres'
3
+ require 'arjdbc/postgresql/connection_methods'
4
+ require 'arjdbc/postgresql/adapter'
@@ -1,24 +1,8 @@
1
+ module ActiveRecord::ConnectionAdapters
2
+ PostgreSQLAdapter = Class.new(AbstractAdapter) unless const_defined?(:PostgreSQLAdapter)
3
+ end
1
4
 
2
- module ::JdbcSpec
3
- # Don't need to load native postgres adapter
4
- $LOADED_FEATURES << "active_record/connection_adapters/postgresql_adapter.rb"
5
-
6
- module ActiveRecordExtensions
7
- add_method_to_remove_from_ar_base(:postgresql_connection)
8
-
9
- def postgresql_connection(config)
10
- require File.dirname(__FILE__) + "/../active_record/connection_adapters/postgresql_adapter"
11
- config[:host] ||= "localhost"
12
- config[:port] ||= 5432
13
- config[:url] ||= "jdbc:postgresql://#{config[:host]}:#{config[:port]}/#{config[:database]}"
14
- config[:url] << config[:pg_params] if config[:pg_params]
15
- config[:driver] ||= "org.postgresql.Driver"
16
- conn = jdbc_connection(config)
17
- conn.execute("SET SEARCH_PATH TO #{config[:schema_search_path]}") if config[:schema_search_path]
18
- conn
19
- end
20
- end
21
-
5
+ module ::ArJdbc
22
6
  module PostgreSQL
23
7
  def self.extended(mod)
24
8
  mod.class.class_eval do
@@ -26,12 +10,8 @@ module ::JdbcSpec
26
10
  end
27
11
  end
28
12
 
29
- def self.adapter_matcher(name, *)
30
- name =~ /postgre/i ? self : false
31
- end
32
-
33
13
  def self.column_selector
34
- [/postgre/i, lambda {|cfg,col| col.extend(::JdbcSpec::PostgreSQL::Column)}]
14
+ [/postgre/i, lambda {|cfg,col| col.extend(::ArJdbc::PostgreSQL::Column)}]
35
15
  end
36
16
 
37
17
  def self.jdbc_connection_class
@@ -188,9 +168,9 @@ module ::JdbcSpec
188
168
  end
189
169
 
190
170
  def quote_regclass(table_name)
191
- table_name.to_s.split('.').map do |part|
171
+ table_name.to_s.split('.').map do |part|
192
172
  part =~ /".*"/i ? part : quote_table_name(part)
193
- end.join('.')
173
+ end.join('.')
194
174
  end
195
175
 
196
176
  # Find a table's primary key and sequence.
@@ -381,15 +361,6 @@ module ::JdbcSpec
381
361
  end
382
362
  end
383
363
 
384
- def _execute(sql, name = nil)
385
- case sql.strip
386
- when /\A\(?\s*(select|show)/i then
387
- @connection.execute_query(sql)
388
- else
389
- @connection.execute_update(sql)
390
- end
391
- end
392
-
393
364
  # SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
394
365
  #
395
366
  # PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and
@@ -0,0 +1,21 @@
1
+ # Don't need to load native postgres adapter
2
+ $LOADED_FEATURES << "active_record/connection_adapters/postgresql_adapter.rb"
3
+
4
+ class ActiveRecord::Base
5
+ class << self
6
+ def postgresql_connection(config)
7
+ require "arjdbc/postgresql"
8
+ config[:host] ||= "localhost"
9
+ config[:port] ||= 5432
10
+ config[:url] ||= "jdbc:postgresql://#{config[:host]}:#{config[:port]}/#{config[:database]}"
11
+ config[:url] << config[:pg_params] if config[:pg_params]
12
+ config[:driver] ||= "org.postgresql.Driver"
13
+ conn = jdbc_connection(config)
14
+ conn.execute("SET SEARCH_PATH TO #{config[:schema_search_path]}") if config[:schema_search_path]
15
+ conn
16
+ end
17
+ alias_method :jdbcpostgresql_connection, :postgresql_connection
18
+ end
19
+ end
20
+
21
+
@@ -0,0 +1,4 @@
1
+ require 'arjdbc/jdbc'
2
+ jdbc_require_driver 'jdbc/sqlite3'
3
+ require 'arjdbc/sqlite3/connection_methods'
4
+ require 'arjdbc/sqlite3/adapter'
@@ -1,47 +1,13 @@
1
- module ::JdbcSpec
2
- # Don't need to load native postgres adapter
3
- $LOADED_FEATURES << "active_record/connection_adapters/sqlite3_adapter.rb"
1
+ require 'arjdbc/jdbc/missing_functionality_helper'
4
2
 
5
- module ActiveRecordExtensions
6
- add_method_to_remove_from_ar_base(:sqlite3_connection)
7
-
8
- def sqlite3_connection(config)
9
- require File.dirname(__FILE__) + "/../active_record/connection_adapters/sqlite3_adapter"
10
-
11
- parse_sqlite3_config!(config)
12
-
13
- config[:url] ||= "jdbc:sqlite:#{config[:database]}"
14
- config[:driver] ||= "org.sqlite.JDBC"
15
- jdbc_connection(config)
16
- end
17
-
18
- def parse_sqlite3_config!(config)
19
- config[:database] ||= config[:dbfile]
20
-
21
- # Allow database path relative to RAILS_ROOT, but only if
22
- # the database path is not the special path that tells
23
- # Sqlite to build a database only in memory.
24
- rails_root_defined = defined?(Rails.root) || Object.const_defined?(:RAILS_ROOT)
25
- if rails_root_defined && ':memory:' != config[:database]
26
- rails_root = defined?(Rails.root) ? Rails.root : RAILS_ROOT
27
- config[:database] = File.expand_path(config[:database], rails_root)
28
- end
29
- end
30
- end
3
+ module ActiveRecord::ConnectionAdapters
4
+ Sqlite3Adapter = Class.new(AbstractAdapter) unless const_defined?(:Sqlite3Adapter)
5
+ end
31
6
 
7
+ module ::ArJdbc
32
8
  module SQLite3
33
- def self.extended(base)
34
- base.class.class_eval do
35
- alias_chained_method :insert, :query_dirty, :insert
36
- end
37
- end
38
-
39
- def self.adapter_matcher(name, *)
40
- name =~ /sqlite/i ? self : false
41
- end
42
-
43
9
  def self.column_selector
44
- [/sqlite/i, lambda {|cfg,col| col.extend(::JdbcSpec::SQLite3::Column)}]
10
+ [/sqlite/i, lambda {|cfg,col| col.extend(::ArJdbc::SQLite3::Column)}]
45
11
  end
46
12
 
47
13
  def self.jdbc_connection_class
@@ -57,12 +23,12 @@ module ::JdbcSpec
57
23
  return nil if value.nil?
58
24
  case type
59
25
  when :string then value
60
- when :integer then JdbcSpec::SQLite3::Column.cast_to_integer(value)
26
+ when :integer then ArJdbc::SQLite3::Column.cast_to_integer(value)
61
27
  when :primary_key then defined?(value.to_i) ? value.to_i : (value ? 1 : 0)
62
28
  when :float then value.to_f
63
- when :datetime then JdbcSpec::SQLite3::Column.cast_to_date_or_time(value)
64
- when :date then JdbcSpec::SQLite3::Column.cast_to_date_or_time(value)
65
- when :time then JdbcSpec::SQLite3::Column.cast_to_time(value)
29
+ when :datetime then ArJdbc::SQLite3::Column.cast_to_date_or_time(value)
30
+ when :date then ArJdbc::SQLite3::Column.cast_to_date_or_time(value)
31
+ when :time then ArJdbc::SQLite3::Column.cast_to_time(value)
66
32
  when :decimal then self.class.value_to_decimal(value)
67
33
  when :boolean then self.class.value_to_boolean(value)
68
34
  else value
@@ -71,10 +37,10 @@ module ::JdbcSpec
71
37
 
72
38
  def type_cast_code(var_name)
73
39
  case type
74
- when :integer then "JdbcSpec::SQLite3::Column.cast_to_integer(#{var_name})"
75
- when :datetime then "JdbcSpec::SQLite3::Column.cast_to_date_or_time(#{var_name})"
76
- when :date then "JdbcSpec::SQLite3::Column.cast_to_date_or_time(#{var_name})"
77
- when :time then "JdbcSpec::SQLite3::Column.cast_to_time(#{var_name})"
40
+ when :integer then "ArJdbc::SQLite3::Column.cast_to_integer(#{var_name})"
41
+ when :datetime then "ArJdbc::SQLite3::Column.cast_to_date_or_time(#{var_name})"
42
+ when :date then "ArJdbc::SQLite3::Column.cast_to_date_or_time(#{var_name})"
43
+ when :time then "ArJdbc::SQLite3::Column.cast_to_time(#{var_name})"
78
44
  else
79
45
  super
80
46
  end
@@ -98,11 +64,11 @@ module ::JdbcSpec
98
64
 
99
65
  def extract_precision(sql_type)
100
66
  case sql_type
101
- when /^(real)\((\d+)(,\d+)?\)/i then $2.to_i
67
+ when /^(real)\((\d+)(,\d+)?\)/i then $2.to_i
102
68
  else super
103
- end
69
+ end
104
70
  end
105
-
71
+
106
72
  def extract_scale(sql_type)
107
73
  case sql_type
108
74
  when /^(real)\((\d+)\)/i then 0
@@ -190,14 +156,7 @@ module ::JdbcSpec
190
156
  end
191
157
 
192
158
  def quote_column_name(name) #:nodoc:
193
- name = name.to_s
194
- # Did not find reference on values needing quoting, but these
195
- # happen in AR unit tests
196
- if name == "references" || name =~ /-/
197
- %Q("#{name}")
198
- else
199
- name
200
- end
159
+ %Q("#{name}")
201
160
  end
202
161
 
203
162
  def quote_string(str)
@@ -262,23 +221,13 @@ module ::JdbcSpec
262
221
  execute "ALTER TABLE #{name} RENAME TO #{new_name}"
263
222
  end
264
223
 
265
- def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
266
- log(sql,name) do
267
- @connection.execute_update(sql)
268
- end
269
- table = sql.split(" ", 4)[2]
270
- id_value || last_insert_id(table, nil)
271
- end
272
-
273
- def last_insert_id(table, sequence_name)
274
- Integer(select_value("SELECT SEQ FROM SQLITE_SEQUENCE WHERE NAME = '#{table}'"))
224
+ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
225
+ @connection.execute_update(sql)
226
+ id_value || last_insert_id
275
227
  end
276
228
 
277
- def add_limit_offset!(sql, options) #:nodoc:
278
- if options[:limit]
279
- sql << " LIMIT #{options[:limit]}"
280
- sql << " OFFSET #{options[:offset]}" if options[:offset]
281
- end
229
+ def last_insert_id
230
+ Integer(select_value("SELECT last_insert_rowid()"))
282
231
  end
283
232
 
284
233
  def tables(name = nil) #:nodoc:
@@ -304,11 +253,13 @@ module ::JdbcSpec
304
253
  name = row[0]
305
254
  index_sql = row[1]
306
255
  unique = (index_sql =~ /unique/i)
307
- cols = index_sql.match(/\((.*)\)/)[1].gsub(/,/,' ').split
256
+ cols = index_sql.match(/\((.*)\)/)[1].gsub(/,/,' ').split.map do |c|
257
+ match = /^"(.+)"$/.match(c); match ? match[1] : c
258
+ end
308
259
  ::ActiveRecord::ConnectionAdapters::IndexDefinition.new(table_name, name, unique, cols)
309
260
  end
310
261
  end
311
-
262
+
312
263
  def primary_key(table_name) #:nodoc:
313
264
  column = table_structure(table_name).find {|field| field['pk'].to_i == 1}
314
265
  column ? column['name'] : nil
@@ -319,14 +270,10 @@ module ::JdbcSpec
319
270
  end
320
271
 
321
272
  def _execute(sql, name = nil)
322
- if ActiveRecord::ConnectionAdapters::JdbcConnection::select?(sql)
323
- @connection.execute_query(sql)
324
- else
325
- affected_rows = @connection.execute_update(sql)
326
- ActiveRecord::ConnectionAdapters::JdbcConnection::insert?(sql) ? last_insert_id(sql.split(" ", 4)[2], nil) : affected_rows
327
- end
273
+ result = super
274
+ ActiveRecord::ConnectionAdapters::JdbcConnection::insert?(sql) ? last_insert_id : result
328
275
  end
329
-
276
+
330
277
  def select(sql, name=nil)
331
278
  execute(sql, name).map do |row|
332
279
  record = {}
@@ -338,49 +285,104 @@ module ::JdbcSpec
338
285
  record
339
286
  end
340
287
  end
341
-
288
+
342
289
  def table_structure(table_name)
343
290
  structure = @connection.execute_query("PRAGMA table_info(#{quote_table_name(table_name)})")
344
291
  raise ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'" if structure.empty?
345
292
  structure
346
293
  end
347
-
348
- def columns(table_name, name = nil) #:nodoc:
294
+
295
+ def columns(table_name, name = nil) #:nodoc:
349
296
  table_structure(table_name).map do |field|
350
297
  ::ActiveRecord::ConnectionAdapters::JdbcColumn.new(@config, field['name'], field['dflt_value'], field['type'], field['notnull'] == 0)
351
298
  end
352
299
  end
353
-
300
+
354
301
  # SELECT ... FOR UPDATE is redundant since the table is locked.
355
302
  def add_lock!(sql, options) #:nodoc:
356
303
  sql
357
304
  end
358
-
305
+
359
306
  protected
360
- include JdbcSpec::MissingFunctionalityHelper
307
+ include ArJdbc::MissingFunctionalityHelper
308
+
309
+ def translate_exception(exception, message)
310
+ case exception.message
311
+ when /column(s)? .* (is|are) not unique/
312
+ ActiveRecord::RecordNotUnique.new(message, exception)
313
+ else
314
+ super
315
+ end
316
+ end
361
317
  end
362
318
  end
363
319
 
364
- module ActiveRecord
365
- module ConnectionAdapters
366
- class JdbcColumn < Column
367
- def self.string_to_binary(value)
368
- value.gsub(/\0|%/n) do |b|
369
- case b
370
- when "\0" then "%00"
371
- when "\%" then "%25"
372
- end
320
+ module ActiveRecord::ConnectionAdapters
321
+ class SQLiteColumn < JdbcColumn
322
+ include ArJdbc::SQLite3::Column
323
+
324
+ def initialize(name, *args)
325
+ if Hash === name
326
+ super
327
+ else
328
+ super(nil, name, *args)
329
+ end
330
+ end
331
+
332
+ def call_discovered_column_callbacks(*)
333
+ end
334
+
335
+ def self.string_to_binary(value)
336
+ value.gsub(/\0|%/n) do |b|
337
+ case b
338
+ when "\0" then "%00"
339
+ when "\%" then "%25"
373
340
  end
374
341
  end
342
+ end
375
343
 
376
- def self.binary_to_string(value)
377
- value.gsub(/%00|%25/n) do |b|
378
- case b
379
- when "%00" then "\0"
380
- when "%25" then "%"
381
- end
344
+ def self.binary_to_string(value)
345
+ value.gsub(/%00|%25/n) do |b|
346
+ case b
347
+ when "%00" then "\0"
348
+ when "%25" then "%"
382
349
  end
383
350
  end
384
351
  end
352
+
353
+ def self.cast_to_integer(value)
354
+ return nil if value =~ /NULL/ or value.to_s.empty? or value.nil?
355
+ return (value.to_i) ? value.to_i : (value ? 1 : 0)
356
+ end
357
+
358
+ def self.cast_to_date_or_time(value)
359
+ return value if value.is_a? Date
360
+ return nil if value.blank?
361
+ guess_date_or_time((value.is_a? Time) ? value : cast_to_time(value))
362
+ end
363
+
364
+ def self.cast_to_time(value)
365
+ return value if value.is_a? Time
366
+ time_array = ParseDate.parsedate value
367
+ time_array[0] ||= 2000; time_array[1] ||= 1; time_array[2] ||= 1;
368
+ Time.send(ActiveRecord::Base.default_timezone, *time_array) rescue nil
369
+ end
370
+
371
+ def self.guess_date_or_time(value)
372
+ (value.hour == 0 and value.min == 0 and value.sec == 0) ?
373
+ Date.new(value.year, value.month, value.day) : value
374
+ end
375
+ end
376
+
377
+ class SQLite3Adapter < JdbcAdapter
378
+ include ArJdbc::SQLite3
379
+
380
+ def adapter_spec(config)
381
+ # return nil to avoid extending ArJdbc::SQLite3, which we've already done
382
+ end
383
+
384
+ def jdbc_column_class
385
+ ActiveRecord::ConnectionAdapters::SQLiteColumn
386
+ end
385
387
  end
386
388
  end