Search in sources :

Example 6 with SqlBind

use of org.eclipse.scout.rt.server.jdbc.SqlBind in project scout.rt by eclipse.

the class AbstractSqlStyleTest method testBuildBindForBigDecimal.

/**
 * Test for {@link AbstractSqlStyle#buildBindFor} for {@link BigDecimal} with nulltype {@link BigDecimal}.
 */
@Test
public void testBuildBindForBigDecimal() {
    final int testValue = 100;
    final BigDecimal b = BigDecimal.valueOf(testValue);
    SqlBind bin = sql.buildBindFor(b, BigDecimal.class);
    assertEquals(Types.NUMERIC, bin.getSqlType());
    assertTrue(bin.getValue() instanceof BigDecimal);
}
Also used : SqlBind(org.eclipse.scout.rt.server.jdbc.SqlBind) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 7 with SqlBind

use of org.eclipse.scout.rt.server.jdbc.SqlBind in project scout.rt by eclipse.

the class AbstractSqlStyleTest method testBuildBindForNullBigDecimal.

/**
 * Test for {@link AbstractSqlStyle#buildBindFor} for null values with nulltype {@link BigDecimal}.
 */
@Test
public void testBuildBindForNullBigDecimal() {
    SqlBind bin = sql.buildBindFor(null, BigDecimal.class);
    assertEquals(Types.NUMERIC, bin.getSqlType());
    assertNull(bin.getValue());
}
Also used : SqlBind(org.eclipse.scout.rt.server.jdbc.SqlBind) Test(org.junit.Test)

Example 8 with SqlBind

use of org.eclipse.scout.rt.server.jdbc.SqlBind in project scout.rt by eclipse.

the class StatementProcessor method prepareInputStatementAndBinds.

private void prepareInputStatementAndBinds() {
    // bind inputs and set replace token on inputs
    m_currentInputBindMap = new TreeMap<Integer, SqlBind>();
    ISqlStyle sqlStyle = m_callerService.getSqlStyle();
    for (IBindInput in : m_inputList) {
        SqlBind bind = in.produceSqlBindAndSetReplaceToken(sqlStyle);
        assert (bind != null) == in.isJdbcBind(sqlStyle);
        if (bind != null) {
            m_currentInputBindMap.put(in.getJdbcBindIndex(), bind);
        }
    }
    // set replace token on outputs
    for (IBindOutput out : m_outputList) {
        out.setReplaceToken(sqlStyle);
    }
    m_currentInputStm = m_bindModel.getFilteredStatement();
}
Also used : SqlBind(org.eclipse.scout.rt.server.jdbc.SqlBind) ISqlStyle(org.eclipse.scout.rt.server.jdbc.style.ISqlStyle)

Example 9 with SqlBind

use of org.eclipse.scout.rt.server.jdbc.SqlBind in project scout.rt by eclipse.

the class StatementProcessor method createSqlDump.

protected String createSqlDump(boolean statementWithBinds, boolean statementPlainText) {
    StringBuilder debugBindBuf = new StringBuilder();
    if (m_currentInputBindMap == null) {
        try {
            prepareInputStatementAndBinds();
        } catch (Exception t) {
            return t.getMessage();
        }
    }
    if (m_currentInputBindMap == null) {
        return "";
    }
    if (m_inputList != null) {
        for (IBindInput in : m_inputList) {
            SqlBind bind = m_currentInputBindMap.get(in.getJdbcBindIndex());
            if (bind == null) {
                continue;
            }
            debugBindBuf.append("IN  ");
            debugBindBuf.append(in.getToken().getParsedToken());
            debugBindBuf.append(" => ");
            debugBindBuf.append(in.getToken().getReplaceToken());
            debugBindBuf.append(" [");
            debugBindBuf.append(SqlBind.decodeJdbcType(bind.getSqlType()));
            switch(bind.getSqlType()) {
                case Types.BLOB:
                case Types.CLOB:
                    {
                        // nop
                        break;
                    }
                default:
                    {
                        debugBindBuf.append(" ");
                        debugBindBuf.append(bind.getValue());
                    }
            }
            debugBindBuf.append("]");
            debugBindBuf.append("\n");
        }
    }
    if (m_outputList != null) {
        for (IBindOutput out : m_outputList) {
            debugBindBuf.append("OUT ");
            debugBindBuf.append(out.getToken().getParsedToken());
            debugBindBuf.append(" => ");
            debugBindBuf.append(out.getToken().getReplaceToken());
            Class bindType = out.getBindType();
            if (bindType != null) {
                debugBindBuf.append(" [");
                debugBindBuf.append(bindType.getSimpleName());
                debugBindBuf.append("]");
            }
            debugBindBuf.append("\n");
        }
    }
    StringBuilder buf = new StringBuilder();
    if (statementWithBinds) {
        buf.append("SQL with binds:\n");
        buf.append(SqlFormatter.wellform(m_originalStm).trim());
        if (debugBindBuf != null && debugBindBuf.length() > 0) {
            buf.append("\n");
            buf.append(debugBindBuf.toString().trim());
        }
    }
    if (statementPlainText) {
        String p = m_currentInputStm != null ? m_currentInputStm : "";
        ArrayList<SqlBind> bindList = new ArrayList<SqlBind>(m_currentInputBindMap.values());
        int pos = findNextBind(p, 0);
        int bindIndex = 0;
        while (pos >= 0 && bindIndex < bindList.size()) {
            SqlBind bind = bindList.get(bindIndex);
            String replacement;
            switch(bind.getSqlType()) {
                case Types.BLOB:
                    {
                        replacement = "__BLOB__";
                        break;
                    }
                case Types.CLOB:
                    {
                        replacement = "__CLOB__";
                        break;
                    }
                default:
                    {
                        replacement = m_callerService.getSqlStyle().toPlainText(bind.getValue());
                    }
            }
            if (replacement == null) {
                replacement = "NULL";
            }
            replacement = replacement.replace('?', ' ');
            // next
            // NOSONAR squid:S2259
            p = p.substring(0, pos) + replacement + p.substring(pos + 1);
            pos = findNextBind(p, pos);
            bindIndex++;
        }
        if (buf.length() > 0) {
            buf.append("\n");
        }
        buf.append("SQL PLAIN Log:\n");
        buf.append(SqlFormatter.wellform(p).trim());
    }
    return buf.toString();
}
Also used : SqlBind(org.eclipse.scout.rt.server.jdbc.SqlBind) ArrayList(java.util.ArrayList) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException)

