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);
}
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());
}
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();
}
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();
}
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);
}
}
Aggregations