Skip to content

Commit

Permalink
Add retrieval of jdbc types as string
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasHafner committed Jul 28, 2024
1 parent 4da5325 commit 8c8018b
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/main/java/org/polypheny/jdbc/types/TypedValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,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:
return otherValue.toString();
}
throw new PrismInterfaceServiceException( PrismInterfaceErrors.DATA_TYPE_MISMATCH, "This value cannot be returned as a string." );
}


Expand Down
89 changes: 89 additions & 0 deletions 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 @@ -954,6 +955,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());
}
}

0 comments on commit 8c8018b

Please sign in to comment.