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

Standard fixes #16

Merged
merged 3 commits into from
Aug 16, 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
4 changes: 4 additions & 0 deletions src/main/java/org/polypheny/jdbc/PolyphenyResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ public double getDouble( int columnIndex ) throws SQLException {


@Override
@Deprecated //according to jdbc 4.3
public BigDecimal getBigDecimal( int columnIndex, int scale ) throws SQLException {
throwIfClosed();
return accessValue( columnIndex ).asBigDecimal( scale );
Expand Down Expand Up @@ -310,6 +311,7 @@ public InputStream getAsciiStream( int columnIndex ) throws SQLException {


@Override
@Deprecated //according to jdbc 4.3
public InputStream getUnicodeStream( int columnIndex ) throws SQLException {
throwIfClosed();
return accessValue( columnIndex ).asUnicodeStream();
Expand Down Expand Up @@ -372,6 +374,7 @@ public double getDouble( String columnLabel ) throws SQLException {


@Override
@Deprecated //according to jdbc 4.3
public BigDecimal getBigDecimal( String columnLabel, int scale ) throws SQLException {
return getBigDecimal( metadata.getColumnIndexFromLabel( columnLabel ), scale );
}
Expand Down Expand Up @@ -408,6 +411,7 @@ public InputStream getAsciiStream( String columnLabel ) throws SQLException {


@Override
@Deprecated //according to jdbc 4.3
public InputStream getUnicodeStream( String columnLabel ) throws SQLException {
return getUnicodeStream( metadata.getColumnIndexFromLabel( columnLabel ) );
}
Expand Down
45 changes: 44 additions & 1 deletion src/main/java/org/polypheny/jdbc/types/TypedValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ public static TypedValue fromDate( Date dateValue ) {


public static TypedValue fromDate( Date dateValue, Calendar calendar ) {
if ( dateValue == null ) {
return fromNull();
}
return fromDate( TypedValueUtils.getDateInCalendar( dateValue, calendar ) );
}

Expand Down Expand Up @@ -539,7 +542,37 @@ public String asString() throws SQLException {
if ( isNull() ) {
return null;
}
throw new PrismInterfaceServiceException( PrismInterfaceErrors.DATA_TYPE_MISMATCH, "This value is not of type CHAR or VARCHAR." );
switch ( valueCase ) {
case BOOLEAN:
return booleanValue ? "1" : "0";
case INTEGER:
return integerValue.toString();
case LONG:
return bigintValue.toString();
case BIG_DECIMAL:
return bigDecimalValue.toString();
case FLOAT:
return floatValue.toString();
case DOUBLE:
return doubleValue.toString();
case DATE:
return dateValue.toString();
case TIME:
return timeValue.toString();
case TIMESTAMP:
return timestampValue.toString();
case INTERVAL:
return ((PolyInterval) otherValue).toString();
case BINARY:
return Arrays.toString( binaryValue );
case NULL:
return null;
case LIST:
case FILE:
case DOCUMENT:
throw new PrismInterfaceServiceException( PrismInterfaceErrors.DATA_TYPE_MISMATCH, "This value cannot be returned as a string." );
}
throw new PrismInterfaceServiceException( PrismInterfaceErrors.DATA_TYPE_MISMATCH, "This value cannot be returned as a string." );
}


Expand Down Expand Up @@ -1429,4 +1462,14 @@ private static PolyInterval getInterval( ProtoInterval interval ) {
return new PolyInterval( interval.getMonths(), interval.getMilliseconds() );
}


@Override
public String toString() {
try {
return asString();
} catch ( SQLException e ) {
throw new RuntimeException( e );
}
}

}
94 changes: 93 additions & 1 deletion src/test/java/org/polypheny/jdbc/types/TypedValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.polypheny.jdbc.PrismInterfaceServiceException;
Expand Down Expand Up @@ -440,7 +441,8 @@ public void fromDateWithNullDateAndCalendarProvided() {
Calendar calendar = Calendar.getInstance();
calendar.set( 2022, Calendar.JANUARY, 1 );

assertThrows( NullPointerException.class, () -> TypedValue.fromDate( null, calendar ) );
TypedValue value = TypedValue.fromDate( null, calendar );
assertTrue( value.isNull() );
}


Expand Down Expand Up @@ -954,4 +956,94 @@ void getLengthTest() throws SQLException {
assertEquals( value.length(), typedValue2.getLength() );
}

@Test
void testFromBoolean() throws SQLException {
TypedValue value = TypedValue.fromBoolean(true);
assertEquals("1", value.asString());
}

@Test
void testFromByte() throws SQLException {
byte byteValue = 10;
TypedValue value = TypedValue.fromByte(byteValue);
assertEquals("10", value.asString());
}

@Test
void testFromShort() throws SQLException {
short shortValue = 20;
TypedValue value = TypedValue.fromShort(shortValue);
assertEquals("20", value.asString());
}

@Test
void testFromInteger() throws SQLException {
int intValue = 30;
TypedValue value = TypedValue.fromInteger(intValue);
assertEquals("30", value.asString());
}

@Test
void testFromLong() throws SQLException {
long longValue = 40L;
TypedValue value = TypedValue.fromLong(longValue);
assertEquals("40", value.asString());
}

@Test
void testFromFloat() throws SQLException {
float floatValue = 50.5f;
TypedValue value = TypedValue.fromFloat(floatValue);
assertEquals("50.5", value.asString());
}

@Test
void testFromDouble() throws SQLException {
double doubleValue = 60.6;
TypedValue value = TypedValue.fromDouble(doubleValue);
assertEquals("60.6", value.asString());
}

@Test
void testFromBigDecimal() throws SQLException {
BigDecimal bigDecimalValue = new BigDecimal("70.7");
TypedValue value = TypedValue.fromBigDecimal(bigDecimalValue);
assertEquals("70.7", value.asString());
}

@Test
void testFromString() throws SQLException {
String stringValue = "test";
TypedValue value = TypedValue.fromString(stringValue);
assertEquals("test", value.asString());
}

@Test
void testFromDate() throws SQLException {
Date dateValue = new Date(2024, 7, 28);
TypedValue value = TypedValue.fromDate(dateValue);
assertEquals(dateValue.toString(), value.asString());
}

@Test
void testFromTime() throws SQLException {
Time timeValue = new Time(System.currentTimeMillis());
TypedValue value = TypedValue.fromTime(timeValue);
assertEquals(timeValue.toString(), value.asString());
}

@Test
void testFromTimestamp() throws SQLException {
Timestamp timestampValue = new Timestamp(System.currentTimeMillis());
TypedValue value = TypedValue.fromTimestamp(timestampValue);
assertEquals(timestampValue.toString(), value.asString());
}


@Test
void testFromInterval() throws SQLException {
PolyInterval interval = new PolyInterval(1, 0);
TypedValue value = TypedValue.fromInterval(interval);
assertEquals(interval.toString(), value.asString());
}
}