Пример #1
0
        public static bool CheckProcedureChanged(IEntitySchema view, ProcedureUsage usage, DbConnectionProvider connectionProvider)
        {
            if (view == null)
            {
                throw new ArgumentNullException(nameof(view));
            }
            if (connectionProvider == null)
            {
                throw new ArgumentNullException(nameof(connectionProvider));
            }
            if (connectionProvider.AccessLevel == AccessLevel.ReadOnly)
            {
                throw new InvalidOperationException("DbProvider key:" + view.ConnectKey + ", read only!");
            }

            if (ProcedureExists(view, usage, connectionProvider))
            {
                var commandText = GetProcedureCommand(view, ProcedureOperate.Alter, usage);
                connectionProvider.ExecuteNonQuery(commandText);
            }
            else
            {
                var commandText = GetProcedureCommand(view, ProcedureOperate.Create, usage);
                connectionProvider.ExecuteNonQuery(commandText);
            }
            return(true);
        }
 private static void Add(IEntitySchema es)
 {
     _schemaIdDict[es.EntityId]     = es;
     _schemaNameDict[es.EntityName] = es;
     _schemaTypeDict[es.EntityType] = es;
     TableCache.AppendTableType(es.EntityName, es.EntityType);
 }
Пример #3
0
        public static bool CheckTypeChanged(IEntitySchema view, DbConnectionProvider connectionProvider)
        {
            if (view == null)
            {
                throw new ArgumentNullException(nameof(view));
            }
            if (connectionProvider == null)
            {
                throw new ArgumentNullException(nameof(connectionProvider));
            }
            if (connectionProvider.AccessLevel == AccessLevel.ReadOnly)
            {
                throw new InvalidOperationException("DbProvider key:" + view.ConnectKey + ", read only!");
            }

            if (view.Attributes.HasFlag(EntitySchemaAttributes.CreateType))
            {
                if (TypeExists(view.Name, connectionProvider))
                {
                    var commandText = string.Format("Drop Type [dbo].[{0}Type]", view.Name);
                    connectionProvider.ExecuteNonQuery(commandText);

                    commandText = GetCreateTypeCommand(view);
                    connectionProvider.ExecuteNonQuery(commandText);
                }
                else
                {
                    var commandText = GetCreateTypeCommand(view);
                    connectionProvider.ExecuteNonQuery(commandText);
                }
            }

            return(true);
        }
Пример #4
0
 private void IfCascadeDelete(string sourceEntity, ReferencedObject refObj, object refValue, Dictionary <string, DeleteInfo> deleteEntitys, Action <string, object, object> action)
 {
     if (refObj.DeleteFlag == DeleteFlag.CascadeDelete)
     {
         string        name   = refObj.EntityType.Name;
         IEntitySchema schema = IEntitySchemaHelper.Get(name);
         IList         list   = this.FindReferenceds(name, refObj, refValue);
         foreach (object obj2 in list)
         {
             object     keyValue = schema.GetKeyValue(obj2);
             DeleteInfo info     = new DeleteInfo {
                 Id           = keyValue,
                 EntitySchema = schema,
                 RefObject    = refObj
             };
             Console.WriteLine(string.Format("找到需要删除的数据:{0}", info.Key));
             if (deleteEntitys["current_delete_entity"].Equals(info))
             {
                 Console.WriteLine("父对象,不添加");
             }
             else if (deleteEntitys.ContainsKey(info.Key))
             {
                 Console.WriteLine("已存在,不添加");
             }
             else
             {
                 deleteEntitys.Add(info.Key, info);
                 Console.WriteLine("添加");
                 action(name, obj2, keyValue);
             }
         }
     }
 }
Пример #5
0
 private void IfSetNull(string sourceEntity, ReferencedObject refObj, object refValue, Dictionary <string, DeleteInfo> setNullEntitys, Action <string, object, object> action)
 {
     if (refObj.DeleteFlag == DeleteFlag.SetNull)
     {
         string        name   = refObj.EntityType.Name;
         IEntitySchema schema = IEntitySchemaHelper.Get(name);
         IList         list   = this.FindReferenceds(name, refObj, refValue);
         foreach (object obj2 in list)
         {
             object     keyValue = schema.GetKeyValue(obj2);
             DeleteInfo info     = new DeleteInfo {
                 Id           = keyValue,
                 EntitySchema = schema,
                 RefObject    = refObj
             };
             Console.WriteLine(string.Format("找到置为空的数据:{0}", info.Key));
             if (setNullEntitys.ContainsKey(info.Key))
             {
                 Console.WriteLine("已存在,不添加");
             }
             else
             {
                 setNullEntitys.Add(info.Key, info);
                 Console.WriteLine("添加");
             }
         }
     }
 }
