neo-activerecord-jdbc-adapter 5.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +33 -0
  3. data/.travis.yml +438 -0
  4. data/.yardopts +4 -0
  5. data/Appraisals +41 -0
  6. data/CONTRIBUTING.md +44 -0
  7. data/Gemfile +62 -0
  8. data/History.md +1191 -0
  9. data/LICENSE.txt +25 -0
  10. data/README.md +266 -0
  11. data/RUNNING_TESTS.md +88 -0
  12. data/Rakefile +100 -0
  13. data/Rakefile.jdbc +20 -0
  14. data/activerecord-jdbc-adapter.gemspec +42 -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/oracle_adapter.rb +1 -0
  29. data/lib/active_record/connection_adapters/postgresql_adapter.rb +1 -0
  30. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -0
  31. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +1 -0
  32. data/lib/activerecord-jdbc-adapter.rb +1 -0
  33. data/lib/arel/visitors/compat.rb +60 -0
  34. data/lib/arel/visitors/db2.rb +137 -0
  35. data/lib/arel/visitors/derby.rb +112 -0
  36. data/lib/arel/visitors/firebird.rb +79 -0
  37. data/lib/arel/visitors/h2.rb +25 -0
  38. data/lib/arel/visitors/hsqldb.rb +32 -0
  39. data/lib/arel/visitors/postgresql_jdbc.rb +6 -0
  40. data/lib/arel/visitors/sql_server.rb +225 -0
  41. data/lib/arel/visitors/sql_server/ng42.rb +293 -0
  42. data/lib/arjdbc.rb +19 -0
  43. data/lib/arjdbc/abstract/database_statements.rb +92 -0
  44. data/lib/arjdbc/abstract/transaction_support.rb +86 -0
  45. data/lib/arjdbc/common_jdbc_methods.rb +13 -0
  46. data/lib/arjdbc/db2.rb +4 -0
  47. data/lib/arjdbc/db2/adapter.rb +789 -0
  48. data/lib/arjdbc/db2/as400.rb +130 -0
  49. data/lib/arjdbc/db2/column.rb +167 -0
  50. data/lib/arjdbc/db2/connection_methods.rb +44 -0
  51. data/lib/arjdbc/derby.rb +3 -0
  52. data/lib/arjdbc/derby/active_record_patch.rb +13 -0
  53. data/lib/arjdbc/derby/adapter.rb +556 -0
  54. data/lib/arjdbc/derby/connection_methods.rb +20 -0
  55. data/lib/arjdbc/derby/schema_creation.rb +15 -0
  56. data/lib/arjdbc/discover.rb +115 -0
  57. data/lib/arjdbc/firebird.rb +4 -0
  58. data/lib/arjdbc/firebird/adapter.rb +434 -0
  59. data/lib/arjdbc/firebird/connection_methods.rb +23 -0
  60. data/lib/arjdbc/h2.rb +3 -0
  61. data/lib/arjdbc/h2/adapter.rb +303 -0
  62. data/lib/arjdbc/h2/connection_methods.rb +27 -0
  63. data/lib/arjdbc/hsqldb.rb +3 -0
  64. data/lib/arjdbc/hsqldb/adapter.rb +297 -0
  65. data/lib/arjdbc/hsqldb/connection_methods.rb +28 -0
  66. data/lib/arjdbc/hsqldb/explain_support.rb +35 -0
  67. data/lib/arjdbc/hsqldb/schema_creation.rb +11 -0
  68. data/lib/arjdbc/informix.rb +5 -0
  69. data/lib/arjdbc/informix/adapter.rb +162 -0
  70. data/lib/arjdbc/informix/connection_methods.rb +9 -0
  71. data/lib/arjdbc/jdbc.rb +59 -0
  72. data/lib/arjdbc/jdbc/adapter.rb +899 -0
  73. data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
  74. data/lib/arjdbc/jdbc/adapter_require.rb +46 -0
  75. data/lib/arjdbc/jdbc/base_ext.rb +44 -0
  76. data/lib/arjdbc/jdbc/callbacks.rb +51 -0
  77. data/lib/arjdbc/jdbc/column.rb +97 -0
  78. data/lib/arjdbc/jdbc/connection.rb +133 -0
  79. data/lib/arjdbc/jdbc/connection_methods.rb +36 -0
  80. data/lib/arjdbc/jdbc/driver.rb +43 -0
  81. data/lib/arjdbc/jdbc/extension.rb +59 -0
  82. data/lib/arjdbc/jdbc/java.rb +15 -0
  83. data/lib/arjdbc/jdbc/jdbc.rake +4 -0
  84. data/lib/arjdbc/jdbc/quoted_primary_key.rb +28 -0
  85. data/lib/arjdbc/jdbc/railtie.rb +2 -0
  86. data/lib/arjdbc/jdbc/rake_tasks.rb +3 -0
  87. data/lib/arjdbc/jdbc/serialized_attributes_helper.rb +3 -0
  88. data/lib/arjdbc/jdbc/type_cast.rb +166 -0
  89. data/lib/arjdbc/jdbc/type_converter.rb +142 -0
  90. data/lib/arjdbc/mimer.rb +3 -0
  91. data/lib/arjdbc/mimer/adapter.rb +142 -0
  92. data/lib/arjdbc/mssql.rb +7 -0
  93. data/lib/arjdbc/mssql/adapter.rb +808 -0
  94. data/lib/arjdbc/mssql/column.rb +200 -0
  95. data/lib/arjdbc/mssql/connection_methods.rb +79 -0
  96. data/lib/arjdbc/mssql/explain_support.rb +99 -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/types.rb +343 -0
  100. data/lib/arjdbc/mssql/utils.rb +82 -0
  101. data/lib/arjdbc/mysql.rb +3 -0
  102. data/lib/arjdbc/mysql/adapter.rb +1006 -0
  103. data/lib/arjdbc/mysql/bulk_change_table.rb +150 -0
  104. data/lib/arjdbc/mysql/column.rb +162 -0
  105. data/lib/arjdbc/mysql/connection_methods.rb +145 -0
  106. data/lib/arjdbc/mysql/explain_support.rb +82 -0
  107. data/lib/arjdbc/mysql/schema_creation.rb +58 -0
  108. data/lib/arjdbc/oracle.rb +4 -0
  109. data/lib/arjdbc/oracle/adapter.rb +952 -0
  110. data/lib/arjdbc/oracle/column.rb +126 -0
  111. data/lib/arjdbc/oracle/connection_methods.rb +21 -0
  112. data/lib/arjdbc/postgresql.rb +3 -0
  113. data/lib/arjdbc/postgresql/_bc_time_cast_patch.rb +21 -0
  114. data/lib/arjdbc/postgresql/adapter.rb +825 -0
  115. data/lib/arjdbc/postgresql/base/array_decoder.rb +26 -0
  116. data/lib/arjdbc/postgresql/base/array_encoder.rb +25 -0
  117. data/lib/arjdbc/postgresql/base/array_parser.rb +95 -0
  118. data/lib/arjdbc/postgresql/base/pgconn.rb +11 -0
  119. data/lib/arjdbc/postgresql/column.rb +51 -0
  120. data/lib/arjdbc/postgresql/connection_methods.rb +54 -0
  121. data/lib/arjdbc/postgresql/name.rb +24 -0
  122. data/lib/arjdbc/postgresql/oid_types.rb +178 -0
  123. data/lib/arjdbc/railtie.rb +11 -0
  124. data/lib/arjdbc/sqlite3.rb +3 -0
  125. data/lib/arjdbc/sqlite3/adapter.rb +703 -0
  126. data/lib/arjdbc/sqlite3/connection_methods.rb +40 -0
  127. data/lib/arjdbc/sybase.rb +2 -0
  128. data/lib/arjdbc/sybase/adapter.rb +47 -0
  129. data/lib/arjdbc/tasks.rb +13 -0
  130. data/lib/arjdbc/tasks/database_tasks.rb +54 -0
  131. data/lib/arjdbc/tasks/databases.rake +91 -0
  132. data/lib/arjdbc/tasks/databases3.rake +215 -0
  133. data/lib/arjdbc/tasks/databases4.rake +39 -0
  134. data/lib/arjdbc/tasks/db2_database_tasks.rb +104 -0
  135. data/lib/arjdbc/tasks/derby_database_tasks.rb +95 -0
  136. data/lib/arjdbc/tasks/h2_database_tasks.rb +31 -0
  137. data/lib/arjdbc/tasks/hsqldb_database_tasks.rb +70 -0
  138. data/lib/arjdbc/tasks/jdbc_database_tasks.rb +169 -0
  139. data/lib/arjdbc/tasks/mssql_database_tasks.rb +46 -0
  140. data/lib/arjdbc/tasks/oracle/enhanced_structure_dump.rb +297 -0
  141. data/lib/arjdbc/tasks/oracle_database_tasks.rb +65 -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 +8 -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 +121 -0
  157. data/rakelib/bundler_ext.rb +11 -0
  158. data/rakelib/compile.rake +62 -0
  159. data/rakelib/db.rake +58 -0
  160. data/rakelib/rails.rake +75 -0
  161. data/src/java/arjdbc/ArJdbcModule.java +178 -0
  162. data/src/java/arjdbc/db2/DB2Module.java +71 -0
  163. data/src/java/arjdbc/db2/DB2RubyJdbcConnection.java +142 -0
  164. data/src/java/arjdbc/derby/DerbyModule.java +179 -0
  165. data/src/java/arjdbc/derby/DerbyRubyJdbcConnection.java +164 -0
  166. data/src/java/arjdbc/firebird/FirebirdRubyJdbcConnection.java +190 -0
  167. data/src/java/arjdbc/h2/H2Module.java +44 -0
  168. data/src/java/arjdbc/h2/H2RubyJdbcConnection.java +67 -0
  169. data/src/java/arjdbc/hsqldb/HSQLDBModule.java +68 -0
  170. data/src/java/arjdbc/informix/InformixRubyJdbcConnection.java +75 -0
  171. data/src/java/arjdbc/jdbc/AdapterJavaService.java +45 -0
  172. data/src/java/arjdbc/jdbc/Callable.java +44 -0
  173. data/src/java/arjdbc/jdbc/JdbcConnectionFactory.java +45 -0
  174. data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +3616 -0
  175. data/src/java/arjdbc/jdbc/SQLBlock.java +54 -0
  176. data/src/java/arjdbc/mssql/MSSQLModule.java +102 -0
  177. data/src/java/arjdbc/mssql/MSSQLRubyJdbcConnection.java +195 -0
  178. data/src/java/arjdbc/mysql/MySQLModule.java +147 -0
  179. data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +397 -0
  180. data/src/java/arjdbc/oracle/OracleModule.java +75 -0
  181. data/src/java/arjdbc/oracle/OracleRubyJdbcConnection.java +465 -0
  182. data/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java +752 -0
  183. data/src/java/arjdbc/sqlite3/SQLite3Module.java +78 -0
  184. data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +351 -0
  185. data/src/java/arjdbc/util/CallResultSet.java +826 -0
  186. data/src/java/arjdbc/util/QuotingUtils.java +111 -0
  187. metadata +255 -0
