activerecord-jdbc-adapter 1.0.3-java → 50.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (268) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +33 -0
  3. data/.travis.yml +79 -0
  4. data/.yardopts +4 -0
  5. data/CONTRIBUTING.md +50 -0
  6. data/Gemfile +91 -0
  7. data/History.md +1191 -0
  8. data/LICENSE.txt +22 -17
  9. data/README.md +169 -0
  10. data/RUNNING_TESTS.md +127 -0
  11. data/Rakefile +294 -5
  12. data/Rakefile.jdbc +20 -0
  13. data/activerecord-jdbc-adapter.gemspec +55 -0
  14. data/lib/active_record/connection_adapters/as400_adapter.rb +2 -0
  15. data/lib/active_record/connection_adapters/db2_adapter.rb +1 -0
  16. data/lib/active_record/connection_adapters/firebird_adapter.rb +1 -0
  17. data/lib/active_record/connection_adapters/mariadb_adapter.rb +1 -0
  18. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +1 -0
  19. data/lib/activerecord-jdbc-adapter.rb +0 -5
  20. data/lib/arel/visitors/compat.rb +60 -0
  21. data/lib/arel/visitors/db2.rb +128 -6
  22. data/lib/arel/visitors/derby.rb +103 -10
  23. data/lib/arel/visitors/firebird.rb +79 -0
  24. data/lib/arel/visitors/h2.rb +25 -0
  25. data/lib/arel/visitors/hsqldb.rb +18 -10
  26. data/lib/arel/visitors/postgresql_jdbc.rb +6 -0
  27. data/lib/arel/visitors/sql_server.rb +225 -0
  28. data/lib/arel/visitors/sql_server/ng42.rb +293 -0
  29. data/lib/arjdbc.rb +11 -21
  30. data/lib/arjdbc/abstract/connection_management.rb +35 -0
  31. data/lib/arjdbc/abstract/core.rb +64 -0
  32. data/lib/arjdbc/abstract/database_statements.rb +64 -0
  33. data/lib/arjdbc/abstract/statement_cache.rb +58 -0
  34. data/lib/arjdbc/abstract/transaction_support.rb +86 -0
  35. data/lib/arjdbc/db2.rb +3 -1
  36. data/lib/arjdbc/db2/adapter.rb +630 -250
  37. data/lib/arjdbc/db2/as400.rb +130 -0
  38. data/lib/arjdbc/db2/column.rb +167 -0
  39. data/lib/arjdbc/db2/connection_methods.rb +44 -0
  40. data/lib/arjdbc/derby.rb +1 -5
  41. data/lib/arjdbc/derby/active_record_patch.rb +13 -0
  42. data/lib/arjdbc/derby/adapter.rb +409 -217
  43. data/lib/arjdbc/derby/connection_methods.rb +16 -14
  44. data/lib/arjdbc/derby/schema_creation.rb +15 -0
  45. data/lib/arjdbc/discover.rb +62 -50
  46. data/lib/arjdbc/firebird.rb +3 -1
  47. data/lib/arjdbc/firebird/adapter.rb +365 -62
  48. data/lib/arjdbc/firebird/connection_methods.rb +23 -0
  49. data/lib/arjdbc/h2.rb +2 -3
  50. data/lib/arjdbc/h2/adapter.rb +273 -6
  51. data/lib/arjdbc/h2/connection_methods.rb +23 -8
  52. data/lib/arjdbc/hsqldb.rb +2 -3
  53. data/lib/arjdbc/hsqldb/adapter.rb +204 -77
  54. data/lib/arjdbc/hsqldb/connection_methods.rb +24 -10
  55. data/lib/arjdbc/hsqldb/explain_support.rb +35 -0
  56. data/lib/arjdbc/hsqldb/schema_creation.rb +11 -0
  57. data/lib/arjdbc/informix.rb +4 -2
  58. data/lib/arjdbc/informix/adapter.rb +78 -54
  59. data/lib/arjdbc/informix/connection_methods.rb +8 -9
  60. data/lib/arjdbc/jdbc.rb +59 -2
  61. data/lib/arjdbc/jdbc/adapter.rb +356 -166
  62. data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
  63. data/lib/arjdbc/jdbc/adapter_require.rb +46 -0
  64. data/lib/arjdbc/jdbc/base_ext.rb +15 -0
  65. data/lib/arjdbc/jdbc/callbacks.rb +27 -18
  66. data/lib/arjdbc/jdbc/column.rb +79 -20
  67. data/lib/arjdbc/jdbc/connection.rb +5 -119
  68. data/lib/arjdbc/jdbc/connection_methods.rb +32 -4
  69. data/lib/arjdbc/jdbc/error.rb +65 -0
  70. data/lib/arjdbc/jdbc/extension.rb +41 -29
  71. data/lib/arjdbc/jdbc/java.rb +5 -6
  72. data/lib/arjdbc/jdbc/jdbc.rake +3 -126
  73. data/lib/arjdbc/jdbc/railtie.rb +2 -9
  74. data/lib/arjdbc/jdbc/rake_tasks.rb +3 -10
  75. data/lib/arjdbc/jdbc/serialized_attributes_helper.rb +3 -0
  76. data/lib/arjdbc/jdbc/type_cast.rb +166 -0
  77. data/lib/arjdbc/jdbc/type_converter.rb +35 -19
  78. data/lib/arjdbc/mssql.rb +6 -3
  79. data/lib/arjdbc/mssql/adapter.rb +630 -298
  80. data/lib/arjdbc/mssql/column.rb +200 -0
  81. data/lib/arjdbc/mssql/connection_methods.rb +66 -17
  82. data/lib/arjdbc/mssql/explain_support.rb +99 -0
  83. data/lib/arjdbc/mssql/limit_helpers.rb +189 -50
  84. data/lib/arjdbc/mssql/lock_methods.rb +77 -0
  85. data/lib/arjdbc/mssql/types.rb +343 -0
  86. data/lib/arjdbc/mssql/utils.rb +82 -0
  87. data/lib/arjdbc/mysql.rb +2 -3
  88. data/lib/arjdbc/mysql/adapter.rb +86 -356
  89. data/lib/arjdbc/mysql/connection_methods.rb +159 -23
  90. data/lib/arjdbc/oracle/adapter.rb +714 -263
  91. data/lib/arjdbc/postgresql.rb +2 -3
  92. data/lib/arjdbc/postgresql/_bc_time_cast_patch.rb +24 -0
  93. data/lib/arjdbc/postgresql/adapter.rb +570 -400
  94. data/lib/arjdbc/postgresql/base/array_decoder.rb +26 -0
  95. data/lib/arjdbc/postgresql/base/array_encoder.rb +25 -0
  96. data/lib/arjdbc/postgresql/base/array_parser.rb +95 -0
  97. data/lib/arjdbc/postgresql/base/pgconn.rb +11 -0
  98. data/lib/arjdbc/postgresql/column.rb +51 -0
  99. data/lib/arjdbc/postgresql/connection_methods.rb +57 -18
  100. data/lib/arjdbc/postgresql/name.rb +24 -0
  101. data/lib/arjdbc/postgresql/oid_types.rb +192 -0
  102. data/lib/arjdbc/railtie.rb +11 -0
  103. data/lib/arjdbc/sqlite3.rb +2 -3
  104. data/lib/arjdbc/sqlite3/adapter.rb +518 -198
  105. data/lib/arjdbc/sqlite3/connection_methods.rb +49 -24
  106. data/lib/arjdbc/sybase.rb +2 -2
  107. data/lib/arjdbc/sybase/adapter.rb +7 -6
  108. data/lib/arjdbc/tasks.rb +13 -0
  109. data/lib/arjdbc/tasks/database_tasks.rb +52 -0
  110. data/lib/arjdbc/tasks/databases.rake +91 -0
  111. data/lib/arjdbc/tasks/databases3.rake +215 -0
  112. data/lib/arjdbc/tasks/databases4.rake +39 -0
  113. data/lib/arjdbc/tasks/db2_database_tasks.rb +104 -0
  114. data/lib/arjdbc/tasks/derby_database_tasks.rb +95 -0
  115. data/lib/arjdbc/tasks/h2_database_tasks.rb +31 -0
  116. data/lib/arjdbc/tasks/hsqldb_database_tasks.rb +70 -0
  117. data/lib/arjdbc/tasks/jdbc_database_tasks.rb +169 -0
  118. data/lib/arjdbc/tasks/mssql_database_tasks.rb +46 -0
  119. data/lib/arjdbc/util/quoted_cache.rb +60 -0
  120. data/lib/arjdbc/util/serialized_attributes.rb +98 -0
  121. data/lib/arjdbc/util/table_copier.rb +110 -0
  122. data/lib/arjdbc/version.rb +1 -6
  123. data/lib/generators/jdbc/USAGE +9 -0
  124. data/lib/generators/jdbc/jdbc_generator.rb +8 -0
  125. data/lib/jdbc_adapter.rb +1 -1
  126. data/lib/jdbc_adapter/rake_tasks.rb +3 -2
  127. data/lib/jdbc_adapter/version.rb +2 -1
  128. data/pom.xml +114 -0
  129. data/rails_generators/jdbc_generator.rb +1 -1
  130. data/rails_generators/templates/config/initializers/jdbc.rb +8 -5
  131. data/rails_generators/templates/lib/tasks/jdbc.rake +7 -4
  132. data/rakelib/01-tomcat.rake +51 -0
  133. data/rakelib/02-test.rake +132 -0
  134. data/rakelib/bundler_ext.rb +11 -0
  135. data/rakelib/compile.rake +67 -22
  136. data/rakelib/db.rake +61 -0
  137. data/rakelib/rails.rake +204 -29
  138. data/src/java/arjdbc/ArJdbcModule.java +286 -0
  139. data/src/java/arjdbc/db2/DB2Module.java +76 -0
  140. data/src/java/arjdbc/db2/DB2RubyJdbcConnection.java +126 -0
  141. data/src/java/arjdbc/derby/DerbyModule.java +99 -243
  142. data/src/java/arjdbc/derby/DerbyRubyJdbcConnection.java +152 -0
  143. data/src/java/arjdbc/firebird/FirebirdRubyJdbcConnection.java +174 -0
  144. data/src/java/arjdbc/{jdbc/JdbcConnectionFactory.java → h2/H2Module.java} +20 -6
  145. data/src/java/arjdbc/h2/H2RubyJdbcConnection.java +27 -12
  146. data/src/java/arjdbc/hsqldb/HSQLDBModule.java +73 -0
  147. data/src/java/arjdbc/informix/InformixRubyJdbcConnection.java +7 -6
  148. data/src/java/arjdbc/jdbc/AdapterJavaService.java +7 -29
  149. data/src/java/arjdbc/jdbc/Callable.java +44 -0
  150. data/src/java/arjdbc/jdbc/ConnectionFactory.java +132 -0
  151. data/src/java/arjdbc/jdbc/DataSourceConnectionFactory.java +157 -0
  152. data/src/java/arjdbc/jdbc/DriverConnectionFactory.java +63 -0
  153. data/src/java/arjdbc/jdbc/DriverWrapper.java +119 -0
  154. data/src/java/arjdbc/jdbc/JdbcResult.java +130 -0
  155. data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +3622 -948
  156. data/src/java/arjdbc/mssql/MSSQLModule.java +90 -0
  157. data/src/java/arjdbc/mssql/MSSQLRubyJdbcConnection.java +181 -0
  158. data/src/java/arjdbc/mysql/MySQLModule.java +99 -81
  159. data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +294 -0
  160. data/src/java/arjdbc/oracle/OracleModule.java +80 -0
  161. data/src/java/arjdbc/oracle/OracleRubyJdbcConnection.java +387 -17
  162. data/src/java/arjdbc/postgresql/ByteaUtils.java +157 -0
  163. data/src/java/arjdbc/postgresql/PgResultSetMetaDataWrapper.java +23 -0
  164. data/src/java/arjdbc/postgresql/PostgreSQLModule.java +77 -0
  165. data/src/java/arjdbc/postgresql/PostgreSQLResult.java +184 -0
  166. data/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java +952 -0
  167. data/src/java/arjdbc/sqlite3/SQLite3Module.java +73 -0
  168. data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +525 -0
  169. data/src/java/arjdbc/util/CallResultSet.java +826 -0
  170. data/src/java/arjdbc/util/DateTimeUtils.java +580 -0
  171. data/src/java/arjdbc/util/ObjectSupport.java +65 -0
  172. data/src/java/arjdbc/util/QuotingUtils.java +138 -0
  173. data/src/java/arjdbc/util/StringCache.java +63 -0
  174. data/src/java/arjdbc/util/StringHelper.java +159 -0
  175. metadata +245 -268
  176. data/History.txt +0 -369
  177. data/Manifest.txt +0 -180
  178. data/README.txt +0 -181
  179. data/lib/active_record/connection_adapters/oracle_adapter.rb +0 -1
  180. data/lib/arel/engines/sql/compilers/db2_compiler.rb +0 -9
  181. data/lib/arel/engines/sql/compilers/derby_compiler.rb +0 -6
  182. data/lib/arel/engines/sql/compilers/h2_compiler.rb +0 -6
  183. data/lib/arel/engines/sql/compilers/hsqldb_compiler.rb +0 -15
  184. data/lib/arel/engines/sql/compilers/jdbc_compiler.rb +0 -6
  185. data/lib/arel/engines/sql/compilers/mssql_compiler.rb +0 -46
  186. data/lib/arel/visitors/mssql.rb +0 -44
  187. data/lib/arjdbc/jdbc/compatibility.rb +0 -51
  188. data/lib/arjdbc/jdbc/core_ext.rb +0 -24
  189. data/lib/arjdbc/jdbc/discover.rb +0 -18
  190. data/lib/arjdbc/jdbc/driver.rb +0 -44
  191. data/lib/arjdbc/jdbc/missing_functionality_helper.rb +0 -87
  192. data/lib/arjdbc/jdbc/quoted_primary_key.rb +0 -28
  193. data/lib/arjdbc/jdbc/require_driver.rb +0 -16
  194. data/lib/arjdbc/mimer.rb +0 -2
  195. data/lib/arjdbc/mimer/adapter.rb +0 -142
  196. data/lib/arjdbc/mssql/tsql_helper.rb +0 -61
  197. data/lib/arjdbc/oracle.rb +0 -3
  198. data/lib/arjdbc/oracle/connection_methods.rb +0 -11
  199. data/lib/pg.rb +0 -4
  200. data/rakelib/package.rake +0 -92
  201. data/rakelib/test.rake +0 -81
  202. data/src/java/arjdbc/jdbc/SQLBlock.java +0 -48
  203. data/src/java/arjdbc/mssql/MssqlRubyJdbcConnection.java +0 -127
  204. data/src/java/arjdbc/postgresql/PostgresqlRubyJdbcConnection.java +0 -57
  205. data/src/java/arjdbc/sqlite3/Sqlite3RubyJdbcConnection.java +0 -64
  206. data/test/abstract_db_create.rb +0 -117
  207. data/test/activerecord/connection_adapters/type_conversion_test.rb +0 -31
  208. data/test/activerecord/connections/native_jdbc_mysql/connection.rb +0 -25
  209. data/test/db/db2.rb +0 -11
  210. data/test/db/derby.rb +0 -12
  211. data/test/db/h2.rb +0 -11
  212. data/test/db/hsqldb.rb +0 -13
  213. data/test/db/informix.rb +0 -11
  214. data/test/db/jdbc.rb +0 -11
  215. data/test/db/jndi_config.rb +0 -40
  216. data/test/db/logger.rb +0 -3
  217. data/test/db/mssql.rb +0 -9
  218. data/test/db/mysql.rb +0 -10
  219. data/test/db/oracle.rb +0 -34
  220. data/test/db/postgres.rb +0 -9
  221. data/test/db/sqlite3.rb +0 -11
  222. data/test/db2_simple_test.rb +0 -66
  223. data/test/derby_migration_test.rb +0 -68
  224. data/test/derby_multibyte_test.rb +0 -12
  225. data/test/derby_simple_test.rb +0 -99
  226. data/test/generic_jdbc_connection_test.rb +0 -29
  227. data/test/h2_simple_test.rb +0 -41
  228. data/test/has_many_through.rb +0 -79
  229. data/test/helper.rb +0 -5
  230. data/test/hsqldb_simple_test.rb +0 -6
  231. data/test/informix_simple_test.rb +0 -48
  232. data/test/jdbc_common.rb +0 -25
  233. data/test/jndi_callbacks_test.rb +0 -40
  234. data/test/jndi_test.rb +0 -25
  235. data/test/manualTestDatabase.rb +0 -191
  236. data/test/models/add_not_null_column_to_table.rb +0 -12
  237. data/test/models/auto_id.rb +0 -18
  238. data/test/models/data_types.rb +0 -28
  239. data/test/models/entry.rb +0 -43
  240. data/test/models/mixed_case.rb +0 -25
  241. data/test/models/reserved_word.rb +0 -18
  242. data/test/models/string_id.rb +0 -18
  243. data/test/models/validates_uniqueness_of_string.rb +0 -19
  244. data/test/mssql_db_create_test.rb +0 -26
  245. data/test/mssql_identity_insert_test.rb +0 -19
  246. data/test/mssql_legacy_types_test.rb +0 -58
  247. data/test/mssql_limit_offset_test.rb +0 -136
  248. data/test/mssql_multibyte_test.rb +0 -18
  249. data/test/mssql_simple_test.rb +0 -55
  250. data/test/mysql_db_create_test.rb +0 -27
  251. data/test/mysql_info_test.rb +0 -113
  252. data/test/mysql_multibyte_test.rb +0 -10
  253. data/test/mysql_nonstandard_primary_key_test.rb +0 -42
  254. data/test/mysql_simple_test.rb +0 -49
  255. data/test/oracle_simple_test.rb +0 -18
  256. data/test/oracle_specific_test.rb +0 -83
  257. data/test/pick_rails_version.rb +0 -3
  258. data/test/postgres_db_create_test.rb +0 -32
  259. data/test/postgres_drop_db_test.rb +0 -16
  260. data/test/postgres_mixed_case_test.rb +0 -29
  261. data/test/postgres_nonseq_pkey_test.rb +0 -38
  262. data/test/postgres_reserved_test.rb +0 -22
  263. data/test/postgres_schema_search_path_test.rb +0 -44
  264. data/test/postgres_simple_test.rb +0 -51
  265. data/test/postgres_table_alias_length_test.rb +0 -15
  266. data/test/simple.rb +0 -546
  267. data/test/sqlite3_simple_test.rb +0 -233
  268. data/test/sybase_jtds_simple_test.rb +0 -28
