public QueryFragment WrapToFragment()
 {
     //if (this.Fragment.SelectPart == null)
     //{
     //    this.Fragment.SelectPart = new List<Column>();
     //}
     if (this.Fragment.SelectPart.Count == 0)
     {
         var members    = this.ExportType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
         var typeMapper = TypeMapperCache.GetTypeMapper(this.ExportType);
         var tableName  = GetTableAlias(this.ExportType);
         for (var i = 0; i < members.Length; i++)
         {
             var member     = members[i];
             var tempColumn = this.GetMappedColumn(this.ExportType, member.Name);
             if (tempColumn == null)
             {
                 var columnMapper = typeMapper.AllMembers.SingleOrDefault(m => m.Name == member.Name);
                 this.Fragment.SelectPart.Add(new MemberColumn(columnMapper.ColumnName, tableName, member.Name)
                 {
                     Type = member.ReflectedType
                 });
             }
             else
             {
                 tempColumn.Alias = member.Name;
                 this.Fragment.SelectPart.Add(tempColumn);
             }
         }
     }
     return(this.Fragment);
 }
Esempio n. 2
0
 protected override Expression VisitConstant(ConstantExpression node)
 {
     if (node.Type.GetGenericTypeDefinition() == typeof(DataQuery <>))
     {
         var elementType = node.Type.GetGenericArguments()[0];
         this.context.ExportType = this.context.ImportType = elementType;
         var typeMapper = TypeMapperCache.GetTypeMapper(elementType);
         this.context.Fragment.FromPart.Add(new SingleTable(typeMapper.TableName, this.context.GetTableAlias(elementType)));
     }
     return(node);
 }
Esempio n. 3
0
        protected override Expression VisitMember(MemberExpression node)
        {
            if (node.Expression != null && node.Expression.IsParameter())
            {
                var parameterExpresion = node.Expression.GetParameterExpresion();
                var memberType         = node.Member.DeclaringType;
                if (parameterExpresion != null && parameterExpresion.Type.IsSubclassOf(memberType))
                {
                    // memberType = parameterExpresion.Type;
                    TypeMapperCache.AddTransferTypeMapper(memberType, parameterExpresion.Type);
                }

                if (node.Member.MemberType == MemberTypes.Property)
                {
                    var property = node.Member as PropertyInfo;
                    if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                    {
                        memberType  = property.PropertyType.GenericTypeArguments.First();
                        this.Column = new MemberColumn("*", null, UseColumnAlias ? node.Member.Name : null)
                        {
                            Type = node.Type
                        };
                        return(node);
                    }
                }

                var tempColumn = this.Context.GetMappedColumn(memberType, node.Member.Name);
                if (tempColumn == null)
                {
                    var typeMapper   = TypeMapperCache.GetTypeMapper(memberType);
                    var columnMapper = typeMapper.AllMembers.SingleOrDefault(m => m.Name == node.Member.Name);
                    this.Column = new MemberColumn(columnMapper.ColumnName, Context.GetTableAlias(TypeMapperCache.GetTransferTypeMapper(memberType)), UseColumnAlias ? node.Member.Name : null)
                    {
                        Type = node.Type
                    };
                }
                else
                {
                    this.Column = tempColumn;
                }
            }
            else
            {
                var objectMember = Expression.Convert(node, typeof(object));
                var getterLambda = Expression.Lambda <Func <object> >(objectMember);
                var getter       = getterLambda.Compile()();
                this.Column = new ParameterColumn(this.Context.AddParameter(getter))
                {
                    Type = node.Type
                };
            }
            return(node);
        }
Esempio n. 4
0
        private static void InitialKeyOfParameter <T>(object parameter)
        {
            var type       = typeof(T);
            var typeMapper = TypeMapperCache.GetTypeMapper(type);
            var seed       = new Snowflake();

            if (typeMapper.Name == TypeMapper.Dictionary_Name)
            {
                var diction = parameter as IDictionary;
                foreach (var member in typeMapper.GetKeyMembers(false).Where(m => m.KeyType == KeyType.Snxowflake))
                {
                    if (!diction.Contains(member.Name))
                    {
                        diction.Add(member.Name, seed.GenerateId());
                    }
                    else
                    {
                        diction[member.Name] = seed.GenerateId();
                    }
                }
            }
            else
            {
                var instanceType = parameter.GetType();
                foreach (var member in typeMapper.GetKeyMembers(false).Where(m => m.KeyType == KeyType.Snxowflake))
                {
                    var property = instanceType.GetProperty(member.Name);
                    if (property == null)
                    {
                        //the property does not exist
                        return;
                    }
                    var setMethod = property.GetSetMethod();
                    if (setMethod == null)
                    {
                        //the property is readonly
                        return;
                    }
                    else
                    {
                        var instance       = Expression.Constant(parameter);
                        var valueParameter = Expression.Parameter(property.PropertyType);
                        var method         = Expression.Call(instance, setMethod, valueParameter);
                        var expression     = Expression.Lambda <Action <long> >(method, valueParameter).Compile();
                        var nextValue      = seed.GenerateId();
                        expression(nextValue);
                    }
                }
            }
        }
Esempio n. 5
0
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            switch (node.Method.Name)
            {
            case "First":
            case "FirstOrDefault":
                this.Visit(node.Arguments[0]);
                if (this.context.IsQueryResultChanged)
                {
                    var oldContext = this.context;
                    this.context = oldContext.CopyTo();
                    this.context.Fragment.FromPart.Add(new QueryTable(oldContext.WrapToFragment(), this.context.GetTableAlias(oldContext.ExportType)));
                }
                if (node.Arguments.Count == 2)
                {
                    ParseWhereExpresson(node.Arguments[1]);
                }
                this.context.Fragment.Take = 1;
                break;

            case "Last":
            case "LastOrDefault":
                this.Visit(node.Arguments[0]);
                if (this.context.IsQueryResultChanged)
                {
                    var oldContext = this.context;
                    this.context = oldContext.CopyTo();
                    this.context.Fragment.FromPart.Add(new QueryTable(oldContext.WrapToFragment(), this.context.GetTableAlias(oldContext.ExportType)));
                }
                if (node.Arguments.Count == 2)
                {
                    ParseWhereExpresson(node.Arguments[1]);
                }
                break;

            case "Where":
                this.Visit(node.Arguments[0]);
                if (this.context.IsQueryResultChanged)
                {
                    var oldContext = this.context;
                    this.context = oldContext.CopyTo();
                    this.context.Fragment.FromPart.Add(new QueryTable(oldContext.WrapToFragment(), this.context.GetTableAlias(oldContext.ExportType)));
                }
                ParseWhereExpresson(node.Arguments[1]);
                break;

            case "Select":
                this.Visit(node.Arguments[0]);
                var selectVisitor = new SelectExpressionVisitor(context);
                selectVisitor.Visit(node.Arguments[1]);
                this.context.Fragment.SelectPart = selectVisitor.Columns;
                break;

            case "Skip":
                this.Visit(node.Arguments[0]);
                this.context.Fragment.Skip = (int)(node.Arguments[1] as ConstantExpression).Value;
                break;

            case "Take":
                this.Visit(node.Arguments[0]);
                this.context.Fragment.Take = (int)(node.Arguments[1] as ConstantExpression).Value;
                break;

            case "OrderBy":
            case "OrderByDescending":
            case "ThenBy":
            case "ThenByDescending":
                this.Visit(node.Arguments[0]);
                var orderVisitor = new MemberExpressionVisitor(context);
                orderVisitor.Visit(node.Arguments[1]);
                this.context.Fragment.OrderPart.Add(new OrderCondition(orderVisitor.Column,
                                                                       node.Method.Name == "OrderBy" || node.Method.Name == "ThenBy" ? "asc" : "desc"));
                break;

            case "Any":
            case "Count":
                var parser = new QueryExpressionParser(this.connection, this.context.CopyTo());
                parser.Visit(node.Arguments[0]);
                this.context.Fragment.FromPart.Add(new QueryTable(parser.context.WrapToFragment(), this.context.GetTableAlias(parser.ExportType)));
                if (node.Arguments.Count > 1)
                {
                    ParseWhereExpresson(node.Arguments[1]);
                }
                this.context.Fragment.SelectPart.Add(new FunctionColumn()
                {
                    Formatter = "Count(*)"
                });
                break;

            case "GroupJoin":
                ParseJoinExpression(node, JoinMode.LeftJoin);
                break;

            case "Join":
                ParseJoinExpression(node, JoinMode.InnerJoin);
                break;

            case "SelectMany":
                ParseSelectManyExpression(node);
                break;

            case "GroupBy":
                ParseGroupByExpression(node);
                break;

            case "Predicate":
                if (node.Arguments[0].Type == typeof(IDbConnection))
                {
                    var elementType = node.Type.GetGenericArguments()[0];
                    this.context.ExportType = this.context.ImportType = elementType;
                    var typeMapper = TypeMapperCache.GetTypeMapper(elementType);
                    this.context.Fragment.FromPart.Add(new SingleTable(typeMapper.TableName, this.context.GetTableAlias(elementType)));
                }
                break;

            case "DefaultIfEmpty":
                break;

            default:
                break;
            }
            return(node);
        }