示例#1
0
  /**
   * execute a plus operation
   *
   * @param sql
   * @param qr QueryResult to execute on it
   * @param expression
   * @param row row of resultset to execute
   * @return result
   * @throws PageException
   */
  private Object executePlus(PageContext pc, SQL sql, Query qr, ZExpression expression, int row)
      throws PageException {
    Object left = executeExp(pc, sql, qr, expression.getOperand(0), row);
    Object right = executeExp(pc, sql, qr, expression.getOperand(1), row);

    try {
      return new Double(Caster.toDoubleValue(left) + Caster.toDoubleValue(right));
    } catch (PageException e) {
      return Caster.toString(left) + Caster.toString(right);
    }
  }
示例#2
0
  static final void validate(String validate, Struct validateParams, Object obj)
      throws PageException {
    if (StringUtil.isEmpty(validate, true)) return;
    validate = validate.trim().toLowerCase();

    if (!validate.equals("regex") && !Decision.isValid(validate, obj))
      throw new ExpressionException(createMessage(validate, obj));

    // range
    if (validateParams == null) return;

    if (validate.equals("integer") || validate.equals("numeric") || validate.equals("number")) {
      double min = Caster.toDoubleValue(validateParams.get(MIN, null), Double.NaN);
      double max = Caster.toDoubleValue(validateParams.get(MAX, null), Double.NaN);
      double d = Caster.toDoubleValue(obj);
      if (!Double.isNaN(min) && d < min)
        throw new ExpressionException(
            validate
                + " ["
                + Caster.toString(d)
                + "] is out of range, value must be more than or equal to ["
                + min
                + "]");
      if (!Double.isNaN(max) && d > max)
        throw new ExpressionException(
            validate
                + " ["
                + Caster.toString(d)
                + "] is out of range, value must be less than or equal to ["
                + max
                + "]");
    } else if (validate.equals("string")) {
      double min = Caster.toDoubleValue(validateParams.get(MIN_LENGTH, null), Double.NaN);
      double max = Caster.toDoubleValue(validateParams.get(MAX_LENGTH, null), Double.NaN);
      String str = Caster.toString(obj);
      int l = str.length();
      if (!Double.isNaN(min) && l < ((int) min))
        throw new ExpressionException(
            "string ["
                + str
                + "] is to short ["
                + l
                + "], the string must be at least ["
                + min
                + "] characters");
      if (!Double.isNaN(max) && l > ((int) max))
        throw new ExpressionException(
            "string ["
                + str
                + "] is to long ["
                + l
                + "], the string can have a maximal length of ["
                + max
                + "] characters");
    } else if (validate.equals("regex")) {
      String pattern = Caster.toString(validateParams.get(PATTERN, null), null);
      String value = Caster.toString(obj);
      if (!StringUtil.isEmpty(pattern, true) && !IsValid.regex(value, pattern))
        throw new ExpressionException(
            "the string ["
                + value
                + "] does not match the regular expression pattern ["
                + pattern
                + "]");
    }
  }
示例#3
0
 public double castToDoubleValue(double defaultValue) {
   return Caster.toDoubleValue(get(key, defaultValue), defaultValue);
 }
示例#4
0
 public double castToDoubleValue() throws PageException {
   return Caster.toDoubleValue(get(key));
 }
示例#5
0
文件: Mod.java 项目: junekee/railo
 @Override
 public Object getValue(PageContext pc) throws PageException {
   return new Double(
       Caster.toDoubleValue(left.getValue(pc)) % Caster.toDoubleValue(right.getValue(pc)));
 }
示例#6
0
 /**
  * execute a multiply operation
  *
  * @param sql
  * @param qr QueryResult to execute on it
  * @param expression
  * @param row row of resultset to execute
  * @return result
  * @throws PageException
  */
 private Object executeMultiply(PageContext pc, SQL sql, Query qr, ZExpression expression, int row)
     throws PageException {
   return new Double(
       Caster.toDoubleValue(executeExp(pc, sql, qr, expression.getOperand(0), row))
           * Caster.toDoubleValue(executeExp(pc, sql, qr, expression.getOperand(1), row)));
 }
