use of com.serotonin.db.spring.ConnectionCallbackVoid in project ma-modules-public by infiniteautomation.
the class SqlController method query.
private void query(final String sqlString, final String serializedDataMsg, final Map<String, Object> model) {
Common.databaseProxy.doInConnection(new ConnectionCallbackVoid() {
@Override
public void doInConnection(Connection conn) throws SQLException {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlString);
ResultSetMetaData meta = rs.getMetaData();
int columns = meta.getColumnCount();
List<String> headers = new ArrayList<String>(columns);
for (int i = 0; i < columns; i++) headers.add(meta.getColumnLabel(i + 1));
List<List<Object>> data = new LinkedList<List<Object>>();
List<Object> row;
while (rs.next()) {
row = new ArrayList<Object>(columns);
data.add(row);
for (int i = 0; i < columns; i++) {
if (meta.getColumnType(i + 1) == Types.CLOB)
row.add(rs.getString(i + 1));
else if (meta.getColumnType(i + 1) == Types.LONGVARBINARY || meta.getColumnType(i + 1) == Types.BLOB) {
Blob blob = rs.getBlob(i + 1);
Object o;
if (blob == null)
o = null;
else
o = SerializationHelper.readObjectInContext(blob.getBinaryStream());
row.add(serializedDataMsg + "(" + o + ")");
} else
row.add(rs.getObject(i + 1));
}
}
model.put("headers", headers);
model.put("data", data);
}
});
}
use of com.serotonin.db.spring.ConnectionCallbackVoid in project ma-core-public by infiniteautomation.
the class DerbyProxy method runScript.
@Override
public void runScript(final InputStream in, OutputStream out) {
final OutputStream finalOut;
if (out == null)
finalOut = new ByteArrayOutputStream();
else
finalOut = out;
Common.databaseProxy.doInConnection(new ConnectionCallbackVoid() {
@Override
public void doInConnection(Connection conn) {
try {
ij.runScript(conn, in, "ASCII", finalOut, Common.UTF8);
} catch (UnsupportedEncodingException e) {
throw new ShouldNeverHappenException(e);
}
}
});
if (finalOut != out) {
ByteArrayOutputStream baos = (ByteArrayOutputStream) finalOut;
String s = new String(baos.toByteArray(), Common.UTF8_CS);
log.info("Derby script output: " + s);
}
}
Aggregations