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,294 @@
1
+ /***** BEGIN LICENSE BLOCK *****
2
+ * Copyright (c) 2012-2013 Karol Bucek <self@kares.org>
3
+ * Copyright (c) 2006-2010 Nick Sieger <nick@nicksieger.com>
4
+ * Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
5
+ * Copyright (c) 2008-2009 Thomas E Enebo <enebo@acm.org>
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining
8
+ * a copy of this software and associated documentation files (the
9
+ * "Software"), to deal in the Software without restriction, including
10
+ * without limitation the rights to use, copy, modify, merge, publish,
11
+ * distribute, sublicense, and/or sell copies of the Software, and to
12
+ * permit persons to whom the Software is furnished to do so, subject to
13
+ * the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ ***** END LICENSE BLOCK *****/
26
+ package arjdbc.mysql;
27
+
28
+ import arjdbc.jdbc.Callable;
29
+ import arjdbc.jdbc.DriverWrapper;
30
+ import arjdbc.jdbc.RubyJdbcConnection;
31
+ import arjdbc.util.DateTimeUtils;
32
+
33
+ import java.lang.reflect.InvocationTargetException;
34
+ import java.sql.Connection;
35
+ import java.sql.DatabaseMetaData;
36
+ import java.sql.PreparedStatement;
37
+ import java.sql.SQLException;
38
+ import java.sql.ResultSet;
39
+ import java.sql.Statement;
40
+ import java.sql.Timestamp;
41
+ import java.sql.Types;
42
+
43
+ import org.joda.time.DateTime;
44
+ import org.joda.time.DateTimeZone;
45
+ import org.jruby.*;
46
+ import org.jruby.anno.JRubyMethod;
47
+ import org.jruby.exceptions.RaiseException;
48
+ import org.jruby.runtime.ObjectAllocator;
49
+ import org.jruby.runtime.ThreadContext;
50
+ import org.jruby.runtime.builtin.IRubyObject;
51
+ import org.jruby.util.SafePropertyAccessor;
52
+
53
+ import static arjdbc.util.StringHelper.newString;
54
+
55
+ /**
56
+ *
57
+ * @author nicksieger
58
+ */
59
+ @org.jruby.anno.JRubyClass(name = "ActiveRecord::ConnectionAdapters::MySQLJdbcConnection")
60
+ public class MySQLRubyJdbcConnection extends RubyJdbcConnection {
61
+ private static final long serialVersionUID = -8842614212147138733L;
62
+
63
+ public MySQLRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
64
+ super(runtime, metaClass);
65
+ }
66
+
67
+ public static RubyClass createMySQLJdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
68
+ RubyClass clazz = getConnectionAdapters(runtime).
69
+ defineClassUnder("MySQLJdbcConnection", jdbcConnection, ALLOCATOR);
70
+ clazz.defineAnnotatedMethods(MySQLRubyJdbcConnection.class);
71
+ return clazz;
72
+ }
73
+
74
+ public static RubyClass load(final Ruby runtime) {
75
+ RubyClass jdbcConnection = getJdbcConnection(runtime);
76
+ return createMySQLJdbcConnectionClass(runtime, jdbcConnection);
77
+ }
78
+
79
+ protected static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
80
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
81
+ return new MySQLRubyJdbcConnection(runtime, klass);
82
+ }
83
+ };
84
+
85
+ @JRubyMethod
86
+ public IRubyObject query(final ThreadContext context, final IRubyObject sql) throws SQLException {
87
+ return execute_update(context, sql);
88
+ }
89
+
90
+ @JRubyMethod(name = { "full_version" })
91
+ public IRubyObject db_version(final ThreadContext context) {
92
+ return withConnection(context, new Callable<IRubyObject>() {
93
+ public IRubyObject call(final Connection connection) throws SQLException {
94
+ final DatabaseMetaData metaData = connection.getMetaData();
95
+ return context.runtime.newString(metaData.getDatabaseProductVersion());
96
+ }
97
+ });
98
+ }
99
+
100
+ @Override
101
+ protected DriverWrapper newDriverWrapper(final ThreadContext context, final String driver) {
102
+ DriverWrapper driverWrapper = super.newDriverWrapper(context, driver);
103
+
104
+ final java.sql.Driver jdbcDriver = driverWrapper.getDriverInstance();
105
+ if ( jdbcDriver.getClass().getName().startsWith("com.mysql.jdbc.") ) {
106
+ final int major = jdbcDriver.getMajorVersion();
107
+ final int minor = jdbcDriver.getMinorVersion();
108
+ if ( major < 5 ) {
109
+ final RubyClass errorClass = getConnectionNotEstablished(context.runtime);
110
+ throw new RaiseException(context.runtime, errorClass,
111
+ "MySQL adapter requires driver >= 5.0 got: " + major + "." + minor + "", false);
112
+ }
113
+ if ( major == 5 && minor < 1 ) { // need 5.1 for JDBC 4.0
114
+ // lightweight validation query: "/* ping */ SELECT 1"
115
+ setConfigValueIfNotSet(context, "connection_alive_sql", context.runtime.newString("/* ping */ SELECT 1"));
116
+ }
117
+ driverAdapter = new MySQLDriverAdapter(); // short-circuit
118
+ }
119
+ else {
120
+ driverAdapter = new DriverAdapter(); // short-circuit (MariaDB)
121
+ }
122
+
123
+ return driverWrapper;
124
+ }
125
+
126
+ private static transient Class MYSQL_CONNECTION;
127
+ private static transient Boolean MYSQL_CONNECTION_FOUND;
128
+
129
+ private static boolean checkMySQLConnection(final Connection connection) {
130
+ Class mysqlDriverIface = MYSQL_CONNECTION;
131
+ if (mysqlDriverIface == null && MYSQL_CONNECTION_FOUND == null) {
132
+ try {
133
+ MYSQL_CONNECTION = Class.forName("com.mysql.jdbc.MySQLConnection", false, MySQLRubyJdbcConnection.class.getClassLoader());
134
+ MYSQL_CONNECTION_FOUND = Boolean.TRUE;
135
+ }
136
+ catch (ClassNotFoundException ex) {
137
+ MYSQL_CONNECTION_FOUND = Boolean.FALSE;
138
+ }
139
+ mysqlDriverIface = MYSQL_CONNECTION;
140
+ }
141
+ if (mysqlDriverIface == null) return false;
142
+ try {
143
+ return connection.isWrapperFor(mysqlDriverIface);
144
+ }
145
+ catch (SQLException ex) {
146
+ return false;
147
+ }
148
+ }
149
+
150
+ private boolean usingMySQLDriver() {
151
+ return checkMySQLConnection(getConnection(true));
152
+ }
153
+
154
+ private transient DriverAdapter driverAdapter;
155
+
156
+ // NOTE: currently un-used but we'll need it if we attempt to handle fast string extraction
157
+ private DriverAdapter getDriverAdapter() {
158
+ if (driverAdapter == null) {
159
+ driverAdapter = usingMySQLDriver() ? new MySQLDriverAdapter() : new DriverAdapter();
160
+ }
161
+ return driverAdapter;
162
+ }
163
+
164
+ private class DriverAdapter { // sensible driver without quirks (MariaDB)
165
+ // left in for encoding specific extraction from driver - would allow us 'fast' string byte[] extraction
166
+ }
167
+
168
+ private class MySQLDriverAdapter extends DriverAdapter { // Connector/J (bloated) 5.x version
169
+ // left in for encoding specific extraction from driver - would allow us 'fast' string byte[] extraction
170
+ }
171
+
172
+ @Override
173
+ protected boolean doExecute(final Statement statement, final String query) throws SQLException {
174
+ return statement.execute(query, Statement.RETURN_GENERATED_KEYS);
175
+ }
176
+
177
+ @Override
178
+ protected IRubyObject jdbcToRuby(final ThreadContext context, final Ruby runtime,
179
+ final int column, final int type, final ResultSet resultSet) throws SQLException {
180
+ if ( type == Types.BIT ) {
181
+ final int value = resultSet.getInt(column);
182
+ return resultSet.wasNull() ? context.nil : runtime.newFixnum(value);
183
+ }
184
+ return super.jdbcToRuby(context, runtime, column, type, resultSet);
185
+ }
186
+
187
+ @Override
188
+ protected void setTimeParameter(final ThreadContext context,
189
+ final Connection connection, final PreparedStatement statement,
190
+ final int index, IRubyObject value,
191
+ final IRubyObject attribute, final int type) throws SQLException {
192
+ // MySQL's TIME supports fractional seconds up-to nano precision: .000000
193
+ setTimestampParameter(context, connection, statement, index, value, attribute, type);
194
+ }
195
+
196
+ @Override
197
+ protected IRubyObject timeToRuby(final ThreadContext context,
198
+ final Ruby runtime, final ResultSet resultSet, final int column)
199
+ throws SQLException { // due MySQL's TIME precision (up to nanos)
200
+
201
+ final Timestamp value = resultSet.getTimestamp(column);
202
+ if ( value == null ) {
203
+ return resultSet.wasNull() ? context.nil : RubyString.newEmptyString(runtime);
204
+ }
205
+
206
+ if ( rawDateTime != null && rawDateTime.booleanValue() ) {
207
+ return RubyString.newString(runtime, DateTimeUtils.dummyTimeToString(value));
208
+ }
209
+
210
+ return DateTimeUtils.newDummyTime(context, value, getDefaultTimeZone(context));
211
+ }
212
+
213
+ @Override
214
+ protected IRubyObject streamToRuby(final ThreadContext context,
215
+ final Ruby runtime, final ResultSet resultSet, final int column)
216
+ throws SQLException {
217
+ final byte[] bytes = resultSet.getBytes(column);
218
+ if ( bytes == null /* || resultSet.wasNull() */ ) return context.nil;
219
+ return newString(runtime, bytes);
220
+ }
221
+
222
+ // MySQL does never storesUpperCaseIdentifiers() :
223
+ // storesLowerCaseIdentifiers() depends on "lower_case_table_names" server variable
224
+
225
+ @Override
226
+ protected final String caseConvertIdentifierForRails(final Connection connection, final String value) {
227
+ return value; // MySQL does not storesUpperCaseIdentifiers() :
228
+ }
229
+
230
+ private transient Boolean lowerCaseIdentifiers;
231
+
232
+ @Override
233
+ protected final String caseConvertIdentifierForJdbc(
234
+ final Connection connection, final String value) throws SQLException {
235
+ if ( value == null ) return null;
236
+ Boolean lowerCase = lowerCaseIdentifiers;
237
+ if (lowerCase == null) {
238
+ lowerCase = lowerCaseIdentifiers = connection.getMetaData().storesLowerCaseIdentifiers();
239
+ }
240
+ return lowerCase.booleanValue() ? value.toLowerCase() : value;
241
+ }
242
+
243
+ @Override
244
+ protected Connection newConnection() throws RaiseException, SQLException {
245
+ final Connection connection;
246
+ try {
247
+ connection = super.newConnection();
248
+ }
249
+ catch (SQLException ex) {
250
+ if (ex.getErrorCode() == 1049) throw newNoDatabaseError(ex);
251
+ throw ex;
252
+ }
253
+ if ( doStopCleanupThread() ) shutdownCleanupThread();
254
+ return connection;
255
+ }
256
+
257
+ private static Boolean stopCleanupThread;
258
+ static {
259
+ final String stopThread = SafePropertyAccessor.getProperty("arjdbc.mysql.stop_cleanup_thread");
260
+ if ( stopThread != null ) stopCleanupThread = Boolean.parseBoolean(stopThread);
261
+ }
262
+
263
+ private static boolean doStopCleanupThread() throws SQLException {
264
+ return stopCleanupThread != null && stopCleanupThread.booleanValue();
265
+ }
266
+
267
+ private static boolean cleanupThreadShutdown;
268
+
269
+ @SuppressWarnings("unchecked")
270
+ private static void shutdownCleanupThread() {
271
+ if ( cleanupThreadShutdown ) return;
272
+ try {
273
+ Class threadClass = Class.forName("com.mysql.jdbc.AbandonedConnectionCleanupThread");
274
+ threadClass.getMethod("shutdown").invoke(null);
275
+ }
276
+ catch (ClassNotFoundException e) {
277
+ debugMessage(null, "missing MySQL JDBC cleanup thread ", e);
278
+ }
279
+ catch (NoSuchMethodException e) {
280
+ debugMessage(null, e);
281
+ }
282
+ catch (IllegalAccessException e) {
283
+ debugMessage(null, e);
284
+ }
285
+ catch (InvocationTargetException e) {
286
+ debugMessage(null, e.getTargetException());
287
+ }
288
+ catch (SecurityException e) {
289
+ debugMessage(null, e);
290
+ }
291
+ finally { cleanupThreadShutdown = true; }
292
+ }
293
+
294
+ }
@@ -0,0 +1,80 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright 2013-2014 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.oracle;
25
+
26
+ import static arjdbc.util.QuotingUtils.BYTES_0;
27
+ import static arjdbc.util.QuotingUtils.BYTES_1;
28
+ import static arjdbc.util.QuotingUtils.quoteCharWith;
29
+
30
+ import org.jruby.Ruby;
31
+ import org.jruby.RubyModule;
32
+ import org.jruby.RubyString;
33
+ import org.jruby.anno.JRubyMethod;
34
+ import org.jruby.runtime.ThreadContext;
35
+ import org.jruby.runtime.builtin.IRubyObject;
36
+
37
+ /**
38
+ * ArJdbc::Oracle
39
+ *
40
+ * @author kares
41
+ */
42
+ public class OracleModule {
43
+
44
+ public static RubyModule load(final RubyModule arJdbc) {
45
+ RubyModule oracle = arJdbc.defineModuleUnder("Oracle");
46
+ oracle.defineAnnotatedMethods( OracleModule.class );
47
+ return oracle;
48
+ }
49
+
50
+ public static RubyModule load(final Ruby runtime) {
51
+ return load( arjdbc.ArJdbcModule.get(runtime) );
52
+ }
53
+
54
+ @JRubyMethod(name = "quote_string", required = 1)
55
+ public static IRubyObject quote_string(
56
+ final ThreadContext context,
57
+ final IRubyObject self,
58
+ final IRubyObject string) { // string.gsub("'", "''") :
59
+ final char single = '\'';
60
+ final RubyString quoted = quoteCharWith(
61
+ context, (RubyString) string, single, single
62
+ );
63
+ return quoted;
64
+ }
65
+
66
+ @JRubyMethod(name = "quoted_true", required = 0)
67
+ public static IRubyObject quoted_true(
68
+ final ThreadContext context,
69
+ final IRubyObject self) {
70
+ return RubyString.newString(context.runtime, BYTES_1);
71
+ }
72
+
73
+ @JRubyMethod(name = "quoted_false", required = 0)
74
+ public static IRubyObject quoted_false(
75
+ final ThreadContext context,
76
+ final IRubyObject self) {
77
+ return RubyString.newString(context.runtime, BYTES_0);
78
+ }
79
+
80
+ }
@@ -1,5 +1,5 @@
1
- /*
2
- **** BEGIN LICENSE BLOCK *****
1
+ /***** BEGIN LICENSE BLOCK *****
2
+ * Copyright (c) 2012-2014 Karol Bucek <self@kares.org>
3
3
  * Copyright (c) 2006-2010 Nick Sieger <nick@nicksieger.com>
4
4
  * Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
5
5
  * Copyright (c) 2008-2009 Thomas E Enebo <enebo@acm.org>
@@ -23,16 +23,41 @@
23
23
  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
24
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
25
  ***** END LICENSE BLOCK *****/
