Exemplo n.º 1
0
        //public static DataPage<T> findPageBySql<T>( String sql, int pageSize ) where T : IEntity {

        //    if (sql == null) throw new ArgumentNullException();

        //    String mysql = sql.ToLower();

        //    String[] arrItem = strUtil.Split( mysql, "where" );

        //    String queryString = arrItem[1];

        //    String[] arrSelect = strUtil.Split( arrItem[0], "from" );
        //    String selectProperty = arrSelect[0];


        //    PageCondition pc = new PageCondition();
        //    pc.ConditionStr = queryString;
        //    pc.Property = selectProperty;

        //    //pc.CurrentPage = state.Pager.getCurrent();
        //    pc.Size = pageSize;

        //    String sql = new SqlBuilder( state.EntityInfo ).GetPageSql( pc );



        //}


        //-------------------------------------------------------------------------

        /// <summary>
        /// 将对象插入数据库
        /// </summary>
        /// <param name="obj"></param>
        /// <returns>返回一个结果对象 Result。如果发生错误,则 Result 中包含错误信息;如果没有错误,result.Info即是obj</returns>
        public static Result insert(Object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException();
            }

            Result result = ObjectDb.Insert((IEntity)obj);

            return(result);
        }
Exemplo n.º 2
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);
        }