public class DataSet
extends Sql
An enhancement of Groovy's Sql class providing support for accessing and querying databases using POGO fields and operators rather than JDBC-level API calls and RDBMS column names. So, instead of a query like:
def db = // an instance of groovy.sql.Sql
def sql = '''select * from Person
where (purchaseCount > ? and birthMonth = ?)
and (lastName < ? or lastName > ?)
and age < ? and age > ? and firstName != ?
order by firstName DESC, age'''
def params = [10, "January", "Zulu", "Alpha", 99, 5, "Bert"]
def sortedPeopleOfInterest = db.rows(sql, params)
You can write code like this:
def person = new DataSet(db, 'Person') // or db.dataSet('Person'), or db.dataSet(Person)
def janFrequentBuyers = person.findAll { it.purchaseCount > 10 && it.lastName == "January"
def sortedPeopleOfInterest = janFrequentBuyers.
findAll{ it.lastName < 'Zulu' || it.lastName > 'Alpha' }.
findAll{ it.age < 99 }.
findAll{ it.age > 5 }.
sort{ it.firstName }.
reverse().
findAll{ it.firstName != 'Bert' }.
sort{ it.age }
}
Currently, the Groovy source code for any accessed POGO must be on the
classpath at runtime. The expressions (or nested expressions) can contain
references to fields of the POGO, literals (i.e. constant Strings or numbers),
and variables captured from the enclosing scope. Method calls, arithmetic,
and other complex expressions are not currently supported.
| Fields inherited from class | Fields |
|---|---|
class Sql |
ALL_RESULT_SETS, ARRAY, BIGINT, BINARY, BIT, BLOB, BOOLEAN, CHAR, CLOB, DATALINK, DATE, DECIMAL, DISTINCT, DOUBLE, FIRST_RESULT_SET, FLOAT, INTEGER, JAVA_OBJECT, LOG, LONGVARBINARY, LONGVARCHAR, NO_RESULT_SETS, NULL, NUMERIC, OTHER, REAL, REF, SMALLINT, STRUCT, TIME, TIMESTAMP, TINYINT, VARBINARY, VARCHAR |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public void |
add(Map<String, Object> map)Adds the provided map of key-value pairs as a new row in the table represented by this DataSet. |
|
public void |
cacheConnection(Closure closure)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 void |
close()Closes this DataSet and its backing Sql resources. |
|
protected void |
closeResources(Connection connection, Statement statement, ResultSet results)An extension point allowing derived classes to change the behavior of resource closing.
|
|
protected void |
closeResources(Connection connection, Statement statement)An extension point allowing the behavior of resource closing to be overridden in derived classes.
|
|
public void |
commit()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 Connection |
createConnection()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 |
createView(Closure criteria)create a subset of the original dataset. |
|
public void |
each(Closure closure)Calls the provided closure for each of the rows of the table represented by this DataSet. |
|
public void |
each(int offset, int maxRows, Closure closure)Calls the provided closure for a "page" of rows from the table represented by this DataSet. |
|
public DataSet |
findAll(Closure where)Return a lazy-implemented filtered view of this DataSet. |
|
public Object |
firstRow()Returns the first row from a DataSet's underlying table |
|
public List<Object> |
getParameters()Returns the positional parameter values corresponding to getSql(). |
|
public String |
getSql()Returns the lazily assembled SQL query represented by this DataSet. |
|
protected SqlOrderByVisitor |
getSqlOrderByVisitor()Returns the lazily initialized visitor used to derive the SQL ORDER BY clause. |
|
protected SqlWhereVisitor |
getSqlWhereVisitor()Returns the lazily initialized visitor used to derive the SQL WHERE clause. |
|
public DataSet |
reverse()Return a lazy-implemented reverse-ordered view of this DataSet. |
|
public void |
rollback()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()Returns a List of all the rows from the DataSet. |
|
public List |
rows(int offset, int maxRows)Returns a "page" of the rows from the table a DataSet represents. |
|
public DataSet |
sort(Closure sort)Return a lazy-implemented re-ordered view of this DataSet. |
|
public int[] |
withBatch(Closure closure)Performs the closure (containing batch operations) within a batch. |
|
public int[] |
withBatch(int batchSize, Closure closure)Performs the closure (containing batch operations) within a batch. |
|
public void |
withTransaction(Closure closure)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.
|
Creates a DataSet whose table name is derived from the supplied type.
sql - the backing Sql instancetype - the type whose simple name identifies the tableAdds the provided map of key-value pairs as a new row in the table represented by this DataSet.
map - the key (column-name), value pairs to add as a new rowCaches 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.
closure - the given closureDelegates connection caching to the backing Sql instance.
Closes this DataSet and its backing Sql resources.
An extension point allowing derived classes to change the behavior of resource closing.
connection - the connection to closestatement - the statement to closeresults - the results to closeDelegates resource cleanup to the backing Sql instance.
An extension point allowing the behavior of resource closing to be overridden in derived classes.
connection - the connection to closestatement - the statement to closeDelegates resource cleanup to the backing Sql instance.
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.
Delegates commit handling to the backing Sql instance.
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.
Delegates connection creation to the backing Sql instance.
create a subset of the original dataset.
criteria - the filter closure describing the subsetDataSet viewCalls the provided closure for each of the rows of the table represented by this DataSet.
closure - called for each row with a GroovyResultSetCalls the provided closure for a "page" of rows from the table represented by this DataSet. A page is defined as starting at a 1-based offset, and containing a maximum number of rows.
offset - the 1-based offset for the first row to be processedmaxRows - the maximum number of rows to be processedclosure - called for each row with a GroovyResultSetReturn a lazy-implemented filtered view of this DataSet.
where - the filtering ClosureReturns the first row from a DataSet's underlying table
Returns the positional parameter values corresponding to getSql().
Returns the lazily assembled SQL query represented by this DataSet.
Returns the lazily initialized visitor used to derive the SQL ORDER BY clause.
Returns the lazily initialized visitor used to derive the SQL WHERE clause.
Return a lazy-implemented reverse-ordered view of this DataSet.
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.
Delegates rollback handling to the backing Sql instance.
Returns a List of all the rows from the DataSet.
Returns a "page" of the rows from the table a DataSet represents. A page is defined as starting at a 1-based offset, and containing a maximum number of rows.
offset - the 1-based offset for the first row to be processedmaxRows - the maximum number of rows to be processedReturn a lazy-implemented re-ordered view of this DataSet.
sort - the ordering ClosurePerforms the closure (containing batch operations) within a batch. Uses a batch size of zero, i.e. no automatic partitioning of batches.
Statement, or the
driver does not support batch statements. Throws BatchUpdateException
(a subclass of SQLException) if one of the commands sent to the
database fails to execute properly or attempts to return a result set.closure - the closure containing batch and optionally other statementsPerforms the closure (containing batch operations) within a batch. For example:
dataSet.withBatch(3) {
add(anint: 1, astring: "Groovy")
add(anint: 2, astring: "rocks")
add(anint: 3, astring: "the")
add(anint: 4, astring: "casbah")
}
SQLException)
if one of the commands sent to the database fails to execute properly.batchSize - partition the batch into batchSize pieces, i.e. after batchSize
addBatch() invocations, call executeBatch() automatically;
0 means manual calls to executeBatch are requiredclosure - the closure containing batch and optionally other statementsPerforms 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.
closure - the given closureDelegates transaction handling to the backing Sql instance.
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.