/wojilu/_wojilu/db.cs

https://bitbucket.org/kingshine/wojilu · C# · 441 lines · 182 code · 86 blank · 173 comment · 27 complexity · ab7f5ce7c40435df17bfec3f5ea9babc MD5 · raw file

  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. /// Ö»Ð޸ĶÔÏóµÄij¸öÌØ¶¨ÊôÐÔ
  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">¸üеIJÙ×÷</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. }