sqlg3.runtime.JdbcInterface Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sqlg3-runtime Show documentation
Show all versions of sqlg3-runtime Show documentation
SQLG is a preprocessor and a library that uses code generation to simplify writing JDBC code
package sqlg3.runtime;
import sqlg3.core.IDBCommon;
import sqlg3.core.ISimpleTransaction;
import java.sql.Connection;
/**
* This class can be used to call SQLG code from JDBC code. If you have {@link Connection} instance then
* you can retrieve business interface ITest
in the following way:
*
* Connection conn = ...;
* ISimpleTransaction trans = new JdbcInterface(global, conn, false);
* ITest iface = trans.getInterface(ITest.class);
*
*/
public final class JdbcInterface implements ISimpleTransaction {
private final TransactionContext transaction;
private final boolean commitCalls;
public JdbcInterface(GlobalContext global, Connection connection, boolean commitCalls, Object userObject, PreCallCheck beforeCall) {
SessionContext session = new SessionContext(new SingleConnectionManager(connection), userObject, beforeCall);
this.transaction = new TransactionContext(global, session);
this.commitCalls = commitCalls;
}
public T getInterface(Class iface) {
return transaction.getInterface(iface, commitCalls, true);
}
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private boolean commitCalls = false;
private Object userObject = null;
private PreCallCheck beforeCall = null;
public Builder setCommitCalls(boolean commitCalls) {
this.commitCalls = commitCalls;
return this;
}
public Builder setUserObject(Object userObject) {
this.userObject = userObject;
return this;
}
public Builder setBeforeCall(PreCallCheck beforeCall) {
this.beforeCall = beforeCall;
return this;
}
public JdbcInterface build(GlobalContext global, Connection connection) {
return new JdbcInterface(global, connection, commitCalls, userObject, beforeCall);
}
}
}