示例#1
0
        public static int Delete(long id, IEntity obj, EntityInfo entityInfo)
        {
            List <IInterceptor> ilist = MappingClass.Instance.InterceptorList;

            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].BeforDelete(obj);
            }

            int rowAffected = 0;

            rowAffected += deleteSingle(id, entityInfo);
            if (entityInfo.ChildEntityList.Count > 0)
            {
                foreach (EntityInfo info in entityInfo.ChildEntityList)
                {
                    rowAffected += deleteSingle(id, Entity.GetInfo(info.Type.FullName));
                }
            }
            if (entityInfo.Parent != null)
            {
                IEntity objP = Entity.New(entityInfo.Parent.Type.FullName);
                rowAffected += deleteSingle(id, Entity.GetInfo(objP));
            }

            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].AfterDelete(obj);
            }

            CacheUtil.CheckCountCache("delete", obj, entityInfo);

            return(rowAffected);
        }
示例#2
0
        public static void Update(IEntity obj, String[] arrPropertyName, EntityInfo entityInfo)
        {
            if (obj == null)
            {
                throw new ArgumentNullException();
            }
            StringBuilder builder = new StringBuilder(String.Empty);

            builder.Append("update ");
            builder.Append(entityInfo.TableName);
            builder.Append(" set ");

            for (int i = 0; i < arrPropertyName.Length; i++)
            {
                String columnName = entityInfo.GetColumnName(arrPropertyName[i]);
                builder.Append(columnName);
                builder.Append("=");
                builder.Append(entityInfo.Dialect.GetParameter(columnName));
                builder.Append(",");
            }
            builder.Append(" where Id=");
            builder.Append(entityInfo.Dialect.GetParameter("Id"));

            String commandText = builder.ToString().Replace(", where", " where");

            logger.Info(LoggerUtil.SqlPrefix + entityInfo.Name + "[" + entityInfo.TableName + "] Update Sql:" + commandText);

            List <IInterceptor> ilist = MappingClass.Instance.InterceptorList;

            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].BeforUpdate(obj);
            }

            IDbCommand cmd = DataFactory.GetCommand(commandText, DbContext.getConnection(entityInfo));

            for (int i = 0; i < arrPropertyName.Length; i++)
            {
                Object parameterValue = obj.get(arrPropertyName[i]);
                String columnName     = entityInfo.GetColumnName(arrPropertyName[i]);
                DataFactory.SetParameter(cmd, columnName, parameterValue);
            }
            long id = obj.Id;

            DataFactory.SetParameter(cmd, "Id", id);
            cmd.ExecuteNonQuery();

            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].AfterUpdate(obj);
            }

            // update cache  timestamp
            CacheTime.updateTable(entityInfo.Type);


            CacheUtil.CheckCountCache("insert", obj, entityInfo);
        }
示例#3
0
        private static Result Insert(IEntity obj, EntityInfo entityInfo, Boolean isInsertParent)
        {
            Result result = Validator.Validate(obj, "insert");

            if (result.HasErrors)
            {
                return(result);
            }

            if (isInsertParent && entityInfo.Parent != null)
            {
                IEntity objP = Entity.New(entityInfo.Type.BaseType.FullName);
                List <EntityPropertyInfo> eplist = Entity.GetInfo(objP).SavedPropertyList;
                foreach (EntityPropertyInfo info in eplist)
                {
                    if (info.IsList)
                    {
                        continue;
                    }
                    objP.set(info.Name, obj.get(info.Name));
                }
                ObjectDB.Insert(objP);

                obj.Id = objP.Id;
            }

            List <IInterceptor> ilist = MappingClass.Instance.InterceptorList;

            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].BeforInsert(obj);
            }

            IDbCommand cmd = DataFactory.GetCommand(getInsertSql(entityInfo), DbContext.getConnection(entityInfo));

            OrmHelper.SetParameters(cmd, "insert", obj, entityInfo);

            lock (objLock) {
                executeCmd(cmd, entityInfo, ref obj);
            }

            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].AfterInsert(obj);
            }
            CacheUtil.CheckCountCache("insert", obj, entityInfo);
            result.Info = obj;

            CacheTime.updateTable(obj.GetType());

            return(result);
        }
示例#4
0
        private static Result Update(IEntity obj, EntityInfo entityInfo)
        {
            Result result = Validator.Validate(obj, "update");

            if (result.IsValid == false)
            {
                return(result);
            }

            List <IInterceptor> ilist = MappingClass.Instance.InterceptorList;

            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].BeforUpdate(obj);
            }

            updateSingle(obj, entityInfo);
            if (entityInfo.Parent != null)
            {
                IEntity objParent = Entity.New(entityInfo.Parent.Type.FullName);
                setParentValueFromChild(objParent, obj);
                updateSingle(objParent, Entity.GetInfo(objParent));
            }
            CacheUtil.CheckCountCache("insert", obj, entityInfo);


            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].AfterUpdate(obj);
            }
            result.Info = obj;

            // update cache  timestamp
            CacheTime.updateTable(entityInfo.Type);

            return(result);
        }
