Skip to content

Commit f953f76

Browse files
olavloitesgorse12
andauthored
feat: add option to indicate that a statement is the last in a transaction (#3644)
* feat: Add LastStatement DML option * Removing debugging changes --------- Co-authored-by: Shirdon Gorse <[email protected]>
1 parent b2ba1f3 commit f953f76

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,9 @@ ExecuteSqlRequest.Builder getExecuteSqlRequestBuilder(
698698
if (!isReadOnly()) {
699699
builder.setSeqno(getSeqNo());
700700
}
701+
if (options.hasLastStatementSet()) {
702+
builder.setLastStatement(options.lastStatementSet());
703+
}
701704
builder.setQueryOptions(buildQueryOptions(statement.getQueryOptions()));
702705
builder.setRequestOptions(buildRequestOptions(options));
703706
return builder;
@@ -743,6 +746,9 @@ ExecuteBatchDmlRequest.Builder getExecuteBatchDmlRequestBuilder(
743746
if (selector != null) {
744747
builder.setTransaction(selector);
745748
}
749+
if (options.hasLastStatementSet()) {
750+
builder.setLastStatements(options.lastStatementSet());
751+
}
746752
builder.setSeqno(getSeqNo());
747753
builder.setRequestOptions(buildRequestOptions(options));
748754
return builder;

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,20 @@ public static DataBoostQueryOption dataBoostEnabled(Boolean dataBoostEnabled) {
236236
return new DataBoostQueryOption(dataBoostEnabled);
237237
}
238238

239+
/**
240+
* If set to true, this option marks the end of the transaction. The transaction should be
241+
* committed or aborted after this statement executes, and attempts to execute any other requests
242+
* against this transaction (including reads and queries) will be rejected. Mixing mutations with
243+
* statements that are marked as the last statement is not allowed.
244+
*
245+
* <p>For DML statements, setting this option may cause some error reporting to be deferred until
246+
* commit time (e.g. validation of unique constraints). Given this, successful execution of a DML
247+
* statement should not be assumed until the transaction commits.
248+
*/
249+
public static LastStatementUpdateOption lastStatementSet(Boolean lastStatementSet) {
250+
return new LastStatementUpdateOption(lastStatementSet);
251+
}
252+
239253
/**
240254
* Specifying this will cause the list operation to start fetching the record from this onwards.
241255
*/
@@ -495,6 +509,8 @@ void appendToOptions(Options options) {
495509
private RpcOrderBy orderBy;
496510
private RpcLockHint lockHint;
497511

512+
private Boolean lastStatementSet;
513+
498514
// Construction is via factory methods below.
499515
private Options() {}
500516

@@ -630,6 +646,14 @@ OrderBy orderBy() {
630646
return orderBy == null ? null : orderBy.proto;
631647
}
632648

649+
boolean hasLastStatementSet() {
650+
return lastStatementSet != null;
651+
}
652+
653+
Boolean lastStatementSet() {
654+
return lastStatementSet;
655+
}
656+
633657
boolean hasLockHint() {
634658
return lockHint != null;
635659
}
@@ -694,6 +718,9 @@ public String toString() {
694718
if (orderBy != null) {
695719
b.append("orderBy: ").append(orderBy).append(' ');
696720
}
721+
if (lastStatementSet != null) {
722+
b.append("lastStatementSet: ").append(lastStatementSet).append(' ');
723+
}
697724
if (lockHint != null) {
698725
b.append("lockHint: ").append(lockHint).append(' ');
699726
}
@@ -737,6 +764,7 @@ public boolean equals(Object o) {
737764
&& Objects.equals(dataBoostEnabled(), that.dataBoostEnabled())
738765
&& Objects.equals(directedReadOptions(), that.directedReadOptions())
739766
&& Objects.equals(orderBy(), that.orderBy())
767+
&& Objects.equals(lastStatementSet(), that.lastStatementSet());
740768
&& Objects.equals(lockHint(), that.lockHint());
741769
}
742770

@@ -797,6 +825,9 @@ public int hashCode() {
797825
if (orderBy != null) {
798826
result = 31 * result + orderBy.hashCode();
799827
}
828+
if (lastStatementSet != null) {
829+
result = 31 * result + lastStatementSet.hashCode();
830+
}
800831
if (lockHint != null) {
801832
result = 31 * result + lockHint.hashCode();
802833
}
@@ -965,4 +996,18 @@ public boolean equals(Object o) {
965996
return Objects.equals(filter, ((FilterOption) o).filter);
966997
}
967998
}
999+
1000+
static final class LastStatementUpdateOption extends InternalOption implements UpdateOption {
1001+
1002+
private final Boolean lastStatementSet;
1003+
1004+
LastStatementUpdateOption(Boolean lastStatementSet) {
1005+
this.lastStatementSet = lastStatementSet;
1006+
}
1007+
1008+
@Override
1009+
void appendToOptions(Options options) {
1010+
options.lastStatementSet = lastStatementSet;
1011+
}
1012+
}
9681013
}

0 commit comments

Comments
 (0)