Package org.apache.torque.criteria

Examples of org.apache.torque.criteria.PreparedStatementPart


            throws TorqueException
    {
        if (!(toAddToFromClause instanceof Column))
        {
            // toAddToFromClause is a literal Value
            return new PreparedStatementPart("?", toAddToFromClause);
        }
        Column column = (Column) toAddToFromClause;
        Column resolvedColumn
                = resolveAliasAndAsColumnAndSchema(column, criteria);
        String fullTableName
                = resolvedColumn.getFullTableName();

        if (!StringUtils.equals(
                resolvedColumn.getTableName(),
                column.getTableName()))
        {
            // If the tables have an alias, add an "<xxx> <yyy> statement"
            // <xxx> AS <yyy> causes problems on oracle
            PreparedStatementPart result = new PreparedStatementPart();
            result.getSql()
                    .append(fullTableName)
                    .append(" ")
                    .append(column.getTableName());
            return result;
        }
        Object resolvedAlias = criteria.getAliases().get(
                resolvedColumn.getTableName());
        if (resolvedAlias != null)
        {
            if (resolvedAlias instanceof Criteria)
            {
                Criteria subquery = (Criteria) resolvedAlias;
                Query renderedSubquery = SqlBuilder.buildQuery(subquery);
                PreparedStatementPart result = new PreparedStatementPart();
                result.getSql().append("(")
                        .append(renderedSubquery.toString())
                        .append(") ")
                        .append(resolvedColumn.getTableName());
                result.getPreparedStatementReplacements().addAll(
                        renderedSubquery.getPreparedStatementReplacements());
                return result;
            }
            else
            {
                throw new TorqueException("Table name "
                    + resolvedColumn.getTableName()
                    + " resolved to an unhandleable class "
                    + resolvedAlias.getClass().getName());
            }
        }

        return new PreparedStatementPart(fullTableName);
    }
View Full Code Here


        Column column = (Column) possibleColumn;
        if (column.getTableName() == null)
        {
            return;
        }
        PreparedStatementPart fromClauseExpression = getExpressionForFromClause(
                column,
                criteria);

        UniqueList<FromElement> queryFromClause = query.getFromClause();

        // it is important that this piece of code is executed AFTER
        // the joins are processed
        if (!fromClauseContainsExpression(
            queryFromClause,
            fromClauseExpression))
        {
            FromElement fromElement = new FromElement(
                    fromClauseExpression.getSql().toString(),
                    null,
                    null,
                    fromClauseExpression.getPreparedStatementReplacements());
            queryFromClause.add(fromElement);
        }
    }
