| java.lang.Object groovy.sql.Sql
All known Subclasses: groovy.sql.DataSet,
Sql | public class Sql (Code) | | Represents an extent of objects
author: Chris Stevenson author: James Strachan version: $Revision: 4066 $ |
Constructor Summary | |
public | Sql(DataSource dataSource) Constructs an SQL instance using the given DataSource. | public | Sql(Connection connection) Construts an SQL instance using the given Connection. | public | Sql(Sql parent) |
Method Summary | |
public static InParameter | ARRAY(Object value) | public static InParameter | BIGINT(Object value) | public static InParameter | BINARY(Object value) | public static InParameter | BIT(Object value) | public static InParameter | BLOB(Object value) | public static InParameter | BOOLEAN(Object value) | public static InParameter | CHAR(Object value) | public static InParameter | CLOB(Object value) | public static InParameter | DATALINK(Object value) | public static InParameter | DATE(Object value) | public static InParameter | DECIMAL(Object value) | public static InParameter | DISTINCT(Object value) | public static InParameter | DOUBLE(Object value) | public static InParameter | FLOAT(Object value) | public static InParameter | INTEGER(Object value) | public static InParameter | JAVA_OBJECT(Object value) | public static InParameter | LONGVARBINARY(Object value) | public static InParameter | LONGVARCHAR(Object value) | public static InParameter | NULL(Object value) | public static InParameter | NUMERIC(Object value) | public static InParameter | OTHER(Object value) | public static InParameter | REAL(Object value) | public static InParameter | REF(Object value) | public static InParameter | SMALLINT(Object value) | public static InParameter | STRUCT(Object value) | public static InParameter | TIME(Object value) | public static InParameter | TIMESTAMP(Object value) | public static InParameter | TINYINT(Object value) | public static InParameter | VARBINARY(Object value) | public static InParameter | VARCHAR(Object value) | protected String | asSql(GString gstring, List values) | public int | call(String sql) | public int | call(String sql, List params) | public void | call(String sql, List params, Closure closure) Performs a stored procedure call with the given parameters. | public int | call(GString gstring) | public void | call(GString gstring, Closure closure) Performs a stored procedure call with the given parameters,
calling the closure once with all result objects. | public void | close() If this SQL object was created with a Connection then this method closes
the connection. | protected void | closeResources(Connection connection, Statement statement, ResultSet results) | protected void | closeResources(Connection connection, Statement statement) | public void | commit() | protected void | configure(Statement statement) | protected Connection | createConnection() | public DataSet | dataSet(String table) | public DataSet | dataSet(Class type) | public void | eachRow(String sql, Closure closure) | public void | eachRow(String sql, List params, Closure closure) | public void | eachRow(GString gstring, Closure closure) | public boolean | execute(String sql) | public boolean | execute(String sql, List params) | public boolean | execute(GString gstring) | public List | executeInsert(String sql) Executes the given SQL statement. | public List | executeInsert(String sql, List params) Executes the given SQL statement with a particular list of
parameter values. | public List | executeInsert(GString gstring) Executes the given SQL with embedded expressions inside, and
returns the values of any auto-generated colums, such as an
autoincrement ID field. | public int | executeUpdate(String sql) | public int | executeUpdate(String sql, List params) | public int | executeUpdate(GString gstring) | public static ExpandedVariable | expand(Object object) Creates a variable to be expanded in the Sql string rather
than representing an sql parameter. | protected int | findWhereKeyword(String sql) Find the first 'where' keyword in the sql. | public Object | firstRow(String sql) | public Object | firstRow(String sql, List params) | public Connection | getConnection() If this instance was created with a single Connection then the connection
is returned. | public DataSource | getDataSource() | protected List | getParameters(GString gstring) | public int | getUpdateCount() | public static InParameter | in(int type, Object value) | public static InOutParameter | inout(InParameter in) Create an inout parameter using this in parameter. | public static void | loadDriver(String driverClassName) | public static Sql | newInstance(String url) | public static Sql | newInstance(String url, Properties properties) | public static Sql | newInstance(String url, Properties properties, String driverClassName) | public static Sql | newInstance(String url, String user, String password) | public static Sql | newInstance(String url, String user, String password, String driverClassName) | public static Sql | newInstance(String url, String driverClassName) | protected String | nullify(String sql) | public static OutParameter | out(int type) Create a new OutParameter
Parameters: type - the JDBC data type. | public void | query(String sql, Closure closure) | public void | query(String sql, List params, Closure closure) | public void | query(GString gstring, Closure closure) | public void | queryEach(String sql, Closure closure) | public void | queryEach(String sql, List params, Closure closure) | public void | queryEach(GString gstring, Closure closure) | public static ResultSetOutParameter | resultSet(int type) Create a new ResultSetOutParameter
Parameters: type - the JDBC data type. | public void | rollback() | public List | rows(String sql) | public List | rows(String sql, List params) | protected void | setObject(PreparedStatement statement, int i, Object value) Strategy method allowing derived classes to handle types differently
such as for CLOBs etc. | protected void | setParameters(List params, PreparedStatement statement) | public void | withStatement(Closure configureStatement) Allows a closure to be passed in to configure the JDBC statements before they are executed
to do things like set the query size etc. |
updateCount | int updateCount(Code) | | |
Sql | public Sql(DataSource dataSource)(Code) | | Constructs an SQL instance using the given DataSource. Each operation
will use a Connection from the DataSource pool and close it when the
operation is completed putting it back into the pool.
Parameters: dataSource - |
Sql | public Sql(Connection connection)(Code) | | Construts an SQL instance using the given Connection. It is the callers
responsibility to close the Connection after the Sql instance has been
used. You can do this on the connection object directly or by calling the
java.sql.Connection.close method.
Parameters: connection - |
asSql | protected String asSql(GString gstring, List values)(Code) | | the SQL version of the given query using ? instead of anyparameter |
call | public int call(String sql, List params) throws Exception(Code) | | Performs a stored procedure call with the given parameters
|
call | public void call(String sql, List params, Closure closure) throws Exception(Code) | | Performs a stored procedure call with the given parameters. The closure
is called once with all the out parameters.
|
call | public int call(GString gstring) throws Exception(Code) | | Performs a stored procedure call with the given parameters
|
call | public void call(GString gstring, Closure closure) throws Exception(Code) | | Performs a stored procedure call with the given parameters,
calling the closure once with all result objects.
|
close | public void close() throws SQLException(Code) | | If this SQL object was created with a Connection then this method closes
the connection. If this SQL object was created from a DataSource then
this method does nothing.
throws: SQLException - |
commit | public void commit()(Code) | | |
configure | protected void configure(Statement statement)(Code) | | Provides a hook to be able to configure JDBC statements, such as to configure
Parameters: statement - |
eachRow | public void eachRow(String sql, Closure closure) throws SQLException(Code) | | Performs the given SQL query calling the closure with each row of the
result set
|
execute | public boolean execute(GString gstring) throws SQLException(Code) | | Executes the given SQL with embedded expressions inside
|
executeInsert | public List executeInsert(String sql) throws SQLException(Code) | | Executes the given SQL statement. See
Sql.executeInsert(GString) for more details.
Parameters: sql - The SQL statement to execute. A list of the auto-generated column values for eachinserted row. |
executeInsert | public List executeInsert(String sql, List params) throws SQLException(Code) | | Executes the given SQL statement with a particular list of
parameter values. See
Sql.executeInsert(GString) for
more details.
Parameters: sql - The SQL statement to execute. Parameters: params - The parameter values that will be substitutedinto the SQL statement's parameter slots. A list of the auto-generated column values for eachinserted row. |
executeInsert | public List executeInsert(GString gstring) throws SQLException(Code) | | Executes the given SQL with embedded expressions inside, and
returns the values of any auto-generated colums, such as an
autoincrement ID field. These values can be accessed using
array notation. For example, to return the second auto-generated
column value of the third row, use keys[3][1] . The
method is designed to be used with SQL INSERT statements, but is
not limited to them.
The standard use for this method is when a table has an
autoincrement ID column and you want to know what the ID is for
a newly inserted row. In this example, we insert a single row
into a table in which the first column contains the autoincrement
ID:
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/groovy",
"user",
"password",
"com.mysql.jdbc.Driver")
def keys = sql.insert("insert into test_table (INT_DATA, STRING_DATA) "
+ "VALUES (1, 'Key Largo')")
def id = keys[0][0]
// 'id' now contains the value of the new row's ID column.
// It can be used to update an object representation's
// id attribute for example.
...
A list of column values representing each row'sauto-generated keys. |
executeUpdate | public int executeUpdate(String sql) throws SQLException(Code) | | Executes the given SQL update
the number of rows updated |
executeUpdate | public int executeUpdate(String sql, List params) throws SQLException(Code) | | Executes the given SQL update with parameters
the number of rows updated |
executeUpdate | public int executeUpdate(GString gstring) throws SQLException(Code) | | Executes the given SQL update with embedded expressions inside
the number of rows updated |
expand | public static ExpandedVariable expand(Object object)(Code) | | Creates a variable to be expanded in the Sql string rather
than representing an sql parameter.
Parameters: object - |
findWhereKeyword | protected int findWhereKeyword(String sql)(Code) | | Find the first 'where' keyword in the sql.
Parameters: sql - |
firstRow | public Object firstRow(String sql, List params) throws SQLException(Code) | | Performs the given SQL query with the list of params and return
the first row of the result set
|
getConnection | public Connection getConnection()(Code) | | If this instance was created with a single Connection then the connection
is returned. Otherwise if this instance was created with a DataSource
then this method returns null
the connection wired into this object, or null if this objectuses a DataSource |
getParameters | protected List getParameters(GString gstring)(Code) | | extracts the parameters from the expression as a List |
getUpdateCount | public int getUpdateCount()(Code) | | Returns the updateCount. |
in | public static InParameter in(int type, Object value)(Code) | | Create a new InParameter
Parameters: type - the JDBC data type Parameters: value - the object value an InParameter |
newInstance | public static Sql newInstance(String url) throws SQLException(Code) | | A helper method which creates a new Sql instance from a JDBC connection
URL
Parameters: url - a new Sql instance with a connection |
newInstance | public static Sql newInstance(String url, Properties properties) throws SQLException(Code) | | A helper method which creates a new Sql instance from a JDBC connection
URL
Parameters: url - a new Sql instance with a connection |
newInstance | public static Sql newInstance(String url, String user, String password) throws SQLException(Code) | | A helper method which creates a new Sql instance from a JDBC connection
URL, username and password
Parameters: url - a new Sql instance with a connection |
newInstance | public static Sql newInstance(String url, String driverClassName) throws SQLException, ClassNotFoundException(Code) | | A helper method which creates a new Sql instance from a JDBC connection
URL and driver class name
Parameters: url - Parameters: driverClassName - the class name of the driver a new Sql instance with a connection |
nullify | protected String nullify(String sql)(Code) | | replace ?'"? references with NULLish
Parameters: sql - |
out | public static OutParameter out(int type)(Code) | | Create a new OutParameter
Parameters: type - the JDBC data type. an OutParameter |
query | public void query(String sql, List params, Closure closure) throws SQLException(Code) | | Performs the given SQL query with parameters calling the closure with the
result set
|
resultSet | public static ResultSetOutParameter resultSet(int type)(Code) | | Create a new ResultSetOutParameter
Parameters: type - the JDBC data type. a ResultSetOutParameter |
rollback | public void rollback()(Code) | | |
rows | public List rows(String sql, List params) throws SQLException(Code) | | Performs the given SQL query with the list of params and return
the rows of the result set
|
withStatement | public void withStatement(Closure configureStatement)(Code) | | Allows a closure to be passed in to configure the JDBC statements before they are executed
to do things like set the query size etc.
Parameters: configureStatement - |
|
|