return createCommandBuilder(config, sql, args);
}
private CommandBuilder createCommandBuilder(final Configuration config2, String sql,
List<Object> args) throws Exception {
final Session session = new Session(config2);
final CommandBuilder builder = new CommandBuilder(session, sql, args);
session.getMsgBus().addHandler(TableEvent.TYPE,
new TableEventHandler() {
@Override
public void onTable(TableEvent e) {
ObjectArray expr;
if (session.getFilters().size() > 0) {
config2.fireOnTableChange(session.getFilters());
// XXX need to do distinct?
expr = ObjectArray.newInstance();
TableFilter tf = session.getFilters().get(0);
String alias = tf.getAlias();
for (Column col : tf.getTable().getColumns()) {
expr.add(new ExpressionColumn(session, null, alias,
col.getName()));
}
} else {
expr = null;
}
builder.getSelect().setExpressions(expr);
}
});
builder.addValueChangeHandler(new ValueChangeHandler<Select>() {
@Override
public void onValueChange(ValueChangeEvent<Select> event) {
List<Object> args = new ArrayList<Object>();
String sql = event.getValue().getSQL(args);
config2.fireOnSqlChange(sql, args);
}
});
builderContainer.setWidget(builder);
config2.fireOnTableChange(session.getFilters());
return builder;
}