-
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.
* Implement transactions * Add executeStatement with param setter --------- Co-authored-by: kaklakariada <[email protected]>
- Loading branch information
1 parent
d08dca6
commit 30290ee
Showing
11 changed files
with
432 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.itsallcode.jdbc; | ||
|
||
import static java.util.function.Predicate.not; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.itsallcode.jdbc.resultset.RowMapper; | ||
import org.itsallcode.jdbc.resultset.SimpleResultSet; | ||
import org.itsallcode.jdbc.resultset.generic.Row; | ||
|
||
/** | ||
* Interface for various DB operations. | ||
*/ | ||
public interface DbOperations extends AutoCloseable { | ||
/** | ||
* Execute all commands in a SQL script, separated with {@code ;}. | ||
* | ||
* @param sqlScript the script to execute. | ||
*/ | ||
default void executeScript(final String sqlScript) { | ||
Arrays.stream(sqlScript.split(";")) | ||
.map(String::trim) | ||
.filter(not(String::isEmpty)) | ||
.forEach(this::executeStatement); | ||
} | ||
|
||
/** | ||
* Execute a single SQL statement. | ||
* | ||
* @param sql the statement | ||
* @param preparedStatementSetter prepared statement setter | ||
*/ | ||
void executeStatement(final String sql, PreparedStatementSetter preparedStatementSetter); | ||
|
||
/** | ||
* Execute a single SQL statement. | ||
* | ||
* @param sql the statement | ||
*/ | ||
void executeStatement(final String sql); | ||
|
||
/** | ||
* Execute a SQL query and return a {@link SimpleResultSet result set} with | ||
* generic {@link Row}s. | ||
* | ||
* @param sql the query | ||
* @return the result set | ||
*/ | ||
SimpleResultSet<Row> query(final String sql); | ||
|
||
/** | ||
* Execute a SQL query and return a {@link SimpleResultSet result set} with rows | ||
* converted to a custom type {@link T}. | ||
* | ||
* @param <T> generic row type | ||
* @param sql SQL query | ||
* @param rowMapper row mapper | ||
* @return the result set | ||
*/ | ||
<T> SimpleResultSet<T> query(final String sql, final RowMapper<T> rowMapper); | ||
|
||
/** | ||
* Execute a SQL query, set parameters and return a {@link SimpleResultSet | ||
* result set} with rows converted to a custom type {@link T}. | ||
* | ||
* @param <T> generic row type | ||
* @param sql SQL query | ||
* @param preparedStatementSetter the prepared statement setter | ||
* @param rowMapper row mapper | ||
* @return the result set | ||
*/ | ||
<T> SimpleResultSet<T> query(final String sql, final PreparedStatementSetter preparedStatementSetter, | ||
final RowMapper<T> rowMapper); | ||
|
||
/** | ||
* Create a batch insert builder | ||
* | ||
* @param rowType row type | ||
* @param <T> row type | ||
* @return batch insert builder | ||
*/ | ||
<T> BatchInsertBuilder<T> batchInsert(final Class<T> rowType); | ||
|
||
@Override | ||
void close(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.itsallcode.jdbc; | ||
|
||
import org.itsallcode.jdbc.resultset.RowMapper; | ||
import org.itsallcode.jdbc.resultset.SimpleResultSet; | ||
import org.itsallcode.jdbc.resultset.generic.Row; | ||
|
||
/** | ||
* A running database transaction. The transaction will be rolled back | ||
* automatically in {@link #close()}. | ||
*/ | ||
public final class Transaction implements DbOperations { | ||
|
||
private final SimpleConnection connection; | ||
|
||
private Transaction(final SimpleConnection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
static Transaction start(final SimpleConnection connection) { | ||
connection.setAutoCommit(false); | ||
return new Transaction(connection); | ||
} | ||
|
||
/** | ||
* Commit the transaction. | ||
*/ | ||
public void commit() { | ||
connection.commit(); | ||
} | ||
|
||
/** | ||
* Rollback the transaction. | ||
*/ | ||
public void rollback() { | ||
connection.rollback(); | ||
} | ||
|
||
@Override | ||
public void executeStatement(final String sql) { | ||
connection.executeStatement(sql); | ||
} | ||
|
||
@Override | ||
public void executeStatement(final String sql, final PreparedStatementSetter preparedStatementSetter) { | ||
connection.executeStatement(sql, preparedStatementSetter); | ||
} | ||
|
||
@Override | ||
public SimpleResultSet<Row> query(final String sql) { | ||
return connection.query(sql); | ||
} | ||
|
||
@Override | ||
public void executeScript(final String sqlScript) { | ||
connection.executeScript(sqlScript); | ||
} | ||
|
||
@Override | ||
public <T> SimpleResultSet<T> query(final String sql, final RowMapper<T> rowMapper) { | ||
return connection.query(sql, rowMapper); | ||
} | ||
|
||
@Override | ||
public <T> SimpleResultSet<T> query(final String sql, final PreparedStatementSetter preparedStatementSetter, | ||
final RowMapper<T> rowMapper) { | ||
return connection.query(sql, preparedStatementSetter, rowMapper); | ||
} | ||
|
||
@Override | ||
public <T> BatchInsertBuilder<T> batchInsert(final Class<T> rowType) { | ||
return connection.batchInsert(rowType); | ||
} | ||
|
||
/** | ||
* Rollback transaction and enable auto commit. | ||
* <p> | ||
* Explicitly run {@link #commit()} before to commit your transaction. | ||
*/ | ||
@Override | ||
public void close() { | ||
this.rollback(); | ||
connection.setAutoCommit(true); | ||
} | ||
} |
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.