public final class SqlMethods extends Object
Provides one-liner methods for easily creating, connecting, and executing statements against a JDBC data source.
Constructor and Description |
---|
SqlMethods(DataSourceManager dataSourceManager) |
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 GloopModel |
executeSelect(String databaseConnectionName,
String sqlStatement,
GloopModel sqlParameters,
GloopExecutionContext context)
Execute a SELECT statement, and then return the first row of the result set.
|
static int |
executeSql(String databaseConnectionName,
String sqlStatement,
GloopModel sqlParameters,
GloopExecutionContext context)
Execute a SQL statement against the specified data source.
|
static GloopCursor |
getCursorForInsertUpdateStatement(String databaseConnectionName,
String sqlStatement,
GloopModel model,
int batchSize,
GloopExecutionContext context)
Get a cursor for writing to the specified data source.
|
static GloopCursor |
getCursorForSelectStatement(String databaseConnectionName,
String sqlStatement,
GloopModel sqlParameters,
String modelReferenceName,
GloopModel modelTemplate,
boolean updateable,
GloopExecutionContext context)
Get a cursor for reading the results of a SQL statement executed against the specified data source.
|
static Connection |
getDatabaseConnection(GloopModel connectionParameters,
boolean autoCommit,
GloopExecutionContext context)
Attempt to establish a connection to a data source using the provided connection details.
|
static Connection |
getDatabaseConnection(String databaseConnectionName,
GloopExecutionContext context)
Attempt to establish a connection to a data source using information
from the provided Martini database connection.
|
static GloopCursor |
getInsertedKeysCursor(GloopCursor batchCursor,
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 Sql |
sql(String poolName)
Construct an
Sql instance using a pre-defined database connection in Martini. |
static Object |
sql(String poolName,
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,
GloopExecutionContext context)
Start a new JTA transaction using the Martini transaction manager.
|
@Autowired SqlMethods(DataSourceManager dataSourceManager)
public static Sql sql(@GloopParameter(allowNull=false) String poolName) throws 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 MartiniSQLException
public static Object sql(@GloopParameter(allowNull=false) String poolName, @GloopParameter(allowNull=false) Closure c) throws 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 MartiniSQLException
public static void commitTransaction()
public static void commitTransaction(@GloopParameter(allowNull=false) Connection connection) throws SQLException
connection
- the affected connectionSQLException
Connection.commit()
public static void rollbackTransaction()
public static void rollbackTransaction(@GloopParameter(allowNull=false) Connection connection) throws SQLException
connection
- the affected connectionSQLException
Connection.rollback()
public static void closeConnection(@GloopParameter(allowNull=false) Connection connection) throws SQLException
connection
- the affected connectionSQLException
Connection.close()
@GloopParameter(name="databaseConnection") public static Connection getDatabaseConnection(@GloopParameter(allowNull=false) String databaseConnectionName, 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) GloopModel connectionParameters, @GloopParameter(defaultValue="true") boolean autoCommit, 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 GloopCursor getCursorForSelectStatement(@GloopParameter(allowNull=false) String databaseConnectionName, @GloopParameter(allowNull=false) String sqlStatement, GloopModel sqlParameters, String modelReferenceName, GloopModel modelTemplate, @GloopParameter(defaultValue="false") boolean updateable, GloopExecutionContext context) throws 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)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 GloopCursor getCursorForInsertUpdateStatement(@GloopParameter(allowNull=false) String databaseConnectionName, @GloopParameter(allowNull=false) String sqlStatement, GloopModel model, @GloopParameter(defaultValue="-1") int batchSize, 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 GloopCursor getInsertedKeysCursor(@GloopParameter(allowNull=false) GloopCursor batchCursor, 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, GloopModel sqlParameters, 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 GloopModel executeSelect(@GloopParameter(allowNull=false) String databaseConnectionName, @GloopParameter(allowNull=false) String sqlStatement, @GloopParameter(value="Query parameters") GloopModel sqlParameters, GloopExecutionContext context) throws 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 closedGloopException
public static void startTransaction(@GloopParameter(defaultValue="false") boolean autoCommit, @GloopParameter(defaultValue="60") int timeout, 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 © 2023. All rights reserved.