Two things:
1) I forgot to mention this only works with the 1.2 milestone release after 8 or 9 (I think).
2) I attached some cleaned up code below that won't get word-wrapped by e-mail.
*------------------------------------------------------------------
import org.objectstyle.cayenne.exp.Expression;
import org.objectstyle.cayenne.exp.ExpressionFactory;
import org.objectstyle.cayenne.query.SelectQuery;
import org.objectstyle.cayenne.CayenneDataObject;
import org.objectstyle.cayenne.dba.AutoAdapter;
import org.objectstyle.cayenne.conn.PoolManager;
import org.objectstyle.cayenne.access.DataNode;
import org.objectstyle.cayenne.access.DataDomain;
import org.objectstyle.cayenne.access.DataContext;
import org.objectstyle.cayenne.access.DbLoader;
import org.objectstyle.cayenne.conf.Configuration;
import org.objectstyle.cayenne.conf.DefaultConfiguration;
import org.objectstyle.cayenne.map.DataMap;
import org.objectstyle.cayenne.query.SelectQuery;
import javax.sql.DataSource;
import groovy.sql.Sql;
import groovy.ui.Console;
def dclass="oracle.jdbc.driver.OracleDriver"
def url="jdbc:oracle:thin:10.1.1.1:MYSID"
def user="MyUser"
def pw="MyPassword";
def schema="MySchema"
// create DataSource
DataSource dataSource = new PoolManager(dclass, url, 1, 5, user, pw)
// get DataMap via DbLoader
DbLoader dbLoader =
new DbLoader(dataSource.getConnection(),
new AutoAdapter(dataSource),
null);
dbLoader.setGenericClassName("org.objectstyle.cayenne.CayenneDataObject");
DataMap dataMap =
dbLoader.loadDataMapFromDB(schema, "USER%", new DataMap());
// assemble stack
DataNode dataNode = new DataNode("DynamicNode");
dataNode.setAdapter(new AutoAdapter(dataSource));
dataNode.setDataSource(dataSource);
dataNode.addDataMap(dataMap);
DataDomain dataDomain = new DataDomain("DynamicDomain");
dataDomain.addNode(dataNode);
DataContext dataContext = dataDomain.createDataContext();
println "Found: ${dataContext.entityResolver.dbEntities.size()}"
/* **************************
// Starts a console so you can try some dynamic queries
println 'Starting Console'
println 'Bound variables: dataContext, dataMap'
def console = new Console()
console.shell.setVariable("dataContext", dataContext)
console.shell.setVariable("dataMap", dataMap)
console.run()
return 1
****************************/
/* **************************/
// Code to show you certain objEntities and their attributes
def objEntities =
dataContext.entityResolver.objEntities.findAll
{ e -> e.name.contains('User') || e.name.contains('Role') }
objEntities.each { e ->
println "${e.name} ${e.dbEntityName}"
println " Attributes:"
e.attributes.each {
attr -> println " ${attr.name}\t${attr.type}"
}
println " Relationships:"
e.relationships.each { rel ->
println " ${rel.name}\t${rel.targetEntityName}"
}
}
/* ***************************/
/**
* This class will be used to add the get(propertyName) method to
* all CayenneDataObject instances so we can access
* persistent properties in a Groovy way (using the dot notation).
*/
class CayenneDataObjectCategory {
static Object get(CayenneDataObject dataObject, String prop){
return dataObject.readProperty(prop);
}
}
// Example of printing out the roles for a set of users...
// The use(...) {...} below adds get(prop) to all
// CayenneDataObject instances used inside of the
// closure below.
use (CayenneDataObjectCategory)
{
// I don't think it would be too hard to create a Groovy
// Cayenne query builder to reduce the syntax needed for
// creating a query....
SelectQuery query = new SelectQuery("Users")
def qual =
ExpressionFactory.likeIgnoreCaseExp("userLogin", "jacarl%")
query.setQualifier(qual)
dataContext.performQuery(query).each
{ u ->
println "${u.userName} has these roles:"
u.userRolesArray.each { userRole ->
userRole.toRoles.each { role ->
println " Role: ${role.roleCode}"
}
}
}
}
______________________________________________________________________
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.
Katun Corporation -- www.katun.com
_____________________________________________________________________
This archive was generated by hypermail 2.0.0 : Fri Mar 03 2006 - 17:52:00 EST