Пример #1
0
  private static String toSQL(String original, List<Object> parameters) throws IOException {
    StringBuffer sb = new StringBuffer();
    int x = 0;
    for (int i = 0; i < original.length(); i++) {
      char c = original.charAt(i);
      if (c == '?') {
        if (x >= parameters.size()) {
          return original;
        }

        Object p = parameters.get(x);
        if (p == null) {
          sb.append("null");
        } else if (p instanceof Number || p instanceof Boolean) {
          sb.append(p);
        } else if (p instanceof Date) {
          sb.append("'").append(Helper.getTime((Date) p)).append("'");
        } else if (p.getClass() == byte[].class || p.getClass() == Byte[].class) {
          appendBytes(sb, (byte[]) p);
        } else if (p instanceof InputStream) {
          appendStream(sb, (InputStream) p);
        } else if (p instanceof Reader) {
          appendReader(sb, (Reader) p);
        } else {
          sb.append(escapeSqlValue(p.toString()));
        }

        x++;
      } else {
        sb.append(c);
      }
    }

    return sb.toString();
  }
Пример #2
0
  @SuppressWarnings("unchecked")
  public String getDataValueText() {
    if (dataValue == null) {
      return null;

    } else if (DataType.DATATYPE_DATE == this.dataType) {
      return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) dataValue);

    } else if (DataType.DATATYPE_LIST == this.dataType) {
      StringBuffer buff = new StringBuffer("[");
      List col = (List) dataValue;
      for (Object o : col) {
        if (o == null) {
          buff.append("null, ");
        } else if (o instanceof Date) {
          buff.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) o)).append(", ");
        } else {
          buff.append(o.toString()).append(", ");
        }
      }
      buff.append("]");
      if (buff.length() > 2) {
        buff.delete(buff.length() - 3, buff.length() - 1);
      }
      return buff.toString();

    } else {
      return dataValue.toString();
    }
  }
  private void appendArgument(StringBuilder sb, Object argument) {
    if (argument instanceof String) {
      sb.append('\'');
      String stringLiteral = argument.toString();
      for (int i = 0; i < stringLiteral.length(); i++) {
        char c = stringLiteral.charAt(i);
        if (c == '\'') {
          sb.append('\'');
        }
        sb.append(c);
      }
      sb.append('\'');
      return;
    }

    if (argument instanceof Number) {
      sb.append(argument);
      return;
    }

    if (argument instanceof Boolean) {
      sb.append(renderBoolean((Boolean) argument));
      return;
    }

    if (argument instanceof Enum) {
      sb.append(renderEnum((Enum) argument));
      return;
    }

    if (argument instanceof Collection) {
      sb.append('(');
      boolean isFirstElement = true;
      for (Object o : (Collection) argument) {
        if (isFirstElement) {
          isFirstElement = false;
        } else {
          sb.append(", ");
        }
        appendArgument(sb, o);
      }
      sb.append(')');
      return;
    }

    if (argument instanceof Object[]) {
      sb.append('(');
      boolean isFirstElement = true;
      for (Object o : (Object[]) argument) {
        if (isFirstElement) {
          isFirstElement = false;
        } else {
          sb.append(", ");
        }
        appendArgument(sb, o);
      }
      sb.append(')');
      return;
    }

    if (argument instanceof Date) {
      sb.append('\'').append(renderDate((Date) argument)).append('\'');
      return;
    }

    sb.append(argument);
  }