|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object groovy.sql.Sql groovy.sql.DataSet
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. Also, at the moment, the expressions (or nested expressions) can only contain references to fields of the POGO or literals (i.e. constant Strings or numbers). This limitation may be removed in a future version of Groovy.
Field Summary |
---|
Fields inherited from class Sql | |
---|---|
ARRAY, BIGINT, BINARY, BIT, BLOB, BOOLEAN, CHAR, CLOB, DATALINK, DATE, DECIMAL, DISTINCT, DOUBLE, FLOAT, INTEGER, JAVA_OBJECT, LOG, LONGVARBINARY, LONGVARCHAR, NULL, NUMERIC, OTHER, REAL, REF, SMALLINT, STRUCT, TIME, TIMESTAMP, TINYINT, VARBINARY, VARCHAR |
Constructor Summary | |
DataSet(Sql sql, java.lang.Class type)
|
|
DataSet(Sql sql, java.lang.String table)
|
Method Summary | |
---|---|
void
|
add(java.util.Map map)
|
void
|
cacheConnection(Closure closure)
|
void
|
close()
|
protected void
|
closeResources(java.sql.Connection connection, java.sql.Statement statement, java.sql.ResultSet results)
|
protected void
|
closeResources(java.sql.Connection connection, java.sql.Statement statement)
|
void
|
commit()
|
protected java.sql.Connection
|
createConnection()
|
DataSet
|
createView(Closure criteria)
|
void
|
each(Closure closure)
Calls the provided closure for each of the rows of the table represented by this DataSet. |
void
|
each(int offset, int maxRows, Closure closure)
Calls the provided closure for a "page" of rows from the table represented by this DataSet. |
DataSet
|
findAll(Closure where)
|
java.lang.Object
|
firstRow()
Returns the first row from a DataSet's underlying table |
java.util.List
|
getParameters()
|
java.lang.String
|
getSql()
|
protected SqlOrderByVisitor
|
getSqlOrderByVisitor()
|
protected SqlWhereVisitor
|
getSqlWhereVisitor()
|
DataSet
|
reverse()
|
void
|
rollback()
|
java.util.List
|
rows()
Returns a List of all of the rows from the table a DataSet represents. |
java.util.List
|
rows(int offset, int maxRows)
Returns a "page" of the rows from the table a DataSet represents. |
DataSet
|
sort(Closure sort)
|
void
|
withTransaction(Closure closure)
|
Constructor Detail |
---|
public DataSet(Sql sql, java.lang.Class type)
public DataSet(Sql sql, java.lang.String table)
Method Detail |
---|
public void add(java.util.Map map)
@Override public void cacheConnection(Closure closure)
@Override public void close()
@Override protected void closeResources(java.sql.Connection connection, java.sql.Statement statement, java.sql.ResultSet results)
@Override protected void closeResources(java.sql.Connection connection, java.sql.Statement statement)
@Override public void commit()
@Override protected java.sql.Connection createConnection()
public DataSet createView(Closure criteria)
public void each(Closure closure)
closure
- called for each row with a GroovyResultSet
public void each(int offset, int maxRows, Closure closure)
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 GroovyResultSet
public DataSet findAll(Closure where)
public java.lang.Object firstRow()
public java.util.List getParameters()
public java.lang.String getSql()
protected SqlOrderByVisitor getSqlOrderByVisitor()
protected SqlWhereVisitor getSqlWhereVisitor()
public DataSet reverse()
@Override public void rollback()
public java.util.List rows()
public java.util.List rows(int offset, int maxRows)
offset
- the 1-based offset for the first row to be processedmaxRows
- the maximum number of rows to be processed
public DataSet sort(Closure sort)
@Override public void withTransaction(Closure closure)
Groovy Documentation