sb.append("SELECT ");
for (int i = 0; i < cdModel.getRowCount(); i++) {
Column column = (Column)cdModel.getValueAt(i, 0);
if ( (cdModel.getValueAt(i, 1) == null) && (cdModel.getValueAt(i, 2) == null) ) {
sb.append(column.getTableName() + "." + column.getColumnName() + ",");
} else if (cdModel.getValueAt(i, 1) != null) {
Boolean isCount = (Boolean)cdModel.getValueAt(i, 1);
if (isCount)
sb.append("COUNT(" + column.getTableName() + "." + column.getColumnName() + ") AS COUNT_" +
column.getTableName() + "_" + column.getColumnName() + ",");
else {
sb.append(column.getTableName() + "." + column.getColumnName() + ",");
}
} else if (cdModel.getValueAt(i, 2) != null) {
Boolean isSum = (Boolean)cdModel.getValueAt(i, 2);
if (isSum)
sb.append("SUM(" + column.getTableName() + "." + column.getColumnName() + ") AS SUM_" +
column.getTableName() + "_" + column.getColumnName() + ",");
else {
sb.append(column.getTableName() + "." + column.getColumnName() + ",");
}
}
}
sb.replace(sb.length() - 1, sb.length(), " ");
BaseClass object = container.getObject();
String basicSQL = object.getBasicSQL();
if (basicSQL.indexOf("WHERE") == -1)
basicSQL = basicSQL.substring(basicSQL.indexOf("FROM"));
else {
basicSQL = basicSQL.substring(basicSQL.indexOf("FROM"), basicSQL.indexOf("WHERE"));
}
sb.append(basicSQL + " ");
boolean hasWhere = false;
basicSQL = object.getBasicSQL();
if (basicSQL.indexOf("WHERE") == -1)
sb.append("WHERE ");
else {
hasWhere = true;
basicSQL = basicSQL.substring(basicSQL.indexOf("WHERE"));
sb.append(basicSQL + " AND ");
}
Vector filters = new Vector();
Vector andors = new Vector();
for (int i = 0; i < caModel.getRowCount(); i++) {
if ( (caModel.getValueAt(i, 2) != null) && (!caModel.getValueAt(i, 2).toString().trim().equals("")) ) {
hasWhere = true;
String andorString = (String)caModel.getValueAt(i, 0);
boolean andor = (andorString.equals("AND"));
andors.add(andor);
Column column = (Column)caModel.getValueAt(i, 1);
String filterString = (String)caModel.getValueAt(i, 2);
filterString = filterString.replaceAll("~", column.getTableName() + "." + column.getColumnName());
filters.add(filterString);
}
}
if (hasWhere) {
//first, add and wheres
for (int i = 0; i < filters.size(); i++) {
Boolean andor = (Boolean)andors.get(i);
if (andor) {
String filterString = (String)filters.get(i);
sb.append(filterString + " AND ");
}
}
//next, add or wheres
for (int i = 0; i < filters.size(); i++) {
Boolean andor = (Boolean)andors.get(i);
if (!andor) {
String filterString = (String)filters.get(i);
sb.append(filterString + " OR ");
}
}
}
if (!hasWhere)
sb.replace(sb.lastIndexOf("WHERE"), sb.lastIndexOf("WHERE") + 5, "");
else
sb.replace(sb.length() - 4, sb.length() - 1, "");
boolean hasGroup = true;
boolean hasCountOrSum = false;
for (int i = 0; i < cdModel.getRowCount(); i++) {
if ( (cdModel.getValueAt(i, 1) == null) && (cdModel.getValueAt(i, 2) == null) ) {
} else if (cdModel.getValueAt(i, 1) != null) {
Boolean isCount = (Boolean)cdModel.getValueAt(i, 1);
if (isCount)
hasCountOrSum = true;
} else if (cdModel.getValueAt(i, 2) != null) {
Boolean isSum = (Boolean)cdModel.getValueAt(i, 2);
if (isSum)
hasCountOrSum = true;
}
}
if (!hasCountOrSum)
hasGroup = false;
if (hasGroup) {
sb.append("GROUP BY ");
for (int i = 0 ; i < cdModel.getRowCount(); i++) {
Column column = (Column)cdModel.getValueAt(i, 0);
if ( (cdModel.getValueAt(i, 1) == null) && (cdModel.getValueAt(i, 2) == null) ) {
sb.append(column.getTableName() + "." + column.getColumnName() + ",");
} else if (cdModel.getValueAt(i, 1) != null) {
Boolean isCount = (Boolean)cdModel.getValueAt(i, 1);
if (!isCount)
sb.append(column.getTableName() + "." + column.getColumnName() + ",");
} else if (cdModel.getValueAt(i, 2) != null) {
Boolean isSum = (Boolean)cdModel.getValueAt(i, 2);
if (!isSum)
sb.append(column.getTableName() + "." + column.getColumnName() + ",");
}
}
sb.replace(sb.length() - 1, sb.length(), " ");
}
boolean hasOrder = false;
if (coModel.getRowCount() > 0)
hasOrder = true;
if (hasOrder) {
sb.append("ORDER BY ");
for (int i = 0; i < coModel.getRowCount(); i++) {
Column column = (Column)coModel.getValueAt(i, 0);
Boolean order = false;
if (coModel.getValueAt(i, 1) != null)
order = (Boolean)coModel.getValueAt(i, 1);
String orderString = "DESC";
if (order)
orderString = "ASC";
int tempRow = findRow(column, container.getColumnDisplayTable());
if ( (cdModel.getValueAt(tempRow, 1) == null) && (cdModel.getValueAt(tempRow, 2) == null) ) {
sb.append(column.getTableName() + "." + column.getColumnName() + " " + orderString + ",");
} else if (cdModel.getValueAt(tempRow, 1) != null) {
Boolean isCount = (Boolean)cdModel.getValueAt(tempRow, 1);
if (isCount)
sb.append("COUNT(" + column.getTableName() + "." + column.getColumnName() + ") " + orderString + ",");
else {
sb.append(column.getTableName() + "." + column.getColumnName() + " " + orderString + ",");
hasGroup = true;
}
} else if (cdModel.getValueAt(tempRow, 2) != null) {
Boolean isSum = (Boolean)cdModel.getValueAt(tempRow, 2);
if (isSum)
sb.append("SUM(" + column.getTableName() + "." + column.getColumnName() + ") " + orderString + ",");
else {
sb.append(column.getTableName() + "." + column.getColumnName() + " " + orderString + ",");
hasGroup = true;
}
}
}