Пример #6
0
        public void MMDelete(Type entityType, int parentId, string parentEntityName, List <object> childIds)
        {
            IOperationManager opm     = new DefaultOperationManager(this._context, this._currentUser);
            IEntitySchema     es      = IEntitySchemaHelper.Get(entityType);
            IEntitySchema     schema2 = IEntitySchemaHelper.Get(parentEntityName);
            object            entity  = this._context.FindById(schema2.EntityType, new object[] { parentId });

            if (entity == null)
            {
                throw new DeleteException(es, parentId, string.Format("父实体[{0}]记录不存在", parentEntityName));
            }
            opm.AUD_OperationCheck(schema2, entity, EntityOperationEnum.Update);
            string        str        = string.Format("{0}s", es.EntityName);
            string        entityName = schema2.MmTables[str];
            IEntitySchema schema3    = IEntitySchemaHelper.Get(entityName);

            using (TransactionScope scope = new TransactionScope())
            {
                foreach (object obj3 in childIds)
                {
                    string condition = string.Format("{0} = {1} and {2} = {3}", new object[] { es.KeyName, obj3, schema2.KeyName, parentId });
                    IList  list      = this._context.Where(schema3.EntityType, condition, new DbParameter[0]);
                    foreach (object obj4 in list)
                    {
                        this._context.Delete((dynamic)obj4);
                    }
                }
                scope.Complete();
            }
        }
Пример #7
0
        private DataTable GetSqlBulkTable(IEntitySchema entitySchema, RowEntry[] rowEntries)
        {
            Type mappingType;
            var  dataTable = new DataTable();
            var  columns   = entitySchema.Columns.Where(p => !p.IsIdentity).ToArray();

            foreach (var column in columns)
            {
                mappingType = GetColumnMappingType(column);
                dataTable.Columns.Add(column.Name, mappingType);
            }

            object  row;
            object? value;
            DataRow dataRow;

            foreach (var rowEntry in rowEntries)
            {
                row     = rowEntry.Value;
                dataRow = dataTable.NewRow();
                foreach (var column in columns)
                {
                    value = column.GetValue(row);
                    dataRow[column.Name] = value ?? DBNull.Value;
                }

                dataTable.Rows.Add(dataRow);
            }
            return(dataTable);
        }
Пример #8
0
        public void Update(object entity)
        {
            IEntitySchema     es  = IEntitySchemaHelper.Get(entity.GetType());
            IOperationManager opm = new DefaultOperationManager(this._context, this._currentUser);
            List <string>     modifiedPropertys = (from p in es.PropertyTypes select p.Key).ToList <string>();

            opm.AUD_OperationCheck(es, entity, EntityOperationEnum.Update);
            this.UpdateActionUniqueKeyCheck(es, entity, modifiedPropertys);
            if (es.PrivilegeModel() == PrivilegeModel.Persional)
            {
                entity.SetPropertyValue("UpdateTime", DateTime.Now, null);
                entity.SetPropertyValue("UpdateUserId", opm.CurrentUser.User_ID, null);
            }
            if (es.EntityName == T_AttachmentTable)
            {
                this._attachHandler.UpdateAttachment(entity as T_Attachment);
            }
            else
            {
                this.AutoUpdateSystemLevelCode(es, entity);
                this.UpdateTableVersion(es, entity);
                this.UpdateEntityChangeLog(es, entity, modifiedPropertys);
                this._context.Update(entity);
                this.SaveAttachmentOwner(es, entity);
            }
        }
Пример #9
0
        internal void Delete(int id, string entityName)
        {
            IEntitySchema es     = IEntitySchemaHelper.Get(entityName);
            object        entity = es.CreateInstance();

            entity.SetPropertyValue(es.KeyName, id);
            this.GenericHelper.Delete(entity);
        }
Пример #10
0
 public static Type GetPropertyType(this IEntitySchema es, string pname)
 {
     if (!es.PropertyTypes.ContainsKey(pname))
     {
         throw new Exception(string.Format("{0} 不是{1} 的有效属性", pname, es.EntityType));
     }
     return(es.PropertyTypes[pname]);
 }
