Example #1
0
        private static DMSDbProvider GetProvider(DMSDbType dbType)
        {
            Type type = typeof(DMS.MssqlObject);

            if (dbType != DMSDbType.MsSql)
            {
                switch (dbType)
                {
                case DMSDbType.Access:
                    type = typeof(DMS.AccessObject);
                    break;

                case DMSDbType.Mysql:
                    type = typeof(DMS.MysqlObject);
                    break;

                case DMSDbType.Oracle:
                    type = typeof(DMS.OracleObject);
                    break;

                case DMSDbType.SQLite:
                    type = typeof(DMS.SQLiteObject);
                    break;

                default:
                    break;
                }
            }
            TableMappingAttribute attribute = DMSExpression.GetTableMappingAttribute(type);
            var provider = DMSExpression.GetDbProvider(attribute.DMSDbType, attribute.ConfigName);

            return(provider);
        }
Example #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="attribute"></param>
 /// <returns></returns>
 public static string[] GetPrimaryKey(this TableMappingAttribute attribute)
 {
     if (attribute == null || string.IsNullOrEmpty(attribute.PrimaryKey))
     {
         return(new string[] { });
     }
     return(attribute.PrimaryKey.Split(new char[] { ',' }));
 }
Example #3
0
        /// <summary>
        /// 获取类的DMSDbType名称
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DMSDbType GetDMSDbType(this Type type)
        {
            TableMappingAttribute attribute = ReflectionUtils.GetAttribute <TableMappingAttribute>(type);

            if (attribute == null)
            {
                return(DMSDbType.MsSql);
            }
            return(attribute.DMSDbType);
        }
Example #4
0
        /// <summary>
        /// 获取类的MAPPING名称
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetEntityName(this Type type)
        {
            TableMappingAttribute attribute = ReflectionUtils.GetAttribute <TableMappingAttribute>(type);

            if (attribute == null)
            {
                return(type.Name);
            }
            return(attribute.Name);
        }
Example #5
0
        internal static TableMappingAttribute GetTableMappingAttribute(Type type)
        {
            TableMappingAttribute attribute = ReflectionUtils.GetAttribute <TableMappingAttribute>(type, true, new TableMappingAttribute()
            {
                ConfigName = ConstExpression.TableConfigDefaultValue,
                Name       = type.Name,
                DMSDbType  = DMSDbType.MsSql,
                TokenFlag  = true,
            });

            return(attribute);
        }
Example #6
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="dms"></param>
        /// <param name="strSql"></param>
        /// <param name="dbParams"></param>
        /// <returns></returns>
        public static IEnumerable <TResult> Query <T, TResult>(this DMS <T> dms, string strSql, dynamic dbParams)
            where T : class
            where TResult : class
        {
            TableMappingAttribute attribute = DMSExpression.GetTableMappingAttribute(typeof(T));
            var provider = DMSExpression.GetDbProvider(attribute.DMSDbType, attribute.ConfigName);

            using (var conn = provider.GetOpenConnection())
            {
                return(DMSFrame.Access.DMSDbAccess.Query <TResult>(conn, typeof(TResult).FullName, strSql, dbParams, 0, null, true, 30));
            }
        }
Example #7
0
        /// <summary>
        /// 获取实体的MAPPING名称
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetEntityTableMappingConfigName(this Type type)
        {
            TableMappingAttribute attribute = ReflectionUtils.GetAttribute <TableMappingAttribute>(type);

            if (attribute == null || string.IsNullOrEmpty(attribute.ConfigName))
            {
                return(ConstExpression.TableConfigDefaultValue);
            }
            if (string.IsNullOrEmpty(attribute.ConfigName))
            {
                return(ConstExpression.TableConfigDefaultValue);
            }
            return(attribute.ConfigName);
        }
