Package org.jpox.store.mapped.query

Examples of org.jpox.store.mapped.query.StatementText


        return new BooleanExpression(new CharacterExpression(qs, mapping, te), OP_IN, expr);
    }
   
    public StatementText toStatementText(int mode)
    {
        StatementText st = super.toStatementText(mode);
        if( mode == ScalarExpression.FILTER )
        {
            st.append(booleanCondition,mode);
        }
        return st;
    }
View Full Code Here


        }
    }

    public StatementText toStatementText(int mode)
    {
        StatementText st = super.toStatementText(mode);
        if (mode == ScalarExpression.FILTER)
        {
            st.append(booleanCondition,mode);
        }
        return st;
    }
View Full Code Here

     */
    public PreparedStatement getStatementForQuery(QueryExpression qs, ObjectManager om, ManagedConnection conn,
            boolean updateLock, String resultSetType, String resultSetConcurrency)
    throws SQLException
    {
        StatementText text = qs.toStatementText(updateLock);
        SQLController sqlControl = getSQLController();

        // Generate the statement using the statement text
        PreparedStatement ps = sqlControl.getStatementForQuery(conn, text.toString(), resultSetType, resultSetConcurrency);

        boolean done = false;
        try
        {
            // Apply any parameter values for the statement
            text.setParameters(om, ps);
            done = true;
        }
        finally
        {
            if (!done)
View Full Code Here

     * query statement.
     * @return The ordering statement
     */
    protected StatementText generateOrderingStatement()
    {
        StatementText orderByStmt = null;
        if (orderingExpressions != null && orderingExpressions.length > 0)
        {
            orderByStmt = new StatementText();
            boolean needsSelect = ((RDBMSAdapter)storeMgr.getDatastoreAdapter()).includeOrderByColumnsInSelect();
            for (int i=0; i<orderingExpressions.length; ++i)
            {
                String orderExpr = "JPOXORDER" + i;
                if (i > 0)
                {
                    orderByStmt.append(',');
                }

                if (needsSelect && !hasAggregateExpression)
                {
                    // Order by the "JPOXORDER?" if we need them to be selected and it isn't an aggregate
                    orderByStmt.append(orderExpr);
                }
                else
                {
                    // Order by the "THIS.COLUMN" otherwise
                    orderByStmt.append(orderingExpressions[i].toStatementText(ScalarExpression.PROJECTION).toStatementString(ScalarExpression.PROJECTION));
                }

                if (orderingDirections[i])
                {
                    orderByStmt.append(" DESC");
                }
            }
        }
        return orderByStmt;
    }
View Full Code Here

     * </pre>
     * @return The StatementText for the DELETE
     **/
    public StatementText toDeleteStatementText()
    {
        StatementText stmtText = new StatementText("DELETE FROM ");
        stmtText.append(mainTableExpr.toString());

        // TODO This only works where the base query statement has no joins

        if (whereExpr != null)
        {
            stmtText.append(" WHERE ").append(
                whereExpr.toStatementText(ScalarExpression.FILTER), ScalarExpression.FILTER);
        }

        return stmtText;
    }
View Full Code Here

     * </pre>
     * @return The StatementText for the UPDATE
     */
    public StatementText toUpdateStatementText()
    {
        StatementText stmtText = new StatementText("UPDATE ");
        stmtText.append(mainTableExpr.toString());

        stmtText.append(" SET ");
        if (updateExprs != null && updateExprs.length > 0)
        {
            for (int i=0;i<updateExprs.length;i++)
            {
                if (i != 0)
                {
                    stmtText.append(",");
                }
                stmtText.append(
                    updateExprs[i].toStatementText(ScalarExpression.PROJECTION), ScalarExpression.PROJECTION);
            }
        }

        if (whereExpr != null)
        {
            stmtText.append(" WHERE ").append(
                whereExpr.toStatementText(ScalarExpression.FILTER), ScalarExpression.FILTER);
        }

        return stmtText;
    }
View Full Code Here

        {
            // Make sure we have any necessary ordering columns in the SELECT (where required)
            addOrderingColumnsToSelect();

            final RDBMSAdapter rdbmsAdapter = (RDBMSAdapter)storeMgr.getDatastoreAdapter();
            StatementText stmtText = null;

      stmtText = new StatementText("SELECT ");

            if (rangeOffset > -1 || rangeCount > -1)
            {
                if (rdbmsAdapter.getRangeByRowNumberColumn().length()>0)
                {
                    // When doing range queries using ROWNUM, we must wrap the original query
                    // in an outer query that does selection based on an aliased ROWNUM column
                    // of the inner (original) query (CORE-2721)
                    //
                    // see also http://72.14.207.104/search?q=cache:IZZXCUsqdIIJ:www.arrowsent.com/oratip/tip41.htm+oracle+rownum+range&hl=en&ct=clnk&cd=1
                    // SELECT the ROWNUM column and alias it in what will be the inner query
                    stmtText.append(rdbmsAdapter.getRangeByRowNumberColumn() + " rn, ");
                }
                else
                {
                    // Add a LIMIT clause to SELECT if it is supported
                    stmtText.append(rdbmsAdapter.getRangeByLimitSelectClause(rangeOffset, rangeCount));
                }
            }

            boolean usingDistinct = false;
            if (!isExistsSubQuery)
            {
              if (distinctResults)
              {
                  stmtText.append("DISTINCT ");
                    usingDistinct = true;
              }

              Iterator iterator = selected.iterator();
              while (iterator.hasNext())
              {
                    Object selectExpr = iterator.next();
                  stmtText.append(selectExpr.toString());
                  if (iterator.hasNext())
                  {
                      stmtText.append(',');
                  }
              }
                if ((rangeOffset > -1 || rangeCount > -1) && rdbmsAdapter.getRangeByRowNumberColumn().length() > 0)
                {
                    // Add a ROW NUMBER column if supported as the means of handling ranges by the RDBMS
                    stmtText.append(',').append(rdbmsAdapter.getRangeByRowNumberColumn());
                }
            }
            else
            {
                JavaTypeMapping m = rdbmsAdapter.getMapping(Integer.class, storeMgr);
                stmtText.append(m.newLiteral(this, new Integer("1")).toStatementText(ScalarExpression.PROJECTION).toStatementString(ScalarExpression.PROJECTION));
            }

            /*
             * 1. Push joins down
             * 2. try to make sure joins refer to previous table in stack
             *
             *  A
             *  JOIN B = A
             *  JOIN C = C
             *  JOIN D = D
             */
            //SORT JOINS
            List sorted = sortJoins(joins);
            final Join[] sortedJoins = (Join[]) sorted.toArray(new Join[sorted.size()]);

            // FROM condition(s)
            stmtText.append(" FROM ");

            List crossJoinss = new ArrayList();
            crossJoinss.add(mainTableExpr);
            crossJoinss.addAll(crossJoins);
            if (crossJoinss.size()==1)
            {
                stmtText.append(crossJoinss.get(0).toString());
                if (lock && rdbmsAdapter.getSelectWithLockOption() != null && rdbmsAdapter.getPlaceWithOptionAfterFromClause())
                {
                    // Add any locking of the rows for datastores that require WITH option to be placed after the FROM clause
                    stmtText.append(" WITH " + rdbmsAdapter.getSelectWithLockOption());
                }
                crossJoinss.remove(0);
            }
           
            for (int i=0; i<sortedJoins.length; i++)
            {
                final Join join = sortedJoins[i];
                for (int j=crossJoinss.size()-1; j>=0; j--)
                {
                    if ((sortedJoins[i].expr1.getLogicSetExpression().equals(crossJoinss.get(j)) ||
                         sortedJoins[i].expr2.getLogicSetExpression().equals(crossJoinss.get(j))))
                    {
                        if (i > 0)
                        {
                            stmtText.append(rdbmsAdapter.cartersianProduct((LogicSetExpression)crossJoinss.get(j)));
                        }
                        else
                        {
                            stmtText.append(crossJoinss.get(j).toString());
                        }
                        if (lock && rdbmsAdapter.getSelectWithLockOption() != null && rdbmsAdapter.getPlaceWithOptionAfterFromClause())
                        {
                            // Add any locking of the rows for datastores that require WITH option to be placed after the FROM clause
                            stmtText.append(" WITH " + rdbmsAdapter.getSelectWithLockOption());
                        }
                        crossJoinss.remove(j);
                        break;
                    }
                }
                stmtText.append(stmtJoinsSeparator).append(join.toString(rdbmsAdapter, lock));
            }
            crossJoinss.remove(null);
            for (int i=0; i<crossJoinss.size(); i++)
            {
                if (sortedJoins.length > 0 || i > 0)
                {
                    stmtText.append(rdbmsAdapter.cartersianProduct((LogicSetExpression)crossJoinss.get(i)));
                }
                else
                {
                    stmtText.append(crossJoinss.get(i).toString());
                }
                if (lock && rdbmsAdapter.getSelectWithLockOption() != null && rdbmsAdapter.getPlaceWithOptionAfterFromClause())
                {
                    // Add any locking of the rows for datastores that require WITH option to be placed after the FROM clause
                    stmtText.append(" WITH " + rdbmsAdapter.getSelectWithLockOption());
                }
            }

            // WHERE condition(s)
            if (whereExpr != null)
            {
                stmtText.append(" WHERE ").append(whereExpr.toStatementText(ScalarExpression.FILTER),ScalarExpression.FILTER);
            }

            // GROUP BY clause(s)
            if (groupingExpressions != null)
            {
                List groupBy = new ArrayList();
                for (int i=0;i<groupingExpressions.size();i++)
                {
                    // Add on the user-specified groupings
                    String exprText = ((ScalarExpression)groupingExpressions.get(i)).toStatementText(ScalarExpression.PROJECTION).toString();
                    if (!groupBy.contains(exprText))
                    {
                        groupBy.add(exprText);
                    }
                }
                if (groupBy.size() > 0 && hasAggregateExpression)
                {
                    stmtText.append(" GROUP BY ");
                    boolean first = true;
                    for (int i=0; i<groupBy.size(); i++)
                    {
                        if (!first)
                        {
                            stmtText.append(',');
                        }
                        stmtText.append((String)groupBy.get(i));
                        first = false;
                    }
                }
            }

            // HAVING clause
            if (havingExpr != null)
            {
                stmtText.append(" HAVING ").append(havingExpr.toStatementText(ScalarExpression.FILTER),ScalarExpression.FILTER);
            }

            // UNION to other query statements
            Iterator iterator = union.iterator();
            while (iterator.hasNext())
            {
                if (!rdbmsAdapter.supportsUnionSyntax())
                {
                    throw new JPOXException(LOCALISER.msg("052504", "UNION")).setFatal();
                }

                if (rdbmsAdapter.useUnionAll())
                {
                    stmtText.append(" UNION ALL ");
                }
                else
                {
                    stmtText.append(" UNION ");
                }
                QueryStatement qs = ((QueryStatement)iterator.next());
                //do not use for update in other union queries. is this correct for all databases?
                stmtText.append(qs.toStatementText(false),ScalarExpression.FILTER);
            }

            // ORDER clause
            if (!isExistsSubQuery)
            {
                if (orderingExpressions != null && orderingExpressions.length > 0)
                {
                    StatementText orderByStmt = generateOrderingStatement();
                  stmtText.append(" ORDER BY ").append(orderByStmt,ScalarExpression.PROJECTION);
              }
            }

            if (rangeOffset > -1 || rangeCount > -1)
            {
                // Add a LIMIT clause to WHERE if it is supported
                stmtText.append(rdbmsAdapter.getRangeByLimitWhereClause(rangeOffset, rangeCount));
            }

            // Add any required locking based on the RDBMS capability
            if (lock && rdbmsAdapter.supportsLockWithSelectForUpdate())
            {
                if (usingDistinct && !rdbmsAdapter.supportsDistinctWithSelectForUpdate())
                {
                    JPOXLogger.QUERY.warn(LOCALISER.msg("052502"));
                }
                else
                {
                    stmtText.append(" FOR UPDATE");
                }
            }
           
            if ((rangeOffset > -1 || rangeCount > -1) && rdbmsAdapter.getRangeByRowNumberColumn().length() > 0)
            {
                // range query using ROWNUM (CORE-2721): wrap in outer query,
                // which must select all columns of the inner query,
                // except for the ROWNUM column (which is not contained in
                // selected list)
                StatementText innerQuery = stmtText;
                stmtText = new StatementText("SELECT ");
                iterator = selected.iterator();
                while (iterator.hasNext())
                {
                    Object selectExpr = iterator.next();
                    stmtText.append("subq.");
View Full Code Here

           
            SQLController sqlControl = storeMgr.getSQLController();

            try
            {
                StatementText stmtText = null;
                if (query.getType() == Query.SELECT)
                {
                    stmtText = queryStmt.toStatementText(useUpdateLock);
                }
                else if (query.getType() == Query.BULK_UPDATE)
                {
                    stmtText = queryStmt.toUpdateStatementText();
                    throw new JPOXException("JPOX doesnt currently support bulk update statements");
                }
                else if (query.getType() == Query.BULK_DELETE)
                {
                    // TODO Distinguish between update and delete
                    stmtText = queryStmt.toDeleteStatementText();
                    throw new JPOXException("JPOX doesnt currently support bulk delete statements");
                }

                PreparedStatement ps = getStatement(mconn, stmtText);
                try
                {
                    // Apply timeouts, result set constraints etc
                    prepareStatementForExecution(ps);

                    // Add a limit on the number of rows to include the maximum we may need
                    long toExclNo = query.getRangeToExcl();
                    if (toExclNo != 0 && toExclNo != Long.MAX_VALUE)
                    {
                        if (toExclNo > Integer.MAX_VALUE)
                        {
                            // setMaxRows takes an int as input so limit to the correct range
                            ps.setMaxRows(Integer.MAX_VALUE);
                        }
                        else
                        {
                            ps.setMaxRows((int)toExclNo);
                        }
                    }

                    if (query.getType() != Query.SELECT)
                    {
                        JPOXLogger.JDO.debug(">> SQLEvaluator.evaluate BULK operation SELECT");
                    }
                    if (query.getType() == Query.SELECT)
                    {
                        // SELECT query
                        ResultSet rs = sqlControl.executeStatementQuery(mconn, stmtText.toString(), ps);
                        try
                        {
                            // Check the type of result set needed
                            if (getResultSetType().equals("scroll-insensitive") ||
                                getResultSetType().equals("scroll-sensitive"))
                            {
                                qr = new ScrollableQueryResult(queryStmt, query, rof, rs,
                                    distinct ? null : candidateCollection);
                            }
                            else
                            {
                                qr = new ForwardQueryResult(queryStmt, query, rof, rs,
                                    distinct ? null : candidateCollection);
                            }

                            final QueryResult qr1 = qr;
                            final ManagedConnection mconn1 = mconn;
                            ManagedConnectionResourceListener listener = new ManagedConnectionResourceListener()
                            {
                                public void managedConnectionPreClose(){}
                                public void managedConnectionPostClose(){}
                                public void managedConnectionFlushed()
                                {
                                    // Disconnect the query from this ManagedConnection (read in unread rows etc)
                                    qr1.disconnect();
                                }
                                public void resourcePostClose()
                                {
                                    mconn1.removeListener(this);
                                }
                            };
                            mconn.addListener(listener);
                            ((AbstractRDBMSQueryResult)qr).addConnectionListener(listener);
                        }
                        finally
                        {
                            if (qr == null)
                            {
                                rs.close();
                            }
                        }
                    }
                    else
                    {
                        // UPDATE/DELETE query
                        int[] rcs = sqlControl.executeStatementUpdate(mconn, stmtText.toString(), ps, true);
                        JPOXLogger.JDO.info(">> Update statement returned " + rcs[0]);
                        // TODO Return the number of affected records
                    }
                }
                finally
View Full Code Here

     * Convenience method to generate the ordering statement to add to the overall query statement.
     * @return The ordering statement
     */
    protected StatementText generateOrderingStatement()
    {
        StatementText orderByStmt = null;
        if (orderingExpressions != null && orderingExpressions.length > 0)
        {
            orderByStmt = new StatementText();
            boolean needsSelect = ((RDBMSAdapter)storeMgr.getDatastoreAdapter()).includeOrderByColumnsInSelect();
            for (int i=0; i<orderingExpressions.length; ++i)
            {
                String orderExpr = "JPOXORDER" + i;
                if (i > 0)
                {
                    orderByStmt.append(',');
                }

                if (needsSelect && !hasAggregateExpression)
                {
                    // Order by the "JPOXORDER?" if we need them to be selected and it isn't an aggregate
                    if (orderingExpressions[i] instanceof CharacterExpression && !nlsSortOrder.equals("BINARY"))
                    {
                        orderByStmt.append("NLSSORT(").append(orderExpr).append(", 'NLS_SORT = ").append(nlsSortOrder).append("')");
                    }
                    else
                    {
                        orderByStmt.append(orderExpr);
                    }

                }
                else
                {
                    // Order by the "THIS.COLUMN" otherwise
                    if (orderingExpressions[i] instanceof CharacterExpression && !nlsSortOrder.equals("BINARY"))
                    {
                        orderByStmt.append("NLSSORT(").append(
                            orderingExpressions[i].toStatementText(ScalarExpression.PROJECTION).toStatementString(
                                ScalarExpression.PROJECTION)).append(", 'NLS_SORT = ").append(nlsSortOrder).append("')");
                    }
                    else
                    {
                        orderByStmt.append(orderingExpressions[i].toStatementText(ScalarExpression.PROJECTION).toStatementString(ScalarExpression.PROJECTION));
                    }
                }

                if (orderingDirections[i])
                {
                    orderByStmt.append(" DESC");
                }
            }
        }
        return orderByStmt;
    }
View Full Code Here

     * Convenience method to generate the ordering statement to add to the overall query statement.
     * @return The ordering statement
     */
    protected StatementText generateOrderingStatement()
    {
        StatementText orderByStmt = null;
        if (orderingExpressions != null && orderingExpressions.length > 0)
        {
            orderByStmt = new StatementText();
            boolean needsSelect = ((RDBMSAdapter)storeMgr.getDatastoreAdapter()).includeOrderByColumnsInSelect();
            for (int i=0; i<orderingExpressions.length; ++i)
            {
                String orderExpr = "JPOXORDER" + i;
                if (i > 0)
                {
                    orderByStmt.append(',');
                }

                if (needsSelect && !hasAggregateExpression)
                {
                    // Order by the "JPOXORDER?" if we need them to be selected and it isn't an aggregate
                    if (orderingExpressions[i] instanceof CharacterExpression && !nlsSortOrder.equals("BINARY"))
                    {
                        orderByStmt.append("NLSSORT(").append(orderExpr).append(", 'NLS_SORT = ").append(nlsSortOrder).append("')");
                    }
                    else if (orderingExpressions[i] instanceof BooleanExpression &&
                            !(orderingExpressions[i] instanceof BooleanCharColumnExpression))
                    {
                        throw new JPOXException(LOCALISER.msg("052505")).setFatal();
                    }
                    else
                    {
                        orderByStmt.append(orderExpr);
                    }
                }
                else
                {
                    // Order by the "THIS.COLUMN" otherwise
                    if (orderingExpressions[i] instanceof CharacterExpression && !nlsSortOrder.equals("BINARY"))
                    {
                        orderByStmt.append("NLSSORT(").append(
                            orderingExpressions[i].toStatementText(ScalarExpression.PROJECTION).toStatementString(
                                ScalarExpression.PROJECTION)).append(", 'NLS_SORT = ").append(nlsSortOrder).append("')");
                    }
                    else if (orderingExpressions[i] instanceof BooleanExpression &&
                            !(orderingExpressions[i] instanceof BooleanCharColumnExpression))
                    {
                        throw new JPOXException(LOCALISER.msg("052505")).setFatal();
                    }
                    else
                    {
                        orderByStmt.append(orderingExpressions[i].toStatementText(ScalarExpression.PROJECTION).toStatementString(ScalarExpression.PROJECTION));
                    }
                }

                if (orderingDirections[i])
                {
                    orderByStmt.append(" DESC");
                }
            }
        }
        return orderByStmt;
    }
View Full Code Here

TOP

Related Classes of org.jpox.store.mapped.query.StatementText

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.