Fixes two NOCOMMITs

The DriverManager registration is now public so the user can control it
(static block might not be enough). The checked exception is also
logged and the rest rethrown.

Fixed param type name to return the correct value

Original commit: elastic/x-pack-elasticsearch@026476a6e4
This commit is contained in:
Costin Leau 2017-09-29 16:19:48 +03:00
parent f8776b8d43
commit 748f9f3cd0
4 changed files with 87 additions and 16 deletions

View File

@ -85,7 +85,6 @@ public final class Debug {
synchronized (Debug.class) {
log = OUTPUT_MANAGED.get(managedPrinter);
if (log == null) {
log = new DebugLog(managedPrinter);
OUTPUT_MANAGED.put(managedPrinter, log);
}
@ -197,7 +196,7 @@ public final class Debug {
OUTPUT_MANAGED.clear();
}
// NOCOMMIT loggers instead, I think
// NOCOMMIT loggers instead, I think - CL: what if the user wants to output to system.err/out like in an embedded app?
@SuppressForbidden(reason="temporary")
private static PrintStream stdout() {
return System.out;

View File

@ -9,6 +9,7 @@ import org.elasticsearch.xpack.sql.jdbc.debug.Debug;
import org.elasticsearch.xpack.sql.jdbc.util.Version;
import java.io.Closeable;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
@ -20,16 +21,36 @@ import java.util.logging.Logger;
public class JdbcDriver implements java.sql.Driver, Closeable {
private static final JdbcDriver INSTANCE = new JdbcDriver();
static {
try {
final JdbcDriver d = new JdbcDriver();
DriverManager.registerDriver(d, d::close);
} catch (Exception ex) {
// NOCOMMIT this seems bad!
// ignore
}
register();
}
public static JdbcDriver register() {
try {
DriverManager.registerDriver(INSTANCE, INSTANCE::close);
} catch (SQLException ex) {
// the SQLException is bogus as there's no source for it
PrintWriter writer = DriverManager.getLogWriter();
if (writer != null) {
ex.printStackTrace(writer);
}
}
return INSTANCE;
}
public static void deregister() {
try {
DriverManager.deregisterDriver(INSTANCE);
} catch (SQLException ex) {
// the SQLException is bogus as there's no source for it
PrintWriter writer = DriverManager.getLogWriter();
if (writer != null) {
ex.printStackTrace(writer);
}
}
}
public static int jdbcMajorVersion() {
return 4;

View File

@ -5,11 +5,11 @@
*/
package org.elasticsearch.xpack.sql.jdbc.jdbc;
import org.elasticsearch.xpack.sql.jdbc.jdbc.PreparedQuery.ParamInfo;
import java.sql.ParameterMetaData;
import java.sql.SQLException;
import org.elasticsearch.xpack.sql.jdbc.jdbc.PreparedQuery.ParamInfo;
class JdbcParameterMetaData implements ParameterMetaData, JdbcWrapper {
private final JdbcPreparedStatement ps;
@ -59,7 +59,7 @@ class JdbcParameterMetaData implements ParameterMetaData, JdbcWrapper {
@Override
public String getParameterClassName(int param) throws SQLException {
return paramInfo(param).type.name(); // NOCOMMIT this is almost certainly wrong
return JdbcUtils.classOf(paramInfo(param).type.getVendorTypeNumber()).getName();
}
@Override

View File

@ -14,20 +14,26 @@ import java.sql.Time;
import java.sql.Timestamp;
import static java.sql.Types.BIGINT;
import static java.sql.Types.BINARY;
import static java.sql.Types.BIT;
import static java.sql.Types.BLOB;
import static java.sql.Types.BOOLEAN;
import static java.sql.Types.CHAR;
import static java.sql.Types.CLOB;
import static java.sql.Types.DATE;
import static java.sql.Types.DECIMAL;
import static java.sql.Types.DOUBLE;
import static java.sql.Types.FLOAT;
import static java.sql.Types.INTEGER;
import static java.sql.Types.LONGVARBINARY;
import static java.sql.Types.LONGVARCHAR;
import static java.sql.Types.NULL;
import static java.sql.Types.NUMERIC;
import static java.sql.Types.REAL;
import static java.sql.Types.SMALLINT;
import static java.sql.Types.TIME;
import static java.sql.Types.TIMESTAMP;
import static java.sql.Types.TIMESTAMP_WITH_TIMEZONE;
import static java.sql.Types.TINYINT;
import static java.sql.Types.VARBINARY;
import static java.sql.Types.VARCHAR;
@ -122,6 +128,55 @@ public abstract class JdbcUtils {
throw new JdbcException("Unrecognized class [" + clazz + "]");
}
// see javax.sql.rowset.RowSetMetaDataImpl
// and https://db.apache.org/derby/docs/10.5/ref/rrefjdbc20377.html
public static Class<?> classOf(int jdbcType) {
switch (jdbcType) {
case NUMERIC:
case DECIMAL:
return BigDecimal.class;
case BOOLEAN:
case BIT:
return Boolean.class;
case TINYINT:
return Byte.class;
case SMALLINT:
return Short.class;
case INTEGER:
return Integer.class;
case BIGINT:
return Long.class;
case REAL:
return Float.class;
case FLOAT:
case DOUBLE:
return Double.class;
case BINARY:
case VARBINARY:
case LONGVARBINARY:
return byte[].class;
case CHAR:
case VARCHAR:
case LONGVARCHAR:
return String.class;
case DATE:
return Date.class;
case TIME:
return Time.class;
case TIMESTAMP:
return Timestamp.class;
case BLOB:
return Blob.class;
case CLOB:
return Clob.class;
case TIMESTAMP_WITH_TIMEZONE:
return Long.class;
default:
throw new JdbcException("Unsupported JDBC type " + jdbcType + ", " + type(jdbcType).getName() + "");
}
}
static boolean isSigned(int type) {
switch (type) {
case BIGINT:
@ -141,8 +196,4 @@ public abstract class JdbcUtils {
static JDBCType type(int jdbcType) {
return JDBCType.valueOf(jdbcType);
}
static String typeName(int jdbcType) {
return type(jdbcType).getName();
}
}