Refine search
@SuppressWarnings({"unchecked", "rawtypes" }) public IterableArgument(final Object value, final StatementContext ctx) { if (value.getClass().isArray()) { ImmutableList.Builder<Object> builder = ImmutableList.builder(); for (int i = 0; i < Array.getLength(value); i++) { builder.add(Array.get(value, i)); } this.values = builder.build(); } else { this.values = (Iterable<?>) value; } this.foreman = new Foreman(); List<ArgumentFactory> factories = (List<ArgumentFactory>) ctx.getAttribute(Jdbi.ARG_FACTORIES); factories.forEach(foreman::register); }
if (a != null) { try { this.context.setAttribute("position", null); a.apply(i + 1, statement, this.context); Integer pos = (Integer) this.context.getAttribute("position"); i += Optional.ofNullable(pos).orElse(1); p += 1; this.context.setAttribute("position", null); } catch (SQLException e) { throw new UnableToExecuteStatementException( this.context.setAttribute("position", null); a.apply(i + 1, statement, this.context); Integer pos = (Integer) this.context.getAttribute("position"); i += Optional.ofNullable(pos).orElse(1); } catch (SQLException e) {
final Binding binding = statementContext.getBinding(); final PreparedStatement statement = statementContext.getStatement(); if (statement != null) {
@Override public RewrittenStatement rewrite(String sql, Binding params, StatementContext ctx) { if ((ctx.getSqlObjectType() != null) && (ctx.getSqlObjectMethod() != null)) { final StringBuilder query = new StringBuilder(sql.length() + 100); query.append("/* "); final String className = ctx.getSqlObjectType().getSimpleName(); if (!className.isEmpty()) { query.append(className).append('.'); } query.append(ctx.getSqlObjectMethod().getName()); query.append(" */ "); query.append(sql); return rewriter.rewrite(query.toString(), params, ctx); } return rewriter.rewrite(sql, params, ctx); } }
public QuboleDbSource map(int index, ResultSet r, StatementContext ctx) throws SQLException { Encrypt encrypt = (Encrypt) ctx.getAttribute("encryptClass"); return new QuboleDbSource(r.getInt("id"), r.getString("type"), r.getString("name"), r.getString("datasource_type"), encrypt.convertToEntityAttribute(r.getString("url")), r.getInt("ds_set_id"), r.getLong("dbtap_id"), encrypt.convertToEntityAttribute(r.getString("auth_token"))); } }
UUID thisRowId = (UUID) r.getObject("id"); if (ctx.getAttribute("__accumulator") != null) { carrier = (Carrier) ctx.getAttribute("__accumulator"); if (!carrier.getId().equals(thisRowId)) { carrier = new Carrier(); carrier.setTitle(r.getString("title")); carrier.setDescription(r.getString("description")); carrier.setId((UUID) r.getObject("id")); carrier.setTenantId((UUID) r.getObject("tenant_id")); ctx.setAttribute("__accumulator", carrier); return carrier;
public JdbcSource map(int index, ResultSet r, StatementContext ctx) throws SQLException { Encrypt encrypt = (Encrypt) ctx.getAttribute("encryptClass"); return new JdbcSource(r.getInt("id"), r.getString("type"), r.getString("name"), r.getString("datasource_type"), encrypt.convertToEntityAttribute(r.getString("url")), r.getInt("ds_set_id"), encrypt.convertToEntityAttribute(r.getString("username")), encrypt.convertToEntityAttribute(r.getString("password"))); } }
@Override public void apply(final int position, final PreparedStatement statement, final StatementContext ctx) throws SQLException { if (collection == null) { statement.setNull(position, sqlType); } else { if (statement.getConnection().getMetaData().getDatabaseProductName().equalsIgnoreCase("h2")) { // h2 does not support createArrayOf statement.setObject(position, collection.toArray()); } else { final Array sqlArray = ctx.getConnection().createArrayOf(type, collection.toArray()); statement.setArray(position, sqlArray); } } }
@Override public void beforeExecution(final PreparedStatement stmt, final StatementContext ctx) throws SQLException { stmt.setFetchSize(fetchSize); if (shouldStream) { if (ctx != null && ctx.getConnection() != null && ctx.getConnection().getMetaData() != null && MYSQL.equalsIgnoreCase(ctx.getConnection().getMetaData().getDatabaseProductName())) { try { // Magic value to force MySQL to stream from the database // See http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-implementation-notes.html (ResultSet) stmt.setFetchSize(Integer.MIN_VALUE); } catch (final SQLException e) { // Shouldn't happen? The exception will be logged by log4jdbc stmt.setFetchSize(0); } } else { // Other engines (H2, PostgreSQL, etc.) stmt.setFetchSize(0); } } else { stmt.setFetchSize(fetchSize); } } }
@SuppressWarnings("unchecked") @Override public T map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException { JdbcMapper<T> mapper = (JdbcMapper<T>) statementContext.getAttribute(STATEMENT_MAPPER); if (mapper == null) { mapper = dynamicMapper.getMapper(resultSet.getMetaData()); statementContext.setAttribute(STATEMENT_MAPPER, mapper); } return mapper.map(resultSet, mapper.newMappingContext(resultSet)); } }
private void setTxnIsolation(StatementContext ctx, int level) throws SQLException { final Connection c = ctx.getConnection(); if (c.getTransactionIsolation() != level) { c.setTransactionIsolation(level); } } });
@Override public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException { Integer length = value.size(); PG_UUID[] uuids = new PG_UUID[length]; for (int i = 0; i < length; i++) { uuids[i] = new PG_UUID(value.get(i).toString()); } Array ary = ctx.getConnection().createArrayOf("uuid", uuids); statement.setArray(position, ary); } };
public void apply(SQLStatement q) throws SQLException { final int initial_level = q.getContext().getConnection().getTransactionIsolation(); q.addStatementCustomizer(new BaseStatementCustomizer() { @Override public void beforeExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException { setTxnIsolation(ctx, level.intValue()); } @Override public void afterExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException { setTxnIsolation(ctx, initial_level); } private void setTxnIsolation(StatementContext ctx, int level) throws SQLException { final Connection c = ctx.getConnection(); if (c.getTransactionIsolation() != level) { c.setTransactionIsolation(level); } } }); } }
@Override public void apply(final int position, final PreparedStatement stmt, final StatementContext ctx) throws SQLException { int i = position; for (Object value : values) { foreman.createArgument(value.getClass(), value, ctx).apply(i, stmt, ctx); i += 1; } ctx.setAttribute("position", i - 1); }
@Override public String locate(final String statementName, final StatementContext context) throws Exception context.setAttribute(MigratoryStatementRewriter.SKIP_REWRITE, null); context.setAttribute(MigratoryStatementRewriter.SKIP_REWRITE, Boolean.TRUE); return rawSql; final String sqlLocation = SQL_LOCATION + context.getAttribute("db_type") + "/" + statementNames[0] + ".st"; template.setAttributes(context.getAttributes()); final String sql = template.toString(); context.setAttribute(MigratoryStatementRewriter.SKIP_REWRITE, Boolean.TRUE); return statementName;
@Override public MetricName getStatementName(StatementContext statementContext) { final Class<?> clazz = statementContext.getSqlObjectType(); final Method method = statementContext.getSqlObjectMethod(); if (clazz != null) { final String rawSql = statementContext.getRawSql(); final String group = clazz.getPackage().getName(); final String name = clazz.getSimpleName(); final String type = method == null ? rawSql : method.getName(); return StatementName.getJmxSafeName(group, name, type); } return null; } }
@Override public String getMessage() { String base = super.getMessage(); StatementContext ctx = getStatementContext(); if (ctx == null) { return base; } else { return String.format("%s [statement:\"%s\", located:\"%s\", rewritten:\"%s\", arguments:%s]", base, ctx.getRawSql(), ctx.getLocatedSql(), ctx.getRewrittenSql(), ctx.getBinding()); } } }
@Override public StatementName getStatementName(final StatementContext statementContext) { final String rawSql = statementContext.getRawSql(); if (rawSql == null || rawSql.length() == 0) { return EMPTY_SQL; } return null; } }
public String getTemplate(final String statementName, final StatementContext context) throws Exception { StringTemplate template; try { template = templateGroup.getInstanceOf(statementName); } catch (IllegalArgumentException ex) { LOG.info("Didn't find a template when expected: %s: %s", groupName, statementName); return defaultLocate(statementName, context); } // This can be used by timing collectors to figure out which statement was used. context.setAttribute(STATEMENT_CLASS, className); context.setAttribute(STATEMENT_GROUP, groupName); context.setAttribute(STATEMENT_NAME, statementName); template.setAttributes(context.getAttributes()); final String sql = template.toString(); LOG.trace("SQL for %s: %s", groupName, sql); return sql; } }
private String templatize(final String rawSql, final StatementContext context) { // This is not very effective but then again, most templates will be used only once and // this code is not intended to be run 1000 times a second anyway. final StringTemplate template = new StringTemplate(rawSql, AngleBracketTemplateLexer.class); template.setAttributes(context.getAttributes()); final String sql = template.toString(); LOG.trace("SQL: {}", sql); return sql; } }