public IEnumerable<UserRole> GetRoles() { using (IObjectDb db = new ObjectDb("UserRole_GetUserRoles")) { return (IEnumerable<UserRole>)db.Query<UserRole>(); } }
/// <summary> /// 根据查询条件、每页数量,返回分页数据集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="condition">查询条件</param> /// <param name="pageSize">每页需要显示的数据量</param> /// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns> public static DataPage <T> findPage <T>(String condition, int pageSize) where T : IEntity { if (pageSize <= 0) { pageSize = 20; } ObjectInfo state = new ObjectInfo(typeof(T)); state.includeAll(); state.Pager.setSize(pageSize); IPageList result = ObjectPool.FindPage(typeof(T), condition, state.Pager); if (result == null) { IList list = ObjectDb.FindPage(state, condition); ObjectPage p = state.Pager; ObjectPool.AddPage(typeof(T), condition, p, list); result = new PageList(); result.Results = list; result.PageCount = p.PageCount; result.RecordCount = p.RecordCount; result.Size = p.getSize(); result.PageBar = p.PageBar; result.Current = p.getCurrent(); } else { result.PageBar = new ObjectPage(result.RecordCount, result.Size, result.Current).PageBar; } return(new DataPage <T>(result)); }
/// <summary> /// 根据查询条件,返回一个查询对象。一般用于参数化查询。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="condition">查询条件</param> /// <returns>返回查询对象xQuery,可以进一步参数化赋值,并得到结果</returns> public static xQuery <T> find <T>(String condition) where T : IEntity { ObjectInfo state = new ObjectInfo(typeof(T)); Query q = ObjectDb.Find(state, condition); return(new xQuery <T>(q)); }
public IEnumerable <Federation> GetAll() { using (var objectDb = new ObjectDb("GetAllFederation")) { return(objectDb.Query <Federation>().OrderBy(x => x.Name)); } }
public IEnumerable<object> GetAll() { using (IObjectDb db = new ObjectDb("User_GetUsers")) { return (IEnumerable<ClientUser>)db.Query<ClientUser>(); } }
/// <summary> /// 根据 id 删除数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id">对象的 id</param> /// <returns>返回受影响的行数</returns> public static int delete <T>(int id) where T : IEntity { int num = ObjectDb.Delete(typeof(T), id); ObjectPool.Delete(typeof(T), id); return(num); }
public IEnumerable <Player> GetAll() { using (IObjectDb db = new ObjectDb("GetAllPlayer")) { return(db.Query <Player>()); } }
private static IList findAllFromChild(IList parents, ObjectInfo state) { ArrayList results = new ArrayList(); foreach (EntityInfo info in state.EntityInfo.ChildEntityList) { ObjectInfo childState = new ObjectInfo(info); childState.includeAll(); IList children = ObjectDb.FindAll(childState); for (int i = 0; i < children.Count; i++) { IEntity child = children[i] as IEntity; // state //child.state.Order = state.Order; results.Add(child); parents.RemoveAt(Query.getIndexOfObject(parents, child)); } } if (parents.Count > 0) { results.AddRange(parents); } results.Sort(); return(results); }
public Player GetById(int id) { using (IObjectDb db = new ObjectDb("GetPlayer")) { var player = db.QueryEntity <Player>(id); return(player); } }
public void Update(object obj) { using (IObjectDb db = new ObjectDb("Movie_UpdateMovie")) { Movie movie = (Movie)obj; db.ExecuteNonQuery(movie); } }
public IEnumerable<object> GetAll() { using (IObjectDb db = new ObjectDb("Movie_GetMovies")) { IEnumerable<Movie> movies = (IEnumerable<Movie>)db.Query<Movie>(); return movies; } }
/// <summary> /// 只修改对象的特定属性 /// </summary> /// <param name="obj"></param> /// <param name="arrPropertyName">需要修改的属性的数组</param> public static void update(Object obj, String[] arrPropertyName) { if (obj == null) { throw new ArgumentNullException(); } ObjectDb.Update((IEntity)obj, arrPropertyName); ObjectPool.Update((IEntity)obj); }
public static IList FindDataOther(Type throughType, Type t, String order, int id) { // 1029 ObjectInfo state = new ObjectInfo(throughType); String relationPropertyName = state.EntityInfo.GetRelationPropertyName(t); EntityPropertyInfo info = state.EntityInfo.FindRelationProperty(t); state.Order = order; state.include(info.Name); return(ObjectDb.Find(state, relationPropertyName + ".Id=" + id).listChildren(info.Name)); }
//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); }
public IEnumerable <Round> GetListRoundByTournament(int id) { var parameter = new { TourID = id.ToString(), }; using (IObjectDb db = new ObjectDb("GetListRoundByTournament")) { return(db.Query <Round>(parameter)); } }
public IEnumerable <Federation> GetFederationInTournament(int id) { var parameter = new { TourId = id.ToString(), }; using (IObjectDb db = new ObjectDb("GetFederationParticipate")) { return(db.Query <Federation>(parameter)); } }
public Federation GetById(int id) { var parameter = new { TourId = id.ToString(), }; using (IObjectDb db = new ObjectDb("GetSingle")) { return(db.Query <Federation>(parameter).FirstOrDefault()); } }
public void Delete(string id) { using (IObjectDb db = new ObjectDb("Movie_DeleteMovie")) { var param = new { Id = id }; db.ExecuteNonQuery(param); } }
// TODO: performance private static IEntity findByIdFromChild(int id, EntityInfo entityInfo) { foreach (EntityInfo ei in entityInfo.ChildEntityList) { IEntity result = ObjectDb.FindById(id, new ObjectInfo(ei)); if (result != null) { return(result); } } return(null); }
public void Delete(string id) { using (IObjectDb db = new ObjectDb("User_DeleteUser")) { var param = new { UserName = id }; db.ExecuteNonQuery(param); } }
/// <summary> /// 更新对象,并存入数据库 /// </summary> /// <param name="obj"></param> /// <returns>返回一个结果对象 Result。如果发生错误,则 Result 中包含错误信息</returns> public static Result update(Object obj) { if (obj == null) { throw new ArgumentNullException(); } Result result = ObjectDb.Update((IEntity)obj); ObjectPool.Update((IEntity)obj); return(result); }
/// <summary> /// 删除数据 /// </summary> /// <param name="obj"></param> /// <returns>返回受影响的行数</returns> public static int delete(Object obj) { if (obj == null) { throw new ArgumentNullException(); } int num = ObjectDb.Delete((IEntity)obj); ObjectPool.Delete((IEntity)obj); return(num); }
/// <summary> /// 根据条件统计对象的所有数目 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="condition">统计条件</param> /// <returns>对象数量</returns> public static int count <T>(String condition) where T : IEntity { int countResult = ObjectPool.FindCount(typeof(T), condition); if (countResult == -1) { countResult = ObjectDb.Count(typeof(T), condition); ObjectPool.AddCount(typeof(T), condition, countResult); } return(countResult); }
/// <summary> /// 根据 sql 语句,返回对象列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <returns></returns> public static List <T> findBySql <T>(String sql) where T : IEntity { IList objList = ObjectPool.FindBySql(sql, typeof(T)); if (objList == null) { objList = ObjectDb.FindBySql(sql, typeof(T)); ObjectPool.AddSqlList(sql, objList); } return(getResults <T>((IList)objList)); }
public object Get(string id) { using (IObjectDb db = new ObjectDb("Movie_GetMovie")) { var param = new { Id = id }; List<Movie> movies = (List<Movie>)db.Query<Movie>(param); return movies.FirstOrDefault(); } }
public object Get(string id) { using (IObjectDb db = new ObjectDb("User_GetUser")) { var param = new { UserName = id }; List<ClientUser> users = (List<ClientUser>)db.Query<ClientUser>(param); return users.FirstOrDefault(); } }
/// <summary> /// 查询 T 类型对象的所有数据 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static List <T> findAll <T>() where T : IEntity { ObjectInfo state = new ObjectInfo(typeof(T)); state.includeAll(); IList objList = ObjectPool.FindAll(typeof(T)); if (objList == null) { objList = ObjectDb.FindAll(state); ObjectPool.AddAll(typeof(T), objList); } return(getResults <T>(objList)); }
public void Add(Player entity) { using (IObjectDb db = new ObjectDb("CreatePlayer")) { var param = new { Name = entity.Name, BirthDate = entity.BirthDate, Image = entity.Image, Sex = entity.Sex, FederationID = entity.FederationID, }; db.ExecuteNonQuery(param); } }
public void Update(object obj) { using (IObjectDb db = new ObjectDb("User_UpdateUser")) { ClientUser user = (ClientUser)obj; var param = new { UserName = user.UserName, Password = user.Password, Role = user.Role, Email = user.Email, }; db.ExecuteNonQuery(param); } }
private static IEntity setEntityProperty(IEntity obj, int id, ObjectInfo state) { if (obj == null) { return(null); } IList entityPropertyList = state.EntityInfo.EntityPropertyList; foreach (EntityPropertyInfo ep in entityPropertyList) { if (!isPropertyInIncluder(ep, state.Includer.EntityPropertyList)) { continue; } IEntity propertyValue = obj.get(ep.Name) as IEntity; if (propertyValue == null) { continue; } int pid = propertyValue.Id; if (pid <= 0) { continue; } IEntity cachedValue = ObjectPool.FindOne(ep.Type, pid); if (cachedValue == null) { propertyValue = ObjectDb.FindById(pid, new ObjectInfo(ep.Type)); ObjectPool.Add(propertyValue); } else { propertyValue = cachedValue; } ValueSetter.setEntityByCheckNull(obj, ep, propertyValue, pid); } return(obj); }
/// <summary> /// 根据 id 查询对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns></returns> public static T findById <T>(int id) where T : IEntity { if (id < 0) { return(default(T)); } IEntity objCache = ObjectPool.FindOne(typeof(T), id); if (objCache == null) { ObjectInfo state = new ObjectInfo(typeof(T)); objCache = ObjectDb.FindById(id, state); ObjectPool.Add(objCache); } return((T)objCache); }
public void Create(object obj) { using (IObjectDb db = new ObjectDb("User_CreateUser")) { ClientUser user = (ClientUser)obj; user.RegDate = System.DateTime.Now; var param = new { UserName = user.UserName, Password = user.Password, Role = user.Role, Email = user.Email, RegDate = user.RegDate }; db.ExecuteNonQuery(param); } }
// The persistence layer will scope data it saves to the player. This is done on the server side // and is transparent to the client. This is a convenience feature for prototyping and development. Do // not use this in a production app. // **IMPORTANT** // Please note that this is still not completely secure, as a hacked client can still write out data // you are not expecting and there is no way to validate the data before saving it (other then the scoping). Any server logic that // you write that depends on data saved using this feature is WRONG. Do not do that. You have been warned. void Start() { //Setup. The following 3 steps are required for peristence to work // 1. Find the ObjectDb actor. It is started automatically by the actor system ObjectDb db = ActorSystem.Instance.Find("ObjectDb") as ObjectDb; // 2. Set the callback to receive objects from the server ObjectDb.ObjectReceived callback = OnObjectReceived; db.OnObjectReceived(callback); // 3. Set the player id db.SetPlayerId(User.Instance.username); // Saving and retrieving entities Entity entity = new Entity(); entity.id = "dbtest"; db.Save(entity); db.Find(entity.id); // If you want to save arbitrary binary data, you must wrap it in a NativeBytes message and attach that // message to the entity you are saving. // Simple byte array to use as a test string str = "Howdy"; byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); // Wrap the byte array and save it NativeBytes nativeBytes = new NativeBytes(); nativeBytes.bytes = bytes; entity.nativeBytes = nativeBytes; entity.id = "native_bytes_test"; db.Save(entity); db.Find(entity.id); }
public static String GetSameTypeIds(Type throughType, Type t, int id) { // 1029 ObjectInfo state = new ObjectInfo(throughType); String relationPropertyName = state.EntityInfo.GetRelationPropertyName(t); EntityPropertyInfo info = state.EntityInfo.FindRelationProperty(t); String ids = ObjectDb.Find(state, relationPropertyName + ".Id=" + id).get(info.Name + ".Id"); EntityPropertyInfo property = state.EntityInfo.GetProperty(relationPropertyName); String sql = String.Format("select distinct {0} from {1} where {2} in ({3}) and {0}<>{4}", property.ColumnName, state.EntityInfo.TableName, info.ColumnName, ids, id); IDbCommand command = DataFactory.GetCommand(sql, DbContext.getConnection(state.EntityInfo)); IDataReader rd = null; StringBuilder builder = new StringBuilder(); try { rd = command.ExecuteReader(); while (rd.Read()) { builder.Append(rd[0]); builder.Append(","); } } catch (Exception exception) { logger.Error(exception.Message); throw exception; } finally { if (command.Connection.State != ConnectionState.Closed) { command.Connection.Close(); command.Connection.Dispose(); OrmHelper.clostCount++; LogManager.GetLogger("Class:System.ORM.Operation.FindRelationOperation Method:GetSameTypeIds").Info("数据库连接已关闭【" + OrmHelper.clostCount + "】"); } OrmHelper.CloseDataReader(rd); } return(builder.ToString().TrimEnd(',')); }
/// <summary> /// 根据查询条件、当前页码和每页数量,返回分页数据集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="condition">查询条件</param> /// <param name="current">当前页码(从0开始)</param> /// <param name="pageSize">每页需要显示的数据量</param> /// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns> public static DataPage <T> findPage <T>(String condition, int current, int pageSize) where T : IEntity { ObjectInfo state = new ObjectInfo(typeof(T)); state.includeAll(); state.Pager.setSize(pageSize); current++; state.Pager.setCurrent(current); 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 = state.Pager.getSize(); result.PageBar = state.Pager.PageBar; result.Current = state.Pager.getCurrent(); return(new DataPage <T>(result)); }
public string GetRole(int id) { using (IObjectDb db = new ObjectDb("UserRole_GetUserRole")) { return db.Query<string>().FirstOrDefault(); } }
public string GetEmail(string email) { using (IObjectDb db = new ObjectDb("User_GetEmail")) { List<string> emails = (List<string>)db.Query<string>(new { Email = email }); return emails.FirstOrDefault(); } }
/// <summary> /// 根据条件批量删除数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="condition">删除条件</param> /// <returns>返回受影响的行数</returns> public static int deleteBatch <T>(String condition) where T : IEntity { return(ObjectDb.DeleteBatch(typeof(T), condition)); }
/// <summary> /// 根据条件批量更新对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="action">更新的操作</param> /// <param name="condition">更新的条件</param> public static void updateBatch <T>(String action, String condition) where T : IEntity { IEntity obj = Entity.New(typeof(T).FullName); ObjectDb.UpdateBatch(obj, action, condition); }
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); }