Пример #11
0
        /// <summary>
        /// 从管理器中移除指定实体架构信息
        /// </summary>
        /// <param name="schema"></param>
        /// <returns></returns>
        public static bool Remove(IEntitySchema schema)
        {
            if (schema == null)
            {
                throw new ArgumentNullException(nameof(schema));
            }

            return(entitySchemas.TryRemove(schema.EntityType.FullName, out _));
        }
Пример #12
0
 public object GetEntity(string entityName, int id)
 {
     if (!(string.IsNullOrEmpty(entityName) || (id == 0)))
     {
         IEntitySchema schema = IEntitySchemaHelper.Get(entityName);
         return(this._context.FindById(schema.EntityType, new object[] { id }));
     }
     return(null);
 }
Пример #13
0
        private static bool ProcedureExists(IEntitySchema view, ProcedureUsage usage, DbConnectionProvider connectionProvider)
        {
            var proceuredName = usage == ProcedureUsage.Insert || usage == ProcedureUsage.Update
                ? string.Format("{0}{1}", usage.ToString(), view.Name)
                : string.Format("{0}{1}Type", usage.ToString().TrimStart("Type".ToArray()), view.Name);
            var strComm = string.Format("Select * From sysobjects Where [xtype] = 'p' And [name] = '{0}'", proceuredName);

            return(connectionProvider.ExecuteLines(strComm) > 0);
        }
Пример #14
0
        /// <summary>
        /// 尝试获取指定实体架构信息
        /// </summary>
        /// <param name="entityType">欲获取架构信息的实体类型</param>
        /// <param name="schema">如果指定实体架构信息存在,则返回,否则返回null。</param>
        /// <returns></returns>
        public static bool TryGetSchema(Type entityType, out IEntitySchema schema)
        {
            if (entityType == null)
            {
                throw new ArgumentNullException(nameof(entityType));
            }

            return(TryGetSchema(entityType.FullName, out schema));
        }
Пример #15
0
        private static int TryGetEntityOwnerId(IEntitySchema es, object entity, string operationName)
        {
            object obj2 = entity.GetPropertyValue("OwnerId", null);

            if (obj2 == null)
            {
                throw new ApplicationException(string.Format("当前{1}的数据 {0} 的 OwnerId 不能为空", es.EntityName, operationName.Replace("权限", "")));
            }
            return(Convert.ToInt32(obj2));
        }
Пример #16
0
        private object GetOldEntity(IEntitySchema es, object entity)
        {
            int id = entity.GetPropertyValue(es.KeyName, null).ToInt();

            if (es.EntityName == T_AttachmentTable)
            {
                return(this._attachHandler.GetAttachment(id));
            }
            return(this._context.FindById(es.EntityType, new object[] { id }));
        }
Пример #17
0
 private void SetDefaultValues(IEntitySchema es, object entity)
 {
     foreach (KeyValuePair <string, string> pair in es.DefaultValues)
     {
         if (entity.GetPropertyValue(pair.Key, null) == null)
         {
             entity.SetPropertyValue(pair.Key, pair.Value, null);
         }
     }
 }
