|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgroovy.sql.Sql
groovy.sql.DataSet
public class DataSet
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.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class groovy.sql.Sql |
---|
Sql.AbstractQueryCommand, Sql.PreparedQueryCommand, Sql.QueryCommand |
Field Summary |
---|
Fields inherited from class groovy.sql.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<java.lang.String,java.lang.Object> map)
|
void |
cacheConnection(Closure closure)
Caches the connection used while the closure is active. |
protected void |
closeResources(java.sql.Connection connection,
java.sql.Statement statement)
An extension point allowing the behavior of resource closing to be overridden in derived classes. |
protected void |
closeResources(java.sql.Connection connection,
java.sql.Statement statement,
java.sql.ResultSet results)
An extension point allowing derived classes to change the behavior of resource closing. |
void |
commit()
If this SQL object was created with a Connection then this method commits the connection. |
protected java.sql.Connection |
createConnection()
An extension point allowing derived classes to change the behavior of connection creation. |
DataSet |
createView(Closure criteria)
|
void |
each(Closure closure)
|
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()
If this SQL object was created with a Connection then this method rolls back the connection. |
java.util.List |
rows()
Returns a List of all of the rows from the table a DataSet represents |
DataSet |
sort(Closure sort)
|
void |
withTransaction(Closure closure)
Performs the closure within a transaction using a cached connection. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DataSet(Sql sql, java.lang.Class type)
public DataSet(Sql sql, java.lang.String table)
Method Detail |
---|
protected java.sql.Connection createConnection() throws java.sql.SQLException
Sql
createConnection
in class Sql
java.sql.SQLException
- if a SQL error occursprotected void closeResources(java.sql.Connection connection, java.sql.Statement statement, java.sql.ResultSet results)
Sql
closeResources
in class Sql
connection
- the connection to closestatement
- the statement to closeresults
- the results to closeprotected void closeResources(java.sql.Connection connection, java.sql.Statement statement)
Sql
closeResources
in class Sql
connection
- the connection to closestatement
- the statement to closepublic void cacheConnection(Closure closure) throws java.sql.SQLException
Sql
cacheConnection
in class Sql
closure
- the given closure
java.sql.SQLException
- if a database error occurspublic void withTransaction(Closure closure) throws java.sql.SQLException
Sql
withTransaction
in class Sql
closure
- the given closure
java.sql.SQLException
- if a database error occurspublic void commit() throws java.sql.SQLException
Sql
commit
in class Sql
java.sql.SQLException
- if a database access error occurspublic void rollback() throws java.sql.SQLException
Sql
rollback
in class Sql
java.sql.SQLException
- if a database access error occurspublic void add(java.util.Map<java.lang.String,java.lang.Object> map) throws java.sql.SQLException
java.sql.SQLException
public DataSet findAll(Closure where)
public DataSet sort(Closure sort)
public DataSet reverse()
public void each(Closure closure) throws java.sql.SQLException
java.sql.SQLException
public java.lang.String getSql()
public java.util.List getParameters()
protected SqlWhereVisitor getSqlWhereVisitor()
protected SqlOrderByVisitor getSqlOrderByVisitor()
public DataSet createView(Closure criteria)
public java.util.List rows() throws java.sql.SQLException
java.sql.SQLException
- if a database error occurspublic java.lang.Object firstRow() throws java.sql.SQLException
java.sql.SQLException
- if a database error occurs
|
Copyright © 2003-2011 The Codehaus. All rights reserved. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |