Exemplo n.º 1
0
        /// <summary>
        /// Tries to evaluate the current expression and add it as a projection
        /// </summary>
        private Expression AddProjection(Expression node, string columnName = null)
        {
            var value = Expression.Lambda(node).Compile().DynamicInvoke();

            if (columnName == null)
            {
                columnName = _pocoData.GetColumnNameByMemberName(_currentBindingName.Get());
                if (columnName == null)
                {
                    throw new CqlLinqNotSupportedException(node, _parsePhase.Get());
                }
            }
            _projections.Add(Tuple.Create(columnName, value, ExpressionType.Assign));
            return(node);
        }
        protected override Expression VisitConstant(ConstantExpression node)
        {
            if (node.Value is ITable)
            {
                return(node);
            }
            switch (_parsePhase.Get())
            {
            case ParsePhase.Condition:
                _currentCondition.Get().Item1.Append("?");
                _currentCondition.Get().Item2.Add(node.Value);
                return(node);

            case ParsePhase.SelectBinding:
            {
                var column = _pocoData.GetColumnByMemberName(_currentBindingName.Get());
                if (column == null)
                {
                    //selecting a field that is not part of PocoType
                    break;
                }
                var expressionType = ExpressionType.Assign;
                if (column.IsCounter)
                {
                    if (!(node.Value is long || node.Value is int))
                    {
                        throw new ArgumentException("Only Int64 and Int32 values are supported as counter increment of decrement values");
                    }
                    expressionType = ExpressionType.Increment;
                }
                _projections.Add(Tuple.Create(column, node.Value, expressionType));
                _selectFields.Add(column.ColumnName);
                return(node);
            }

            case ParsePhase.Take:
                _limit = (int)node.Value;
                return(node);

            case ParsePhase.OrderBy:
            case ParsePhase.OrderByDescending:
            {
                var columnName = _pocoData.GetColumnNameByMemberName((string)node.Value);
                if (columnName == null)
                {
                    //order by a field that is not part of PocoType
                    break;
                }
                _orderBy.Add(Tuple.Create(columnName, _parsePhase.Get() == ParsePhase.OrderBy));
                return(node);
            }
            }
            throw new CqlLinqNotSupportedException(node, _parsePhase.Get());
        }