Пример #18
0
        public static string Q_OperationCheck(this IOperationManager opm, IEntitySchema es, int objectId)
        {
            List <int>    userIdByDeptId;
            string        str2;
            IUserIdentity currentUser = opm.CurrentUser;

            if (currentUser == null)
            {
                throw new ApplicationException("登录用户为空,无法判断权限");
            }
            EntityPrivilegeEnum enum2 = opm.TryCanOperation(currentUser.User_ID, es.EntityId, EntityOperationEnum.Query);
            string str = "";

            if ((enum2 == EntityPrivilegeEnum.NoPermission) && !opm.CheckHasSharedPrivilege(objectId, es.EntityName, EntityOperationEnum.Query))
            {
                throw new ApplicationException(string.Format("当前登录的用户没有对 {0} 的查询权限", es.EntityName));
            }
            if (es.PrivilegeModel() == King.Framework.EntityLibrary.PrivilegeModel.Organization)
            {
                if ((enum2 != EntityPrivilegeEnum.AllRights) && !opm.CheckHasSharedPrivilege(objectId, es.EntityName, EntityOperationEnum.Query))
                {
                    throw new ApplicationException(string.Format("当前登录的用户没有对 {0} 的查询权限", es.EntityName));
                }
                return("");
            }
            if ((enum2 == EntityPrivilegeEnum.Personal) && !opm.CheckHasSharedPrivilege(objectId, es.EntityName, EntityOperationEnum.Query))
            {
                str = string.Format(" ( it.OwnerId = {0} )", currentUser.User_ID);
            }
            if ((enum2 == EntityPrivilegeEnum.Department) && !opm.CheckHasSharedPrivilege(objectId, es.EntityName, EntityOperationEnum.Query))
            {
                userIdByDeptId = opm.GetUserIdByDeptId(new int[] { currentUser.Department_ID.Value });
                if (userIdByDeptId.Count == 0)
                {
                    userIdByDeptId.Add(-2147483647);
                }
                str2 = string.Join <int>(" , ", userIdByDeptId);
                str2 = "{ " + str2 + " }";
                str  = string.Format(" ( it.OwnerId in {0} )", str2);
            }
            if ((enum2 == EntityPrivilegeEnum.DepartmentAndSubSector) && !opm.CheckHasSharedPrivilege(objectId, es.EntityName, EntityOperationEnum.Query))
            {
                List <int> subDeptIds = opm.GetSubDeptIds(currentUser.Department_ID);
                subDeptIds.Add(currentUser.Department_ID.Value);
                userIdByDeptId = opm.GetUserIdByDeptId(subDeptIds.ToArray());
                if (userIdByDeptId.Count == 0)
                {
                    userIdByDeptId.Add(-2147483647);
                }
                str2 = string.Join <int>(" , ", userIdByDeptId);
                str2 = "{ " + str2 + " }";
                str  = string.Format(" ( it.OwnerId in {0} )", str2);
            }
            return(str);
        }
Пример #19
0
        static public void CopyTo(this EntityColumn it, IEntitySchema columnDef)
        {
            var attris = it.GetAttributesEntity();

            columnDef.ColumnName   = it.ColumnName;
            columnDef.DataType     = string.IsNullOrEmpty(it.DataType) ? defaultDataType : it.DataType;
            columnDef.FieldType    = attris.FieldType.ToString();
            columnDef.AliasType    = attris.AliasType;
            columnDef.KeyGen       = attris.KeyGen;
            columnDef.Unique       = it.Unique;
            columnDef.DefaultValue = (it.DefaultValue == null) ? null : it.DefaultValue.ToString();
        }
Пример #20
0
        /// <summary>
        /// 尝试获取指定实体架构信息
        /// </summary>
        /// <param name="fullName">欲获取架构信息的实体全名</param>
        /// <param name="schema">如果指定实体架构信息存在,则返回,否则返回null。</param>
        /// <returns></returns>
        public static bool TryGetSchema(string fullName, out IEntitySchema schema)
        {
            if (fullName == null)
            {
                throw new ArgumentNullException(nameof(fullName));
            }
            if (fullName == string.Empty)
            {
                throw new ArgumentException(nameof(fullName));
            }

            return(entitySchemas.TryGetValue(fullName, out schema));
        }
Пример #21
0
 public string GetDisplayValue(long entityId, int objectId)
 {
     try
     {
         IEntitySchema schema = IEntitySchemaHelper.Get(entityId);
         object        entity = this.GetEntity(schema.EntityName, objectId);
         return(string.Empty);
     }
     catch
     {
         return(string.Empty);
     }
 }
Пример #22
0
        public static void CheckSchema(IEntitySchema view)
        {
            if (view == null)
            {
                throw new ArgumentNullException(nameof(view));
            }

            var dbProvider = DbConnectionManager.Gain(view.ConnectKey, true);

            if (dbProvider.AccessLevel == AccessLevel.ReadOnly)
            {
                throw new InvalidOperationException("DbProvider key:" + view.ConnectKey + ", read only!");
            }
            var tables = view.Tables;

            if (tables.Count == 0)
            {
                throw new InvalidOperationException("no entity schema table!");
            }
            if (!tables.All(p => CheckTableChanged(p, dbProvider)))
            {
                return;
            }
            if (tables.Count >= 2)
            {
                CheckViewChanged(view, dbProvider);
            }
            if (view.Attributes.HasFlag(EntitySchemaAttributes.CreateProcedure))
            {
                CheckProcedureChanged(view, ProcedureUsage.Insert, dbProvider);
                CheckProcedureChanged(view, ProcedureUsage.Update, dbProvider);
                if (view.Attributes.HasFlag(EntitySchemaAttributes.CreateType))
                {
                    if (ProcedureExists(view, ProcedureUsage.TypeInsert, dbProvider))
                    {
                        var strComm = string.Format("Drop Procedure [dbo].[Insert{0}Type]", view.Name);
                        dbProvider.ExecuteNonQuery(strComm);
                    }
                    if (ProcedureExists(view, ProcedureUsage.TypeUpdate, dbProvider))
                    {
                        var strComm = string.Format("Drop Procedure [dbo].[Update{0}Type]", view.Name);
                        dbProvider.ExecuteNonQuery(strComm);
                    }

                    CheckTypeChanged(view, dbProvider);
                    CheckProcedureChanged(view, ProcedureUsage.TypeInsert, dbProvider);
                    CheckProcedureChanged(view, ProcedureUsage.TypeUpdate, dbProvider);
                }
            }
        }
