Пример #1
0
  @Override
  public String format(PreparedStatement stmt) {
    try {
      if (stmt instanceof JdbcPreparedStatement) {
        String sql = (String) ReflectUtil.getValue(SQL_FIELD, stmt);

        boolean insert = false;
        if (sql.toUpperCase().startsWith("INSERT INTO")) {
          int pos = sql.indexOf("(");
          sql = sql.substring(0, pos) + "SET " + sql.substring(pos + 1);
          sql = sql.substring(0, sql.indexOf(" VALUES"));
          insert = true;
        }

        CommandInterface command = (CommandInterface) ReflectUtil.getValue(COMMAND_FIELD, stmt);
        ObjectArray<? extends ParameterInterface> parameters = command.getParameters();

        int pos = 0;
        for (int i = 0; i < parameters.size(); i++) {
          ParameterInterface parameter = parameters.get(i);
          Value value = parameter.getParamValue();

          if (insert) {
            String string = "=" + value;

            pos = sql.indexOf(',', pos);
            if (pos == -1) {
              pos = sql.indexOf(')');
              sql = sql.substring(0, pos) + string;
              break;
            }

            sql = sql.substring(0, pos) + string + sql.substring(pos);
            pos += string.length() + 1;
          } else {
            pos = sql.indexOf('?');
            sql = sql.substring(0, pos) + value + sql.substring(pos + 1);
          }
        }

        return sql;
      }
    } catch (Throwable t) {
      // $FALL-THROUGH$
    }

    return super.format(stmt);
  }