363 lines
12 KiB
C#
363 lines
12 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
using CYQ.Data.Table;
|
|||
|
|
using CYQ.Data.SQL;
|
|||
|
|
using CYQ.Data.Tool;
|
|||
|
|
using System.Web;
|
|||
|
|
using System.Threading;
|
|||
|
|
using System.Data;
|
|||
|
|
|
|||
|
|
|
|||
|
|
namespace CYQ.Data.Orm
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ٲ<EFBFBD><D9B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ࡣ
|
|||
|
|
/// </summary>
|
|||
|
|
public static class DBFast
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ǰ <20>û<EFBFBD> <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
internal static bool HasTransation(string key)
|
|||
|
|
{
|
|||
|
|
return TransationKeys.ContainsKey(key);
|
|||
|
|
}
|
|||
|
|
internal static IsolationLevel GetTransationLevel(string key)
|
|||
|
|
{
|
|||
|
|
if (TransationKeys.ContainsKey(key))
|
|||
|
|
{
|
|||
|
|
return TransationKeys[key];
|
|||
|
|
}
|
|||
|
|
return IsolationLevel.ReadCommitted;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>浵<EFBFBD><E6B5B5><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>ʶ
|
|||
|
|
/// </summary>
|
|||
|
|
public static MDictionary<string, IsolationLevel> TransationKeys = new MDictionary<string, IsolationLevel>();
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD><D1B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>false<73><65>
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool BeginTransation(string conn)
|
|||
|
|
{
|
|||
|
|
return BeginTransation(conn, IsolationLevel.ReadCommitted);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD><D1B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>false<73><65>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="conn"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD></param>
|
|||
|
|
/// <param name="level"><3E><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD></param>
|
|||
|
|
public static bool BeginTransation(string conn, IsolationLevel level)
|
|||
|
|
{
|
|||
|
|
string key = StaticTool.GetTransationKey(conn);
|
|||
|
|
if (!TransationKeys.ContainsKey(key))
|
|||
|
|
{
|
|||
|
|
TransationKeys.Add(key, level);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ύ<EFBFBD><E1BDBB><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool EndTransation(string conn)
|
|||
|
|
{
|
|||
|
|
string key = StaticTool.GetTransationKey(conn);
|
|||
|
|
TransationKeys.Remove(key);
|
|||
|
|
DalBase dal = DalCreate.Get(key);
|
|||
|
|
if (dal != null && dal.EndTransaction())//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ع<EFBFBD><D8B9>ˣ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
dal.Dispose();
|
|||
|
|
return DalCreate.Remove(key);
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ع<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool RollBack(string conn)
|
|||
|
|
{
|
|||
|
|
string key = StaticTool.GetTransationKey(conn);
|
|||
|
|
TransationKeys.Remove(key);
|
|||
|
|
DalBase dal = DalCreate.Get(key);
|
|||
|
|
if (dal != null && dal.RollBack())
|
|||
|
|
{
|
|||
|
|
dal.Dispose();
|
|||
|
|
return DalCreate.Remove(key);
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD>¼
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="where"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="columns">ָ<><D6B8><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ѡ<EFBFBD><D1A1></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static T Find<T>(object where, params string[] columns) where T : class
|
|||
|
|
{
|
|||
|
|
T result = default(T);
|
|||
|
|
MDataRow row = null;
|
|||
|
|
using (MAction action = GetMAction<T>())
|
|||
|
|
{
|
|||
|
|
if (columns != null && columns.Length > 0)
|
|||
|
|
{
|
|||
|
|
action.SetSelectColumns(columns);
|
|||
|
|
}
|
|||
|
|
if (action.Fill(where))
|
|||
|
|
{
|
|||
|
|
row = action.Data;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (row != null)
|
|||
|
|
{
|
|||
|
|
result = row.ToEntity<T>();
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
public static List<T> Select<T>() where T : class
|
|||
|
|
{
|
|||
|
|
int count;
|
|||
|
|
return Select<T>(0, 0, null, out count, null);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>б<EFBFBD><D0B1><EFBFBD>ѯ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="where"><3E><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>[<5B>ɸ<EFBFBD><C9B8><EFBFBD> order by <20><><EFBFBD><EFBFBD>]</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<T> Select<T>(string where, params string[] columns) where T : class
|
|||
|
|
{
|
|||
|
|
int count;
|
|||
|
|
return Select<T>(0, 0, where, out count, columns);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>б<EFBFBD><D0B1><EFBFBD>ѯ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="topN"><3E><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="where"><3E><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>[<5B>ɸ<EFBFBD><C9B8><EFBFBD> order by <20><><EFBFBD><EFBFBD>]</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<T> Select<T>(int topN, string where, params string[] columns) where T : class
|
|||
|
|
{
|
|||
|
|
int count;
|
|||
|
|
return Select<T>(1, topN, where, out count, columns);
|
|||
|
|
}
|
|||
|
|
public static List<T> Select<T>(int pageIndex, int pageSize, params string[] columns) where T : class
|
|||
|
|
{
|
|||
|
|
int count;
|
|||
|
|
return Select<T>(pageIndex, pageSize, null, out count, columns);
|
|||
|
|
}
|
|||
|
|
public static List<T> Select<T>(int pageIndex, int pageSize, string where, params string[] columns) where T : class
|
|||
|
|
{
|
|||
|
|
int count;
|
|||
|
|
return Select<T>(pageIndex, pageSize, where, out count, columns);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>Ҷ<EFBFBD><D2B6><EFBFBD><EFBFBD><EFBFBD>¼
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="pageIndex"><3E><>Nҳ</param>
|
|||
|
|
/// <param name="pageSize">ÿҳN<D2B3><4E></param>
|
|||
|
|
/// <param name="where"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="count"><3E><><EFBFBD>ؼ<EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="columns">ָ<><D6B8><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ѡ<EFBFBD><D1A1></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<T> Select<T>(int pageIndex, int pageSize, object where, out int count, params string[] columns) where T : class
|
|||
|
|
{
|
|||
|
|
//MDataTable dt = null;
|
|||
|
|
using (MAction action = GetMAction<T>())
|
|||
|
|
{
|
|||
|
|
if (columns != null && columns.Length > 0)
|
|||
|
|
{
|
|||
|
|
action.SetSelectColumns(columns);
|
|||
|
|
}
|
|||
|
|
return action.SelectList<T>(pageIndex, pageSize, where, out count);
|
|||
|
|
//dt = action.Select(pageIndex, pageSize, where, out count);
|
|||
|
|
}
|
|||
|
|
// return dt.ToList<T>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// ɾ<><C9BE><EFBFBD><EFBFBD>¼(<28><>Ӱ<EFBFBD><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>>0<><30>Ϊtrue)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="where"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool Delete<T>(object where) where T : class
|
|||
|
|
{
|
|||
|
|
return Delete<T>(where, false);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ɾ<><C9BE><EFBFBD><EFBFBD>¼(<28><>Ӱ<EFBFBD><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>>0<><30>Ϊtrue)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="where"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="isIgnoreDeleteField"><3E>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>ʶ</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool Delete<T>(object where, bool isIgnoreDeleteField) where T : class
|
|||
|
|
{
|
|||
|
|
if (where == null) { return false; }
|
|||
|
|
using (MAction action = GetMAction<T>())
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
if (typeof(T).FullName == where.GetType().FullName)//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
action.Data.LoadFrom(where);
|
|||
|
|
return action.Delete(null, isIgnoreDeleteField) && action.RecordsAffected > 0;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return action.Delete(where, isIgnoreDeleteField) && action.RecordsAffected > 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public static bool Insert<T>(T t) where T : class
|
|||
|
|
{
|
|||
|
|
return Insert<T>(t, InsertOp.ID, false);
|
|||
|
|
}
|
|||
|
|
public static bool Insert<T>(T t, InsertOp op) where T : class
|
|||
|
|
{
|
|||
|
|
return Insert<T>(t, op, false);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>¼
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="t">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool Insert<T>(T t, InsertOp op, bool insertID) where T : class
|
|||
|
|
{
|
|||
|
|
bool result = false;
|
|||
|
|
MDataRow row = null;
|
|||
|
|
using (MAction action = GetMAction<T>())
|
|||
|
|
{
|
|||
|
|
action.AllowInsertID = insertID;
|
|||
|
|
action.Data.LoadFrom(t, BreakOp.Null);
|
|||
|
|
result = action.Insert(op);
|
|||
|
|
if (result && op != InsertOp.None)
|
|||
|
|
{
|
|||
|
|
row = action.Data;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (row != null)
|
|||
|
|
{
|
|||
|
|
row.SetToEntity(t);
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
public static bool Update<T>(T t) where T : class
|
|||
|
|
{
|
|||
|
|
return Update<T>(t, null);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>¼<EFBFBD>¼(<28><>Ӱ<EFBFBD><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>>0<><30>Ϊtrue)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
|||
|
|
/// <param name="t">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="where"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="updateColumns">ָ<><D6B8>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool Update<T>(T t, object where, params string[] updateColumns) where T : class
|
|||
|
|
{
|
|||
|
|
using (MAction action = GetMAction<T>())
|
|||
|
|
{
|
|||
|
|
action.Data.LoadFrom(t, BreakOp.Null);
|
|||
|
|
if (updateColumns.Length > 0)
|
|||
|
|
{
|
|||
|
|
action.Data.SetState(0);
|
|||
|
|
if (updateColumns.Length == 1)
|
|||
|
|
{
|
|||
|
|
updateColumns = updateColumns[0].Split(',');
|
|||
|
|
}
|
|||
|
|
foreach (string item in updateColumns)
|
|||
|
|
{
|
|||
|
|
MDataCell cell = action.Data[item];
|
|||
|
|
if (cell != null)
|
|||
|
|
{
|
|||
|
|
cell.State = 2;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return action.Update(where) && action.RecordsAffected > 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool Exists<T>(object where) where T : class
|
|||
|
|
{
|
|||
|
|
using (MAction action = GetMAction<T>())
|
|||
|
|
{
|
|||
|
|
return action.Exists(where);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public static int GetCount<T>(object where) where T : class
|
|||
|
|
{
|
|||
|
|
using (MAction action = GetMAction<T>())
|
|||
|
|
{
|
|||
|
|
return action.GetCount(where);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private static MAction GetMAction<T>()
|
|||
|
|
{
|
|||
|
|
string conn = string.Empty;
|
|||
|
|
MAction action = new MAction(GetTableName<T>(out conn), conn);
|
|||
|
|
//action.SetAopState(CYQ.Data.Aop.AopOp.CloseAll);
|
|||
|
|
return action;
|
|||
|
|
}
|
|||
|
|
internal static string GetTableName<T>(out string conn)
|
|||
|
|
{
|
|||
|
|
Type t = typeof(T);
|
|||
|
|
return GetTableName(t, out conn);
|
|||
|
|
}
|
|||
|
|
internal static string GetTableName(Type t, out string conn)
|
|||
|
|
{
|
|||
|
|
conn = string.Empty;
|
|||
|
|
string[] items = t.FullName.Split('.');
|
|||
|
|
if (items.Length > 1)
|
|||
|
|
{
|
|||
|
|
conn = items[items.Length - 2] + "Conn";
|
|||
|
|
if (string.IsNullOrEmpty(AppConfig.GetConn(conn)))
|
|||
|
|
{
|
|||
|
|
conn = null;
|
|||
|
|
}
|
|||
|
|
items = null;
|
|||
|
|
}
|
|||
|
|
string tName = t.Name;
|
|||
|
|
object[] names = ReflectTool.GetAttributes(t, typeof(TableNameAttribute));
|
|||
|
|
if (names != null && names.Length > 0)
|
|||
|
|
{
|
|||
|
|
foreach (object item in names)
|
|||
|
|
{
|
|||
|
|
if (item is TableNameAttribute)
|
|||
|
|
{
|
|||
|
|
tName = ((TableNameAttribute)item).TableName;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
if (!string.IsNullOrEmpty(tName))
|
|||
|
|
{
|
|||
|
|
t = null;
|
|||
|
|
if (tName.EndsWith(AppConfig.DB.EntitySuffix))
|
|||
|
|
{
|
|||
|
|
tName = tName.Substring(0, tName.Length - AppConfig.DB.EntitySuffix.Length);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string fixName;
|
|||
|
|
conn = CrossDB.GetConn(tName, out fixName, conn);
|
|||
|
|
if (!string.IsNullOrEmpty(fixName))
|
|||
|
|
{
|
|||
|
|
tName = fixName;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return tName;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|