Skip to content

Commit

Permalink
Update setters & getters for Time, Date and Timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
alex268 committed Aug 23, 2024
1 parent 455d5ba commit d3e5863
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 94 deletions.
30 changes: 21 additions & 9 deletions jdbc/src/main/java/tech/ydb/jdbc/common/MappingSetters.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.math.BigInteger;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
Expand All @@ -23,6 +24,7 @@
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;

import tech.ydb.jdbc.YdbConst;
import tech.ydb.table.values.DecimalType;
import tech.ydb.table.values.DecimalValue;
import tech.ydb.table.values.ListType;
Expand All @@ -33,10 +35,6 @@
import tech.ydb.table.values.Type;
import tech.ydb.table.values.Value;

import static tech.ydb.jdbc.YdbConst.CANNOT_LOAD_DATA_FROM_IS;
import static tech.ydb.jdbc.YdbConst.CANNOT_LOAD_DATA_FROM_READER;
import static tech.ydb.jdbc.YdbConst.UNABLE_TO_CAST;

public class MappingSetters {
private MappingSetters() { }

Expand Down Expand Up @@ -123,15 +121,15 @@ private static String toString(Object x) {
}

private static SQLException castNotSupported(PrimitiveType type, Object x, Exception cause) {
return new SQLException(String.format(UNABLE_TO_CAST, toString(x), type), cause);
return new SQLException(String.format(YdbConst.UNABLE_TO_CAST, toString(x), type), cause);
}

private static SQLException castNotSupported(PrimitiveType type, Object x) {
return new SQLException(String.format(UNABLE_TO_CAST, toString(x), type));
return new SQLException(String.format(YdbConst.UNABLE_TO_CAST, toString(x), type));
}

private static SQLException castNotSupported(Type.Kind kind, Object x) {
return new SQLException(String.format(UNABLE_TO_CAST, toString(x), kind));
return new SQLException(String.format(YdbConst.UNABLE_TO_CAST, toString(x), kind));
}

private static ListValue castAsList(ListType type, Setters itemSetter, Object x) throws SQLException {
Expand Down Expand Up @@ -263,12 +261,20 @@ private static short castAsShort(PrimitiveType type, Object x) throws SQLExcepti
private static int castAsInt(PrimitiveType type, Object x) throws SQLException {
if (x instanceof Integer) {
return (Integer) x;
} else if (x instanceof Long) {
return ((Long) x).intValue();
} else if (x instanceof Short) {
return (Short) x;
} else if (x instanceof Byte) {
return (Byte) x;
} else if (x instanceof Boolean) {
return ((Boolean) x) ? 1 : 0;
} else if (x instanceof Time) {
return ((Time) x).toLocalTime().toSecondOfDay();
} else if (x instanceof Date) {
return (int) ((Date) x).toLocalDate().toEpochDay();
} else if (x instanceof Timestamp) {
return (int) ((Timestamp) x).getTime();
}
throw castNotSupported(type, x);
}
Expand All @@ -286,6 +292,12 @@ private static long castAsLong(PrimitiveType type, Object x) throws SQLException
return ((Boolean) x) ? 1L : 0L;
} else if (x instanceof BigInteger) {
return ((BigInteger) x).longValue();
} else if (x instanceof Time) {
return ((Time) x).toLocalTime().toSecondOfDay();
} else if (x instanceof Date) {
return ((Date) x).toLocalDate().toEpochDay();
} else if (x instanceof Timestamp) {
return ((Timestamp) x).getTime();
}
throw castNotSupported(type, x);
}
Expand Down Expand Up @@ -480,7 +492,7 @@ static CharStream fromReader(Reader reader, long length) {
return CharStreams.toString(reader);
}
} catch (IOException e) {
throw new RuntimeException(CANNOT_LOAD_DATA_FROM_READER + e.getMessage(), e);
throw new RuntimeException(YdbConst.CANNOT_LOAD_DATA_FROM_READER + e.getMessage(), e);
}
};
}
Expand All @@ -499,7 +511,7 @@ static ByteStream fromInputStream(InputStream stream, long length) {
return ByteStreams.toByteArray(stream);
}
} catch (IOException e) {
throw new RuntimeException(CANNOT_LOAD_DATA_FROM_IS + e.getMessage(), e);
throw new RuntimeException(YdbConst.CANNOT_LOAD_DATA_FROM_IS + e.getMessage(), e);
}
};
}
Expand Down
23 changes: 20 additions & 3 deletions jdbc/src/main/java/tech/ydb/jdbc/common/TypeDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class TypeDescription {
}

private final Type type;

private final boolean isTimestamp;
private final boolean isNumber;
private final boolean isNull;

private final boolean optional;
private final OptionalValue optionalValue;

Expand All @@ -41,15 +46,27 @@ private TypeDescription(Type type,
this.getters = Objects.requireNonNull(getters);
this.setters = Objects.requireNonNull(setters);
this.sqlType = Objects.requireNonNull(sqlType);

this.isTimestamp = type == PrimitiveType.Timestamp;
this.isNumber = type == PrimitiveType.Int8 || type == PrimitiveType.Uint8
|| type == PrimitiveType.Int16 || type == PrimitiveType.Uint16
|| type == PrimitiveType.Int32 || type == PrimitiveType.Uint32
|| type == PrimitiveType.Int64 || type == PrimitiveType.Uint64;
this.isNull = type.getKind() == Type.Kind.NULL || type.getKind() == Type.Kind.VOID;
}

public boolean isNullType() {
return type.getKind() == Type.Kind.NULL || type.getKind() == Type.Kind.VOID;
public boolean isNull() {
return isNull;
}

public boolean isTimestamp() {
return type == PrimitiveType.Timestamp;
return isTimestamp;
}

public boolean isNumber() {
return isNumber;
}

public boolean isOptional() {
return optional;
}
Expand Down
Loading

0 comments on commit d3e5863

Please sign in to comment.