activerecord-jdbc-adapter 0.9.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. data/History.txt +163 -0
  2. data/Manifest.txt +130 -65
  3. data/README.txt +64 -72
  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 -633
  9. data/lib/active_record/connection_adapters/jndi_adapter.rb +1 -1
  10. data/lib/active_record/connection_adapters/mssql_adapter.rb +1 -0
  11. data/lib/active_record/connection_adapters/mysql2_adapter.rb +1 -0
  12. data/lib/active_record/connection_adapters/mysql_adapter.rb +1 -13
  13. data/lib/active_record/connection_adapters/oracle_adapter.rb +1 -1
  14. data/lib/active_record/connection_adapters/postgresql_adapter.rb +1 -13
  15. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -13
  16. data/lib/activerecord-jdbc-adapter.rb +6 -0
  17. data/lib/arel/engines/sql/compilers/db2_compiler.rb +9 -0
  18. data/lib/arel/engines/sql/compilers/derby_compiler.rb +6 -0
  19. data/lib/arel/engines/sql/compilers/h2_compiler.rb +6 -0
  20. data/lib/arel/engines/sql/compilers/hsqldb_compiler.rb +15 -0
  21. data/lib/arel/engines/sql/compilers/jdbc_compiler.rb +6 -0
  22. data/lib/arel/engines/sql/compilers/mssql_compiler.rb +46 -0
  23. data/lib/arel/visitors/db2.rb +15 -0
  24. data/lib/arel/visitors/derby.rb +19 -0
  25. data/lib/arel/visitors/hsqldb.rb +24 -0
  26. data/lib/arel/visitors/mssql.rb +44 -0
  27. data/lib/arjdbc.rb +29 -0
  28. data/lib/arjdbc/db2.rb +2 -0
  29. data/lib/arjdbc/db2/adapter.rb +413 -0
  30. data/lib/arjdbc/derby.rb +7 -0
  31. data/lib/{jdbc_adapter/jdbc_derby.rb → arjdbc/derby/adapter.rb} +58 -132
  32. data/lib/arjdbc/derby/connection_methods.rb +18 -0
  33. data/lib/arjdbc/discover.rb +92 -0
  34. data/lib/arjdbc/firebird.rb +2 -0
  35. data/lib/{jdbc_adapter/jdbc_firebird.rb → arjdbc/firebird/adapter.rb} +36 -14
  36. data/lib/arjdbc/h2.rb +4 -0
  37. data/lib/arjdbc/h2/adapter.rb +36 -0
  38. data/lib/arjdbc/h2/connection_methods.rb +12 -0
  39. data/lib/arjdbc/hsqldb.rb +4 -0
  40. data/lib/{jdbc_adapter/jdbc_hsqldb.rb → arjdbc/hsqldb/adapter.rb} +34 -62
  41. data/lib/arjdbc/hsqldb/connection_methods.rb +14 -0
  42. data/lib/arjdbc/informix.rb +3 -0
  43. data/lib/{jdbc_adapter/jdbc_informix.rb → arjdbc/informix/adapter.rb} +10 -19
  44. data/lib/arjdbc/informix/connection_methods.rb +10 -0
  45. data/lib/arjdbc/jdbc.rb +2 -0
  46. data/lib/arjdbc/jdbc/adapter.rb +285 -0
  47. data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
  48. data/lib/arjdbc/jdbc/callbacks.rb +44 -0
  49. data/lib/arjdbc/jdbc/column.rb +38 -0
  50. data/lib/arjdbc/jdbc/compatibility.rb +51 -0
  51. data/lib/arjdbc/jdbc/connection.rb +128 -0
  52. data/lib/arjdbc/jdbc/connection_methods.rb +16 -0
  53. data/lib/arjdbc/jdbc/core_ext.rb +24 -0
  54. data/lib/arjdbc/jdbc/discover.rb +18 -0
  55. data/lib/arjdbc/jdbc/driver.rb +44 -0
  56. data/lib/arjdbc/jdbc/extension.rb +47 -0
  57. data/lib/arjdbc/jdbc/java.rb +14 -0
  58. data/lib/{jdbc_adapter → arjdbc/jdbc}/jdbc.rake +36 -26
  59. data/lib/{jdbc_adapter → arjdbc/jdbc}/missing_functionality_helper.rb +5 -5
  60. data/lib/arjdbc/jdbc/quoted_primary_key.rb +28 -0
  61. data/lib/arjdbc/jdbc/railtie.rb +9 -0
  62. data/lib/arjdbc/jdbc/rake_tasks.rb +10 -0
  63. data/lib/arjdbc/jdbc/require_driver.rb +16 -0
  64. data/lib/arjdbc/jdbc/type_converter.rb +126 -0
  65. data/lib/arjdbc/mimer.rb +2 -0
  66. data/lib/{jdbc_adapter/jdbc_mimer.rb → arjdbc/mimer/adapter.rb} +17 -16
  67. data/lib/arjdbc/mssql.rb +4 -0
  68. data/lib/arjdbc/mssql/adapter.rb +472 -0
  69. data/lib/arjdbc/mssql/connection_methods.rb +30 -0
  70. data/lib/arjdbc/mssql/limit_helpers.rb +92 -0
  71. data/lib/{jdbc_adapter → arjdbc/mssql}/tsql_helper.rb +2 -1
  72. data/lib/arjdbc/mysql.rb +4 -0
  73. data/lib/arjdbc/mysql/adapter.rb +416 -0
  74. data/lib/arjdbc/mysql/connection_methods.rb +27 -0
  75. data/lib/arjdbc/oracle.rb +3 -0
  76. data/lib/{jdbc_adapter/jdbc_oracle.rb → arjdbc/oracle/adapter.rb} +68 -33
  77. data/lib/arjdbc/oracle/connection_methods.rb +11 -0
  78. data/lib/arjdbc/postgresql.rb +4 -0
  79. data/lib/{jdbc_adapter/jdbc_postgre.rb → arjdbc/postgresql/adapter.rb} +147 -72
  80. data/lib/arjdbc/postgresql/connection_methods.rb +21 -0
  81. data/lib/arjdbc/sqlite3.rb +4 -0
  82. data/lib/arjdbc/sqlite3/adapter.rb +381 -0
  83. data/lib/arjdbc/sqlite3/connection_methods.rb +34 -0
  84. data/lib/arjdbc/sybase.rb +2 -0
  85. data/lib/arjdbc/sybase/adapter.rb +46 -0
  86. data/lib/arjdbc/version.rb +8 -0
  87. data/lib/generators/jdbc/jdbc_generator.rb +9 -0
  88. data/lib/jdbc_adapter.rb +2 -27
  89. data/lib/jdbc_adapter/rake_tasks.rb +3 -10
  90. data/lib/jdbc_adapter/version.rb +3 -5
  91. data/lib/pg.rb +23 -1
  92. data/rails_generators/jdbc_generator.rb +2 -2
  93. data/rails_generators/templates/{jdbc.rb → config/initializers/jdbc.rb} +1 -1
  94. data/rails_generators/templates/{jdbc.rake → lib/tasks/jdbc.rake} +2 -2
  95. data/rakelib/compile.rake +5 -3
  96. data/rakelib/db.rake +19 -0
  97. data/rakelib/package.rake +12 -6
  98. data/rakelib/test.rake +24 -14
  99. data/src/java/{jdbc_adapter/JdbcDerbySpec.java → arjdbc/derby/DerbyModule.java} +63 -34
  100. data/src/java/arjdbc/h2/H2RubyJdbcConnection.java +70 -0
  101. data/src/java/{jdbc_adapter/MssqlRubyJdbcConnection.java → arjdbc/informix/InformixRubyJdbcConnection.java} +15 -12
  102. data/src/java/{jdbc_adapter/JdbcAdapterInternalService.java → arjdbc/jdbc/AdapterJavaService.java} +21 -8
  103. data/src/java/{jdbc_adapter → arjdbc/jdbc}/JdbcConnectionFactory.java +6 -6
  104. data/src/java/{jdbc_adapter → arjdbc/jdbc}/RubyJdbcConnection.java +210 -67
  105. data/src/java/arjdbc/jdbc/SQLBlock.java +48 -0
  106. data/src/java/arjdbc/mssql/MssqlRubyJdbcConnection.java +127 -0
  107. data/src/java/{jdbc_adapter/JdbcMySQLSpec.java → arjdbc/mysql/MySQLModule.java} +12 -12
  108. data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +87 -0
  109. data/src/java/arjdbc/oracle/OracleRubyJdbcConnection.java +85 -0
  110. data/src/java/{jdbc_adapter/PostgresRubyJdbcConnection.java → arjdbc/postgresql/PostgresqlRubyJdbcConnection.java} +11 -9
  111. data/src/java/arjdbc/sqlite3/Sqlite3RubyJdbcConnection.java +64 -0
  112. data/test/abstract_db_create.rb +98 -19
  113. data/test/activerecord/connection_adapters/type_conversion_test.rb +1 -1
  114. data/test/db/db2.rb +4 -2
  115. data/test/db/derby.rb +12 -14
  116. data/test/db/hsqldb.rb +3 -2
  117. data/test/db/jndi_config.rb +22 -12
  118. data/test/db/mssql.rb +5 -5
  119. data/test/db/sqlite3.rb +2 -6
  120. data/test/db2_simple_test.rb +56 -0
  121. data/test/derby_migration_test.rb +50 -3
  122. data/test/derby_simple_test.rb +87 -0
  123. data/test/generic_jdbc_connection_test.rb +21 -1
  124. data/test/h2_simple_test.rb +35 -0
  125. data/test/jdbc_common.rb +4 -5
  126. data/test/jndi_callbacks_test.rb +3 -1
  127. data/test/jndi_test.rb +1 -11
  128. data/test/models/entry.rb +20 -0
  129. data/test/models/mixed_case.rb +7 -2
  130. data/test/models/string_id.rb +18 -0
  131. data/test/mssql_db_create_test.rb +26 -0
  132. data/test/mssql_identity_insert_test.rb +19 -0
  133. data/test/mssql_legacy_types_test.rb +58 -0
  134. data/test/mssql_limit_offset_test.rb +136 -0
  135. data/test/mssql_multibyte_test.rb +18 -0
  136. data/test/mssql_simple_test.rb +49 -0
  137. data/test/mysql_db_create_test.rb +8 -6
  138. data/test/mysql_info_test.rb +123 -0
  139. data/test/mysql_simple_test.rb +19 -1
  140. data/test/oracle_simple_test.rb +9 -20
  141. data/test/oracle_specific_test.rb +83 -0
  142. data/test/pick_rails_version.rb +3 -0
  143. data/test/postgres_db_create_test.rb +11 -0
  144. data/test/postgres_drop_db_test.rb +16 -0
  145. data/test/postgres_mixed_case_test.rb +11 -1
  146. data/test/postgres_nonseq_pkey_test.rb +0 -2
  147. data/test/postgres_schema_search_path_test.rb +44 -0
  148. data/test/postgres_simple_test.rb +53 -0
  149. data/test/postgres_table_alias_length_test.rb +15 -0
  150. data/test/simple.rb +103 -9
  151. data/test/sqlite3_simple_test.rb +1 -0
  152. data/test/sybase_jtds_simple_test.rb +22 -0
  153. metadata +239 -137
  154. data/lib/active_record/connection_adapters/cachedb_adapter.rb +0 -1
  155. data/lib/active_record/connection_adapters/jdbc_adapter_spec.rb +0 -26
  156. data/lib/jdbc_adapter/jdbc_adapter_internal.jar +0 -0
  157. data/lib/jdbc_adapter/jdbc_cachedb.rb +0 -33
  158. data/lib/jdbc_adapter/jdbc_db2.rb +0 -191
  159. data/lib/jdbc_adapter/jdbc_mssql.rb +0 -333
  160. data/lib/jdbc_adapter/jdbc_mysql.rb +0 -236
  161. data/lib/jdbc_adapter/jdbc_sqlite3.rb +0 -379
  162. data/lib/jdbc_adapter/jdbc_sybase.rb +0 -50
  163. data/src/java/jdbc_adapter/SQLBlock.java +0 -27
  164. data/src/java/jdbc_adapter/Sqlite3RubyJdbcConnection.java +0 -41
  165. data/test/cachedb_simple_test.rb +0 -6
  166. data/test/db/cachedb.rb +0 -9
  167. data/test/jdbc_adapter/jdbc_db2_test.rb +0 -26
  168. data/test/jdbc_adapter/jdbc_sybase_test.rb +0 -33
  169. data/test/minirunit.rb +0 -109
  170. data/test/minirunit/testConnect.rb +0 -14
  171. data/test/minirunit/testH2.rb +0 -73
  172. data/test/minirunit/testHsqldb.rb +0 -73
  173. data/test/minirunit/testLoadActiveRecord.rb +0 -3
  174. data/test/minirunit/testMysql.rb +0 -83
  175. data/test/minirunit/testRawSelect.rb +0 -24
