Skip to content

Commit

Permalink
Fix asObject deserialization for binary, interval and document values
Browse files Browse the repository at this point in the history
  • Loading branch information
gartens committed Apr 30, 2024
1 parent 9da1929 commit c2d08b4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/polypheny/jdbc/types/TypedValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ public double asDouble() throws SQLException {
if ( floatValue != null ) {
return floatValue.doubleValue();
}
if (bigDecimalValue != null) {
if ( bigDecimalValue != null ) {
return bigDecimalValue.doubleValue();
}
if ( isNull() ) {
Expand Down Expand Up @@ -825,17 +825,17 @@ public Object asObject() throws SQLException {
case TIMESTAMP:
return asTimestamp();
case INTERVAL:
return otherValue;
return asInterval();
case STRING:
return asString();
case BINARY:
asBytes();
return asBytes();
case NULL:
return null;
case LIST:
return asArray();
case DOCUMENT:
return otherValue;
return asDocument();
case FILE:
return asBlob();
default:
Expand Down Expand Up @@ -866,17 +866,17 @@ public Object asObject( Calendar calendar ) throws SQLException {
case TIMESTAMP:
return asTimestamp( calendar );
case INTERVAL:
return otherValue;
return asInterval();
case STRING:
return asString();
case BINARY:
asBytes();
return asBytes();
case NULL:
return null;
case LIST:
return asArray();
case DOCUMENT:
return otherValue;
return asDocument();
case FILE:
return asBlob();
default:
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/org/polypheny/jdbc/types/TypedValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,18 @@ void binaryTest() throws SQLException {
}


@Test
void binaryTestAsObject() throws SQLException {
byte[] value = new byte[]{ 1, 2, 3, 4 };
TypedValue typedValue1 = TypedValue.fromBytes( value );
ProtoValue protoValue = typedValue1.serialize();

assertEquals( ValueCase.BINARY, protoValue.getValueCase() );

assertArrayEquals( value, (byte[]) new TypedValue( protoValue ).asObject() );
}


@Test
void dateTest() throws SQLException {
Date value = new Date( 49852800000L );
Expand Down Expand Up @@ -873,6 +885,18 @@ void intervalTest() throws SQLException {
}


@Test
void intervalTestAsObject() throws SQLException {
PolyInterval value = new PolyInterval( 32, 0 );
TypedValue typedValue1 = TypedValue.fromInterval( value );
ProtoValue protoValue = typedValue1.serialize();

assertEquals( ValueCase.INTERVAL, protoValue.getValueCase() );

assertEquals( value, new TypedValue( protoValue ).asObject() );
}


@Test
void documentTest() throws SQLException {
PolyDocument value = new PolyDocument();
Expand All @@ -892,6 +916,23 @@ void documentTest() throws SQLException {
}


@Test
void documentTestAsObject() throws SQLException {
PolyDocument value = new PolyDocument();
value.put( "firstValue", TypedValue.fromBoolean( true ) );
value.put( "secondValue", TypedValue.fromDouble( 12.345 ) );
value.put( "thirdValue", TypedValue.fromInterval( new PolyInterval( 69, 0 ) ) );

TypedValue typedValue1 = TypedValue.fromDocument( value );
ProtoValue protoValue = typedValue1.serialize();

PolyDocument document = (PolyDocument) new TypedValue( protoValue ).asObject();
assertEquals( value.get( "firstValue" ).asBoolean(), document.get( "firstValue" ).asBoolean() );
assertEquals( value.get( "secondValue" ).asDouble(), document.get( "secondValue" ).asDouble() );
assertEquals( value.get( "thirdValue" ).asInterval(), document.get( "thirdValue" ).asInterval() );
}


@Test
void fileTest() throws SQLException {
Blob value = new PolyphenyBlob( new byte[]{ 1, 2, 3, 4, 5 } );
Expand All @@ -904,6 +945,7 @@ void fileTest() throws SQLException {
assertArrayEquals( value.getBytes( 1, 5 ), typedValue2.asBlob().getBytes( 1, 5 ) );
}


@Test
void getLengthTest() throws SQLException {
String value = "12345678";
Expand Down

0 comments on commit c2d08b4

Please sign in to comment.