Пример #23
0
        static public void CopyTo(this IEntitySchema it, EntityColumn column)
        {
            var attris = column.GetAttributesEntity();

            column.ColumnName = it.ColumnName;
            if (!string.IsNullOrEmpty(it.FieldType))
            {
                attris.FieldType = (EntityFieldType)Enum.Parse(typeof(EntityFieldType), it.FieldType, true);
            }
            attris.AliasType    = it.AliasType;
            column.DataType     = (attris.FieldType == EntityFieldType.Var && string.IsNullOrEmpty(it.DataType)) ? defaultDataType : it.DataType;
            attris.KeyGen       = it.KeyGen;
            column.Unique       = it.Unique;
            column.DefaultValue = string.IsNullOrEmpty(it.DefaultValue) ? null : it.DefaultValue;
        }
Пример #24
0
        private void SaveAttachmentOwner(IEntitySchema es, object entity)
        {
            string str = ConfigurationManager.AppSettings[SaveAttachmentEntityKey];

            if ((string.IsNullOrEmpty(str) || (str.ToLower() == "true")) && (es.EntityName != T_AttachmentTable))
            {
                if (es.EntityName == T_AttachmentListTable)
                {
                    this.SaveAttachmentOwnerForAttachmentList(es, entity);
                }
                else
                {
                    this.SaveAttachmentOwnerForCommonEntities(es, entity);
                }
            }
        }
Пример #25
0
        public static string GetCreateProcedureCommand(IEntitySchema view, ProcedureUsage usage)
        {
            if (view == null)
            {
                throw new ArgumentNullException("table");
            }

            var columns = view.Columns;

            if (columns.Count(p => p.IsPrimary) > 1)
            {
                throw new ArgumentException("primary count > 1");
            }

            return(GetProcedureCommand(view, ProcedureOperate.Create, usage));
        }
Пример #26
0
        public static void LoadPrimary(IEntitySchema entitySchema)
        {
            if (entitySchema == null)
            {
                throw new ArgumentNullException(nameof(entitySchema));
            }
            if (!DbConnectionManager.TryGet(entitySchema.ConnectKey, out DbConnectionProvider connectionProvider))
            {
                throw new InvalidOperationException("not found DbProvider:" + entitySchema.ConnectKey);
            }

            var str        = string.Format("Select IsNull(Max([ID]), 0) From [{0}]", entitySchema.Name);
            var obj        = connectionProvider.ExecuteScalar(str);
            var maxPrimary = Convert.ToInt64(obj);

            _mkv[entitySchema.EntityType] = maxPrimary;
        }
Пример #27
0
        private void SaveAttachmentOwnerForAttachmentList(IEntitySchema es, object entity)
        {
            T_AttachmentList list = entity as T_AttachmentList;

            if ((list.Attachment_ID.HasValue && list.EntityId.HasValue) && list.ObjectID.HasValue)
            {
                string       entityNameById = this.GetEntityNameById(list.EntityId.Value);
                int          num            = list.ObjectID.Value;
                T_Attachment attach         = this._attachHandler.GetAttachment(list.Attachment_ID.Value);
                if (attach != null)
                {
                    attach.OwnerEntityType = entityNameById;
                    attach.OwnerObjectId   = new int?(num);
                    this._attachHandler.UpdateAttachment(attach, p => new { OwnerEntityType = p.OwnerEntityType, OwnerObjectId = p.OwnerObjectId });
                }
            }
        }