I have a set of classes that can generate SQL code for me. I would like to use a generated SQL to implement the Query interface found in ibatis. I'm trying to create a special Query component that caches all results in memory.
I'm having trouble writing this method:
public void loadAllItems() {
String sql = buildSQL();
List<Map<String, Object>> query = DB.rows(sql, new RowBounds(0, maxRows+1));
this.rows = new ArrayList<Item>();
for (Map<String,Object> row : query) {
Item item = new PropertysetItem();
// How can I create a QueryDefinition for my query???
Collection<Object> propertyIds = definition.getPropertyIds();
for (Object propertyId : propertyIds) {
Object value = row.get(propertyId);
if (value!=null) {
item.addItemProperty(propertyId, new ObjectProperty(value));
}
}
this.rows.add(item);
}
}
Here, the DB.rows method returns a List of rows. For each row, column names are mapped to column values. I have the rows and values for all columns. The only problem is that I cannot put results into a list of Item instances. I don't know how to create a QueryDefinition instance for an arbitrary SQL.
I know that there is an SQL builder ( http://mybatis.github.io/mybatis-3/statement-builders.html ) but I DO NOT want to use it. I want to use a plain SQL that was already generated, and put the results into a list of Item instances. How do I do that? What kind of properties do I need to assign before I could use these items to implement the Query interface of ibatis?
Okay, this was silly. They are named "properties" but actually they are just column names. So if I have a plain SQL and I know the names of the colum names then I don't need a QueryDefinition at all. Suppose we have a list of column names called "colNames" for a SQL query:
public void loadAllItems() {
String sql = buildSQL();
List<Map<String, Object>> query = DB.rows(sql, new RowBounds(0, maxRows+1));
this.rows = new ArrayList<Item>();
for (Map<String,Object> row : query) {
Item item = new PropertysetItem();
for (Object colName : this.colNames) {
Object value = row.get(colName);
if (value!=null) {
item.addItemProperty(colName, new ObjectProperty(value));
}
}
this.rows.add(item);
}
}