public static void createPreparedStatement(Criteria criteria,
StringBuffer queryString,
List params)
throws Exception
{
DB db = TurbineDB.getDB( criteria.getDbName() );
DatabaseMap dbMap = TurbineDB.getDatabaseMap( criteria.getDbName() );
Query query = new Query();
StringStackBuffer selectModifiers = query.getSelectModifiers();
StringStackBuffer selectClause = query.getSelectClause();
StringStackBuffer fromClause = query.getFromClause();
StringStackBuffer whereClause = query.getWhereClause();
StringStackBuffer orderByClause = query.getOrderByClause();
StringStackBuffer orderBy = criteria.getOrderByColumns();
boolean ignoreCase = criteria.isIgnoreCase();
StringStackBuffer select = criteria.getSelectColumns();
Hashtable aliases = criteria.getAsColumns();
StringStackBuffer modifiers = criteria.getSelectModifiers();
for (int i=0; i<modifiers.size(); i++)
{
selectModifiers.add( modifiers.get(i) );
}
for (int i=0; i<modifiers.size(); i++)
{
selectModifiers.add( modifiers.get(i) );
}
for (int i=0; i<select.size(); i++)
{
String columnName = select.get(i);
String tableName = null;
selectClause.add(columnName);
int parenPos = columnName.indexOf('(');
if (parenPos == -1)
{
tableName = columnName.substring(0,
columnName.indexOf('.') );
}
else
{
tableName = columnName.substring(parenPos + 1,
columnName.indexOf('.') );
}
String tableName2 = criteria.getTableForAlias(tableName);
if ( tableName2 != null )
{
fromClause.add(
new StringBuffer(tableName.length() +
tableName2.length() + 1)
.append(tableName2).append(' ').append(tableName)
.toString() );
}
else
{
fromClause.add(tableName);
}
}
Iterator it = aliases.keySet().iterator();
while(it.hasNext())
{
String key = (String)it.next();
selectClause.add((String)aliases.get(key) + " AS " + key);
}
Enumeration e = criteria.keys();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
Criteria.Criterion criterion =
(Criteria.Criterion)criteria.getCriterion(key);
Criteria.Criterion[] someCriteria =
criterion.getAttachedCriterion();
String table = null;
for (int i=0; i<someCriteria.length; i++)
{
String tableName = someCriteria[i].getTable();
table = criteria.getTableForAlias(tableName);
if ( table != null )
{
fromClause.add(
new StringBuffer(tableName.length() +
table.length() + 1)
.append(table).append(' ').append(tableName)
.toString() );
}
else
{
fromClause.add(tableName);
table = tableName;
}
boolean ignorCase = (criteria.isIgnoreCase() &&
(dbMap.getTable(table).getColumn(
someCriteria[i].getColumn()).getType() instanceof String));
someCriteria[i].setIgnoreCase(ignorCase);
}
criterion.setDB(db);
StringBuffer sb = new StringBuffer();
criterion.appendPsTo (sb,params);
whereClause.add( sb.toString() );
}
List join = criteria.getJoinL();
if ( join != null)
{
for ( int i=0; i<join.size(); i++ )
{
String join1 = (String)join.get(i);
String join2 = (String)criteria.getJoinR().get(i);
String tableName = join1.substring(0, join1.indexOf('.'));
String table = criteria.getTableForAlias(tableName);
if ( table != null )
{
fromClause.add(
new StringBuffer(tableName.length() +
table.length() + 1)
.append(table).append(' ').append(tableName)
.toString() );
}
else
{
fromClause.add(tableName);
}
int dot = join2.indexOf('.');
tableName = join2.substring(0, dot);
table = criteria.getTableForAlias(tableName);
if ( table != null )
{
fromClause.add(
new StringBuffer(tableName.length() +
table.length() + 1)
.append(table).append(' ').append(tableName)
.toString() );
}
else
{
fromClause.add(tableName);
table = tableName;
}
boolean ignorCase = (criteria.isIgnoreCase() &&
(dbMap.getTable(table).getColumn(
join2.substring(dot+1, join2.length()) )
.getType() instanceof String));
whereClause.add(
SqlExpression.buildInnerJoin(join1, join2,
ignorCase, db) );
}
}
if ( orderBy != null && orderBy.size() > 0)
{
// Check for each String/Character column and apply
// toUpperCase().
for (int i=0; i<orderBy.size(); i++)
{
String orderByColumn = orderBy.get(i);
String table = orderByColumn.substring(0,orderByColumn.indexOf('.') );
// See if there's a space (between the column list and sort
// order in ORDER BY table.column DESC).
int spacePos = orderByColumn.indexOf(' ');
String columnName;
if (spacePos == -1)
columnName = orderByColumn.substring(orderByColumn.indexOf('.') + 1);
else
columnName = orderByColumn.substring(orderByColumn.indexOf('.') + 1, spacePos);
ColumnMap column = dbMap.getTable(table).getColumn( columnName );
if ( column.getType() instanceof String )
{
if (spacePos == -1)
orderByClause.add( db.ignoreCaseInOrderBy(orderByColumn) );
else
orderByClause.add( db.ignoreCaseInOrderBy(orderByColumn.substring(0, spacePos)) + orderByColumn.substring(spacePos) );
}
else
{
orderByClause.add(orderByColumn);
}
}
}
// Limit the number of rows returned.
int limit = criteria.getLimit();
int offset = criteria.getOffset();
String limitString = null;
if ( offset > 0 && db.supportsNativeOffset() )
{
switch(db.getLimitStyle())
{
case DB.LIMIT_STYLE_MYSQL:
limitString = new StringBuffer().append(offset)
.append(", ")
.append(limit)
.toString();
break;
case DB.LIMIT_STYLE_POSTGRES:
limitString = new StringBuffer().append(limit)
.append(", ")
.append(offset)
.toString();
break;
}
// Now set the criteria's limit and offset to return the
// full resultset since the results are limited on the
// server.
criteria.setLimit(-1);
criteria.setOffset(0);
}
else if (limit > 0 && db.supportsNativeLimit() )
{
limitString = String.valueOf(limit);
// Now set the criteria's limit to return the full
// resultset since the results are limited on the server.