View Full Code Here

                WhereClauseExpression whereClausePart,
                boolean ignoreCase,
                Adapter adapter)
            throws TorqueException
    {
        PreparedStatementPart result = new PreparedStatementPart();

        // Handle SqlEnum.Custom
        if (SqlEnum.CUSTOM == whereClausePart.getOperator())
        {
            result.getSql().append(whereClausePart.getRValue());
            return result;
        }

        // Handle SqlEnum.CURRENT_DATE and SqlEnum.CURRENT_TIME
        if (whereClausePart.getRValue() instanceof SqlEnum)
        {
            result.getSql().append(whereClausePart.getLValue())
                .append(whereClausePart.getOperator())
                .append(whereClausePart.getRValue());
            return result;
        }

        // If rValue is an ObjectKey, take the value of that ObjectKey.
        if (whereClausePart.getRValue() instanceof ObjectKey)
        {
            whereClausePart.setRValue(
                    ((ObjectKey) whereClausePart.getRValue()).getValue());
        }

        /*  If rValue is null, check to see if the operator
         *  is an =, <>, or !=.  If so, replace the comparison
         *  with SqlEnum.ISNULL or SqlEnum.ISNOTNULL.
         */
        if (whereClausePart.getRValue() == null)
        {
            if (whereClausePart.getOperator().equals(SqlEnum.EQUAL))
            {
                result.getSql().append(whereClausePart.getLValue())
                        .append(SqlEnum.ISNULL);
                return result;
            }
            if (whereClausePart.getOperator().equals(SqlEnum.NOT_EQUAL)
                || whereClausePart.getOperator().equals(
                        SqlEnum.ALT_NOT_EQUAL))
            {
                result.getSql().append(whereClausePart.getLValue())
                    .append(SqlEnum.ISNOTNULL);
                return result;
            }
        }

        // Handle SqlEnum.ISNULL and SqlEnum.ISNOTNULL
        if (whereClausePart.getOperator().equals(SqlEnum.ISNULL)
            || whereClausePart.getOperator().equals(SqlEnum.ISNOTNULL))
        {
            result.getSql().append(whereClausePart.getLValue())
                    .append(whereClausePart.getOperator());
            return result;
        }

        // handle Subqueries
        if (whereClausePart.getRValue() instanceof Criteria)
        {
            Query subquery = SqlBuilder.buildQuery(
                    (Criteria) whereClausePart.getRValue());
            result.getPreparedStatementReplacements().addAll(
                    subquery.getPreparedStatementReplacements());
            result.getSql().append(whereClausePart.getLValue())
                    .append(whereClausePart.getOperator())
                    .append("(").append(subquery.toString()).append(")");
                return result;
        }
        if (whereClausePart.getRValue()
                instanceof org.apache.torque.util.Criteria)
        {
            Query subquery = SqlBuilder.buildQuery(
                    (org.apache.torque.util.Criteria)
                        whereClausePart.getRValue());
            result.getPreparedStatementReplacements().addAll(
                    subquery.getPreparedStatementReplacements());
            result.getSql().append(whereClausePart.getLValue())
                    .append(whereClausePart.getOperator())
                    .append("(").append(subquery.toString()).append(")");
                return result;
        }

        // handle LIKE and similar
        if (whereClausePart.getOperator().equals(Criteria.LIKE)
            || whereClausePart.getOperator().equals(Criteria.NOT_LIKE)
            || whereClausePart.getOperator().equals(Criteria.ILIKE)
            || whereClausePart.getOperator().equals(Criteria.NOT_ILIKE))
        {
            return buildPsLike(whereClausePart, ignoreCase, adapter);
        }

        // handle IN and similar
        if (whereClausePart.getOperator().equals(Criteria.IN)
                || whereClausePart.getOperator().equals(Criteria.NOT_IN))
        {
            return buildPsIn(whereClausePart, ignoreCase, adapter);
        }

        // Standard case
        result.getPreparedStatementReplacements().add(
                whereClausePart.getRValue());
        if (ignoreCase
            && whereClausePart.getRValue() instanceof String)
        {
            result.getSql().append(
                    adapter.ignoreCase((String) whereClausePart.getLValue()))
                .append(whereClausePart.getOperator())
                .append(adapter.ignoreCase("?"));
        }
        else
        {
            result.getSql().append(whereClausePart.getLValue())
                .append(whereClausePart.getOperator())
                .append("?");
        }
        return result;
    }
View Full Code Here

                whereClausePart.setLValue(
                        adapter.ignoreCase((String) whereClausePart.getLValue()));
            }
        }

        PreparedStatementPart result = new PreparedStatementPart();
        result.getSql().append(whereClausePart.getLValue());

        if (replaceWithEquals)
        {
            if (whereClausePart.getOperator().equals(SqlEnum.NOT_LIKE)
                    || whereClausePart.getOperator().equals(SqlEnum.NOT_ILIKE))
            {
                result.getSql().append(SqlEnum.NOT_EQUAL);
            }
            else
            {
                result.getSql().append(SqlEnum.EQUAL);
            }

            // remove escape backslashes from String
            position = 0;
            sb = new StringBuffer();
            while (position < value.length())
            {
                char checkWildcard = value.charAt(position);

                if (checkWildcard == BACKSLASH
                        && position + 1 < value.length())
                {
                    position++;
                    // code below copies escaped character into sb
                    checkWildcard = value.charAt(position);
                }
                sb.append(checkWildcard);
                position++;
            }
            value = sb.toString();
        }
        else
        {
            result.getSql().append(whereClausePart.getOperator());
        }

        String rValueSql = "?";
        // handle ignoreCase if necessary
        if (ignoreCase && (!(adapter.useIlike()) || replaceWithEquals))
        {
            rValueSql = adapter.ignoreCase(rValueSql);
        }
        // handle escape clause if necessary
        if (!replaceWithEquals && adapter.useEscapeClauseForLike())
        {
            rValueSql = rValueSql + SqlEnum.ESCAPE + "'\\'";
        }

        result.getPreparedStatementReplacements().add(value);
        result.getSql().append(rValueSql);
        return result;
    }
