From 748f9f3cd091ef95f2845a0b5ed0cb6be01e5706 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Fri, 29 Sep 2017 16:19:48 +0300 Subject: [PATCH] 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@026476a6e4c2806ba502f157816db88976e4646b --- .../xpack/sql/jdbc/debug/Debug.java | 3 +- .../xpack/sql/jdbc/jdbc/JdbcDriver.java | 35 ++++++++--- .../sql/jdbc/jdbc/JdbcParameterMetaData.java | 6 +- .../xpack/sql/jdbc/jdbc/JdbcUtils.java | 59 +++++++++++++++++-- 4 files changed, 87 insertions(+), 16 deletions(-) diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/debug/Debug.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/debug/Debug.java index dd928282664..038337d3ee3 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/debug/Debug.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/debug/Debug.java @@ -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; diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDriver.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDriver.java index ae0ac353874..10dac5a28d2 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDriver.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDriver.java @@ -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; diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcParameterMetaData.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcParameterMetaData.java index e2c91075b5d..405601e57b7 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcParameterMetaData.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcParameterMetaData.java @@ -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 diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcUtils.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcUtils.java index b34d19676bb..3ec3f704692 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcUtils.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcUtils.java @@ -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(); - } } \ No newline at end of file