Example #8
0
        /// <summary>
        /// 获取实体的MAPPING名称
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static string GetEntityTableMappingName(this IEntity entity)
        {
            if (entity == null)
            {
                return(null);
            }
            TableMappingAttribute attribute = ReflectionUtils.GetAttribute <TableMappingAttribute>(entity.GetType());

            if (attribute == null)
            {
                return(entity.GetType().Name);
            }
            return(attribute.Name);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="elementType"></param>
        private void changeInternalDbProvider(Type elementType)
        {
            if (elementType == null)
            {
                return;
            }
            if (InternalDbProvider != null)
            {
                return;
            }
            TableMappingAttribute attribute = DMSExpression.GetTableMappingAttribute(elementType);
            string configName = attribute == null ? ConstExpression.TableConfigConfigName : attribute.ConfigName;

            this.InternalDbProvider = DMSExpression.GetDbProvider(attribute == null ? DMSDbType.MsSql : attribute.DMSDbType, configName);
        }
Example #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <param name="bDataBase"></param>
        /// <param name="bWithLock"></param>
        /// <param name="bNeedParams"></param>
        /// <param name="bNeedQueryProvider"></param>
        protected DMS(Type type, string bDataBase, bool bWithLock, bool bNeedParams, bool bNeedQueryProvider)
        {
            DMSFrameException.ThrowIfNull(type);

            this.DataType    = type;
            this.CurrentType = type;
            TableMappingAttribute attribute = DMSExpression.GetTableMappingAttribute(type);

            this.TableExpressioin   = DMSExpression.GetTableExpression(attribute.DMSDbType);
            this.ColumnsExpressioin = DMSExpression.GetColumnsExpression(attribute.DMSDbType);
            this.WhereExpressioin   = DMSExpression.GetWhereExpression(attribute.DMSDbType);
            this.OrderByExpressioin = DMSExpression.GetOrderByExpression(attribute.DMSDbType);
            this.GroupByExpression  = DMSExpression.GetGroupByExpression(attribute.DMSDbType);
            this.HavingExpression   = DMSExpression.GetHavingExpression(attribute.DMSDbType);
            this.SplitExpression    = DMSExpression.GetSplitExpression(attribute);
            this.Provider           = DMSExpression.GetDbProvider(attribute.DMSDbType, attribute.ConfigName);

            this.dynamicParameters = new DynamicParameters();

            DMSFrameException.ThrowIfNull(this.TableExpressioin, this.ColumnsExpressioin, this.WhereExpressioin, this.OrderByExpressioin, this.GroupByExpression, this.HavingExpression);

            if (!string.IsNullOrEmpty(bDataBase))
            {
                this.TableExpressioin.bDataBase = bDataBase;
                DMSDataBase myDb = new DMSDataBase(bDataBase);
                this.TableExpressioin.Append <DMSDataBase, DMSDataBase>(q => myDb);
            }
            this.TableExpressioin.WithLock = bWithLock;
            this.ExcuteType = DMSExcuteType.SELECT;

            this.TableExpressioin.NeedParams   = bNeedParams;
            this.ColumnsExpressioin.NeedParams = bNeedParams;
            this.WhereExpressioin.NeedParams   = bNeedParams;
            this.OrderByExpressioin.NeedParams = bNeedParams;
            this.GroupByExpression.NeedParams  = bNeedParams;
            this.HavingExpression.NeedParams   = bNeedParams;


            this.TableExpressioin.SplitExpression   = this.SplitExpression;
            this.ColumnsExpressioin.SplitExpression = this.SplitExpression;
            this.WhereExpressioin.SplitExpression   = this.SplitExpression;
            this.OrderByExpressioin.SplitExpression = this.SplitExpression;
            this.GroupByExpression.SplitExpression  = this.SplitExpression;
            this.HavingExpression.SplitExpression   = this.SplitExpression;
        }
Example #11
0
        internal static DMSSplitExpressionVistor GetSplitExpression(TableMappingAttribute attribute)
        {
            DMSSplitExpressionVistor result;

            switch (attribute.DMSDbType)
            {
            case DMSDbType.MsSql:
            default:
                result = new DMSSplitMssqlExpressionVistor();
                break;

            case DMSDbType.Mysql:
                result = new DMSFrame.Visitor.Mysql.DMSSplitMysqlExpressionVistor();
                break;
            }
            if (result != null)
            {
                result.TableMapping = attribute;
            }
            return(result);
        }
Example #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string[] GetPrimaryKey(this Type type)
        {
            TableMappingAttribute attribute = ReflectionUtils.GetAttribute <TableMappingAttribute>(type);

            return(GetPrimaryKey(attribute));
        }
Example #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="modify"></param>
        /// <param name="genericType"></param>
        /// <param name="expr"></param>
        protected virtual void AppendColumnExpression(bool modify, Type genericType, Expression expr)
        {
            if (expr == null)
            {
                return;
            }
            NewArrayExpression newArrayExpression2 = null;

            if (this.ExcuteType == DMSExcuteType.SELECT)
            {
                if (expr.Type.IsPrimitive())
                {
                    if (expr is MemberExpression)
                    {
                        //q.Column
                        newArrayExpression2 = InitializerExpression(newArrayExpression2, expr as MemberExpression);
                    }
                    else if (expr is MethodCallExpression)
                    {
                        //q.Column.Desc()
                        MethodCallExpression methodCallExpression = expr as MethodCallExpression;
                        if (expr != null)
                        {
                            Expression convertExpr = Expression.MakeUnary(ExpressionType.Convert, expr, typeof(object));
                            newArrayExpression2 = Expression.NewArrayInit(typeof(object), convertExpr);
                        }
                    }
                    else if (expr is ConstantExpression)
                    {
                        //q=>*
                        var convertExpr = Expression.MakeUnary(ExpressionType.Convert, expr, typeof(object));
                        if (newArrayExpression2 == null)
                        {
                            newArrayExpression2 = Expression.NewArrayInit(typeof(object), convertExpr);
                        }
                        else
                        {
                            NewArrayExpression       newArrayExpression3 = Expression.NewArrayInit(typeof(object), convertExpr);
                            IEnumerable <Expression> initializers        = newArrayExpression2.Expressions.Concat(newArrayExpression3.Expressions);
                            newArrayExpression2 = Expression.NewArrayInit(typeof(object), initializers);
                        }
                    }
                }
                else if (expr is ParameterExpression)
                {
                    // q => q;

                    if (newArrayExpressionCache.ContainsKey(expr.Type.AssemblyQualifiedName))
                    {
                        newArrayExpression2 = newArrayExpressionCache[expr.Type.AssemblyQualifiedName];
                    }
                    else
                    {
                        NewArrayExpression  newArrayExpression3 = null;
                        ParameterExpression paramExpr           = expr as ParameterExpression;
                        PropertyInfo[]      propertyInfos       = expr.Type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                        foreach (PropertyInfo item in propertyInfos)
                        {
                            MemberExpression memberExpr = Expression.Property(paramExpr, item);
                            newArrayExpression3 = InitializerExpression(newArrayExpression3, memberExpr);
                        }
                        lock (newArrayExpressionCache)
                        {
                            if (!newArrayExpressionCache.ContainsKey(expr.Type.AssemblyQualifiedName))
                            {
                                newArrayExpressionCache.Add(expr.Type.AssemblyQualifiedName, newArrayExpression3);
                            }
                        }
                        newArrayExpression2 = newArrayExpression3;
                    }
                }

                else if (expr is NewExpression)
                {
                    //q=> new {}
                    Expression convertExpr = Expression.MakeUnary(ExpressionType.Convert, expr, typeof(object));
                    newArrayExpression2 = Expression.NewArrayInit(typeof(object), convertExpr);
                }
                else if (expr is MethodCallExpression)
                {
                    // q => q.Columns(q.Column0,q.Column1)
                    // q => q.NewGuid()
                    MethodCallExpression methodCallExpression = expr as MethodCallExpression;
                    if (DMSExpression.ColumnsFuncString.Contains(methodCallExpression.Method.Name))
                    {
                        newArrayExpression2 = Expression.NewArrayInit(typeof(object), methodCallExpression);
                    }
                    else if (DMSExpression.ArrayInitFuncString.Contains(methodCallExpression.Method.Name))
                    {
                        if (expr != null)
                        {
                            newArrayExpression2 = methodCallExpression.Arguments[1] as NewArrayExpression;
                        }
                    }
                    else
                    {
                        Log.Debug(ReflectionUtils.GetMethodBaseInfo(System.Reflection.MethodBase.GetCurrentMethod()), "no supported " + methodCallExpression.Method.Name, null);
                        throw new NotSupportedException("no supported " + methodCallExpression.Method.Name);
                    }
                }
                else if (expr is LambdaExpression)
                {
                }
                else if (expr is NewArrayExpression)
                {
                    newArrayExpression2 = (NewArrayExpression)expr;
                }
            }
            else if (this.ExcuteType == DMSExcuteType.UPDATE ||
                     this.ExcuteType == DMSExcuteType.INSERT ||
                     this.ExcuteType == DMSExcuteType.INSERTIDENTITY ||
                     this.ExcuteType == DMSExcuteType.INSERT_SELECT ||
                     this.ExcuteType == DMSExcuteType.UPDATE_WHERE)
            {
                if (expr is ConstantExpression)
                {
                    ConstantExpression consExpr = expr as ConstantExpression;
                    Type elementType            = consExpr.Value != null?consExpr.Value.GetType() : consExpr.Type;

                    if (elementType == typeof(IDictionary <string, object>) || elementType == typeof(Dictionary <string, object>))
                    {
                        TableMappingAttribute attribute = GetTableMappingAttribute(genericType);
                        string[] primaryKeys            = attribute.GetPrimaryKey();
                        foreach (KeyValuePair <string, object> item in (IDictionary <string, object>)consExpr.Value)
                        {
                            if (item.Value == null)
                            {
                                Log.Debug(ReflectionUtils.GetMethodBaseInfo(System.Reflection.MethodBase.GetCurrentMethod()), string.Format("{0}不能为空值", item.Key), null);
                                throw new DMSFrameException(string.Format("{0}不能为空值", item.Key));
                            }
                            PropertyInfo propertyInfo = genericType.GetProperty(item.Key, BindingFlags.Instance | BindingFlags.Public);
                            this.AppendColumnExpression(genericType, primaryKeys, item.Key, item.Value, propertyInfo, ref newArrayExpression2);
                        }
                    }
                    else if (elementType.IsClass)
                    {
                        TableMappingAttribute attribute = GetTableMappingAttribute(elementType);
                        string[] primaryKeys            = attribute.GetPrimaryKey();
                        foreach (System.Reflection.PropertyInfo item in elementType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                        {
                            object itemValue = item.GetValue(consExpr.Value, null);
                            if (itemValue != null)
                            {
                                this.AppendColumnExpression(elementType, primaryKeys, item.Name, itemValue, item, ref newArrayExpression2);
                            }
                        }
                    }
                }
                else if (expr is MemberInitExpression)
                {
                    MemberInitExpression expr0 = (MemberInitExpression)expr;

                    Expression convertExpr = Expression.MakeUnary(ExpressionType.Convert, expr0, expr.Type);
                    newArrayExpression2 = Expression.NewArrayInit(expr.Type, convertExpr);
                }
            }
            if (newArrayExpression2 != null)
            {
                if (this.Expression != null)
                {
                    //合并之前
                    NewArrayExpression       newArrayExpression = this.Expression as NewArrayExpression;
                    IEnumerable <Expression> initializers       = newArrayExpression.Expressions.Concat(newArrayExpression2.Expressions);
                    newArrayExpression2 = Expression.NewArrayInit(typeof(object), initializers);
                }
                if (modify)
                {
                    this.Expression = this.Modify(newArrayExpression2);
                }
                else
                {
                    this.Expression = newArrayExpression2;
                }
            }
        }