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
@@ -1,5 +1,6 @@
1
1
  /***** BEGIN LICENSE BLOCK *****
2
- * Copyright (c) 2006-2007, 2010 Nick Sieger <nick@nicksieger.com>
2
+ * Copyright (c) 2012-2013 Karol Bucek <self@kares.org>
3
+ * Copyright (c) 2006-2011 Nick Sieger <nick@nicksieger.com>
3
4
  * Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
4
5
  *
5
6
  * Permission is hereby granted, free of charge, to any person obtaining
@@ -24,20 +25,12 @@
24
25
 
25
26
  package arjdbc.derby;
26
27
 
27
- import java.sql.SQLException;
28
-
29
- import arjdbc.jdbc.RubyJdbcConnection;
28
+ import static arjdbc.util.QuotingUtils.BYTES_0;
29
+ import static arjdbc.util.QuotingUtils.BYTES_1;
30
+ import static arjdbc.util.QuotingUtils.BYTES_SINGLE_Q_x2;
30
31
 
31
32
  import org.jruby.Ruby;
32
- import org.jruby.RubyBigDecimal;
33
- import org.jruby.RubyBignum;
34
- import org.jruby.RubyBoolean;
35
- import org.jruby.RubyFixnum;
36
- import org.jruby.RubyFloat;
37
33
  import org.jruby.RubyModule;
38
- import org.jruby.RubyNumeric;
39
- import org.jruby.RubyObjectAdapter;
40
- import org.jruby.RubyRange;
41
34
  import org.jruby.RubyString;
42
35
  import org.jruby.anno.JRubyMethod;
43
36
  import org.jruby.runtime.ThreadContext;
@@ -45,278 +38,141 @@ import org.jruby.runtime.builtin.IRubyObject;
45
38
  import org.jruby.util.ByteList;
46
39
 
47
40
  public class DerbyModule {
48
- private static RubyObjectAdapter rubyApi;
49
- public static void load(RubyModule arJdbc, RubyObjectAdapter adapter) {
41
+
42
+ public static RubyModule load(final RubyModule arJdbc) {
50
43
  RubyModule derby = arJdbc.defineModuleUnder("Derby");
51
- derby.defineAnnotatedMethods(DerbyModule.class);
44
+ derby.defineAnnotatedMethods( DerbyModule.class );
52
45
  RubyModule column = derby.defineModuleUnder("Column");
53
46
  column.defineAnnotatedMethods(Column.class);
54
- rubyApi = adapter;
47
+ return derby;
48
+ }
49
+
50
+ public static RubyModule load(final Ruby runtime) {
51
+ return load( arjdbc.ArJdbcModule.get(runtime) );
55
52
  }
56
53
 
57
54
  public static class Column {
55
+
58
56
  @JRubyMethod(name = "type_cast", required = 1)
59
- public static IRubyObject type_cast(IRubyObject recv, IRubyObject value) {
60
- Ruby runtime = recv.getRuntime();
57
+ public static IRubyObject type_cast(final ThreadContext context,
58
+ final IRubyObject self, final IRubyObject value) {
61
59
 
62
- if (value.isNil() || ((value instanceof RubyString) && value.toString().trim().equalsIgnoreCase("null"))) {
63
- return runtime.getNil();
60
+ if ( value.isNil() ||
61
+ ( (value instanceof RubyString) && value.toString().trim().equalsIgnoreCase("null") ) ) {
62
+ return context.nil;
64
63
  }
65
64
 
66
- String type = rubyApi.getInstanceVariable(recv, "@type").toString();
65
+ final String type = self.getInstanceVariables().getInstanceVariable("@type").toString();
67
66
 
68
67
  switch (type.charAt(0)) {
69
- case 's': //string
70
- return value;
71
- case 't': //text, timestamp, time
72
- if (type.equals("text")) {
73
- return value;
74
- } else if (type.equals("timestamp")) {
75
- return rubyApi.callMethod(recv.getMetaClass(), "string_to_time", value);
76
- } else { //time
77
- return rubyApi.callMethod(recv.getMetaClass(), "string_to_dummy_time", value);
78
- }
79
- case 'i': //integer
80
- case 'p': //primary key
81
- if (value.respondsTo("to_i")) {
82
- return rubyApi.callMethod(value, "to_i");
83
- } else {
84
- return runtime.newFixnum(value.isTrue() ? 1 : 0);
85
- }
86
- case 'd': //decimal, datetime, date
87
- if (type.equals("datetime")) {
88
- return rubyApi.callMethod(recv.getMetaClass(), "string_to_time", value);
89
- } else if (type.equals("date")) {
90
- return rubyApi.callMethod(recv.getMetaClass(), "string_to_date", value);
91
- } else {
92
- return rubyApi.callMethod(recv.getMetaClass(), "value_to_decimal", value);
93
- }
94
- case 'f': //float
95
- return rubyApi.callMethod(value, "to_f");
96
- case 'b': //binary, boolean
97
- if (type.equals("binary")) {
98
- return rubyApi.callMethod(recv.getMetaClass(), "binary_to_string", value);
99
- } else {
100
- return rubyApi.callMethod(recv.getMetaClass(), "value_to_boolean", value);
101
- }
102
- }
103
- return value;
104
- }
105
- }
106
-
107
- @JRubyMethod(name = "quote", required = 1, optional = 1)
108
- public static IRubyObject quote(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
109
- Ruby runtime = recv.getRuntime();
110
- IRubyObject value = args[0];
111
- if (args.length > 1) {
112
- IRubyObject col = args[1];
113
- String type = rubyApi.callMethod(col, "type").toString();
114
- // intercept and change value, maybe, if the column type is :text or :string
115
- if (type.equals("text") || type.equals("string")) {
116
- value = make_ruby_string_for_text_column(context, recv, runtime, value);
117
- }
118
- if (value instanceof RubyString) {
119
- if (type.equals("string")) {
120
- return quote_string_with_surround(runtime, "'", (RubyString)value, "'");
121
- } else if (type.equals("text")) {
122
- return quote_string_with_surround(runtime, "CAST('", (RubyString)value, "' AS CLOB)");
123
- } else if (type.equals("binary")) {
124
- return hexquote_string_with_surround(runtime, "CAST(X'", (RubyString)value, "' AS BLOB)");
125
- } else {
126
- // column type :integer or other numeric or date version
127
- if (only_digits((RubyString)value)) {
128
- return value;
129
- } else {
130
- return super_quote(context, recv, runtime, value, col);
131
- }
68
+ case 's': //string
69
+ return value;
70
+ case 't': //text, timestamp, time
71
+ if ( type.equals("time") ) {
72
+ return self.getMetaClass().callMethod(context, "string_to_dummy_time", value);
132
73
  }
133
- } else if ((value instanceof RubyFloat) || (value instanceof RubyFixnum) || (value instanceof RubyBignum)) {
134
- if (type.equals("string")) {
135
- return quote_string_with_surround(runtime, "'", RubyString.objAsString(context, value), "'");
74
+ if ( type.equals("timestamp") ) {
75
+ return self.getMetaClass().callMethod(context, "string_to_time", value);
136
76
  }
77
+ return value; // text
78
+ case 'i': //integer
79
+ case 'p': //primary key
80
+ if ( value.respondsTo("to_i") ) {
81
+ return value.callMethod(context, "to_i");
82
+ }
83
+ return context.getRuntime().newFixnum( value.isTrue() ? 1 : 0 );
84
+ case 'd': //decimal, datetime, date
85
+ if ( type.equals("datetime") ) {
86
+ return self.getMetaClass().callMethod(context, "string_to_time", value);
87
+ }
88
+ if ( type.equals("date") ) {
89
+ return self.getMetaClass().callMethod(context, "string_to_date", value);
90
+ }
91
+ return self.getMetaClass().callMethod(context, "value_to_decimal", value);
92
+ case 'f': //float
93
+ return value.callMethod(context, "to_f");
94
+ case 'b': //binary, boolean
95
+ return type.equals("binary") ?
96
+ self.getMetaClass().callMethod(context, "binary_to_string", value) :
97
+ self.getMetaClass().callMethod(context, "value_to_boolean", value) ;
137
98
  }
138
- }
139
- return super_quote(context, recv, runtime, value, runtime.getNil());
140
- }
141
-
142
- /*
143
- * Derby is not permissive like MySql. Try and send an Integer to a CLOB or VARCHAR column and Derby will vomit.
144
- * This method turns non stringy things into strings.
145
- */
146
- private static IRubyObject make_ruby_string_for_text_column(ThreadContext context, IRubyObject recv, Ruby runtime, IRubyObject value) {
147
- RubyModule multibyteChars = (RubyModule)
148
- ((RubyModule) ((RubyModule) runtime.getModule("ActiveSupport")).getConstant("Multibyte")).getConstantAt("Chars");
149
- if (value instanceof RubyString || rubyApi.isKindOf(value, multibyteChars) || value.isNil()) {
150
- return value;
151
- }
152
- if (value instanceof RubyBoolean) {
153
- return value.isTrue() ? runtime.newString("1") : runtime.newString("0");
154
- } else if (value instanceof RubyFloat || value instanceof RubyFixnum || value instanceof RubyBignum) {
155
- return RubyString.objAsString(context, value);
156
- } else if ( value instanceof RubyBigDecimal) {
157
- return rubyApi.callMethod(value, "to_s", runtime.newString("F"));
158
- } else {
159
- if (rubyApi.callMethod(value, "acts_like?", runtime.newString("date")).isTrue() || rubyApi.callMethod(value, "acts_like?", runtime.newString("time")).isTrue()) {
160
- return (RubyString)rubyApi.callMethod(recv, "quoted_date", value);
161
- } else {
162
- return (RubyString)rubyApi.callMethod(value, "to_yaml");
163
- }
164
- }
165
- }
166
-
167
- private final static ByteList NULL = new ByteList("NULL".getBytes());
168
-
169
- private static IRubyObject super_quote(ThreadContext context, IRubyObject recv, Ruby runtime, IRubyObject value, IRubyObject col) {
170
- if (value.respondsTo("quoted_id")) {
171
- return rubyApi.callMethod(value, "quoted_id");
99
+ return value;
172
100
  }
173
101
 
174
- IRubyObject type = (col.isNil()) ? col : rubyApi.callMethod(col, "type");
175
- RubyModule multibyteChars = (RubyModule)
176
- ((RubyModule) ((RubyModule) runtime.getModule("ActiveSupport")).getConstant("Multibyte")).getConstantAt("Chars");
177
- if (value instanceof RubyString || rubyApi.isKindOf(value, multibyteChars)) {
178
- RubyString svalue = RubyString.objAsString(context, value);
179
- if (type == runtime.newSymbol("binary") && col.getType().respondsTo("string_to_binary")) {
180
- return quote_string_with_surround(runtime, "'", (RubyString)(rubyApi.callMethod(col.getType(), "string_to_binary", svalue)), "'");
181
- } else if (type == runtime.newSymbol("integer") || type == runtime.newSymbol("float")) {
182
- return RubyString.objAsString(context, ((type == runtime.newSymbol("integer")) ?
183
- rubyApi.callMethod(svalue, "to_i") :
184
- rubyApi.callMethod(svalue, "to_f")));
185
- } else {
186
- return quote_string_with_surround(runtime, "'", svalue, "'");
187
- }
188
- } else if (value.isNil()) {
189
- return runtime.newString(NULL);
190
- } else if (value instanceof RubyBoolean) {
191
- return (value.isTrue() ?
192
- (type == runtime.newSymbol(":integer")) ? runtime.newString("1") : rubyApi.callMethod(recv, "quoted_true") :
193
- (type == runtime.newSymbol(":integer")) ? runtime.newString("0") : rubyApi.callMethod(recv, "quoted_false"));
194
- } else if((value instanceof RubyFloat) || (value instanceof RubyFixnum) || (value instanceof RubyBignum)) {
195
- return RubyString.objAsString(context, value);
196
- } else if(value instanceof RubyBigDecimal) {
197
- return rubyApi.callMethod(value, "to_s", runtime.newString("F"));
198
- } else if (rubyApi.callMethod(value, "acts_like?", runtime.newString("date")).isTrue() || rubyApi.callMethod(value, "acts_like?", runtime.newString("time")).isTrue()) {
199
- return quote_string_with_surround(runtime, "'", (RubyString)(rubyApi.callMethod(recv, "quoted_date", value)), "'");
200
- } else {
201
- return quote_string_with_surround(runtime, "'", (RubyString)(rubyApi.callMethod(value, "to_yaml")), "'");
202
- }
203
102
  }
