Methods Summary |
---|
private void | bind(java.sql.PreparedStatement stmt, int ind, java.util.Iterator bindings)Binds the search bindings to the statement in progress.
while( bindings.hasNext() ) {
SearchBinding bdg = (SearchBinding)bindings.next();
Object val = bdg.getValue();
if( val instanceof SearchCriteria ) {
SearchCriteria sc = (SearchCriteria)val;
bind(stmt, ind, sc.bindings());
}
else if( val instanceof BaseFacade ) {
BaseFacade ref = (BaseFacade)val;
stmt.setLong(ind++, ref.getObjectID());
}
else {
stmt.setObject(ind++, val);
}
}
|
public java.util.Collection | find(com.imaginary.lwp.Transaction tr, com.imaginary.lwp.SearchCriteria sc)Executes a search for objects meeting the specified criteria
using the specified transaction.
Iterator bindings = sc.bindings();
DistributedList list = new DistributedList();
String sql = getFindSQL(sc);
try {
JDBCTransaction trans;
Connection conn;
trans = (JDBCTransaction)tr;
try {
conn = trans.getConnection();
}
catch( Exception e ) {
e.printStackTrace();
return null;
}
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSetMetaData meta;
ResultSet rs;
int cc;
bind(stmt, 1, bindings);
rs = stmt.executeQuery();
meta = rs.getMetaData();
cc = meta.getColumnCount();
while( rs.next() ) {
HashMap map = new HashMap();
long oid = rs.getLong(1);
String cls = rs.getString(2);
for(int i=3; i<=cc; i++) {
String tbl = meta.getTableName(i).toUpperCase();
String name = meta.getColumnLabel(i).toUpperCase();
Object val = rs.getObject(i);
if( tbl.equals("") ) {
tbl = getPrimaryTable().toUpperCase();
}
name = tbl + "." + name;
if( rs.wasNull() ) {
val = null;
}
map.put(name, val);
}
list.add(getFacade(oid, cls, map));
}
return list;
}
catch( SQLException e ) {
throw new FindException("Database error: " + e.getMessage());
}
|
public final com.imaginary.lwp.BaseFacade | getFacade(long oid, java.lang.String cls, java.util.HashMap vals)Provides the reference object for objects supported by this
persistence support object.
try {
BaseFacade ref;
ref = (BaseFacade)Class.forName(cls).newInstance();
ref.assign(oid, vals);
return ref;
}
catch( Exception e ) {
throw new FindException("Database error: " + e.getMessage());
}
|
protected java.lang.String | getFindSQL(com.imaginary.lwp.SearchCriteria sc)Special method for building a SELECT statement that
will perform a search using the named search critieria.
StringBuffer sql = new StringBuffer("SELECT ");
ArrayList tables = new ArrayList();
String where, order;
Iterator it;
sql.append(getPrimaryTable() + "." + mapField("objectID"));
sql.append(", " + getPrimaryTable() + ".CRT_CLASS");
tables.add(getPrimaryTable());
it = sc.preloads();
while( it.hasNext() ) {
String fld = mapField((String)it.next());
int i = fld.indexOf(".");
String tbl;
if( i != -1 ) {
tbl = fld.substring(0, i);
if( !tables.contains(tbl) ) {
tables.add(tbl);
}
}
sql.append(", ");
sql.append(fld);
}
where = getWhere(sc.bindings(), tables);
order = getOrder(sc.sorts(), tables);
it = tables.iterator();
sql.append(" FROM ");
while( it.hasNext() ) {
sql.append((String)it.next());
if( it.hasNext() ) {
sql.append(", ");
}
}
if( where.length() > 0 ) {
sql.append(" WHERE ");
sql.append("(" + where + ")");
}
else if( tables.size() > 1 ) {
sql.append(" WHERE ");
}
it = tables.iterator();
while( it.hasNext() ) {
String tbl = (String)it.next();
JDBCJoin join;
if( tbl.equals(getPrimaryTable()) ) {
continue;
}
join = getJoin(tbl);
sql.append(" AND " + join.toString() + " ");
}
if( order.length() > 0 ) {
sql.append(" ORDER BY " + order);
}
return sql.toString();
|
protected abstract JDBCJoin | getJoin(java.lang.String tbl)Given a table, this method needs to provide a portion of a
WHERE clause that supports joining to the specified
table.
|
private java.lang.String | getOrder(java.util.Iterator sorts, java.util.ArrayList tables)Provides the ORDER BY clause to support ordering of
the results.
StringBuffer order = null;
if( !sorts.hasNext() ) {
return "";
}
do {
String col = (String)sorts.next();
int i;
if( order == null ) {
order = new StringBuffer();
}
else {
order.append(", ");
}
col = mapField(col);
order.append(col);
i = col.indexOf(".");
if( i != -1 ) {
String tbl = col.substring(0, i);
if( !tables.contains(tbl) ) {
tables.add(tbl);
}
}
} while( sorts.hasNext() );
return order.toString();
|
protected abstract java.lang.String | getPrimaryTable()Implemented by subclasses to provide the name of the primary
table for storing objects supported by this class.
|
private java.lang.String | getWhere(java.util.Iterator bindings, java.util.ArrayList tables)Provides the WHERE clause to support a find.
StringBuffer where = null;
if( !bindings.hasNext() ) {
return "";
}
do {
SearchBinding bdg = (SearchBinding)bindings.next();
Object val = bdg.getValue();
String fld = bdg.getField();
if( where == null ) {
where = new StringBuffer();
}
else {
where.append(" " + bdg.getBoolean().toString() + " ");
}
if( val instanceof SearchCriteria ) {
SearchCriteria sc = (SearchCriteria)val;
where.append("(");
where.append(getWhere(sc.bindings(), tables));
where.append(")");
}
else {
int i;
fld = mapField(fld);
where.append(fld);
i = fld.indexOf(".");
if( i != -1 ) {
String tbl = fld.substring(0, i);
if( !tables.contains(tbl) ) {
tables.add(tbl);
}
}
where.append(" " + bdg.getOperator().toString() + " ?");
}
} while( bindings.hasNext() );
if( where == null ) {
return "";
}
else {
return where.toString();
}
|
protected abstract java.lang.String | mapField(java.lang.String fld)Maps a field from the supported object's attributes to a database
field.
|