All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.query.JDOQLQueryHelper Maven / Gradle / Ivy

Go to download

DataNucleus Core provides the primary components of a heterogenous Java persistence solution. It supports persistence API's being layered on top of the core functionality.

There is a newer version: 6.0.7
Show newest version
/**********************************************************************
Copyright (c) 2008 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
    ...
**********************************************************************/
package org.datanucleus.query;

import java.util.Iterator;
import java.util.List;

import org.datanucleus.query.expression.DyadicExpression;
import org.datanucleus.query.expression.Expression;
import org.datanucleus.query.expression.InvokeExpression;
import org.datanucleus.query.expression.Literal;
import org.datanucleus.query.expression.ParameterExpression;
import org.datanucleus.query.expression.PrimaryExpression;
import org.datanucleus.query.expression.VariableExpression;

/**
 * JDOQL query helper class providing key information about the language etc.
 */
public class JDOQLQueryHelper
{
    /** Keywords used in single-string JDOQL. Uppercase variants specified here, but we allow the lowercase form. */
    static final String[] SINGLE_STRING_KEYWORDS = {
            "SELECT", "UNIQUE", "INTO", "FROM", "EXCLUDE", "SUBCLASSES", "WHERE",
            "VARIABLES", "PARAMETERS", "GROUP", "ORDER", "BY", "RANGE"
            };
    /** Keywords in lowercase (we avoid calling toLowerCase() multiple times, which is expensive operation) **/
    static final String[] SINGLE_STRING_KEYWORDS_LOWERCASE = {
            "select", "unique", "into", "from", "exclude", "subclasses", "where",
            "variables", "parameters", "group", "order", "by", "range"
            };

    /**
     * Convenience method returning if the supplied name is a keyword for this query language.
     * @param name Name to check
     * @return Whether it is a keyword
     */
    public static boolean isKeyword(String name)
    {
        for (int i=0;i ");
            }
            else if (dyExpr.getOperator() == Expression.OP_LT)
            {
                str.append(" < ");
            }
            else if (dyExpr.getOperator() == Expression.OP_GTEQ)
            {
                str.append(" >= ");
            }
            else if (dyExpr.getOperator() == Expression.OP_LTEQ)
            {
                str.append(" <= ");
            }
            else if (dyExpr.getOperator() == Expression.OP_NOTEQ)
            {
                str.append(" != ");
            }
            else if (dyExpr.getOperator() == Expression.OP_DISTINCT)
            {
                // Processed above
            }
            else
            {
                // TODO Support other operators
                throw new UnsupportedOperationException("Dont currently support operator " + dyExpr.getOperator() + " in JDOQL conversion");
            }

            if (right != null)
            {
                str.append(JDOQLQueryHelper.getJDOQLForExpression(right));
            }
            str.append(")");
            return str.toString();
        }
        else if (expr instanceof PrimaryExpression)
        {
            PrimaryExpression primExpr = (PrimaryExpression)expr;
            if (primExpr.getLeft() != null)
            {
                return JDOQLQueryHelper.getJDOQLForExpression(primExpr.getLeft()) + "." + primExpr.getId();
            }
            return primExpr.getId();
        }
        else if (expr instanceof ParameterExpression)
        {
            ParameterExpression paramExpr = (ParameterExpression)expr;
            if (paramExpr.getId() != null)
            {
                return ":" + paramExpr.getId();
            }
            return "?" + paramExpr.getPosition();
        }
        else if (expr instanceof VariableExpression)
        {
            VariableExpression varExpr = (VariableExpression)expr;
            return varExpr.getId();
        }
        else if (expr instanceof InvokeExpression)
        {
            InvokeExpression invExpr = (InvokeExpression)expr;
            StringBuilder str = new StringBuilder();
            if (invExpr.getLeft() != null)
            {
                str.append(JDOQLQueryHelper.getJDOQLForExpression(invExpr.getLeft())).append(".");
            }
            str.append(invExpr.getOperation());
            str.append("(");
            List args = invExpr.getArguments();
            if (args != null)
            {
                Iterator iter = args.iterator();
                while (iter.hasNext())
                {
                    str.append(JDOQLQueryHelper.getJDOQLForExpression(iter.next()));
                    if (iter.hasNext())
                    {
                        str.append(",");
                    }
                }
            }
            str.append(")");
            return str.toString();
        }
        else if (expr instanceof Literal)
        {
            Literal litExpr = (Literal)expr;
            Object value = litExpr.getLiteral();
            if (value instanceof String || value instanceof Character)
            {
                return "'" + value.toString() + "'";
            }
            else if (value instanceof Boolean)
            {
                return ((Boolean)value ? "TRUE" : "FALSE");
            }
            else
            {
                if (litExpr.getLiteral() == null)
                {
                    return "null";
                }
                return litExpr.getLiteral().toString();
            }
        }
        else if (expr instanceof VariableExpression)
        {
            VariableExpression varExpr = (VariableExpression)expr;
            return varExpr.getId();
        }
        else
        {
            throw new UnsupportedOperationException("Dont currently support " + expr.getClass().getName() + " in JDOQLHelper");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy