public void bindParams(PreparedStatement ps, EqlRun eqlRun, Logger logger) { this.eqlRun = eqlRun; boundParams = new StringBuilder(); this.ps = ps; switch (eqlRun.getPlaceHolderType()) { case AUTO_SEQ: for (int i = 0; i < eqlRun.getPlaceholderNum(); ++i) setParam(i, getParamByIndex(i), ParamExtra.Normal); break; case MANU_SEQ: for (int i = 0; i < eqlRun.getPlaceholderNum(); ++i) setParam(i, findParamBySeq(i + 1), ParamExtra.Normal); break; case VAR_NAME: for (int i = 0; i < eqlRun.getPlaceholderNum(); ++i) setParam(i, findParamByName(i), ParamExtra.Normal); break; default: break; } bindExtraParams(); if (boundParams.length() > 0) logger.debug("param: {}", boundParams); }
private boolean regiesterOut(int index) throws SQLException { EqlParamPlaceholder.InOut inOut = eqlRun.getPlaceHolders()[index].getInOut(); if (EqlUtils.isProcedure(eqlRun.getSqlType()) && inOut != EqlParamPlaceholder.InOut.IN) ((CallableStatement) ps).registerOutParameter(index + 1, Types.VARCHAR); return inOut == EqlParamPlaceholder.InOut.OUT; }
private void bindExtraParams() { Object[] extraBindParams = eqlRun.getExtraBindParams(); if (extraBindParams == null) return; for (int i = eqlRun.getPlaceholderNum(); i < eqlRun.getPlaceholderNum() + extraBindParams.length; ++i) setParam(i, extraBindParams[i - eqlRun.getPlaceholderNum()], ParamExtra.Extra); }
private Object getParamByIndex(int index) { EqlParamPlaceholder[] placeHolders = eqlRun.getPlaceHolders(); if (index < placeHolders.length && EqlUtils.isProcedure(eqlRun.getSqlType()) && placeHolders[index].getInOut() == EqlParamPlaceholder.InOut.OUT) return null; Object[] params = eqlRun.getParams(); if (params != null && index < params.length) return params[index]; throw new EqlExecuteException("[" + eqlRun.getSqlId() + "]执行过程中缺少参数"); }
private Object findParamByName(int index) { String varName = eqlRun.getPlaceHolders()[index].getPlaceholder(); ExpressionEvaluator evaluator = eqlRun.getEqlConfig().getExpressionEvaluator(); Object property = evaluator.eval(varName, eqlRun); if (property != null) return property; String propertyName = EqlUtils.convertUnderscoreNameToPropertyName(varName); return Objects.equal(propertyName, varName) ? property : evaluator.eval(propertyName, eqlRun); }
@Test public void testUnless() { Map<String, Object> map = Maps.newHashMap(); map.put("a", 1); Eql eql = new Eql().params(map); eql.execute(); EqlRun eqlRun = eql.getEqlRuns().get(0); assertThat(eqlRun.getPrintSql(), is(equalTo("SELECT A,B,C,D,E FROM EQL_TEST"))); eql = new Eql().params("1"); eql.execute(); eqlRun = eql.getEqlRuns().get(0); assertThat(eqlRun.getPrintSql(), is(equalTo("SELECT A,B,C,D,E FROM EQL_TEST WHERE A = ?"))); }
public int addBatch(EqlRun eqlRun) throws SQLException { PreparedStatement ps = batchedMap.get(eqlRun.getRunSql()); if (ps == null) { ps = EqlUtils.prepareSQL(sqlClassPath, eqlConfig, eqlRun, sqlId, tagSqlId); batchedMap.put(eqlRun.getRunSql(), ps); batchedPs.add(ps); } eqlRun.bindParams(ps, sqlClassPath); ps.addBatch(); ++currentBatches; return maxBatches > 0 && currentBatches >= maxBatches ? executeBatch(false) : 0; }
private void setParam(int index, Object value, ParamExtra extra) { EqlParamPlaceholder placeHolder = eqlRun.getPlaceHolder(index); try { switch (extra) { case Extra: setParamExtra(placeHolder, index, value); break; default: setParamEx(placeHolder, index, value); break; } } catch (SQLException e) { throw new EqlExecuteException("set parameters fail", e); } }
private Object findParamBySeq(int index) { return getParamByIndex(eqlRun.getPlaceHolders()[index - 1].getSeq() - 1); }