예제 #1
0
  private String orderByPhrase() {
    List<String> orderByList = new ArrayList<String>();
    try {
      Collection<AnnotationValue> orderBys =
          ModelUtil.getAnnotationValue(method, mirror, "orderBy");
      for (AnnotationValue orderByValue : orderBys) {
        AnnotationMirror orderByMirror = (AnnotationMirror) orderByValue.getValue();
        String columnProperty = ModelUtil.getAnnotationValue(method, orderByMirror, "column");
        ColumnProperty column = columns.findByProperty(columnProperty);
        if (column == null)
          throw new AnnotationError(method, mirror, "invalid column property: " + columnProperty);
        Order order =
            Order.valueOf(
                ((VariableElement) ModelUtil.getAnnotationValue(method, orderByMirror, "order"))
                    .getSimpleName()
                    .toString());
        orderByList.add("\"+" + column.columnName(true) + "+\" " + order.keyword);
      }
    } catch (AnnotationError ex) {
      // ignore
    }

    return orderByList.size() > 0
        ? " ORDER BY " + StringUtil.join(orderByList.iterator(), ", ")
        : "";
  }
예제 #2
0
  protected CharSequence[] defaultSQL(Iterator<VariableElement> iter) {
    int paramCount = method.getParameters().size();
    List<String> code = new ArrayList<String>();
    CollectionUtil.addAll(
        code,
        "java.util.List<String> __conditions = new java.util.ArrayList<String>("
            + paramCount
            + ");",
        "java.util.List<Object> __params = new java.util.ArrayList<Object>(" + paramCount + ");");

    List<String> params = new ArrayList<String>();
    List<String> where = new ArrayList<String>();
    while (iter.hasNext()) {
      VariableElement param = iter.next();
      String paramName = param.getSimpleName().toString();
      boolean primitive = ModelUtil.isPrimitive(param.asType());
      if (paramName.indexOf('_') == -1) {
        ColumnProperty column = getColumn(param);
        where.add("\"+" + column.columnName(true) + "+\"=?");
        params.add(column.toNativeTypeCode(paramName));
        if (!primitive) CollectionUtil.addAll(code, "if(" + paramName + "!=null){", PLUS);
        CollectionUtil.addAll(
            code,
            "__conditions.add(" + where.get(where.size() - 1).substring(2) + "\");",
            "__params.add(" + column.toNativeTypeCode(paramName) + ");");
        if (!primitive) CollectionUtil.addAll(code, MINUS, "}");
        iter.remove();
      } else {
        int underscore = paramName.indexOf('_');
        String hint = paramName.substring(0, underscore);
        String propertyName = paramName.substring(underscore + 1);
        ColumnProperty column = getColumn(param, propertyName);

        String hintValue = HINTS.get(hint);
        if (hintValue != null) {
          where.add("\"+" + column.columnName(true) + "+\"" + hintValue);
          params.add(column.toNativeTypeCode(paramName));
          if (!primitive) CollectionUtil.addAll(code, "if(" + paramName + "!=null){", PLUS);
          CollectionUtil.addAll(
              code,
              "__conditions.add(" + where.get(where.size() - 1).substring(2) + "\");",
              "__params.add(" + column.toNativeTypeCode(paramName) + ");");
          if (!primitive) CollectionUtil.addAll(code, MINUS, "}");
          iter.remove();
        } else if ("from".equals(hint)) {
          iter.remove();
          final VariableElement nextParam = iter.next();
          final String nextParamName = nextParam.getSimpleName().toString();
          boolean nextPrimitive = ModelUtil.isPrimitive(nextParam.asType());
          if (!nextParamName.equals("to_" + propertyName))
            throw new AnnotationError(
                method, "the next parameter of " + paramName + " must be to_" + propertyName);
          if (param.asType() != nextParam.asType())
            throw new AnnotationError(
                method, paramName + " and " + nextParamName + " must be of same type");
          where.add("\"+" + column.columnName(true) + "+\" BETWEEN ? and ?");
          params.add(column.toNativeTypeCode(paramName));
          params.add(column.toNativeTypeCode(nextParamName));
          if (!primitive || !nextPrimitive) {
            String condition = "";
            if (!primitive) condition = paramName + "!=null";
            if (!nextPrimitive) {
              if (condition.length() > 0) condition += " && ";
              condition += nextParamName + "!=null";
            }
            CollectionUtil.addAll(code, "if(" + condition + "){", PLUS);
          }
          CollectionUtil.addAll(
              code,
              "__conditions.add(" + where.get(where.size() - 1).substring(2) + "\");",
              "__params.add(" + column.toNativeTypeCode(paramName) + ");",
              "__params.add(" + column.toNativeTypeCode(nextParamName) + ");");
          if (!primitive || !nextPrimitive) CollectionUtil.addAll(code, MINUS, "}");
          iter.remove();
        } else throw new AnnotationError(param, "invalid hint: " + hint);
      }
    }

    Boolean ignoreNullConditions = false;
    try {
      ignoreNullConditions = ModelUtil.getAnnotationValue(method, mirror, "ignoreNullConditions");
    } catch (AnnotationError ex) {
      // ignore
    }

    if (ignoreNullConditions) {
      String orderByPhrase = orderByPhrase();
      if (orderByPhrase.length() > 0) initialQuery = "";

      String queryInitialValue;
      if (initialQuery == null) queryInitialValue = "null";
      else queryInitialValue = '"' + initialQuery + '"';
      CollectionUtil.addAll(
          code,
          "String __query = " + queryInitialValue + ";",
          "if(__conditions.size()>0)",
          PLUS,
          "__query "
              + (initialQuery == null ? "" : "+")
              + "= \" WHERE \" + "
              + StringUtil.class.getName()
              + ".join(__conditions.iterator(), \" AND \");",
          MINUS);

      if (orderByPhrase.length() > 0) {
        CollectionUtil.addAll(code, "__query += \"" + orderByPhrase + "\";");
      }

      CollectionUtil.addAll(code, "__query", "__params.toArray()");
      return code.toArray(new CharSequence[code.size()]);
    } else {
      StringBuilder query = new StringBuilder();
      if (initialQuery != null) query.append(initialQuery).append(' ');
      if (where.size() > 0)
        query.append("WHERE ").append(StringUtil.join(where.iterator(), " AND "));
      query.append(orderByPhrase());
      return new CharSequence[] {query, StringUtil.join(params.iterator(), ", ")};
    }
  }