Esempio n. 1
0
  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
    switch (typeName) {
      case BOOLEAN:
        writer.keyword(value == null ? "UNKNOWN" : (Boolean) value ? "TRUE" : "FALSE");
        break;
      case NULL:
        writer.keyword("NULL");
        break;
      case CHAR:
      case DECIMAL:
      case DOUBLE:
      case BINARY:

        // should be handled in subtype
        throw Util.unexpected(typeName);

      case SYMBOL:
        if (value instanceof Enum) {
          Enum enumVal = (Enum) value;
          writer.keyword(enumVal.toString());
        } else {
          writer.keyword(String.valueOf(value));
        }
        break;
      default:
        writer.literal(value.toString());
    }
  }
 public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
   final SqlWriter.Frame frame = writer.startFunCall(getName());
   call.operand(0).unparse(writer, leftPrec, rightPrec);
   writer.sep("FROM");
   call.operand(1).unparse(writer, leftPrec, rightPrec);
   writer.endFunCall(frame);
 }
Esempio n. 3
0
 public void unparse(SqlWriter writer, SqlNode[] operands, int leftPrec, int rightPrec) {
   final SqlWriter.Frame frame = writer.startFunCall(getName());
   operands[0].unparse(writer, leftPrec, rightPrec);
   writer.sep("PLACING");
   operands[1].unparse(writer, leftPrec, rightPrec);
   writer.sep("FROM");
   operands[2].unparse(writer, leftPrec, rightPrec);
   if (4 == operands.length) {
     writer.sep("FOR");
     operands[3].unparse(writer, leftPrec, rightPrec);
   }
   writer.endFunCall(frame);
 }
Esempio n. 4
0
 @Override
 public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
   super.unparse(writer, call, leftPrec, rightPrec);
   if (withOrdinality) {
     writer.keyword("WITH ORDINALITY");
   }
 }
Esempio n. 5
0
 void commaList(SqlWriter writer) {
   // The precedence of the comma operator if low but not zero. For
   // instance, this ensures parentheses in
   //    select x, (select * from foo order by z), y from t
   for (SqlNode node : list) {
     writer.sep(",");
     node.unparse(writer, 2, 3);
   }
 }
  public void unparse(SqlWriter writer, SqlNode[] operands, int leftPrec, int rightPrec) {
    final SqlWriter.Frame frame = writer.startList(BetweenFrameType, "", "");
    operands[VALUE_OPERAND].unparse(writer, getLeftPrec(), 0);
    writer.sep(getName());
    operands[SYMFLAG_OPERAND].unparse(writer, 0, 0);

    // If the expression for the lower bound contains a call to an AND
    // operator, we need to wrap the expression in parentheses to prevent
    // the AND from associating with BETWEEN. For example, we should
    // unparse
    //    a BETWEEN b OR (c AND d) OR e AND f
    // as
    //    a BETWEEN (b OR c AND d) OR e) AND f
    // If it were unparsed as
    //    a BETWEEN b OR c AND d OR e AND f
    // then it would be interpreted as
    //    (a BETWEEN (b OR c) AND d) OR (e AND f)
    // which would be wrong.
    int lowerPrec = new AndFinder().containsAnd(operands[LOWER_OPERAND]) ? 100 : 0;
    operands[LOWER_OPERAND].unparse(writer, lowerPrec, lowerPrec);
    writer.sep("AND");
    operands[UPPER_OPERAND].unparse(writer, 0, getRightPrec());
    writer.endList(frame);
  }
