}
}
@Override
public FactResultSet query(Query query) throws Exception {
FactResultSet frs = null;
if (query != null) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection(DB_URL);
FactSchema schema = validateQuery(query, con);
// from here on we have valid data
String factSchemaB64 = Base64.encodeBase64URLSafeString(query.getFromSchemaURN().getBytes());
StringBuilder querySql = new StringBuilder("SELECT ");
boolean firstRole = true;
for (String role : query.getRoles()) {
if (!firstRole) {
querySql.append(",");
}
querySql.append(role);
firstRole = false;
}
querySql.append(" FROM ").append(factSchemaB64);
List<String> queryParams = new ArrayList<String>();
querySql.append(" WHERE ");
for (WhereClause wc : query.getWhereClauses()) {
querySql.append('(');
querySql.append(wc.getComparedRole());
switch (wc.getCompareOperator()) {
case EQ:
querySql.append(" = ").append('?');
queryParams.add(wc.getSearchedValue());
break;
}
}
querySql.append(')');
// TODO load context, too
logger.info("performing query {}", querySql);
ps = con.prepareStatement(querySql.toString());
for (int i = 0; i < queryParams.size(); i++) {
ps.setString(i + 1, queryParams.get(i));
}
rs = ps.executeQuery();
if (rs != null) {
List<String> header = new ArrayList<String>();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
header.add(schema.fixSpellingOfRole(rs.getMetaData().getColumnName(i)));
}
frs = new FactResultSet();
frs.setHeader(header);
while (rs.next()) {
FactResult result = new FactResult();
List<String> values = new ArrayList<String>();
for (String head : header) {
values.add(rs.getString(head));
}
result.setValues(values);
frs.addFactResult(result);
}
}
} catch (Exception e) {
throw e;