PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wojilu/_wojilu/db.cs

https://bitbucket.org/kingshine/wojilu
C# | 441 lines | 182 code | 86 blank | 173 comment | 27 complexity | ab7f5ce7c40435df17bfec3f5ea9babc MD5 | raw file
Possible License(s): MIT
  1. /*
  2. * Copyright 2010 www.wojilu.com
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. using System.Data;
  20. using System.Text;
  21. using wojilu.ORM;
  22. using wojilu.Data;
  23. using wojilu.ORM.Operation;
  24. using wojilu.Web;
  25. using wojilu.ORM.Caching;
  26. namespace wojilu {
  27. /// <summary>
  28. /// wojilu ORM 最主要的工具,集中了对象的常用 CRUD (读取/插入/更新/删除) 操作。主要方法都是泛型方法。
  29. /// </summary>
  30. public class db {
  31. /// <summary>
  32. /// 查询 T 类型对象的所有数据
  33. /// </summary>
  34. /// <typeparam name="T"></typeparam>
  35. /// <returns></returns>
  36. public static List<T> findAll<T>() where T : IEntity {
  37. ObjectInfo state = new ObjectInfo( typeof( T ) );
  38. state.includeAll();
  39. IList objList = ObjectPool.FindAll( typeof( T ) );
  40. if (objList == null) {
  41. objList = ObjectDB.FindAll( state );
  42. ObjectPool.AddAll( typeof( T ), objList );
  43. }
  44. return getResults<T>( objList );
  45. }
  46. /// <summary>
  47. /// 根据 id 查询对象
  48. /// </summary>
  49. /// <typeparam name="T"></typeparam>
  50. /// <param name="id"></param>
  51. /// <returns></returns>
  52. public static T findById<T>( int id ) where T : IEntity {
  53. if (id < 0) return default( T );
  54. IEntity objCache = ObjectPool.FindOne( typeof( T ), id );
  55. if (objCache == null) {
  56. ObjectInfo state = new ObjectInfo( typeof( T ) );
  57. objCache = ObjectDB.FindById( id, state );
  58. ObjectPool.Add( objCache );
  59. }
  60. return (T)objCache;
  61. }
  62. /// <summary>
  63. /// 根据查询条件,返回一个查询对象。一般用于参数化查询。
  64. /// </summary>
  65. /// <typeparam name="T"></typeparam>
  66. /// <param name="condition">查询条件</param>
  67. /// <returns>返回查询对象xQuery,可以进一步参数化赋值,并得到结果</returns>
  68. public static xQuery<T> find<T>( String condition ) where T : IEntity {
  69. ObjectInfo state = new ObjectInfo( typeof( T ) );
  70. Query q = ObjectDB.Find( state, condition );
  71. return new xQuery<T>( q );
  72. }
  73. /// <summary>
  74. /// 根据查询条件,返回分页数据集合(默认每页返回20条记录)
  75. /// </summary>
  76. /// <typeparam name="T"></typeparam>
  77. /// <param name="condition">查询条件</param>
  78. /// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
  79. public static DataPage<T> findPage<T>( String condition ) where T : IEntity {
  80. return findPage<T>( condition, 20 );
  81. }
  82. /// <summary>
  83. /// 存档模式翻页(默认按照 order by Id asc 排序)
  84. /// </summary>
  85. /// <typeparam name="T"></typeparam>
  86. /// <param name="condition">查询条件</param>
  87. /// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
  88. public static DataPage<T> findPageArchive<T>( String condition ) where T : IEntity {
  89. return findPageArchive<T>( condition, 20 );
  90. }
  91. /// <summary>
  92. /// 存档模式翻页(默认按照 order by Id asc 排序)
  93. /// </summary>
  94. /// <typeparam name="T"></typeparam>
  95. /// <param name="condition">查询条件</param>
  96. /// <param name="pageSize">每页需要显示的数据量</param>
  97. /// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
  98. public static DataPage<T> findPageArchive<T>( String condition, int pageSize ) where T : IEntity {
  99. if (strUtil.IsNullOrEmpty( condition )) condition = "order by Id asc";
  100. if (condition.ToLower().IndexOf( "order" ) < 0) condition = condition + " order by Id asc";
  101. DataPage<T> list = findPage<T>( condition, pageSize );
  102. list.Results.Sort( compareEntity );
  103. return list;
  104. }
  105. private static int compareEntity<T>( T p1, T p2 ) where T : IEntity {
  106. return p1.Id > p2.Id ? -1 : 1;
  107. }
  108. /// <summary>
  109. /// 根据查询条件、每页数量,返回分页数据集合
  110. /// </summary>
  111. /// <typeparam name="T"></typeparam>
  112. /// <param name="condition">查询条件</param>
  113. /// <param name="pageSize">每页需要显示的数据量</param>
  114. /// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
  115. public static DataPage<T> findPage<T>( String condition, int pageSize ) where T : IEntity {
  116. if (pageSize <= 0) pageSize = 20;
  117. ObjectInfo state = new ObjectInfo( typeof( T ) );
  118. state.includeAll();
  119. state.Pager.setSize( pageSize );
  120. IPageList result = ObjectPool.FindPage( typeof( T ), condition, state.Pager );
  121. if (result == null) {
  122. IList list = ObjectDB.FindPage( state, condition );
  123. ObjectPage p = state.Pager;
  124. ObjectPool.AddPage( typeof( T ), condition, p, list );
  125. result = new PageList();
  126. result.Results = list;
  127. result.PageCount = p.PageCount;
  128. result.RecordCount = p.RecordCount;
  129. result.Size = p.getSize();
  130. result.PageBar = p.PageBar;
  131. result.Current = p.getCurrent();
  132. }
  133. else {
  134. result.PageBar = new ObjectPage( result.RecordCount, result.Size, result.Current ).PageBar;
  135. }
  136. return new DataPage<T>( result );
  137. }
  138. /// <summary>
  139. /// 根据查询条件、当前页码和每页数量,返回分页数据集合
  140. /// </summary>
  141. /// <typeparam name="T"></typeparam>
  142. /// <param name="condition">查询条件</param>
  143. /// <param name="current">当前页码</param>
  144. /// <param name="pageSize">每页需要显示的数据量</param>
  145. /// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
  146. public static DataPage<T> findPage<T>( String condition, int current, int pageSize ) where T : IEntity {
  147. ObjectInfo state = new ObjectInfo( typeof( T ) );
  148. state.includeAll();
  149. state.Pager.setSize( pageSize );
  150. state.Pager.setCurrent( current );
  151. IList list = ObjectDB.FindPage( state, condition );
  152. IPageList result = new PageList();
  153. result.Results = list;
  154. result.PageCount = state.Pager.PageCount;
  155. result.RecordCount = state.Pager.RecordCount;
  156. result.Size = state.Pager.getSize();
  157. result.PageBar = state.Pager.PageBar;
  158. result.Current = state.Pager.getCurrent();
  159. return new DataPage<T>( result );
  160. }
  161. /// <summary>
  162. /// 根据 sql 语句,返回对象列表
  163. /// </summary>
  164. /// <typeparam name="T"></typeparam>
  165. /// <param name="sql"></param>
  166. /// <returns></returns>
  167. public static List<T> findBySql<T>( String sql ) where T : IEntity {
  168. IList objList = ObjectPool.FindBySql( sql, typeof( T ) );
  169. if (objList == null) {
  170. objList = ObjectDB.FindBySql( sql, typeof( T ) );
  171. ObjectPool.AddSqlList( sql, objList );
  172. }
  173. return getResults<T>( (IList)objList );
  174. }
  175. //public static DataPage<T> findPageBySql<T>( String sql, int pageSize ) where T : IEntity {
  176. // if (sql == null) throw new ArgumentNullException();
  177. // String mysql = sql.ToLower();
  178. // String[] arrItem = strUtil.Split( mysql, "where" );
  179. // String queryString = arrItem[1];
  180. // String[] arrSelect = strUtil.Split( arrItem[0], "from" );
  181. // String selectProperty = arrSelect[0];
  182. // PageCondition pc = new PageCondition();
  183. // pc.ConditionStr = queryString;
  184. // pc.Property = selectProperty;
  185. // //pc.CurrentPage = state.Pager.getCurrent();
  186. // pc.Size = pageSize;
  187. // String sql = new SqlBuilder( state.EntityInfo ).GetPageSql( pc );
  188. //}
  189. /// <summary>
  190. /// 返回一个不经过缓存的查询工具,用于直接从数据库检索数据
  191. /// </summary>
  192. public static NoCacheDbFinder nocache {
  193. get { return new NoCacheDbFinder(); }
  194. }
  195. //-------------------------------------------------------------------------
  196. /// <summary>
  197. /// 将对象插入数据库
  198. /// </summary>
  199. /// <param name="obj"></param>
  200. /// <returns>返回一个结果对象 Result。如果发生错误,则 Result 中包含错误信息;如果没有错误,result.Info即是obj</returns>
  201. public static Result insert( Object obj ) {
  202. if (obj == null) throw new ArgumentNullException();
  203. Result result = ObjectDB.Insert( (IEntity)obj );
  204. return result;
  205. }
  206. /// <summary>
  207. /// 更新对象,并存入数据库
  208. /// </summary>
  209. /// <param name="obj"></param>
  210. /// <returns>返回一个结果对象 Result。如果发生错误,则 Result 中包含错误信息</returns>
  211. public static Result update( Object obj ) {
  212. if (obj == null) throw new ArgumentNullException();
  213. Result result = ObjectDB.Update( (IEntity)obj );
  214. ObjectPool.Update( (IEntity)obj );
  215. return result;
  216. }
  217. /// <summary>
  218. /// 只修改对象的某个特定属性
  219. /// </summary>
  220. /// <param name="obj"></param>
  221. /// <param name="propertyName">需要修改的属性名称</param>
  222. public static void update( Object obj, String propertyName ) {
  223. if (obj == null) throw new ArgumentNullException();
  224. ObjectDB.Update( (IEntity)obj, propertyName );
  225. ObjectPool.Update( (IEntity)obj );
  226. }
  227. /// <summary>
  228. /// 只修改对象的特定属性
  229. /// </summary>
  230. /// <param name="obj"></param>
  231. /// <param name="arrPropertyName">需要修改的属性的数组</param>
  232. public static void update( Object obj, String[] arrPropertyName ) {
  233. if (obj == null) throw new ArgumentNullException();
  234. ObjectDB.Update( (IEntity)obj, arrPropertyName );
  235. ObjectPool.Update( (IEntity)obj );
  236. }
  237. /// <summary>
  238. /// 根据条件批量更新对象
  239. /// </summary>
  240. /// <typeparam name="T"></typeparam>
  241. /// <param name="action">更新的操作</param>
  242. /// <param name="condition">更新的条件</param>
  243. public static void updateBatch<T>( String action, String condition ) where T : IEntity {
  244. IEntity obj = Entity.New( typeof( T ).FullName );
  245. ObjectDB.UpdateBatch( obj, action, condition );
  246. }
  247. /// <summary>
  248. /// 删除数据
  249. /// </summary>
  250. /// <param name="obj"></param>
  251. /// <returns>返回受影响的行数</returns>
  252. public static int delete( Object obj ) {
  253. if (obj == null) throw new ArgumentNullException();
  254. int num = ObjectDB.Delete( (IEntity)obj );
  255. ObjectPool.Delete( (IEntity)obj );
  256. return num;
  257. }
  258. /// <summary>
  259. /// 根据 id 删除数据
  260. /// </summary>
  261. /// <typeparam name="T"></typeparam>
  262. /// <param name="id">对象的 id</param>
  263. /// <returns>返回受影响的行数</returns>
  264. public static int delete<T>( int id ) where T : IEntity {
  265. int num = ObjectDB.Delete( typeof( T ), id );
  266. ObjectPool.Delete( typeof( T ), id );
  267. return num;
  268. }
  269. /// <summary>
  270. /// 根据条件批量删除数据
  271. /// </summary>
  272. /// <typeparam name="T"></typeparam>
  273. /// <param name="condition">删除条件</param>
  274. /// <returns>返回受影响的行数</returns>
  275. public static int deleteBatch<T>( String condition ) where T : IEntity {
  276. return ObjectDB.DeleteBatch( typeof( T ), condition );
  277. }
  278. //-------------------------------------------------------------------------
  279. /// <summary>
  280. /// 统计对象的所有数目
  281. /// </summary>
  282. /// <typeparam name="T"></typeparam>
  283. /// <returns>对象数量</returns>
  284. public static int count<T>() where T : IEntity {
  285. int countResult = ObjectPool.FindCount( typeof( T ) );
  286. if (countResult == -1) {
  287. countResult = ObjectDB.Count( typeof( T ) );
  288. ObjectPool.AddCount( typeof( T ), countResult );
  289. }
  290. return countResult;
  291. }
  292. /// <summary>
  293. /// 根据条件统计对象的所有数目
  294. /// </summary>
  295. /// <typeparam name="T"></typeparam>
  296. /// <param name="condition">统计条件</param>
  297. /// <returns>对象数量</returns>
  298. public static int count<T>( String condition ) where T : IEntity {
  299. int countResult = ObjectPool.FindCount( typeof( T ), condition );
  300. if (countResult == -1) {
  301. countResult = ObjectDB.Count( typeof( T ), condition );
  302. ObjectPool.AddCount( typeof( T ), condition, countResult );
  303. }
  304. return countResult;
  305. }
  306. //-------------------------------------------------------------------------
  307. internal static List<T> getResults<T>( IList list ) {
  308. List<T> results = new List<T>();
  309. foreach (T obj in list) {
  310. results.Add( obj );
  311. }
  312. return results;
  313. }
  314. //-------------------------------------------------------------------------
  315. /// <summary>
  316. /// 根据 sql 语句查询,返回一个 IDataReader
  317. /// </summary>
  318. /// <typeparam name="T"></typeparam>
  319. /// <param name="sql"></param>
  320. /// <returns>返回一个 IDataReader</returns>
  321. public static IDataReader RunReader<T>( String sql ) {
  322. return DataFactory.GetCommand( sql, DbContext.getConnection( typeof( T ) ) ).ExecuteReader( CommandBehavior.CloseConnection );
  323. }
  324. /// <summary>
  325. /// 根据 sql 语句查询,返回单行单列数据
  326. /// </summary>
  327. /// <typeparam name="T"></typeparam>
  328. /// <param name="sql"></param>
  329. /// <returns>返回单行单列数据</returns>
  330. public static Object RunScalar<T>( String sql ) {
  331. return DataFactory.GetCommand( sql, DbContext.getConnection( typeof( T ) ) ).ExecuteScalar();
  332. }
  333. /// <summary>
  334. /// 执行 sql 语句
  335. /// </summary>
  336. /// <typeparam name="T"></typeparam>
  337. /// <param name="sql"></param>
  338. public static void RunSql<T>( String sql ) {
  339. IDbCommand cmd = DataFactory.GetCommand( sql, DbContext.getConnection( typeof( T ) ) );
  340. cmd.ExecuteNonQuery();
  341. CacheTime.updateTable( typeof( T ) );
  342. }
  343. /// <summary>
  344. /// 根据 sql 语句查询,返回一个 DataTable
  345. /// </summary>
  346. /// <typeparam name="T"></typeparam>
  347. /// <param name="sql"></param>
  348. /// <returns></returns>
  349. public static DataTable RunTable<T>( String sql ) {
  350. DataTable dataTable = new DataTable();
  351. DataFactory.GetAdapter( sql, DbContext.getConnection( typeof( T ) ) ).Fill( dataTable );
  352. return dataTable;
  353. }
  354. }
  355. }