示例#7
0
  /**
   * Executes a Expression
   *
   * @param sql
   * @param qr
   * @param expression
   * @param row
   * @return result
   * @throws PageException
   */
  private Object executeExpression(
      PageContext pc, SQL sql, Query qr, ZExpression expression, int row) throws PageException {
    String op = StringUtil.toLowerCase(expression.getOperator());
    int count = expression.nbOperands();

    if (op.equals("and")) return executeAnd(pc, sql, qr, expression, row);
    else if (op.equals("or")) return executeOr(pc, sql, qr, expression, row);
    if (count == 0 && op.equals("?")) {
      int pos = sql.getPosition();
      if (sql.getItems().length <= pos)
        throw new DatabaseException("invalid syntax for SQL Statment", null, sql, null);
      sql.setPosition(pos + 1);
      return sql.getItems()[pos].getValueForCF();
    }
    // 11111111111111111111111111111111111111111111111111111
    else if (count == 1) {
      Object value = executeExp(pc, sql, qr, expression.getOperand(0), row);

      // Functions
      switch (op.charAt(0)) {
        case 'a':
          if (op.equals("abs")) return new Double(MathUtil.abs(Caster.toDoubleValue(value)));
          if (op.equals("acos")) return new Double(Math.acos(Caster.toDoubleValue(value)));
          if (op.equals("asin")) return new Double(Math.asin(Caster.toDoubleValue(value)));
          if (op.equals("atan")) return new Double(Math.atan(Caster.toDoubleValue(value)));
          break;
        case 'c':
          if (op.equals("ceiling")) return new Double(Math.ceil(Caster.toDoubleValue(value)));
          if (op.equals("cos")) return new Double(Math.cos(Caster.toDoubleValue(value)));
          break;
        case 'e':
          if (op.equals("exp")) return new Double(Math.exp(Caster.toDoubleValue(value)));
          break;
        case 'f':
          if (op.equals("floor")) return new Double(Math.floor(Caster.toDoubleValue(value)));
          break;
        case 'i':
          if (op.equals("is not null")) return Boolean.valueOf(value != null);
          if (op.equals("is null")) return Boolean.valueOf(value == null);
          break;
        case 'u':
          if (op.equals("upper") || op.equals("ucase")) return Caster.toString(value).toUpperCase();
          break;

        case 'l':
          if (op.equals("lower") || op.equals("lcase")) return Caster.toString(value).toLowerCase();
          if (op.equals("ltrim")) return StringUtil.ltrim(Caster.toString(value), null);
          if (op.equals("length")) return new Double(Caster.toString(value).length());
          break;
        case 'r':
          if (op.equals("rtrim")) return StringUtil.rtrim(Caster.toString(value), null);
          break;
        case 's':
          if (op.equals("sign")) return new Double(MathUtil.sgn(Caster.toDoubleValue(value)));
          if (op.equals("sin")) return new Double(Math.sin(Caster.toDoubleValue(value)));
          if (op.equals("soundex")) return StringUtil.soundex(Caster.toString(value));
          if (op.equals("sin")) return new Double(Math.sqrt(Caster.toDoubleValue(value)));
          break;
        case 't':
          if (op.equals("tan")) return new Double(Math.tan(Caster.toDoubleValue(value)));
          if (op.equals("trim")) return Caster.toString(value).trim();
          break;
      }

    }

    // 22222222222222222222222222222222222222222222222222222
    else if (count == 2) {

      if (op.equals("=") || op.equals("in")) return executeEQ(pc, sql, qr, expression, row);
      else if (op.equals("!=") || op.equals("<>")) return executeNEQ(pc, sql, qr, expression, row);
      else if (op.equals("<")) return executeLT(pc, sql, qr, expression, row);
      else if (op.equals("<=")) return executeLTE(pc, sql, qr, expression, row);
      else if (op.equals(">")) return executeGT(pc, sql, qr, expression, row);
      else if (op.equals(">=")) return executeGTE(pc, sql, qr, expression, row);
      else if (op.equals("-")) return executeMinus(pc, sql, qr, expression, row);
      else if (op.equals("+")) return executePlus(pc, sql, qr, expression, row);
      else if (op.equals("/")) return executeDivide(pc, sql, qr, expression, row);
      else if (op.equals("*")) return executeMultiply(pc, sql, qr, expression, row);
      else if (op.equals("^")) return executeExponent(pc, sql, qr, expression, row);

      Object left = executeExp(pc, sql, qr, expression.getOperand(0), row);
      Object right = executeExp(pc, sql, qr, expression.getOperand(1), row);

      // Functions
      switch (op.charAt(0)) {
        case 'a':
          if (op.equals("atan2"))
            return new Double(Math.atan2(Caster.toDoubleValue(left), Caster.toDoubleValue(right)));
          break;
        case 'b':
          if (op.equals("bitand"))
            return new Double(
                Operator.bitand(Caster.toDoubleValue(left), Caster.toDoubleValue(right)));
          if (op.equals("bitor"))
            return new Double(
                Operator.bitor(Caster.toDoubleValue(left), Caster.toDoubleValue(right)));
          break;
        case 'c':
          if (op.equals("concat")) return Caster.toString(left).concat(Caster.toString(right));
          break;
        case 'l':
          if (op.equals("like")) return executeLike(pc, sql, qr, expression, row);
          break;
        case 'm':
          if (op.equals("mod"))
            return new Double(
                Operator.modulus(Caster.toDoubleValue(left), Caster.toDoubleValue(right)));
          break;
      }

      throw new DatabaseException("unsopprted sql statement [" + op + "]", null, sql, null);
    }
    // 3333333333333333333333333333333333333333333333333333333333333333333
    else if (count == 3) {
      if (op.equals("between")) return executeBetween(pc, sql, qr, expression, row);
    }

    if (op.equals("in")) return executeIn(pc, sql, qr, expression, row);

    /*

    addCustomFunction("cot",1);
    addCustomFunction("degrees",1);
    addCustomFunction("log",1);
    addCustomFunction("log10",1);

    addCustomFunction("pi",0);
    addCustomFunction("power",2);
    addCustomFunction("radians",1);
    addCustomFunction("rand",0);
    addCustomFunction("round",2);
    addCustomFunction("roundmagic",1);
    addCustomFunction("truncate",2);
    addCustomFunction("ascii",1);
    addCustomFunction("bit_length",1);
    addCustomFunction("char",1);
    addCustomFunction("char_length",1);
    addCustomFunction("difference",2);
    addCustomFunction("hextoraw",1);
    addCustomFunction("insert",4);
    addCustomFunction("left",2);
    addCustomFunction("locate",3);
    addCustomFunction("octet_length",1);
    addCustomFunction("rawtohex",1);
    addCustomFunction("repeat",2);
    addCustomFunction("replace",3);
    addCustomFunction("right",2);
    addCustomFunction("space",1);
    addCustomFunction("substr",3);
    addCustomFunction("substring",3);
    addCustomFunction("curdate",0);
    addCustomFunction("curtime",0);
    addCustomFunction("datediff",3);
    addCustomFunction("dayname",1);
    addCustomFunction("dayofmonth",1);
    addCustomFunction("dayofweek",1);
    addCustomFunction("dayofyear",1);
    addCustomFunction("hour",1);
    addCustomFunction("minute",1);
    addCustomFunction("month",1);
    addCustomFunction("monthname",1);
    addCustomFunction("now",0);
    addCustomFunction("quarter",1);
    addCustomFunction("second",1);
    addCustomFunction("week",1);
    addCustomFunction("year",1);
    addCustomFunction("current_date",1);
    addCustomFunction("current_time",1);
    addCustomFunction("current_timestamp",1);
    addCustomFunction("database",0);
    addCustomFunction("user",0);
    addCustomFunction("current_user",0);
    addCustomFunction("identity",0);
    addCustomFunction("ifnull",2);
    addCustomFunction("casewhen",3);
    addCustomFunction("convert",2);
    //addCustomFunction("cast",1);
    addCustomFunction("coalesce",1000);
    addCustomFunction("nullif",2);
    addCustomFunction("extract",1);
    addCustomFunction("position",1);
    */

    // print(expression);
    throw new DatabaseException(
        "unsopprted sql statement (op-count:" + expression.nbOperands() + ";operator:" + op + ") ",
        null,
        sql,
        null);
  }