Search in sources :

Example 1 with StatementType

use of com.google.cloud.spanner.connection.AbstractStatementParser.StatementType in project java-spanner by googleapis.

the class AbstractConnectionImplTest method testAnalyzeQuery.

private void testAnalyzeQuery(final StatementType type) {
    // TODO: add log statements when ANALYZE ... sql statements are supported
    try (Connection connection = getConnection()) {
        for (QueryAnalyzeMode mode : QueryAnalyzeMode.values()) {
            final QueryAnalyzeMode currentMode = mode;
            if (type == StatementType.QUERY && isExecuteAllowed(StatementType.QUERY)) {
                ResultSet rs = connection.analyzeQuery(getTestStatement(type), currentMode);
                assertThat(rs, is(notNullValue()));
                while (rs.next()) {
                }
                assertThat(rs.getStats(), is(notNullValue()));
            } else if (type == StatementType.QUERY) {
                // it is a query, but queries are not allowed for this connection state
                expectSpannerException(type + " should not be allowed", t -> t.analyzeQuery(getTestStatement(type), currentMode), connection, ErrorCode.FAILED_PRECONDITION);
            } else {
                expectSpannerException(type + " should be an invalid argument", t -> t.analyzeQuery(getTestStatement(type), currentMode), connection, ErrorCode.INVALID_ARGUMENT);
            }
        }
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) QueryAnalyzeMode(com.google.cloud.spanner.ReadContext.QueryAnalyzeMode) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) ReadOnlyStalenessUtil.getTimeUnitAbbreviation(com.google.cloud.spanner.connection.ReadOnlyStalenessUtil.getTimeUnitAbbreviation) RunWith(org.junit.runner.RunWith) Timestamp(com.google.cloud.Timestamp) StatementType(com.google.cloud.spanner.connection.AbstractStatementParser.StatementType) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) ResultSet(com.google.cloud.spanner.ResultSet) OutputStreamWriter(java.io.OutputStreamWriter) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) PrintWriter(java.io.PrintWriter) SpannerExceptionMatcher.matchCode(com.google.cloud.spanner.connection.SpannerExceptionMatcher.matchCode) AfterClass(org.junit.AfterClass) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Test(org.junit.Test) Mutation(com.google.cloud.spanner.Mutation) JUnit4(org.junit.runners.JUnit4) TimestampBound(com.google.cloud.spanner.TimestampBound) StandardCharsets(java.nio.charset.StandardCharsets) ErrorCode(com.google.cloud.spanner.ErrorCode) SpannerException(com.google.cloud.spanner.SpannerException) Statement(com.google.cloud.spanner.Statement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Rule(org.junit.Rule) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) QueryAnalyzeMode(com.google.cloud.spanner.ReadContext.QueryAnalyzeMode) ResultSet(com.google.cloud.spanner.ResultSet)

Example 2 with StatementType

use of com.google.cloud.spanner.connection.AbstractStatementParser.StatementType in project java-spanner by googleapis.

the class AbstractConnectionImplTest method testExecuteQuery.

private void testExecuteQuery(final StatementType type) {
    try (Connection connection = getConnection()) {
        if (type == StatementType.QUERY && isExecuteAllowed(StatementType.QUERY)) {
            log("@EXPECT RESULT_SET 'TEST',1");
            log(getTestStatement(type).getSql() + ";");
            ResultSet rs = connection.executeQuery(getTestStatement(type));
            assertThat(rs, is(notNullValue()));
            assertThat(rs.getStats(), is(nullValue()));
        } else if (type == StatementType.QUERY) {
            // it is a query, but queries are not allowed for this connection state
            expectSpannerException(type + " should not be allowed", t -> {
                log("@EXPECT EXCEPTION FAILED_PRECONDITION");
                log(getTestStatement(type).getSql() + ";");
                t.executeQuery(getTestStatement(type));
            }, connection, ErrorCode.FAILED_PRECONDITION);
        } else {
            expectSpannerException(type + " should be an invalid argument", t -> t.executeQuery(getTestStatement(type)), connection, ErrorCode.INVALID_ARGUMENT);
        }
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) QueryAnalyzeMode(com.google.cloud.spanner.ReadContext.QueryAnalyzeMode) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) ReadOnlyStalenessUtil.getTimeUnitAbbreviation(com.google.cloud.spanner.connection.ReadOnlyStalenessUtil.getTimeUnitAbbreviation) RunWith(org.junit.runner.RunWith) Timestamp(com.google.cloud.Timestamp) StatementType(com.google.cloud.spanner.connection.AbstractStatementParser.StatementType) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) ResultSet(com.google.cloud.spanner.ResultSet) OutputStreamWriter(java.io.OutputStreamWriter) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) PrintWriter(java.io.PrintWriter) SpannerExceptionMatcher.matchCode(com.google.cloud.spanner.connection.SpannerExceptionMatcher.matchCode) AfterClass(org.junit.AfterClass) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Test(org.junit.Test) Mutation(com.google.cloud.spanner.Mutation) JUnit4(org.junit.runners.JUnit4) TimestampBound(com.google.cloud.spanner.TimestampBound) StandardCharsets(java.nio.charset.StandardCharsets) ErrorCode(com.google.cloud.spanner.ErrorCode) SpannerException(com.google.cloud.spanner.SpannerException) Statement(com.google.cloud.spanner.Statement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Rule(org.junit.Rule) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ResultSet(com.google.cloud.spanner.ResultSet)

Aggregations

Timestamp (com.google.cloud.Timestamp)2 ErrorCode (com.google.cloud.spanner.ErrorCode)2 Mutation (com.google.cloud.spanner.Mutation)2 QueryAnalyzeMode (com.google.cloud.spanner.ReadContext.QueryAnalyzeMode)2 ResultSet (com.google.cloud.spanner.ResultSet)2 SpannerException (com.google.cloud.spanner.SpannerException)2 Statement (com.google.cloud.spanner.Statement)2 TimestampBound (com.google.cloud.spanner.TimestampBound)2 StatementType (com.google.cloud.spanner.connection.AbstractStatementParser.StatementType)2 ReadOnlyStalenessUtil.getTimeUnitAbbreviation (com.google.cloud.spanner.connection.ReadOnlyStalenessUtil.getTimeUnitAbbreviation)2 SpannerExceptionMatcher.matchCode (com.google.cloud.spanner.connection.SpannerExceptionMatcher.matchCode)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2