Пример #1
0
        public SqlExpression GetResult()
        {
            if (_ofType != null)
            {
                _where = _where == null ? _ofType : new WhereExpression(_where, _ofType);
            }
            if (_where != null && !(_where is WhereExpression))
            {
                _where = new WhereExpression(_where, null);
            }

            if (_targetExpression == typeof(SelectExpression))
            {
                var select = _select ?? new SelectExpression();
                if (select.Selection == null)
                {
                    SelectAll(_dbSet, null, select, string.Empty);
                }
                if (select.From == null)
                {
                    select.From = GetFrom();
                }
                select.AddWhere(_where);
                select.OrderBy = _orderBy;
                return(select);
            }
            if (_targetExpression == typeof(DeleteExpression))
            {
                var from = GetFrom();
                if (from is ListingExpression)
                {
                    throw new NotSupportedException("Multiple from is not supported in case of Delete");
                }
                return(new DeleteExpression(from)
                {
                    Where = _where
                });
            }

            if (_targetExpression == typeof(UpdateExpression))
            {
                //if (_update == null || _update.Set == null) throw new Exception("Update statement is missing.");
                if (_update == null)
                {
                    _update = new UpdateExpression();
                }
                if (_update.Target == null)
                {
                    _update.Target = GetFrom();
                }
                _update.AddWhere(_where);
                return(_update);
            }

            throw new NotSupportedException("Invalid target expression: " + _targetExpression);
        }
 protected virtual Expression VisitUpdate(UpdateExpression node)
 {
     Append("UPDATE");
     //TODO: support from
     Visit(node.Target);
     Append("SET");
     Visit(node.Set);
     if (node.Where != null)
     {
         Append("WHERE");
         Visit(node.Where);
     }
     return(node);
 }
Пример #3
0
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            var expression = node.Arguments.Count > 1 ? BuildExpression(node.Arguments[1]) : null;

            switch (node.Method.Name)
            {
            case "Where":
            case "Single":
            case "SingleOrDefault":
            case "StartsWith":
            case "EndsWith":
                _where = _where == null ? expression : new WhereExpression(_where, expression);
                break;

            case "Select":
                if (_select == null)
                {
                    _select = new SelectExpression();
                }
                _select.AddSelection(expression);
                break;

            case "Update":
                Debugger.Break();
                if (_update == null)
                {
                    _update = new UpdateExpression();
                }
                _update.AddSet(expression);
                break;

            case "Count":
                _select = new SelectExpression {
                    Selection = new CountExpression()
                };
                _where = _where == null ? expression : new WhereExpression(_where, expression);
                break;

            case "Max":
                _select = new SelectExpression {
                    Selection = new MaxExpression(expression)
                };
                break;

            case "Any":
                _select = new SelectExpression {
                    Selection = new CountExpression()
                };
                _where = _where == null ? expression : new WhereExpression(_where, expression);
                break;

            case "First":
            case "FirstOrDefault":
                if (_select == null)
                {
                    _select = new SelectExpression();
                }
                _select.Top = Expression.Constant(1);
                _where      = _where == null ? expression : new WhereExpression(_where, expression);
                break;

            case "Take":
                if (_select == null)
                {
                    _select = new SelectExpression();
                }
                _select.Top = node.Arguments[1];
                break;

            case "OrderBy":
            case "ThenBy":
                if (_orderBy == null)
                {
                    _orderBy = new OrderByExpression();
                }
                _orderBy.AddColumn(expression, ListSortDirection.Ascending);
                break;

            case "ThenByDescending":
            case "OrderByDescending":
                if (_orderBy == null)
                {
                    _orderBy = new OrderByExpression();
                }
                _orderBy.AddColumn(expression, ListSortDirection.Descending);
                break;

            case "OfType":
                if (_ofType == null)
                {
                    var constantExpression = expression as ConstantExpression;
                    var param = constantExpression == null?node.Method.ReturnType.GetGenericArguments()[0] : constantExpression.Value;

                    var types = _dbSet.GetDiscriminatorValues((Type)param);
                    if (types != null)
                    {
                        _ofType = new ContainsExpression(new ColumnExpression("Discriminator"), types.Cast <Type>().Select(t => t.FullName));
                    }
                }
                break;

            case "Contains":
                break;

            case "Skip":
            case "AssignNewValue":
                throw new NotImplementedException();

            default:
                throw new NotSupportedException("Not supported method: " + node.Method.Name);
            }
            return(base.VisitMethodCall(node));
        }
Пример #4
0
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            var expression = node.Arguments.Count > 1 ? BuildExpression(node.Arguments[1]) : null;

            switch (node.Method.Name)
            {
            case "Where":
            case "Single":
            case "SingleOrDefault":
                _where = _where == null ? expression : new WhereExpression(_where, expression);
                break;

            case "Select":
                if (_select == null)
                {
                    _select = new SelectExpression();
                }
                _select.AddSelection(expression);
                break;

            case "Update":
                if (_update == null)
                {
                    _update = new UpdateExpression();
                }
                _update.AddSet(expression);
                break;

            case "Count":
                _select = new SelectExpression {
                    Selection = new CountExpression()
                };
                _where = _where == null ? expression : new WhereExpression(_where, expression);
                break;

            case "First":
            case "FirstOrDefault":
                if (_select == null)
                {
                    _select = new SelectExpression();
                }
                _select.Top = Expression.Constant(1);
                _where      = _where == null ? expression : new WhereExpression(_where, expression);
                break;

            case "Take":
                if (_select == null)
                {
                    _select = new SelectExpression();
                }
                _select.Top = node.Arguments[1];
                break;

            case "Skip":
                throw new NotImplementedException();

            case "OrderBy":
            case "ThenBy":
                if (_orderBy == null)
                {
                    _orderBy = new OrderByExpression();
                }
                _orderBy.AddColumn(expression, ListSortDirection.Ascending);
                break;

            case "ThenByDescending":
            case "OrderByDescending":
                if (_orderBy == null)
                {
                    _orderBy = new OrderByExpression();
                }
                _orderBy.AddColumn(expression, ListSortDirection.Descending);
                break;

            case "Contains":
            case "StartsWith":
            case "EndsWith":
            case "AssignNewValue":
                break;

            default:
                throw new NotSupportedException("Not supported method: " + node.Method.Name);
            }
            return(base.VisitMethodCall(node));
        }
 protected virtual Expression VisitUpdate(UpdateExpression node)
 {
     Append("UPDATE");
     //TODO: support from
     Visit(node.Target);
     Append("SET");
     Visit(node.Set);
     if (node.Where != null)
     {
         Append("WHERE");
         Visit(node.Where);
     }
     return node;
 }