Search in sources :

Example 1 with PropertySymbol

use of org.datanucleus.query.compiler.PropertySymbol in project datanucleus-core by datanucleus.

the class ParameterExpression method bind.

/**
 * Method to bind the expression to the symbol table as appropriate.
 * @param symtbl Symbol Table
 * @return The symbol for this expression
 */
public Symbol bind(SymbolTable symtbl) {
    if (symtbl.hasSymbol(getId())) {
        symbol = symtbl.getSymbol(getId());
    } else {
        // No symbol for this parameter yet, so add one
        symbol = new PropertySymbol(getId());
        symbol.setType(Symbol.PARAMETER);
        if (type != null) {
            // Add valueType if known
            symbol.setValueType(type);
        }
        symtbl.addSymbol(symbol);
    }
    return symbol;
}
Also used : PropertySymbol(org.datanucleus.query.compiler.PropertySymbol)

Example 2 with PropertySymbol

use of org.datanucleus.query.compiler.PropertySymbol in project datanucleus-core by datanucleus.

the class PrimaryExpression method bind.

/**
 * Method to bind the expression to the symbol table as appropriate.
 * @param symtbl Symbol Table
 * @return The symbol for this expression
 */
public Symbol bind(SymbolTable symtbl) {
    if (left != null) {
        left.bind(symtbl);
    }
    // TODO Cater for finding field of left expression
    if (left == null && symtbl.hasSymbol(getId())) {
        symbol = symtbl.getSymbol(getId());
        if (symbol.getType() == Symbol.VARIABLE) {
            // This is a variable, so needs converting
            throw new PrimaryExpressionIsVariableException(symbol.getQualifiedName());
        }
        return symbol;
    }
    if (left != null) {
        return null;
    }
    if (symbol == null) {
        // Try with our symbol table
        try {
            Class symbolType = symtbl.getSymbolResolver().getType(tuples);
            symbol = new PropertySymbol(getId(), symbolType);
        } catch (NucleusUserException nue) {
        // Thrown if a field in the primary expression doesn't exist.
        }
    }
    if (symbol == null && symtbl.getParentSymbolTable() != null) {
        // Try parent symbol table if present
        try {
            Class symbolType = symtbl.getParentSymbolTable().getSymbolResolver().getType(tuples);
            symbol = new PropertySymbol(getId(), symbolType);
        } catch (NucleusUserException nue) {
        // Thrown if a field in the primary expression doesn't exist.
        }
    }
    if (symbol == null) {
        // This may be due to an entry like "org.jpox.samples.MyClass" used for "instanceof"
        String className = getId();
        try {
            // Try to find this as a complete class name (e.g as used in "instanceof")
            Class cls = symtbl.getSymbolResolver().resolveClass(className);
            // Represents a valid class so throw exception to get the PrimaryExpression swapped
            throw new PrimaryExpressionIsClassLiteralException(cls);
        } catch (ClassNotResolvedException cnre) {
            // Try to find classname.staticField
            if (className.indexOf('.') < 0) {
                // {candidateCls}.primary so "primary" is staticField ?
                Class primaryCls = symtbl.getSymbolResolver().getPrimaryClass();
                if (primaryCls == null) {
                    throw new NucleusUserException("Class name " + className + " could not be resolved");
                }
                try {
                    Field fld = primaryCls.getDeclaredField(className);
                    if (!Modifier.isStatic(fld.getModifiers())) {
                        throw new NucleusUserException("Identifier " + className + " is unresolved (not a static field)");
                    }
                    throw new PrimaryExpressionIsClassStaticFieldException(fld);
                } catch (NoSuchFieldException nsfe) {
                    if (symtbl.getSymbolResolver().supportsImplicitVariables() && left == null) {
                        // Implicit variable assumed so swap this primary for it
                        throw new PrimaryExpressionIsVariableException(className);
                    }
                    throw new NucleusUserException("Class name " + className + " could not be resolved");
                }
            }
            try {
                String staticFieldName = className.substring(className.lastIndexOf('.') + 1);
                className = className.substring(0, className.lastIndexOf('.'));
                Class cls = symtbl.getSymbolResolver().resolveClass(className);
                try {
                    Field fld = cls.getDeclaredField(staticFieldName);
                    if (!Modifier.isStatic(fld.getModifiers())) {
                        throw new NucleusUserException("Identifier " + className + "." + staticFieldName + " is unresolved (not a static field)");
                    }
                    throw new PrimaryExpressionIsClassStaticFieldException(fld);
                } catch (NoSuchFieldException nsfe) {
                    throw new NucleusUserException("Identifier " + className + "." + staticFieldName + " is unresolved (not a static field)");
                }
            } catch (ClassNotResolvedException cnre2) {
                if (getId().indexOf(".") > 0) {
                    Iterator<String> tupleIter = tuples.iterator();
                    Class cls = null;
                    while (tupleIter.hasNext()) {
                        String tuple = tupleIter.next();
                        if (cls == null) {
                            Symbol sym = symtbl.getSymbol(tuple);
                            if (sym == null) {
                                sym = symtbl.getSymbol("this");
                                if (sym == null) {
                                    // TODO Need to get hold of candidate alias
                                    break;
                                }
                            }
                            cls = sym.getValueType();
                        } else {
                            // Look for member of the current class
                            if (cls.isArray() && tuple.equals("length") && !tupleIter.hasNext()) {
                                // Special case of Array.length
                                PrimaryExpression primExpr = new PrimaryExpression(left, tuples.subList(0, tuples.size() - 1));
                                InvokeExpression invokeExpr = new InvokeExpression(primExpr, "size", null);
                                throw new PrimaryExpressionIsInvokeException(invokeExpr);
                            }
                            cls = ClassUtils.getClassForMemberOfClass(cls, tuple);
                        }
                    }
                    if (cls != null) {
                    // TODO Add symbol
                    }
                }
            }
            if (symtbl.getSymbolResolver().supportsImplicitVariables() && left == null) {
                // Implicit variable, so put as "left" and remove from tuples
                String varName = tuples.remove(0);
                VariableExpression varExpr = new VariableExpression(varName);
                varExpr.bind(symtbl);
                left = varExpr;
            } else {
                // Just throw the original exception
                throw new NucleusUserException("Cannot find type of (part of) " + getId() + " since symbol has no type; implicit variable?");
            }
        }
    }
    return symbol;
}
Also used : PropertySymbol(org.datanucleus.query.compiler.PropertySymbol) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Symbol(org.datanucleus.query.compiler.Symbol) PropertySymbol(org.datanucleus.query.compiler.PropertySymbol) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) Field(java.lang.reflect.Field)

