Java Code Examples for com.google.cloud.spanner.Statement#of()
The following examples show how to use
com.google.cloud.spanner.Statement#of() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpannerStatementQueryExecutor.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
/** * Creates a Cloud Spanner statement. * @param sql the SQL string with tags. * @param tags the tags that appear in the SQL string. * @param paramStructConvertFunc a function to use to convert params to {@link Struct} * objects if they cannot be directly mapped to Cloud Spanner supported param types. * If null then this last-attempt conversion is skipped. * @param spannerCustomConverter a converter used to convert params that aren't Cloud * Spanner native types. if {@code null} then this conversion is not attempted. * @param params the parameters to substitute the tags. The ordering must be the same as * the tags. * @param queryMethodParams the parameter metadata from Query Method if available. * @return an SQL statement ready to use with Spanner. * @throws IllegalArgumentException if the number of tags does not match the number of * params, or if a param of an unsupported type is given. */ public static Statement buildStatementFromSqlWithArgs(String sql, List<String> tags, Function<Object, Struct> paramStructConvertFunc, SpannerCustomConverter spannerCustomConverter, Object[] params, Map<String, Parameter> queryMethodParams) { if (tags == null && params == null) { return Statement.of(sql); } if (tags == null || params == null || tags.size() != params.length) { throw new IllegalArgumentException( "The number of tags does not match the number of params."); } Statement.Builder builder = Statement.newBuilder(sql); for (int i = 0; i < tags.size(); i++) { bindParameter(builder.bind(tags.get(i)), paramStructConvertFunc, spannerCustomConverter, params[i], queryMethodParams == null ? null : queryMethodParams.get(tags.get(i))); } return builder.build(); }
Example 2
Source File: BeforeExecuteDmlEventTest.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
@Test public void equalsHashcodeTest() { BeforeExecuteDmlEvent beforeExecuteDmlEvent = new BeforeExecuteDmlEvent(Statement.of("a")); BeforeExecuteDmlEvent beforeExecuteDmlEvent1 = new BeforeExecuteDmlEvent(Statement.of("a")); BeforeExecuteDmlEvent beforeExecuteDmlEvent2 = new BeforeExecuteDmlEvent(Statement.of("b")); assertThat(beforeExecuteDmlEvent).isEqualTo(beforeExecuteDmlEvent); assertThat(beforeExecuteDmlEvent).isEqualTo(beforeExecuteDmlEvent1); assertThat(beforeExecuteDmlEvent2).isNotEqualTo(beforeExecuteDmlEvent); assertThat(beforeExecuteDmlEvent).isNotEqualTo(null); assertThat(beforeExecuteDmlEvent).isNotEqualTo(new Object()); assertThat(beforeExecuteDmlEvent.hashCode()).isEqualTo(beforeExecuteDmlEvent.hashCode()); assertThat(beforeExecuteDmlEvent.hashCode()).isEqualTo(beforeExecuteDmlEvent1.hashCode()); assertThat(beforeExecuteDmlEvent2.hashCode()).isNotEqualTo(beforeExecuteDmlEvent.hashCode()); }
Example 3
Source File: AfterExecuteDmlEventTest.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
@Test public void equalsHashcodeTest() { AfterExecuteDmlEvent afterExecuteDmlEventa1 = new AfterExecuteDmlEvent(Statement.of("a"), 1L); AfterExecuteDmlEvent afterExecuteDmlEventa1x = new AfterExecuteDmlEvent(Statement.of("a"), 1L); AfterExecuteDmlEvent afterExecuteDmlEventa2 = new AfterExecuteDmlEvent(Statement.of("a"), 2L); AfterExecuteDmlEvent afterExecuteDmlEventb1 = new AfterExecuteDmlEvent(Statement.of("b"), 1L); AfterExecuteDmlEvent afterExecuteDmlEventb2 = new AfterExecuteDmlEvent(Statement.of("b"), 2L); assertThat(afterExecuteDmlEventa1).isEqualTo(afterExecuteDmlEventa1); assertThat(afterExecuteDmlEventa1).isEqualTo(afterExecuteDmlEventa1x); assertThat(afterExecuteDmlEventa1).isNotEqualTo(afterExecuteDmlEventa2); assertThat(afterExecuteDmlEventb1).isNotEqualTo(afterExecuteDmlEventb2); assertThat(afterExecuteDmlEventa1).isNotEqualTo(afterExecuteDmlEventb2); assertThat(afterExecuteDmlEventa1).isNotEqualTo(null); assertThat(afterExecuteDmlEventa1).isNotEqualTo(new Object()); assertThat(afterExecuteDmlEventa1.hashCode()).isEqualTo(afterExecuteDmlEventa1.hashCode()); assertThat(afterExecuteDmlEventa1.hashCode()).isEqualTo(afterExecuteDmlEventa1x.hashCode()); assertThat(afterExecuteDmlEventa1.hashCode()).isNotEqualTo(afterExecuteDmlEventa2.hashCode()); assertThat(afterExecuteDmlEventb1.hashCode()).isNotEqualTo(afterExecuteDmlEventb2.hashCode()); assertThat(afterExecuteDmlEventa1.hashCode()).isNotEqualTo(afterExecuteDmlEventb2.hashCode()); }
Example 4
Source File: App.java From java-docs-samples with Apache License 2.0 | 6 votes |
static void query(DatabaseClient dbClient) { Statement statement = Statement.of( "SELECT p.PlayerId, p.PlayerName, s.Score, s.Timestamp " + "FROM Players p " + "JOIN Scores s ON p.PlayerId = s.PlayerId " + "ORDER BY s.Score DESC LIMIT 10"); ResultSet resultSet = dbClient.singleUse().executeQuery(statement); while (resultSet.next()) { String scoreDate = String.valueOf(resultSet.getTimestamp("Timestamp")); String score = String.format("%,d", resultSet.getLong("Score")); System.out.printf( "PlayerId: %d PlayerName: %s Score: %s Timestamp: %s\n", resultSet.getLong("PlayerId"), resultSet.getString("PlayerName"), score, scoreDate.substring(0,10)); } }
Example 5
Source File: App.java From java-docs-samples with Apache License 2.0 | 6 votes |
static void query(DatabaseClient dbClient) { Statement statement = Statement.of( "SELECT p.PlayerId, p.PlayerName, s.Score, s.Timestamp " + "FROM Players p " + "JOIN Scores s ON p.PlayerId = s.PlayerId " + "ORDER BY s.Score DESC LIMIT 10"); ResultSet resultSet = dbClient.singleUse().executeQuery(statement); while (resultSet.next()) { String scoreDate = String.valueOf(resultSet.getTimestamp("Timestamp")); String score = String.format("%,d", resultSet.getLong("Score")); System.out.printf( "PlayerId: %d PlayerName: %s Score: %s Timestamp: %s\n", resultSet.getLong("PlayerId"), resultSet.getString("PlayerName"), score, scoreDate.substring(0,10)); } }
Example 6
Source File: SpannerTemplate.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
@Override public <T> long count(Class<T> entityClass) { SpannerPersistentEntity<?> persistentEntity = this.mappingContext .getPersistentEntity(entityClass); Statement statement = Statement.of( String.format("SELECT COUNT(*) FROM %s", persistentEntity.tableName())); try (ResultSet resultSet = executeQuery(statement, null)) { resultSet.next(); return resultSet.getLong(0); } }
Example 7
Source File: SpannerTemplateTests.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
@Test public void queryTest() { when(this.readContext.read(any(), any(), any())).thenReturn(null); Statement query = Statement.of("test"); verifyAfterEvents(new AfterQueryEvent(Collections.emptyList(), query, null), () -> assertThat(this.spannerTemplate.query(TestEntity.class, query, null)).isEmpty(), x -> { }); verify(this.databaseClient, times(1)).singleUse(); }
Example 8
Source File: SpannerTemplateTests.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
@Test public void queryFuncTest() { ResultSet resultSet = mock(ResultSet.class); when(resultSet.next()).thenReturn(false); Statement query = Statement.of("test"); when(this.readContext.executeQuery(eq(query))).thenReturn(resultSet); verifyAfterEvents(new AfterQueryEvent(Collections.emptyList(), query, null), () -> assertThat(this.spannerTemplate.query(x -> null, query, null)).isEmpty(), x -> { }); }