using System;
using System.Collections.Generic;
using System.Text;
using CYQ.Data.Table;
namespace CYQ.Data.Orm
{
///
/// 快速操作操作类。
///
public static class DBFast
{
///
/// 查找单条记录
///
/// 实体类型
/// 条件
/// 指定查询的列(可选)
///
public static T Find(object where, params string[] columns)
{
T result = default(T);
MDataRow row = null;
using (MAction action = GetMAction())
{
if (columns != null && columns.Length > 0)
{
action.SetSelectColumns(columns);
}
if (action.Fill(where))
{
row = action.Data;
}
}
if (row != null)
{
result = row.ToEntity();
}
return result;
}
public static List Select()
{
int count;
return Select(0, 0, null, out count, null);
}
///
/// 列表查询
///
/// 查询条件[可附带 order by 语句]
///
public static List Select(string where, params string[] columns)
{
int count;
return Select(0, 0, where, out count, columns);
}
///
/// 列表查询
///
/// 查询几条
/// 查询条件[可附带 order by 语句]
///
public static List Select(int topN, string where, params string[] columns)
{
int count;
return Select(1, topN, where, out count, columns);
}
public static List Select(int pageIndex, int pageSize, params string[] columns)
{
int count;
return Select(pageIndex, pageSize, null, out count, columns);
}
public static List Select(int pageIndex, int pageSize, string where, params string[] columns)
{
int count;
return Select(pageIndex, pageSize, where, out count, columns);
}
///
/// 查找多条记录
///
/// 实体类型
/// 第N页
/// 每页N条
/// 条件
/// 返回记录总数
/// 指定查询的列(可选)
///
public static List Select(int pageIndex, int pageSize, object where, out int count, params string[] columns)
{
MDataTable dt = null;
using (MAction action = GetMAction())
{
if (columns != null && columns.Length > 0)
{
action.SetSelectColumns(columns);
}
dt = action.Select(pageIndex, pageSize, where, out count);
}
return dt.ToList();
}
///
/// 删除记录
///
/// 实体类型
/// 条件
///
public static bool Delete(object where)
{
if (where == null) { return false; }
using (MAction action = GetMAction())
{
if (typeof(T).FullName == where.GetType().FullName)//传进实体类
{
action.Data.LoadFrom(where);
return action.Delete();
}
else
{
return action.Delete(where);
}
}
}
public static bool Insert(T t)
{
return Insert(t, InsertOp.ID, false);
}
public static bool Insert(T t, InsertOp op)
{
return Insert(t, op, false);
}
///
/// 添加一条记录
///
/// 实体类型
/// 实体对象
///
public static bool Insert(T t, InsertOp op, bool insertID)
{
bool result = false;
MDataRow row = null;
using (MAction action = GetMAction())
{
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)
{
return Update(t, null);
}
///
/// 更新记录
///
/// 实体类型
/// 实体对象
/// 条件
///
public static bool Update(T t, object where)
{
using (MAction action = GetMAction())
{
action.Data.LoadFrom(t, BreakOp.Null);
return action.Update(where);
}
}
///
/// 是否存在指定的条件
///
public static bool Exists(object where)
{
using (MAction action = GetMAction())
{
return action.Exists(where);
}
}
public static int GetCount(object where)
{
using (MAction action = GetMAction())
{
return action.GetCount(where);
}
}
private static MAction GetMAction()
{
string conn = string.Empty;
MAction action = new MAction(GetTableName(out conn), conn);
//action.SetAopState(CYQ.Data.Aop.AopOp.CloseAll);
return action;
}
private static string GetTableName(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;
}
}
}