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

org.datanucleus.query.compiler.QueryCompilation 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.compiler;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.datanucleus.query.QueryUtils;
import org.datanucleus.query.expression.Expression;
import org.datanucleus.query.expression.ParameterExpression;
import org.datanucleus.query.symbol.Symbol;
import org.datanucleus.query.symbol.SymbolTable;

/**
 * Representation of the components of a compiled java "string-based" query.
 * Assumes that the query has the following forms
 * 
 * SELECT {result} FROM {from} WHERE {filter} GROUP BY {grouping} HAVING {having} ORDER BY {order}
 * UPDATE {from} SET {update} WHERE {filter}
 * DELETE FROM {from} WHERE {filter}
 * 
*/ public class QueryCompilation implements Serializable { private static final long serialVersionUID = 2976142726587145777L; /** Query language that this is a compilation for. */ protected String queryLanguage; /** Primary candidate class. */ protected Class candidateClass; /** Alias for the (primary) candidate. Defaults to "this". */ protected String candidateAlias = "this"; /** Whether the query will return a single row. */ protected boolean returnsSingleRow = false; /** Compiled Symbol Table. */ protected SymbolTable symtbl; /** Whether the result is distinct. */ protected boolean resultDistinct = false; /** Compiled result expression. */ protected Expression[] exprResult; /** Compiled from expression. */ protected Expression[] exprFrom; /** Compiled update expression. */ protected Expression[] exprUpdate; /** Compiled filter expression */ protected Expression exprFilter = null; /** Compiled grouping expression. */ protected Expression[] exprGrouping; /** Compiled having expression. */ protected Expression exprHaving; /** Compiled ordering expression. */ protected Expression[] exprOrdering; /** Compilations of any subqueries, keyed by the subquery variable name. */ protected Map subqueryCompilations = null; public QueryCompilation(Class candidateCls, String candidateAlias, SymbolTable symtbl, Expression[] results, Expression[] froms, Expression filter, Expression[] groupings, Expression having, Expression[] orderings, Expression[] updates) { this.candidateClass = candidateCls; this.candidateAlias = candidateAlias; this.symtbl = symtbl; this.exprResult = results; this.exprFrom = froms; this.exprFilter = filter; this.exprGrouping = groupings; this.exprHaving = having; this.exprOrdering = orderings; this.exprUpdate = updates; } public void setQueryLanguage(String lang) { this.queryLanguage = lang; } public String getQueryLanguage() { return this.queryLanguage; } public void setResultDistinct() { this.resultDistinct = true; } public boolean getResultDistinct() { return resultDistinct; } public void setReturnsSingleRow() { this.returnsSingleRow = true; } /** * Method to add the compilation for a subquery of this query. * @param alias Alias for the subquery (variable name) * @param compilation The compilation */ public void addSubqueryCompilation(String alias, QueryCompilation compilation) { if (subqueryCompilations == null) { subqueryCompilations = new HashMap(); } subqueryCompilations.put(alias, compilation); } /** * Accessor for the compilation for a subquery with the specified alias. * @param alias Alias of subquery * @return The compilation */ public QueryCompilation getCompilationForSubquery(String alias) { return (subqueryCompilations != null ? subqueryCompilations.get(alias) : null); } /** * Accessor for the aliases for any subqueries in this compilation. * @return The subquery aliases (if any) */ public String[] getSubqueryAliases() { if (subqueryCompilations == null || subqueryCompilations.isEmpty()) { return null; } String[] aliases = new String[subqueryCompilations.size()]; Iterator iter = subqueryCompilations.keySet().iterator(); int i = 0; while (iter.hasNext()) { aliases[i++] = iter.next(); } return aliases; } /** * Accessor for whether this query will return a single row. * This is true if all result selects are aggregates, or the user has set "unique" on the * query. * @return Whether this query will return a single row */ public boolean returnsSingleRow() { return returnsSingleRow; } /** * Accessor for the types of the result row components. * If no result is defined then will be an array of size 1 with element type "candidate". * @return The result type(s) */ public Class[] getResultTypes() { if (exprResult != null && exprResult.length > 0) { Class[] results = new Class[exprResult.length]; for (int i=0;i 0) { str.append(","); } str.append(exprResult[i]); } str.append("]\n"); } if (exprFrom != null) { str.append(indent).append("[from:"); for (int i=0;i 0) { str.append(","); } str.append(exprFrom[i]); } str.append("]\n"); } if (exprUpdate != null) { str.append(indent).append("[update:"); for (int i=0;i 0) { str.append(","); } str.append(exprUpdate[i]); } str.append("]\n"); } if (exprFilter != null) { str.append(indent).append("[filter:").append(exprFilter).append("]\n"); } if (exprGrouping != null) { str.append(indent).append("[grouping:"); for (int i=0;i 0) { str.append(","); } str.append(exprGrouping[i]); } str.append("]\n"); } if (exprHaving != null) { str.append(indent).append("[having:").append(exprHaving).append("]\n"); } if (exprOrdering != null) { str.append(indent).append("[ordering:"); for (int i=0;i 0) { str.append(","); } str.append(exprOrdering[i]); } str.append("]\n"); } str.append(indent).append("[symbols: "); Iterator symIter = symtbl.getSymbolNames().iterator(); while (symIter.hasNext()) { String symName = symIter.next(); Symbol sym = symtbl.getSymbol(symName); if (sym.getValueType() != null) { str.append(symName + " type=" + sym.getValueType().getName()); } else { str.append(symName + " type=unknown"); } if (symIter.hasNext()) { str.append(", "); } } str.append("]"); if (subqueryCompilations != null) { str.append("\n"); Iterator subqIter = subqueryCompilations.entrySet().iterator(); while (subqIter.hasNext()) { Map.Entry entry = (Map.Entry)subqIter.next(); str.append(indent).append("[subquery: " + entry.getKey() + "\n"); str.append(entry.getValue().debugString(indent + " ")).append("]"); if (subqIter.hasNext()) { str.append("\n"); } } } return str.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy