* shutdown server
* @param aConnector
* @param aToken
*/
public void select(WebSocketConnector aConnector, Token aToken) {
TokenServer lServer = getServer();
if (mLog.isDebugEnabled()) {
mLog.debug("Processing 'select'...");
}
// check if user is allowed to run 'select' command
if (!SecurityFactory.checkRight(lServer.getUsername(aConnector), NS_JDBC + ".select")) {
lServer.sendToken(aConnector, lServer.createAccessDenied(aToken));
// return;
}
// obtain required parameters for query
String lTable = aToken.getString("table");
String lFields = aToken.getString("fields");
String lOrder = aToken.getString("order");
String lWhere = aToken.getString("where");
String lGroup = aToken.getString("group");
String lHaving = aToken.getString("having");
// buld SQL string
String lSQL =
"select "
+ lFields
+ " from "
+ lTable;
if (lWhere != null && lWhere.length() > 0) {
lSQL += " where " + lWhere;
}
if (lOrder != null && lOrder.length() > 0) {
lSQL += " order by " + lOrder;
}
// instantiate response token
Token lResponse = lServer.createResponse(aToken);
// TODO: should work with usual arrays as well!
// Object[] lColumns = null;
int lRowCount = 0;
int lColCount = 0;
List<Map> lColumns = new FastList<Map>();
List lData = new FastList<Map>();
try {
DBQueryResult lRes = DBConnectSingleton.querySQL(DBConnectSingleton.USR_SYSTEM, lSQL);
// TODO: metadata should be optional to save bandwidth!
// generate the meta data for the response
lColCount = lRes.metaData.getColumnCount();
lResponse.put("colcount", lColCount);
for (int i = 1; i <= lColCount; i++) {
// get name of colmuns
String lSimpleClass = JDBCTools.extractSimpleClass(
lRes.metaData.getColumnClassName(i));
// convert to json type
String lRIAType = JDBCTools.getJSONType(lSimpleClass, lRes.metaData);
Map lColHeader = new FastMap<String, Object>();
lColHeader.put("name", lRes.metaData.getColumnName(i));
lColHeader.put("jsontype", lRIAType);
lColHeader.put("jdbctype", lRes.metaData.getColumnTypeName(i));
lColumns.add(lColHeader);
}
// generate the result data
while (lRes.resultSet.next()) {
lData.add(getResultColumns(lRes.resultSet, lColCount));
lRowCount++;
}
} catch (Exception ex) {
mLog.error(ex.getClass().getSimpleName() + " on query: " + ex.getMessage());
}
// complete the response token
lResponse.put("rowcount", lRowCount);
lResponse.put("columns", lColumns);
lResponse.put("data", lData);
// send response to requester
lServer.sendToken(aConnector, lResponse);
}