forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for customizing transaction start - fixes eclipse-vertx#432
Signed-off-by: Billy Yuan <[email protected]>
- Loading branch information
Showing
36 changed files
with
933 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
vertx-db2-client/src/main/java/io/vertx/db2client/impl/util/TransactionSqlBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,58 @@ | ||
package io.vertx.db2client.impl.util; | ||
|
||
import io.vertx.sqlclient.transaction.TransactionAccessMode; | ||
import io.vertx.sqlclient.transaction.TransactionIsolationLevel; | ||
|
||
public class TransactionSqlBuilder { | ||
private static final String SET_ISOLATION = "SET TRANSACTION"; | ||
|
||
private static final String PREDEFINED_TX_REPEATABLE_READ = " ISOLATION LEVEL REPEATABLE READ"; | ||
private static final String PREDEFINED_TX_SERIALIZABLE = " ISOLATION LEVEL SERIALIZABLE"; | ||
private static final String PREDEFINED_TX_READ_COMMITTED = " ISOLATION LEVEL READ COMMITTED"; | ||
private static final String PREDEFINED_TX_READ_UNCOMMITTED = " ISOLATION LEVEL READ UNCOMMITTED"; | ||
|
||
|
||
private static final String PREDEFINED_TX_RW = " READ WRITE"; | ||
private static final String PREDEFINED_TX_RO = " READ ONLY"; | ||
|
||
public static String buildSetTxIsolationLevelSql(TransactionIsolationLevel isolationLevel, TransactionAccessMode accessMode) { | ||
boolean isCharacteristicExisted = false; | ||
StringBuilder sqlBuilder = new StringBuilder(SET_ISOLATION); | ||
|
||
if (isolationLevel != null) { | ||
switch (isolationLevel) { | ||
case READ_UNCOMMITTED: | ||
sqlBuilder.append(PREDEFINED_TX_READ_UNCOMMITTED); | ||
break; | ||
case READ_COMMITTED: | ||
sqlBuilder.append(PREDEFINED_TX_READ_COMMITTED); | ||
break; | ||
case REPEATABLE_READ: | ||
sqlBuilder.append(PREDEFINED_TX_REPEATABLE_READ); | ||
break; | ||
case SERIALIZABLE: | ||
sqlBuilder.append(PREDEFINED_TX_SERIALIZABLE); | ||
break; | ||
} | ||
isCharacteristicExisted = true; | ||
} | ||
|
||
if (accessMode != null) { | ||
if (isCharacteristicExisted) { | ||
sqlBuilder.append(','); | ||
} else { | ||
isCharacteristicExisted = true; | ||
} | ||
switch (accessMode) { | ||
case READ_ONLY: | ||
sqlBuilder.append(PREDEFINED_TX_RO); | ||
break; | ||
case READ_WRITE: | ||
sqlBuilder.append(PREDEFINED_TX_RW); | ||
break; | ||
} | ||
} | ||
|
||
return sqlBuilder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
vertx-db2-client/src/test/java/io/vertx/db2client/util/TransactionSqlBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,27 @@ | ||
package io.vertx.db2client.util; | ||
|
||
import io.vertx.db2client.impl.util.TransactionSqlBuilder; | ||
import io.vertx.sqlclient.transaction.TransactionAccessMode; | ||
import io.vertx.sqlclient.transaction.TransactionIsolationLevel; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TransactionSqlBuilderTest { | ||
@Test | ||
public void testSetReadCommitted() { | ||
String sql = TransactionSqlBuilder.buildSetTxIsolationLevelSql(TransactionIsolationLevel.READ_COMMITTED, null); | ||
Assert.assertEquals("SET TRANSACTION ISOLATION LEVEL READ COMMITTED" ,sql); | ||
} | ||
|
||
@Test | ||
public void testSetReadOnly() { | ||
String sql = TransactionSqlBuilder.buildSetTxIsolationLevelSql(null, TransactionAccessMode.READ_ONLY); | ||
Assert.assertEquals("SET TRANSACTION READ ONLY" ,sql); | ||
} | ||
|
||
@Test | ||
public void testSerializableReadOnly() { | ||
String sql = TransactionSqlBuilder.buildSetTxIsolationLevelSql(TransactionIsolationLevel.SERIALIZABLE, TransactionAccessMode.READ_ONLY); | ||
Assert.assertEquals("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE, READ ONLY" ,sql); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.