/wojilu/_wojilu/db.cs
https://bitbucket.org/kingshine/wojilu · C# · 441 lines · 182 code · 86 blank · 173 comment · 27 complexity · ab7f5ce7c40435df17bfec3f5ea9babc MD5 · raw file
- /*
- * Copyright 2010 www.wojilu.com
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Text;
- using wojilu.ORM;
- using wojilu.Data;
- using wojilu.ORM.Operation;
- using wojilu.Web;
- using wojilu.ORM.Caching;
- namespace wojilu {
- /// <summary>
- /// wojilu ORM ×îÖ÷ÒªµÄ¹¤¾ß£¬¼¯ÖÐÁ˶ÔÏóµÄ³£Óà CRUD (¶ÁÈ¡/²åÈë/¸üÐÂ/ɾ³ý) ²Ù×÷¡£Ö÷Òª·½·¨¶¼ÊÇ·ºÐÍ·½·¨¡£
- /// </summary>
- public class db {
- /// <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 );
- }
- /// <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;
- }
- /// <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 );
- }
- /// <summary>
- /// ¸ù¾Ý²éѯÌõ¼þ£¬·µ»Ø·ÖÒ³Êý¾Ý¼¯ºÏ(ĬÈÏÿҳ·µ»Ø20Ìõ¼Ç¼)
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="condition">²éѯÌõ¼þ</param>
- /// <returns>·ÖÒ³Êý¾ÝÁÐ±í£¬°üÀ¨µ±Ç°Ò³¡¢×ܼǼÊý¡¢·ÖÒ³ÌõµÈ</returns>
- public static DataPage<T> findPage<T>( String condition ) where T : IEntity {
- return findPage<T>( condition, 20 );
- }
- /// <summary>
- /// ´æµµÄ£Ê½·Ò³(ĬÈϰ´ÕÕ order by Id asc ÅÅÐò)
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="condition">²éѯÌõ¼þ</param>
- /// <returns>·ÖÒ³Êý¾ÝÁÐ±í£¬°üÀ¨µ±Ç°Ò³¡¢×ܼǼÊý¡¢·ÖÒ³ÌõµÈ</returns>
- public static DataPage<T> findPageArchive<T>( String condition ) where T : IEntity {
- return findPageArchive<T>( condition, 20 );
- }
- /// <summary>
- /// ´æµµÄ£Ê½·Ò³(ĬÈϰ´ÕÕ order by Id asc ÅÅÐò)
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="condition">²éѯÌõ¼þ</param>
- /// <param name="pageSize">ÿҳÐèÒªÏÔʾµÄÊý¾ÝÁ¿</param>
- /// <returns>·ÖÒ³Êý¾ÝÁÐ±í£¬°üÀ¨µ±Ç°Ò³¡¢×ܼǼÊý¡¢·ÖÒ³ÌõµÈ</returns>
- public static DataPage<T> findPageArchive<T>( String condition, int pageSize ) where T : IEntity {
- if (strUtil.IsNullOrEmpty( condition )) condition = "order by Id asc";
- if (condition.ToLower().IndexOf( "order" ) < 0) condition = condition + " order by Id asc";
- DataPage<T> list = findPage<T>( condition, pageSize );
- list.Results.Sort( compareEntity );
- return list;
- }
- private static int compareEntity<T>( T p1, T p2 ) where T : IEntity {
- return p1.Id > p2.Id ? -1 : 1;
- }
- /// <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>
- /// <param name="current">µ±Ç°Ò³Âë</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 );
- 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 );
- }
- /// <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 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>
- public static NoCacheDbFinder nocache {
- get { return new NoCacheDbFinder(); }
- }
- //-------------------------------------------------------------------------
- /// <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;
- }
- /// <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>
- /// Ö»Ð޸ĶÔÏóµÄij¸öÌØ¶¨ÊôÐÔ
- /// </summary>
- /// <param name="obj"></param>
- /// <param name="propertyName">ÐèÒªÐ޸ĵÄÊôÐÔÃû³Æ</param>
- public static void update( Object obj, String propertyName ) {
- if (obj == null) throw new ArgumentNullException();
- ObjectDB.Update( (IEntity)obj, propertyName );
- ObjectPool.Update( (IEntity)obj );
- }
- /// <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 );
- }
- /// <summary>
- /// ¸ù¾ÝÌõ¼þÅúÁ¿¸üжÔÏó
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="action">¸üеIJÙ×÷</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 );
- }
- /// <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>
- /// ¸ù¾Ý 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;
- }
- /// <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>
- /// <returns>¶ÔÏóÊýÁ¿</returns>
- public static int count<T>() where T : IEntity {
- int countResult = ObjectPool.FindCount( typeof( T ) );
- if (countResult == -1) {
- countResult = ObjectDB.Count( typeof( T ) );
- ObjectPool.AddCount( typeof( T ), countResult );
- }
- return countResult;
- }
- /// <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;
- }
- //-------------------------------------------------------------------------
- internal static List<T> getResults<T>( IList list ) {
- List<T> results = new List<T>();
- foreach (T obj in list) {
- results.Add( obj );
- }
- return results;
- }
- //-------------------------------------------------------------------------
- /// <summary>
- /// ¸ù¾Ý sql Óï¾ä²éѯ£¬·µ»ØÒ»¸ö IDataReader
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="sql"></param>
- /// <returns>·µ»ØÒ»¸ö IDataReader</returns>
- public static IDataReader RunReader<T>( String sql ) {
- return DataFactory.GetCommand( sql, DbContext.getConnection( typeof( T ) ) ).ExecuteReader( CommandBehavior.CloseConnection );
- }
- /// <summary>
- /// ¸ù¾Ý sql Óï¾ä²éѯ£¬·µ»Øµ¥Ðе¥ÁÐÊý¾Ý
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="sql"></param>
- /// <returns>·µ»Øµ¥Ðе¥ÁÐÊý¾Ý</returns>
- public static Object RunScalar<T>( String sql ) {
- return DataFactory.GetCommand( sql, DbContext.getConnection( typeof( T ) ) ).ExecuteScalar();
- }
- /// <summary>
- /// Ö´ÐÐ sql Óï¾ä
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="sql"></param>
- public static void RunSql<T>( String sql ) {
- IDbCommand cmd = DataFactory.GetCommand( sql, DbContext.getConnection( typeof( T ) ) );
- cmd.ExecuteNonQuery();
- CacheTime.updateTable( typeof( T ) );
- }
- /// <summary>
- /// ¸ù¾Ý sql Óï¾ä²éѯ£¬·µ»ØÒ»¸ö DataTable
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="sql"></param>
- /// <returns></returns>
- public static DataTable RunTable<T>( String sql ) {
- DataTable dataTable = new DataTable();
- DataFactory.GetAdapter( sql, DbContext.getConnection( typeof( T ) ) ).Fill( dataTable );
- return dataTable;
- }
- }
- }