26
+ // NOTE: file contains code adapted from **oracle-enhanced** adapter, license follows
27
+ /*
28
+ Copyright (c) 2008-2011 Graham Jenkins, Michael Schoen, Raimonds Simanovskis
29
+
30
+ ... LICENSING TERMS ARE THE VERY SAME AS ACTIVERECORD-JDBC-ADAPTER'S ABOVE ...
31
+ */
26
32
  package arjdbc.oracle;
27
33
 
34
+ import arjdbc.jdbc.Callable;
35
+ import arjdbc.jdbc.RubyJdbcConnection;
36
+ import arjdbc.util.CallResultSet;
37
+
38
+ import java.io.IOException;
39
+ import java.io.Reader;
40
+ import java.sql.CallableStatement;
41
+ import java.sql.Connection;
42
+ import java.sql.DatabaseMetaData;
28
43
  import java.sql.ResultSet;
29
44
  import java.sql.SQLException;
30
-
31
- import arjdbc.jdbc.RubyJdbcConnection;
45
+ import java.sql.PreparedStatement;
46
+ import java.sql.ResultSetMetaData;
47
+ import java.sql.Statement;
48
+ import java.sql.Types;
49
+ import java.util.Collections;
50
+ import java.util.List;
51
+ import java.util.Locale;
52
+ import java.util.regex.Pattern;
32
53
 
