Search in sources :

Example 21 with CloudSpannerSQLException

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;
}
Also used : SpannerException(com.google.cloud.spanner.SpannerException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 22 with CloudSpannerSQLException

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);
}
Also used : BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) BigDecimal(java.math.BigDecimal) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 23 with CloudSpannerSQLException

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());
}
Also used : CloudSpannerDriver(nl.topicus.jdbc.CloudSpannerDriver) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 24 with CloudSpannerSQLException

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");
}
Also used : CloudSpannerXADataSource(nl.topicus.jdbc.CloudSpannerXADataSource) CloudSpannerXAConnection(nl.topicus.jdbc.xa.CloudSpannerXAConnection) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) SQLException(java.sql.SQLException) XAException(javax.transaction.xa.XAException)

Aggregations

CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)24 SQLException (java.sql.SQLException)6 Select (net.sf.jsqlparser.statement.select.Select)6 JSQLParserException (net.sf.jsqlparser.JSQLParserException)5 ReadContext (com.google.cloud.spanner.ReadContext)4 SpannerException (com.google.cloud.spanner.SpannerException)4 Code (com.google.rpc.Code)4 PreparedStatement (java.sql.PreparedStatement)4 Statement (net.sf.jsqlparser.statement.Statement)4 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)4 Partition (com.google.cloud.spanner.Partition)3 ResultSet (java.sql.ResultSet)3 TokenMgrException (net.sf.jsqlparser.parser.TokenMgrException)3 Column (net.sf.jsqlparser.schema.Column)3 CloudSpannerConnection (nl.topicus.jdbc.CloudSpannerConnection)3 CloudSpannerDriver (nl.topicus.jdbc.CloudSpannerDriver)3 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)2 Connection (java.sql.Connection)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2