Search in sources :

Example 1 with JdbcSqlExceptionImpl

use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl in project java-spanner-jdbc by googleapis.

the class JdbcConnectionTest method testCatalog.

@Test
public void testCatalog() throws SQLException {
    ConnectionOptions options = mockOptions();
    when(options.getDatabaseName()).thenReturn("test");
    try (JdbcConnection connection = createConnection(options)) {
        // The connection should always return the empty string as the current catalog, as no other
        // catalogs exist in the INFORMATION_SCHEMA.
        assertThat(connection.getCatalog()).isEqualTo("");
        // This should be allowed.
        connection.setCatalog("");
        try {
            // This should cause an exception.
            connection.setCatalog("other");
            fail("missing expected exception");
        } catch (JdbcSqlExceptionImpl e) {
            assertThat(e.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
        }
    }
}
Also used : JdbcSqlExceptionImpl(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl) ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) ConnectionImplTest(com.google.cloud.spanner.connection.ConnectionImplTest) AbstractConnectionImplTest(com.google.cloud.spanner.connection.AbstractConnectionImplTest) Test(org.junit.Test)

Example 2 with JdbcSqlExceptionImpl

use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl in project java-spanner-jdbc by googleapis.

the class JdbcStatementTest method testExecuteUpdate.

@Test
public void testExecuteUpdate() throws SQLException {
    Statement statement = createStatement();
    assertThat(statement.executeUpdate(UPDATE)).isEqualTo(1);
    try {
        statement.executeUpdate(LARGE_UPDATE);
        fail("missing expected exception");
    } catch (JdbcSqlExceptionImpl e) {
        assertThat(e.getCode()).isEqualTo(Code.OUT_OF_RANGE);
    }
}
Also used : JdbcSqlExceptionImpl(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl) Statement(java.sql.Statement) Test(org.junit.Test)

Example 3 with JdbcSqlExceptionImpl

use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl in project java-spanner-jdbc by googleapis.

the class JdbcStatementTest method testExecuteWithUpdateStatement.

@Test
public void testExecuteWithUpdateStatement() throws SQLException {
    Statement statement = createStatement();
    boolean res = statement.execute(UPDATE);
    assertThat(res).isFalse();
    assertThat(statement.getResultSet()).isNull();
    assertThat(statement.getUpdateCount()).isEqualTo(1);
    assertThat(statement.execute(LARGE_UPDATE)).isFalse();
    assertThat(statement.getResultSet()).isNull();
    try {
        statement.getUpdateCount();
        fail("missing expected exception");
    } catch (JdbcSqlExceptionImpl e) {
        assertThat(e.getCode()).isEqualTo(Code.OUT_OF_RANGE);
    }
    assertThat(statement.getLargeUpdateCount()).isEqualTo(Integer.MAX_VALUE + 1L);
}
Also used : JdbcSqlExceptionImpl(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl) Statement(java.sql.Statement) Test(org.junit.Test)

Example 4 with JdbcSqlExceptionImpl

use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl in project java-spanner-jdbc by googleapis.

the class JdbcStatementTest method testInternalExecuteUpdate.

@Test
public void testInternalExecuteUpdate() throws SQLException {
    JdbcConnection connection = mock(JdbcConnection.class);
    when(connection.getDialect()).thenReturn(dialect);
    Connection spannerConnection = mock(Connection.class);
    when(connection.getSpannerConnection()).thenReturn(spannerConnection);
    com.google.cloud.spanner.Statement updateStatement = com.google.cloud.spanner.Statement.of(UPDATE);
    com.google.cloud.spanner.Statement largeUpdateStatement = com.google.cloud.spanner.Statement.of(LARGE_UPDATE);
    when(spannerConnection.executeUpdate(updateStatement)).thenReturn(1L);
    when(spannerConnection.executeUpdate(largeUpdateStatement)).thenReturn(Integer.MAX_VALUE + 1L);
    try (JdbcStatement statement = new JdbcStatement(connection)) {
        assertThat(statement.executeUpdate(updateStatement)).isEqualTo(1);
        try {
            statement.executeUpdate(largeUpdateStatement);
            fail("missing expected exception");
        } catch (JdbcSqlExceptionImpl e) {
            assertThat(e.getCode()).isEqualTo(Code.OUT_OF_RANGE);
        }
    }
}
Also used : JdbcSqlExceptionImpl(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl) Connection(com.google.cloud.spanner.connection.Connection) Test(org.junit.Test)

Example 5 with JdbcSqlExceptionImpl

use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl in project java-spanner-jdbc by googleapis.

the class JdbcConnectionTest method testIsValid.

@Test
public void testIsValid() throws SQLException {
    // Setup.
    ConnectionOptions options = mockOptions();
    com.google.cloud.spanner.connection.Connection spannerConnection = mock(com.google.cloud.spanner.connection.Connection.class);
    when(spannerConnection.getDialect()).thenReturn(dialect);
    when(options.getConnection()).thenReturn(spannerConnection);
    Statement statement = Statement.of(JdbcConnection.IS_VALID_QUERY);
    // Verify that an opened connection that returns a result set is valid.
    try (JdbcConnection connection = new JdbcConnection("url", options)) {
        when(spannerConnection.executeQuery(statement)).thenReturn(createSelect1ResultSet());
        assertThat(connection.isValid(1)).isTrue();
        try {
            // Invalid timeout value.
            connection.isValid(-1);
            fail("missing expected exception");
        } catch (JdbcSqlExceptionImpl e) {
            assertThat(e.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
        }
        // Now let the query return an error. isValid should now return false.
        when(spannerConnection.executeQuery(statement)).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, "the current transaction has been aborted"));
        assertThat(connection.isValid(1)).isFalse();
    }
}
Also used : JdbcSqlExceptionImpl(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl) PreparedStatement(java.sql.PreparedStatement) Statement(com.google.cloud.spanner.Statement) ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) ConnectionImplTest(com.google.cloud.spanner.connection.ConnectionImplTest) AbstractConnectionImplTest(com.google.cloud.spanner.connection.AbstractConnectionImplTest) Test(org.junit.Test)

Aggregations

JdbcSqlExceptionImpl (com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl)5 Test (org.junit.Test)5 AbstractConnectionImplTest (com.google.cloud.spanner.connection.AbstractConnectionImplTest)2 ConnectionImplTest (com.google.cloud.spanner.connection.ConnectionImplTest)2 ConnectionOptions (com.google.cloud.spanner.connection.ConnectionOptions)2 Statement (java.sql.Statement)2 Statement (com.google.cloud.spanner.Statement)1 Connection (com.google.cloud.spanner.connection.Connection)1 PreparedStatement (java.sql.PreparedStatement)1