@@ -1,236 +0,0 @@
1
- require 'active_record/connection_adapters/abstract/schema_definitions'
2
-
3
- module ::JdbcSpec
4
- # Don't need to load native mysql adapter
5
- $LOADED_FEATURES << "active_record/connection_adapters/mysql_adapter.rb"
6
-
7
- module ActiveRecordExtensions
8
- add_method_to_remove_from_ar_base(:mysql_connection)
9
-
10
- def mysql_connection(config)
11
- require File.dirname(__FILE__) + "/../active_record/connection_adapters/mysql_adapter"
12
- config[:port] ||= 3306
13
- url_options = "zeroDateTimeBehavior=convertToNull&jdbcCompliantTruncation=false&useUnicode=true&characterEncoding="
14
- url_options << (config[:encoding] || 'utf8')
15
- if config[:url]
16
- config[:url] = config[:url]['?'] ? "#{config[:url]}&#{url_options}" : "#{config[:url]}?#{url_options}"
17
- else
18
- config[:url] = "jdbc:mysql://#{config[:host]}:#{config[:port]}/#{config[:database]}?#{url_options}"
19
- end
20
- config[:driver] = "com.mysql.jdbc.Driver"
21
- connection = jdbc_connection(config)
22
- ::JdbcSpec::MySQL.kill_cancel_timer(connection.raw_connection)
23
- connection
24
- end
25
- end
26
-
27
- module MySQL
28
- def self.adapter_matcher(name, *)
29
- name =~ /mysql/i ? self : false
30
- end
31
-
32
- def self.column_selector
33
- [/mysql/i, lambda {|cfg,col| col.extend(::JdbcSpec::MySQL::Column)}]
34
- end
35
-
36
- def self.extended(adapter)
37
- adapter.execute("SET SQL_AUTO_IS_NULL=0")
38
- end
39
-
40
- module Column
41
- TYPES_ALLOWING_EMPTY_STRING_DEFAULT = Set.new([:binary, :string, :text])
42
-
43
- def simplified_type(field_type)
44
- return :boolean if field_type =~ /tinyint\(1\)|bit/i
45
- return :string if field_type =~ /enum/i
46
- super
47
- end
48
-
49
- def init_column(name, default, *args)
50
- @original_default = default
51
- @default = nil if missing_default_forged_as_empty_string?
52
- end
53
-
54
- # MySQL misreports NOT NULL column default when none is given.
55
- # We can't detect this for columns which may have a legitimate ''
56
- # default (string, text, binary) but we can for others (integer,
57
- # datetime, boolean, and the rest).
58
- #
59
- # Test whether the column has default '', is not null, and is not
60
- # a type allowing default ''.
61
- def missing_default_forged_as_empty_string?
62
- !null && @original_default == '' && !TYPES_ALLOWING_EMPTY_STRING_DEFAULT.include?(type)
63
- end
64
- end
65
-
66
- def modify_types(tp)
67
- tp[:primary_key] = "int(11) DEFAULT NULL auto_increment PRIMARY KEY"
68
- tp[:decimal] = { :name => "decimal" }
69
- tp[:timestamp] = { :name => "datetime" }
70
- tp[:datetime][:limit] = nil
71
- tp
72
- end
73
-
74
- # QUOTING ==================================================
75
-
76
- def quote(value, column = nil)
77
- return value.quoted_id if value.respond_to?(:quoted_id)
78
-
79
- if column && column.type == :primary_key
80
- value.to_s
81
- elsif column && String === value && column.type == :binary && column.class.respond_to?(:string_to_binary)
82
- s = column.class.string_to_binary(value).unpack("H*")[0]
83
- "x'#{s}'"
84
- elsif BigDecimal === value
85
- "'#{value.to_s("F")}'"
86
- else
87
- super
88
- end
89
- end
90
-
91
- def quoted_true
92
- "1"
93
- end
94
-
95
- def quoted_false
96
- "0"
97
- end
98
-
99
- def begin_db_transaction #:nodoc:
100
- @connection.begin
101
- rescue Exception
102
- # Transactions aren't supported
103
- end
104
-
105
- def commit_db_transaction #:nodoc:
106
- @connection.commit
107
- rescue Exception
108
- # Transactions aren't supported
109
- end
110
-
111
- def rollback_db_transaction #:nodoc:
112
- @connection.rollback
113
- rescue Exception
114
- # Transactions aren't supported
115
- end
116
-
117
- def disable_referential_integrity(&block) #:nodoc:
118
- old = select_value("SELECT @@FOREIGN_KEY_CHECKS")
119
- begin
120
- update("SET FOREIGN_KEY_CHECKS = 0")
121
- yield
122
- ensure
123
- update("SET FOREIGN_KEY_CHECKS = #{old}")
124
- end
125
- end
126
-
127
- # SCHEMA STATEMENTS ========================================
128
-
129
- def structure_dump #:nodoc:
130
- if supports_views?
131
- sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
132
- else
133
- sql = "SHOW TABLES"
134
- end
135
-
136
- select_all(sql).inject("") do |structure, table|
137
- table.delete('Table_type')
138
-
139
- hash = select_one("SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}")
140
-
141
- if(table = hash["Create Table"])
142
- structure += table + ";\n\n"
143
- elsif(view = hash["Create View"])
144
- structure += view + ";\n\n"
145
- end
146
- end
147
- end
148
-
149
- def recreate_database(name) #:nodoc:
150
- drop_database(name)
151
- create_database(name)
152
- end
153
-
154
- def character_set(options) #:nodoc:
155
- str = "CHARACTER SET `#{options[:charset] || 'utf8'}`"
156
- str += " COLLATE `#{options[:collation]}`" if options[:collation]
157
- str
158
- end
159
- private :character_set
160
-
161
- def create_database(name, options = {}) #:nodoc:
162
- execute "CREATE DATABASE `#{name}` DEFAULT #{character_set(options)}"
163
- end
164
-
165
- def drop_database(name) #:nodoc:
166
- execute "DROP DATABASE IF EXISTS `#{name}`"
167
- end
168
-
169
- def current_database
170
- select_one("SELECT DATABASE() as db")["db"]
171
- end
172
-
173
- def create_table(name, options = {}) #:nodoc:
174
- super(name, {:options => "ENGINE=InnoDB #{character_set(options)}"}.merge(options))
175
- end
176
-
177
- def rename_table(name, new_name)
178
- execute "RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}"
179
- end
180
-
181
- def change_column_default(table_name, column_name, default) #:nodoc:
182
- current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
183
-
184
- execute("ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{current_type} DEFAULT #{quote(default)}")
185
- end
186
-
187
- def change_column(table_name, column_name, type, options = {}) #:nodoc:
188
- unless options_include_default?(options)
189
- if column = columns(table_name).find { |c| c.name == column_name.to_s }
190
- options[:default] = column.default
191
- else
192
- raise "No such column: #{table_name}.#{column_name}"
193
- end
194
- end
195
-
196
- change_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
197
- add_column_options!(change_column_sql, options)
198
- execute(change_column_sql)
199
- end
200
-
201
- def rename_column(table_name, column_name, new_column_name) #:nodoc:
202
- cols = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")
203
- current_type = cols["Type"] || cols["COLUMN_TYPE"]
204
- execute "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_table_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
205
- end
206
-
207
- def add_limit_offset!(sql, options) #:nodoc:
208
- if limit = options[:limit]
209
- unless offset = options[:offset]
210
- sql << " LIMIT #{limit}"
211
- else
212
- sql << " LIMIT #{offset}, #{limit}"
213
- end
214
- end
215
- end
216
-
217
- def show_variable(var)
218
- res = execute("show variables like '#{var}'")
219
- row = res.detect {|row| row["Variable_name"] == var }
220
- row && row["Value"]
221
- end
222
-
223
- def charset
224
- show_variable("character_set_database")
225
- end
226
-
227
- def collation
228
- show_variable("collation_database")
229
- end
230
-
231
- private
232
- def supports_views?
233
- false
234
- end
235
- end
236
- end
@@ -1,379 +0,0 @@
1
- module ::JdbcSpec
2
- # Don't need to load native postgres adapter
3
- $LOADED_FEATURES << "active_record/connection_adapters/sqlite3_adapter.rb"
4
-
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
- if Object.const_defined?(:RAILS_ROOT) && ':memory:' != config[:database]
25
- config[:database] = File.expand_path(config[:database], RAILS_ROOT)
26
- end
27
- end
28
- end
29
-
30
- module SQLite3
31
- def self.adapter_matcher(name, *)
32
- name =~ /sqlite/i ? self : false
33
- end
34
-
35
- def self.column_selector
36
- [/sqlite/i, lambda {|cfg,col| col.extend(::JdbcSpec::SQLite3::Column)}]
37
- end
38
-
39
- def self.jdbc_connection_class
40
- ::ActiveRecord::ConnectionAdapters::Sqlite3JdbcConnection
41
- end
42
-
43
- module Column
44
- def init_column(name, default, *args)
45
- @default = '' if default =~ /NULL/
46
- end
47
-
48
- def type_cast(value)
49
- return nil if value.nil?
50
- case type
51
- when :string then value
52
- when :integer then JdbcSpec::SQLite3::Column.cast_to_integer(value)
53
- when :primary_key then defined?(value.to_i) ? value.to_i : (value ? 1 : 0)
54
- when :float then value.to_f
55
- when :datetime then JdbcSpec::SQLite3::Column.cast_to_date_or_time(value)
56
- when :date then JdbcSpec::SQLite3::Column.cast_to_date_or_time(value)
57
- when :time then JdbcSpec::SQLite3::Column.cast_to_time(value)
58
- when :decimal then self.class.value_to_decimal(value)
59
- when :boolean then self.class.value_to_boolean(value)
60
- else value
61
- end
62
- end
63
-
64
- def type_cast_code(var_name)
65
- case type
66
- when :integer then "JdbcSpec::SQLite3::Column.cast_to_integer(#{var_name})"
67
- when :datetime then "JdbcSpec::SQLite3::Column.cast_to_date_or_time(#{var_name})"
68
- when :date then "JdbcSpec::SQLite3::Column.cast_to_date_or_time(#{var_name})"
69
- when :time then "JdbcSpec::SQLite3::Column.cast_to_time(#{var_name})"
70
- else
71
- super
72
- end
73
- end
74
-
75
- private
76
- def simplified_type(field_type)
77
- case field_type
78
- when /boolean/i then :boolean
79
- when /text/i then :text
80
- when /varchar/i then :string
81
- when /int/i then :integer
82
- when /float/i then :float
83
- when /real/i then @scale == 0 ? :integer : :decimal
84
- when /datetime/i then :datetime
85
- when /date/i then :date
86
- when /time/i then :time
87
- when /blob/i then :binary
88
- end
89
- end
90
-
91
- def extract_precision(sql_type)
92
- case sql_type
93
- when /^(real)\((\d+)(,\d+)?\)/i then $2.to_i
94
- else super
95
- end
96
- end
97
-
98
- def extract_scale(sql_type)
99
- case sql_type
100
- when /^(real)\((\d+)\)/i then 0
101
- when /^(real)\((\d+)(,(\d+))\)/i then $4.to_i
102
- else super
103
- end
104
- end
105
-
106
- # Post process default value from JDBC into a Rails-friendly format (columns{-internal})
107
- def default_value(value)
108
- # jdbc returns column default strings with actual single quotes around the value.
109
- return $1 if value =~ /^'(.*)'$/
110
-
111
- value
112
- end
113
-
114
- def self.cast_to_integer(value)
115
- return nil if value =~ /NULL/ or value.to_s.empty? or value.nil?
116
- return (value.to_i) ? value.to_i : (value ? 1 : 0)
117
- end
118
-
119
- def self.cast_to_date_or_time(value)
120
- return value if value.is_a? Date
121
- return nil if value.blank?
122
- guess_date_or_time((value.is_a? Time) ? value : cast_to_time(value))
123
- end
124
-
125
- def self.cast_to_time(value)
126
- return value if value.is_a? Time
127
- time_array = ParseDate.parsedate value
128
- time_array[0] ||= 2000; time_array[1] ||= 1; time_array[2] ||= 1;
129
- Time.send(ActiveRecord::Base.default_timezone, *time_array) rescue nil
130
- end
131
-
132
- def self.guess_date_or_time(value)
133
- (value.hour == 0 and value.min == 0 and value.sec == 0) ?
134
- Date.new(value.year, value.month, value.day) : value
135
- end
136
- end
137
-
138
- def adapter_name #:nodoc:
139
- 'SQLite'
140
- end
141
-
142
- def supports_count_distinct? #:nodoc:
143
- sqlite_version >= '3.2.6'
144
- end
145
-
146
- def supports_autoincrement? #:nodoc:
147
- sqlite_version >= '3.1.0'
148
- end
149
-
150
- def sqlite_version
151
- @sqlite_version ||= select_value('select sqlite_version(*)')
152
- end
153
-
154
- def modify_types(tp)
155
- tp[:primary_key] = "INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL"
156
- tp[:string] = { :name => "VARCHAR", :limit => 255 }
157
- tp[:float] = { :name => "REAL" }
158
- tp[:decimal] = { :name => "REAL" }
159
- tp[:datetime] = { :name => "DATETIME" }
160
- tp[:timestamp] = { :name => "DATETIME" }
161
- tp[:time] = { :name => "TIME" }
162
- tp[:date] = { :name => "DATE" }
163
- tp[:boolean] = { :name => "BOOLEAN" }
164
- tp
165
- end
166
-
167
- def quote(value, column = nil) # :nodoc:
168
- return value.quoted_id if value.respond_to?(:quoted_id)
169
- case value
170
- when String
171
- if column && column.type == :binary
172
- "'#{quote_string(column.class.string_to_binary(value))}'"
173
- elsif column && [:integer, :float].include?(column.type)
174
- (column.type == :integer ? value.to_i : value.to_f).to_s
175
- elsif column && column.respond_to?(:primary) && column.primary
176
- value.to_i.to_s
177
- else
178
- "'#{quote_string(value)}'"
179
- end
180
- else super
181
- end
182
- end
183
-
184
- def quote_column_name(name) #:nodoc:
185
- name = name.to_s
186
- # Did not find reference on values needing quoting, but these
187
- # happen in AR unit tests
188
- if name == "references" || name =~ /-/
189
- %Q("#{name}")
190
- else
191
- name
192
- end
193
- end
194
-
195
- def quote_string(str)
196
- str.gsub(/'/, "''")
197
- end
198
-
199
- def quoted_true
200
- %Q{'t'}
201
- end
202
-
203
- def quoted_false
204
- %Q{'f'}
205
- end
206
-
207
- def add_column(table_name, column_name, type, options = {})
208
- if option_not_null = options[:null] == false
209
- option_not_null = options.delete(:null)
210
- end
211
- add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
212
- add_column_options!(add_column_sql, options)
213
- execute(add_column_sql)
214
- if option_not_null
215
- alter_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} NOT NULL"
216
- end
217
- end
218
-
219
- def remove_column(table_name, *column_names) #:nodoc:
220
- column_names.flatten.each do |column_name|
221
- alter_table(table_name) do |definition|
222
- definition.columns.delete(definition[column_name])
223
- end
224
- end
225
- end
226
- alias :remove_columns :remove_column
227
-
228
- def change_column(table_name, column_name, type, options = {}) #:nodoc:
229
- alter_table(table_name) do |definition|
230
- include_default = options_include_default?(options)
231
- definition[column_name].instance_eval do
232
- self.type = type
233
- self.limit = options[:limit] if options.include?(:limit)
234
- self.default = options[:default] if include_default
235
- self.null = options[:null] if options.include?(:null)
236
- end
237
- end
238
- end
239
-
240
- def change_column_default(table_name, column_name, default) #:nodoc:
241
- alter_table(table_name) do |definition|
242
- definition[column_name].default = default
243
- end
244
- end
245
-
246
- def rename_column(table_name, column_name, new_column_name) #:nodoc:
247
- unless columns(table_name).detect{|c| c.name == column_name.to_s }
248
- raise ActiveRecord::ActiveRecordError, "Missing column #{table_name}.#{column_name}"
249
- end
250
- alter_table(table_name, :rename => {column_name.to_s => new_column_name.to_s})
251
- end
252
-
253
- def rename_table(name, new_name)
254
- execute "ALTER TABLE #{name} RENAME TO #{new_name}"
255
- end
256
-
257
- def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
258
- log(sql,name) do
259
- @connection.execute_update(sql)
260
- clear_query_cache
261
- end
262
- table = sql.split(" ", 4)[2]
263
- id_value || last_insert_id(table, nil)
264
- end
265
-
266
- def last_insert_id(table, sequence_name)
267
- Integer(select_value("SELECT SEQ FROM SQLITE_SEQUENCE WHERE NAME = '#{table}'"))
268
- end
269
-
270
- def add_limit_offset!(sql, options) #:nodoc:
271
- if options[:limit]
272
- sql << " LIMIT #{options[:limit]}"
273
- sql << " OFFSET #{options[:offset]}" if options[:offset]
274
- end
275
- end
276
-
277
- def tables(name = nil) #:nodoc:
278
- sql = <<-SQL
279
- SELECT name
280
- FROM sqlite_master
281
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
282
- SQL
283
-
284
- select_rows(sql, name).map do |row|
285
- row[0]
286
- end
287
- end
288
-
289
- def remove_index(table_name, options = {})
290
- execute "DROP INDEX #{quote_column_name(index_name(table_name, options))}"
291
- end
292
-
293
- def indexes(table_name, name = nil)
294
- result = select_rows("SELECT name, sql FROM sqlite_master WHERE tbl_name = '#{table_name}' AND type = 'index'", name)
295
-
296
- result.collect do |row|
297
- name = row[0]
298
- index_sql = row[1]
299
- unique = (index_sql =~ /unique/i)
300
- cols = index_sql.match(/\((.*)\)/)[1].gsub(/,/,' ').split
301
- ::ActiveRecord::ConnectionAdapters::IndexDefinition.new(table_name, name, unique, cols)
302
- end
303
- end
304
-
305
- def primary_key(table_name) #:nodoc:
306
- column = table_structure(table_name).find {|field| field['pk'].to_i == 1}
307
- column ? column['name'] : nil
308
- end
309
-
310
- def recreate_database(name)
311
- tables.each{ |table| drop_table(table) }
312
- end
313
-
314
- def _execute(sql, name = nil)
315
- if ActiveRecord::ConnectionAdapters::JdbcConnection::select?(sql)
316
- @connection.execute_query(sql)
317
- else
318
- affected_rows = @connection.execute_update(sql)
319
- ActiveRecord::ConnectionAdapters::JdbcConnection::insert?(sql) ? last_insert_id(sql.split(" ", 4)[2], nil) : affected_rows
320
- end
321
- end
322
-
323
- def select(sql, name=nil)
324
- execute(sql, name).map do |row|
325
- record = {}
326
- row.each_key do |key|
327
- if key.is_a?(String)
328
- record[key.sub(/^"?\w+"?\./, '')] = row[key]
329
- end
330
- end
331
- record
332
- end
333
- end
334
-
335
- def table_structure(table_name)
336
- returning structure = @connection.execute_query("PRAGMA table_info(#{quote_table_name(table_name)})") do
337
- raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if structure.empty?
338
- end
339
- end
340
-
341
- def columns(table_name, name = nil) #:nodoc:
342
- table_structure(table_name).map do |field|
343
- ::ActiveRecord::ConnectionAdapters::JdbcColumn.new(@config, field['name'], field['dflt_value'], field['type'], field['notnull'] == 0)
344
- end
345
- end
346
-
347
- # SELECT ... FOR UPDATE is redundant since the table is locked.
348
- def add_lock!(sql, options) #:nodoc:
349
- sql
350
- end
351
-
352
- protected
353
- include JdbcSpec::MissingFunctionalityHelper
354
- end
355
- end
356
-
357
- module ActiveRecord
358
- module ConnectionAdapters
359
- class JdbcColumn < Column
360
- def self.string_to_binary(value)
361
- value.gsub(/\0|%/n) do |b|
362
- case b
363
- when "\0" then "%00"
364
- when "\%" then "%25"
365
- end
366
- end
367
- end
368
-
369
- def self.binary_to_string(value)
370
- value.gsub(/%00|%25/n) do |b|
371
- case b
372
- when "%00" then "\0"
373
- when "%25" then "%"
374
- end
375
- end
376
- end
377
- end
378
- end
379
- end