Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * See the COPYRIGHT.txt file distributed with this work for information
   * regarding copyright ownership.  Some portions may be licensed
   * to Red Hat, Inc. under one or more contributor license agreements.
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
  * version 2.1 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  * 02110-1301 USA.
  */
 package org.teiid.olingo;
 
 import static org.teiid.language.SQLConstants.Reserved.CONVERT;
 
 import java.util.List;
 
 import  org.apache.olingo.commons.api.edm.EdmEntityType;
 import  org.apache.olingo.commons.api.edm.EdmNavigationProperty;
 import  org.apache.olingo.commons.core.edm.primitivetype.SingletonPrimitiveType;
 import  org.apache.olingo.server.api.uri.UriInfo;
 import  org.apache.olingo.server.api.uri.UriResource;
 import  org.apache.olingo.server.api.uri.UriResourceKind;
 import  org.apache.olingo.server.api.uri.UriResourceNavigation;
 import  org.apache.olingo.server.api.uri.UriResourceProperty;
 import  org.apache.olingo.server.api.uri.queryoption.expression.Alias;
 import  org.apache.olingo.server.api.uri.queryoption.expression.Binary;
 import  org.apache.olingo.server.api.uri.queryoption.expression.Enumeration;
 import  org.apache.olingo.server.api.uri.queryoption.expression.Expression;
 import  org.apache.olingo.server.api.uri.queryoption.expression.LambdaRef;
 import  org.apache.olingo.server.api.uri.queryoption.expression.Literal;
 import  org.apache.olingo.server.api.uri.queryoption.expression.Member;
 import  org.apache.olingo.server.api.uri.queryoption.expression.Method;
 import  org.apache.olingo.server.api.uri.queryoption.expression.TypeLiteral;
 import  org.apache.olingo.server.api.uri.queryoption.expression.Unary;
 
     private final Stack<org.teiid.query.sql.symbol.Expressionstack = new Stack<org.teiid.query.sql.symbol.Expression>();
     private final ODataQueryContext context;
     private final ArrayList<SQLParamparams = new ArrayList<SQLParam>();
     private boolean prepared = false;
     private final List<Exceptionexceptions = new ArrayList<Exception>();
     private final UriInfo uriInfo;
 
     public ODataExpressionToSQLVisitor(ODataQueryContext contextboolean prepared, UriInfo info) {
         this. = context;
         this. = prepared;
         this. = info;
     }
 
     public org.teiid.query.sql.symbol.Expression getExpression(Expression expr) {
         accept(expr);
         return this..pop();
     }
 
     @Override
     public void visit(Alias expr) {
         Object value = LiteralParser.parseLiteral(this..getValueForAlias(expr.getParameterName()));
         if (this.) {
             .add(new Reference(this..size()));
             this..add(new SQLParam(value, JDBCSQLTypeInfo.getSQLTypeFromClass(value.getClass().getName())));
         } else {
             this..add(new Constant(value));
         }
    }
    @Override
    public void visit(Binary expr) {
        accept(expr.getLeftOperand());
        org.teiid.query.sql.symbol.Expression lhs = this..pop();
        accept(expr.getRightOperand());
        org.teiid.query.sql.symbol.Expression rhs = this..pop();
        switch (expr.getOperator()) {
        case HAS:
            // TODO: not supported. What would be SQL equivalent?
            break;
        case MUL:
            .push(new Function("*"new org.teiid.query.sql.symbol.Expression[] { lhsrhs })); //$NON-NLS-1$
            break;
        case DIV:
            .push(new Function("/"new org.teiid.query.sql.symbol.Expression[] { lhsrhs })); //$NON-NLS-1$
            break;
        case MOD:
            .push(new Function("MOD"new org.teiid.query.sql.symbol.Expression[] { lhsrhs })); //$NON-NLS-1$
            break;
        case ADD:
            .push(new Function("+"new org.teiid.query.sql.symbol.Expression[] { lhsrhs })); //$NON-NLS-1$
            break;
        case SUB:
            .push(new Function("-"new org.teiid.query.sql.symbol.Expression[] { lhsrhs })); //$NON-NLS-1$
            break;
        case GT:
            this..push(new CompareCriteria(lhs.rhs));
            break;
        case GE:
            this..push(new CompareCriteria(lhs.rhs));
            break;
        case LT:
            this..push(new CompareCriteria(lhs.rhs));
            break;
        case LE:
            this..push(new CompareCriteria(lhs.rhs));
            break;
        case EQ:
            if (rhs instanceof Constant && ((Constantrhs).getType() == ..) {
                this..push(new IsNullCriteria(lhs));
            } else {
                this..push(new CompareCriteria(lhs.rhs));
            }
            break;
        case NE:
            if (rhs instanceof Constant && ((Constantrhs).getType() == ..) {
                IsNullCriteria crit = new IsNullCriteria(lhs);
                crit.setNegated(true);
                this..push(crit);
            } else {
                this..push(new CompareCriteria(lhs.rhs));
            }
            break;
        case AND:
            this..push(new CompoundCriteria(., (Criterialhs, (Criteriarhs));
            break;
        case OR:
            this..push(new CompoundCriteria(., (Criterialhs, (Criteriarhs));
            break;
        }
    }
    @Override
    public void visit(Enumeration expr) {
        this..add(new TeiidException("un supported option"));//$NON-NLS-1$
    }
    @Override
    public void visit(LambdaRef expr) {
        // TODO: any and All implementations
    }
    @Override
    public void visit(Literal expr) {
        Object value = LiteralParser.parseLiteral(expr.getText());
        if (this.) {
            .add(new Reference(this..size()));
            this..add(new SQLParam(value, JDBCSQLTypeInfo.getSQLTypeFromClass(value.getClass().getName())));
        } else {
            this..add(new Constant(value));
        }
    }
    @Override
    public void visit(Member expr) {
        ResourcePropertyCollector visitor = new ResourcePropertyCollector();
        visitor.visit(expr.getResourcePath());
        UriResource resource = visitor.getResource();
        if (resource.getKind() == UriResourceKind.primitiveProperty) {
            this..add(new ElementSymbol(((UriResourceProperty) resource)
                            .getProperty().getName(), 
                            .getEdmEntityTableGroup()));
        } else if (resource.getKind() == UriResourceKind.navigationProperty) {
            EdmNavigationProperty navigation = ((UriResourceNavigation) resource).getProperty();
            EdmEntityType type = navigation.getType();
            if (!visitor.isCount()) {
            }
            GroupSymbol navGroup = new GroupSymbol(.getNextAliasGroup(), type.getNamespace() + "." + type.getName());//$NON-NLS-1$
            Query query = new Query();
            query.setSelect(new Select(Arrays.asList(new AggregateSymbol(...name(), falsenull))));
            query.setFrom(new From(Arrays.asList(new UnaryFromClause(navGroup))));
            Criteria criteria = null;
            for (ForeignKey fk : .getEdmEntityTable().getForeignKeys()) {
                if (fk.getName().equals(navigation.getName())) {
                    List<StringlhsColumns = ODataSQLBuilder.getColumnNames(fk.getColumns());
                    List<StringrhsColumns = fk.getReferenceColumns();
                    for (int i = 0; i < lhsColumns.size(); i++) {
                        if (criteria == null) {
                            criteria = new CompareCriteria(new ElementSymbol(lhsColumns.get(i),.getEdmEntityTableGroup()),
                                    .new ElementSymbol(rhsColumns.get(i), navGroup));
                        } else {
                            Criteria subcriteria = new CompareCriteria(new ElementSymbol(lhsColumns.get(i), .getEdmEntityTableGroup()),
                                    .new ElementSymbol(rhsColumns.get(i), navGroup));
                            criteria = new CompoundCriteria(.criteriasubcriteria);
                        }
                    }
                    break;
                }
            }
            query.setCriteria(criteria);
            this..add(new ScalarSubquery(query));
        }
    }
    @Override
    public void visit(Method expr) {
        List<org.teiid.query.sql.symbol.ExpressionteiidExprs = new ArrayList<org.teiid.query.sql.symbol.Expression>();
        for (Expression exp : expr.getParameters()) {
            accept(exp);
            teiidExprs.add(this..pop());
        }
        switch (expr.getMethod()) {
        case CONTAINS:
            CompareCriteria criteria = new CompareCriteria(new Function("LOCATE"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(1), teiidExprs.get(0), new Constant(1) }), .new Constant(1)); //$NON-NLS-1$
            this..push(criteria);
            break;
        case STARTSWITH:
            criteria = new CompareCriteria(new Function("LOCATE"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0), teiidExprs.get(1), new Constant(1) }), .new Constant(1)); //$NON-NLS-1$
            this..push(criteria);
            break;
        case ENDSWITH:
            criteria = new CompareCriteria(new Function("ENDSWITH"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0), teiidExprs.get(1) }), .new Constant(.));//$NON-NLS-1$
            this..push(criteria);
            break;
        case LENGTH:
            this..push(new Function("LENGTH"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case INDEXOF:
            .push(new Function("LOCATE"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(1), teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case SUBSTRING:
            this..push(new Function("SUBSTRING"teiidExprs.toArray(new org.teiid.query.sql.symbol.Expression[teiidExprs.size()]))); //$NON-NLS-1$
            break;
        case TOLOWER:
            this..push(new Function("LCASE"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case TOUPPER:
            this..push(new Function("UCASE"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case TRIM:
            this..push(new Function("TRIM"new org.teiid.query.sql.symbol.Expression[] { new Constant("BOTH"), new Constant(' '), teiidExprs.get(0) })); //$NON-NLS-1$ //$NON-NLS-2$
            break;
        case CONCAT:
            this..push(new Function("CONCAT2"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0), teiidExprs.get(1) })); //$NON-NLS-1$
            break;
        case YEAR:
            this..push(new Function("YEAR"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case MONTH:
            this..push(new Function("MONTH"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case DAY:
            this..push(new Function("DAYOFMONTH"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case HOUR:
            this..push(new Function("HOUR"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case MINUTE:
            this..push(new Function("MINUTE"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case SECOND:
            this..push(new Function("SECOND"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case NOW:
            this..push(new Function("NOW"new org.teiid.query.sql.symbol.Expression[] {})); //$NON-NLS-1$
            break;
        case ROUND:
            .push(new Function("ROUND"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0), new Constant(0) })); //$NON-NLS-1$
            break;
        case FLOOR:
            this..push(new Function("FLOOR"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case CEILING:
            this..push(new Function("CEILING"new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) })); //$NON-NLS-1$
            break;
        case CAST:
            this..push(new Function(,new org.teiid.query.sql.symbol.Expression[] {teiidExprs.get(0), teiidExprs.get(1) }));
            break;
        case FRACTIONALSECONDS:
        case TOTALSECONDS:
        case DATE:
        case TIME:
        case TOTALOFFSETMINUTES:
        case MINDATETIME:
        case MAXDATETIME:
        case GEODISTANCE:
        case GEOLENGTH:
        case GEOINTERSECTS:
        case ISOF:
        default:
            this..add(new TeiidException(....gs(..expr.getMethod())));
        }
    }
    @Override
    public void visit(TypeLiteral expr) {
        this..push(new Constant(ODataTypeManager.teiidType((SingletonPrimitiveType) expr.getType())));
    }
    @Override
    public void visit(Unary expr) {
        accept(expr.getOperand());
        org.teiid.query.sql.symbol.Expression teiidExpr = this..pop();
        switch (expr.getOperator()) {
        case MINUS:
            this..push(new Function(.,new org.teiid.query.sql.symbol.Expression[] {new Constant(-1), teiidExpr }));
            break;
        case NOT:
            this..push(new NotCriteria(new ExpressionCriteria(teiidExpr)));
            break;
        }
    }
New to GrepCode? Check out our FAQ X