Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added casting between datetime and number types #60

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine} -Duser.timezone=GMT-04</argLine>
<environmentVariables>
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
<YDB_DOCKER_IMAGE>cr.yandex/yc/yandex-docker-local-ydb:trunk</YDB_DOCKER_IMAGE>
</environmentVariables>
<systemPropertyVariables>
<java.util.logging.config.file>src/test/resources/logging.properties</java.util.logging.config.file>
</systemPropertyVariables>

<!--
Enable support of test's custom display names
https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html#surefire-extensions-and-reports-configuration-for-displayname
Expand Down
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