Skip to content

Commit

Permalink
FMWK-253 Fix handling of null values in AerospikeRecordResultSet (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn authored Oct 18, 2023
1 parent 7f49806 commit 4a86ab3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<List<?>> tablesData;
if (catalog == null) {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -17,9 +17,9 @@ public enum OperatorBinary implements Operator {
OR(Exp::or),
AND(Exp::and);

private final BiFunction<Exp, Exp, Exp> func;
private final BinaryOperator<Exp> func;

OperatorBinary(BiFunction<Exp, Exp, Exp> expFunc) {
OperatorBinary(BinaryOperator<Exp> expFunc) {
this.func = expFunc;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/aerospike/jdbc/predicate/OperatorUnary.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Exp, Exp> func;
private final UnaryOperator<Exp> func;

OperatorUnary(Function<Exp, Exp> expFunc) {
OperatorUnary(UnaryOperator<Exp> expFunc) {
this.func = expFunc;
}

Expand Down
41 changes: 25 additions & 16 deletions src/main/java/com/aerospike/jdbc/sql/AerospikeRecordResultSet.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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;

import java.math.BigDecimal;
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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<Value> getUserKey() {
return Optional.ofNullable(recordSet.getKey().userKey);
}

private Optional<Object> getBin(String columnLabel) {
return Optional.ofNullable(recordSet.getRecord().bins.get(columnLabel));
}
}

0 comments on commit 4a86ab3

Please sign in to comment.