public final class SqlMethods extends Object
Provides one-liner methods for easily creating, connecting, and executing statements against a JDBC data source.
| Modifier and Type | Method and Description |
|---|---|
static void |
closeConnection(Connection connection)
Close the provided connection.
|
static void |
commitTransaction()
Commit the current JTA transaction.
|
static void |
commitTransaction(Connection connection)
Commit changes in the provided connection.
|
static io.toro.gloop.object.property.GloopModel |
executeSelect(String databaseConnectionName,
String sqlStatement,
io.toro.gloop.object.property.GloopModel sqlParameters,
io.toro.gloop.engine.GloopExecutionContext context)
Execute a SELECT statement, and then return the first row of the result set.
|
static int |
executeSql(String databaseConnectionName,
String sqlStatement,
io.toro.gloop.object.property.GloopModel sqlParameters,
io.toro.gloop.engine.GloopExecutionContext context)
Execute a SQL statement against the specified data source.
|
static io.toro.gloop.object.cursor.GloopCursor |
getCursorForInsertUpdateStatement(String databaseConnectionName,
String sqlStatement,
io.toro.gloop.object.property.GloopModel model,
int batchSize,
io.toro.gloop.engine.GloopExecutionContext context)
Get a cursor for writing to the specified data source.
|
static io.toro.gloop.object.cursor.GloopCursor |
getCursorForSelectStatement(String databaseConnectionName,
String sqlStatement,
io.toro.gloop.object.property.GloopModel sqlParameters,
String modelReferenceName,
io.toro.gloop.object.property.GloopModel modelTemplate,
boolean updateable,
io.toro.gloop.engine.GloopExecutionContext context)
Get a cursor for reading the results of a SQL statement executed against the specified data source.
|
static Connection |
getDatabaseConnection(io.toro.gloop.object.property.GloopModel connectionParameters,
boolean autoCommit,
io.toro.gloop.engine.GloopExecutionContext context)
Attempt to establish a connection to a data source using the provided connection details.
|
static Connection |
getDatabaseConnection(String databaseConnectionName,
io.toro.gloop.engine.GloopExecutionContext context)
Attempt to establish a connection to a data source using information
from the provided Martini database connection.
|
static io.toro.gloop.object.cursor.GloopCursor |
getInsertedKeysCursor(io.toro.gloop.object.cursor.GloopCursor batchCursor,
io.toro.gloop.engine.GloopExecutionContext context)
Get a cursor pointing to the keys generated from a batch insert operation.
|
static void |
rollbackTransaction()
Perform a roll back of the current JTA transaction.
|
static void |
rollbackTransaction(Connection connection)
Roll back changes in the provided connection.
|
static groovy.sql.Sql |
sql(String poolName)
Construct an
Sql instance using a pre-defined database connection in Martini. |
static Object |
sql(String poolName,
groovy.lang.Closure c)
Invoke the provided closure,
which will be supplied with a
Sql instance from the specified Martini database connection. |
static void |
startTransaction(boolean autoCommit,
int timeout,
io.toro.gloop.engine.GloopExecutionContext context)
Start a new JTA transaction using the Martini transaction manager.
|
public static groovy.sql.Sql sql(@GloopParameter(allowNull=false)
String poolName)
throws ToroException,
SQLException
Construct an Sql instance using a pre-defined database connection in Martini.
It is the caller's responsibility to close the Sql object after use.
Example usage:
def sql = 'my-connection-pool'.sql()
println 'Your existing projects:'
sql.eachRow('select * from PROJECT') { row ->
println "${row.name.padRight(10)} ($row.url)"
}
sql.close()
poolName - the name of the JDBC connection pool saved in MartiniToroExceptionSQLExceptionpublic static Object sql(@GloopParameter(allowNull=false) String poolName, @GloopParameter(allowNull=false) groovy.lang.Closure c) throws ToroException, SQLException
Invoke the provided closure,
which will be supplied with a Sql instance from the specified Martini database connection.
The Sql instance will be automatically closed after the closure's execution.
Example usage:
'my-connection-pool'.sql() { sql ->
println 'Your existing projects:'
sql.eachRow('select * from PROJECT') { row ->
println "${row.name.padRight(10)} ($row.url)"
}
}
poolName - the name of the JDBC connection pool saved in MartiniToroExceptionSQLExceptionpublic static void commitTransaction()
public static void commitTransaction(@GloopParameter(allowNull=false)
Connection connection)
throws SQLException
connection - the affected connectionSQLExceptionConnection.commit()public static void rollbackTransaction()
public static void rollbackTransaction(@GloopParameter(allowNull=false)
Connection connection)
throws SQLException
connection - the affected connectionSQLExceptionConnection.rollback()public static void closeConnection(@GloopParameter(allowNull=false)
Connection connection)
throws SQLException
connection - the affected connectionSQLExceptionConnection.close()@GloopParameter(name="databaseConnection") public static Connection getDatabaseConnection(@GloopParameter(allowNull=false) String databaseConnectionName, io.toro.gloop.engine.GloopExecutionContext context) throws SQLException
databaseConnectionName - the name of the saved database connection in Martinicontext - the Gloop execution context (automatically mapped by Gloop)SQLException - if a database connection with the provided name could not be found@GloopParameter(name="databaseConnection") public static Connection getDatabaseConnection(@GloopObjectParameter(value="connectionParameters:JDBC Connection Parameters{\ndriverClassName::JDBC Driver Class Name\nconnectionUrl::JDBC Connection URL\nusername::Database username\nupassword::Database password\n}") @GloopParameter(allowNull=false) io.toro.gloop.object.property.GloopModel connectionParameters, @GloopParameter(defaultValue="true") boolean autoCommit, io.toro.gloop.engine.GloopExecutionContext context) throws ClassNotFoundException, SQLException
connectionParameters - the connection details of the data source to connect toautoCommit - flag for setting the to-be-created connection on auto-commit modecontext - the Gloop execution context (automatically mapped by Gloop)ClassNotFoundException - if the provided database driver does not exist in the class pathSQLException - if a database access error occurs; if the database's URL is null@GloopParameter(name="selectCursor")
public static io.toro.gloop.object.cursor.GloopCursor getCursorForSelectStatement(@GloopParameter(allowNull=false)
String databaseConnectionName,
@GloopParameter(allowNull=false)
String sqlStatement,
io.toro.gloop.object.property.GloopModel sqlParameters,
String modelReferenceName,
io.toro.gloop.object.property.GloopModel modelTemplate,
@GloopParameter(defaultValue="false")
boolean updateable,
io.toro.gloop.engine.GloopExecutionContext context)
throws io.toro.gloop.exception.GloopException,
SQLException
databaseConnectionName - the name of the saved database connection in Martini
that will be receive the statementsqlStatement - the SQL statement to execute; supports query parameterssqlParameters - the SQL statement query parametersmodelReferenceName - the namespace of the model to use as reference for the entries in the cursor;modelTemplate - the model to use as reference for the entries in the cursor
if modelReferenceName is unspecifiedupdateable - whether or not the cursor's result set is updatablecontext - the Gloop execution context (automatically mapped by Gloop)io.toro.gloop.exception.GloopException - if modelReferenceName could not be resolved (if provided)SQLException - if a database connection with the provided name could not be found@GloopParameter(name="insertUpdateCursor")
public static io.toro.gloop.object.cursor.GloopCursor getCursorForInsertUpdateStatement(@GloopParameter(allowNull=false)
String databaseConnectionName,
@GloopParameter(allowNull=false)
String sqlStatement,
io.toro.gloop.object.property.GloopModel model,
@GloopParameter(defaultValue="-1")
int batchSize,
io.toro.gloop.engine.GloopExecutionContext context)
throws SQLException
databaseConnectionName - the name of the saved database connection in Martini
that will be receive the statementsqlStatement - the SQL statement to executemodel - the model to use as reference for the entries to be written to the cursorbatchSize - batch size before sending the update request to the databasecontext - the Gloop execution context (automatically mapped by Gloop)SQLException - if a database connection with the provided name could not be found;
if a database access error occurs@GloopParameter(name="insertedKeysCursor")
public static io.toro.gloop.object.cursor.GloopCursor getInsertedKeysCursor(@GloopParameter(allowNull=false)
io.toro.gloop.object.cursor.GloopCursor batchCursor,
io.toro.gloop.engine.GloopExecutionContext context)
throws SQLException
batchCursor - open batch insert cursorcontext - the Gloop execution context (automatically mapped by Gloop)SQLException - if the provided cursor is already closed; if provided cursor is not a batch cursor@GloopParameter(name="updateCount")
public static int executeSql(@GloopParameter(allowNull=false)
String databaseConnectionName,
@GloopParameter(allowNull=false)
String sqlStatement,
io.toro.gloop.object.property.GloopModel sqlParameters,
io.toro.gloop.engine.GloopExecutionContext context)
throws SQLException
databaseConnectionName - the name of the saved database connection in Martini
that will be receive the statementsqlStatement - the SQL statement to executesqlParameters - the SQL statement query parameterscontext - the Gloop execution context (automatically mapped by Gloop)SQLException - if a database connection with the provided name could not be found;
if a database access error occurs@GloopParameter(name="firstRow")
public static io.toro.gloop.object.property.GloopModel executeSelect(@GloopParameter(allowNull=false)
String databaseConnectionName,
@GloopParameter(allowNull=false)
String sqlStatement,
@GloopParameter(value="Query parameters")
io.toro.gloop.object.property.GloopModel sqlParameters,
io.toro.gloop.engine.GloopExecutionContext context)
throws io.toro.gloop.exception.GloopException,
SQLException,
IOException
databaseConnectionName - the name of the saved database connection in Martini
that will be receive the statementsqlStatement - the SQL statement to executesqlParameters - the SQL statement query parameterscontext - the Gloop execution context (automatically mapped by Gloop)SQLException - if a database connection with the provided name could not be foundIOException - if cursor cannot be closedio.toro.gloop.exception.GloopExceptionpublic static void startTransaction(@GloopParameter(defaultValue="false")
boolean autoCommit,
@GloopParameter(defaultValue="60")
int timeout,
io.toro.gloop.engine.GloopExecutionContext context)
Start a new JTA transaction using the Martini transaction manager. This only works with running services, not services that are being debugged.
autoCommit - if true, this will try and commit after the service has finished,
even if an exception was throwntimeout - amount of time in seconds to wait before timing out the transactioncontext - the Gloop execution context (automatically mapped by Gloop)Copyright © 2020. All rights reserved.