33
54
  import org.jruby.Ruby;
55
+ import org.jruby.RubyArray;
34
56
  import org.jruby.RubyClass;
57
+ import org.jruby.RubyString;
58
+ import org.jruby.anno.JRubyMethod;
35
59
  import org.jruby.runtime.ObjectAllocator;
60
+ import org.jruby.runtime.ThreadContext;
36
61
  import org.jruby.runtime.builtin.IRubyObject;
37
62
 
38
63
  /**
@@ -40,46 +65,391 @@ import org.jruby.runtime.builtin.IRubyObject;
40
65
  * @author nicksieger
41
66
  */
42
67
  public class OracleRubyJdbcConnection extends RubyJdbcConnection {
43
- protected OracleRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
68
+ private static final long serialVersionUID = -6469731781108431512L;
69
+
70
+ public OracleRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
44
71
  super(runtime, metaClass);
45
72
  }
46
73
 
47
74
  public static RubyClass createOracleJdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
48
- RubyClass clazz = RubyJdbcConnection.getConnectionAdapters(runtime).defineClassUnder("OracleJdbcConnection",
49
- jdbcConnection, ORACLE_JDBCCONNECTION_ALLOCATOR);
75
+ final RubyClass clazz = getConnectionAdapters(runtime).
76
+ defineClassUnder("OracleJdbcConnection", jdbcConnection, ALLOCATOR);
50
77
  clazz.defineAnnotatedMethods(OracleRubyJdbcConnection.class);
51
-
52
78
  return clazz;
53
79
  }
54
80
 
55
- private static ObjectAllocator ORACLE_JDBCCONNECTION_ALLOCATOR = new ObjectAllocator() {
81
+ public static RubyClass load(final Ruby runtime) {
82
+ RubyClass jdbcConnection = getJdbcConnection(runtime);
83
+ return createOracleJdbcConnectionClass(runtime, jdbcConnection);
84
+ }
85
+
86
+ protected static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
56
87
  public IRubyObject allocate(Ruby runtime, RubyClass klass) {
57
88
  return new OracleRubyJdbcConnection(runtime, klass);
58
89
  }
59
90
  };
60
91
 
92
+ @JRubyMethod(name = "next_sequence_value", required = 1)
93
+ public IRubyObject next_sequence_value(final ThreadContext context, final IRubyObject sequence) {
94
+ return withConnection(context, new Callable<IRubyObject>() {
95
+ public IRubyObject call(final Connection connection) throws SQLException {
96
+ Statement statement = null; ResultSet valSet = null;
97
+ try {
98
+ statement = connection.createStatement();
99
+ valSet = statement.executeQuery("SELECT "+ sequence +".NEXTVAL id FROM dual");
100
+ if ( ! valSet.next() ) return context.nil;
101
+ return context.runtime.newFixnum( valSet.getLong(1) );
102
+ }
103
+ catch (final SQLException e) {
104
+ debugMessage(context.runtime, "failed to get " + sequence + ".NEXTVAL : " + e.getMessage());
105
+ throw e;
106
+ }
107
+ finally { close(valSet); close(statement); }
108
+ }
109
+ });
110
+ }
111
+
112
+ @JRubyMethod(name = "execute_insert_returning", required = 2)
113
+ public IRubyObject execute_insert_returning(final ThreadContext context,
114
+ final IRubyObject sql, final IRubyObject binds) {
115
+ final String query = sqlString(sql);
116
+ final int outType = Types.VARCHAR;
117
+ if ( binds == null || binds.isNil() ) { // no prepared statements
118
+ return executePreparedCall(context, query, RubyArray.newEmptyArray(context.runtime), outType);
119
+ }
120
+ else { // allow prepared statements with empty binds parameters
121
+ return executePreparedCall(context, query, (RubyArray) binds, outType);
122
+ }
123
+ }
124
+
125
+ private IRubyObject executePreparedCall(final ThreadContext context, final String query,
126
+ final RubyArray binds, final int outType) {
127
+ return withConnection(context, new Callable<IRubyObject>() {
128
+ public IRubyObject call(final Connection connection) throws SQLException {
129
+ CallableStatement statement = null;
130
+ final int outIndex = binds.size() + 1;
131
+ try {
132
+ statement = connection.prepareCall("{call " + query + " }");
133
+ setStatementParameters(context, connection, statement, binds);
134
+ statement.registerOutParameter(outIndex, outType);
135
+ statement.executeUpdate();
136
+ ResultSet resultSet = new CallResultSet(statement);
137
+ return jdbcToRuby(context, context.getRuntime(), outIndex, outType, resultSet);
138
+ }
139
+ catch (final SQLException e) {
140
+ debugErrorSQL(context, query);
141
+ throw e;
142
+ }
143
+ finally { close(statement); }
144
+ }
145
+ });
146
+ }
147
+
148
+ protected static final boolean generatedKeys;
149
+ static {
150
+ final String genKeys = System.getProperty("arjdbc.oracle.generated_keys");
151
+ if ( genKeys == null ) {
152
+ generatedKeys = true; // by default
153
+ }
154
+ else {
155
+ generatedKeys = Boolean.parseBoolean(genKeys);
156
+ }
157
+ }
158
+
159
+ @Override
160
+ protected IRubyObject mapGeneratedKeys(
161
+ final Ruby runtime, final Connection connection,
162
+ final Statement statement, final Boolean singleResult)
163
+ throws SQLException {
164
+ if ( generatedKeys ) {
165
+ return super.mapGeneratedKeys(runtime, connection, statement, singleResult);
166
+ }
167
+ return null; // disabled using -Darjdbc.oracle.generated_keys=false
168
+ }
169
+
170
+ private static final boolean returnRowID = Boolean.getBoolean("arjdbc.oracle.generated_keys.rowid");
171
+
172
+ @Override // NOTE: Invalid column type:
173
+ // getLong not implemented for class oracle.jdbc.driver.T4CRowidAccessor
174
+ protected IRubyObject mapGeneratedKey(final Ruby runtime, final ResultSet genKeys)
175
+ throws SQLException {
176
+ // NOTE: it's likely a ROWID which we do not care about :
177
+ final String value = genKeys.getString(1); // "AAAsOjAAFAAABUlAAA"
178
+ if ( isPositiveInteger(value) ) {
179
+ return runtime.newFixnum( Long.parseLong(value) );
180
+ }
181
+ return returnRowID ? RubyString.newString(runtime, value) : runtime.getNil();
182
+ }
183
+
184
+ private static boolean isPositiveInteger(final String value) {
185
+ for ( int i = 0; i < value.length(); i++ ) {
186
+ if ( ! Character.isDigit(value.charAt(i)) ) return false;
187
+ }
188
+ return true;
189
+ }
190
+
191
+ @Override // resultSet.wasNull() might be falsy for '' treated as null
192
+ protected IRubyObject stringToRuby(final ThreadContext context,
193
+ final Ruby runtime, final ResultSet resultSet, final int column)
194
+ throws SQLException {
195
+ final String value = resultSet.getString(column);
196
+ if ( value == null ) return context.nil;
197
+ return RubyString.newUnicodeString(runtime, value);
198
+ }
199
+
200
+ @Override
201
+ protected IRubyObject readerToRuby(final ThreadContext context,
202
+ final Ruby runtime, final ResultSet resultSet, final int column)
203
+ throws SQLException, IOException {
204
+ final Reader reader = resultSet.getCharacterStream(column);
205
+ try {
206
+ if ( reader == null || resultSet.wasNull() ) {
207
+ return RubyString.newEmptyString(runtime);
208
+ }
209
+
210
+ final int bufSize = streamBufferSize;
211
+ final StringBuilder string = new StringBuilder(bufSize);
212
+
213
+ final char[] buf = new char[ bufSize / 2 ];
214
+ for (int len = reader.read(buf); len != -1; len = reader.read(buf)) {
215
+ string.append(buf, 0, len);
216
+ }
217
+
218
+ return RubyString.newUnicodeString(runtime, string.toString());
219
+ }
220
+ finally { if ( reader != null ) reader.close(); }
221
+ }
222
+
223
+ @Override // booleans are emulated can not setNull(index, Types.BOOLEAN)
224
+ protected void setBooleanParameter(final ThreadContext context,
225
+ final Connection connection, final PreparedStatement statement,
226
+ final int index, final IRubyObject value,
227
+ final IRubyObject column, final int type) throws SQLException {
228
+ if ( value.isNil() ) statement.setNull(index, Types.TINYINT);
229
+ else {
230
+ statement.setBoolean(index, value.isTrue());
231
+ }
232
+ }
233
+
61
234
  /**
62
235
  * Oracle needs this override to reconstruct NUMBER which is different
63
236
  * from NUMBER(x) or NUMBER(x,y).
64
237
  */
