Save This Page
Home » groovy-src-1.6.3 » groovy » sql » [javadoc | source]
groovy.sql
public class: Sql [javadoc | source]
java.lang.Object
   groovy.sql.Sql

Direct Known Subclasses:
    DataSet

Represents an extent of objects
Field Summary
protected  Logger log    Hook to allow derived classes to access the 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:
 public Sql(DataSource dataSource) 
    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 - the DataSource to use
 public Sql(Connection connection) 
    Constructs an SQL instance using the given Connection. It is the caller's 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 Connection#close() method.
    Parameters:
    connection - the Connection to use
 public Sql(Sql parent) 
Method from groovy.sql.Sql Summary:
ARRAY,   BIGINT,   BINARY,   BIT,   BLOB,   BOOLEAN,   CHAR,   CLOB,   DATALINK,   DATE,   DECIMAL,   DISTINCT,   DOUBLE,   FLOAT,   INTEGER,   JAVA_OBJECT,   LONGVARBINARY,   LONGVARCHAR,   NULL,   NUMERIC,   OTHER,   REAL,   REF,   SMALLINT,   STRUCT,   TIME,   TIMESTAMP,   TINYINT,   VARBINARY,   VARCHAR,   asSql,   cacheConnection,   cacheStatements,   call,   call,   call,   call,   call,   close,   closeResources,   closeResources,   commit,   configure,   createConnection,   dataSet,   dataSet,   eachRow,   eachRow,   eachRow,   eachRow,   execute,   execute,   execute,   executeInsert,   executeInsert,   executeInsert,   executeUpdate,   executeUpdate,   executeUpdate,   expand,   findWhereKeyword,   firstRow,   firstRow,   firstRow,   getConnection,   getDataSource,   getParameters,   getResultSetConcurrency,   getResultSetHoldability,   getResultSetType,   getUpdateCount,   in,   inout,   isCacheStatements,   loadDriver,   newInstance,   newInstance,   newInstance,   newInstance,   newInstance,   newInstance,   nullify,   out,   query,   query,   query,   resultSet,   rollback,   rows,   rows,   rows,   rows,   setCacheStatements,   setObject,   setParameters,   setResultSetConcurrency,   setResultSetHoldability,   setResultSetType,   withStatement,   withTransaction
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from groovy.sql.Sql Detail:
 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) 
    Hook to allow derived classes to override sql generation from Gstrings.
 public synchronized  void cacheConnection(Closure closure) throws SQLException 
    Caches the connection used while the closure is active. If the closure takes a single argument, it will be called with the connection, otherwise it will be called with no arguments.
 public synchronized  void cacheStatements(Closure closure) throws SQLException 
    Caches every created preparedStatement in closure closure
    Every cached preparedStatement is closed after closure has been called. If the closure takes a single argument, it will be called with the connection, otherwise it will be called with no arguments.
 public int call(String sql) throws Exception 
    Performs a stored procedure call.
 public int call(GString gstring) throws Exception 
    Performs a stored procedure call with the given parameters.
 public int call(String sql,
    List params) throws Exception 
    Performs a stored procedure call with the given parameters.
 public  void call(GString gstring,
    Closure closure) throws Exception 
    Performs a stored procedure call with the given parameters, calling the closure once with all result objects.
 public  void call(String sql,
    List params,
    Closure closure) throws Exception 
    Performs a stored procedure call with the given parameters. The closure is called once with all the out parameters.
 public  void close() throws SQLException 
    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.
 protected  void closeResources(Connection connection,
    Statement statement) 
    An extension point allowing the behavior of resource closing to be overriden in derived classes.
 protected  void closeResources(Connection connection,
    Statement statement,
    ResultSet results) 
    An extension point allowing derived classes to change the behavior of resource closing.
 public  void commit() throws SQLException 
    If this SQL object was created with a Connection then this method commits the connection. If this SQL object was created from a DataSource then this method does nothing.
 protected  void configure(Statement statement) 
    Provides a hook for derived classes to be able to configure JDBC statements. Default behavior is to call a previously saved closure, if any, using the statement as a parameter.
 protected Connection createConnection() throws SQLException 
    An extension point allowing derived classes to change the behavior of connection creation. The default behavior is to either use the supplied connection or obtain it from the supplied datasource.
 public DataSet dataSet(String table) 
 public DataSet dataSet(Class type) 
 public  void eachRow(String sql,
    Closure closure) throws SQLException 
    Performs the given SQL query calling the closure with each row of the result set.
 public  void eachRow(GString gstring,
    Closure closure) throws SQLException 
    Performs the given SQL query calling the closure with the result set.
 public  void eachRow(String sql,
    Closure metaClosure,
    Closure rowClosure) throws SQLException 
    Performs the given SQL query calling closures for metadata and each row
 public  void eachRow(String sql,
    List params,
    Closure closure) throws SQLException 
    Performs the given SQL query calling the closure with the result set.
 public boolean execute(String sql) throws SQLException 
    Executes the given piece of SQL.
 public boolean execute(GString gstring) throws SQLException 
    Executes the given SQL with embedded expressions inside.
 public boolean execute(String sql,
    List params) throws SQLException 
    Executes the given piece of SQL with parameters.
 public List executeInsert(String sql) throws 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. ...

 public List executeInsert(String sql,
    List params) throws SQLException 
    Executes the given SQL statement with a particular list of parameter values. See #executeInsert(GString) for more details.
 public int executeUpdate(String sql) throws SQLException 
    Executes the given SQL update.
 public int executeUpdate(GString gstring) throws SQLException 
    Executes the given SQL update with embedded expressions inside.
 public int executeUpdate(String sql,
    List params) throws SQLException 
    Executes the given SQL update with parameters.
 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) 
    Hook to allow derived classes to override where clause sniffing. Default behavior is to find the first 'where' keyword in the sql doing simple avoidance of the word 'where' within quotes.
 public Object firstRow(String sql) throws SQLException 
    Performs the given SQL query and return the first row of the result set.
 public Object firstRow(GString gstring) throws SQLException 
    Performs the given SQL query and return the first row of the result set.
 public Object firstRow(String sql,
    List params) throws SQLException 
    Performs the given SQL query with the list of params and return the first row of the result set.
 public Connection getConnection() 
    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
 public DataSource getDataSource() 
 protected List getParameters(GString gstring) 
    Hook to allow derived classes to override behavior associated with extracting params from a GString.
 public int getResultSetConcurrency() 
    Gets the resultSetConcurrency for statements created using the connection.
 public int getResultSetHoldability() 
    Gets the resultSetHoldability for statements created using the connection.
 public int getResultSetType() 
    Gets the resultSetType for statements created using the connection.
 public int getUpdateCount() 
 public static InParameter in(int type,
    Object value) 
    Create a new InParameter
 public static InOutParameter inout(InParameter in) 
    Create an inout parameter using this in parameter.
 public boolean isCacheStatements() 
 public static  void loadDriver(String driverClassName) throws ClassNotFoundException 
    Attempts to load the JDBC driver on the thread, current or system class loaders
 public static Sql newInstance(String url) throws SQLException 
    Creates a new Sql instance given a JDBC connection URL.
 public static Sql newInstance(String url,
    Properties properties) throws SQLException 
    Creates a new Sql instance given a JDBC connection URL and some properties.
 public static Sql newInstance(String url,
    String driverClassName) throws ClassNotFoundException, SQLException 
    Creates a new Sql instance given a JDBC connection URL and a driver class name.
 public static Sql newInstance(String url,
    Properties properties,
    String driverClassName) throws ClassNotFoundException, SQLException 
    Creates a new Sql instance given a JDBC connection URL, some properties and a driver class name.
 public static Sql newInstance(String url,
    String user,
    String password) throws SQLException 
    Creates a new Sql instance given a JDBC connection URL, a username and a password.
 public static Sql newInstance(String url,
    String user,
    String password,
    String driverClassName) throws ClassNotFoundException, SQLException 
    Creates a new Sql instance given a JDBC connection URL, a username, a password and a driver class name.
 protected String nullify(String sql) 
    Hook to allow derived classes to override null handling. Default behavior is to replace ?'"? references with NULLish
 public static OutParameter out(int type) 
    Create a new OutParameter
 public  void query(String sql,
    Closure closure) throws SQLException 
    Performs the given SQL query calling the closure with the result set.
 public  void query(GString gstring,
    Closure closure) throws SQLException 
    Performs the given SQL query calling the closure with the result set.
 public  void query(String sql,
    List params,
    Closure closure) throws SQLException 
    Performs the given SQL query with parameters calling the closure with the result set.
 public static ResultSetOutParameter resultSet(int type) 
    Create a new ResultSetOutParameter
 public  void rollback() throws SQLException 
    If this SQL object was created with a Connection then this method rolls back the connection. If this SQL object was created from a DataSource then this method does nothing.
 public List rows(String sql) throws SQLException 
    Performs the given SQL query and return the rows of the result set.
 public List rows(GString gstring) throws SQLException 
    Performs the given SQL query and return the rows of the result set.
 public List rows(String sql,
    Closure metaClosure) throws SQLException 
    Performs the given SQL query and return the rows of the result set.
 public List rows(String sql,
    List params) throws SQLException 
    Performs the given SQL query with the list of params and return the rows of the result set.
 public synchronized  void setCacheStatements(boolean cacheStatements) 
    Enables statement caching.
    if b is true, cache is created and all created prepared statements will be cached.
    if b is false, all cached statements will be properly closed.
 protected  void setObject(PreparedStatement statement,
    int i,
    Object value) throws SQLException 
    Strategy method allowing derived classes to handle types differently such as for CLOBs etc.
 protected  void setParameters(List params,
    PreparedStatement statement) throws SQLException 
    Hook to allow derived classes to override behavior associated with setting params for a prepared statement. Default behavior is to append the parameters to the given statement using setObject.
 public  void setResultSetConcurrency(int resultSetConcurrency) 
    Sets the resultSetConcurrency for statements created using the connection. May cause SQLFeatureNotSupportedException exceptions to occur if the underlying database doesn't support the requested concurrency value.
 public  void setResultSetHoldability(int resultSetHoldability) 
    Sets the resultSetHoldability for statements created using the connection. May cause SQLFeatureNotSupportedException exceptions to occur if the underlying database doesn't support the requested holdability value.
 public  void setResultSetType(int resultSetType) 
    Sets the resultSetType for statements created using the connection. May cause SQLFeatureNotSupportedException exceptions to occur if the underlying database doesn't support the requested type value.
 public  void withStatement(Closure configureStatement) 
    Allows a closure to be passed in to configure the JDBC statements before they are executed. It can be used to do things like set the query size etc. When this method is invoked, the supplied closure is saved. Statements subsequent created from other methods will then be configured using this closure. The statement being configured is passed into the closure as its single argument, e.g.:
    sql.withStatement{ stmt -> stmt.maxRows == 10 }
    def firstTenRows = sql.rows("select * from table")
    
 public synchronized  void withTransaction(Closure closure) throws SQLException 
    Performs the closure within a transaction using a cached connection. If the closure takes a single argument, it will be called with the connection, otherwise it will be called with no arguments.