-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathMySQLRubyJdbcConnection.java
More file actions
328 lines (281 loc) · 13 KB
/
Copy pathMySQLRubyJdbcConnection.java
File metadata and controls
328 lines (281 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2012-2013 Karol Bucek <self@kares.org>
* Copyright (c) 2006-2010 Nick Sieger <nick@nicksieger.com>
* Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
* Copyright (c) 2008-2009 Thomas E Enebo <enebo@acm.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***** END LICENSE BLOCK *****/
package arjdbc.mysql;
import arjdbc.jdbc.Callable;
import arjdbc.jdbc.DriverWrapper;
import arjdbc.jdbc.RubyJdbcConnection;
import arjdbc.util.DateTimeUtils;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import org.jruby.*;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.SafePropertyAccessor;
import static arjdbc.util.StringHelper.newString;
/**
*
* @author nicksieger
*/
@org.jruby.anno.JRubyClass(name = "ActiveRecord::ConnectionAdapters::MySQLJdbcConnection")
public class MySQLRubyJdbcConnection extends RubyJdbcConnection {
private static final long serialVersionUID = -8842614212147138733L;
public MySQLRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
super(runtime, metaClass);
}
public static RubyClass createMySQLJdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
RubyClass clazz = getConnectionAdapters(runtime).
defineClassUnder("MySQLJdbcConnection", jdbcConnection, ALLOCATOR);
clazz.defineAnnotatedMethods(MySQLRubyJdbcConnection.class);
return clazz;
}
public static RubyClass load(final Ruby runtime) {
RubyClass jdbcConnection = getJdbcConnection(runtime);
return createMySQLJdbcConnectionClass(runtime, jdbcConnection);
}
protected static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new MySQLRubyJdbcConnection(runtime, klass);
}
};
@JRubyMethod
public IRubyObject query(final ThreadContext context, final IRubyObject sql) throws SQLException {
return execute_update(context, sql);
}
@JRubyMethod(name = { "full_version" })
public IRubyObject db_version(final ThreadContext context) {
return withConnection(context, (Callable<IRubyObject>) connection -> {
final DatabaseMetaData metaData = connection.getMetaData();
return context.runtime.newString(metaData.getDatabaseProductVersion());
});
}
@Override
protected DriverWrapper newDriverWrapper(final ThreadContext context, final String driver) {
DriverWrapper driverWrapper = super.newDriverWrapper(context, driver);
final java.sql.Driver jdbcDriver = driverWrapper.getDriverInstance();
if ( jdbcDriver.getClass().getName().startsWith("com.mysql.jdbc.") ) {
final int major = jdbcDriver.getMajorVersion();
final int minor = jdbcDriver.getMinorVersion();
if ( major < 5 ) {
final RubyClass errorClass = getConnectionNotEstablished(context.runtime);
throw context.runtime.newRaiseException(errorClass,
"MySQL adapter requires driver >= 5.0 got: " + major + "." + minor);
}
if ( major == 5 && minor < 1 ) { // need 5.1 for JDBC 4.0
// lightweight validation query: "/* ping */ SELECT 1"
setConfigValueIfNotSet(context, "connection_alive_sql", context.runtime.newString("/* ping */ SELECT 1"));
}
driverAdapter = new MySQLDriverAdapter(); // short-circuit
}
else {
driverAdapter = new DriverAdapter(); // short-circuit (MariaDB)
}
return driverWrapper;
}
@JRubyMethod(name = "ping")
public RubyBoolean db_ping(final ThreadContext context) {
final Connection connection = getConnection(true);
if (connection == null) return context.fals;
// NOTE: It seems only `connection.isValid(aliveTimeout)` is needed
// for JDBC 4.0 and up. https://jira.mariadb.org/browse/CONJ-51
return context.runtime.newBoolean(isConnectionValid(context, connection));
}
private static transient Class MYSQL_CONNECTION;
private static transient Boolean MYSQL_CONNECTION_FOUND;
private static boolean checkMySQLConnection(final Connection connection) {
Class mysqlDriverIface = MYSQL_CONNECTION;
if (mysqlDriverIface == null && MYSQL_CONNECTION_FOUND == null) {
try {
MYSQL_CONNECTION = Class.forName("com.mysql.jdbc.MySQLConnection", false, MySQLRubyJdbcConnection.class.getClassLoader());
MYSQL_CONNECTION_FOUND = Boolean.TRUE;
}
catch (ClassNotFoundException ex) {
MYSQL_CONNECTION_FOUND = Boolean.FALSE;
}
mysqlDriverIface = MYSQL_CONNECTION;
}
if (mysqlDriverIface == null) return false;
try {
return connection.isWrapperFor(mysqlDriverIface);
}
catch (SQLException ex) {
return false;
}
}
private boolean usingMySQLDriver() {
return checkMySQLConnection(getConnection(true));
}
private transient DriverAdapter driverAdapter;
// NOTE: currently un-used but we'll need it if we attempt to handle fast string extraction
private DriverAdapter getDriverAdapter() {
if (driverAdapter == null) {
driverAdapter = usingMySQLDriver() ? new MySQLDriverAdapter() : new DriverAdapter();
}
return driverAdapter;
}
private class DriverAdapter { // sensible driver without quirks (MariaDB)
// left in for encoding specific extraction from driver - would allow us 'fast' string byte[] extraction
}
private class MySQLDriverAdapter extends DriverAdapter { // Connector/J (bloated) 5.x version
// left in for encoding specific extraction from driver - would allow us 'fast' string byte[] extraction
}
@Override
protected boolean doExecute(final Statement statement, final String query) throws SQLException {
return statement.execute(query, Statement.RETURN_GENERATED_KEYS);
}
@Override
protected IRubyObject jdbcToRuby(final ThreadContext context, final Ruby runtime,
final int column, final int type, final ResultSet resultSet) throws SQLException {
if ( type == Types.BIT ) {
final int value = resultSet.getInt(column);
return resultSet.wasNull() ? context.nil : runtime.newFixnum(value);
}
return super.jdbcToRuby(context, runtime, column, type, resultSet);
}
@Override
protected void setTimeParameter(final ThreadContext context,
final Connection connection, final PreparedStatement statement,
final int index, IRubyObject value,
final IRubyObject attribute, final int type) throws SQLException {
// MySQL's TIME supports fractional seconds up-to nano precision: .000000
setTimestampParameter(context, connection, statement, index, value, attribute, type);
}
@Override
protected IRubyObject timeToRuby(final ThreadContext context,
final Ruby runtime, final ResultSet resultSet, final int column)
throws SQLException { // due MySQL's TIME precision (up to nanos)
final Timestamp value = resultSet.getTimestamp(column);
if ( value == null ) {
return resultSet.wasNull() ? context.nil : RubyString.newEmptyString(runtime);
}
if ( rawDateTime != null && rawDateTime) {
return RubyString.newString(runtime, DateTimeUtils.dummyTimeToString(value));
}
return DateTimeUtils.newDummyTime(context, value, getDefaultTimeZone(context));
}
@Override
protected IRubyObject timestampToRuby(final ThreadContext context,
final Ruby runtime, final ResultSet resultSet, final int column)
throws SQLException {
final Timestamp value;
try {
value = resultSet.getTimestamp(column);
}
catch (SQLException e) {
if (e.getMessage().contains("HOUR_OF_DAY")) {
return stringToRuby(context, runtime, resultSet, column);
}
else {
throw e;
}
}
if ( value == null ) {
return resultSet.wasNull() ? context.nil : RubyString.newEmptyString(runtime);
}
if ( rawDateTime != null && rawDateTime) {
return RubyString.newString(runtime, DateTimeUtils.timestampToString(value));
}
// NOTE: with 'raw' String AR's Type::DateTime does put the time in proper time-zone
// while when returning a Time object it just adjusts usec (apply_seconds_precision)
// yet for custom SELECTs to work (SELECT created_at ... ) and for compatibility we
// should be returning Time (by default) - AR does this by adjusting mysql2/pg returns
return DateTimeUtils.newTime(context, value, getDefaultTimeZone(context));
}
@Override
protected IRubyObject streamToRuby(final ThreadContext context,
final Ruby runtime, final ResultSet resultSet, final int column)
throws SQLException {
final byte[] bytes = resultSet.getBytes(column);
if ( bytes == null /* || resultSet.wasNull() */ ) return context.nil;
return newString(runtime, bytes);
}
// MySQL does never storesUpperCaseIdentifiers() :
// storesLowerCaseIdentifiers() depends on "lower_case_table_names" server variable
@Override
protected final String caseConvertIdentifierForRails(final Connection connection, final String value) {
return value; // MySQL does not storesUpperCaseIdentifiers() :
}
private transient Boolean lowerCaseIdentifiers;
@Override
protected final String caseConvertIdentifierForJdbc(
final Connection connection, final String value) throws SQLException {
if ( value == null ) return null;
Boolean lowerCase = lowerCaseIdentifiers;
if (lowerCase == null) {
lowerCase = lowerCaseIdentifiers = connection.getMetaData().storesLowerCaseIdentifiers();
}
return lowerCase ? value.toLowerCase() : value;
}
@Override
protected Connection newConnection() throws RaiseException, SQLException {
final Connection connection;
try {
connection = super.newConnection();
}
catch (SQLException ex) {
int errorCode = ex.getErrorCode();
// access denied, no database
if (errorCode == 1044 || errorCode == 1049) throw newNoDatabaseError(ex);
throw ex;
}
if ( doStopCleanupThread() ) shutdownCleanupThread();
return connection;
}
private static Boolean stopCleanupThread;
static {
final String stopThread = SafePropertyAccessor.getProperty("arjdbc.mysql.stop_cleanup_thread");
if ( stopThread != null ) stopCleanupThread = Boolean.parseBoolean(stopThread);
}
private static boolean doStopCleanupThread() throws SQLException {
return stopCleanupThread != null && stopCleanupThread;
}
private static boolean cleanupThreadShutdown;
@SuppressWarnings("unchecked")
private static void shutdownCleanupThread() {
if ( cleanupThreadShutdown ) return;
try {
Class threadClass = Class.forName("com.mysql.jdbc.AbandonedConnectionCleanupThread");
threadClass.getMethod("shutdown").invoke(null);
}
catch (ClassNotFoundException e) {
debugMessage(null, "missing MySQL JDBC cleanup thread ", e);
}
catch (NoSuchMethodException | SecurityException | IllegalAccessException e) {
debugMessage(null, e);
} catch (InvocationTargetException e) {
debugMessage(null, e.getTargetException());
} finally { cleanupThreadShutdown = true; }
}
}