224 lines
7.0 KiB
C#
224 lines
7.0 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
using CYQ.Data.Table;
|
|||
|
|
|
|||
|
|
|
|||
|
|
namespace CYQ.Data.Orm
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ٲ<EFBFBD><D9B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ࡣ
|
|||
|
|
/// </summary>
|
|||
|
|
public static class DBFast
|
|||
|
|
{
|
|||
|
|
/// <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)
|
|||
|
|
{
|
|||
|
|
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>()
|
|||
|
|
{
|
|||
|
|
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)
|
|||
|
|
{
|
|||
|
|
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)
|
|||
|
|
{
|
|||
|
|
int count;
|
|||
|
|
return Select<T>(1, topN, where, out count, columns);
|
|||
|
|
}
|
|||
|
|
public static List<T> Select<T>(int pageIndex, int pageSize, params string[] columns)
|
|||
|
|
{
|
|||
|
|
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)
|
|||
|
|
{
|
|||
|
|
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)
|
|||
|
|
{
|
|||
|
|
MDataTable dt = null;
|
|||
|
|
using (MAction action = GetMAction<T>())
|
|||
|
|
{
|
|||
|
|
if (columns != null && columns.Length > 0)
|
|||
|
|
{
|
|||
|
|
action.SetSelectColumns(columns);
|
|||
|
|
}
|
|||
|
|
dt = action.Select(pageIndex, pageSize, where, out count);
|
|||
|
|
}
|
|||
|
|
return dt.ToList<T>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// ɾ<><C9BE><EFBFBD><EFBFBD>¼
|
|||
|
|
/// </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)
|
|||
|
|
{
|
|||
|
|
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();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return action.Delete(where);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public static bool Insert<T>(T t)
|
|||
|
|
{
|
|||
|
|
return Insert<T>(t, InsertOp.ID, false);
|
|||
|
|
}
|
|||
|
|
public static bool Insert<T>(T t, InsertOp op)
|
|||
|
|
{
|
|||
|
|
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)
|
|||
|
|
{
|
|||
|
|
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)
|
|||
|
|
{
|
|||
|
|
return Update<T>(t, null);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>¼<EFBFBD>¼
|
|||
|
|
/// </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>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool Update<T>(T t, object where)
|
|||
|
|
{
|
|||
|
|
using (MAction action = GetMAction<T>())
|
|||
|
|
{
|
|||
|
|
action.Data.LoadFrom(t, BreakOp.Null);
|
|||
|
|
return action.Update(where);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool Exists<T>(object where)
|
|||
|
|
{
|
|||
|
|
using (MAction action = GetMAction<T>())
|
|||
|
|
{
|
|||
|
|
return action.Exists(where);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public static int GetCount<T>(object where)
|
|||
|
|
{
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
private static string GetTableName<T>(out string conn)
|
|||
|
|
{
|
|||
|
|
conn = string.Empty;
|
|||
|
|
Type t = typeof(T);
|
|||
|
|
|
|||
|
|
string[] items = t.FullName.Split('.');
|
|||
|
|
if (items.Length > 1)
|
|||
|
|
{
|
|||
|
|
conn = items[items.Length - 2] + "Conn";
|
|||
|
|
items = null;
|
|||
|
|
}
|
|||
|
|
string tName = t.Name;
|
|||
|
|
t = null;
|
|||
|
|
if (tName.EndsWith(AppConfig.EntitySuffix))
|
|||
|
|
{
|
|||
|
|
tName = tName.Substring(0, tName.Length - AppConfig.EntitySuffix.Length);
|
|||
|
|
}
|
|||
|
|
tName = CYQ.Data.Tool.DBTool.GetMapTableName(conn, tName);
|
|||
|
|
return tName;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|