/* // $Id: //guest/julian_hyde/saffron/src/main/openjava/ptree/QueryExpression.java#3 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. */ package openjava.ptree; import openjava.mop.Environment; import openjava.mop.OJClass; import openjava.mop.QueryEnvironment; import openjava.mop.Toolbox; import openjava.ptree.util.ParseTreeVisitor; import openjava.ptree.util.SyntheticClass; /** * <code>QueryExpression</code> is a <code>select ... group by ... from * ... where ... order by ...</code> expression. When used as an expression, * it evaluates to an array, but you can also use it inside a <code>for * (<i>variable</i> in select ...)</code> construct. When used in this way, it * is evaluated as an iterator. * * <p> A null <code>groupList</code> means don't aggregate, whereas an empty * <code>groupList</code> means group by the 0-tuple <code>{}</code>, which * produces a single row even if there are no input rows. **/ public class QueryExpression extends SetExpression { boolean boxed; public QueryExpression( ExpressionList selectList, boolean boxed, ExpressionList groupList, Expression from, Expression where, ExpressionList sortList) { if (selectList == null) { selectList = new ExpressionList(); Expression[] expressions = SetExpression.flatten(from); for (int i = 0; i < expressions.length; i++) { String alias = Toolbox.getAlias(expressions[i]); if (alias == null) { Toolbox.assert( expressions.length == 1, "joined expressions must have aliases"); Toolbox.assert(from == expressions[0]); alias = "$" + Integer.toHexString(from.getObjectID()); from = new AliasedExpression(from, alias); } selectList.add(new Variable(alias)); } boxed = (expressions.length != 1); } this.boxed = boxed; if (!boxed) { Toolbox.assert(selectList.size() == 1); } set(selectList, groupList, from, where, sortList); } public ExpressionList getSelectList() { return (ExpressionList) elementAt(0); } public boolean isBoxed() { return boxed; } public ExpressionList getGroupList() { return (ExpressionList) elementAt(1); } public Expression getFrom() { return (Expression) elementAt(2); } public Expression getWhere() { return (Expression) elementAt(3); } public ExpressionList getSort() { return (ExpressionList) elementAt(4); } public void accept(ParseTreeVisitor v) throws ParseTreeException { v.visit(this); } // implement SetExpression public OJClass deriveRowType(Environment env) throws Exception { QueryEnvironment queryEnv = new QueryEnvironment(env, this); ExpressionList selectList = getSelectList(); if (!boxed) { Toolbox.assert(selectList.size() == 1); Expression select = selectList.get(0); return Toolbox.getType(queryEnv, select); } OJClass clazz = queryEnv.lookupClass(queryEnv.currentClassName()); OJClass[] classes = new OJClass[selectList.size()]; String[] fieldNames = new String[classes.length]; for (int i = 0; i < classes.length; i++) { Expression exp = selectList.get(i); classes[i] = Toolbox.getType(queryEnv, exp); fieldNames[i] = Toolbox.getAlias(exp); } return SyntheticClass.createProject(clazz, classes, fieldNames); } }; // End QueryExpression.java
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 1801 | Julian Hyde |
saffron: add ObjectSchema; rules can now be matched more than once; started to implement correlations in queries in from list. |
||
#2 | 1474 | Julian Hyde |
saffron: Aggregations are working. Renamed 'aggregator' to 'aggregation'. |
||
#1 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |