activerecord-jdbc-alt-adapter 50.3.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (198) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +35 -0
  3. data/.travis.yml +100 -0
  4. data/.yardopts +4 -0
  5. data/CONTRIBUTING.md +50 -0
  6. data/Gemfile +92 -0
  7. data/History.md +1191 -0
  8. data/LICENSE.txt +26 -0
  9. data/README.md +240 -0
  10. data/RUNNING_TESTS.md +127 -0
  11. data/Rakefile +336 -0
  12. data/Rakefile.jdbc +20 -0
  13. data/activerecord-jdbc-adapter.gemspec +55 -0
  14. data/activerecord-jdbc-alt-adapter.gemspec +56 -0
  15. data/lib/active_record/connection_adapters/as400_adapter.rb +2 -0
  16. data/lib/active_record/connection_adapters/db2_adapter.rb +1 -0
  17. data/lib/active_record/connection_adapters/derby_adapter.rb +1 -0
  18. data/lib/active_record/connection_adapters/firebird_adapter.rb +1 -0
  19. data/lib/active_record/connection_adapters/h2_adapter.rb +1 -0
  20. data/lib/active_record/connection_adapters/hsqldb_adapter.rb +1 -0
  21. data/lib/active_record/connection_adapters/informix_adapter.rb +1 -0
  22. data/lib/active_record/connection_adapters/jdbc_adapter.rb +1 -0
  23. data/lib/active_record/connection_adapters/jndi_adapter.rb +1 -0
  24. data/lib/active_record/connection_adapters/mariadb_adapter.rb +1 -0
  25. data/lib/active_record/connection_adapters/mssql_adapter.rb +1 -0
  26. data/lib/active_record/connection_adapters/mysql2_adapter.rb +1 -0
  27. data/lib/active_record/connection_adapters/mysql_adapter.rb +1 -0
  28. data/lib/active_record/connection_adapters/postgresql_adapter.rb +1 -0
  29. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -0
  30. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +1 -0
  31. data/lib/activerecord-jdbc-adapter.rb +1 -0
  32. data/lib/arel/visitors/compat.rb +60 -0
  33. data/lib/arel/visitors/db2.rb +137 -0
  34. data/lib/arel/visitors/derby.rb +112 -0
  35. data/lib/arel/visitors/firebird.rb +79 -0
  36. data/lib/arel/visitors/h2.rb +25 -0
  37. data/lib/arel/visitors/hsqldb.rb +32 -0
  38. data/lib/arel/visitors/postgresql_jdbc.rb +6 -0
  39. data/lib/arel/visitors/sql_server.rb +225 -0
  40. data/lib/arel/visitors/sql_server/ng42.rb +294 -0
  41. data/lib/arel/visitors/sqlserver.rb +214 -0
  42. data/lib/arjdbc.rb +19 -0
  43. data/lib/arjdbc/abstract/connection_management.rb +35 -0
  44. data/lib/arjdbc/abstract/core.rb +74 -0
  45. data/lib/arjdbc/abstract/database_statements.rb +64 -0
  46. data/lib/arjdbc/abstract/statement_cache.rb +58 -0
  47. data/lib/arjdbc/abstract/transaction_support.rb +86 -0
  48. data/lib/arjdbc/db2.rb +4 -0
  49. data/lib/arjdbc/db2/adapter.rb +789 -0
  50. data/lib/arjdbc/db2/as400.rb +130 -0
  51. data/lib/arjdbc/db2/column.rb +167 -0
  52. data/lib/arjdbc/db2/connection_methods.rb +44 -0
  53. data/lib/arjdbc/derby.rb +3 -0
  54. data/lib/arjdbc/derby/active_record_patch.rb +13 -0
  55. data/lib/arjdbc/derby/adapter.rb +540 -0
  56. data/lib/arjdbc/derby/connection_methods.rb +20 -0
  57. data/lib/arjdbc/derby/schema_creation.rb +15 -0
  58. data/lib/arjdbc/discover.rb +104 -0
  59. data/lib/arjdbc/firebird.rb +4 -0
  60. data/lib/arjdbc/firebird/adapter.rb +434 -0
  61. data/lib/arjdbc/firebird/connection_methods.rb +23 -0
  62. data/lib/arjdbc/h2.rb +3 -0
  63. data/lib/arjdbc/h2/adapter.rb +303 -0
  64. data/lib/arjdbc/h2/connection_methods.rb +27 -0
  65. data/lib/arjdbc/hsqldb.rb +3 -0
  66. data/lib/arjdbc/hsqldb/adapter.rb +297 -0
  67. data/lib/arjdbc/hsqldb/connection_methods.rb +28 -0
  68. data/lib/arjdbc/hsqldb/explain_support.rb +35 -0
  69. data/lib/arjdbc/hsqldb/schema_creation.rb +11 -0
  70. data/lib/arjdbc/informix.rb +5 -0
  71. data/lib/arjdbc/informix/adapter.rb +162 -0
  72. data/lib/arjdbc/informix/connection_methods.rb +9 -0
  73. data/lib/arjdbc/jdbc.rb +59 -0
  74. data/lib/arjdbc/jdbc/adapter.rb +475 -0
  75. data/lib/arjdbc/jdbc/adapter_require.rb +46 -0
  76. data/lib/arjdbc/jdbc/base_ext.rb +15 -0
  77. data/lib/arjdbc/jdbc/callbacks.rb +53 -0
  78. data/lib/arjdbc/jdbc/column.rb +97 -0
  79. data/lib/arjdbc/jdbc/connection.rb +14 -0
  80. data/lib/arjdbc/jdbc/connection_methods.rb +37 -0
  81. data/lib/arjdbc/jdbc/error.rb +65 -0
  82. data/lib/arjdbc/jdbc/extension.rb +59 -0
  83. data/lib/arjdbc/jdbc/java.rb +13 -0
  84. data/lib/arjdbc/jdbc/railtie.rb +2 -0
  85. data/lib/arjdbc/jdbc/rake_tasks.rb +3 -0
  86. data/lib/arjdbc/jdbc/serialized_attributes_helper.rb +3 -0
  87. data/lib/arjdbc/jdbc/type_cast.rb +166 -0
  88. data/lib/arjdbc/jdbc/type_converter.rb +142 -0
  89. data/lib/arjdbc/mssql.rb +7 -0
  90. data/lib/arjdbc/mssql/adapter.rb +384 -0
  91. data/lib/arjdbc/mssql/column.rb +29 -0
  92. data/lib/arjdbc/mssql/connection_methods.rb +79 -0
  93. data/lib/arjdbc/mssql/database_statements.rb +134 -0
  94. data/lib/arjdbc/mssql/errors.rb +6 -0
  95. data/lib/arjdbc/mssql/explain_support.rb +129 -0
  96. data/lib/arjdbc/mssql/extensions.rb +36 -0
  97. data/lib/arjdbc/mssql/limit_helpers.rb +231 -0
  98. data/lib/arjdbc/mssql/lock_methods.rb +77 -0
  99. data/lib/arjdbc/mssql/old_adapter.rb +804 -0
  100. data/lib/arjdbc/mssql/old_column.rb +200 -0
  101. data/lib/arjdbc/mssql/quoting.rb +101 -0
  102. data/lib/arjdbc/mssql/schema_creation.rb +31 -0
  103. data/lib/arjdbc/mssql/schema_definitions.rb +74 -0
  104. data/lib/arjdbc/mssql/schema_statements.rb +329 -0
  105. data/lib/arjdbc/mssql/transaction.rb +69 -0
  106. data/lib/arjdbc/mssql/types.rb +52 -0
  107. data/lib/arjdbc/mssql/types/binary_types.rb +33 -0
  108. data/lib/arjdbc/mssql/types/date_and_time_types.rb +134 -0
  109. data/lib/arjdbc/mssql/types/deprecated_types.rb +40 -0
  110. data/lib/arjdbc/mssql/types/numeric_types.rb +71 -0
  111. data/lib/arjdbc/mssql/types/string_types.rb +56 -0
  112. data/lib/arjdbc/mssql/utils.rb +66 -0
  113. data/lib/arjdbc/mysql.rb +3 -0
  114. data/lib/arjdbc/mysql/adapter.rb +140 -0
  115. data/lib/arjdbc/mysql/connection_methods.rb +166 -0
  116. data/lib/arjdbc/oracle/adapter.rb +863 -0
  117. data/lib/arjdbc/postgresql.rb +3 -0
  118. data/lib/arjdbc/postgresql/adapter.rb +687 -0
  119. data/lib/arjdbc/postgresql/base/array_decoder.rb +26 -0
  120. data/lib/arjdbc/postgresql/base/array_encoder.rb +25 -0
  121. data/lib/arjdbc/postgresql/base/array_parser.rb +95 -0
  122. data/lib/arjdbc/postgresql/base/pgconn.rb +11 -0
  123. data/lib/arjdbc/postgresql/column.rb +51 -0
  124. data/lib/arjdbc/postgresql/connection_methods.rb +67 -0
  125. data/lib/arjdbc/postgresql/name.rb +24 -0
  126. data/lib/arjdbc/postgresql/oid_types.rb +266 -0
  127. data/lib/arjdbc/railtie.rb +11 -0
  128. data/lib/arjdbc/sqlite3.rb +3 -0
  129. data/lib/arjdbc/sqlite3/adapter.rb +678 -0
  130. data/lib/arjdbc/sqlite3/connection_methods.rb +59 -0
  131. data/lib/arjdbc/sybase.rb +2 -0
  132. data/lib/arjdbc/sybase/adapter.rb +47 -0
  133. data/lib/arjdbc/tasks.rb +13 -0
  134. data/lib/arjdbc/tasks/database_tasks.rb +31 -0
  135. data/lib/arjdbc/tasks/databases.rake +48 -0
  136. data/lib/arjdbc/tasks/db2_database_tasks.rb +104 -0
  137. data/lib/arjdbc/tasks/derby_database_tasks.rb +95 -0
  138. data/lib/arjdbc/tasks/h2_database_tasks.rb +31 -0
  139. data/lib/arjdbc/tasks/hsqldb_database_tasks.rb +70 -0
  140. data/lib/arjdbc/tasks/jdbc_database_tasks.rb +169 -0
  141. data/lib/arjdbc/tasks/mssql_database_tasks.rb +46 -0
  142. data/lib/arjdbc/util/quoted_cache.rb +60 -0
  143. data/lib/arjdbc/util/serialized_attributes.rb +98 -0
  144. data/lib/arjdbc/util/table_copier.rb +110 -0
  145. data/lib/arjdbc/version.rb +3 -0
  146. data/lib/generators/jdbc/USAGE +9 -0
  147. data/lib/generators/jdbc/jdbc_generator.rb +17 -0
  148. data/lib/jdbc_adapter.rb +2 -0
  149. data/lib/jdbc_adapter/rake_tasks.rb +4 -0
  150. data/lib/jdbc_adapter/version.rb +4 -0
  151. data/pom.xml +114 -0
  152. data/rails_generators/jdbc_generator.rb +15 -0
  153. data/rails_generators/templates/config/initializers/jdbc.rb +10 -0
  154. data/rails_generators/templates/lib/tasks/jdbc.rake +11 -0
  155. data/rakelib/01-tomcat.rake +51 -0
  156. data/rakelib/02-test.rake +132 -0
  157. data/rakelib/bundler_ext.rb +11 -0
  158. data/rakelib/db.rake +75 -0
  159. data/rakelib/rails.rake +223 -0
  160. data/src/java/arjdbc/ArJdbcModule.java +276 -0
  161. data/src/java/arjdbc/db2/DB2Module.java +76 -0
  162. data/src/java/arjdbc/db2/DB2RubyJdbcConnection.java +126 -0
  163. data/src/java/arjdbc/derby/DerbyModule.java +178 -0
  164. data/src/java/arjdbc/derby/DerbyRubyJdbcConnection.java +152 -0
  165. data/src/java/arjdbc/firebird/FirebirdRubyJdbcConnection.java +174 -0
  166. data/src/java/arjdbc/h2/H2Module.java +50 -0
  167. data/src/java/arjdbc/h2/H2RubyJdbcConnection.java +85 -0
  168. data/src/java/arjdbc/hsqldb/HSQLDBModule.java +73 -0
  169. data/src/java/arjdbc/informix/InformixRubyJdbcConnection.java +75 -0
  170. data/src/java/arjdbc/jdbc/AdapterJavaService.java +43 -0
  171. data/src/java/arjdbc/jdbc/Callable.java +44 -0
  172. data/src/java/arjdbc/jdbc/ConnectionFactory.java +45 -0
  173. data/src/java/arjdbc/jdbc/DataSourceConnectionFactory.java +156 -0
  174. data/src/java/arjdbc/jdbc/DriverConnectionFactory.java +63 -0
  175. data/src/java/arjdbc/jdbc/DriverWrapper.java +119 -0
  176. data/src/java/arjdbc/jdbc/JdbcResult.java +130 -0
  177. data/src/java/arjdbc/jdbc/RubyConnectionFactory.java +61 -0
  178. data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +3979 -0
  179. data/src/java/arjdbc/mssql/MSSQLModule.java +90 -0
  180. data/src/java/arjdbc/mssql/MSSQLRubyJdbcConnection.java +508 -0
  181. data/src/java/arjdbc/mysql/MySQLModule.java +152 -0
  182. data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +294 -0
  183. data/src/java/arjdbc/oracle/OracleModule.java +80 -0
  184. data/src/java/arjdbc/oracle/OracleRubyJdbcConnection.java +455 -0
  185. data/src/java/arjdbc/postgresql/ByteaUtils.java +157 -0
  186. data/src/java/arjdbc/postgresql/PgDateTimeUtils.java +52 -0
  187. data/src/java/arjdbc/postgresql/PostgreSQLModule.java +77 -0
  188. data/src/java/arjdbc/postgresql/PostgreSQLResult.java +192 -0
  189. data/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java +948 -0
  190. data/src/java/arjdbc/sqlite3/SQLite3Module.java +73 -0
  191. data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +525 -0
  192. data/src/java/arjdbc/util/CallResultSet.java +826 -0
  193. data/src/java/arjdbc/util/DateTimeUtils.java +699 -0
  194. data/src/java/arjdbc/util/ObjectSupport.java +65 -0
  195. data/src/java/arjdbc/util/QuotingUtils.java +137 -0
  196. data/src/java/arjdbc/util/StringCache.java +63 -0
  197. data/src/java/arjdbc/util/StringHelper.java +145 -0
  198. metadata +269 -0
@@ -0,0 +1,90 @@
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.mssql;
25
+
26
+ import static arjdbc.util.QuotingUtils.BYTES_0;
27
+ import static arjdbc.util.QuotingUtils.BYTES_1;
28
+ import static arjdbc.util.QuotingUtils.quoteCharAndDecorateWith;
29
+ import static arjdbc.util.QuotingUtils.quoteSingleQuotesWithFallback;
30
+
31
+ import org.jruby.Ruby;
32
+ import org.jruby.RubyModule;
33
+ import org.jruby.RubyString;
34
+ import org.jruby.anno.JRubyMethod;
35
+ import org.jruby.runtime.ThreadContext;
36
+ import org.jruby.runtime.Visibility;
37
+ import org.jruby.runtime.builtin.IRubyObject;
38
+ import org.jruby.util.ByteList;
39
+
40
+ /**
41
+ * ArJdbc::MSSQL
42
+ *
43
+ * @author kares
44
+ */
45
+ public class MSSQLModule {
46
+
47
+ public static RubyModule load(final RubyModule arJdbc) {
48
+ RubyModule mssql = arJdbc.defineModuleUnder("MSSQL");
49
+ mssql.defineAnnotatedMethods( MSSQLModule.class );
50
+ return mssql;
51
+ }
52
+
53
+ public static RubyModule load(final Ruby runtime) {
54
+ return load( arjdbc.ArJdbcModule.get(runtime) );
55
+ }
56
+
57
+ @JRubyMethod(name = "quote_string", required = 1)
58
+ public static IRubyObject quote_string(final ThreadContext context,
59
+ final IRubyObject self, final IRubyObject string) {
60
+ return quoteSingleQuotesWithFallback(context, string);
61
+ }
62
+
63
+ @JRubyMethod(name = "quoted_true", required = 0)
64
+ public static IRubyObject quoted_true(final ThreadContext context,
65
+ final IRubyObject self) {
66
+ return RubyString.newString(context.runtime, BYTES_1);
67
+ }
68
+
69
+ @JRubyMethod(name = "quoted_false", required = 0)
70
+ public static IRubyObject quoted_false(final ThreadContext context,
71
+ final IRubyObject self) {
72
+ return RubyString.newString(context.runtime, BYTES_0);
73
+ }
74
+
75
+ // part =~ /^\[.*\]$/ ? part : "[#{part.gsub(']', ']]')}]"
76
+ @SuppressWarnings("deprecation")
77
+ @JRubyMethod(required = 1, visibility = Visibility.PRIVATE)
78
+ public static IRubyObject quote_name_part(final ThreadContext context,
79
+ final IRubyObject self, final IRubyObject part) {
80
+
81
+ final RubyString partString = (RubyString) part;
82
+ final ByteList str = partString.getByteList();
83
+ if ( str.charAt(0) == '[' && str.charAt(str.length() - 1) == ']' ) {
84
+ return part; // part =~ /^\[.*\]$/ ? part
85
+ }
86
+
87
+ return quoteCharAndDecorateWith(context, partString, ']', ']', (byte) '[', (byte) ']');
88
+ }
89
+
90
+ }
@@ -0,0 +1,508 @@
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.mssql;
27
+
28
+ import arjdbc.jdbc.Callable;
29
+ import arjdbc.jdbc.RubyJdbcConnection;
30
+ import arjdbc.util.DateTimeUtils;
31
+
32
+ import java.sql.Connection;
33
+ import java.sql.DatabaseMetaData;
34
+ import java.sql.PreparedStatement;
35
+ import java.sql.ResultSet;
36
+ import java.sql.Savepoint;
37
+ import java.sql.SQLException;
38
+ import java.sql.Types;
39
+ import java.sql.Timestamp;
40
+ import java.util.Locale;
41
+
42
+ import org.jruby.Ruby;
43
+ import org.jruby.RubyArray;
44
+ import org.jruby.RubyBoolean;
45
+ import org.jruby.RubyClass;
46
+ import org.jruby.RubyString;
47
+ import org.jruby.RubySymbol;
48
+ import org.jruby.anno.JRubyMethod;
49
+ import org.jruby.runtime.ObjectAllocator;
50
+ import org.jruby.runtime.ThreadContext;
51
+ import org.jruby.runtime.builtin.IRubyObject;
52
+ import org.jruby.util.ByteList;
53
+
54
+ /**
55
+ *
56
+ * @author nicksieger
57
+ */
58
+ public class MSSQLRubyJdbcConnection extends RubyJdbcConnection {
59
+ private static final long serialVersionUID = -745716565005219263L;
60
+
61
+ public MSSQLRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
62
+ super(runtime, metaClass);
63
+ }
64
+
65
+ public static RubyClass createMSSQLJdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
66
+ final RubyClass clazz = getConnectionAdapters(runtime). // ActiveRecord::ConnectionAdapters
67
+ defineClassUnder("MSSQLJdbcConnection", jdbcConnection, ALLOCATOR);
68
+ clazz.defineAnnotatedMethods(MSSQLRubyJdbcConnection.class);
69
+ getConnectionAdapters(runtime).setConstant("MssqlJdbcConnection", clazz); // backwards-compat
70
+ return clazz;
71
+ }
72
+
73
+ public static RubyClass load(final Ruby runtime) {
74
+ RubyClass jdbcConnection = getJdbcConnection(runtime);
75
+ return createMSSQLJdbcConnectionClass(runtime, jdbcConnection);
76
+ }
77
+
78
+ protected static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
79
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
80
+ return new MSSQLRubyJdbcConnection(runtime, klass);
81
+ }
82
+ };
83
+
84
+ private static final byte[] EXEC = new byte[] { 'e', 'x', 'e', 'c' };
85
+
86
+ @JRubyMethod(name = "exec?", required = 1, meta = true, frame = false)
87
+ public static RubyBoolean exec_p(ThreadContext context, IRubyObject self, IRubyObject sql) {
88
+ final ByteList sqlBytes = sql.asString().getByteList();
89
+ return context.runtime.newBoolean( startsWithIgnoreCase(sqlBytes, EXEC) );
90
+ }
91
+
92
+ @Override
93
+ protected RubyArray mapTables(final ThreadContext context, final Connection connection,
94
+ final String catalog, final String schemaPattern, final String tablePattern,
95
+ final ResultSet tablesSet) throws SQLException, IllegalStateException {
96
+
97
+ final RubyArray tables = context.runtime.newArray();
98
+
99
+ while ( tablesSet.next() ) {
100
+ String schema = tablesSet.getString(TABLES_TABLE_SCHEM);
101
+ if ( schema != null ) schema = schema.toLowerCase();
102
+ // Under MS-SQL, don't return system tables/views unless explicitly asked for :
103
+ if ( schemaPattern == null &&
104
+ ( "sys".equals(schema) || "information_schema".equals(schema) ) ) {
105
+ continue;
106
+ }
107
+ String name = tablesSet.getString(TABLES_TABLE_NAME);
108
+ if ( name == null ) {
109
+ // NOTE: seems there's a jTDS but when doing getColumns while
110
+ // EXPLAIN is on (e.g. `SET SHOWPLAN_TEXT ON`) not returning
111
+ // correct result set with table info (null NAME, invalid CAT)
112
+ throw new IllegalStateException("got null name while matching table(s): [" +
113
+ catalog + "." + schemaPattern + "." + tablePattern + "] check " +
114
+ "if this happened during EXPLAIN (SET SHOWPLAN_TEXT ON) if so please try " +
115
+ "turning it off using the system property 'arjdbc.mssql.explain_support.disabled=true' " +
116
+ "or programatically by changing: `ArJdbc::MSSQL::ExplainSupport::DISABLED`");
117
+ }
118
+ tables.add( cachedString(context, caseConvertIdentifierForRails(connection, name)) );
119
+ }
120
+ return tables;
121
+ }
122
+
123
+ // Format properly SQL Server types (this is the variable sql_type)
124
+ @Override
125
+ protected String typeFromResultSet(final ResultSet resultSet) throws SQLException {
126
+ final int precision = intFromResultSet(resultSet, COLUMN_SIZE);
127
+ final int scale = intFromResultSet(resultSet, DECIMAL_DIGITS);
128
+ final int length = intFromResultSet(resultSet, BUFFER_LENGTH);
129
+
130
+ final int dataType = intFromResultSet(resultSet, DATA_TYPE);
131
+ final String typeName = resultSet.getString(TYPE_NAME);
132
+
133
+ final String uuid = "uniqueidentifier";
134
+ final String money = "money";
135
+ final String smallmoney = "smallmoney";
136
+ final String datetime2 = "datetime2";
137
+
138
+ switch (dataType) {
139
+ // For integers the Rails limit option is the JDBC BUFFER_LENGTH.
140
+ case Types.INTEGER:
141
+ // limit is 4
142
+ return typeName;
143
+ case Types.TINYINT:
144
+ // limit is 1
145
+ return typeName;
146
+ case Types.SMALLINT:
147
+ // limit is 2
148
+ return typeName;
149
+ case Types.BIGINT:
150
+ // limit is 8
151
+ return typeName;
152
+ case Types.BIT:
153
+ case Types.REAL:
154
+ case Types.DOUBLE:
155
+ // SQL server FLOAT type is double in jdbc
156
+ return typeName;
157
+ case Types.DATE:
158
+ case Types.TIMESTAMP:
159
+ // For datetime2 sql type, Rails precision comes in the scale
160
+ if (typeName.equals(datetime2)) {
161
+ return formatTypeWithPrecision(typeName, scale);
162
+ }
163
+
164
+ return typeName;
165
+ case Types.TIME:
166
+ // For time the Rails precision comes in the scale
167
+ return formatTypeWithPrecision(typeName, scale);
168
+ case Types.NUMERIC:
169
+ case Types.DECIMAL:
170
+ // money and smallmoney are considered decimals with specific
171
+ // precision, money(19,4) and smallmoney(10, 4)
172
+ if ( typeName.equals(money) || typeName.equals(smallmoney) ) {
173
+ return typeName;
174
+ }
175
+
176
+ return formatTypeWithPrecisionAndScale(typeName, precision, scale);
177
+ case Types.CHAR:
178
+ // The uuid is char type
179
+ if ( typeName.equals(uuid) ) {
180
+ return typeName;
181
+ }
182
+
183
+ return formatTypeWithPrecision(typeName, precision);
184
+ case Types.NCHAR:
185
+ return formatTypeWithPrecision(typeName, precision);
186
+ case Types.VARCHAR:
187
+ case Types.NVARCHAR:
188
+ if ( precision == 2147483647 ) {
189
+ return formatTypeWithPrecisionMax(typeName, "max");
190
+ }
191
+
192
+ return formatTypeWithPrecision(typeName, precision);
193
+ case Types.BINARY:
194
+ case Types.VARBINARY:
195
+ if ( precision == 2147483647 ) {
196
+ return formatTypeWithPrecisionMax(typeName, "max");
197
+ }
198
+
199
+ return formatTypeWithPrecision(typeName, precision);
200
+ case Types.LONGVARBINARY:
201
+ // This maps to IMAGE type
202
+ return typeName;
203
+ case Types.LONGVARCHAR:
204
+ // This maps to TEXT type
205
+ return typeName;
206
+ case Types.LONGNVARCHAR:
207
+ // This maps to XML and NTEXT types
208
+ // FIXME: The XML type needs to be reviewed.
209
+ return typeName;
210
+ default:
211
+ return formatTypeWithPrecisionAndScale(typeName, precision, scale);
212
+ }
213
+ }
214
+
215
+ // Append the limit to types such as integers and strings
216
+ protected static String formatTypeWithLimit(final String type, final int limit ) {
217
+
218
+ if ( limit <= 0 ) return type;
219
+
220
+ final StringBuilder typeStr = new StringBuilder();
221
+
222
+ typeStr.append(type).append('(').append(limit).append(')');
223
+
224
+ return typeStr.toString();
225
+ }
226
+
227
+ protected static String formatTypeWithPrecision(final String type, final int precision) {
228
+
229
+ if ( precision < 0 ) return type;
230
+
231
+ final StringBuilder typeStr = new StringBuilder().append(type);
232
+ typeStr.append('(').append(precision).append(')');
233
+
234
+ return typeStr.toString();
235
+ }
236
+
237
+ protected static String formatTypeWithPrecisionMax(final String type, final String precision) {
238
+
239
+ final StringBuilder typeStr = new StringBuilder().append(type);
240
+ typeStr.append('(').append(precision).append(')');
241
+
242
+ return typeStr.toString();
243
+ }
244
+
245
+ // differs from method in parent class because this appends scale 0
246
+ protected static String formatTypeWithPrecisionAndScale(
247
+ final String type, final int precision, final int scale) {
248
+
249
+ if ( precision <= 0 ) return type;
250
+
251
+ final StringBuilder typeStr = new StringBuilder().append(type);
252
+ typeStr.append('(').append(precision);
253
+ if ( scale >= 0 ) typeStr.append(',').append(scale);
254
+ return typeStr.append(')').toString();
255
+ }
256
+
257
+ // Overrides method in parent, for some reason the method in parent
258
+ // converts fractional seconds with padded zeros (e.g. 000xxxyyy)
259
+ // maybe a bug in Jruby strftime('%6N') ?
260
+ @Override
261
+ protected IRubyObject timestampToRuby(ThreadContext context, Ruby runtime, ResultSet resultSet, int column) throws SQLException {
262
+
263
+ final String value = resultSet.getString(column);
264
+
265
+ if (value == null) return context.nil;
266
+
267
+ return DateTimeUtils.parseDateTime(context, value, getDefaultTimeZone(context));
268
+ }
269
+
270
+ // For some reason DateTimeUtils.parseTime does not work, similar issue
271
+ // seen in timestampToRuby, fractional seconds padded with zeros.
272
+ @Override
273
+ protected IRubyObject timeToRuby(ThreadContext context, Ruby runtime, ResultSet resultSet, int column) throws SQLException {
274
+ final String value = resultSet.getString(column);
275
+
276
+ if ( value == null ) return context.nil;
277
+
278
+ final String datetime_value = "2000-01-01 " + value;
279
+
280
+ return DateTimeUtils.parseDateTime(context, datetime_value, getDefaultTimeZone(context));
281
+ }
282
+
283
+ // This overrides method in parent because of the issue in prepared
284
+ // statements when setting datatime data types, the mssql-jdbc
285
+ // uses datetime2 to cast datetime.
286
+ //
287
+ // More at: https://github.com/Microsoft/mssql-jdbc/issues/443
288
+ @Override
289
+ protected void setTimestampParameter(final ThreadContext context,
290
+ final Connection connection, final PreparedStatement statement,
291
+ final int index, IRubyObject value,
292
+ final IRubyObject attribute, final int type) throws SQLException {
293
+ String tsString = DateTimeUtils.timestampTimeToString(context, value, getDefaultTimeZone(context), false);
294
+
295
+ statement.setObject(index, tsString, Types.NVARCHAR);
296
+ }
297
+
298
+ // Handle more fractional second precision than (default) 59.123 only
299
+ @Override
300
+ protected void setTimeParameter(final ThreadContext context,
301
+ final Connection connection, final PreparedStatement statement,
302
+ final int index, IRubyObject value,
303
+ final IRubyObject attribute, final int type) throws SQLException {
304
+ String timeStr = DateTimeUtils.timeString(context, value, getDefaultTimeZone(context), true);
305
+ statement.setObject(index, timeStr, Types.NVARCHAR);
306
+ }
307
+
308
+ // Overrides the method in parent, we only remove the savepoint
309
+ // from the getSavepoints Map
310
+ @JRubyMethod(name = "release_savepoint", required = 1)
311
+ public IRubyObject release_savepoint(final ThreadContext context, final IRubyObject name) {
312
+ if (name == context.nil) throw context.runtime.newArgumentError("nil savepoint name given");
313
+
314
+ final Connection connection = getConnection(true);
315
+
316
+ Object savepoint = getSavepoints(context).remove(name);
317
+
318
+ if (savepoint == null) throw newSavepointNotSetError(context, name, "release");
319
+
320
+ // NOTE: RubyHash.remove does not convert to Java as get does :
321
+ if (!(savepoint instanceof Savepoint)) {
322
+ savepoint = ((IRubyObject) savepoint).toJava(Savepoint.class);
323
+ }
324
+
325
+ // The 'releaseSavepoint' method is not currently supported
326
+ // by the Microsoft SQL Server JDBC Driver
327
+ // connection.releaseSavepoint((Savepoint) savepoint);
328
+ return context.nil;
329
+ }
330
+
331
+ //----------------------------------------------------------------
332
+ // read_uncommitted: "READ UNCOMMITTED",
333
+ // read_committed: "READ COMMITTED",
334
+ // repeatable_read: "REPEATABLE READ",
335
+ // serializable: "SERIALIZABLE"
336
+ // snapshot: "SNAPSHOT"
337
+ //
338
+
339
+ // Overrides method in parent only because it needs to call static method of child class
340
+ protected void setTransactionIsolation(final ThreadContext context, final Connection connection,
341
+ final IRubyObject isolation) throws SQLException {
342
+ final int level = mapTransactionIsolationLevel(isolation);
343
+ try {
344
+ connection.setTransactionIsolation(level);
345
+ }
346
+ catch (SQLException e) {
347
+ RubyClass txError = ActiveRecord(context).getClass("TransactionIsolationError");
348
+ if ( txError != null ) throw wrapException(context, txError, e);
349
+ throw e; // let it roll - will be wrapped into a JDBCError (non 4.0)
350
+ }
351
+ }
352
+
353
+ // Overrides method in parent only because it needs to call static method of child class
354
+ @JRubyMethod(name = "transaction_isolation", alias = "get_transaction_isolation")
355
+ public IRubyObject get_transaction_isolation(final ThreadContext context) {
356
+ return withConnection(context, new Callable<IRubyObject>() {
357
+ public IRubyObject call(final Connection connection) throws SQLException {
358
+ final int level = connection.getTransactionIsolation();
359
+ final String isolationSymbol = formatTransactionIsolationLevel(level);
360
+ if ( isolationSymbol == null ) return context.nil;
361
+ return context.runtime.newSymbol(isolationSymbol);
362
+ }
363
+ });
364
+ }
365
+
366
+ // Overrides method in parent only because it needs to call static method of child class
367
+ @JRubyMethod(name = "transaction_isolation=", alias = "set_transaction_isolation")
368
+ public IRubyObject set_transaction_isolation(final ThreadContext context, final IRubyObject isolation) {
369
+ return withConnection(context, new Callable<IRubyObject>() {
370
+ public IRubyObject call(final Connection connection) throws SQLException {
371
+ final int level;
372
+
373
+ if ( isolation.isNil() ) {
374
+ level = connection.getMetaData().getDefaultTransactionIsolation();
375
+ } else {
376
+ level = mapTransactionIsolationLevel(isolation);
377
+ }
378
+
379
+ connection.setTransactionIsolation(level);
380
+
381
+ final String isolationSymbol = formatTransactionIsolationLevel(level);
382
+ if ( isolationSymbol == null ) return context.nil;
383
+ return context.runtime.newSymbol(isolationSymbol);
384
+ }
385
+ });
386
+ }
387
+
388
+ // hide parent method to support mssql specific transaction isolation level
389
+ public static String formatTransactionIsolationLevel(final int level) {
390
+ if ( level == Connection.TRANSACTION_READ_UNCOMMITTED ) return "read_uncommitted"; // 1
391
+ if ( level == Connection.TRANSACTION_READ_COMMITTED ) return "read_committed"; // 2
392
+ if ( level == Connection.TRANSACTION_REPEATABLE_READ ) return "repeatable_read"; // 4
393
+ if ( level == Connection.TRANSACTION_SERIALIZABLE ) return "serializable"; // 8
394
+ // this is alternative to SQLServerConnection.TRANSACTION_SNAPSHOT
395
+ if ( level == Connection.TRANSACTION_READ_COMMITTED + 4094) return "snapshot";
396
+ if ( level == 0 ) return null;
397
+ throw new IllegalArgumentException("unexpected transaction isolation level: " + level);
398
+ }
399
+
400
+
401
+ // hide parent method to support mssql specific transaction isolation level
402
+ public static int mapTransactionIsolationLevel(final IRubyObject isolation) {
403
+ final Object isolationString;
404
+
405
+ if ( isolation instanceof RubySymbol ) {
406
+ isolationString = ((RubySymbol) isolation).asJavaString(); // RubySymbol (interned)
407
+ } else {
408
+ isolationString = isolation.asString().toString().toLowerCase(Locale.ENGLISH).intern();
409
+ }
410
+
411
+ if ( isolationString == "read_uncommitted" ) return Connection.TRANSACTION_READ_UNCOMMITTED; // 1
412
+ if ( isolationString == "read_committed" ) return Connection.TRANSACTION_READ_COMMITTED; // 2
413
+ if ( isolationString == "repeatable_read" ) return Connection.TRANSACTION_REPEATABLE_READ; // 4
414
+ if ( isolationString == "serializable" ) return Connection.TRANSACTION_SERIALIZABLE; // 8
415
+ // this is alternative to SQLServerConnection.TRANSACTION_SNAPSHOT
416
+ if ( isolationString == "snapshot" ) return Connection.TRANSACTION_READ_COMMITTED + 4094;
417
+
418
+ throw new IllegalArgumentException(
419
+ "unexpected isolation level: " + isolation + " (" + isolationString + ")"
420
+ );
421
+ }
422
+
423
+ // overrides method in parent for database specific types
424
+ protected String mapTypeToString(final IRubyObject type) {
425
+ final String typeStr = type.asJavaString();
426
+
427
+ if (typeStr == "smalldatetime") return "datetime";
428
+
429
+ return typeStr;
430
+ }
431
+
432
+ // Get MSSQL major version, 8, 9, 10, 11, 12, etc.
433
+ @JRubyMethod
434
+ public IRubyObject database_major_version(final ThreadContext context) throws SQLException {
435
+ return withConnection(context, new Callable<IRubyObject>() {
436
+ public IRubyObject call(final Connection connection) throws SQLException {
437
+ final DatabaseMetaData metaData = connection.getMetaData();
438
+
439
+ return context.runtime.newFixnum( metaData.getDatabaseMajorVersion() );
440
+ }
441
+ });
442
+ }
443
+
444
+ /**
445
+ * Microsoft SQL 2000+ support schemas
446
+ */
447
+ @Override
448
+ protected boolean databaseSupportsSchemas() {
449
+ return true;
450
+ }
451
+
452
+ /**
453
+ * Treat LONGVARCHAR as CLOB on MSSQL for purposes of converting a JDBC value to Ruby.
454
+ */
455
+ @Override
456
+ protected IRubyObject jdbcToRuby(
457
+ final ThreadContext context, final Ruby runtime,
458
+ final int column, int type, final ResultSet resultSet)
459
+ throws SQLException {
460
+ if ( type == Types.LONGVARCHAR || type == Types.LONGNVARCHAR ) type = Types.CLOB;
461
+ return super.jdbcToRuby(context, runtime, column, type, resultSet);
462
+ }
463
+
464
+ @Override
465
+ protected ColumnData[] extractColumns(final ThreadContext context,
466
+ final Connection connection, final ResultSet resultSet,
467
+ final boolean downCase) throws SQLException {
468
+ return filterRowNumFromColumns( super.extractColumns(context, connection, resultSet, downCase) );
469
+ }
470
+
471
+ /**
472
+ * Filter out the <tt>_row_num</tt> column from results.
473
+ */
474
+ private static ColumnData[] filterRowNumFromColumns(final ColumnData[] columns) {
475
+ for ( int i = 0; i < columns.length; i++ ) {
476
+ if ( "_row_num".equals( columns[i].getName() ) ) {
477
+ final ColumnData[] filtered = new ColumnData[columns.length - 1];
478
+
479
+ if ( i > 0 ) {
480
+ System.arraycopy(columns, 0, filtered, 0, i);
481
+ }
482
+
483
+ if ( i + 1 < columns.length ) {
484
+ System.arraycopy(columns, i + 1, filtered, i, columns.length - (i + 1));
485
+ }
486
+
487
+ return filtered;
488
+ }
489
+ }
490
+ return columns;
491
+ }
492
+
493
+ // internal helper not meant as a "public" API - used in one place thus every
494
+ @JRubyMethod(name = "jtds_driver?")
495
+ public RubyBoolean jtds_driver_p(final ThreadContext context) throws SQLException {
496
+ // "jTDS Type 4 JDBC Driver for MS SQL Server and Sybase"
497
+ // SQLJDBC: "Microsoft JDBC Driver 4.0 for SQL Server"
498
+ return withConnection(context, new Callable<RubyBoolean>() {
499
+ // NOTE: only used in one place for now (on release_savepoint) ...
500
+ // might get optimized to only happen once since driver won't change
501
+ public RubyBoolean call(final Connection connection) throws SQLException {
502
+ final String driver = connection.getMetaData().getDriverName();
503
+ return context.getRuntime().newBoolean( driver.indexOf("jTDS") >= 0 );
504
+ }
505
+ });
506
+ }
507
+
508
+ }