204
103
 
205
- private final static ByteList TWO_SINGLE = new ByteList(new byte[]{'\'','\''});
104
+ @JRubyMethod(name = "quote_binary", required = 1)
105
+ public static IRubyObject quote_binary(final ThreadContext context,
106
+ final IRubyObject self, final IRubyObject string) {
107
+ final byte[] empty = ByteList.NULL_ARRAY; // ""
108
+ return quoteStringHex(context.runtime, empty, string, empty);
109
+ }
206
110
 
207
- private static IRubyObject quote_string_with_surround(Ruby runtime, String before, RubyString string, String after) {
208
- ByteList input = string.getByteList();
209
- ByteList output = new ByteList(before.getBytes());
210
- for(int i = input.begin; i< input.begin + input.realSize; i++) {
211
- switch(input.bytes[i]) {
212
- case '\'':
213
- output.append(input.bytes[i]);
214
- //FALLTHROUGH
215
- default:
216
- output.append(input.bytes[i]);
217
- }
111
+ private final static byte[] HEX = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
218
112
 
219
- }
113
+ private static RubyString quoteStringHex(final Ruby runtime,
114
+ final byte[] before, final IRubyObject string, final byte[] after) {
220
115
 
221
- output.append(after.getBytes());
116
+ final ByteList input = ((RubyString) string).getByteList();
117
+ final int size = input.getRealSize();
118
+ final ByteList output = new ByteList( before.length + size * 2 + after.length );
119
+ output.append( before );
222
120
 
223
- return runtime.newString(output);
224
- }
121
+ final byte[] inputBytes = input.unsafeBytes();
225
122
 
226
- private final static byte[] HEX = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
227
-
228
- private static IRubyObject hexquote_string_with_surround(Ruby runtime, String before, RubyString string, String after) {
229
- ByteList input = string.getByteList();
230
- ByteList output = new ByteList(before.getBytes());
231
123
  int written = 0;
232
- for(int i = input.begin; i< input.begin + input.realSize; i++) {
233
- byte b1 = input.bytes[i];
234
- byte higher = HEX[(((char)b1)>>4)%16];
235
- byte lower = HEX[((char)b1)%16];
236
- output.append(higher);
237
- output.append(lower);
124
+ for (int i = input.getBegin(); i < input.getBegin() + size; i++) {
125
+ final byte b = inputBytes[i];
126
+ byte h = HEX[ ( ((char) b) >> 4 ) % 16 ];
127
+ byte l = HEX[ ( (char) b ) % 16 ];
128
+ output.append(h).append(l);
238
129
  written += 2;
239
- if(written >= 16334) { // max hex length = 16334
240
- output.append("'||X'".getBytes());
241
- written = 0;
130
+ if ( written >= 16334 ) { // max hex length = 16334
131
+ output.append("'||X'".getBytes());
132
+ written = 0;
242
133
  }
243
134
  }
244
135
 
245
- output.append(after.getBytes());
246
- return runtime.newStringShared(output);
136
+ output.append( after );
137
+ return RubyString.newString(runtime, output);
247
138
  }
248
139
 
249
- private static boolean only_digits(RubyString inp) {
250
- ByteList input = inp.getByteList();
251
- for(int i = input.begin; i< input.begin + input.realSize; i++) {
252
- if(input.bytes[i] < '0' || input.bytes[i] > '9') {
253
- return false;
254
- }
140
+ @JRubyMethod(name = "quote_string", required = 1)
141
+ public static IRubyObject quote_string(final IRubyObject self, IRubyObject string) {
142
+ if ( ! ( string instanceof RubyString ) ) {
143
+ string = string.asString(); // e.g. Multibyte::Chars
255
144
  }
256
- return true;
257
- }
258
145
 
259
- @JRubyMethod(name = "quote_string", required = 1)
260
- public static IRubyObject quote_string(IRubyObject recv, IRubyObject string) {
261
- boolean replacementFound = false;
262
- ByteList bl = ((RubyString) string).getByteList();
146
+ ByteList bytes = ((RubyString) string).getByteList();
263
147
 
264
- for(int i = bl.begin; i < bl.begin + bl.realSize; i++) {
265
- switch (bl.bytes[i]) {
266
- case '\'': break;
267
- default: continue;
148
+ boolean newBytes = false;
149
+ for ( int i = 0; i < bytes.length(); i++ ) {
150
+ switch ( bytes.get(i) ) {
151
+ case '\'': break;
152
+ default: continue;
268
153
  }
269
-
270
- // On first replacement allocate a different bytelist so we don't manip original
271
- if(!replacementFound) {
272
- i-= bl.begin;
273
- bl = new ByteList(bl);
274
- replacementFound = true;
154
+ // on first replacement allocate so we don't manip original
155
+ if ( ! newBytes ) {
156
+ bytes = new ByteList(bytes);
157
+ newBytes = true;
275
158
  }
276
-
277
- bl.replace(i, 1, TWO_SINGLE);
278
- i+=1;
159
+ bytes.replace(i, 1, BYTES_SINGLE_Q_x2);
160
+ i += 1;
279
161
  }
280
- if(replacementFound) {
281
- return recv.getRuntime().newStringShared(bl);
282
- } else {
283
- return string;
284
- }
285
- }
286
162
 
287
- @JRubyMethod(name = "select_all", rest = true)
288
- public static IRubyObject select_all(IRubyObject recv, IRubyObject[] args) {
289
- return rubyApi.callMethod(recv, "execute", args);
163
+ return newBytes ? RubyString.newString(self.getRuntime(), bytes) : string;
290
164
  }
291
165
 
292
- @JRubyMethod(name = "select_one", rest = true)
293
- public static IRubyObject select_one(IRubyObject recv, IRubyObject[] args) {
294
- IRubyObject limit = rubyApi.getInstanceVariable(recv, "@limit");
295
- if (limit == null || limit.isNil()) {
296
- rubyApi.setInstanceVariable(recv, "@limit", recv.getRuntime().newFixnum(1));
297
- }
298
- try {
299
- IRubyObject result = rubyApi.callMethod(recv, "execute", args);
300
- return rubyApi.callMethod(result, "first");
301
- } finally {
302
- rubyApi.setInstanceVariable(recv, "@limit", recv.getRuntime().getNil());
303
- }
166
+ @JRubyMethod(name = "quoted_true", required = 0, frame = false)
167
+ public static RubyString quoted_true(
168
+ final ThreadContext context, final IRubyObject self) {
169
+ return RubyString.newStringShared(context.runtime, BYTES_1);
304
170
  }
305
171
 
306
- @JRubyMethod(name = "_execute", required = 1, optional = 1)
307
- public static IRubyObject _execute(ThreadContext context, IRubyObject recv, IRubyObject[] args) throws SQLException, java.io.IOException {
308
- Ruby runtime = recv.getRuntime();
309
- RubyJdbcConnection conn = (RubyJdbcConnection) rubyApi.getInstanceVariable(recv, "@connection");
310
- String sql = args[0].toString().trim().toLowerCase();
311
- if (sql.charAt(0) == '(') {
312
- sql = sql.substring(1).trim();
313
- }
314
- if (sql.startsWith("insert")) {
315
- return conn.execute_insert(context, args[0]);
316
- } else if (sql.startsWith("select") || sql.startsWith("show") || sql.startsWith("values")) {
317
- return conn.execute_query(context, args[0]);
318
- } else {
319
- return conn.execute_update(context, args[0]);
320
- }
172
+ @JRubyMethod(name = "quoted_false", required = 0, frame = false)
173
+ public static RubyString quoted_false(
174
+ final ThreadContext context, final IRubyObject self) {
175
+ return RubyString.newStringShared(context.runtime, BYTES_0);
321
176
  }
177
+
322
178
  }
@@ -0,0 +1,152 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright 2013 Karol Bucek.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ */
24
+ package arjdbc.derby;
25
+
26
+ import arjdbc.jdbc.Callable;
27
+ import arjdbc.jdbc.DriverWrapper;
28
+ import arjdbc.jdbc.RubyJdbcConnection;
29
+
30
+ import java.sql.Connection;
31
+ import java.sql.PreparedStatement;
32
+ import java.sql.ResultSet;
33
+ import java.sql.SQLException;
34
+
35
+ import org.jruby.Ruby;
36
+ import org.jruby.RubyBoolean;
37
+ import org.jruby.RubyClass;
38
+ import org.jruby.RubyString;
39
+ import org.jruby.anno.JRubyMethod;
40
+ import org.jruby.exceptions.RaiseException;
41
+ import org.jruby.runtime.ObjectAllocator;
42
+ import org.jruby.runtime.ThreadContext;
43
+ import org.jruby.runtime.builtin.IRubyObject;
44
+ import org.jruby.util.ByteList;
45
+
46
+ /**
47
+ * ArJdbc::DerbyJdbcConnection
48
+ *
49
+ * @author kares
50
+ */
51
+ public class DerbyRubyJdbcConnection extends RubyJdbcConnection {
52
+ private static final long serialVersionUID = 4809475910953623325L;
53
+
54
+ public DerbyRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
55
+ super(runtime, metaClass);
56
+ }
57
+
58
+ public static RubyClass createDerbyJdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
59
+ final RubyClass clazz = getConnectionAdapters(runtime). // ActiveRecord::ConnectionAdapters
60
+ defineClassUnder("DerbyJdbcConnection", jdbcConnection, ALLOCATOR);
61
+ clazz.defineAnnotatedMethods(DerbyRubyJdbcConnection.class);
62
+ return clazz;
63
+ }
64
+
65
+ public static RubyClass load(final Ruby runtime) {
66
+ RubyClass jdbcConnection = getJdbcConnection(runtime);
67
+ return createDerbyJdbcConnectionClass(runtime, jdbcConnection);
68
+ }
69
+
70
+ protected static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
71
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
72
+ return new DerbyRubyJdbcConnection(runtime, klass);
73
+ }
74
+ };
75
+
76
+ @Override
77
+ protected DriverWrapper newDriverWrapper(final ThreadContext context, final String driver) {
78
+ DriverWrapper driverWrapper = super.newDriverWrapper(context, driver);
79
+
80
+ final java.sql.Driver jdbcDriver = driverWrapper.getDriverInstance();
81
+ if ( jdbcDriver.getClass().getName().startsWith("org.apache.derby.") ) {
82
+ final int major = jdbcDriver.getMajorVersion();
83
+ final int minor = jdbcDriver.getMinorVersion();
84
+ if ( major < 10 || ( major == 10 && minor < 5 ) ) {
85
+ final RubyClass errorClass = getConnectionNotEstablished(context.runtime);
86
+ throw new RaiseException(context.runtime, errorClass,
87
+ "adapter requires Derby >= 10.5 got: " + major + "." + minor + "", false);
88
+ }
89
+ if ( major == 10 && minor < 8 ) { // 10.8 ~ supports JDBC 4.1
90
+ // config[:connection_alive_sql] ||= 'SELECT 1 FROM SYS.SYSSCHEMAS FETCH FIRST 1 ROWS ONLY'
91
+ setConfigValueIfNotSet(context, "connection_alive_sql", // FROM clause mandatory
92
+ context.runtime.newString("SELECT 1 FROM SYS.SYSSCHEMAS FETCH FIRST 1 ROWS ONLY"));
93
+ }
94
+ }
95
+
96
+ return driverWrapper;
97
+ }
98
+
99
+ @JRubyMethod(name = "select?", required = 1, meta = true, frame = false)
100
+ public static RubyBoolean select_p(final ThreadContext context,
101
+ final IRubyObject self, final IRubyObject sql) {
102
+ final RubyString sqlStr = sql.asString();
103
+ if ( isValues(sqlStr) ) return context.runtime.getTrue();
104
+ return arjdbc.jdbc.RubyJdbcConnection.select_p(context, self, sqlStr);
105
+ }
106
+
107
+ // Derby supports 'stand-alone' VALUES expressions
108
+ private static final byte[] VALUES = new byte[]{ 'v','a','l','u','e','s' };
109
+
110
+ private static boolean isValues(final RubyString sql) {
111
+ final ByteList sqlBytes = sql.getByteList();
112
+ return startsWithIgnoreCase(sqlBytes, VALUES);
113
+ }
114
+
115
+ @JRubyMethod(name = {"identity_val_local", "last_insert_id"})
116
+ public IRubyObject identity_val_local(final ThreadContext context)
117
+ throws SQLException {
118
+ return withConnection(context, new Callable<IRubyObject>() {
119
+ public IRubyObject call(final Connection connection) throws SQLException {
120
+ PreparedStatement statement = null; ResultSet genKeys = null;
121
+ try {
122
+ statement = connection.prepareStatement("values IDENTITY_VAL_LOCAL()");
123
+ genKeys = statement.executeQuery();
124
+ return doMapGeneratedKeys(context.getRuntime(), genKeys, true);
125
+ }
126
+ catch (final SQLException e) {
127
+ debugMessage(context.runtime, "failed to get generated keys: ", e);
128
+ throw e;
129
+ }
130
+ finally { close(genKeys); close(statement); }
131
+ }
132
+ });
133
+ }
134
+
135
+ @Override
136
+ protected boolean supportsGeneratedKeys(final Connection connection) throws SQLException {
137
+ return true; // driver reports false from supportsGetGeneratedKeys()
138
+ }
139
+
140
+ @Override
141
+ protected IRubyObject matchTables(final ThreadContext context,
142
+ final Connection connection,
143
+ final String catalog, String schemaPattern,
144
+ final String tablePattern, final String[] types,
145
+ final boolean checkExistsOnly) throws SQLException {
146
+ if (schemaPattern != null && schemaPattern.equals("")) {
147
+ schemaPattern = null; // Derby doesn't like empty-string schema name
148
+ }
149
+ return super.matchTables(context, connection, catalog, schemaPattern, tablePattern, types, checkExistsOnly);
150
+ }
151
+
152
+ }