use of nl.topicus.jdbc.exception.CloudSpannerSQLException in project spanner-jdbc by olavloite.
the class CloudSpannerResultSet method next.
@Override
public boolean next() throws SQLException {
ensureOpen();
if (beforeFirst && nextCalledForInternalReasons) {
currentRowIndex++;
nextCalledForInternalReasons = false;
beforeFirst = false;
afterLast = !nextCalledForInternalReasonsResult;
return nextCalledForInternalReasonsResult;
}
boolean res = false;
try {
res = resultSet.next();
} catch (SpannerException e) {
throw new CloudSpannerSQLException(e);
}
currentRowIndex++;
beforeFirst = false;
afterLast = !res;
return res;
}
use of nl.topicus.jdbc.exception.CloudSpannerSQLException in project spanner-jdbc by olavloite.
the class CloudSpannerConversionUtil method convert.
/**
* Converts the given value from the Google {@link Type} to the Java {@link Class} type.
*
* @param value The value to convert
* @param type The type in the database
* @param targetType The java class target type to convert to
* @return The converted value
* @throws CloudSpannerSQLException Thrown if the given value cannot be converted to the specified
* type
*/
public static Object convert(Object value, Type type, Class<?> targetType) throws CloudSpannerSQLException {
Preconditions.checkNotNull(type, "type may not be null");
Preconditions.checkNotNull(targetType, "targetType may not be null");
if (value == null)
return null;
if (targetType.equals(String.class))
return value.toString();
try {
if (targetType.equals(Boolean.class) && type.getCode() == Code.BOOL)
return value;
if (targetType.equals(Boolean.class) && type.getCode() == Code.INT64)
return Boolean.valueOf((Long) value != 0);
if (targetType.equals(Boolean.class) && type.getCode() == Code.FLOAT64)
return Boolean.valueOf((Double) value != 0d);
if (targetType.equals(Boolean.class) && type.getCode() == Code.STRING)
return Boolean.valueOf((String) value);
if (targetType.equals(BigDecimal.class) && type.getCode() == Code.BOOL)
return (Boolean) value ? BigDecimal.ONE : BigDecimal.ZERO;
if (targetType.equals(BigDecimal.class) && type.getCode() == Code.INT64)
return BigDecimal.valueOf((Long) value);
if (targetType.equals(BigDecimal.class) && type.getCode() == Code.FLOAT64)
return BigDecimal.valueOf((Double) value);
if (targetType.equals(BigDecimal.class) && type.getCode() == Code.STRING)
return new BigDecimal((String) value);
if (targetType.equals(Long.class) && type.getCode() == Code.BOOL)
return (Boolean) value ? 1L : 0L;
if (targetType.equals(Long.class) && type.getCode() == Code.INT64)
return value;
if (targetType.equals(Long.class) && type.getCode() == Code.FLOAT64)
return ((Double) value).longValue();
if (targetType.equals(Long.class) && type.getCode() == Code.STRING)
return Long.valueOf((String) value);
if (targetType.equals(Integer.class) && type.getCode() == Code.BOOL)
return (Boolean) value ? 1 : 0;
if (targetType.equals(Integer.class) && type.getCode() == Code.INT64)
return ((Long) value).intValue();
if (targetType.equals(Integer.class) && type.getCode() == Code.FLOAT64)
return ((Double) value).intValue();
if (targetType.equals(Integer.class) && type.getCode() == Code.STRING)
return Integer.valueOf((String) value);
if (targetType.equals(BigInteger.class) && type.getCode() == Code.BOOL)
return (Boolean) value ? BigInteger.ONE : BigInteger.ZERO;
if (targetType.equals(BigInteger.class) && type.getCode() == Code.INT64)
return BigInteger.valueOf((Long) value);
if (targetType.equals(BigInteger.class) && type.getCode() == Code.FLOAT64)
return BigInteger.valueOf(((Double) value).longValue());
if (targetType.equals(BigInteger.class) && type.getCode() == Code.STRING)
return new BigInteger((String) value);
if (targetType.equals(Float.class) && type.getCode() == Code.BOOL)
return (Boolean) value ? Float.valueOf(1f) : Float.valueOf(0f);
if (targetType.equals(Float.class) && type.getCode() == Code.INT64)
return ((Long) value).floatValue();
if (targetType.equals(Float.class) && type.getCode() == Code.FLOAT64)
return ((Double) value).floatValue();
if (targetType.equals(Float.class) && type.getCode() == Code.STRING)
return Float.valueOf((String) value);
if (targetType.equals(Double.class) && type.getCode() == Code.BOOL)
return (Boolean) value ? Double.valueOf(1d) : Double.valueOf(0d);
if (targetType.equals(Double.class) && type.getCode() == Code.INT64)
return ((Long) value).doubleValue();
if (targetType.equals(Double.class) && type.getCode() == Code.FLOAT64)
return value;
if (targetType.equals(Double.class) && type.getCode() == Code.STRING)
return Double.valueOf((String) value);
} catch (Exception e) {
throw new CloudSpannerSQLException("Cannot convert " + value + " to " + targetType.getName(), com.google.rpc.Code.INVALID_ARGUMENT, e);
}
throw new CloudSpannerSQLException("Cannot convert " + type.getCode().name() + " to " + targetType.getName(), com.google.rpc.Code.INVALID_ARGUMENT);
}
use of nl.topicus.jdbc.exception.CloudSpannerSQLException in project spanner-jdbc by olavloite.
the class CloudSpannerIT method createConnection.
private Connection createConnection() throws SQLException {
try {
Class.forName(CloudSpannerDriver.class.getName());
} catch (ClassNotFoundException e) {
throw new CloudSpannerSQLException("Could not load JDBC driver", Code.UNKNOWN, e);
}
StringBuilder url = new StringBuilder("jdbc:cloudspanner:");
url.append(getHost());
url.append(";Project=").append(getProject());
url.append(";Instance=").append(instanceId);
url.append(";Database=").append(DATABASE_ID);
url.append(";PvtKeyPath=").append(credentialsPath);
url.append(";UseCustomHost=true");
return DriverManager.getConnection(url.toString());
}
use of nl.topicus.jdbc.exception.CloudSpannerSQLException in project spanner-jdbc by olavloite.
the class XATester method testXA.
public void testXA(String projectId, String instanceId, String database, String pvtKeyPath) throws SQLException {
log.info("Starting XA tests");
int originalLogLevel = CloudSpannerDriver.getLogLevel();
CloudSpannerDriver.setLogLevel(CloudSpannerDriver.DEBUG);
CloudSpannerXADataSource ds = new CloudSpannerXADataSource();
ds.setHost(CloudSpannerIT.getHost());
ds.setUseCustomHost(true);
ds.setProjectId(projectId);
ds.setInstanceId(instanceId);
ds.setDatabase(database);
ds.setPvtKeyPath(pvtKeyPath);
ds.setAllowExtendedMode(true);
try (CloudSpannerXAConnection xaConnection = ds.getXAConnection()) {
testXATransaction(xaConnection, CommitMode.TwoPhase);
testXARollback(xaConnection);
deleteTestRow(xaConnection);
testXARecover(xaConnection);
deleteTestRow(xaConnection);
} catch (Exception e) {
throw new CloudSpannerSQLException("Exception occurred during XA tests", Code.INTERNAL, e);
} finally {
CloudSpannerDriver.setLogLevel(originalLogLevel);
}
log.info("Finished XA tests");
}
Aggregations