Skip to content

Commit

Permalink
feat: Default transaction isolation (#1998)
Browse files Browse the repository at this point in the history
* Added code for supporting not deferrable and setting transaction isolation to default

* added tests

* Update ClientSideStatementValueConverters.java

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
gauravsnj and gcf-owl-bot[bot] committed Sep 12, 2022
1 parent 5e8dc0f commit 33aa21c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
Expand Up @@ -382,8 +382,9 @@ public Class<PgTransactionMode> getParameterClass() {
public PgTransactionMode convert(String value) {
PgTransactionMode mode = new PgTransactionMode();
// Transaction mode may contain multiple spaces.
String valueWithoutDeferrable = value.replaceAll("(?i)(not\\s+deferrable)", " ");
String valueWithSingleSpaces =
value.replaceAll("\\s+", " ").toLowerCase(Locale.ENGLISH).trim();
valueWithoutDeferrable.replaceAll("\\s+", " ").toLowerCase(Locale.ENGLISH).trim();
int currentIndex = 0;
while (currentIndex < valueWithSingleSpaces.length()) {
// This will use the last access mode and isolation level that is encountered in the string.
Expand Down
Expand Up @@ -159,11 +159,11 @@
"exampleStatements": []
},
{
"name": "{START | BEGIN} [TRANSACTION | WORK] [{ (READ ONLY|READ WRITE) [[,] (ISOLATION LEVEL (DEFAULT|SERIALIZABLE))] }]",
"name": "{START | BEGIN} [TRANSACTION | WORK] [{ (READ ONLY|READ WRITE) [[,] (ISOLATION LEVEL (DEFAULT|SERIALIZABLE))] [[,] NOT DEFERRABLE]}]",
"executorName": "ClientSideStatementPgBeginExecutor",
"resultType": "NO_RESULT",
"statementType": "BEGIN",
"regex": "(?is)\\A\\s*(?:begin|start)(?:\\s+transaction|\\s+work)?((?:(?:\\s+|\\s*,\\s*)read\\s+only|(?:\\s+|\\s*,\\s*)read\\s+write|(?:\\s+|\\s*,\\s*)isolation\\s+level\\s+default|(?:\\s+|\\s*,\\s*)isolation\\s+level\\s+serializable)*)?\\s*\\z",
"regex": "(?is)\\A\\s*(?:begin|start)(?:\\s+transaction|\\s+work)?((?:(?:\\s+|\\s*,\\s*)read\\s+only|(?:\\s+|\\s*,\\s*)read\\s+write|(?:\\s+|\\s*,\\s*)isolation\\s+level\\s+default|(?:\\s+|\\s*,\\s*)isolation\\s+level\\s+serializable|(?:\\s+|\\s*,\\s*)not\\s+deferrable)*)?\\s*\\z",
"method": "statementBeginPgTransaction",
"exampleStatements": [
"begin", "start", "begin transaction", "start transaction", "begin work", "start work",
Expand All @@ -173,7 +173,23 @@
"begin isolation level serializable", "start isolation level serializable", "begin transaction isolation level serializable", "start transaction isolation level serializable", "begin work isolation level serializable", "start work isolation level serializable",
"begin isolation level default read write", "start isolation level default read only", "begin transaction isolation level default read only", "start transaction isolation level default read write", "begin work isolation level default read write", "start work isolation level default read only",
"begin isolation level serializable read write", "start isolation level serializable read write", "begin transaction isolation level serializable read only", "start transaction isolation level serializable read write", "begin work isolation level serializable read write", "start work isolation level serializable read only",
"begin isolation level serializable, read write", "start isolation level serializable, read write", "begin transaction isolation level serializable, read only", "start transaction isolation level serializable, read write", "begin work isolation level serializable, read write", "start work isolation level serializable, read only"
"begin isolation level serializable, read write", "start isolation level serializable, read write", "begin transaction isolation level serializable, read only", "start transaction isolation level serializable, read write", "begin work isolation level serializable, read write", "start work isolation level serializable, read only",
"begin not deferrable", "start not deferrable", "begin transaction not deferrable", "start transaction not deferrable", "begin work not deferrable", "start work not deferrable",
"begin read only not deferrable", "start read only not deferrable", "begin transaction read only not deferrable", "start transaction read only not deferrable", "begin work read only not deferrable", "start work read only not deferrable",
"begin read write not deferrable", "start read write not deferrable", "begin transaction read write not deferrable", "start transaction read write not deferrable", "begin work read write not deferrable", "start work read write not deferrable",
"begin isolation level default not deferrable", "start isolation level default not deferrable", "begin transaction isolation level default not deferrable", "start transaction isolation level default not deferrable", "begin work isolation level default not deferrable", "start work isolation level default not deferrable",
"begin isolation level serializable not deferrable", "start isolation level serializable not deferrable", "begin transaction isolation level serializable not deferrable", "start transaction isolation level serializable not deferrable", "begin work isolation level serializable not deferrable", "start work isolation level serializable not deferrable",
"begin isolation level default read write not deferrable", "start isolation level default read only not deferrable", "begin transaction isolation level default read only not deferrable", "start transaction isolation level default read write not deferrable", "begin work isolation level default read write not deferrable", "start work isolation level default read only not deferrable",
"begin isolation level serializable read write not deferrable", "start isolation level serializable read write not deferrable", "begin transaction isolation level serializable read only not deferrable", "start transaction isolation level serializable read write not deferrable", "begin work isolation level serializable read write not deferrable", "start work isolation level serializable read only not deferrable",
"begin isolation level serializable, read write, not deferrable", "start isolation level serializable, read write, not deferrable", "begin transaction isolation level serializable, read only, not deferrable", "start transaction isolation level serializable, read write, not deferrable", "begin work isolation level serializable, read write, not deferrable", "start work isolation level serializable, read only",
"begin transaction not deferrable", "start transaction not deferrable", "begin work not deferrable", "start work not deferrable",
"begin not deferrable read only", "start read only", "begin transaction not deferrable read only", "start transaction read only", "begin work not deferrable read only", "start work read only",
"begin not deferrable read write", "start read write", "begin transaction not deferrable read write", "start transaction read write", "begin work not deferrable read write", "start work read write",
"begin not deferrable isolation level default", "start isolation level default", "begin transaction not deferrable isolation level default", "start transaction isolation level default", "begin work not deferrable isolation level default", "start work isolation level default",
"begin not deferrable isolation level serializable", "start isolation level serializable", "begin transaction not deferrable isolation level serializable", "start transaction isolation level serializable", "begin work not deferrable isolation level serializable", "start work isolation level serializable",
"begin not deferrable isolation level default read write", "start isolation level default read only", "begin transaction not deferrable isolation level default read only", "start transaction isolation level default read write", "begin work not deferrable isolation level default read write", "start work isolation level default read only",
"begin not deferrable isolation level serializable read write", "start isolation level serializable read write", "begin transaction not deferrable isolation level serializable read only", "start transaction isolation level serializable read write", "begin work not deferrable isolation level serializable read write", "start work isolation level serializable read only",
"begin not deferrable isolation level serializable, read write", "start isolation level serializable, read write", "begin transaction not deferrable isolation level serializable, read only", "start transaction isolation level serializable, read write", "begin work not deferrable isolation level serializable, read write", "start work isolation level serializable, read only"
]
},
{
Expand Down Expand Up @@ -360,7 +376,7 @@
}
},
{
"name": "SET DEFAULT_TRANSACTION_ISOLATION =|TO 'SERIALIZABLE'|SERIALIZABLE",
"name": "SET DEFAULT_TRANSACTION_ISOLATION =|TO 'SERIALIZABLE'|SERIALIZABLE|DEFAULT",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_READONLY",
Expand All @@ -371,12 +387,14 @@
"set default_transaction_isolation to serializable",
"set default_transaction_isolation to 'serializable'",
"set default_transaction_isolation = 'serializable'",
"set default_transaction_isolation = \"SERIALIZABLE\""
"set default_transaction_isolation = \"SERIALIZABLE\"",
"set default_transaction_isolation = DEFAULT",
"set default_transaction_isolation to DEFAULT"
],
"setStatement": {
"propertyName": "default_transaction_isolation",
"separator": "(?:=|\\s+TO\\s+)",
"allowedValues": "(SERIALIZABLE|'SERIALIZABLE'|\"SERIALIZABLE\")",
"allowedValues": "(SERIALIZABLE|'SERIALIZABLE'|\"SERIALIZABLE\"|DEFAULT)",
"converterName": "ClientSideStatementValueConverters$PgTransactionIsolationConverter"
}
},
Expand Down
Expand Up @@ -148,4 +148,37 @@ public void testBeginReadOnlyWithIsolationLevel() {
index++;
}
}

@Test
public void testBeginWithNotDeferrable() {
ConnectionImpl connection = mock(ConnectionImpl.class);
ConnectionStatementExecutorImpl executor = new ConnectionStatementExecutorImpl(connection);

int index = 1;
for (String sql :
ImmutableList.of(
"begin read only isolation level serializable not deferrable",
"begin read only isolation level default not deferrable",
"begin isolation level serializable read only not deferrable",
"begin isolation level default read only not deferrable",
"begin read write isolation level default read only not deferrable",
"begin read write, isolation level default, read only not deferrable",
"begin read write , \nisolation level default\n\t,read only \n\n not \t deferrable",
"begin not deferrable read only isolation level serializable",
"begin not deferrable read only isolation level default",
"begin not deferrable isolation level serializable read only",
"begin not deferrable isolation level default read only",
"begin not deferrable read write isolation level default read only",
"begin not deferrable read write, isolation level default, read only",
"begin not deferrable read write , \nisolation level default\n\t,read only")) {
ParsedStatement statement = parser.parse(Statement.of(sql));
assertEquals(sql, StatementType.CLIENT_SIDE, statement.getType());
statement.getClientSideStatement().execute(executor, sql);

verify(connection, times(index)).beginTransaction();
verify(connection, times(index)).setTransactionMode(TransactionMode.READ_ONLY_TRANSACTION);
verify(connection, never()).setTransactionMode(TransactionMode.READ_WRITE_TRANSACTION);
index++;
}
}
}
Expand Up @@ -147,6 +147,10 @@ public void testDefaultTransactionIsolation() {
"set default_transaction_isolation to 'serializable'",
"set default_transaction_isolation to 'SERIALIZABLE'",
"set default_transaction_isolation to \"SERIALIZABLE\"",
"set default_transaction_isolation to default",
"set default_transaction_isolation to DEFAULT",
"set default_transaction_isolation = default",
"set default_transaction_isolation = DEFAULT"
}) {
ParsedStatement statement = parser.parse(Statement.of(sql));
assertEquals(sql, StatementType.CLIENT_SIDE, statement.getType());
Expand Down

0 comments on commit 33aa21c

Please sign in to comment.