Class SqlMethods
- java.lang.Object
-
- io.toro.martini.SqlMethods
-
public final class SqlMethods extends Object
Provides one-liner methods for easily creating, connecting, and executing statements against a JDBC data source.
-
-
Constructor Summary
Constructors Constructor Description SqlMethods(io.toro.martini.database.DataSourceManager sourceManager)
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
closeConnection(Connection connection)
Close the provided connection.static void
commitTransaction()
Commit the current JTA transaction.static void
commitTransaction(io.toro.martini.transaction.TransactionFacade transaction)
Commit changes in the provided 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)
Deprecated.Execute a SQL statement against the specified data source.static int
executeWriteSql(String databaseConnectionName, String sqlStatement, GloopModel sqlParameters, GloopExecutionContext context)
Execute a Write 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(io.toro.martini.transaction.TransactionFacade transaction)
Perform a roll back of given JTA transaction.static void
rollbackTransaction(Connection connection)
Roll back changes in the provided connection.static Sql
sql(String poolName)
Construct anSql
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 aSql
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.static io.toro.martini.transaction.TransactionFacade
startTransaction(boolean autoCommit, int timeout, String propagationType, GloopExecutionContext context)
Start a new JTA transaction using the Martini transaction manager.
-
-
-
Constructor Detail
-
SqlMethods
@Autowired SqlMethods(io.toro.martini.database.DataSourceManager sourceManager)
-
-
Method Detail
-
sql
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 theSql
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()
- Parameters:
poolName
- the name of the JDBC connection pool saved in Martini- Returns:
- the SQL object
- Throws:
SQLException
-
sql
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. TheSql
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)" } }
- Parameters:
poolName
- the name of the JDBC connection pool saved in Martini- Returns:
- whatever object the closure returns
- Throws:
SQLException
-
commitTransaction
public static void commitTransaction()
Commit the current JTA transaction.- Since:
- 1.0
-
commitTransaction
public static void commitTransaction(@GloopParameter(allowNull=false) Connection connection) throws SQLException
Commit changes in the provided connection.- Parameters:
connection
- the affected connection- Throws:
SQLException
- Since:
- 1.0
- See Also:
Connection.commit()
-
commitTransaction
public static void commitTransaction(@GloopParameter(allowNull=false) io.toro.martini.transaction.TransactionFacade transaction) throws SQLException
Commit changes in the provided JTA transaction.- Parameters:
transaction
- the affected transaction- Throws:
SQLException
- Since:
- 1.0
- See Also:
TransactionFacade.commit()
-
rollbackTransaction
public static void rollbackTransaction()
Perform a roll back of the current JTA transaction.
-
rollbackTransaction
public static void rollbackTransaction(@GloopParameter(allowNull=false) Connection connection) throws SQLException
Roll back changes in the provided connection.- Parameters:
connection
- the affected connection- Throws:
SQLException
- Since:
- 1.0
- See Also:
Connection.rollback()
-
rollbackTransaction
public static void rollbackTransaction(@GloopParameter(allowNull=false) io.toro.martini.transaction.TransactionFacade transaction)
Perform a roll back of given JTA transaction.
-
closeConnection
public static void closeConnection(@GloopParameter(allowNull=false) Connection connection) throws SQLException
Close the provided connection.- Parameters:
connection
- the affected connection- Throws:
SQLException
- Since:
- 1.0
- See Also:
Connection.close()
-
getDatabaseConnection
@GloopParameter(name="databaseConnection") public static Connection getDatabaseConnection(@GloopParameter(allowNull=false) String databaseConnectionName, GloopExecutionContext context) throws SQLException
Attempt to establish a connection to a data source using information from the provided Martini database connection.- Parameters:
databaseConnectionName
- the name of the saved database connection in Martinicontext
- the Gloop execution context (automatically mapped by Gloop)- Returns:
- connection to the requested data source
- Throws:
SQLException
- if a database connection with the provided name could not be found
-
getDatabaseConnection
@GloopParameter(name="databaseConnection") public static Connection getDatabaseConnection(@GloopObjectParameter("connectionParameters:JDBC Connection Parameters{\ndriverClassName::JDBC Driver Class Name\nconnectionUrl::JDBC Connection URL\nusername::Database username\npassword::Database password\n}") @GloopParameter(allowNull=false) GloopModel connectionParameters, @GloopParameter(defaultValue="true") boolean autoCommit, GloopExecutionContext context) throws ClassNotFoundException, SQLException
Attempt to establish a connection to a data source using the provided connection details.- Parameters:
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)- Returns:
- connection to the requested data source
- Throws:
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
-
getCursorForSelectStatement
@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
Get a cursor for reading the results of a SQL statement executed against the specified data source.- Parameters:
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 ifmodelReferenceName
is unspecifiedupdateable
- whether or not the cursor's result set is updatablecontext
- the Gloop execution context (automatically mapped by Gloop)- Returns:
- an input cursor where results can be read from
- Throws:
GloopException
- ifmodelReferenceName
could not be resolved (if provided)SQLException
- if a database connection with the provided name could not be found
-
getCursorForInsertUpdateStatement
@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
Get a cursor for writing to the specified data source.- Parameters:
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)- Returns:
- an output cursor where records can be written to
- Throws:
SQLException
- if a database connection with the provided name could not be found; if a database access error occurs
-
getInsertedKeysCursor
@GloopParameter(name="insertedKeysCursor") public static GloopCursor getInsertedKeysCursor(@GloopParameter(allowNull=false) GloopCursor batchCursor, GloopExecutionContext context) throws SQLException
Get a cursor pointing to the keys generated from a batch insert operation. Requires the batch insert statement to have been executed with the RETURN_GENERATED_KEYS flag.- Parameters:
batchCursor
- open batch insert cursorcontext
- the Gloop execution context (automatically mapped by Gloop)- Returns:
- an input cursor containing generated keys
- Throws:
SQLException
- if the provided cursor is already closed; if provided cursor is not a batch cursor
-
executeSql
@Deprecated(since="2.2") @GloopParameter(name="updateCount") public static int executeSql(@GloopParameter(allowNull=false) String databaseConnectionName, @GloopParameter(allowNull=false) String sqlStatement, GloopModel sqlParameters, GloopExecutionContext context) throws SQLException
Deprecated.Execute a SQL statement against the specified data source.This method has been deprecated due to non-user-friendly name. UseexecuteWriteSql()
instead.- Parameters:
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)- Returns:
- the row count for SQL Data Manipulation Language (DML) statements; 0 for SQL statements that return nothing
- Throws:
SQLException
- if a database connection with the provided name could not be found; if a database access error occurs
-
executeWriteSql
@GloopParameter(name="updateCount") public static int executeWriteSql(@GloopParameter(allowNull=false) String databaseConnectionName, @GloopParameter(allowNull=false) String sqlStatement, GloopModel sqlParameters, GloopExecutionContext context) throws SQLException
Execute a Write SQL statement against the specified data source. This method is specifically designed for executing SQL statements that perform write operations, including INSERT, UPDATE, DELETE, and DDL (Data Definition Language) methods such as CREATE, ALTER, and DROP. Use this method when you need to modify the data or structure of the database.- Parameters:
databaseConnectionName
- the name of the saved database connection in Martini that will be receive the statementsqlStatement
- the Write SQL statement to executesqlParameters
- the SQL statement parameterscontext
- the Gloop execution context (automatically mapped by Gloop)- Returns:
- the row count for SQL Data Manipulation Language (DML) statements; 0 for SQL statements that return nothing
- Throws:
SQLException
- if a database connection with the provided name could not be found; if a database access error occurs
-
executeSelect
@GloopParameter(name="firstRow") public static GloopModel executeSelect(@GloopParameter(allowNull=false) String databaseConnectionName, @GloopParameter(allowNull=false) String sqlStatement, @GloopParameter("Query parameters") GloopModel sqlParameters, GloopExecutionContext context) throws GloopException, SQLException, IOException
Execute a SELECT statement, and then return the first row of the result set.- Parameters:
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)- Returns:
- the first row
- Throws:
SQLException
- if a database connection with the provided name could not be foundIOException
- if cursor cannot be closedGloopException
-
startTransaction
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.
- Parameters:
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)
-
startTransaction
public static io.toro.martini.transaction.TransactionFacade startTransaction(@GloopParameter(defaultValue="false") boolean autoCommit, @GloopParameter(defaultValue="60") int timeout, @GloopParameter(defaultValue="SUPPORTS",choices={"REQUIRED","SUPPORTS","MANDATORY","NEVER"}) String propagationType, GloopExecutionContext context)
Start a new JTA transaction using the Martini transaction manager. This only works with running services, not services that are being debugged.
- Parameters:
autoCommit
- if true, this will try and commit after the service has finished, even if an exception was thrownpropagationType
- defaults to SUPPORTS, supported propagation types are REQUIRED, SUPPORTS, MANDATORY, NEVERtimeout
- amount of time in seconds to wait before timing out the transactioncontext
- the Gloop execution context (automatically mapped by Gloop)
-
-