using System;
using System.Collections.Generic;
using System.Text;
using CYQ.Data.Table;
using System.Reflection;
using CYQ.Data.Tool;
using CYQ.Data.Cache;
using CYQ.Data.SQL;
using System.Data;
using CYQ.Data.UI;
using CYQ.Data.Aop;
namespace CYQ.Data.Orm
{
///
/// ORM扩展基类
///
[AopAttribute]
public abstract partial class OrmBase : ContextBoundObject, IDisposable
{
private SimpleOrmBase sob = new SimpleOrmBase();
///
/// 字段来源(当字段变更时,可以设置此属性来切换更新)
///
public static FieldSource FieldSource
{
get
{
return SimpleOrmBase.FieldSource;
}
set
{
SimpleOrmBase.FieldSource = value;
}
}
//Object entity;//实体对象
//Type typeInfo;//实体对象类型
MAction action
{
get
{
return sob.Action;
}
}
///
/// 构造函数
///
public OrmBase()
{
sob.IsUseAop = true;
}
///
/// 设置Aop状态
///
///
public void SetAopState(AopOp op)
{
sob.SetAopState(op);
}
///
/// 初始化状态[继承此基类的实体在构造函数中需调用此方法]
///
/// 实体对象,一般写:this
protected void SetInit(Object entityInstance)
{
sob.SetInit2(entityInstance, null, AppConfig.DB.DefaultConn);
}
///
/// 初始化状态[继承此基类的实体在构造函数中需调用此方法]
///
/// 实体对象,一般写:this
/// 表名,如:Users
protected void SetInit(Object entityInstance, string tableName)
{
sob.SetInit2(entityInstance, tableName, AppConfig.DB.DefaultConn);
}
///
/// 初始化状态[继承此基类的实体在构造函数中需调用此方法]
///
/// 实体对象,一般写:this
/// 表名,如:Users
/// 数据链接,单数据库时可写Null,或写默认链接配置项:"Conn",或直接数据库链接字符串
protected void SetInit(Object entityInstance, string tableName, string conn)
{
sob.SetInit2(entityInstance, tableName, conn);
}
protected void SetInit(Object entityInstance, string tableName, string conn, AopOp op)
{
sob.SetInit2(entityInstance, tableName, conn, op);
}
///
/// 设置值,例如:[action.Set(TableName.ID,10);]
///
/// 字段名称,可用枚举如:[TableName.ID]
/// 要设置给字段的值
///
/// set示例:action.Set(Users.UserName,"路过秋天");
/// get示例:int id=action.Get<int>(Users.ID);
///
protected void Set(object key, object value)
{
if (sob != null)
{
sob.Set(key, value);
}
}
#region 基础增删改查 成员
#region 插入
///
/// 插入数据
///
public bool Insert()
{
return Insert(false, InsertOp.ID);
}
///
/// 插入数据
///
/// 插入选项
public bool Insert(InsertOp option)
{
return Insert(false, option);
}
///
/// 插入数据
///
/// 自动从控制获取值
public bool Insert(bool autoSetValue)
{
return Insert(autoSetValue, InsertOp.ID);
}
///
/// 插入数据
///
/// 自动从控制获取值
/// 插入选项
public bool Insert(bool autoSetValue, InsertOp option)
{
return sob.Insert(autoSetValue, option, AllowInsertID);
}
#endregion
#region 更新
///
/// 更新数据
///
public bool Update()
{
return sob.Update();
}
///
/// 更新数据
///
/// where条件,可直接传id的值如:[88],或传完整where条件如:[id=88 and name='路过秋天']
public bool Update(object where)
{
return sob.Update(where);
}
///
/// 更新数据
///
/// where条件,可直接传id的值如:[88],或传完整where条件如:[id=88 and name='路过秋天']
/// 是否自动获取值[自动从控件获取值,需要先调用SetAutoPrefix或SetAutoParentControl方法设置控件前缀]
public bool Update(object where, bool autoSetValue)
{
return sob.Update(where, autoSetValue);
}
#endregion
#region 删除
///
/// 删除数据
///
public bool Delete()
{
return sob.Delete();
}
///
/// 删除数据
///
/// where条件,可直接传id的值如:[88],或传完整where条件如:[id=88 and name='路过秋天']
public bool Delete(object where)
{
return sob.Delete(where);
}
#endregion
#region 查询
///
/// 查询1条数据(自动取值)
///
///
public bool Fill()
{
return sob.Fill();
}
///
/// 查询1条数据
///
public bool Fill(object where)
{
return sob.Fill(where);
}
///
/// 列表查询
///
public MDataTable Select()
{
int count;
return Select(0, 0, null, out count);
}
///
/// 列表查询
///
/// 查询条件[可附带 order by 语句]
///
public MDataTable Select(string where)
{
int count;
return Select(0, 0, where, out count);
}
///
/// 列表查询
///
/// 查询几条
/// 查询条件[可附带 order by 语句]
///
public MDataTable Select(int topN, string where)
{
int count;
return Select(0, topN, where, out count);
}
public MDataTable Select(int pageIndex, int pageSize)
{
int count;
return Select(pageIndex, pageSize, null, out count);
}
public MDataTable Select(int pageIndex, int pageSize, string where)
{
int count;
return Select(pageIndex, pageSize, where, out count);
}
///
/// 带分布功能的选择[多条件查询,选择所有时只需把PageIndex/PageSize设置为0]
///
/// 第几页
/// 每页数量[为0时默认选择所有]
/// 查询条件[可附带 order by 语句]
/// 返回的记录总数
public MDataTable Select(int pageIndex, int pageSize, string where, out int count)
{
return sob.Select(pageIndex, pageSize, where, out count);
}
///
/// 获取记录总数
///
public int GetCount(object where)
{
return sob.GetCount(where);
}
///
/// 查询是否存在指定的条件的数据
///
public bool Exists(object where)
{
return sob.Exists(where);
}
#endregion
#endregion
#region IDisposable 成员
///
/// 释放资源
///
public void Dispose()
{
if (sob != null)
{
sob.Dispose();
}
}
#endregion
///
/// 批量加载值(可从Json、实体对象、MDataRow、泛型字段等批量加载值)
///
/// json字符串或实体对象
public void LoadFrom(object jsonOrEntity)
{
action.Data.LoadFrom(jsonOrEntity);
sob.SetValueToEntity();
}
///
/// 本方法可以在单表使用时查询指定的列[设置后可使用Fill与Select方法]
/// 提示:分页查询时,排序条件的列必须指定选择。
///
/// 可设置多个列名[调用Fill或Select后,本参数将被清除]
public void SetSelectColumns(params object[] columnNames)
{
action.SetSelectColumns(columnNames);
}
///
/// 参数化传参[当Where条件为参数化(如:name=@name)语句时使用]
///
/// 参数名称
/// 参数值
/// 参数类型
public void SetPara(object paraName, object value, DbType dbType)
{
action.SetPara(paraName, value, dbType);
}
///
/// 清除(SetPara设置的)自定义参数
///
public void ClearPara()
{
action.ClearPara();
}
///
/// 更新操作的自定义表达式设置。
///
/// 例如a字段值自加1:"a=a+1"
public void SetExpression(string updateExpression)
{
action.SetExpression(updateExpression);
}
///
/// 是否允许插入自增ID
///
public bool AllowInsertID
{
get
{
return action.AllowInsertID;
}
set { action.AllowInsertID = value; }
}
///
/// 获取或设置表名
///
public string TableName
{
get
{
if (action != null)
{
return action.TableName;
}
return string.Empty;
}
set
{
if (action != null)
{
action.TableName = value;
}
}
}
private MDataColumn _Columns = null;
///
/// 获取表的结构。
///
public MDataColumn Columns
{
get
{
return _Columns;
}
}
///
/// 执行的调试语句输出
///
public string DebugInfo
{
get
{
return action.DebugInfo;
}
}
///
/// 当前的数据库类型
///
public DalType DalType
{
get
{
return action.DalType;
}
}
///
/// 原始行数据。
///
public MDataRow RawData
{
get
{
return action.Data;
}
}
///
/// UI操作
///
public MActionUI UI
{
get
{
if (action.UI.IsOnAfterGetFromEventNull)
{
action.UI.OnAfterGetFromEvent += new CYQ.Data.UI.MActionUI.OnAfterGetFrom(UI_OnAfterGetFromEvent);
}
return action.UI;
}
}
void UI_OnAfterGetFromEvent(string propValue)
{
if (!string.IsNullOrEmpty(propValue))
{
sob.SetValueToEntity(propValue);
}
}
}
}