示例#5
0
        private static Result Insert(IEntity obj, EntityInfo entityInfo, Boolean isInsertParent)
        {
            Result result = Validator.Validate(obj, "insert");

            if (result.HasErrors)
            {
                return(result);
            }

            if (isInsertParent && entityInfo.Parent != null)
            {
                IEntity objP = Entity.New(entityInfo.Type.BaseType.FullName);
                List <EntityPropertyInfo> eplist = Entity.GetInfo(objP).SavedPropertyList;
                foreach (EntityPropertyInfo info in eplist)
                {
                    if (info.IsList)
                    {
                        continue;
                    }
                    objP.set(info.Name, obj.get(info.Name));
                }
                ObjectDb.Insert(objP);

                obj.Id = objP.Id;
            }

            List <IInterceptor> ilist = MappingClass.Instance.InterceptorList;

            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].BeforInsert(obj);
            }
            var conn = DbContext.getConnection(entityInfo);

            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
                OrmHelper.initCount++;
                LogManager.GetLogger("Class:System.Data.InsertOperation Method:Insert").Info("数据库连接已开启【" + OrmHelper.initCount + "】");
            }
            IDbCommand cmd = DataFactory.GetCommand(getInsertSql(entityInfo), conn);

            OrmHelper.SetParameters(cmd, "insert", obj, entityInfo);
            obj.Id = executeCmd(cmd, entityInfo);
            if (!DbContext.shouldTransaction())
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                    OrmHelper.clostCount++;
                    LogManager.GetLogger("Class:System.ORM.Operation.InsertOperation Method:Insert").Info("数据库连接已关闭【" + OrmHelper.clostCount + "】");
                }
            }
            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].AfterInsert(obj);
            }
            CacheUtil.CheckCountCache("insert", obj, entityInfo);
            result.Info = obj;

            CacheTime.updateTable(obj.GetType());

            return(result);
        }
示例#6
0
        public static void Update(IEntity obj, String[] arrPropertyName, EntityInfo entityInfo)
        {
            if (obj == null)
            {
                throw new ArgumentNullException();
            }
            StringBuilder builder = new StringBuilder(String.Empty);

            builder.Append("update ");
            builder.Append(entityInfo.TableName);
            builder.Append(" set ");
            for (int i = 0; i < arrPropertyName.Length; i++)
            {
                String columnName = entityInfo.GetColumnName(arrPropertyName[i]);
                builder.Append(columnName);
                builder.Append("=");
                builder.Append(entityInfo.Dialect.GetParameter(columnName));
                builder.Append(",");
            }
            builder.Append(" where Id=");
            builder.Append(entityInfo.Dialect.GetParameter("Id"));

            String commandText = builder.ToString().Replace(", where", " where");

            logger.Info(LoggerUtil.SqlPrefix + entityInfo.Name + "[" + entityInfo.TableName + "] Update Sql:" + commandText);

            List <IInterceptor> ilist = MappingClass.Instance.InterceptorList;

            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].BeforUpdate(obj);
            }

            var conn = DbContext.getConnection(entityInfo);

            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
                OrmHelper.initCount++;
                LogManager.GetLogger("Class:System.ORM.Operation.UpdateOperation Method:Update").Info("数据库连接已开启【" + OrmHelper.initCount + "】");
            }
            IDbCommand cmd = DataFactory.GetCommand(commandText, conn);

            for (int i = 0; i < arrPropertyName.Length; i++)
            {
                Object parameterValue = obj.get(arrPropertyName[i]);
                String columnName     = entityInfo.GetColumnName(arrPropertyName[i]);
                DataFactory.SetParameter(cmd, columnName, parameterValue);
            }
            int id = obj.Id;

            DataFactory.SetParameter(cmd, "Id", id);
            cmd.ExecuteNonQuery();

            if (!DbContext.shouldTransaction())
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                    OrmHelper.clostCount++;
                    LogManager.GetLogger("Class:System.ORM.Operation.UpdateOperation Method:Update").Info("数据库连接已关闭【" + OrmHelper.clostCount + "】");
                }
            }

            for (int i = 0; i < ilist.Count; i++)
            {
                ilist[i].AfterUpdate(obj);
            }

            // update cache  timestamp
            CacheTime.updateTable(entityInfo.Type);


            CacheUtil.CheckCountCache("insert", obj, entityInfo);
        }