Пример #1
0
 /**
  * Walk the tree looking for METHOD_DEFs. Any simple METHOD_DEF (one without TYPE_PARAMETERS)
  * lacking an access specifier is given public access.
  *
  * @param node
  */
 private void makeSimpleMethodsPublic(final AST node) {
   if (node.getType() == PdeTokenTypes.METHOD_DEF) {
     final AST mods = node.getFirstChild();
     final AST oldFirstMod = mods.getFirstChild();
     for (AST mod = oldFirstMod; mod != null; mod = mod.getNextSibling()) {
       final int t = mod.getType();
       if (t == PdeTokenTypes.LITERAL_private
           || t == PdeTokenTypes.LITERAL_protected
           || t == PdeTokenTypes.LITERAL_public) {
         return;
       }
     }
     if (mods.getNextSibling().getType() == PdeTokenTypes.TYPE_PARAMETERS) {
       return;
     }
     final CommonHiddenStreamToken publicToken =
         new CommonHiddenStreamToken(PdeTokenTypes.LITERAL_public, "public") {
           {
             setHiddenAfter(new CommonHiddenStreamToken(PdeTokenTypes.WS, " "));
           }
         };
     final AST publicNode = new CommonASTWithHiddenTokens(publicToken);
     publicNode.setNextSibling(oldFirstMod);
     mods.setFirstChild(publicNode);
   } else {
     for (AST kid = node.getFirstChild(); kid != null; kid = kid.getNextSibling())
       makeSimpleMethodsPublic(kid);
   }
 }
 private void createSelectClauseFromFromClause(QueryNode qn) throws SemanticException {
   AST select = astFactory.create(SELECT_CLAUSE, "{derived select clause}");
   AST sibling = qn.getFromClause();
   qn.setFirstChild(select);
   select.setNextSibling(sibling);
   selectClause = (SelectClause) select;
   selectClause.initializeDerivedSelectClause(currentFromClause);
   if (log.isDebugEnabled()) {
     log.debug("Derived SELECT clause created.");
   }
 }
Пример #3
0
 private AST processIsEmpty(AST node, boolean negated) {
   node.setNextSibling(null);
   // NOTE: Because we're using ASTUtil.createParent(), the tree must be created from the bottom
   // up.
   // IS EMPTY x => (EXISTS (QUERY (SELECT_FROM (FROM x) ) ) )
   AST ast = createSubquery(node);
   ast = ASTUtil.createParent(astFactory, EXISTS, "exists", ast);
   // Add NOT if it's negated.
   if (!negated) {
     ast = ASTUtil.createParent(astFactory, NOT, "not", ast);
   }
   return ast;
 }
  protected void prepareVersioned(AST updateNode, AST versioned) throws SemanticException {
    UpdateStatement updateStatement = (UpdateStatement) updateNode;
    FromClause fromClause = updateStatement.getFromClause();
    if (versioned != null) {
      // Make sure that the persister is versioned
      Queryable persister = fromClause.getFromElement().getQueryable();
      if (!persister.isVersioned()) {
        throw new SemanticException(
            "increment option specified for update of non-versioned entity");
      }

      VersionType versionType = persister.getVersionType();
      if (versionType instanceof UserVersionType) {
        throw new SemanticException(
            "user-defined version types not supported for increment option");
      }

      AST eq = getASTFactory().create(HqlSqlTokenTypes.EQ, "=");
      AST versionPropertyNode = generateVersionPropertyNode(persister);

      eq.setFirstChild(versionPropertyNode);

      AST versionIncrementNode = null;
      if (Date.class.isAssignableFrom(versionType.getReturnedClass())) {
        versionIncrementNode = getASTFactory().create(HqlSqlTokenTypes.PARAM, "?");
        ParameterSpecification paramSpec = new VersionTypeSeedParameterSpecification(versionType);
        ((ParameterNode) versionIncrementNode).setHqlParameterSpecification(paramSpec);
        parameters.add(0, paramSpec);
      } else {
        // Not possible to simply re-use the versionPropertyNode here as it causes
        // OOM errors due to circularity :(
        versionIncrementNode = getASTFactory().create(HqlSqlTokenTypes.PLUS, "+");
        versionIncrementNode.setFirstChild(generateVersionPropertyNode(persister));
        versionIncrementNode.addChild(getASTFactory().create(HqlSqlTokenTypes.IDENT, "1"));
      }

      eq.addChild(versionIncrementNode);

      evaluateAssignment(eq, persister, 0);

      AST setClause = updateStatement.getSetClause();
      AST currentFirstSetElement = setClause.getFirstChild();
      setClause.setFirstChild(eq);
      eq.setNextSibling(currentFirstSetElement);
    }
  }
Пример #5
0
  public final OrderByClause getOrderByClause() {
    if (orderByClause == null) {
      orderByClause = locateOrderByClause();

      // if there is no order by, make one
      if (orderByClause == null) {
        log.debug("getOrderByClause() : Creating a new ORDER BY clause");
        orderByClause =
            (OrderByClause)
                ASTUtil.create(getWalker().getASTFactory(), SqlTokenTypes.ORDER, "ORDER");

        // Find the WHERE; if there is no WHERE, find the FROM...
        AST prevSibling = ASTUtil.findTypeInChildren(this, SqlTokenTypes.WHERE);
        if (prevSibling == null) {
          prevSibling = ASTUtil.findTypeInChildren(this, SqlTokenTypes.FROM);
        }

        // Now, inject the newly built ORDER BY into the tree
        orderByClause.setNextSibling(prevSibling.getNextSibling());
        prevSibling.setNextSibling(orderByClause);
      }
    }
    return orderByClause;
  }
  protected void postProcessInsert(AST insert) throws SemanticException, QueryException {
    InsertStatement insertStatement = (InsertStatement) insert;
    insertStatement.validate();

    SelectClause selectClause = insertStatement.getSelectClause();
    Queryable persister = insertStatement.getIntoClause().getQueryable();

    if (!insertStatement.getIntoClause().isExplicitIdInsertion()) {
      // We need to generate ids as part of this bulk insert.
      //
      // Note that this is only supported for sequence-style generators and
      // post-insert-style generators; basically, only in-db generators
      IdentifierGenerator generator = persister.getIdentifierGenerator();
      if (!supportsIdGenWithBulkInsertion(generator)) {
        throw new QueryException(
            "can only generate ids as part of bulk insert with either sequence or post-insert style generators");
      }

      AST idSelectExprNode = null;

      if (SequenceGenerator.class.isAssignableFrom(generator.getClass())) {
        String seqName = (String) ((SequenceGenerator) generator).generatorKey();
        String nextval =
            sessionFactoryHelper.getFactory().getDialect().getSelectSequenceNextValString(seqName);
        idSelectExprNode = getASTFactory().create(HqlSqlTokenTypes.SQL_TOKEN, nextval);
      } else {
        // Don't need this, because we should never ever be selecting no columns in an insert ...
        // select...
        // and because it causes a bug on DB2
        /*String idInsertString = sessionFactoryHelper.getFactory().getDialect().getIdentityInsertString();
        if ( idInsertString != null ) {
        	idSelectExprNode = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, idInsertString );
        }*/
      }

      if (idSelectExprNode != null) {
        AST currentFirstSelectExprNode = selectClause.getFirstChild();
        selectClause.setFirstChild(idSelectExprNode);
        idSelectExprNode.setNextSibling(currentFirstSelectExprNode);

        insertStatement.getIntoClause().prependIdColumnSpec();
      }
    }

    final boolean includeVersionProperty =
        persister.isVersioned()
            && !insertStatement.getIntoClause().isExplicitVersionInsertion()
            && persister.isVersionPropertyInsertable();
    if (includeVersionProperty) {
      // We need to seed the version value as part of this bulk insert
      VersionType versionType = persister.getVersionType();
      AST versionValueNode = null;

      if (sessionFactoryHelper.getFactory().getDialect().supportsParametersInInsertSelect()) {
        versionValueNode = getASTFactory().create(HqlSqlTokenTypes.PARAM, "?");
        ParameterSpecification paramSpec = new VersionTypeSeedParameterSpecification(versionType);
        ((ParameterNode) versionValueNode).setHqlParameterSpecification(paramSpec);
        parameters.add(0, paramSpec);
      } else {
        if (isIntegral(versionType)) {
          try {
            Object seedValue = versionType.seed(null);
            versionValueNode =
                getASTFactory().create(HqlSqlTokenTypes.SQL_TOKEN, seedValue.toString());
          } catch (Throwable t) {
            throw new QueryException(
                "could not determine seed value for version on bulk insert [" + versionType + "]");
          }
        } else if (isDatabaseGeneratedTimestamp(versionType)) {
          String functionName =
              sessionFactoryHelper.getFactory().getDialect().getCurrentTimestampSQLFunctionName();
          versionValueNode = getASTFactory().create(HqlSqlTokenTypes.SQL_TOKEN, functionName);
        } else {
          throw new QueryException(
              "cannot handle version type ["
                  + versionType
                  + "] on bulk inserts with dialects not supporting parameters in insert-select statements");
        }
      }

      AST currentFirstSelectExprNode = selectClause.getFirstChild();
      selectClause.setFirstChild(versionValueNode);
      versionValueNode.setNextSibling(currentFirstSelectExprNode);

      insertStatement.getIntoClause().prependVersionColumnSpec();
    }

    if (insertStatement.getIntoClause().isDiscriminated()) {
      String sqlValue = insertStatement.getIntoClause().getQueryable().getDiscriminatorSQLValue();
      AST discrimValue = getASTFactory().create(HqlSqlTokenTypes.SQL_TOKEN, sqlValue);
      insertStatement.getSelectClause().addChild(discrimValue);
    }
  }
Пример #7
0
 private AST createIsNullParent(AST node, boolean negated) {
   node.setNextSibling(null);
   int type = negated ? IS_NOT_NULL : IS_NULL;
   String text = negated ? "is not null" : "is null";
   return ASTUtil.createParent(astFactory, type, text, node);
 }