use of com.google.cloud.spanner.Type.Code in project java-spanner-jdbc by googleapis.
the class JdbcResultSet method getShort.
@Override
public short getShort(int columnIndex) throws SQLException {
checkClosedAndValidRow();
boolean isNull = isNull(columnIndex);
int spannerIndex = columnIndex - 1;
Code type = spanner.getColumnType(spannerIndex).getCode();
switch(type) {
case BOOL:
return isNull ? 0 : (spanner.getBoolean(spannerIndex) ? (short) 1 : 0);
case FLOAT64:
return isNull ? 0 : checkedCastToShort(Double.valueOf(spanner.getDouble(spannerIndex)).longValue());
case INT64:
return isNull ? 0 : checkedCastToShort(spanner.getLong(spannerIndex));
case NUMERIC:
return isNull ? 0 : checkedCastToShort(spanner.getBigDecimal(spannerIndex));
case PG_NUMERIC:
return isNull ? 0 : checkedCastToShort(parseBigDecimal(spanner.getString(spannerIndex)).toBigInteger());
case STRING:
return isNull ? 0 : checkedCastToShort(parseLong(spanner.getString(spannerIndex)));
case BYTES:
case JSON:
case DATE:
case STRUCT:
case TIMESTAMP:
case ARRAY:
default:
throw createInvalidToGetAs("short", type);
}
}
use of com.google.cloud.spanner.Type.Code in project java-spanner-jdbc by googleapis.
the class JdbcResultSet method getLong.
@Override
public long getLong(int columnIndex) throws SQLException {
checkClosedAndValidRow();
boolean isNull = isNull(columnIndex);
int spannerIndex = columnIndex - 1;
Code type = spanner.getColumnType(spannerIndex).getCode();
switch(type) {
case BOOL:
return isNull ? 0L : (spanner.getBoolean(spannerIndex) ? 1L : 0L);
case FLOAT64:
return isNull ? 0L : Double.valueOf(spanner.getDouble(spannerIndex)).longValue();
case INT64:
return isNull ? 0L : spanner.getLong(spannerIndex);
case NUMERIC:
return isNull ? 0 : checkedCastToLong(parseBigDecimal(spanner.getString(spannerIndex)));
case PG_NUMERIC:
return isNull ? 0L : checkedCastToLong(parseBigDecimal(spanner.getString(spannerIndex)).toBigInteger());
case STRING:
return isNull ? 0L : parseLong(spanner.getString(spannerIndex));
case BYTES:
case JSON:
case DATE:
case STRUCT:
case TIMESTAMP:
case ARRAY:
default:
throw createInvalidToGetAs("long", type);
}
}
use of com.google.cloud.spanner.Type.Code in project java-spanner-jdbc by googleapis.
the class JdbcResultSet method getDouble.
@Override
public double getDouble(int columnIndex) throws SQLException {
checkClosedAndValidRow();
boolean isNull = isNull(columnIndex);
int spannerIndex = columnIndex - 1;
Code type = spanner.getColumnType(spannerIndex).getCode();
switch(type) {
case BOOL:
return isNull ? 0 : (spanner.getBoolean(spannerIndex) ? (double) 1 : 0);
case FLOAT64:
return isNull ? 0 : spanner.getDouble(spannerIndex);
case INT64:
return isNull ? 0 : spanner.getLong(spannerIndex);
case NUMERIC:
return isNull ? 0 : spanner.getBigDecimal(spannerIndex).doubleValue();
case PG_NUMERIC:
return isNull ? 0 : parseDouble(spanner.getString(spannerIndex));
case STRING:
return isNull ? 0 : parseDouble(spanner.getString(spannerIndex));
case BYTES:
case JSON:
case DATE:
case STRUCT:
case TIMESTAMP:
case ARRAY:
default:
throw createInvalidToGetAs("double", type);
}
}
use of com.google.cloud.spanner.Type.Code in project jans by JanssenProject.
the class SpannerOperationServiceImpl method getAttributeDataList.
private List<AttributeData> getAttributeDataList(String objectClass, ResultSet resultSet, boolean skipDn) throws EntryConvertationException {
try {
if ((resultSet == null)) {
return null;
}
if (!resultSet.next()) {
return null;
}
List<AttributeData> result = new ArrayList<AttributeData>();
// TODO: Include child table columns
Set<String> nullableColumns = connectionProvider.getTableNullableColumns(objectClass);
List<StructField> structFields = resultSet.getType().getStructFields();
int columnsCount = resultSet.getColumnCount();
for (int i = 0; i < columnsCount; i++) {
StructField structField = structFields.get(i);
String attributeName = structField.getName();
Code columnTypeCode = structField.getType().getCode();
boolean isNullable = nullableColumns.contains(attributeName.toLowerCase());
if (SpannerOperationService.DOC_ID.equalsIgnoreCase(attributeName) || SpannerOperationService.ID.equalsIgnoreCase(attributeName)) {
// Skip internal attributes
continue;
}
if (skipDn && SpannerOperationService.DN.equalsIgnoreCase(attributeName)) {
// Skip DN attribute
continue;
}
Boolean multiValued = Boolean.FALSE;
Object[] attributeValueObjects;
if (resultSet.isNull(i)) {
attributeValueObjects = NO_OBJECTS;
if (isNullable) {
// Ignore columns with default NULL values
continue;
}
} else {
if (Code.ARRAY == columnTypeCode) {
attributeValueObjects = convertDbArrayToValue(resultSet, structField.getType().getArrayElementType(), i, attributeName);
multiValued = Boolean.TRUE;
} else if (Code.BOOL == columnTypeCode) {
attributeValueObjects = new Object[] { resultSet.getBoolean(i) };
} else if (Code.DATE == columnTypeCode) {
attributeValueObjects = new Object[] { com.google.cloud.Date.toJavaUtilDate(resultSet.getDate(i)) };
} else if (Code.TIMESTAMP == columnTypeCode) {
attributeValueObjects = new Object[] { resultSet.getTimestamp(i).toDate() };
} else if (Code.INT64 == columnTypeCode) {
attributeValueObjects = new Object[] { resultSet.getLong(i) };
} else if (Code.NUMERIC == columnTypeCode) {
attributeValueObjects = new Object[] { resultSet.getBigDecimal(i).longValue() };
} else if (Code.STRING == columnTypeCode) {
Object value = resultSet.getString(i);
try {
value = com.google.cloud.Timestamp.parseTimestamp(value.toString());
} catch (Exception ex) {
}
attributeValueObjects = new Object[] { value };
} else {
throw new EntryConvertationException(String.format("Column with name '%s' does not contain unsupported type '%s'", attributeName, columnTypeCode));
}
}
unescapeValues(attributeValueObjects);
AttributeData tmpAttribute = new AttributeData(attributeName, attributeValueObjects, multiValued);
if (multiValued != null) {
tmpAttribute.setMultiValued(multiValued);
}
result.add(tmpAttribute);
}
return result;
} catch (SpannerException ex) {
throw new EntryConvertationException("Failed to convert entry!", ex);
}
}
Aggregations