65
238
  @Override
66
- protected String typeFromResultSet(ResultSet resultSet) throws SQLException {
239
+ protected String typeFromResultSet(final ResultSet resultSet) throws SQLException {
67
240
  int precision = intFromResultSet(resultSet, COLUMN_SIZE);
68
241
  int scale = intFromResultSet(resultSet, DECIMAL_DIGITS);
69
242
 
70
243
  // According to http://forums.oracle.com/forums/thread.jspa?threadID=658646
71
244
  // Unadorned NUMBER reports scale == null, so we look for that here.
72
- if (scale < 0 && resultSet.getInt(DATA_TYPE) == java.sql.Types.DECIMAL) {
245
+ if ( scale < 0 && resultSet.getInt(DATA_TYPE) == java.sql.Types.DECIMAL ) {
73
246
  precision = -1;
74
247
  }
75
248
 
76
- String type = resultSet.getString(TYPE_NAME);
77
- if (precision > 0) {
78
- type += "(" + precision;
79
- if(scale > 0) type += "," + scale;
80
- type += ")";
249
+ final String type = resultSet.getString(TYPE_NAME);
250
+ return formatTypeWithPrecisionAndScale(type, precision, scale);
251
+ }
252
+
253
+ @Override
254
+ protected RubyArray mapTables(final ThreadContext context, final Connection connection,
255
+ final String catalog, final String schemaPattern, final String tablePattern,
256
+ final ResultSet tablesSet) throws SQLException {
257
+ final RubyArray tables = context.runtime.newArray(32);
258
+ while ( tablesSet.next() ) {
259
+ String name = tablesSet.getString(TABLES_TABLE_NAME);
260
+ name = caseConvertIdentifierForRails(connection, name);
261
+ // Handle stupid Oracle 10g RecycleBin feature
262
+ if ( name.startsWith("bin$") ) continue;
263
+ tables.append(RubyString.newUnicodeString(context.runtime, name));
264
+ }
265
+ return tables;
266
+ }
267
+
268
+ @Override
269
+ protected ColumnData[] extractColumns(final ThreadContext context,
270
+ final Connection connection, final ResultSet resultSet,
271
+ final boolean downCase) throws SQLException {
272
+
273
+ final ResultSetMetaData resultMetaData = resultSet.getMetaData();
274
+
275
+ final int columnCount = resultMetaData.getColumnCount();
276
+ final ColumnData[] columns = new ColumnData[columnCount];
277
+
278
+ for ( int i = 1; i <= columnCount; i++ ) { // metadata is one-based
279
+ String name = resultMetaData.getColumnLabel(i);
280
+ if ( downCase ) {
281
+ name = name.toLowerCase(Locale.ENGLISH);
282
+ } else {
283
+ name = caseConvertIdentifierForRails(connection, name);
284
+ }
285
+
286
+ int columnType = resultMetaData.getColumnType(i);
287
+ if (columnType == Types.NUMERIC) {
288
+ // avoid extracting all NUMBER columns as BigDecimal :
289
+ if (resultMetaData.getScale(i) == 0) {
290
+ final int prec = resultMetaData.getPrecision(i);
291
+ if ( prec < 10 ) { // fits into int
292
+ columnType = Types.INTEGER;
293
+ }
294
+ else if ( prec < 19 ) { // fits into long
295
+ columnType = Types.BIGINT;
296
+ }
297
+ }
298
+ }
299
+
300
+ columns[i - 1] = new ColumnData(name, columnType, i);
301
+ }
302
+
303
+ return columns;
304
+ }
305
+
306
+ // storesMixedCaseIdentifiers() return false;
307
+ // storesLowerCaseIdentifiers() return false;
308
+ // storesUpperCaseIdentifiers() return true;
309
+
310
+ @Override
311
+ protected String caseConvertIdentifierForRails(final Connection connection, final String value)
312
+ throws SQLException {
313
+ return value == null ? null : value.toLowerCase();
314
+ }
315
+
316
+ @Override
317
+ protected String caseConvertIdentifierForJdbc(final Connection connection, final String value)
318
+ throws SQLException {
319
+ return value == null ? null : value.toUpperCase();
320
+ }
321
+
322
+ // based on OracleEnhanced's Ruby connection.describe
323
+ @JRubyMethod(name = "describe", required = 1)
324
+ public IRubyObject describe(final ThreadContext context, final IRubyObject name) {
325
+ final RubyArray desc = describe(context, name.toString(), null);
326
+ return desc == null ? context.nil : desc; // TODO raise instead of nil
327
+ }
328
+
329
+ @JRubyMethod(name = "describe", required = 2)
330
+ public IRubyObject describe(final ThreadContext context, final IRubyObject name, final IRubyObject owner) {
331
+ final RubyArray desc = describe(context, name.toString(), owner.isNil() ? null : owner.toString());
332
+ return desc == null ? context.nil : desc; // TODO raise instead of nil
333
+ }
334
+
335
+ private RubyArray describe(final ThreadContext context, final String name, final String owner) {
336
+ final String dbLink; String defaultOwner, theName = name; int delim;
337
+ if ( ( delim = theName.indexOf('@') ) > 0 ) {
338
+ dbLink = theName.substring(delim).toUpperCase(); // '@DBLINK'
339
+ theName = theName.substring(0, delim);
340
+ defaultOwner = null; // will SELECT username FROM all_dbLinks ...
341
+ }
342
+ else {
343
+ dbLink = ""; defaultOwner = owner; // config[:username] || meta_data.user_name
344
+ }
345
+
346
+ theName = isValidTableName(theName) ? theName.toUpperCase() : unquoteTableName(theName);
347
+
348
+ final String tableName; final String tableOwner;
349
+ if ( ( delim = theName.indexOf('.') ) > 0 ) {
350
+ tableOwner = theName.substring(0, delim);
351
+ tableName = theName.substring(delim + 1);
352
+ }
353
+ else {
354
+ tableName = theName;
355
+ tableOwner = (defaultOwner == null && dbLink.length() > 0) ? selectOwner(context, dbLink) : defaultOwner;
81
356
  }
82
357
 
83
- return type;
358
+ return withConnection(context, new Callable<RubyArray>() {
359
+ public RubyArray call(final Connection connection) throws SQLException {
360
+ String owner = tableOwner == null ? connection.getMetaData().getUserName() : tableOwner;
361
+ final String sql =
362
+ "SELECT owner, table_name, 'TABLE' name_type" +
363
+ " FROM all_tables" + dbLink +
364
+ " WHERE owner = '" + owner + "' AND table_name = '" + tableName + "'" +
365
+ " UNION ALL " +
366
+ "SELECT owner, view_name table_name, 'VIEW' name_type" +
367
+ " FROM all_views" + dbLink +
368
+ " WHERE owner = '" + owner + "' AND view_name = '" + tableName + "'" +
369
+ " UNION ALL " +
370
+ "SELECT table_owner, DECODE(db_link, NULL, table_name, table_name||'@'||db_link), 'SYNONYM' name_type" +
371
+ " FROM all_synonyms" + dbLink +
372
+ " WHERE owner = '" + owner + "' AND synonym_name = '" + tableName + "'" +
373
+ " UNION ALL " +
374
+ "SELECT table_owner, DECODE(db_link, NULL, table_name, table_name||'@'||db_link), 'SYNONYM' name_type" +
375
+ " FROM all_synonyms" + dbLink +
376
+ " WHERE owner = 'PUBLIC' AND synonym_name = '" + tableName + "'" ;
377
+
378
+ Statement statement = null; ResultSet result = null;
379
+ try {
380
+ statement = connection.createStatement();
381
+ result = statement.executeQuery(sql);
382
+
383
+ if ( ! result.next() ) return null; // NOTE: should raise
384
+
385
+ owner = result.getString("owner");
386
+ final String table_name = result.getString("table_name");
387
+ final String name_type = result.getString("name_type");
388
+
389
+ if ( "SYNONYM".equals(name_type) ) {
390
+ final StringBuilder name = new StringBuilder();
391
+ if ( owner != null && owner.length() > 0 ) {
392
+ name.append(owner).append('.');
393
+ }
394
+ name.append(table_name);
395
+ if ( dbLink != null ) name.append(dbLink);
396
+ return describe(context, name.toString(), owner);
397
+ }
398
+
399
+ final RubyArray arr = RubyArray.newArray(context.runtime, 3);
400
+ arr.append( context.runtime.newString(owner) );
401
+ arr.append( context.runtime.newString(table_name) );
402
+ if ( dbLink != null ) arr.append( context.runtime.newString(dbLink) );
403
+ return arr;
404
+ }
405
+ catch (final SQLException e) {
406
+ debugMessage(context.runtime, "failed to describe '" + name + "' : ", e);
407
+ throw e;
408
+ }
409
+ finally { close(result); close(statement); }
410
+ }
411
+ });
412
+ }
413
+
414
+ private String selectOwner(final ThreadContext context, final String dbLink) {
415
+ return withConnection(context, new Callable<String>() {
416
+ public String call(final Connection connection) throws SQLException {
417
+ Statement statement = null; ResultSet result = null;
418
+ final String sql = "SELECT username FROM all_db_links WHERE db_link = '" + dbLink + "'";
419
+ try {
420
+ statement = connection.createStatement();
421
+ result = statement.executeQuery(sql);
422
+ // if ( ! result.next() ) return null;
423
+ return result.getString(1);
424
+ }
425
+ catch (final SQLException e) {
426
+ debugMessage(context.runtime, "select owner (" + sql + ") failed: ", e);
427
+ throw e;
428
+ }
429
+ finally { close(result); close(statement); }
430
+ }
431
+ });
84
432
  }
433
+
434
+ private static final Pattern VALID_TABLE_NAME;
435
+ static {
436
+ final String NONQUOTED_OBJECT_NAME = "[A-Za-z][A-z0-9$#]{0,29}";
437
+ final String NONQUOTED_DATABASE_LINK = "[A-Za-z][A-z0-9$#\\.@]{0,127}";
438
+ VALID_TABLE_NAME = Pattern.compile(
439
+ "\\A(?:" + NONQUOTED_OBJECT_NAME + "\\.)?" + NONQUOTED_OBJECT_NAME + "(?:@" + NONQUOTED_DATABASE_LINK + ")?\\Z");
440
+ }
441
+
442
+ private static boolean isValidTableName(final String name) {
443
+ return VALID_TABLE_NAME.matcher(name).matches();
444
+ }
445
+
446
+ private static String unquoteTableName(String name) {
447
+ name = name.trim();
448
+ final int len = name.length();
449
+ if (len > 0 && name.charAt(0) == '"' && name.charAt(len - 1) == '"') {
450
+ return name.substring(1, len - 1);
451
+ }
452
+ return name;
453
+ }
454
+
85
455
  }