Represents a select query
Inheritance: System.Linq.Expressions.Expression
Example #1
0
        public ProjectionExpression UpdateSelect(ProjectionExpression projection, Expression selectExpression)
        {
            //get the lambda expression of the select method
            var lambda = (LambdaExpression)selectExpression.StripQuotes();

            //if the lambda, is the identity lamda, simply return
            if (lambda.IsIdentityLambda())
                return projection;

            //map the source argument of the lambda expression to the existing projection
            Map.Add(lambda.Parameters[0], projection.Projection);

            //get the new projection
            var newProjection = Visit(lambda.Body);

            //check if projection is actually changed
            if (newProjection == lambda.Body)
                return projection;

            //get used columns
            IEnumerable<SelectorExpression> columns = new ColumnFinder().FindColumns(newProjection);

            SelectStatementExpression select = projection.Select;
            var newSelect = new SelectStatementExpression(typeof(IEnumerable<>).MakeGenericType(newProjection.Type),
                                                          new SelectClauseExpression(columns.ToArray(),
                                                                                     select.SelectClause.Distinct),
                                                          select.TableName,
                                                          select.WhereClause,
                                                          select.OrderBy,
                                                          select.Limit,
                                                          select.AllowFiltering);
            
            return new ProjectionExpression(newSelect, newProjection, projection.Aggregator, false, projection.Consistency, projection.PageSize);
        }
Example #2
0
        public ProjectionExpression BuildWhere(ProjectionExpression projection, Expression whereClause)
        {
            //get the lambda expression of the select method
            var lambda = (LambdaExpression)whereClause.StripQuotes();

            //map the source argument of the lambda expression to the existing projection (the projection is what the where clause queries)
            Map.Add(lambda.Parameters[0], projection.Projection);

            if (projection.Select.WhereClause != null)
                _relations = new HashSet<RelationExpression>(projection.Select.WhereClause);
            else
                _relations = new HashSet<RelationExpression>();

            //get the new projections
            Expression expression = Visit(lambda.Body);

            if (!expression.IsTrue())
                throw new CqlLinqException("Where clause contains unsupported constructs");

            var select = projection.Select;

            var newSelectStmt = new SelectStatementExpression(select.Type,
                                                              select.SelectClause,
                                                              select.TableName,
                                                              _relations.ToArray(),
                                                              select.OrderBy,
                                                              select.Limit,
                                                              select.AllowFiltering);

            return new ProjectionExpression(newSelectStmt, projection.Projection,
                                            projection.Aggregator, projection.CanTrackChanges, projection.Consistency, projection.PageSize);
        }
 public ProjectionExpression(SelectStatementExpression @select, Expression projection, LambdaExpression aggregator, bool canTrackChanges, CqlConsistency? consistency, int? pageSize)
 {
     _select = @select;
     _projection = projection;
     _canTrackChanges = canTrackChanges;
     _consistency = consistency;
     _pageSize = pageSize;
     _aggregator = aggregator;
 }
Example #4
0
        public override Expression VisitSelectStatement(SelectStatementExpression selectStatement)
        {
            base.VisitSelectStatement(selectStatement);

            var builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append(_translations[selectStatement.SelectClause]);
            builder.Append(" FROM \"");
            builder.Append(selectStatement.TableName.Replace("\"", "\"\"").Replace(".","\".\""));
            builder.Append("\"");

            if (selectStatement.WhereClause != null && selectStatement.WhereClause.Any())
            {
                builder.Append(" WHERE ");
                var wheres = selectStatement.WhereClause.Select(relation => _translations[relation]);
                builder.Append(string.Join(" AND ", wheres));
            }

            if (selectStatement.OrderBy != null && selectStatement.OrderBy.Any())
            {
                builder.Append(" ORDER BY ");
                var orders = selectStatement.OrderBy.Select(order => _translations[order]);
                builder.Append(string.Join(",", orders));
            }

            if (selectStatement.Limit.HasValue)
            {
                builder.Append(" LIMIT ");
                builder.Append(selectStatement.Limit);
            }

            if (selectStatement.AllowFiltering)
            {
                builder.Append(" ALLOW FILTERING");
            }

            builder.Append(";");

            _translations[selectStatement] = builder.ToString();

            return selectStatement;
        }
Example #5
0
        public ProjectionExpression UpdateOrder(ProjectionExpression projection, Expression keySelectExpression,
                                                bool ascending)
        {
            //get the lambda expression of the select method
            var lambda = (LambdaExpression)keySelectExpression.StripQuotes();

            //map the source argument of the lambda expression to the existing projection
            Map.Add(lambda.Parameters[0], projection.Projection);

            //get the new projection
            var key = Visit(lambda.Body);

            //check if we are dealing with a column
            if ((CqlExpressionType)key.NodeType != CqlExpressionType.IdentifierSelector)
                throw new CqlLinqException("Select key in OrderBy does not map to table column");

            //get a reference to the select clause
            SelectStatementExpression select = projection.Select;

            //add the ordering to the list of orderBy clauses
            var ordering = new List<OrderingExpression>(select.OrderBy);
            ordering.Add(new OrderingExpression((SelectorExpression)key,
                                                ascending
                                                    ? CqlExpressionType.OrderAscending
                                                    : CqlExpressionType.OrderDescending));

            var newSelect = new SelectStatementExpression(select.Type,
                                                          select.SelectClause,
                                                          select.TableName,
                                                          select.WhereClause,
                                                          ordering,
                                                          select.Limit,
                                                          select.AllowFiltering);

            return new ProjectionExpression(newSelect, projection.Projection,
                                            projection.Aggregator, projection.CanTrackChanges, projection.Consistency, projection.PageSize);
        }
 public virtual Expression VisitSelectStatement(SelectStatementExpression selectStatement)
 {
     return base.VisitExtension(selectStatement);
 }