StringBuilder statement = new StringBuilder();
statement.append("select\n");
Iterator<DBColumn> columnIterator = columns.iterator();
while (columnIterator.hasNext()) {
DBColumn column = columnIterator.next();
statement.append(" ");
if (useAliases) {
statement.append(aliases.getAlias(column.getDataset()));
statement.append(".");
}
statement.append(column.getName());
if (columnIterator.hasNext()) {
statement.append(",");
}
statement.append("\n");
}
statement.append("from\n");
Iterator<DBDataset> datasetIterator = datasets.iterator();
while (datasetIterator.hasNext()) {
DBDataset dataset = datasetIterator.next();
statement.append(" ");
statement.append(dataset.getName());
if (useAliases) {
statement.append(" ");
statement.append(aliases.getAlias(dataset));
}
if (datasetIterator.hasNext()) {
statement.append(",\n");
}
}
if (joinBundle != null && !joinBundle.isEmpty()) {
statement.append("\nwhere\n");
Iterator<DatasetJoin> joinIterator = joinBundle.getJoins().iterator();
while (joinIterator.hasNext()) {
DatasetJoin join = joinIterator.next();
if (!join.isEmpty()) {
Map<DBColumn,DBColumn> mappings = join.getMappings();
Iterator<DBColumn> joinColumnIterator = mappings.keySet().iterator();
while (joinColumnIterator.hasNext()) {
DBColumn column1 = joinColumnIterator.next();
DBColumn column2 = mappings.get(column1);
statement.append(" ");
if (useAliases) {
statement.append(aliases.getAlias(column1.getDataset()));
statement.append(".");
}
statement.append(column1.getName());
statement.append(" = ");
if (useAliases) {
statement.append(aliases.getAlias(column2.getDataset()));
statement.append(".");
}
statement.append(column2.getName());
if (joinIterator.hasNext() || joinColumnIterator.hasNext()) {
statement.append(" and\n");
}
}
}