using System; using System.Collections; using System.Text; using Zhaizj.Framework.ORM; using Zhaizj.Framework.ORM.Caching; namespace Zhaizj.Framework { /// /// 集中了对象的常用 CRUD (读取/插入/更新/删除) 操作方法,非泛型实现。主要用于某些不能使用泛型的场合,不太常用。 /// public class ndb { /// /// 根据 id 查询对象 /// /// 对象的类型 /// 对象的 id /// public static IEntity findById( Type t, int id ) { if (id < 0) return null; IEntity objCache = ObjectPool.FindOne( t, id ); if (objCache == null) { ObjectInfo state = new ObjectInfo( t ); objCache = ObjectDB.FindById( id, state ); ObjectPool.Add( objCache ); } return objCache; } /// /// 查询 t 类型对象的所有数据 /// /// /// public static IList findAll( Type t ) { ObjectInfo state = new ObjectInfo( t ); state.includeAll(); IList objList = ObjectPool.FindAll( t ); if (objList == null) { objList = ObjectDB.FindAll( state ); ObjectPool.AddAll( t, objList ); } return objList; } /// /// 根据条件查询 /// /// /// 查询对象 /// 返回查询对象Query,可以进一步参数化赋值,并得到结果 public static Query find( Type t, String condition ) { ObjectInfo state = new ObjectInfo( t ); return ObjectDB.Find( state, condition ); } /// /// 根据查询条件,返回分页数据集合 /// /// /// 查询条件 /// 分页数据列表,包括当前页、总记录数、分页条等 public static IPageList findPage( Type t, String condition ) { return findPage( t, condition, -1 ); } /// /// 根据查询条件,返回分页数据集合 /// /// /// 查询条件 /// 每页数量 /// 分页数据列表,包括当前页、总记录数、分页条等 public static IPageList findPage( Type t, String condition, int pageSize ) { ObjectInfo state = new ObjectInfo( t ); state.includeAll(); if (pageSize > 0) state.Pager.setSize( pageSize ); IList list = ObjectDB.FindPage( state, condition ); IPageList result = new PageList(); result.Results = list; result.PageCount = state.Pager.PageCount; result.RecordCount = state.Pager.RecordCount; result.Size = pageSize>0 ? pageSize: state.Pager.getSize(); result.PageBar = state.Pager.PageBar; result.Current = state.Pager.getCurrent(); return result; } /// /// 根据 sql 语句,查询对象 /// /// /// /// 返回对象列表 public static Object findBySql( Type t, String sql ) { IList objList = ObjectPool.FindBySql( sql, t ); if (objList == null) { objList = ObjectDB.FindBySql( sql, t ); ObjectPool.AddSqlList( sql, objList ); } return objList; } /// /// 统计 t 类型对象的所有数据量 /// /// /// public static int count( Type t ) { return ObjectDB.Count( t ); } /// /// 根据条件统计数据量 /// /// /// 统计条件 /// public static int count( Type t, String condition ) { return ObjectDB.Count( t, condition ); } /// /// 根据 id 删除对象 /// /// /// 对象 id /// 返回受影响的行数 public static int delete( Type t, int objId ) { int num = ObjectDB.Delete( t, objId ); ObjectPool.Delete( t, objId ); return num; } } }