@@ -0,0 +1,54 @@
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
+
27
+ package arjdbc.jdbc;
28
+
29
+ import java.sql.Connection;
30
+ import java.sql.ResultSet;
31
+ import java.sql.SQLException;
32
+ import java.sql.Statement;
33
+
34
+ /**
35
+ * @deprecated implement a {@link Callable} directly instead.
36
+ *
37
+ * @author nicksieger
38
+ */
39
+ @Deprecated
40
+ public abstract class SQLBlock implements Callable {
41
+
42
+ /**
43
+ * @see Callable#call(Connection)
44
+ */
45
+ public abstract Object call(final Connection connection) throws SQLException;
46
+
47
+ public void close(Statement statement) {
48
+ RubyJdbcConnection.close(statement);
49
+ }
50
+
51
+ public void close(ResultSet resultSet) {
52
+ RubyJdbcConnection.close(resultSet);
53
+ }
54
+ }
@@ -0,0 +1,102 @@
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.quoteCharWith;
29
+ import static arjdbc.util.QuotingUtils.quoteSingleQuotesWithFallback;
30
+
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.Visibility;
36
+ import org.jruby.runtime.builtin.IRubyObject;
37
+ import org.jruby.util.ByteList;
38
+
39
+ /**
40
+ * ArJdbc::MSSQL
41
+ *
42
+ * @author kares
43
+ */
44
+ public class MSSQLModule {
45
+
46
+ public static RubyModule load(final RubyModule arJdbc) {
47
+ RubyModule mssql = arJdbc.defineModuleUnder("MSSQL");
48
+ mssql.defineAnnotatedMethods( MSSQLModule.class );
49
+ return mssql;
50
+ }
51
+
52
+ @JRubyMethod(name = "quote_string", required = 1)
53
+ public static IRubyObject quote_string(final ThreadContext context,
54
+ final IRubyObject self, final IRubyObject string) {
55
+ return quoteSingleQuotesWithFallback(context, string);
56
+ }
57
+
58
+ @JRubyMethod(name = "quoted_true", required = 0)
59
+ public static IRubyObject quoted_true(final ThreadContext context,
60
+ final IRubyObject self) {
61
+ return RubyString.newString(context.getRuntime(), BYTES_1);
62
+ }
63
+
64
+ @JRubyMethod(name = "quoted_false", required = 0)
65
+ public static IRubyObject quoted_false(final ThreadContext context,
66
+ final IRubyObject self) {
67
+ return RubyString.newString(context.getRuntime(), BYTES_0);
68
+ }
69
+
70
+ // part =~ /^\[.*\]$/ ? part : "[#{part.gsub(']', ']]')}]"
71
+ @SuppressWarnings("deprecation")
72
+ @JRubyMethod(required = 1, visibility = Visibility.PRIVATE)
73
+ public static IRubyObject quote_name_part(final ThreadContext context,
74
+ final IRubyObject self, final IRubyObject part) {
75
+
76
+ final RubyString partString = ((RubyString) part);
77
+ final ByteList str = partString.getByteList();
78
+ if ( str.charAt(0) == '[' && str.charAt(str.length() - 1) == ']' ) {
79
+ return part; // part =~ /^\[.*\]$/ ? part
80
+ }
81
+ final RubyString quotedString = // part.gsub(']', ']]')
82
+ quoteCharWith(context, partString, ']', ']', 1, 4 + 1);
83
+ if ( quotedString == partString ) {
84
+ final int realSize = str.getRealSize();
85
+ final ByteList quoted = new ByteList(
86
+ new byte[realSize + 2], partString.getEncoding(), false
87
+ );
88
+ quoted.begin = 0; quoted.realSize = 0;
89
+ quoted.append('[');
90
+ quoted.append(str.unsafeBytes(), str.getBegin(), realSize);
91
+ quoted.append(']');
92
+ return context.getRuntime().newString(quoted);
93
+ }
94
+ // we got a new string with a reserve of 1 byte front and back :
95
+ final ByteList quoted = quotedString.getByteList();
96
+ quoted.begin = 0; // setBegin invalidates
97
+ quoted.bytes[0] = '['; quoted.realSize++;
98
+ quoted.append(']');
99
+ return quotedString;
100
+ }
101
+
102
+ }
@@ -0,0 +1,195 @@
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
+
31
+ import java.sql.Connection;
32
+ import java.sql.DatabaseMetaData;
33
+ import java.sql.ResultSet;
34
+ import java.sql.SQLException;
35
+ import java.sql.Types;
36
+
37
+ import org.jcodings.specific.UTF8Encoding;
38
+
39
+ import org.jruby.Ruby;
40
+ import org.jruby.RubyArray;
41
+ import org.jruby.RubyClass;
42
+ import org.jruby.RubyString;
43
+ import org.jruby.anno.JRubyMethod;
44
+ import org.jruby.runtime.ObjectAllocator;
45
+ import org.jruby.runtime.ThreadContext;
46
+ import org.jruby.runtime.builtin.IRubyObject;
47
+ import org.jruby.util.ByteList;
48
+
49
+ /**
50
+ *
51
+ * @author nicksieger
52
+ */
53
+ public class MSSQLRubyJdbcConnection extends RubyJdbcConnection {
54
+ private static final long serialVersionUID = -745716565005219263L;
55
+
56
+ protected MSSQLRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
57
+ super(runtime, metaClass);
58
+ }
59
+
60
+ public static RubyClass createMSSQLJdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
61
+ final RubyClass clazz = getConnectionAdapters(runtime). // ActiveRecord::ConnectionAdapters
62
+ defineClassUnder("MSSQLJdbcConnection", jdbcConnection, ALLOCATOR);
63
+ clazz.defineAnnotatedMethods(MSSQLRubyJdbcConnection.class);
64
+ getConnectionAdapters(runtime).setConstant("MssqlJdbcConnection", clazz); // backwards-compat
65
+ return clazz;
66
+ }
67
+
68
+ private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
69
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
70
+ return new MSSQLRubyJdbcConnection(runtime, klass);
71
+ }
72
+ };
73
+
74
+ private static final byte[] EXEC = new byte[] { 'e', 'x', 'e', 'c' };
75
+
76
+ @JRubyMethod(name = "exec?", required = 1, meta = true, frame = false)
77
+ public static IRubyObject exec_p(ThreadContext context, IRubyObject self, IRubyObject sql) {
78
+ final ByteList sqlBytes = sql.convertToString().getByteList();
79
+ return context.getRuntime().newBoolean( startsWithIgnoreCase(sqlBytes, EXEC) );
80
+ }
81
+
82
+ @Override
83
+ protected RubyArray mapTables(final Ruby runtime, final DatabaseMetaData metaData,
84
+ final String catalog, final String schemaPattern, final String tablePattern,
85
+ final ResultSet tablesSet) throws SQLException, IllegalStateException {
86
+
87
+ final RubyArray tables = runtime.newArray();
88
+
89
+ while ( tablesSet.next() ) {
90
+ String schema = tablesSet.getString(TABLES_TABLE_SCHEM);
91
+ if ( schema != null ) schema = schema.toLowerCase();
92
+ // Under MS-SQL, don't return system tables/views unless explicitly asked for :
93
+ if ( schemaPattern == null &&
94
+ ( "sys".equals(schema) || "information_schema".equals(schema) ) ) {
95
+ continue;
96
+ }
97
+ String name = tablesSet.getString(TABLES_TABLE_NAME);
98
+ if ( name == null ) {
99
+ // NOTE: seems there's a jTDS but when doing getColumns while
100
+ // EXPLAIN is on (e.g. `SET SHOWPLAN_TEXT ON`) not returning
101
+ // correct result set with table info (null NAME, invalid CAT)
102
+ throw new IllegalStateException("got null name while matching table(s): [" +
103
+ catalog + "." + schemaPattern + "." + tablePattern + "] check " +
104
+ "if this happened during EXPLAIN (SET SHOWPLAN_TEXT ON) if so please try " +
105
+ "turning it off using the system property 'arjdbc.mssql.explain_support.disabled=true' " +
106
+ "or programatically by changing: `ArJdbc::MSSQL::ExplainSupport::DISABLED`");
107
+ }
108
+ name = caseConvertIdentifierForRails(metaData, name);
109
+ tables.add(RubyString.newUnicodeString(runtime, name));
110
+ }
111
+ return tables;
112
+ }
113
+
114
+ @Override
115
+ protected RubyArray mapColumnsResult(final ThreadContext context,
116
+ final DatabaseMetaData metaData, final TableName components, final ResultSet results)
117
+ throws SQLException {
118
+
119
+ final RubyClass Column = getJdbcColumnClass(context);
120
+ final boolean lookupCastType = Column.isMethodBound("cast_type", false);
121
+ // NOTE: MSSQL depends on Column#primary? no matter the AR version - thus always set @primary
122
+ return mapColumnsResult(context, metaData, components, results, Column, lookupCastType, true);
123
+ }
124
+
125
+ /**
126
+ * Microsoft SQL 2000+ support schemas
127
+ */
128
+ @Override
129
+ protected boolean databaseSupportsSchemas() {
130
+ return true;
131
+ }
132
+
133
+ /**
134
+ * Treat LONGVARCHAR as CLOB on MSSQL for purposes of converting a JDBC value to Ruby.
135
+ */
136
+ @Override
137
+ protected IRubyObject jdbcToRuby(
138
+ final ThreadContext context, final Ruby runtime,
139
+ final int column, int type, final ResultSet resultSet)
140
+ throws SQLException {
141
+ if ( type == Types.LONGVARCHAR || type == Types.LONGNVARCHAR ) type = Types.CLOB;
142
+ return super.jdbcToRuby(context, runtime, column, type, resultSet);
143
+ }
144
+
145
+ @Override
146
+ protected ColumnData[] extractColumns(final Ruby runtime,
147
+ final Connection connection, final ResultSet resultSet,
148
+ final boolean downCase) throws SQLException {
149
+ return filterRowNumFromColumns( super.extractColumns(runtime, connection, resultSet, downCase) );
150
+ }
151
+
152
+ private static final ByteList _row_num; // "_row_num"
153
+ static {
154
+ _row_num = new ByteList(new byte[] { '_','r','o','w','_','n','u','m' }, false);
155
+ _row_num.setEncoding(UTF8Encoding.INSTANCE);
156
+ }
157
+
158
+ /**
159
+ * Filter out the <tt>_row_num</tt> column from results.
160
+ */
161
+ private static ColumnData[] filterRowNumFromColumns(final ColumnData[] columns) {
162
+ for ( int i = 0; i < columns.length; i++ ) {
163
+ if ( _row_num.equal( columns[i].name.getByteList() ) ) {
164
+ final ColumnData[] filtered = new ColumnData[columns.length - 1];
165
+
166
+ if ( i > 0 ) {
167
+ System.arraycopy(columns, 0, filtered, 0, i);
168
+ }
169
+
170
+ if ( i + 1 < columns.length ) {
171
+ System.arraycopy(columns, i + 1, filtered, i, columns.length - (i + 1));
172
+ }
173
+
174
+ return filtered;
175
+ }
176
+ }
177
+ return columns;
178
+ }
179
+
180
+ // internal helper not meant as a "public" API - used in one place thus every
181
+ @JRubyMethod(name = "jtds_driver?")
182
+ public IRubyObject jtds_driver_p(final ThreadContext context) throws SQLException {
183
+ // "jTDS Type 4 JDBC Driver for MS SQL Server and Sybase"
184
+ // SQLJDBC: "Microsoft JDBC Driver 4.0 for SQL Server"
185
+ return withConnection(context, new Callable<IRubyObject>() {
186
+ // NOTE: only used in one place for now (on release_savepoint) ...
187
+ // might get optimized to only happen once since driver won't change
188
+ public IRubyObject call(final Connection connection) throws SQLException {
189
+ final String driver = connection.getMetaData().getDriverName();
190
+ return context.getRuntime().newBoolean( driver.indexOf("jTDS") >= 0 );
191
+ }
192
+ });
193
+ }
194
+
195
+ }
@@ -0,0 +1,147 @@
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
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining
7
+ * a copy of this software and associated documentation files (the
8
+ * "Software"), to deal in the Software without restriction, including
9
+ * without limitation the rights to use, copy, modify, merge, publish,
10
+ * distribute, sublicense, and/or sell copies of the Software, and to
11
+ * permit persons to whom the Software is furnished to do so, subject to
12
+ * the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be
15
+ * included in all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ ***** END LICENSE BLOCK *****/
25
+
26
+ package arjdbc.mysql;
27
+
28
+ import static arjdbc.util.QuotingUtils.BYTES_0;
29
+ import static arjdbc.util.QuotingUtils.BYTES_1;
30
+ import static arjdbc.util.QuotingUtils.quoteCharWith;
31
+
32
+ import org.jcodings.specific.UTF8Encoding;
33
+
34
+ import org.jruby.Ruby;
35
+ import org.jruby.RubyModule;
36
+ import org.jruby.RubyString;
37
+ import org.jruby.anno.JRubyMethod;
38
+ import org.jruby.runtime.ThreadContext;
39
+ import org.jruby.runtime.builtin.IRubyObject;
40
+ import org.jruby.util.ByteList;
41
+
42
+ public class MySQLModule {
43
+
44
+ public static RubyModule load(final RubyModule arJdbc) {
45
+ RubyModule mysql = arJdbc.defineModuleUnder("MySQL");
46
+ mysql.defineAnnotatedMethods(MySQLModule.class);
47
+ return mysql;
48
+ }
49
+
50
+ //private final static byte[] ZERO = new byte[] {'\\','0'};
51
+ //private final static byte[] NEWLINE = new byte[] {'\\','n'};
52
+ //private final static byte[] CARRIAGE = new byte[] {'\\','r'};
53
+ //private final static byte[] ZED = new byte[] {'\\','Z'};
54
+ //private final static byte[] DBL = new byte[] {'\\','"'};
55
+ //private final static byte[] SINGLE = new byte[] {'\\','\''};
56
+ //private final static byte[] ESCAPE = new byte[] {'\\','\\'};
57
+
58
+ private static final int STRING_QUOTES_OPTIMISTIC_QUESS = 24;
59
+
60
+ @JRubyMethod(name = "quote_string", required = 1, frame = false)
61
+ public static IRubyObject quote_string(final ThreadContext context,
62
+ final IRubyObject recv, final IRubyObject string) {
63
+
64
+ final ByteList stringBytes = ((RubyString) string).getByteList();
65
+ final byte[] bytes = stringBytes.unsafeBytes();
66
+ final int begin = stringBytes.getBegin();
67
+ final int realSize = stringBytes.getRealSize();
68
+
69
+ ByteList quotedBytes = null; int appendFrom = begin;
70
+ for ( int i = begin; i < begin + realSize; i++ ) {
71
+ final byte byte2;
72
+ switch ( bytes[i] ) {
73
+ case 0 : byte2 = '0'; break;
74
+ case '\n' : byte2 = 'n'; break;
75
+ case '\r' : byte2 = 'r'; break;
76
+ case 26 : byte2 = 'Z'; break;
77
+ case '"' : byte2 = '"'; break;
78
+ case '\'' : byte2 = '\''; break;
79
+ case '\\' : byte2 = '\\'; break;
80
+ default : byte2 = 0;
81
+ }
82
+ if ( byte2 != 0 ) {
83
+ if ( quotedBytes == null ) {
84
+ quotedBytes = new ByteList(
85
+ new byte[realSize + STRING_QUOTES_OPTIMISTIC_QUESS],
86
+ stringBytes.getEncoding()
87
+ );
88
+ quotedBytes.setBegin(0);
89
+ quotedBytes.setRealSize(0);
90
+ } // copy string on-first quote we "optimize" for non-quoted
91
+ quotedBytes.append(bytes, appendFrom, i - appendFrom);
92
+ quotedBytes.append('\\').append(byte2);
93
+ appendFrom = i + 1;
94
+ }
95
+ }
96
+ if ( quotedBytes != null ) { // append what's left in the end :
97
+ quotedBytes.append(bytes, appendFrom, begin + realSize - appendFrom);
98
+ }
99
+ else return string; // nothing changed, can return original
100
+
101
+ final Ruby runtime = context.getRuntime();
102
+ final RubyString quoted = runtime.newString(quotedBytes);
103
+
104
+ quoted.associateEncoding(UTF8Encoding.INSTANCE);
105
+
106
+ return quoted;
107
+ }
108
+
109
+ @JRubyMethod(name = "quoted_true", required = 0, frame = false)
110
+ public static IRubyObject quoted_true(
111
+ final ThreadContext context,
112
+ final IRubyObject self) {
113
+ return RubyString.newString(context.getRuntime(), BYTES_1);
114
+ }
115
+
116
+ @JRubyMethod(name = "quoted_false", required = 0, frame = false)
117
+ public static IRubyObject quoted_false(
118
+ final ThreadContext context,
119
+ final IRubyObject self) {
120
+ return RubyString.newString(context.getRuntime(), BYTES_0);
121
+ }
122
+
123
+ @JRubyMethod(name = "quote_column_name", required = 1, frame = false)
124
+ public static IRubyObject quote_column_name(
125
+ final ThreadContext context,
126
+ final IRubyObject self,
127
+ final IRubyObject string) { // "`#{name.to_s.gsub('`', '``')}`"
128
+ RubyString origin = string.asString();
129
+ RubyString quoted = quoteCharWith(context, origin, '`', '`');
130
+ final ByteList bytes = quoted.getByteList();
131
+ if ( origin == quoted ) {
132
+ final RubyString result =
133
+ RubyString.newStringLight(context.getRuntime(), bytes.getRealSize() + 2, bytes.getEncoding());
134
+ final ByteList resultBytes = result.getByteList();
135
+ resultBytes.append( (byte) '`' );
136
+ resultBytes.append( bytes );
137
+ resultBytes.append( (byte) '`' );
138
+ return result;
139
+ }
140
+ else {
141
+ bytes.append( (byte) '`' );
142
+ bytes.prepend( (byte) '`' );
143
+ return quoted;
144
+ }
145
+ }
146
+
147
+ }