Example 3 with PropertySymbol

use of org.datanucleus.query.compiler.PropertySymbol in project datanucleus-api-jdo by datanucleus.

the class AbstractJDOQLTypedQuery method compile.

/**
 * Method to compile the query as it is currently defined.
 * @param mmgr Metadata manager
 * @param clr ClassLoader resolver
 * @return The generic compilation
 */
protected QueryCompilation compile(MetaDataManager mmgr, ClassLoaderResolver clr) {
    SymbolTable symtbl = new SymbolTable();
    symtbl.setSymbolResolver(new JDOQLSymbolResolver(mmgr, clr, symtbl, candidateCls, candidateAlias));
    symtbl.addSymbol(new PropertySymbol(candidateAlias, candidateCls));
    org.datanucleus.query.expression.Expression[] resultExprs = null;
    if (result != null && !result.isEmpty()) {
        resultExprs = new org.datanucleus.query.expression.Expression[result.size()];
        Iterator iter = result.iterator();
        int i = 0;
        while (iter.hasNext()) {
            ExpressionImpl result = (ExpressionImpl) iter.next();
            org.datanucleus.query.expression.Expression resultExpr = result.getQueryExpression();
            resultExpr.bind(symtbl);
            resultExprs[i++] = resultExpr;
        }
        if (resultExprs.length == 1 && resultExprs[0] instanceof PrimaryExpression) {
            // Check for special case of "Object(p)" in result, which means no special result
            String resultExprId = ((PrimaryExpression) resultExprs[0]).getId();
            if (resultExprId.equalsIgnoreCase(candidateAlias)) {
                resultExprs = null;
            }
        }
    }
    org.datanucleus.query.expression.Expression filterExpr = null;
    if (filter != null) {
        filterExpr = filter.getQueryExpression();
        if (filterExpr != null) {
            filterExpr.bind(symtbl);
        }
    }
    org.datanucleus.query.expression.Expression[] groupingExprs = null;
    if (grouping != null && !grouping.isEmpty()) {
        groupingExprs = new org.datanucleus.query.expression.Expression[grouping.size()];
        Iterator iter = grouping.iterator();
        int i = 0;
        while (iter.hasNext()) {
            ExpressionImpl grp = (ExpressionImpl) iter.next();
            org.datanucleus.query.expression.Expression groupingExpr = grp.getQueryExpression();
            groupingExpr.bind(symtbl);
            groupingExprs[i++] = groupingExpr;
        }
    }
    org.datanucleus.query.expression.Expression havingExpr = null;
    if (having != null) {
        havingExpr = having.getQueryExpression();
        havingExpr.bind(symtbl);
    }
    org.datanucleus.query.expression.Expression[] orderExprs = null;
    if (ordering != null && !ordering.isEmpty()) {
        orderExprs = new org.datanucleus.query.expression.Expression[ordering.size()];
        Iterator<OrderExpressionImpl> iter = ordering.iterator();
        int i = 0;
        while (iter.hasNext()) {
            OrderExpressionImpl order = iter.next();
            org.datanucleus.query.expression.OrderExpression orderExpr = new org.datanucleus.query.expression.OrderExpression(((ExpressionImpl) order.getExpression()).getQueryExpression(), order.getDirection() == OrderDirection.ASC ? "ascending" : "descending");
            orderExpr.bind(symtbl);
            orderExprs[i++] = orderExpr;
        }
    }
    org.datanucleus.query.expression.Expression[] updateExprs = null;
    if (this.updateExprs != null) {
        Iterator<ExpressionImpl> expIter = this.updateExprs.iterator();
        Iterator<ExpressionImpl> valIter = this.updateVals.iterator();
        updateExprs = new Expression[this.updateExprs.size()];
        int i = 0;
        while (expIter.hasNext()) {
            ExpressionImpl updateExpr = expIter.next();
            ExpressionImpl updateVal = valIter.next();
            updateExprs[i++] = new DyadicExpression(updateExpr.getQueryExpression(), Expression.OP_EQ, updateVal.getQueryExpression());
        }
    }
    compilation = new QueryCompilation(candidateCls, candidateAlias, symtbl, resultExprs, null, filterExpr, groupingExprs, havingExpr, orderExprs, updateExprs);
    compilation.setQueryLanguage(Query.LANGUAGE_JDOQL);
    return compilation;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) PropertySymbol(org.datanucleus.query.compiler.PropertySymbol) Expression(org.datanucleus.query.expression.Expression) SymbolTable(org.datanucleus.query.compiler.SymbolTable) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Iterator(java.util.Iterator) JDOQLSymbolResolver(org.datanucleus.query.compiler.JDOQLSymbolResolver) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Example 4 with PropertySymbol