View Full Code Here

    static PreparedStatementPart buildPsIn(
            WhereClauseExpression whereClausePart,
            boolean ignoreCase,
            Adapter adapter)
    {
        PreparedStatementPart result = new PreparedStatementPart();

        boolean ignoreCaseApplied = false;
        List<String> inClause = new ArrayList<String>();
        boolean nullContained = false;
        if (whereClausePart.getRValue() instanceof Iterable)
        {
            for (Object listValue : (Iterable<?>) whereClausePart.getRValue())
            {
                if (listValue == null)
                {
                    nullContained = true;
                    continue;
                }
                result.getPreparedStatementReplacements().add(listValue);
                if (ignoreCase && listValue instanceof String)
                {
                    inClause.add(adapter.ignoreCase("?"));
                    ignoreCaseApplied = true;
                }
                else
                {
                    inClause.add("?");
                }
            }
        }
        else if (whereClausePart.getRValue().getClass().isArray())
        {
            for (Object arrayValue : (Object[]) whereClausePart.getRValue())
            {
                if (arrayValue == null)
                {
                    nullContained = true;
                    continue;
                }
                result.getPreparedStatementReplacements().add(arrayValue);
                if (ignoreCase && arrayValue instanceof String)
                {
                    inClause.add(adapter.ignoreCase("?"));
                    ignoreCaseApplied = true;
                }
                else
                {
                    inClause.add("?");
                }
            }
        }
        else
        {
            throw new IllegalArgumentException(
                    "Unknown rValue type "
                    + whereClausePart.getRValue().getClass().getName()
                    + ". rValue must be an instance of "
                    + " Iterable or Array");
        }

        if (nullContained)
        {
            result.getSql().append('(');
        }

        if (ignoreCaseApplied)
        {
            result.getSql().append(
                    adapter.ignoreCase((String) whereClausePart.getLValue()));
        }
        else
        {
            result.getSql().append(whereClausePart.getLValue());
        }

        result.getSql().append(whereClausePart.getOperator())
                .append('(')
                .append(StringUtils.join(inClause.iterator(), ","))
                .append(')');
        if (nullContained)
        {
            if (whereClausePart.getOperator() == SqlEnum.IN)
            {
                result.getSql().append(Criterion.OR)
                    .append(whereClausePart.getLValue()).append(SqlEnum.ISNULL);
            }
            else if (whereClausePart.getOperator() == SqlEnum.NOT_IN)
            {
                result.getSql().append(Criterion.AND)
                    .append(whereClausePart.getLValue()).append(
                            SqlEnum.ISNOTNULL);
            }
            result.getSql().append(')');
        }
        return result;
    }
View Full Code Here

            Object toBuildFrom,
            boolean ignoreCase,
            Adapter adapter)
        throws TorqueException
    {
        PreparedStatementPart result = new PreparedStatementPart();
        if (toBuildFrom instanceof Column)
        {
            Column column = (Column) toBuildFrom;
            if (ignoreCase)
            {
                result.getSql().append(
                        adapter.ignoreCase(column.getSqlExpression()));
            }
            else
            {
                result.getSql().append(column.getSqlExpression());
            }
            return result;
        }

        // plain object

        if (toBuildFrom instanceof Criteria)
        {
            Query subquery = SqlBuilder.buildQuery(
                    (Criteria) toBuildFrom);
            result.getPreparedStatementReplacements().addAll(
                    subquery.getPreparedStatementReplacements());
            result.getSql().append("(").append(subquery.toString()).append(")");
            return result;
        }

        if (toBuildFrom instanceof org.apache.torque.util.Criteria)
        {
            Query subquery = SqlBuilder.buildQuery(
                    (org.apache.torque.util.Criteria) toBuildFrom);
            result.getPreparedStatementReplacements().addAll(
                    subquery.getPreparedStatementReplacements());
            result.getSql().append("(").append(subquery.toString()).append(")");
            return result;
        }

        if (toBuildFrom.equals(
                SqlEnum.CURRENT_DATE)
                || toBuildFrom.equals(
                        SqlEnum.CURRENT_TIME)
                || toBuildFrom.equals(
                        SqlEnum.CURRENT_TIMESTAMP))
        {
            result.getSql().append(toBuildFrom.toString());
            return result;
        }
        // If rValue is an ObjectKey, take the value of that ObjectKey.
        if (toBuildFrom instanceof ObjectKey)
        {
            toBuildFrom = ((ObjectKey) toBuildFrom).getValue();
        }

        // handle ignoreCase
        if (ignoreCase && toBuildFrom instanceof String)
        {
            result.getSql().append(adapter.ignoreCase("?"));
        }
        else
        {
            result.getSql().append("?");
        }
        result.getPreparedStatementReplacements().add(toBuildFrom);
        return result;
    }
