コード例 #1
0
ファイル: sql.cs プロジェクト: nttlong/SqlBuilder
        public Sql <T> Where(Expression <Func <T, bool> > Expr)
        {
            Sql <T> ret = BaseSql.Clone <T, T>(this);

            if (!this.IsSubQuery)
            {
                if (this.SelectedFields != null && this.SelectedFields.Count > 0)
                {
                    if (ret.SelectedFields == null)
                    {
                        ret.SelectedFields = new List <TreeExpr>();
                    }
                    ret.SelectedFields.AddRange(this.SelectedFields.Select(p => p.Clone()));
                }
                if (((BaseSql)ret).filter == null)
                {
                    ((BaseSql)ret).filter = ExprCompiler.Compile(((BaseSql)ret), Expr.Body, null);
                }
                else
                {
                    ((BaseSql)ret).filter = new TreeExpr
                    {
                        Op    = ExprCompiler.GetOp(ExpressionType.And),
                        Left  = ((BaseSql)ret).filter,
                        Right = ExprCompiler.Compile(((BaseSql)ret), Expr.Body, ((MemberExpression)Expr.Body).Member)
                    };
                }
                return(ret);
            }
            else
            {
                ret.AliasCount = this.AliasCount + 1;
                ret.Alias      = "sql" + ret.AliasCount;
                ret.MapFields.ForEach(p => {
                    if (p.TableName == null)
                    {
                        p.TableName = ret.Alias;
                    }
                });
                ret.source = new ExprDataSource
                {
                    Alias     = ret.Alias,
                    Fields    = this.SelectedFields.Select(p => p.Clone()).ToList(),
                    Schema    = this.schema,
                    Table     = this.table,
                    Source    = this.source,
                    ParamExpr = Expression.Parameter(ret.ElementType, "p")
                };
                if (this.filter != null)
                {
                    ret.source.filter = this.filter.Clone();
                }
                ret.filter = ExprCompiler.Compile(((BaseSql)ret), Expr.Body, null);
                return(ret);
            }
        }
コード例 #2
0
ファイル: sql.cs プロジェクト: nttlong/SqlBuilder
        public Sql <T2> Join <T1, T2>(Sql <T1> sql, Expression <Func <T, T1, bool> > ExprJoin, Expression <Func <T, T1, T2> > Selector)
        {
            if ((!this.IsSubQuery) && (!sql.IsSubQuery))
            {
                var ret = new Sql <T2>();

                ret.MapFields = new List <MapFieldInfo>();
                this.AliasCount++;
                sql.AliasCount++;
                ret.AliasCount++;
                var leftAlias  = "l" + ret.AliasCount + "" + (this.AliasCount);
                var rightAlias = "r" + ret.AliasCount + "" + (sql.AliasCount);

                this.MapFields.ForEach(p =>
                {
                    var P = p.Clone();
                    if (!ExprCompiler.IsPrimitiveType(((PropertyInfo)p.Member).PropertyType))
                    {
                        P.Name = null;
                    }
                    P.SetTableName(leftAlias);
                    P.ParamExpr = ExprJoin.Parameters[0];
                    ret.MapFields.Add(P);
                });
                sql.MapFields.ForEach(p =>
                {
                    var P = p.Clone();
                    if (!ExprCompiler.IsPrimitiveType(((PropertyInfo)p.Member).PropertyType))
                    {
                        P.Name = null;
                    }
                    P.SetTableName(rightAlias);
                    P.ParamExpr = ExprJoin.Parameters[1];
                    ret.MapFields.Add(P);
                });
                if (Selector.Body is NewExpression)
                {
                    var nx = Selector.Body as NewExpression;
                    if (nx.Members != null)
                    {
                        nx.Members.ToList().ForEach(p =>
                        {
                            var fx = nx.Arguments[nx.Members.IndexOf(p)];
                            var mp = ret.MapFields.FirstOrDefault(x => x.Member == p && x.ParamExpr == fx);
                            if (mp == null)
                            {
                                mp = ret.MapFields.FirstOrDefault(x => x.Member == p);
                            }
                            ret.MapFields.Add(new MapFieldInfo()
                            {
                                Alias     = (mp != null) ? mp.Alias : null,
                                AliasName = (mp != null) ? mp.AliasName : null,
                                ExprField = (mp != null) ? mp.ExprField.Clone() : null,
                                Member    = p as PropertyInfo,
                                Name      = (mp != null) ? mp.Name : p.Name
                            });
                        });
                    }
                }
                var JoinExpr = ExprCompiler.Compile(ret, ExprJoin.Body, null);
                JoinExpr.ClearAliasName();
                ret.source = new ExprDataSource
                {
                    JoinType   = "inner",
                    JoinExpr   = JoinExpr,
                    LeftSource = new ExprDataSource
                    {
                        Alias     = leftAlias,
                        Schema    = this.schema,
                        Table     = this.table,
                        ParamExpr = Expression.Parameter(this.ElementType, "p"),
                        Source    = (this.source != null)?this.source.Clone():null
                    },
                    RightSource = new ExprDataSource
                    {
                        Alias     = rightAlias,
                        Schema    = sql.schema,
                        Table     = sql.table,
                        ParamExpr = Expression.Parameter(sql.ElementType, "p"),
                        Source    = (sql.source != null) ? sql.source.Clone() : null
                    }
                };
                if (Selector.Body is NewExpression)
                {
                    ret.SelectedFields = ExprCompiler.GetSelectedFieldsFromNewExpression(ret, (NewExpression)Selector.Body);
                }
                else if (Selector.Body is MemberInitExpression)
                {
                    ret.SelectedFields = ExprCompiler.GetSelectedFieldsFromMemberInitExpression(ret, (MemberInitExpression)Selector.Body);
                }
                else if (Selector.Body is ParameterExpression)
                {
                    if (ret.SelectedFields == null)
                    {
                        ret.SelectedFields = new List <TreeExpr>();
                    }
                    if (Selector.Parameters[0] == Selector.Body)
                    {
                        ret.SelectedFields.Add(new TreeExpr
                        {
                            Field = new FieldExpr
                            {
                                TableName = leftAlias
                            }
                        });
                    }
                    else if (Selector.Body == Selector.Parameters[1])
                    {
                        ret.SelectedFields.Add(new TreeExpr
                        {
                            Field = new FieldExpr
                            {
                                TableName = rightAlias
                            }
                        });
                    }
                }
                else
                {
                    throw new NotSupportedException();
                }
                if (this.filter != null)
                {
                    ret.filter = this.filter.Clone();
                    ret.filter.SetTableName(leftAlias);
                }
                if (sql.filter != null)
                {
                    var rFilter = sql.filter.Clone();
                    rFilter.SetTableName(rightAlias);
                    sql.filter = new TreeExpr
                    {
                        Op    = ExprCompiler.GetOp(ExpressionType.And),
                        Left  = sql.filter,
                        Right = rFilter
                    };
                }
                ret.SelectedFields.Join(ret.MapFields, p => p.AliasName, q => q.Name, (p, q) => new { p, q })
                .ToList().ForEach(F =>
                {
                    F.q.ExprField = F.p.Clone();
                });
                return(ret);
            }
            throw new NotImplementedException();
        }
コード例 #3
0
ファイル: ExprCompiler.cs プロジェクト: nttlong/SqlBuilder
        public static IQueryable <T> DoInnerJoin <T>(BaseSql qr1, BaseSql qr2, MemberExpression leftKey, MemberExpression rightKey, Expression selector)
        {
            var ret       = new Sql <T>(true);
            var leftName  = "l" + ret.AliasCount + "" + qr1.AliasCount + "" + qr2.AliasCount;
            var rightName = "r" + ret.AliasCount + "" + qr1.AliasCount + "" + qr2.AliasCount;

            if (!(qr1.IsSubQuery) && (!qr2.IsSubQuery))
            {
                var leftMp  = qr1.MapFields.FirstOrDefault(p => p.Member == leftKey.Member);
                var rightMp = qr2.MapFields.FirstOrDefault(p => p.Member == rightKey.Member);
                ret.source = new ExprDataSource
                {
                    JoinType   = "inner",
                    LeftSource = new ExprDataSource
                    {
                        Alias  = leftName,
                        Schema = qr1.schema,
                        Table  = qr1.table
                    },
                    RightSource = new ExprDataSource
                    {
                        Alias  = rightName,
                        Schema = qr2.schema,
                        Table  = qr2.table
                    },
                    JoinExpr = new TreeExpr
                    {
                        Op   = ExprCompiler.GetOp(ExpressionType.Equal),
                        Left = new TreeExpr
                        {
                            Field = new FieldExpr
                            {
                                TableName = leftName,
                                Name      = leftMp.Name
                            }
                        },
                        Right = new TreeExpr
                        {
                            Field = new FieldExpr
                            {
                                TableName = rightName,
                                Name      = rightMp.Name
                            }
                        }
                    }
                };
                return(ret as IQueryable <T>);
            }
            if ((qr1.IsSubQuery) && (!qr2.IsSubQuery))
            {
                var sql1    = new BaseSql();
                var leftMp  = qr1.MapFields.FirstOrDefault(p => p.Member == leftKey.Member);
                var rightMp = qr2.MapFields.FirstOrDefault(p => p.Member == rightKey.Member);
                ret.source = new ExprDataSource
                {
                    JoinType   = "inner",
                    LeftSource = new ExprDataSource
                    {
                        Source = new ExprDataSource
                        {
                            Alias        = qr1.Alias,
                            Fields       = (qr1.SelectedFields != null)?qr1.SelectedFields.Select(p => p.Clone()).ToList():null,
                            filter       = (qr1.filter != null)?qr1.filter.Clone():null,
                            GroupFields  = (qr1.GroupByFields != null)?qr1.GroupByFields.Select(p => p.Clone()).ToList():null,
                            HavingFields = (qr1.HavingFields != null)?qr1.HavingFields.Select(p => p.Clone()).ToList():null,
                            Schema       = qr1.schema,
                            Table        = qr1.table,
                            JoinExpr     = (qr1.source != null)?qr1.source.JoinExpr:null,
                            JoinType     = (qr1.source != null) ? qr1.source.JoinType : null,
                            LeftSource   = (qr1.source != null) ? qr1.source.LeftSource : null,
                            RightSource  = (qr1.source != null) ? qr1.source.RightSource : null,
                            ParamExpr    = qr1.ParamExpr
                        }
                    },
                    RightSource = new ExprDataSource
                    {
                        Alias  = rightName,
                        Schema = qr2.schema,
                        Table  = qr2.table
                    },
                    JoinExpr = new TreeExpr
                    {
                        Op   = ExprCompiler.GetOp(ExpressionType.Equal),
                        Left = new TreeExpr
                        {
                            Field = new FieldExpr
                            {
                                TableName = leftName,
                                Name      = leftMp.Name
                            }
                        },
                        Right = new TreeExpr
                        {
                            Field = new FieldExpr
                            {
                                TableName = rightName,
                                Name      = rightMp.Name
                            }
                        }
                    }
                };
                if (ret.source.LeftSource.Fields == null ||
                    ret.source.Fields.Count == 0)
                {
                    ret.source.Fields = new List <TreeExpr>();
                    ret.source.Fields.Add(new TreeExpr
                    {
                        Field = new FieldExpr
                        {
                            TableName = qr1.table
                        }
                    });
                }
                return(ret as IQueryable <T>);
            }
            throw new NotSupportedException();
        }