@@ -0,0 +1,39 @@
1
+ module ActiveRecord::Tasks
2
+
3
+ DatabaseTasks.module_eval do
4
+
5
+ # patched to adapt jdbc configuration
6
+ def each_current_configuration(environment)
7
+ environments = [environment]
8
+ environments << 'test' if environment == 'development'
9
+
10
+ configurations = ActiveRecord::Base.configurations.values_at(*environments)
11
+ configurations.compact.each do |config|
12
+ yield adapt_jdbc_config(config) unless config['database'].blank?
13
+ end
14
+ end
15
+
16
+ # patched to adapt jdbc configuration
17
+ def each_local_configuration
18
+ ActiveRecord::Base.configurations.each_value do |config|
19
+ next unless config['database']
20
+
21
+ if local_database?(config)
22
+ yield adapt_jdbc_config(config)
23
+ else
24
+ $stderr.puts "This task only modifies local databases. #{config['database']} is on a remote host."
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ MySQLDatabaseTasks.class_eval do
32
+
33
+ def error_class
34
+ ActiveRecord::JDBCError
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,104 @@
1
+ require 'arjdbc/tasks/jdbc_database_tasks'
2
+
3
+ module ArJdbc
4
+ module Tasks
5
+ class DB2DatabaseTasks < JdbcDatabaseTasks
6
+
7
+ def create
8
+ raise "AR-JDBC adapter 'DB2' does not support create_database"
9
+ end
10
+
11
+ def purge
12
+ establish_connection(config)
13
+ connection.recreate_database
14
+ end
15
+
16
+ # NOTE: does not work correctly (on non AS400) due driver meta data issue
17
+ #
18
+ # also try db2move e.g. `db2move SAMPLE EXPORT -sn db2inst`
19
+ # - where SAMPLE is the database name
20
+ # - and -sn specified schema name
21
+ #
22
+
23
+ def structure_dump(filename)
24
+ establish_connection(config)
25
+ dump = File.open(filename, "w:utf-8")
26
+
27
+ schema_name = connection.schema.upcase if connection.schema
28
+ meta_data = connection.jdbc_connection.meta_data
29
+ tables_rs = meta_data.getTables(nil, schema_name, nil, ["TABLE"].to_java(:String))
30
+
31
+ have_scale = ArJdbc::DB2::HAVE_SCALE
32
+ have_precision = ArJdbc::DB2::HAVE_LIMIT + ArJdbc::DB2::HAVE_LIMIT
33
+
34
+ while tables_rs.next
35
+ table_name = tables_rs.getString('TABLE_NAME')
36
+ dump << "CREATE TABLE #{connection.quote_table_name(table_name)} (\n"
37
+
38
+ cols_rs = meta_data.getColumns(nil, schema_name, table_name, nil)
39
+ begin
40
+ first_col = true
41
+ while cols_rs.next
42
+ column_name = cols_rs.getString(4)
43
+ default = cols_rs.getString(13)
44
+ default = default.empty? ? "" : " DEFAULT #{default}" if default
45
+ type = cols_rs.getString(6)
46
+ precision, scale = cols_rs.getString(7), cols_rs.getString(9)
47
+ column_size = ""
48
+ if scale && have_scale.include?(type)
49
+ column_size = "(#{precision},#{scale})"
50
+ elsif precision && have_precision.include?(type)
51
+ column_size = "(#{precision})"
52
+ end
53
+ nulling = ( cols_rs.getString(18) == 'NO' ? " NOT NULL" : nil )
54
+ autoinc = ( cols_rs.getString(23) == 'YES' ? " GENERATED ALWAYS AS IDENTITY" : nil )
55
+
56
+ create_column = connection.quote_column_name(column_name)
57
+ create_column << " #{type}"
58
+ create_column << column_size
59
+ create_column << nulling.to_s
60
+ create_column << default.to_s
61
+ create_column << autoinc.to_s
62
+
63
+ create_column = first_col ? " #{create_column}" : ",\n #{create_column}"
64
+ dump << create_column
65
+
66
+ first_col = false
67
+ end
68
+ ensure
69
+ cols_rs.close
70
+ end
71
+
72
+ dump << "\n);\n\n"
73
+
74
+ pk_rs = meta_data.getPrimaryKeys(nil, schema_name, table_name)
75
+ primary_keys = {}
76
+ begin
77
+ while pk_rs.next
78
+ name = pk_rs.getString(6)
79
+ primary_keys[name] ||= []
80
+ primary_keys[name] << pk_rs.getString(4)
81
+ end
82
+ ensure
83
+ pk_rs.close
84
+ end
85
+ primary_keys.each do |constraint_name, cols|
86
+ dump << "ALTER TABLE #{connection.quote_table_name(table_name)}\n"
87
+ dump << " ADD CONSTRAINT #{constraint_name}\n"
88
+ dump << " PRIMARY KEY (#{cols.join(', ')});\n\n"
89
+ end
90
+ end
91
+
92
+ dump.close
93
+ end
94
+
95
+ def structure_load(filename)
96
+ establish_connection(config)
97
+ IO.read(filename).split(/;\n*/m).each do |ddl|
98
+ connection.execute ddl.sub(/;$/, '')
99
+ end
100
+ end
101
+
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,95 @@
1
+ require 'arjdbc/tasks/jdbc_database_tasks'
2
+
3
+ module ArJdbc
4
+ module Tasks
5
+ class DerbyDatabaseTasks < JdbcDatabaseTasks
6
+
7
+ def create
8
+ establish_connection(config)
9
+ ActiveRecord::Base.connection
10
+ end
11
+
12
+ def drop
13
+ db_dir = expand_path resolve_database(config, true)
14
+ if File.exist?(db_dir)
15
+ FileUtils.rm_r(db_dir)
16
+ FileUtils.rmdir(db_dir) rescue nil
17
+ end
18
+ end
19
+
20
+ SIZEABLE = %w( VARCHAR CLOB BLOB )
21
+
22
+ def structure_dump(filename)
23
+ establish_connection(config)
24
+ dump = File.open(filename, "w:utf-8")
25
+
26
+ meta_data = connection.jdbc_connection.meta_data
27
+ tables_rs = meta_data.getTables(nil, nil, nil, ["TABLE"].to_java(:String))
28
+
29
+ while tables_rs.next
30
+ table_name = tables_rs.getString('TABLE_NAME') # getString(3)
31
+ dump << "CREATE TABLE #{connection.quote_table_name(table_name)} (\n"
32
+
33
+ columns_rs = meta_data.getColumns(nil, nil, table_name, nil)
34
+ first_col = true
35
+ while columns_rs.next
36
+ column_name = columns_rs.getString(4)
37
+ default = columns_rs.getString(13)
38
+ if default =~ /^GENERATED_/
39
+ default = column_auto_increment_def(table_name, column_name)
40
+ elsif default
41
+ default = " DEFAULT #{default}"
42
+ end
43
+ type = columns_rs.getString(6)
44
+ column_size = columns_rs.getString(7)
45
+ nulling = ( columns_rs.getString(18) == 'NO' ? " NOT NULL" : nil )
46
+
47
+ create_column = connection.quote_column_name(column_name)
48
+ create_column << " #{type}"
49
+ create_column << ( SIZEABLE.include?(type) ? "(#{column_size})" : "" )
50
+ create_column << nulling.to_s
51
+ create_column << default.to_s
52
+
53
+ create_column = first_col ? " #{create_column}" : ",\n #{create_column}"
54
+ dump << create_column
55
+
56
+ first_col = false
57
+ end
58
+ dump << "\n);\n\n"
59
+ end
60
+
61
+ dump.close
62
+ end
63
+
64
+ def structure_load(filename)
65
+ establish_connection(config)
66
+ IO.read(filename).split(/;\n*/m).each { |ddl| connection.execute(ddl) }
67
+ end
68
+
69
+ private
70
+
71
+ AUTO_INCREMENT_SQL = '' <<
72
+ "SELECT AUTOINCREMENTSTART, AUTOINCREMENTINC, COLUMNNAME, REFERENCEID, COLUMNDEFAULT " <<
73
+ "FROM SYS.SYSCOLUMNS WHERE REFERENCEID = " <<
74
+ "(SELECT T.TABLEID FROM SYS.SYSTABLES T WHERE T.TABLENAME = '%s') AND COLUMNNAME = '%s'"
75
+
76
+ def column_auto_increment_def(table_name, column_name)
77
+ sql = AUTO_INCREMENT_SQL % [ table_name, column_name ]
78
+ if data = connection.execute(sql).first
79
+ if start = data['autoincrementstart']
80
+ ai_def = ' GENERATED '
81
+ ai_def << ( data['columndefault'].nil? ? "ALWAYS" : "BY DEFAULT " )
82
+ ai_def << "AS IDENTITY (START WITH "
83
+ ai_def << start.to_s
84
+ ai_def << ", INCREMENT BY "
85
+ ai_def << data['autoincrementinc'].to_s
86
+ ai_def << ")"
87
+ return ai_def
88
+ end
89
+ end
90
+ ''
91
+ end
92
+
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,31 @@
1
+ require 'arjdbc/tasks/hsqldb_database_tasks'
2
+
3
+ module ArJdbc
4
+ module Tasks
5
+ class H2DatabaseTasks < HSQLDBDatabaseTasks
6
+
7
+ protected
8
+
9
+ # @override
10
+ def do_drop_database(config)
11
+ # ActiveRecord::JDBCError: org.h2.jdbc.JdbcSQLException:
12
+ # Database is already closed (to disable automatic closing at VM
13
+ # shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-170]:
14
+ # SHUTDOWN COMPACT
15
+ #
16
+ # connection.shutdown
17
+ connection.drop_database resolve_database(config)
18
+ end
19
+
20
+ # @override
21
+ def delete_database_files(config)
22
+ return unless db_base = database_base_name(config)
23
+ for suffix in [ '.h2,db', '.mv.db', '.lock.db', '.trace.db' ]
24
+ db_file = "#{db_base}#{suffix}"
25
+ File.delete(db_file) if File.exist?(db_file)
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,70 @@
1
+ require 'arjdbc/tasks/jdbc_database_tasks'
2
+
3
+ module ArJdbc
4
+ module Tasks
5
+ class HSQLDBDatabaseTasks < JdbcDatabaseTasks
6
+
7
+ def create
8
+ establish_connection(config)
9
+ ActiveRecord::Base.connection
10
+ end
11
+
12
+ def drop
13
+ error = nil
14
+ begin
15
+ establish_connection(config)
16
+ do_drop_database(config)
17
+ rescue => e
18
+ error = e
19
+ raise error
20
+ ensure
21
+ begin
22
+ keep_db_files = ENV['KEEP_DB_FILES'] && ENV['KEEP_DB_FILES'] != 'false'
23
+ delete_database_files(config) unless keep_db_files
24
+ rescue => e
25
+ raise e unless error
26
+ end
27
+ end
28
+ end
29
+ alias :purge :drop
30
+
31
+ protected
32
+
33
+ def do_drop_database(config)
34
+ connection.drop_database resolve_database(config)
35
+ connection.shutdown
36
+ end
37
+
38
+ def delete_database_files(config)
39
+ return unless db_base = database_base_name(config)
40
+ Dir.glob("#{db_base}.*").each do |file|
41
+ # test.hsqldb.tmp (dir)
42
+ # test.hsqldb.lck
43
+ # test.hsqldb.lobs
44
+ # test.hsqldb.script
45
+ # test.hsqldb.properties
46
+ if File.directory?(file)
47
+ FileUtils.rm_r(file)
48
+ FileUtils.rmdir(file)
49
+ else
50
+ FileUtils.rm(file)
51
+ end
52
+ end
53
+ if File.exist?(db_base)
54
+ FileUtils.rm_r(db_base)
55
+ FileUtils.rmdir(db_base)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def database_base_name(config)
62
+ db = resolve_database(config, true)
63
+ db[0, 4] == 'mem:' ? nil : begin
64
+ expand_path db[0, 5] == 'file:' ? db[5..-1] : db
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,169 @@
1
+ module ArJdbc
2
+ module Tasks
3
+ # Sharing task related code between AR 3.x and 4.x
4
+ #
5
+ # @note this class needs to conform to the API available since AR 4.0
6
+ # mostly to be usable with ActiveRecord::Tasks::DatabaseTasks module
7
+ class JdbcDatabaseTasks
8
+
9
+ attr_reader :configuration
10
+ alias_method :config, :configuration
11
+
12
+ def initialize(configuration)
13
+ @configuration = configuration
14
+ end
15
+
16
+ delegate :connection, :establish_connection, :to => ActiveRecord::Base
17
+
18
+ def create
19
+ begin
20
+ establish_connection(config)
21
+ ActiveRecord::Base.connection
22
+ if defined? ActiveRecord::Tasks::DatabaseAlreadyExists
23
+ raise ActiveRecord::Tasks::DatabaseAlreadyExists # AR-4.x
24
+ end # silence on AR < 4.0
25
+ rescue #=> error # database does not exists :
26
+ url = config['url']
27
+ url = $1 if url && url =~ /^(.*(?<!\/)\/)(?=\w)/
28
+
29
+ establish_connection(config.merge('database' => nil, 'url' => url))
30
+
31
+ unless connection.respond_to?(:create_database)
32
+ raise "AR-JDBC adapter '#{adapter_with_spec}' does not support create_database"
33
+ end
34
+ connection.create_database(resolve_database(config), config)
35
+
36
+ establish_connection(config)
37
+ end
38
+ end
39
+
40
+ def drop
41
+ establish_connection(config)
42
+ unless ActiveRecord::Base.connection.respond_to?(:drop_database)
43
+ raise "AR-JDBC adapter '#{adapter_with_spec}' does not support drop_database"
44
+ end
45
+ connection.drop_database resolve_database(config)
46
+ end
47
+
48
+ def purge
49
+ establish_connection(config) # :test
50
+ unless ActiveRecord::Base.connection.respond_to?(:recreate_database)
51
+ raise "AR-JDBC adapter '#{adapter_with_spec}' does not support recreate_database (purge)"
52
+ end
53
+ db_name = ActiveRecord::Base.connection.database_name
54
+ ActiveRecord::Base.connection.recreate_database(db_name, config)
55
+ end
56
+
57
+ def charset
58
+ establish_connection(config)
59
+ if connection.respond_to?(:charset)
60
+ puts connection.charset
61
+ elsif connection.respond_to?(:encoding)
62
+ puts connection.encoding
63
+ else
64
+ raise "AR-JDBC adapter '#{adapter_with_spec}' does not support charset/encoding"
65
+ end
66
+ end
67
+
68
+ def collation
69
+ establish_connection(config)
70
+ if connection.respond_to?(:collation)
71
+ puts connection.collation
72
+ else
73
+ raise "AR-JDBC adapter '#{adapter_with_spec}' does not support collation"
74
+ end
75
+ end
76
+
77
+ def structure_dump(filename)
78
+ establish_connection(config)
79
+ if connection.respond_to?(:structure_dump)
80
+ File.open(filename, "w:utf-8") { |f| f << connection.structure_dump }
81
+ else
82
+ raise "AR-JDBC adapter '#{adapter_with_spec}' does not support structure_dump"
83
+ end
84
+ end
85
+
86
+ def structure_load(filename)
87
+ establish_connection(config)
88
+ if connection.respond_to?(:structure_load)
89
+ connection.structure_load IO.read(filename)
90
+ else
91
+ #IO.read(filename).split(/;\n*/m).each do |ddl|
92
+ # connection.execute(ddl)
93
+ #end
94
+ raise "AR-JDBC adapter '#{adapter_with_spec}' does not support structure_load"
95
+ end
96
+ end
97
+
98
+ protected
99
+
100
+ def expand_path(path)
101
+ require 'pathname'
102
+ path = Pathname.new path
103
+ return path.to_s if path.absolute?
104
+ rails_root ? File.join(rails_root, path) : File.expand_path(path)
105
+ end
106
+
107
+ def resolve_database(config, file_paths = false)
108
+ config['database'] || resolve_database_from_url(config['url'] || '', file_paths)
109
+ end
110
+
111
+ def resolve_database_from_url(url, file_paths = false)
112
+ ( config = config_from_url(url, file_paths) ) ? config['database'] : nil
113
+ end
114
+
115
+ private
116
+
117
+ def config_from_url(url, file_paths = false)
118
+ match = url.match %r{
119
+ ^ jdbc:
120
+ ( [\w]+ ): # $1 protocol
121
+ (?: ([\w]+) : )? # $2 (sub-protocol)
122
+ (?://)?
123
+ (?: ([\w\-]*) (?: [/:] ([\w\-]*) )? @ (?://)? )? # user[:password]@ or user[/password]@ ($3 $4)
124
+ ( [\w\.\-]+ )? # $5 host (or database if there's nothing left)
125
+ (?: : (\d+) )? # $6 port if any
126
+ (?: :? (/?[\w\-\./~]+) [\?;]? )? ([^/]*?) $
127
+ # $7 database part (ends with '?' or ';') and $8 query string - properties
128
+ }x
129
+
130
+ return nil unless match
131
+
132
+ config = {}
133
+ config['_protocol'] = match[1]
134
+ config['_sub_protocol'] = match[2] if match[2]
135
+ config['username'] = match[3] if match[3]
136
+ config['password'] = match[4] if match[4]
137
+ host = match[5]; port = match[6]
138
+ database = match[7]
139
+ if database.nil? && port.nil?
140
+ config['database'] = database = host
141
+ else
142
+ config['host'] = host if host
143
+ config['port'] = port if port
144
+ config['database'] = database
145
+ end
146
+ if database && ! file_paths && database[0...1] == '/'
147
+ config['database'] = database[1..-1]
148
+ end
149
+ if query_string = match[8]
150
+ properties = query_string.split('&').inject({}) do |memo, pair|
151
+ pair = pair.split("="); memo[ pair[0] ] = pair[1]; memo
152
+ end
153
+ config['properties'] = properties
154
+ end
155
+ config
156
+ end
157
+
158
+ def adapter_with_spec
159
+ adapter, spec = config['adapter'], config['adapter_spec']
160
+ spec ? "#{adapter} (#{spec})" : adapter
161
+ end
162
+
163
+ def rails_root
164
+ defined?(Rails.root) ? Rails.root : ( RAILS_ROOT )
165
+ end
166
+
167
+ end
168
+ end
169
+ end