View Full Code Here

            {
                Criterion joinCondition = join.getJoinCondition();

                // get the table names
                // (and the alias names for them if necessary))
                PreparedStatementPart leftExpression;
                if (join.getLeftTable() != null)
                {
                    leftExpression = join.getLeftTable();
                }
                else
                {
                    if (joinCondition.isComposite())
                    {
                        throw new TorqueException(
                                "join condition is composite "
                                + "and there is no leftTable defined "
                                + "in the join. "
                                + "Please define a leftTable in the join");
                    }
                    Object lValue = joinCondition.getLValue();
                    leftExpression = SqlBuilder.getExpressionForFromClause(
                            lValue,
                            criteria);
                }
                PreparedStatementPart rightExpression;
                if (join.getRightTable() != null)
                {
                    rightExpression = join.getRightTable();
                }
                else
                {
                    if (joinCondition.isComposite())
                    {
                        throw new TorqueException(
                                "join condition is composite "
                                + "and there is no rightTable defined "
                                + "in the join. "
                                + "Please define a rightTable in the join");
                    }
                    Object rValue = joinCondition.getRValue();
                    rightExpression = SqlBuilder.getExpressionForFromClause(
                            rValue,
                            criteria);

                }

                // check whether the order of the join must be "reversed"
                // This if the case if the fromClause already contains
                // rightTableName

                if (!SqlBuilder.fromClauseContainsExpression(
                            queryFromClause,
                            rightExpression))
                {
                    if (!SqlBuilder.fromClauseContainsExpression(
                                queryFromClause,
                                leftExpression))
                    {
                        FromElement fromElement = new FromElement(
                            leftExpression.getSql().toString(),
                            null,
                            null,
                            leftExpression.getPreparedStatementReplacements());
                        queryFromClause.add(fromElement);
                    }

                    FromElement fromElement = new FromElement(
                            rightExpression.getSql().toString(),
                            joinType,
                            buildJoinCondition(joinCondition, criteria));
                    queryFromClause.add(fromElement);
                }
                else
                {
                    if (SqlBuilder.fromClauseContainsExpression(
                                queryFromClause,
                                leftExpression))
                    {
                        // We cannot add an explicit join if both tables
                        // are already present in the from clause
                        throw new TorqueException(
                                "Unable to create a" + joinType
                                + "because both expressions "
                                + leftExpression.getSql()
                                + " and " + rightExpression.getSql()
                                + " are already in use. "
                                + "Try to create an(other) alias.");
                    }
                    // now add the join in reverse order
                    // rightTableName must not be added
View Full Code Here

    private static PreparedStatementPart buildJoinCondition(
                Criterion joinCondition,
                CriteriaInterface<?> criteria)
            throws TorqueException
    {
        PreparedStatementPart joinPart = new PreparedStatementPart();
        appendJoinCondition(joinCondition, criteria, joinPart);
        return joinPart;
    }
View Full Code Here

                firstPart = false;
            }
            joinPart.getSql().append(')');
            return;
        }
        PreparedStatementPart joinConditionStatementPart
                = SqlBuilder.processCriterion(joinCondition, criteria);
        joinPart.append(joinConditionStatementPart);
    }
View Full Code Here

TOP

Related Classes of org.apache.torque.criteria.PreparedStatementPart

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.