Search in sources :

Example 1 with ConnectionCallbackVoid

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);
        }
    });
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ConnectionCallbackVoid(com.serotonin.db.spring.ConnectionCallbackVoid)

Example 2 with ConnectionCallbackVoid

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);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Connection(java.sql.Connection) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConnectionCallbackVoid(com.serotonin.db.spring.ConnectionCallbackVoid)

Aggregations

ConnectionCallbackVoid (com.serotonin.db.spring.ConnectionCallbackVoid)2 Connection (java.sql.Connection)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Blob (java.sql.Blob)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1