/// <summary> /// 获取第一个类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql">SQL 语句</param> /// <param name="args">SQL 参数</param> /// <returns></returns> public T FirstOrDefault <T>(string sql, params object[] args) { sql = SelectHelper.AddSelectClause <T>(_provider, sql, _tableNameManger); return(Run <T>(sql, args, () => { var db = getDatabase(ConnectionType.Read); return db.Query <T>(sql, args).FirstOrDefault(); }, "FirstOrDefault")); }
/// <summary> /// 执行SQL 查询,返回Page类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="page">页数</param> /// <param name="itemsPerPage">每页数量</param> /// <param name="sql">SQL 语句</param> /// <param name="args">SQL 参数</param> /// <returns></returns> public Page <T> Page <T>(long page, long itemsPerPage, string sql, params object[] args) { sql = SelectHelper.AddSelectClause <T>(_provider, sql, _tableNameManger); return(Run <Page <T> >(sql, args, () => { var db = getDatabase(ConnectionType.Read); return db.Page <T>(page, itemsPerPage, sql, args); }, "Page", page.ToString(), itemsPerPage.ToString())); }
/// <summary> /// 执行SQL 查询,返回集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql">SQL 语句</param> /// <param name="args">SQL 参数</param> /// <returns></returns> public List <T> Select <T>(string sql, params object[] args) { sql = SelectHelper.AddSelectClause <T>(_provider, sql, _tableNameManger); return(Run <List <T> >(sql, args, () => { var db = getDatabase(ConnectionType.Read); return db.Query <T>(sql, args).ToList(); }, "Select")); }
/// <summary> /// 获取唯一一个类型,若数量大于1,则抛出异常 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="primaryKey">主键名</param> /// <returns></returns> public T SingleOrDefaultById <T>(object primaryKey) { var pd = PocoData.ForType(typeof(T)); var sql = string.Format("WHERE {0}=@0", _provider.EscapeSqlIdentifier(pd.TableInfo.PrimaryKey)); sql = SelectHelper.AddSelectClause <T>(_provider, sql, _tableNameManger); return(SingleOrDefault <T>(sql, primaryKey)); }
/// <summary> /// 执行SQL 查询,返回集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="skip">跳过</param> /// <param name="take">获取个数</param> /// <param name="sql">SQL 语句</param> /// <param name="args">SQL 参数</param> /// <returns></returns> public List <T> SkipTake <T>(long skip, long take, string sql, params object[] args) { sql = SelectHelper.AddSelectClause <T>(_provider, sql, _tableNameManger); return(Run <List <T> >(sql, args, () => { var db = getDatabase(ConnectionType.Read); return db.SkipTake <T>(skip, take, sql, args); }, "SkipTake", skip.ToString(), take.ToString())); }