activerecord-jdbc-alt-adapter 50.3.0-java

Sign up to get free protection for your applications and to get access to all the features.
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,152 @@
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 org.jcodings.specific.UTF8Encoding;
29
+ import org.jruby.Ruby;
30
+ import org.jruby.RubyFixnum;
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
+ import org.jruby.util.ByteList;
37
+
38
+ import static arjdbc.util.QuotingUtils.BYTES_0;
39
+ import static arjdbc.util.QuotingUtils.BYTES_1;
40
+ import static arjdbc.util.QuotingUtils.quoteCharAndDecorateWith;
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
+ public static RubyModule load(final Ruby runtime) {
51
+ return load( arjdbc.ArJdbcModule.get(runtime) );
52
+ }
53
+
54
+ // ActiveRecord::ConnectionAdapters::MySQL::Quoting (almost all of)
55
+ // without the column/table name 'caches' replicated across threads
56
+
57
+ //private final static byte[] ZERO = new byte[] {'\\','0'};
58
+ //private final static byte[] NEWLINE = new byte[] {'\\','n'};
59
+ //private final static byte[] CARRIAGE = new byte[] {'\\','r'};
60
+ //private final static byte[] ZED = new byte[] {'\\','Z'};
61
+ //private final static byte[] DBL = new byte[] {'\\','"'};
62
+ //private final static byte[] SINGLE = new byte[] {'\\','\''};
63
+ //private final static byte[] ESCAPE = new byte[] {'\\','\\'};
64
+
65
+ private static final int STRING_QUOTES_OPTIMISTIC_QUESS = 24;
66
+
67
+ @JRubyMethod(name = "quote_string", required = 1)
68
+ public static IRubyObject quote_string(final ThreadContext context,
69
+ final IRubyObject recv, final IRubyObject string) {
70
+
71
+ final ByteList stringBytes = ((RubyString) string).getByteList();
72
+ final byte[] bytes = stringBytes.unsafeBytes();
73
+ final int begin = stringBytes.getBegin();
74
+ final int realSize = stringBytes.getRealSize();
75
+
76
+ ByteList quotedBytes = null; int appendFrom = begin;
77
+ for ( int i = begin; i < begin + realSize; i++ ) {
78
+ final byte byte2;
79
+ switch ( bytes[i] ) {
80
+ case 0 : byte2 = '0'; break;
81
+ case '\n' : byte2 = 'n'; break;
82
+ case '\r' : byte2 = 'r'; break;
83
+ case 26 : byte2 = 'Z'; break;
84
+ case '"' : byte2 = '"'; break;
85
+ case '\'' : byte2 = '\''; break;
86
+ case '\\' : byte2 = '\\'; break;
87
+ default : byte2 = 0;
88
+ }
89
+ if ( byte2 != 0 ) {
90
+ if ( quotedBytes == null ) {
91
+ quotedBytes = new ByteList(
92
+ new byte[realSize + STRING_QUOTES_OPTIMISTIC_QUESS],
93
+ stringBytes.getEncoding()
94
+ );
95
+ quotedBytes.setBegin(0);
96
+ quotedBytes.setRealSize(0);
97
+ } // copy string on-first quote we "optimize" for non-quoted
98
+ quotedBytes.append(bytes, appendFrom, i - appendFrom);
99
+ quotedBytes.append('\\').append(byte2);
100
+ appendFrom = i + 1;
101
+ }
102
+ }
103
+ if ( quotedBytes != null ) { // append what's left in the end :
104
+ quotedBytes.append(bytes, appendFrom, begin + realSize - appendFrom);
105
+ }
106
+ else return string; // nothing changed, can return original
107
+
108
+ final Ruby runtime = context.runtime;
109
+ final RubyString quoted = runtime.newString(quotedBytes);
110
+
111
+ quoted.associateEncoding(UTF8Encoding.INSTANCE);
112
+
113
+ return quoted;
114
+ }
115
+
116
+ @JRubyMethod(name = "quoted_true")
117
+ public static IRubyObject quoted_true(
118
+ final ThreadContext context,
119
+ final IRubyObject self) {
120
+ return RubyString.newString(context.runtime, BYTES_1).freeze(context);
121
+ }
122
+
123
+ @JRubyMethod(name = "quoted_false")
124
+ public static IRubyObject quoted_false(
125
+ final ThreadContext context,
126
+ final IRubyObject self) {
127
+ return RubyString.newString(context.runtime, BYTES_0).freeze(context);
128
+ }
129
+
130
+ @JRubyMethod(name = "unquoted_true")
131
+ public static IRubyObject unquoted_true(
132
+ final ThreadContext context,
133
+ final IRubyObject self) {
134
+ return RubyFixnum.one(context.runtime);
135
+ }
136
+
137
+ @JRubyMethod(name = "unquoted_false")
138
+ public static IRubyObject unquoted_false(
139
+ final ThreadContext context,
140
+ final IRubyObject self) {
141
+ return RubyFixnum.zero(context.runtime);
142
+ }
143
+
144
+ @JRubyMethod(name = "quote_column_name", required = 1)
145
+ public static IRubyObject quote_column_name(
146
+ final ThreadContext context,
147
+ final IRubyObject self,
148
+ final IRubyObject string) { // "`#{name.to_s.gsub('`', '``')}`"
149
+ return quoteCharAndDecorateWith(context, string.asString(), '`', '`', (byte) '`', (byte) '`');
150
+ }
151
+
152
+ }
@@ -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 context.runtime.newRaiseException(errorClass,
111
+ "MySQL adapter requires driver >= 5.0 got: " + major + "." + minor + "");
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
+ }