Esempio n. 7
0
  void andOrList(SqlWriter writer, SqlKind sepKind) {
    SqlBinaryOperator sepOp =
        sepKind == SqlKind.AND ? SqlStdOperatorTable.AND : SqlStdOperatorTable.OR;
    for (int i = 0; i < list.size(); i++) {
      SqlNode node = list.get(i);
      writer.sep(sepKind.name(), false);

      // The precedence pulling on the LHS of a node is the
      // right-precedence of the separator operator, except at the start
      // of the list; similarly for the RHS of a node. If the operator
      // has left precedence 4 and right precedence 5, the precedences
      // in a 3-node list will look as follows:
      //   0 <- node1 -> 4  5 <- node2 -> 4  5 <- node3 -> 0
      int lprec = (i == 0) ? 0 : sepOp.getRightPrec();
      int rprec = (i == (list.size() - 1)) ? 0 : sepOp.getLeftPrec();
      node.unparse(writer, lprec, rprec);
    }
  }
 public void unparse(SqlWriter writer, SqlNode[] operands, int leftPrec, int rightPrec) {
   assert operands.length >= 2;
   final SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.Simple);
   operands[0].unparse(writer, leftPrec, getLeftPrec());
   final boolean needsSpace = true;
   writer.setNeedWhitespace(needsSpace);
   writer.sep("AS");
   writer.setNeedWhitespace(needsSpace);
   operands[1].unparse(writer, getRightPrec(), rightPrec);
   if (operands.length > 2) {
     final SqlWriter.Frame frame1 = writer.startList(SqlWriter.FrameTypeEnum.Simple, "(", ")");
     for (int i = 2; i < operands.length; i++) {
       SqlNode operand = operands[i];
       writer.sep(",", false);
       operand.unparse(writer, 0, 0);
     }
     writer.endList(frame1);
   }
   writer.endList(frame);
 }
Esempio n. 9
0
  /** Appends additional sqlWhere in relationship if any to the end of the join. */
  private void appendAdditionalJoinCondition(
      SqlWriter writer,
      DatabaseType databaseType,
      final String sourceVarName,
      final String targetVarName) {
    if (relationship.getSqlFilter() == null || relationship.getSqlFilter().length() == 0) {
      return;
    }
    Node node = NodeFactory.parseExpression(relationship.getSqlFilter());
    NodeVisitor visit =
        new AbstractNodeVisitor() {
          @Override
          public boolean visitIdentifier(Identifier identifier) {
            String path = identifier.getName();
            int dotPos = path.indexOf('.');
            if (dotPos < 0) {
              throw new EJBQLException(
                  "Invalid sqlWhere in relationship: "
                      + relationship
                      + " - "
                      + relationship.getSqlFilter());
            }
            String entityName = path.substring(0, dotPos);
            if (entityName.equals(relationship.getSourceEntity().getSystemName())
                || entityName.equals(relationship.getSourceEntity().getTableName())) {
              identifier.setName(sourceVarName + path.substring(dotPos));
            } else if (entityName.equals(relationship.getTargetEntity().getSystemName())
                || entityName.equals(relationship.getTargetEntity().getTableName())) {
              identifier.setName(targetVarName + path.substring(dotPos));
            }

            return true;
          }
        };

    node.accept(visit);

    writer.write(" AND ");
    node.toString(writer, databaseType);
  }
Esempio n. 10
0
  @Test
  public void should_use_custom_names_from_property() {
    String sql = sqlWriter.insert(CustomNamePropertyEntity.class);

    assertEquals("INSERT INTO myTableName(customDateColumn) VALUES(?)", Utils.singleLine(sql));
  }
Esempio n. 11
0
  @Test
  public void should_use_field() {
    String sql = sqlWriter.insert(SimpleEntity.class);

    assertEquals("INSERT INTO SimpleEntity(name) VALUES(?)", Utils.singleLine(sql));
  }
Esempio n. 12
0
 public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
   final SqlWriter.Frame frame =
       ((leftPrec > 0) || (rightPrec > 0)) ? writer.startList("(", ")") : writer.startList("", "");
   commaList(writer);
   writer.endList(frame);
 }
Esempio n. 13
0
  @Override
  public void toString(SqlWriter writer, DatabaseType databaseType) {
    if (context == null) {
      writer.write(joinType == TYPE_INNER_JOIN ? "INNER JOIN" : "LEFT OUTER JOIN").write(" ");
      if (fetch) {
        writer.write("FETCH ").write(schemaName);
      } else {
        writer.write(schemaName).write(" ").write(variableName);
      }
    } else { // compiled.
      if (joinType == TYPE_JOIN_TO_OUTER_QUERY) {
        // no need to join, as join is handled in the where clause.
        writer
            .write(relationship.getTargetEntity().getTableName())
            .append(' ')
            .append(variableName);
        return;
      }

      // normal joins - INNER/LEFT OUTER JOINS.
      writer.write(joinType == TYPE_INNER_JOIN ? "INNER JOIN" : "LEFT OUTER JOIN").write(" ");

      RelationshipJoinTable joinTable = relationship.getJoinTable();

      // retrieve the source table info.
      String schemaParent = QueryUtil.getPathParent(schemaName);
      PathInfo sourceTable = context.getPathInfo(schemaParent);
      if (sourceTable == null) {
        throw new EJBQLException("Can not find source reference in the context: " + schemaParent);
      }
      String sourceTableVarName = sourceTable.varName;

      if (joinTable == null) { // without join table.
        // To-one may have filter too
        boolean hasFilter = /*relationship.isToMany() &&*/
            !StringUtilities.isEmpty(relationship.getSqlFilter());

        writer.write(relationship.getTargetEntity().getTableName()).write(" ").write(variableName);
        writer.write(" ON ");
        if (hasFilter) {
          writer.append("(");
        }

        writer
            .append(sourceTableVarName)
            .append('.')
            .append(relationship.getSourceAttribute().getSystemName());

        writer
            .write(" = ")
            .append(variableName)
            .append('.')
            .append(relationship.getTargetAttribute().getSystemName());

        if (hasFilter) {
          String filterCondition = relationship.getSqlFilter();
          filterCondition = filterCondition.replace("{T}", variableName);
          filterCondition = filterCondition.replace("{S}", sourceTableVarName);
          writer.append(" AND ").append(filterCondition).append(")");
        }

        // filter org id and status since such filtering can not be done in WHERE otherwise break
        // LEFT OUTER JOIN (see comments in SelectStatement.toString).
        // added on Apr 13, 2010 by Jack
        if (getJoinType() == Join.TYPE_LEFT_OUTER_JOIN) {
          SelectStatement selectStmt = (SelectStatement) getFirstAncestor(TYPE_SELECT_STATEMENT);
          if (selectStmt == null) {
            throw new RuntimeException("Should not reach here 67584");
          }

          boolean multitenancy =
              !((Entity) relationship.getTargetEntity()).isCoreEntity()
                  && // this line avoid loading metadata being hung.
                  EOThreadLocal.getEOService() != null
                  && EOThreadLocal.getEOService().getMetaDomain() != null
                  && EOThreadLocal.getEOService().getMetaDomain().multitenancy;
          if (multitenancy
              && selectStmt.isOrgSpecific(schemaName, relationship.getTargetEntity())) {
            writer
                .append(" AND ")
                .append(variableName)
                .append('.')
                .append(EOConstants.COL_ORG_ID)
                .append(" = ")
                .append(EOThreadLocal.getOrgId());
          }

          boolean softDeletion =
              EOThreadLocal.getEOService() != null
                  && EOThreadLocal.getEOService().getMetaDomain() != null
                  && !EOThreadLocal.getEOService().getMetaDomain().hardDeletion;

          if (softDeletion) {
            writer
                .append(" AND ")
                .append(variableName)
                .append('.')
                .append(EOConstants.COL_STATUS)
                .append(" <> ")
                .append(EOConstants.STATUS_DELETED);
          }
        }

      } else { // with join table.

        String joinTableVarName =
            context.generateUniqueVarName(joinTable.getTableName().substring(0, 1));

        // join the join table first.
        writer.write(joinTable.getTableName()).append(' ').write(joinTableVarName);
        writer
            .write(" ON ")
            .append(sourceTableVarName)
            .append('.')
            .append(relationship.getSourceAttribute().getSystemName());
        writer
            .write(" = ")
            .append(joinTableVarName)
            .append('.')
            .append(joinTable.getSourceIdColumnName());

        // then the target entity.
        writer.write(" INNER JOIN ");
        writer.write(relationship.getTargetEntity().getTableName()).write(" ").write(variableName);
        writer
            .write(" ON ")
            .append(joinTableVarName)
            .append('.')
            .append(joinTable.getTargetIdColumnName());
        writer
            .write(" = ")
            .append(variableName)
            .append('.')
            .append(relationship.getTargetAttribute().getSystemName());
      }

      // additional sqlWhere in relationship.
      // Commented out by Jack on Mar 29, 2009 - sqlFilter has already been handled in above code.
      // appendAdditionalJoinCondition(writer, databaseType, sourceTableVarName, variableName);
    }
  }
 public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
   writer.print("?");
   writer.setNeedWhitespace(false);
 }
Esempio n. 15
0
 public void unparse(
     SqlWriter writer, SqlOperator operator, SqlCall call, int leftPrec, int rightPrec) {
   assert call.operandCount() == 1;
   call.operand(0).unparse(writer, operator.getLeftPrec(), operator.getRightPrec());
   writer.keyword(operator.getName());
 }