diff --git a/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java b/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java index 2d82063..fc381a8 100644 --- a/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java +++ b/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java @@ -34,6 +34,7 @@ import static com.aerospike.jdbc.util.Constants.defaultKeyName; import static com.aerospike.jdbc.util.Constants.defaultSchemaName; import static com.aerospike.jdbc.util.Constants.schemaScanRecords; +import static com.google.common.base.Strings.isNullOrEmpty; import static java.lang.String.format; import static java.sql.Connection.TRANSACTION_NONE; import static java.sql.JDBCType.OTHER; @@ -720,8 +721,8 @@ public ResultSet getProcedureColumns(String catalog, String schemaPattern, Strin @Override public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) { - Pattern tableNameRegex = tableNamePattern == null || "".equals(tableNamePattern) ? - null : Pattern.compile(tableNamePattern.replace("%", ".*")); + Pattern tableNameRegex = isNullOrEmpty(tableNamePattern) ? null + : Pattern.compile(tableNamePattern.replace("%", ".*")); final Iterable> tablesData; if (catalog == null) { @@ -771,7 +772,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa String columnNamePattern) throws SQLException { logger.info(() -> String.format("AerospikeDatabaseMetadata getColumns; %s, %s, %s, %s", catalog, schemaPattern, tableNamePattern, columnNamePattern)); - Pattern tableNameRegex = tableNamePattern == null || "".equals(tableNamePattern) ? null + Pattern tableNameRegex = isNullOrEmpty(tableNamePattern) ? null : Pattern.compile(tableNamePattern.replace("%", ".*")); final String namespace = catalog == null ? schemaPattern : catalog; diff --git a/src/main/java/com/aerospike/jdbc/predicate/OperatorBinary.java b/src/main/java/com/aerospike/jdbc/predicate/OperatorBinary.java index bb5523b..ca399c6 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/OperatorBinary.java +++ b/src/main/java/com/aerospike/jdbc/predicate/OperatorBinary.java @@ -3,7 +3,7 @@ import com.aerospike.client.exp.Exp; import com.google.common.base.Preconditions; -import java.util.function.BiFunction; +import java.util.function.BinaryOperator; public enum OperatorBinary implements Operator { @@ -17,9 +17,9 @@ public enum OperatorBinary implements Operator { OR(Exp::or), AND(Exp::and); - private final BiFunction func; + private final BinaryOperator func; - OperatorBinary(BiFunction expFunc) { + OperatorBinary(BinaryOperator expFunc) { this.func = expFunc; } diff --git a/src/main/java/com/aerospike/jdbc/predicate/OperatorUnary.java b/src/main/java/com/aerospike/jdbc/predicate/OperatorUnary.java index dd3163c..9a457fa 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/OperatorUnary.java +++ b/src/main/java/com/aerospike/jdbc/predicate/OperatorUnary.java @@ -3,15 +3,15 @@ import com.aerospike.client.exp.Exp; import com.google.common.base.Preconditions; -import java.util.function.Function; +import java.util.function.UnaryOperator; public enum OperatorUnary implements Operator { NOT(Exp::not); - private final Function func; + private final UnaryOperator func; - OperatorUnary(Function expFunc) { + OperatorUnary(UnaryOperator expFunc) { this.func = expFunc; } diff --git a/src/main/java/com/aerospike/jdbc/sql/AerospikeRecordResultSet.java b/src/main/java/com/aerospike/jdbc/sql/AerospikeRecordResultSet.java index 8438676..86101a8 100644 --- a/src/main/java/com/aerospike/jdbc/sql/AerospikeRecordResultSet.java +++ b/src/main/java/com/aerospike/jdbc/sql/AerospikeRecordResultSet.java @@ -1,6 +1,7 @@ package com.aerospike.jdbc.sql; import com.aerospike.client.Record; +import com.aerospike.client.Value; import com.aerospike.jdbc.async.RecordSet; import com.aerospike.jdbc.model.DataColumn; @@ -8,6 +9,7 @@ import java.sql.Statement; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.logging.Logger; import static com.aerospike.jdbc.util.Constants.defaultKeyName; @@ -43,34 +45,33 @@ protected boolean moveToNext() { public Object getObject(String columnLabel) { logger.fine(() -> "getObject: " + columnLabel); if (columnLabel.equals(defaultKeyName)) { - return recordSet.getKey().userKey; + return getUserKey().map(Value::getObject).orElse(null); } - return recordSet.getRecord().bins.get(columnLabel); + return getBin(columnLabel).orElse(null); } @Override public String getString(String columnLabel) { logger.fine(() -> "getString: " + columnLabel); if (columnLabel.equals(defaultKeyName)) { - return recordSet.getKey().userKey.toString(); + return getUserKey().map(Value::toString).orElse(null); } - Object bin = recordSet.getRecord().bins.get(columnLabel); - return Objects.isNull(bin) ? null : bin.toString(); + return getBin(columnLabel).map(Objects::toString).orElse(null); } @Override public boolean getBoolean(String columnLabel) { logger.fine(() -> "getBoolean: " + columnLabel); if (columnLabel.equals(defaultKeyName)) { - return Boolean.parseBoolean(recordSet.getKey().userKey.toString()); + return getUserKey().map(Value::toString).map(Boolean::parseBoolean).orElse(false); } - return Boolean.parseBoolean(recordSet.getRecord().bins.get(columnLabel).toString()); + return getBin(columnLabel).map(Object::toString).map(Boolean::parseBoolean).orElse(false); } @Override public byte getByte(String columnLabel) { logger.fine(() -> "getByte: " + columnLabel); - return 0; + return (byte) getInt(columnLabel); } @Override @@ -83,18 +84,18 @@ public short getShort(String columnLabel) { public int getInt(String columnLabel) { logger.fine(() -> "getInt: " + columnLabel); if (columnLabel.equals(defaultKeyName)) { - return recordSet.getKey().userKey.toInteger(); + return getUserKey().map(Value::toInteger).orElse(0); } - return Integer.parseInt(recordSet.getRecord().bins.get(columnLabel).toString()); + return getBin(columnLabel).map(Object::toString).map(Integer::parseInt).orElse(0); } @Override public long getLong(String columnLabel) { logger.fine(() -> "getLong: " + columnLabel); if (columnLabel.equals(defaultKeyName)) { - return recordSet.getKey().userKey.toLong(); + return getUserKey().map(Value::toLong).orElse(0L); } - return Long.parseLong(recordSet.getRecord().bins.get(columnLabel).toString()); + return getBin(columnLabel).map(Object::toString).map(Long::parseLong).orElse(0L); } @Override @@ -107,9 +108,9 @@ public float getFloat(String columnLabel) { public double getDouble(String columnLabel) { logger.fine(() -> "getDouble: " + columnLabel); if (columnLabel.equals(defaultKeyName)) { - return Double.parseDouble(recordSet.getKey().userKey.toString()); + return getUserKey().map(Value::toString).map(Double::parseDouble).orElse(0.0d); } - return Double.parseDouble(recordSet.getRecord().bins.get(columnLabel).toString()); + return getBin(columnLabel).map(Object::toString).map(Double::parseDouble).orElse(0.0d); } @Override @@ -122,8 +123,16 @@ public BigDecimal getBigDecimal(String columnLabel, int scale) { public byte[] getBytes(String columnLabel) { logger.fine(() -> "getBytes: " + columnLabel); if (columnLabel.equals(defaultKeyName)) { - return recordSet.getKey().userKey.toString().getBytes(); + return getUserKey().map(Value::toString).map(String::getBytes).orElse(null); } - return (byte[]) recordSet.getRecord().bins.get(columnLabel); + return getBin(columnLabel).map(byte[].class::cast).orElse(null); + } + + private Optional getUserKey() { + return Optional.ofNullable(recordSet.getKey().userKey); + } + + private Optional getBin(String columnLabel) { + return Optional.ofNullable(recordSet.getRecord().bins.get(columnLabel)); } }