Example 10 with SqlBind

use of org.eclipse.scout.rt.server.jdbc.SqlBind in project scout.rt by eclipse.

the class StatementProcessor method processSelectStreaming.

@SuppressWarnings({ "resource", "squid:S2095" })
@Override
public void processSelectStreaming(Connection conn, IStatementCache cache, ISelectStreamHandler handler) {
    PreparedStatement ps = null;
    ResultSet rs = null;
    ISqlStyle sqlStyle = m_callerService.getSqlStyle();
    try {
        int rowCount = 0;
        while (hasNextInputBatch()) {
            nextInputBatch();
            prepareInputStatementAndBinds();
            dump();
            ps = cache.getPreparedStatement(conn, m_currentInputStm);
            bindBatch(ps);
            registerActiveStatement(ps);
            try {
                rs = ps.executeQuery();
                ResultSetMetaData meta = rs.getMetaData();
                int colCount = meta.getColumnCount();
                while (rs.next()) {
                    ArrayList<SqlBind> row = new ArrayList<SqlBind>(colCount);
                    for (int i = 0; i < colCount; i++) {
                        int type = meta.getColumnType(i + 1);
                        Object value = sqlStyle.readBind(rs, meta, type, i + 1);
                        row.add(new SqlBind(type, value));
                    }
                    handler.handleRow(conn, ps, rs, rowCount, row);
                    rowCount++;
                    if (m_maxRowCount > 0 && rowCount >= m_maxRowCount) {
                        break;
                    }
                }
            } finally {
                unregisterActiveStatement(ps);
                /*
           * The PreparedStatement and the ResultSet of the last input batch are not allowed to be closed
           * yet because the handler could do finishing work.
           * Closing the last PreparedStatement and its ResultSet is done in the outer finally block.
           */
                if (hasNextInputBatch()) {
                    releasePreparedStatementAndResultSet(ps, cache, rs);
                }
            }
        }
        finishOutputBatch();
        handler.finished(conn, ps, rs, rowCount);
    } catch (SQLException | RuntimeException e) {
        throw BEANS.get(PlatformExceptionTranslator.class).translate(e).withContextInfo("statement", createSqlDump(true, false));
    } finally {
        releasePreparedStatementAndResultSet(ps, cache, rs);
    }
}
Also used : SqlBind(org.eclipse.scout.rt.server.jdbc.SqlBind) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ResultSetMetaData(java.sql.ResultSetMetaData) ISqlStyle(org.eclipse.scout.rt.server.jdbc.style.ISqlStyle) ResultSet(java.sql.ResultSet) PlatformExceptionTranslator(org.eclipse.scout.rt.platform.exception.PlatformExceptionTranslator)

Aggregations

SqlBind (org.eclipse.scout.rt.server.jdbc.SqlBind)15 Test (org.junit.Test)9 PreparedStatement (java.sql.PreparedStatement)7 BigDecimal (java.math.BigDecimal)3 ISqlStyle (org.eclipse.scout.rt.server.jdbc.style.ISqlStyle)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 PlatformExceptionTranslator (org.eclipse.scout.rt.platform.exception.PlatformExceptionTranslator)1 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)1 IHolder (org.eclipse.scout.rt.platform.holders.IHolder)1 NVPair (org.eclipse.scout.rt.platform.holders.NVPair)1 ISqlService (org.eclipse.scout.rt.server.jdbc.ISqlService)1