use of org.datanucleus.query.compiler.PropertySymbol in project datanucleus-core by datanucleus.

the class CreatorExpression method bind.

/**
 * Method to bind the expression to the symbol table as appropriate.
 * @param symtbl Symbol table
 * @return The symbol for this expression
 */
public Symbol bind(SymbolTable symtbl) {
    if (symtbl.hasSymbol(getId())) {
        symbol = symtbl.getSymbol(getId());
    } else {
        try {
            // Try to find this as a complete class name (e.g as used in "instanceof")
            Class cls = symtbl.getSymbolResolver().resolveClass(getId());
            symbol = new PropertySymbol(getId(), cls);
        } catch (ClassNotResolvedException cnre) {
            throw new NucleusUserException("CreatorExpression defined with class of " + getId() + " yet this class is not found");
        }
    }
    return symbol;
}
Also used : PropertySymbol(org.datanucleus.query.compiler.PropertySymbol) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException)

Example 5 with PropertySymbol

use of org.datanucleus.query.compiler.PropertySymbol in project datanucleus-core by datanucleus.

the class VariableExpression method bind.

/**
 * Method to bind the expression to the symbol table as appropriate.
 * @return The symbol for this expression
 */
public Symbol bind(SymbolTable symtbl) {
    if (symtbl.hasSymbol(getId())) {
        symbol = symtbl.getSymbol(getId());
    } else {
        // No symbol for this variable yet, so add one
        if (type != null) {
            symbol = new PropertySymbol(getId(), type);
        } else {
            symbol = new PropertySymbol(getId());
        }
        symbol.setType(Symbol.VARIABLE);
        symtbl.addSymbol(symbol);
    }
    return symbol;
}
Also used : PropertySymbol(org.datanucleus.query.compiler.PropertySymbol)

Aggregations

PropertySymbol (org.datanucleus.query.compiler.PropertySymbol)5 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 Field (java.lang.reflect.Field)1 Iterator (java.util.Iterator)1 JDOQLSymbolResolver (org.datanucleus.query.compiler.JDOQLSymbolResolver)1 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)1 Symbol (org.datanucleus.query.compiler.Symbol)1 SymbolTable (org.datanucleus.query.compiler.SymbolTable)1 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)1 Expression (org.datanucleus.query.expression.Expression)1 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)1 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)1 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)1 VariableExpression (org.datanucleus.query.expression.VariableExpression)1