static Query createQuery(Criteria criteria)
throws TorqueException
{
Query query = new Query();
DB db = Torque.getDB(criteria.getDbName());
DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());
UniqueList selectModifiers = query.getSelectModifiers();
UniqueList selectClause = query.getSelectClause();
UniqueList fromClause = query.getFromClause();
UniqueList whereClause = query.getWhereClause();
UniqueList orderByClause = query.getOrderByClause();
UniqueList groupByClause = query.getGroupByClause();
UniqueList orderBy = criteria.getOrderByColumns();
UniqueList groupBy = criteria.getGroupByColumns();
UniqueList select = criteria.getSelectColumns();
Hashtable aliases = criteria.getAsColumns();
UniqueList modifiers = criteria.getSelectModifiers();
for (int i = 0; i < modifiers.size(); i++)
{
selectModifiers.add(modifiers.get(i));
}
for (int i = 0; i < select.size(); i++)
{
String columnName = (String) select.get(i);
if (columnName.indexOf('.') == -1 && columnName.indexOf('*') == -1)
{
throwMalformedColumnNameException("select", columnName);
}
String tableName = null;
selectClause.add(columnName);
int parenPos = columnName.indexOf('(');
if (parenPos == -1)
{
tableName = columnName.substring(0, columnName.indexOf('.'));
}
else if (columnName.indexOf('.') > -1)
{
tableName =
columnName.substring(parenPos + 1, columnName.indexOf('.'));
// functions may contain qualifiers so only take the last
// word as the table name.
int lastSpace = tableName.lastIndexOf(' ');
if (lastSpace != -1)
{
tableName = tableName.substring(lastSpace + 1);
}
}
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);
}
Iterator critKeys = criteria.keySet().iterator();
while (critKeys.hasNext())
{
String key = (String) critKeys.next();
Criteria.Criterion 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()
|| someCriteria[i].isIgnoreCase())
&& (dbMap
.getTable(table)
.getColumn(someCriteria[i].getColumn())
.getType()
instanceof String));
someCriteria[i].setIgnoreCase(ignorCase);
}
criterion.setDB(db);
whereClause.add(criterion.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);
if (join1.indexOf('.') == -1)
{
throwMalformedColumnNameException("join", join1);
}
if (join2.indexOf('.') == -1)
{
throwMalformedColumnNameException("join", join2);
}
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));
}
}
// need to allow for multiple group bys
if (groupBy != null && groupBy.size() > 0)
{
for (int i = 0; i < groupBy.size(); i++)
{
String groupByColumn = (String) groupBy.get(i);
if (groupByColumn.indexOf('.') == -1)
{
throwMalformedColumnNameException("group by",
groupByColumn);
}
groupByClause.add(groupByColumn);
}
}
Criteria.Criterion having = criteria.getHaving();
if (having != null)
{
//String groupByString = null;
query.setHaving(having.toString());
}
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 = (String) orderBy.get(i);
if (orderByColumn.indexOf('.') == -1)
{
throwMalformedColumnNameException("order by",
orderByColumn);
}
String tableName =
orderByColumn.substring(0, orderByColumn.indexOf('.'));
String table = criteria.getTableForAlias(tableName);
if (table == null)
{
table = tableName;
}
// 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(