Skip to content

Commit d677799

Browse files
Address comments
1 parent 899400c commit d677799

File tree

7 files changed

+56
-50
lines changed

7 files changed

+56
-50
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseClient.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,14 +609,15 @@ ServerStream<BatchWriteResponse> batchWriteAtLeastOnce(
609609
long executePartitionedUpdate(Statement stmt, UpdateOption... options);
610610

611611
/**
612-
* Returns StatementFactory for the given dialect. With StatementFactory, unnamed parameterized
613-
* queries can be passed along with the values to create a Statement.
612+
* Returns StatementFactory for the given dialect.
613+
*
614+
* <p>A {@link StatementFactory}, can be used to create statements with unnamed parameters.
614615
*
615616
* <p>Examples using {@link StatementFactory}
616617
*
617-
* <p>databaseClient.newStatementFactory().of("SELECT NAME FROM TABLE WHERE ID = ?", 10)
618+
* <p>databaseClient.getStatementFactory().of("SELECT NAME FROM TABLE WHERE ID = ?", 10)
618619
*/
619-
default StatementFactory newStatementFactory() {
620+
default StatementFactory getStatementFactory() {
620621
throw new UnsupportedOperationException("method should be overwritten");
621622
}
622623
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseClientImpl.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,28 @@ public Dialect getDialect() {
146146
return pool.getDialect();
147147
}
148148

149+
private final AbstractLazyInitializer<StatementFactory> statementFactorySupplier =
150+
new AbstractLazyInitializer<StatementFactory>() {
151+
@Override
152+
protected StatementFactory initialize() {
153+
try {
154+
Dialect dialect = getDialectAsync().get(30, TimeUnit.SECONDS);
155+
return new StatementFactory(dialect);
156+
} catch (ExecutionException | TimeoutException e) {
157+
throw SpannerExceptionFactory.asSpannerException(e);
158+
} catch (InterruptedException e) {
159+
throw SpannerExceptionFactory.propagateInterrupt(e);
160+
}
161+
}
162+
};
163+
149164
@Override
150-
public StatementFactory newStatementFactory() {
151-
if (statementFactory == null) {
152-
try {
153-
Dialect dialect = getDialectAsync().get(5, TimeUnit.SECONDS);
154-
statementFactory = new StatementFactory(dialect);
155-
} catch (ExecutionException | TimeoutException e) {
156-
throw SpannerExceptionFactory.asSpannerException(e);
157-
} catch (InterruptedException e) {
158-
throw SpannerExceptionFactory.propagateInterrupt(e);
159-
}
165+
public StatementFactory getStatementFactory() {
166+
try {
167+
return statementFactorySupplier.get();
168+
} catch (Exception exception) {
169+
throw SpannerExceptionFactory.asSpannerException(exception);
160170
}
161-
return statementFactory;
162171
}
163172

164173
@Override

google-cloud-spanner/src/main/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ public Dialect getDialect() {
653653
}
654654
}
655655

656-
public Future<Dialect> getDialectAsync() {
656+
Future<Dialect> getDialectAsync() {
657657
try {
658658
return MAINTAINER_SERVICE.submit(dialectSupplier::get);
659659
} catch (Exception exception) {

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerTypeConverter.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.google.cloud.Date;
2020
import com.google.protobuf.ListValue;
21-
import java.text.SimpleDateFormat;
2221
import java.time.LocalDate;
2322
import java.time.LocalDateTime;
2423
import java.time.OffsetDateTime;
@@ -35,10 +34,7 @@
3534

3635
final class SpannerTypeConverter {
3736

38-
private static final String DATE_PATTERN = "yyyy-MM-dd";
39-
private static final SimpleDateFormat SIMPLE_DATE_FORMATTER = new SimpleDateFormat(DATE_PATTERN);
4037
private static final ZoneId UTC_ZONE = ZoneId.of("UTC");
41-
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_PATTERN);
4238
private static final DateTimeFormatter ISO_8601_DATE_FORMATTER =
4339
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
4440

@@ -61,7 +57,7 @@ static <T extends TemporalAccessor> String convertToISO8601(T dateTime) {
6157
return ISO_8601_DATE_FORMATTER.format(dateTime);
6258
}
6359

64-
static <T> Value createUntypedValue(T value) {
60+
static <T> Value createUntypedStringValue(T value) {
6561
return Value.untyped(
6662
com.google.protobuf.Value.newBuilder().setStringValue(String.valueOf(value)).build());
6763
}
@@ -79,12 +75,8 @@ static <T> Iterable<T> convertToTypedIterable(T val, Iterator<?> iterator) {
7975
return convertToTypedIterable(v -> v, val, iterator);
8076
}
8177

82-
static Date convertUtilDateToSpannerDate(java.util.Date date) {
83-
return Date.parseDate(SIMPLE_DATE_FORMATTER.format(date));
84-
}
85-
8678
static Date convertLocalDateToSpannerDate(LocalDate date) {
87-
return Date.parseDate(DATE_FORMATTER.format(date));
79+
return Date.fromYearMonthDay(date.getYear(), date.getMonthValue(), date.getDayOfMonth());
8880
}
8981

9082
static <T> Value createUntypedIterableValue(
@@ -104,15 +96,15 @@ static <T> Value createUntypedIterableValue(
10496
.build());
10597
}
10698

107-
static ZonedDateTime convertToUTCTimezone(LocalDateTime localDateTime) {
99+
static ZonedDateTime atUTC(LocalDateTime localDateTime) {
108100
return localDateTime.atZone(UTC_ZONE);
109101
}
110102

111-
static ZonedDateTime convertToUTCTimezone(OffsetDateTime localDateTime) {
103+
static ZonedDateTime atUTC(OffsetDateTime localDateTime) {
112104
return localDateTime.atZoneSameInstant(UTC_ZONE);
113105
}
114106

115-
static ZonedDateTime convertToUTCTimezone(ZonedDateTime localDateTime) {
107+
static ZonedDateTime atUTC(ZonedDateTime localDateTime) {
116108
return localDateTime.withZoneSameInstant(UTC_ZONE);
117109
}
118110
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Statement.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ public Statement of(String sql) {
261261
}
262262

263263
/**
264-
* @param sql SQL statement with unnamed parameter denoted as ?
265-
* @param values list of values which needs to replace ? in the sql
266-
* @return Statement object
264+
* @param sql SQL statement with unnamed parameters denoted as ?
265+
* @param values positional list of values for the unnamed parameters in the SQL string
266+
* @return Statement a statement that can be executed on Spanner
267267
* <p>This function accepts the SQL statement with unnamed parameters(?) and accepts the
268268
* list of objects to replace unnamed parameters. Primitive types are supported
269269
* <p>For Date column, following types are supported
@@ -281,9 +281,9 @@ public Statement of(String sql) {
281281
* <li>ZonedDateTime
282282
* </ul>
283283
* <p>
284-
* @see DatabaseClient#newStatementFactory
284+
* @see DatabaseClient#getStatementFactory
285285
*/
286-
public Statement of(String sql, Object... values) {
286+
public Statement withUnnamedParameters(String sql, Object... values) {
287287
Map<String, Value> parameters = getUnnamedParametersMap(values);
288288
AbstractStatementParser statementParser = AbstractStatementParser.getInstance(this.dialect);
289289
ParametersInfo parametersInfo =

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Value.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
package com.google.cloud.spanner;
1818

19+
import static com.google.cloud.spanner.SpannerTypeConverter.atUTC;
1920
import static com.google.cloud.spanner.SpannerTypeConverter.convertLocalDateToSpannerDate;
2021
import static com.google.cloud.spanner.SpannerTypeConverter.convertToISO8601;
2122
import static com.google.cloud.spanner.SpannerTypeConverter.convertToTypedIterable;
22-
import static com.google.cloud.spanner.SpannerTypeConverter.convertToUTCTimezone;
2323
import static com.google.cloud.spanner.SpannerTypeConverter.createUntypedArrayValue;
2424
import static com.google.cloud.spanner.SpannerTypeConverter.createUntypedIterableValue;
25-
import static com.google.cloud.spanner.SpannerTypeConverter.createUntypedValue;
25+
import static com.google.cloud.spanner.SpannerTypeConverter.createUntypedStringValue;
2626

2727
import com.google.cloud.ByteArray;
2828
import com.google.cloud.Date;
@@ -846,7 +846,7 @@ static Value toValue(Object value) {
846846
return Value.bool((Boolean) value);
847847
}
848848
if (value instanceof Long || value instanceof Integer) {
849-
return createUntypedValue(String.valueOf(value));
849+
return createUntypedStringValue(String.valueOf(value));
850850
}
851851
if (value instanceof Float) {
852852
return Value.float32((Float) value);
@@ -867,19 +867,21 @@ static Value toValue(Object value) {
867867
return Value.date((Date) value);
868868
}
869869
if (value instanceof java.util.Date) {
870-
return Value.date(SpannerTypeConverter.convertUtilDateToSpannerDate((java.util.Date) value));
870+
return Value.date(Date.fromJavaUtilDate((java.util.Date) value));
871871
}
872872
if (value instanceof LocalDate) {
873873
return Value.date(convertLocalDateToSpannerDate((LocalDate) value));
874874
}
875875
if (value instanceof LocalDateTime) {
876-
return createUntypedValue(convertToISO8601(convertToUTCTimezone((LocalDateTime) value)));
876+
return createUntypedStringValue(
877+
convertToISO8601(SpannerTypeConverter.atUTC((LocalDateTime) value)));
877878
}
878879
if (value instanceof OffsetDateTime) {
879-
return createUntypedValue(convertToISO8601(convertToUTCTimezone((OffsetDateTime) value)));
880+
return createUntypedStringValue(
881+
convertToISO8601(SpannerTypeConverter.atUTC((OffsetDateTime) value)));
880882
}
881883
if (value instanceof ZonedDateTime) {
882-
return createUntypedValue(convertToISO8601(convertToUTCTimezone((ZonedDateTime) value)));
884+
return createUntypedStringValue(convertToISO8601(atUTC((ZonedDateTime) value)));
883885
}
884886
if (value instanceof ProtocolMessageEnum) {
885887
return Value.protoEnum((ProtocolMessageEnum) value);
@@ -939,10 +941,7 @@ static Value toValue(Object value) {
939941
}
940942
if (object instanceof java.util.Date) {
941943
return Value.dateArray(
942-
convertToTypedIterable(
943-
SpannerTypeConverter::convertUtilDateToSpannerDate,
944-
(java.util.Date) object,
945-
iterator));
944+
convertToTypedIterable(Date::fromJavaUtilDate, (java.util.Date) object, iterator));
946945
}
947946
if (object instanceof LocalDate) {
948947
return Value.dateArray(
@@ -951,15 +950,19 @@ static Value toValue(Object value) {
951950
}
952951
if (object instanceof LocalDateTime) {
953952
return createUntypedIterableValue(
954-
(LocalDateTime) object, iterator, val -> convertToISO8601(convertToUTCTimezone(val)));
953+
(LocalDateTime) object,
954+
iterator,
955+
val -> convertToISO8601(SpannerTypeConverter.atUTC(val)));
955956
}
956957
if (object instanceof OffsetDateTime) {
957958
return createUntypedIterableValue(
958-
(OffsetDateTime) object, iterator, val -> convertToISO8601(convertToUTCTimezone(val)));
959+
(OffsetDateTime) object,
960+
iterator,
961+
val -> convertToISO8601(SpannerTypeConverter.atUTC(val)));
959962
}
960963
if (object instanceof ZonedDateTime) {
961964
return createUntypedIterableValue(
962-
(ZonedDateTime) object, iterator, val -> convertToISO8601(convertToUTCTimezone(val)));
965+
(ZonedDateTime) object, iterator, val -> convertToISO8601(atUTC(val)));
963966
}
964967
}
965968

@@ -995,7 +998,7 @@ static Value toValue(Object value) {
995998
return createUntypedArrayValue(Arrays.stream((int[]) value).boxed());
996999
}
9971000

998-
return createUntypedValue(value);
1001+
return createUntypedStringValue(value);
9991002
}
10001003

10011004
/** Returns the type of this value. This will return a type even if {@code isNull()} is true. */

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4901,7 +4901,8 @@ public void testStatementWithIUnnamedParametersParameter() {
49014901
DatabaseClient client =
49024902
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
49034903

4904-
Statement statement = client.newStatementFactory().of("select id from test where b=?", true);
4904+
Statement statement =
4905+
client.getStatementFactory().withUnnamedParameters("select id from test where b=?", true);
49054906
Statement generatedStatement =
49064907
Statement.newBuilder("select id from test where b=@p1").bind("p1").to(true).build();
49074908
mockSpanner.putStatementResult(StatementResult.query(generatedStatement, SELECT1_RESULTSET));

0 commit comments

Comments
 (0)