|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgroovy.sql.Sql
public class Sql
Represents an extent of objects
Field Summary | |
---|---|
static OutParameter |
ARRAY
|
static OutParameter |
BIGINT
|
static OutParameter |
BINARY
|
static OutParameter |
BIT
|
static OutParameter |
BLOB
|
static OutParameter |
BOOLEAN
|
static OutParameter |
CHAR
|
static OutParameter |
CLOB
|
static OutParameter |
DATALINK
|
static OutParameter |
DATE
|
static OutParameter |
DECIMAL
|
static OutParameter |
DISTINCT
|
static OutParameter |
DOUBLE
|
static OutParameter |
FLOAT
|
static OutParameter |
INTEGER
|
static OutParameter |
JAVA_OBJECT
|
protected Logger |
log
|
static OutParameter |
LONGVARBINARY
|
static OutParameter |
LONGVARCHAR
|
static OutParameter |
NULL
|
static OutParameter |
NUMERIC
|
static OutParameter |
OTHER
|
static OutParameter |
REAL
|
static OutParameter |
REF
|
static OutParameter |
SMALLINT
|
static OutParameter |
STRUCT
|
static OutParameter |
TIME
|
static OutParameter |
TIMESTAMP
|
static OutParameter |
TINYINT
|
static OutParameter |
VARBINARY
|
static OutParameter |
VARCHAR
|
Constructor Summary | |
---|---|
Sql(Connection connection)
Constructs an SQL instance using the given Connection. |
|
Sql(DataSource dataSource)
Constructs an SQL instance using the given DataSource. |
|
Sql(Sql parent)
|
Method Summary | |
---|---|
static InParameter |
ARRAY(Object value)
|
protected String |
asSql(GString gstring,
List values)
|
static InParameter |
BIGINT(Object value)
|
static InParameter |
BINARY(Object value)
|
static InParameter |
BIT(Object value)
|
static InParameter |
BLOB(Object value)
|
static InParameter |
BOOLEAN(Object value)
|
int |
call(GString gstring)
Performs a stored procedure call with the given parameters |
void |
call(GString gstring,
Closure closure)
Performs a stored procedure call with the given parameters, calling the closure once with all result objects. |
int |
call(String sql)
Performs a stored procedure call |
int |
call(String sql,
List params)
Performs a stored procedure call with the given parameters |
void |
call(String sql,
List params,
Closure closure)
Performs a stored procedure call with the given parameters. |
static InParameter |
CHAR(Object value)
|
static InParameter |
CLOB(Object value)
|
void |
close()
If this SQL object was created with a Connection then this method closes the connection. |
protected void |
closeResources(Connection connection,
Statement statement)
|
protected void |
closeResources(Connection connection,
Statement statement,
ResultSet results)
|
void |
commit()
|
protected void |
configure(Statement statement)
Provides a hook to be able to configure JDBC statements, such as to configure |
protected Connection |
createConnection()
|
static InParameter |
DATALINK(Object value)
|
DataSet |
dataSet(Class type)
|
DataSet |
dataSet(String table)
|
static InParameter |
DATE(Object value)
|
static InParameter |
DECIMAL(Object value)
|
static InParameter |
DISTINCT(Object value)
|
static InParameter |
DOUBLE(Object value)
|
void |
eachRow(GString gstring,
Closure closure)
Performs the given SQL query calling the closure with the result set |
void |
eachRow(String sql,
Closure closure)
Performs the given SQL query calling the closure with each row of the result set |
void |
eachRow(String sql,
Closure metaClosure,
Closure rowClosure)
Performs the given SQL query calling closures for metadata and each row |
void |
eachRow(String sql,
List params,
Closure closure)
Performs the given SQL query calling the closure with the result set |
boolean |
execute(GString gstring)
Executes the given SQL with embedded expressions inside |
boolean |
execute(String sql)
Executes the given piece of SQL |
boolean |
execute(String sql,
List params)
Executes the given piece of SQL with parameters |
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. |
List |
executeInsert(String sql)
Executes the given SQL statement. |
List |
executeInsert(String sql,
List params)
Executes the given SQL statement with a particular list of parameter values. |
int |
executeUpdate(GString gstring)
Executes the given SQL update with embedded expressions inside |
int |
executeUpdate(String sql)
Executes the given SQL update |
int |
executeUpdate(String sql,
List params)
Executes the given SQL update with parameters |
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. |
Object |
firstRow(String sql)
Performs the given SQL query and return the first row of the result set |
Object |
firstRow(String sql,
List params)
Performs the given SQL query with the list of params and return the first row of the result set |
static InParameter |
FLOAT(Object value)
|
Connection |
getConnection()
If this instance was created with a single Connection then the connection is returned. |
DataSource |
getDataSource()
|
protected List |
getParameters(GString gstring)
|
int |
getUpdateCount()
|
static InParameter |
in(int type,
Object value)
Create a new InParameter |
static InOutParameter |
inout(InParameter in)
Create an inout parameter using this in parameter. |
static InParameter |
INTEGER(Object value)
|
static InParameter |
JAVA_OBJECT(Object value)
|
static void |
loadDriver(String driverClassName)
Attempts to load the JDBC driver on the thread, current or system class loaders |
static InParameter |
LONGVARBINARY(Object value)
|
static InParameter |
LONGVARCHAR(Object value)
|
static Sql |
newInstance(String url)
A helper method which creates a new Sql instance from a JDBC connection URL |
static Sql |
newInstance(String url,
Properties properties)
A helper method which creates a new Sql instance from a JDBC connection URL |
static Sql |
newInstance(String url,
Properties properties,
String driverClassName)
A helper method which creates a new Sql instance from a JDBC connection URL and driver class name |
static Sql |
newInstance(String url,
String driverClassName)
A helper method which creates a new Sql instance from a JDBC connection URL and driver class name |
static Sql |
newInstance(String url,
String user,
String password)
A helper method which creates a new Sql instance from a JDBC connection URL, username and password |
static Sql |
newInstance(String url,
String user,
String password,
String driverClassName)
A helper method which creates a new Sql instance from a JDBC connection URL, username, password and driver class name |
static InParameter |
NULL(Object value)
|
protected String |
nullify(String sql)
replace ?'"? references with NULLish |
static InParameter |
NUMERIC(Object value)
|
static InParameter |
OTHER(Object value)
|
static OutParameter |
out(int type)
Create a new OutParameter |
void |
query(GString gstring,
Closure closure)
Performs the given SQL query calling the closure with the result set |
void |
query(String sql,
Closure closure)
Performs the given SQL query calling the closure with the result set |
void |
query(String sql,
List params,
Closure closure)
Performs the given SQL query with parameters calling the closure with the result set |
void |
queryEach(GString gstring,
Closure closure)
Deprecated. please use eachRow instead |
void |
queryEach(String sql,
Closure closure)
Deprecated. please use eachRow instead |
void |
queryEach(String sql,
List params,
Closure closure)
Deprecated. please use eachRow instead |
static InParameter |
REAL(Object value)
|
static InParameter |
REF(Object value)
|
static ResultSetOutParameter |
resultSet(int type)
Create a new ResultSetOutParameter |
void |
rollback()
|
List |
rows(String sql)
Performs the given SQL query and return the rows of the result set |
List |
rows(String sql,
Closure metaClosure)
Performs the given SQL query and return the rows of the result set |
List |
rows(String sql,
List params)
Performs the given SQL query with the list of params and return the rows of the result set |
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)
Appends the parameters to the given statement |
static InParameter |
SMALLINT(Object value)
|
static InParameter |
STRUCT(Object value)
|
static InParameter |
TIME(Object value)
|
static InParameter |
TIMESTAMP(Object value)
|
static InParameter |
TINYINT(Object value)
|
static InParameter |
VARBINARY(Object value)
|
static InParameter |
VARCHAR(Object value)
|
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected Logger log
public static final OutParameter ARRAY
public static final OutParameter BIGINT
public static final OutParameter BINARY
public static final OutParameter BIT
public static final OutParameter BLOB
public static final OutParameter BOOLEAN
public static final OutParameter CHAR
public static final OutParameter CLOB
public static final OutParameter DATALINK
public static final OutParameter DATE
public static final OutParameter DECIMAL
public static final OutParameter DISTINCT
public static final OutParameter DOUBLE
public static final OutParameter FLOAT
public static final OutParameter INTEGER
public static final OutParameter JAVA_OBJECT
public static final OutParameter LONGVARBINARY
public static final OutParameter LONGVARCHAR
public static final OutParameter NULL
public static final OutParameter NUMERIC
public static final OutParameter OTHER
public static final OutParameter REAL
public static final OutParameter REF
public static final OutParameter SMALLINT
public static final OutParameter STRUCT
public static final OutParameter TIME
public static final OutParameter TIMESTAMP
public static final OutParameter TINYINT
public static final OutParameter VARBINARY
public static final OutParameter VARCHAR
Constructor Detail |
---|
public Sql(DataSource dataSource)
dataSource
- public Sql(Connection connection)
Connection.close()
method.
connection
- public Sql(Sql parent)
Method Detail |
---|
public static Sql newInstance(String url) throws SQLException
url
-
SQLException
public static Sql newInstance(String url, Properties properties) throws SQLException
url
-
SQLException
public static Sql newInstance(String url, Properties properties, String driverClassName) throws SQLException, ClassNotFoundException
url
-
SQLException
ClassNotFoundException
public static Sql newInstance(String url, String user, String password) throws SQLException
url
-
SQLException
public static Sql newInstance(String url, String user, String password, String driverClassName) throws SQLException, ClassNotFoundException
url
-
SQLException
ClassNotFoundException
public static Sql newInstance(String url, String driverClassName) throws SQLException, ClassNotFoundException
url
- driverClassName
- the class name of the driver
SQLException
ClassNotFoundException
public static void loadDriver(String driverClassName) throws ClassNotFoundException
driverClassName
-
ClassNotFoundException
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)
public static InParameter in(int type, Object value)
type
- the JDBC data typevalue
- the object value
public static OutParameter out(int type)
type
- the JDBC data type.
public static InOutParameter inout(InParameter in)
in
- public static ResultSetOutParameter resultSet(int type)
type
- the JDBC data type.
public static ExpandedVariable expand(Object object)
object
- public DataSet dataSet(String table)
public DataSet dataSet(Class type)
public void query(String sql, Closure closure) throws SQLException
SQLException
public void query(String sql, List params, Closure closure) throws SQLException
SQLException
public void query(GString gstring, Closure closure) throws SQLException
SQLException
public void queryEach(String sql, Closure closure) throws SQLException
SQLException
public void eachRow(String sql, Closure closure) throws SQLException
SQLException
public void eachRow(String sql, Closure metaClosure, Closure rowClosure) throws SQLException
sql
- the sql statementmetaClosure
- called for meta data (only once after sql execution)rowClosure
- called for each row with a GroovyResultSet
SQLException
public void queryEach(String sql, List params, Closure closure) throws SQLException
SQLException
public void eachRow(String sql, List params, Closure closure) throws SQLException
SQLException
public void eachRow(GString gstring, Closure closure) throws SQLException
SQLException
public void queryEach(GString gstring, Closure closure) throws SQLException
SQLException
public List rows(String sql) throws SQLException
SQLException
public List rows(String sql, Closure metaClosure) throws SQLException
sql
- the SQL statementmetaClosure
- called with meta data of the ResultSet
SQLException
public Object firstRow(String sql) throws SQLException
SQLException
public List rows(String sql, List params) throws SQLException
SQLException
public Object firstRow(String sql, List params) throws SQLException
SQLException
public boolean execute(String sql) throws SQLException
SQLException
public int executeUpdate(String sql) throws SQLException
SQLException
public List executeInsert(String sql) throws SQLException
executeInsert(GString)
for more details.
sql
- The SQL statement to execute.
SQLException
public boolean execute(String sql, List params) throws SQLException
SQLException
public int executeUpdate(String sql, List params) throws SQLException
SQLException
public List executeInsert(String sql, List params) throws SQLException
executeInsert(GString)
for
more details.
sql
- The SQL statement to execute.params
- The parameter values that will be substituted
into the SQL statement's parameter slots.
SQLException
public boolean execute(GString gstring) throws SQLException
SQLException
public int executeUpdate(GString gstring) throws SQLException
SQLException
public List executeInsert(GString gstring) throws SQLException
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. ...
SQLException
public int call(String sql) throws Exception
Exception
public int call(String sql, List params) throws Exception
Exception
public void call(String sql, List params, Closure closure) throws Exception
Exception
public int call(GString gstring) throws Exception
Exception
public void call(GString gstring, Closure closure) throws Exception
Exception
public void close() throws SQLException
SQLException
public DataSource getDataSource()
public void commit()
public void rollback()
public int getUpdateCount()
public Connection getConnection()
public void withStatement(Closure configureStatement)
configureStatement
- protected String asSql(GString gstring, List values)
protected String nullify(String sql)
sql
- protected int findWhereKeyword(String sql)
sql
- protected List getParameters(GString gstring)
protected void setParameters(List params, PreparedStatement statement) throws SQLException
SQLException
protected void setObject(PreparedStatement statement, int i, Object value) throws SQLException
SQLException
protected Connection createConnection() throws SQLException
SQLException
protected void closeResources(Connection connection, Statement statement, ResultSet results)
protected void closeResources(Connection connection, Statement statement)
protected void configure(Statement statement)
statement
-
|
Copyright © 2003-2007 The Codehaus. All rights reserved. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |