Skip to content

Commit

Permalink
Add Interval
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasHafner committed Mar 20, 2024
1 parent fa5842a commit 94d877a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
import java.sql.Types;
import java.util.List;
import java.util.stream.Collectors;
import org.polypheny.db.protointerface.proto.ProtoInterval;
import org.polypheny.db.protointerface.proto.ProtoInterval.UnitCase;
import org.polypheny.db.protointerface.proto.ProtoValue;
import org.polypheny.jdbc.jdbctypes.PolyphenyArray;
import org.polypheny.jdbc.jdbctypes.TypedValue;
import org.polypheny.jdbc.nativetypes.PolyInterval;
import org.polypheny.jdbc.nativetypes.PolyInterval.Unit;

public class ProtoValueDeserializer {

Expand Down Expand Up @@ -53,6 +57,7 @@ public static TypedValue deserializeToTypedValue( ProtoValue value ) {
case LIST:
return TypedValue.fromArray( getArray( value ) );
case INTERVAL:
return TypedValue.fromInterval( getInterval( value.getInterval() ) );
case MAP:
case DOCUMENT:
default:
Expand All @@ -64,6 +69,15 @@ public static TypedValue deserializeToTypedValue( ProtoValue value ) {
}


private static PolyInterval getInterval( ProtoInterval interval ) {
if ( interval.getUnitCase() == UnitCase.MILLISECONDS ) {
return new PolyInterval( interval.getMilliseconds(), Unit.MILLISECONDS );
} else {
return new PolyInterval( interval.getMilliseconds(), Unit.MONTHS );
}
}


private static Array getArray( ProtoValue value ) throws SQLException {
String baseType = value.getValueCase().name();
List<TypedValue> values = value.getList().getValuesList().stream()
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/org/polypheny/jdbc/jdbctypes/TypedValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@
import java.util.Map;
import lombok.Getter;
import org.apache.commons.lang3.NotImplementedException;
import org.polypheny.jdbc.ProtoInterfaceServiceException;
import org.polypheny.jdbc.ProtoInterfaceErrors;
import org.polypheny.jdbc.ProtoInterfaceServiceException;
import org.polypheny.jdbc.deserialization.UDTPrototype;
import org.polypheny.jdbc.nativetypes.PolyInterval;
import org.polypheny.jdbc.utils.TypedValueUtils;

public class TypedValue implements Convertible {
Expand Down Expand Up @@ -84,11 +85,23 @@ private TypedValue( UDTPrototype udtPrototype ) {
}


private TypedValue( Object object, String internalType ) {
this.jdbcType = Types.OTHER;
this.value = object;
this.internalType = internalType;
}


public static TypedValue fromUdtPrototype( UDTPrototype udtPrototype ) {
return new TypedValue( udtPrototype );
}


public static TypedValue fromInterval( PolyInterval interval ) {
return new TypedValue( interval, "INTERVAL" );
}


public static TypedValue fromBoolean( boolean value ) {
return new TypedValue( Types.BOOLEAN, value );
}
Expand Down Expand Up @@ -965,6 +978,10 @@ public Object asObject() throws SQLException {
return asNClob();
case Types.SQLXML:
return asSQLXML();
case Types.OTHER:
if ( internalType.equals( "INTERVAL" ) ) {
return value;
}
}
throw new IllegalArgumentException( "No conversion to object possible for jdbc type: " + getJdbcType() );
}
Expand Down Expand Up @@ -1033,6 +1050,10 @@ public Object asObject( Calendar calendar ) throws SQLException {
return asNClob();
case Types.SQLXML:
return asSQLXML();
case Types.OTHER:
if ( internalType.equals( "INTERVAL" ) ) {
return value;
}
}
throw new IllegalArgumentException( "No conversion to object possible for jdbc type: " + getJdbcType() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.polypheny.jdbc.ProtoInterfaceServiceException;
import org.polypheny.jdbc.ProtoInterfaceErrors;
import org.polypheny.db.protointerface.proto.ProtoBigDecimal;
import org.polypheny.db.protointerface.proto.ProtoBinary;
import org.polypheny.db.protointerface.proto.ProtoBoolean;
import org.polypheny.db.protointerface.proto.ProtoDate;
import org.polypheny.db.protointerface.proto.ProtoDouble;
import org.polypheny.db.protointerface.proto.ProtoFloat;
import org.polypheny.db.protointerface.proto.ProtoInteger;
import org.polypheny.db.protointerface.proto.ProtoInterval;
import org.polypheny.db.protointerface.proto.ProtoList;
import org.polypheny.db.protointerface.proto.ProtoLong;
import org.polypheny.db.protointerface.proto.ProtoNull;
import org.polypheny.db.protointerface.proto.ProtoString;
import org.polypheny.db.protointerface.proto.ProtoTime;
import org.polypheny.db.protointerface.proto.ProtoTimestamp;
import org.polypheny.db.protointerface.proto.ProtoValue;
import org.polypheny.jdbc.ProtoInterfaceErrors;
import org.polypheny.jdbc.ProtoInterfaceServiceException;
import org.polypheny.jdbc.jdbctypes.TypedValue;
import org.polypheny.jdbc.nativetypes.PolyInterval;
import org.polypheny.jdbc.properties.DriverProperties;

public class ProtoValueSerializer {
Expand Down Expand Up @@ -96,7 +98,10 @@ public static ProtoValue serialize( TypedValue typedValue ) throws SQLException
case Types.NULL:
return serializeAsProtoNull();
case Types.OTHER:
// TODO TH: find something useful to do here...
switch ( typedValue.getInternalType() ) {
case "INTERVAL":
return serializeInterval( typedValue );
}
break;
case Types.JAVA_OBJECT:
// TODO TH: find something useful to do here...
Expand Down Expand Up @@ -140,6 +145,23 @@ public static ProtoValue serialize( TypedValue typedValue ) throws SQLException
}


private static ProtoValue serializeInterval( TypedValue typedValue ) {
PolyInterval interval = (PolyInterval) typedValue.getValue();
ProtoInterval.Builder protoInterval = ProtoInterval.newBuilder();
switch ( interval.unit ) {
case MILLISECONDS:
protoInterval.setMilliseconds( interval.value );
break;
case MONTHS:
protoInterval.setMonths( interval.value );
break;
}
return ProtoValue.newBuilder()
.setInterval( protoInterval.build() )
.build();
}


private static ProtoValue serializeAsProtoRowId( TypedValue typedValue ) throws SQLException {
throw new RuntimeException( "RowID serialization is not implemented" );
// ProtoRowId protoRowId = ProtoRowId.newBuilder()
Expand Down

0 comments on commit 94d877a

Please sign in to comment.