Skip to content

Commit

Permalink
Added integration test for typed query parameter and fixed Interval p…
Browse files Browse the repository at this point in the history
…arsing regex
  • Loading branch information
Sagar Agarwal committed Oct 24, 2024
1 parent f16ae42 commit c201180
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public abstract class Interval implements Serializable {

private static final Pattern INTERVAL_PATTERN =
Pattern.compile(
"^P(?!$)(-?\\d+Y)?(-?\\d+M)?(-?\\d+D)?(T(?=-?\\d)(-?\\d+H)?(-?\\d+M)?(-?\\d+(\\.\\d{1,9})?S)?)?$");
"^P(?!$)(-?\\d+Y)?(-?\\d+M)?(-?\\d+D)?(T(?=-?.?\\d)(-?\\d+H)?(-?\\d+M)?(-?((\\d+(\\.\\d{1,9})?)|(\\.\\d{1,9}))S)?)?$");

/** Returns the months component of the interval. */
public abstract int months();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@
import com.google.cloud.ByteArray;
import com.google.cloud.Date;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.*;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Interval;
import com.google.cloud.spanner.ProtobufResultSet;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.Type;
import com.google.cloud.spanner.Value;
import com.google.common.base.Preconditions;
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.ProtocolMessageEnum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,7 @@ public void testValueToProto() {
.addAllValues(
Arrays.asList(
com.google.protobuf.Value.newBuilder()
.setStringValue("P1Y2M3DT5H6M2.456787800S")
.setStringValue("P1Y2M3DT5H6M2.4567878S")
.build(),
com.google.protobuf.Value.newBuilder()
.setNullValue(NullValue.NULL_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ public void queryInterval() {
}
}

@Test
public void queryWithIntervalParam() {
String query;
if (dialect.dialect == Dialect.POSTGRESQL) {
query = "SELECT TIMESTAMP '2004-10-19 10:23:54+0530' + $1 AS Col1";
} else {
query = "SELECT TIMESTAMP('2004-10-19 10:23:54+0530') + @p1 AS Col1";
}

try (ResultSet resultSet =
client
.singleUse()
.executeQuery(
Statement.newBuilder(query)
.bind("p1")
.to(Value.interval(Interval.ofDays(10)))
.build())) {
assertTrue(resultSet.next());
assertEquals(resultSet.getTimestamp(0), Timestamp.parseTimestamp("2004-10-29 10:23:54+0530"));
}
}

@Test
public void queryWithUntypedIntervalParam() {
String query;
Expand Down

0 comments on commit c201180

Please sign in to comment.