Package org.datanucleus.query.compiler

Source Code of org.datanucleus.query.compiler.SQLCompiler

/**********************************************************************
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.compiler;

import java.util.Collection;
import java.util.Map;

import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.metadata.MetaDataManager;
import org.datanucleus.query.expression.Expression;
import org.datanucleus.query.symbol.SymbolTable;
import org.datanucleus.util.Imports;

/**
* Implementation of a compiler for SQL (simplified ANSI-like SQL for non-RDBMS).
* Supported SQL is not "SQL" as such, but involves classes/field names.
* Examples :-
* <pre>
* SELECT * FROM Contact
* </pre>
* Will select objects of type Contact, where "Contact" is the name of some class resolved
* by use of the imports and aliases in MetaData.
* In general it will support :-
* <pre>
* SELECT {resultExpr}
*     FROM {fromExpr}
*     WHERE {whereExpr}
*     GROUP BY {groupExpr}
*     HAVING {havingExpr}
*     ORDER BY {orderExpr}
* approximately following JPQL (which is virtually SQL anyway)
* </pre>
*/
public class SQLCompiler extends JavaQueryCompiler
{
    public SQLCompiler(MetaDataManager metaDataManager, ClassLoaderResolver clr,
            String from, Class candidateClass, Collection candidates,
            String filter, Imports imports, String ordering, String result, String grouping, String having,
            String params)
    {
        super(metaDataManager, clr, from, candidateClass, candidates,
            filter, imports, ordering, result, grouping, having, params, null, null);
        this.from = from;
    }

    /**
     * Method to compile the query, and return the compiled results.
     * @param parameters the parameter map of values keyed by param name
     * @param subqueryMap Map of subqueries, keyed by the subquery name (implicit variable)
     * @return The compiled query
     */
    public QueryCompilation compile(Map parameters, Map subqueryMap)
    {
        parser = new SQLParser(null); // TODO Add options
        symtbl = new SymbolTable(clr);
        symtbl.setSymbolResolver(this);

        // TODO Handle subqueryMap

        compileCandidatesParametersVariables(parameters);
        Expression[] exprFrom = compileFrom();
        Expression exprFilter = compileFilter();
        Expression[] exprOrdering = compileOrdering();
        Expression[] exprResult = compileResult();
        Expression[] exprGrouping = compileGrouping();
        Expression exprHaving = compileHaving();
        QueryCompilation compilation = new QueryCompilation(candidateClass, candidateAlias, symtbl,
            exprResult, exprFrom, exprFilter, exprGrouping, exprHaving, exprOrdering, null);
        compilation.setQueryLanguage("SQL");

        // TODO Compile subqueries - see query.getSubqueries()

        return compilation;
    }

    /* (non-Javadoc)
     * @see org.datanucleus.query.symbol.SymbolResolver#supportsVariables()
     */
    public boolean supportsImplicitVariables()
    {
        return false;
    }

    /* (non-Javadoc)
     * @see org.datanucleus.query.symbol.SymbolResolver#caseSensitiveSymbolNames()
     */
    public boolean caseSensitiveSymbolNames()
    {
        return false;
    }
}
TOP

Related Classes of org.datanucleus.query.compiler.SQLCompiler

TOP
Copyright © 2018 www.massapi.com. 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.