예제 #1
0
파일: DMS.cs 프로젝트: githuer/DMSFrame
 /// <summary>
 ///
 /// </summary>
 /// <param name="type"></param>
 /// <param name="dms"></param>
 protected DMS(Type type, DMS dms)
 {
     this.TableExpressioin   = dms.TableExpressioin;
     this.ColumnsExpressioin = dms.ColumnsExpressioin;
     this.WhereExpressioin   = dms.WhereExpressioin;
     this.OrderByExpressioin = dms.OrderByExpressioin;
     this.GroupByExpression  = dms.GroupByExpression;
     this.HavingExpression   = dms.HavingExpression;
     this.SplitExpression    = dms.SplitExpression;
     this.DataType           = dms.DataType;
     this.CurrentType        = type;
     this.Provider           = dms.Provider;
 }
예제 #2
0
        internal static DMS DMSInsertIdentity(this DMS dms, IEntity entity)
        {
            dms.ExcuteType = DMSExcuteType.INSERTIDENTITY;
            IDictionary <string, object> ChangedProperties = ((IEntity)entity).ChangedMappingProperties;

            if (ChangedProperties == null || ChangedProperties.Count == 0)
            {
                Log.Debug(ReflectionUtils.GetMethodBaseInfo(System.Reflection.MethodBase.GetCurrentMethod()), "插入字段为空,不能进行添加数据", null);
                throw new DMSFrameException("插入字段为空,不能进行添加数据");
            }
            System.Linq.Expressions.ParameterExpression yExpr      = System.Linq.Expressions.Expression.Parameter(entity.GetType(), "x");
            System.Linq.Expressions.LambdaExpression    lambdaExpr = System.Linq.Expressions.Expression.Lambda(System.Linq.Expressions.Expression.Constant(ChangedProperties), yExpr);
            dms.ColumnsExpressioin.Append(lambdaExpr);
            return(dms);
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dms"></param>
        /// <param name="dataTable"></param>
        /// <param name="tableFunc"></param>
        /// <param name="columnMapping"></param>
        /// <returns></returns>
        public static int BulkCopy <T>(this DMS <T> dms, System.Data.DataTable dataTable, Func <string, string> tableFunc = null, Dictionary <string, string> columnMapping = null)
            where T : class
        {
            string conStr = dms.Provider.ConnectionString;
            string name   = typeof(T).Name;

            if (typeof(T).IsSubclassOf(typeof(BaseEntity)))
            {
                name = typeof(T).GetEntityName();
            }
            if (tableFunc != null)
            {
                name = tableFunc(name);
            }
            return(dms.Provider.ExecuteBulkCopy(conStr, name, dataTable, columnMapping).TotalRecord);
        }
예제 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TInsert"></typeparam>
        /// <typeparam name="TSelect"></typeparam>
        /// <param name="entity"></param>
        /// <param name="whereFunc"></param>
        /// <param name="result"></param>
        /// <param name="aDataBase"></param>
        /// <param name="bDataBase"></param>
        public void AddTS <TInsert, TSelect>(Expression <Func <TSelect, TInsert> > entity, Expression <Func <TSelect, bool> > whereFunc, bool result, string aDataBase = ConstExpression.DataBase, string bDataBase = ConstExpression.DataBase)
            where TInsert : class, new()
            where TSelect : class, new()
        {
            var    dms       = new DMS <TInsert, TSelect>(aDataBase, bDataBase, ConstExpression.WithLock, ConstExpression.NeedParams, ConstExpression.NeedQueryProvider).DMSInsertSelect(entity, whereFunc);
            string resultSql = dms.GetResultSql();
            TransactionScopeEntity tEntity = new TransactionScopeEntity()
            {
                DataParameter = dms.dynamicParameters,
                ResultSql     = resultSql,
                ElementType   = typeof(TInsert),
                ResultFlag    = result,
                ExcuteType    = DMSExcuteType.INSERT_SELECT,
                EntityName    = typeof(TInsert).FullName,
            };

            changeInternalDbProvider(typeof(TInsert));
            TransactionScopeEntityList.Enqueue(tEntity);
        }
예제 #5
0
        /// <summary>
        /// 支持from语法的join写法
        /// </summary>
        /// <typeparam name="TOuter"></typeparam>
        /// <typeparam name="TInner"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="outer"></param>
        /// <param name="inner"></param>
        /// <param name="outerKeySelector"></param>
        /// <param name="innerKeySelector"></param>
        /// <param name="resultSelector"></param>
        /// <returns></returns>
        public static DMS <TResult> Join <TOuter, TInner, TKey, TResult>(this DMS <TOuter> outer,
                                                                         DMS <TInner> inner, Expression <Func <TOuter, TKey> > outerKeySelector,
                                                                         Expression <Func <TInner, TKey> > innerKeySelector, Expression <Func <TOuter, TInner, TResult> > resultSelector)
            where TInner : class
            where TOuter : class
            where TResult : class
        {
            ParameterExpression xExpr = Expression.Parameter(typeof(TOuter), "x");
            ParameterExpression yExpr = Expression.Parameter(typeof(TInner), "y");

            if (typeof(TKey).IsPrimitive())
            {
                Expression bExpr     = Expression.MakeBinary(ExpressionType.Equal, outerKeySelector.Body, innerKeySelector.Body);
                var        whereClip = Expression.Lambda <Func <TOuter, TInner, bool> >(bExpr, xExpr, yExpr);
                var        innerJoin = DMS.Create <TInner>();
                var        list      = Join(outer, innerJoin, whereClip, resultSelector);
                return(list);
            }
            else
            {
                NewExpression OutterNewExpr = outerKeySelector.Body as NewExpression;
                NewExpression InnerNewExpr  = innerKeySelector.Body as NewExpression;
                int           index         = 0;
                Expression    binaryExpr    = null;
                //进行分解得到表达式
                foreach (Expression item in OutterNewExpr.Arguments)
                {
                    var rightExpr = Expression.MakeBinary(ExpressionType.Equal, item, InnerNewExpr.Arguments[index++]);
                    if (binaryExpr == null)
                    {
                        binaryExpr = rightExpr;
                    }
                    else
                    {
                        binaryExpr = Expression.MakeBinary(ExpressionType.AndAlso, binaryExpr, rightExpr);
                    }
                }
                var whereClip = Expression.Lambda <Func <TOuter, TInner, bool> >(binaryExpr, xExpr, yExpr);
                var innerJoin = DMS.Create <TInner>();
                var list      = Join(outer, innerJoin, whereClip, resultSelector);
                return(list);
            }
        }
예제 #6
0
        internal static DMS <T> DMSInsert <T>(this DMS <T> dms, DMSExcuteType excutetype, T entity) where T : class
#endif
        {
            dms.ExcuteType = excutetype;
            if (entity is IEntity)
            {
                IDictionary <string, object> ChangedProperties = ((IEntity)entity).ChangedMappingProperties;
                if (ChangedProperties == null || ChangedProperties.Count == 0)
                {
                    Log.Debug(ReflectionUtils.GetMethodBaseInfo(System.Reflection.MethodBase.GetCurrentMethod()), "插入字段为空,不能进行添加数据", null);
                    throw new DMSFrameException("插入字段为空,不能进行添加数据");
                }
                dms.ColumnsExpressioin.Append <T, IDictionary <string, object> >(q => ChangedProperties);
            }
            else
            {
                dms.ColumnsExpressioin.Append <T, T>(q => entity);
            }
            return(dms);
        }
예제 #7
0
        internal static DMS <T> DMSEdit <T>(this DMS <T> dms, T entity, System.Linq.Expressions.Expression <Func <T, bool> > whereFunc) where T : class
#endif
        {
            dms.ExcuteType = DMSExcuteType.UPDATE;
            if (entity is IEntity)
            {
                IDictionary <string, object> ChangedProperties = ((IEntity)entity).ChangedMappingProperties;
                if (ChangedProperties == null || ChangedProperties.Count == 0)
                {
                    new DMSFrameException("插入字段为空,不能进行添加数据");
                }
                dms.ColumnsExpressioin.Append <T, IDictionary <string, object> >(q => ChangedProperties);
            }
            else
            {
                dms.ColumnsExpressioin.Append <T, T>(q => entity);
            }
            dms.WhereExpressioin.Append(whereFunc);
            return(dms);
        }
예제 #8
0
        private static void AnalyzeExpressionSelectPacker <T, TResult>(this DMS <T> dms)
            where T : class
            where TResult : class
        {
            //string key = string.Empty;
            //int tableIndex = this.TableIndex;
            dms.SplitExpression.DMSFrame = dms;
            dms.AnalyzeExpressionSelect();
            DMSTableKeys lastTableKeys = dms.TableExpressioin.TableKeys.LastOrDefault();

            dms.SplitExpression.AnalyzeExpressionSelectPacker(lastTableKeys.TableSpecialName, typeof(TResult));
            //this.splitExpt.DMSBase = this;
            //this.splitExpt.AnalyzeExpressionSelect(this.JoinFlag, this.currentType, ref tableIndex, ref key, ref TableKeys);
            ////加上当前Type
            //this.SelectKey = key;
            //this.splitExpt.AnalyzeExpressionSelectPacker(this.JoinFlag, typeof(TResult), typeof(T), ref tableIndex, ref TableKeys);
            //string str = this.splitExpt.ResultSql;
            //this.TopTableKeys = TableKeys.LastOrDefault();
            //this.TableIndex = tableIndex;
            dms.TableExpressioin.JoinFlag = false;
        }
예제 #9
0
        /// <summary>
        /// 新增一项数据
        /// </summary>
        /// <param name="entity">添加的实体参数</param>
        /// <param name="result">是否需要返回结果才继续执行,默认为true</param>
        /// <param name="bDataBase">数据库名称(默认为当前数据库)</param>
        /// <param name="tableFunc">替换表名称</param>
        public void AddTS <T>(T entity, bool result, string bDataBase = ConstExpression.DataBase, Expression <Func <Type, string> > tableFunc = null) where T : class, IEntity
        {
            DMS dms = DMS.Create(entity, entity.GetType(), bDataBase).DMSInsert(entity);

            if (tableFunc != null)
            {
                dms.TableExpressioin.ReplaceTable(tableFunc);
            }
            string resultSql = dms.GetResultSql();
            TransactionScopeEntity tEntity = new TransactionScopeEntity()
            {
                DataParameter = dms.dynamicParameters,
                ResultSql     = resultSql,
                ElementType   = entity.GetType(),
                ResultFlag    = result,
                ExcuteType    = DMSExcuteType.INSERT,
            };

            changeInternalDbProvider(entity.GetType());
            TransactionScopeEntityList.Enqueue(tEntity);
        }
예제 #10
0
        /// <summary>
        /// 编辑数据
        /// </summary>
        /// <typeparam name="T">必须是可实例化的(IEntity)类型</typeparam>
        /// <param name="entity">编辑的实体参数,必须有字段进行编辑过</param>
        /// <param name="bDataBase">数据库名称(默认为当前数据库)</param>
        /// <param name="whereFunc">编辑的条件信息</param>
        /// <param name="result">是否需要返回结果才继续执行,默认为true</param>
        /// <param name="tableFunc">替换表名称</param>
        public void EditTS <T>(T entity, Expression <Func <T, bool> > whereFunc, bool result, string bDataBase = ConstExpression.DataBase, Expression <Func <Type, string> > tableFunc = null) where T : class, IEntity
        {
            T   value = Activator.CreateInstance(typeof(T)) as T;
            var dms   = new DMS <T>(bDataBase, ConstExpression.WithLock, ConstExpression.NeedParams, ConstExpression.NeedQueryProvider).DMSEdit <T>(entity, whereFunc);

            if (tableFunc != null)
            {
                dms.ReplaceTable(tableFunc);
            }
            string resultSql = dms.GetResultSql();
            TransactionScopeEntity tEntity = new TransactionScopeEntity()
            {
                DataParameter = dms.dynamicParameters,
                ResultSql     = resultSql,
                ElementType   = entity.GetType(),
                ResultFlag    = result,
                ExcuteType    = DMSExcuteType.UPDATE,
            };

            changeInternalDbProvider(typeof(T));
            TransactionScopeEntityList.Enqueue(tEntity);
        }
예제 #11
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dms"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public static DMS <T> Pager <T>(this DMS <T> dms, int PageIndex, int PageSize) where T : class
        {
            if (PageIndex == 0 || PageSize == 0)
            {
                string errMsg = string.Format("参数不能为0,PageIndex:{0},PageSize:{1}", PageIndex, PageSize);
                Log.Debug(ReflectionUtils.GetMethodBaseInfo(System.Reflection.MethodBase.GetCurrentMethod()), errMsg, null);
                throw new DMSFrameException(errMsg);
            }
            if (PageIndex != 1 && dms.OrderByExpressioin.Expression == null)
            {
                string errMsg = "分页前必须有排序规则";
                Log.Debug(ReflectionUtils.GetMethodBaseInfo(System.Reflection.MethodBase.GetCurrentMethod()), errMsg, null);
                throw new DMSFrameException(errMsg);
            }

            dms.OrderByExpressioin.PageIndex      = PageIndex;
            dms.OrderByExpressioin.PageSize       = PageSize;
            dms.OrderByExpressioin.SplitPagerFlag = false;
            if (dms.OrderByExpressioin.PageIndex > 0 && dms.OrderByExpressioin.PageSize > 0)
            {
                dms.OrderByExpressioin.SplitPagerFlag = true;
            }
            return(dms);
        }
예제 #12
0
        internal static DMS <TResult> Join <T, T1, TGroupBy, TResult>(this DMS <T> outer, DMSJoinType JoinType, DMS <T1> inner, Expression <Func <T, T1, bool> > whereFunc,
                                                                      Expression <Func <T, T1, TResult> > selector, Expression <Func <T, T1, TGroupBy> > groupby, Expression <Func <T, T1, bool> > having)
            where T : class
            where T1 : class
            where TGroupBy : class
            where TResult : class
        {
            DMS <TResult> result = Join(outer, JoinType, inner, whereFunc, selector);

            if (groupby != null)
            {
                result.GroupByExpression.Append <T, T1, TGroupBy>(groupby);
            }
            if (having != null)
            {
                if (groupby == null)
                {
                    Log.Debug(ReflectionUtils.GetMethodBaseInfo(System.Reflection.MethodBase.GetCurrentMethod()), "分组条件必须要先有分组", null);
                    throw new DMSFrameException("分组条件必须要先有分组");
                }
                result.HavingExpression.Append <T, T1>(having);
            }
            return(result);
        }
예제 #13
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="orderby"></param>
 /// <returns></returns>
 public static DMS <T> OrderBy <T>(this DMS <T> dms, OrderByClip <T> orderby)
     where T : class
 {
     dms.OrderByExpressioin.Append <T>(orderby);
     return(dms);
 }
예제 #14
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="whereFunc"></param>
 /// <returns></returns>
 public int DeleteAsync(Expression <Func <T, bool> > whereFunc)
 {
     return(DMS.Create <T>().Delete(whereFunc));
 }
예제 #15
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="list"></param>
 /// <param name="tableFunc"></param>
 /// <param name="columnMapping"></param>
 /// <returns></returns>
 public static int BulkCopy <T>(this DMS <T> dms, IEnumerable <T> list, Func <string, string> tableFunc = null, Dictionary <string, string> columnMapping = null)
     where T : class
 {
     System.Data.DataTable dataTable = list.ToDataTable();
     return(BulkCopy <T>(dms, dataTable, tableFunc, columnMapping));
 }
예제 #16
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="havingFunc"></param>
 /// <returns></returns>
 public static DMS <T> Having <T>(this DMS <T> dms, WhereClip <T> havingFunc)
     where T : class
 {
     dms.HavingExpression.Append <T>(havingFunc);
     return(dms);
 }
예제 #17
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="groupby"></param>
 /// <returns></returns>
 public static DMS <T> GroupBy <T>(this DMS <T> dms, GroupByClip <T> groupby)
     where T : class
 {
     dms.GroupByExpression.Append <T>(groupby);
     return(dms);
 }
예제 #18
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="whereFunc"></param>
 /// <returns></returns>
 public static DMS <T> Where <T>(this DMS <T> dms, Expression <Func <T, bool> > whereFunc) where T : class
 {
     dms.WhereExpressioin.Append <T>(whereFunc);
     return(dms);
 }
예제 #19
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="whereFunc"></param>
 /// <returns></returns>
 public static DMS <T> Where <T>(this DMS <T> dms, WhereClip <T> whereFunc) where T : class
 {
     dms.WhereExpressioin.Append <T>(whereFunc);
     return(dms);
 }
예제 #20
0
 public static DMS <T> DMSInsert <T>(this DMS <T> dms, DMSExcuteType excutetype, System.Linq.Expressions.Expression <Func <T, T> > columns) where T : class
예제 #21
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="havingFunc"></param>
 /// <returns></returns>
 public static DMS <T> Having <T>(this DMS <T> dms, Expression <Func <T, bool> > havingFunc)
     where T : class
 {
     dms.HavingExpression.Append <T>(havingFunc);
     return(dms);
 }
예제 #22
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="entity"></param>
 /// <returns></returns>
 public static int Insert <T>(this DMS <T> dms, T entity) where T : class
 {
     return(DMSInsert(dms, DMSExcuteType.INSERT, entity).Execute());
 }
예제 #23
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="tableFunc"></param>
 /// <returns></returns>
 public static DMS <T> ReplaceTable <T>(this DMS <T> dms, Expression <Func <Type, string> > tableFunc)
     where T : class
 {
     dms.TableExpressioin.ReplaceTable(tableFunc);
     return(dms);
 }
예제 #24
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="columns"></param>
 /// <returns></returns>
 public static int Insert <T>(this DMS <T> dms, System.Linq.Expressions.Expression <Func <T, T> > columns) where T : class
 {
     return(DMSInsert(dms, DMSExcuteType.INSERT, columns).Execute());
 }
예제 #25
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <returns></returns>
 public static DMS <T> Distinct <T>(this DMS <T> dms) where T : class
 {
     dms.TableExpressioin.DistinctFlag = true;
     return(dms);
 }
예제 #26
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="whereFunc"></param>
 /// <returns></returns>
 public int UpdateAsync(T entity, Expression <Func <T, bool> > whereFunc)
 {
     return(DMS.Create <T>().Edit(entity, whereFunc));
 }
예제 #27
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="PageSize"></param>
 /// <returns></returns>
 public static DMS <T> Pager <T>(this DMS <T> dms, int PageSize) where T : class
 {
     return(Pager <T>(dms, 1, PageSize));
 }
예제 #28
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public int AddIdentityAsync(T entity)
 {
     return(DMS.Create <T>().InsertIdentity(entity));
 }
예제 #29
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dms"></param>
 /// <param name="orderbyFunc"></param>
 /// <returns></returns>
 public static DMS <T> OrderBy <T>(this DMS <T> dms, Expression <Func <T, T> > orderbyFunc)
     where T : class
 {
     dms.OrderByExpressioin.Append <T, T>(orderbyFunc);
     return(dms);
 }
예제 #30
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="whereFunc"></param>
 /// <returns></returns>
 public T Get(Expression <Func <T, bool> > whereFunc)
 {
     return(DMS.Create <T>().Where(whereFunc).ToEntity());
 }