using System; using CYQ.Data.Table; using CYQ.Data.Tool; using System.Data; using CYQ.Data.UI; using CYQ.Data.Aop; using CYQ.Data.Json; namespace CYQ.Data.Orm { /// /// ORM 基类【内置RealProxy,能拦截属性赋值变化,实现按需要插入或更新,NetCore下该属性无效】 /// [AopAttribute] public abstract partial class OrmBase : ContextBoundObject, IDisposable { /// /// 获取 - 基类 ORM 的相关信息【Orm内部属性】 /// [JsonIgnore] public OrmBaseInfo BaseInfo { get { return sob.BaseInfo; } } private SimpleOrmBaseDefaultInstance sob = new SimpleOrmBaseDefaultInstance(); ///// ///// 字段来源(当字段变更时,可以设置此属性来切换更新) ///// //public static FieldSource FieldSource //{ // get // { // return SimpleOrmBase.FieldSource; // } // set // { // SimpleOrmBase.FieldSource = value; // } //} //Object entity;//实体对象 //Type typeInfo;//实体对象类型 internal 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, null, true); } /// /// 初始化状态[继承此基类的实体在构造函数中需调用此方法] /// /// 实体对象,一般写:this /// 表名,如:Users protected void SetInit(Object entityInstance, string tableName) { sob.SetInit2(entityInstance, tableName, null, true); } /// /// 初始化状态[继承此基类的实体在构造函数中需调用此方法] /// /// 实体对象,一般写:this /// 表名,如:Users /// 数据链接,单数据库时可写Null,或写默认链接配置项:"Conn",或直接数据库链接字符串 protected void SetInit(Object entityInstance, string tableName, string conn) { sob.SetInit2(entityInstance, tableName, conn, true); } /// /// 初始化状态[继承此基类的实体在构造函数中需调用此方法] /// /// 实体对象,一般写:this /// 表名,如:Users /// 数据链接,单数据库时可写Null,或写默认链接配置项:"Conn",或直接数据库链接字符串 /// 当执行发生异常时,是否输出日志 protected void SetInit(Object entityInstance, string tableName, string conn, bool isWriteLogOnError) { sob.SetInit2(entityInstance, tableName, conn, isWriteLogOnError); } /// /// 设置值,例如:[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 sob.Insert(false, InsertOp.ID, false); } /// /// 插入数据 /// /// 插入选项 public bool Insert(InsertOp option) { return sob.Insert(false, option, false); } /// /// 插入数据 /// /// 插入主键 public bool Insert(InsertOp option, bool insertID) { return sob.Insert(false, option, insertID); } /// /// 插入数据 /// /// 是否自动获取值[自动从控件获取值,需要先调用this.UI.SetAutoPrefix或this.UI.SetAutoParentControl方法设置控件前缀] public bool Insert(bool autoSetValue) { return sob.Insert(autoSetValue, InsertOp.ID, false); } /// /// 插入数据 /// /// 是否自动获取值[自动从控件获取值,需要先调用this.UI.SetAutoPrefix或this.UI.SetAutoParentControl方法设置控件前缀] public bool Insert(bool autoSetValue, InsertOp option) { return sob.Insert(autoSetValue, option, false); } /// /// 插入数据 /// /// 是否自动获取值[自动从控件获取值,需要先调用this.UI.SetAutoPrefix或this.UI.SetAutoParentControl方法设置控件前缀] /// 插入选项 /// 自定义插入主键 public bool Insert(bool autoSetValue, InsertOp option, bool insertID) { return sob.Insert(autoSetValue, option, insertID); } #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); } /// /// 查询1条数据并返回新的一行 /// public MDataRow Get(object where) { if (sob.Fill(where)) { return sob.Action.Data.Clone(); } return null; } /// /// 列表查询 /// 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() { return sob.GetCount(); } /// /// 获取记录总数 /// 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) { sob.LoadFrom(jsonOrEntity); } /// /// 本方法可以在单表使用时查询指定的列[设置后可使用Fill与Select方法] /// 提示:分页查询时,排序条件的列必须指定选择。 /// /// 可设置多个列名[调用Fill或Select后,本参数将被清除] public void SetSelectColumns(params object[] columnNames) { sob.SetSelectColumns(columnNames); } /// /// 参数化传参[当Where条件为参数化(如:name=@name)语句时使用] /// /// 参数名称 /// 参数值 /// 参数类型 public void SetPara(object paraName, object value, DbType dbType) { sob.SetPara(paraName, value, dbType); } /// /// 设置参数化 /// public void SetPara(object paraName, object value) { sob.SetPara(paraName, value); } /// /// 清除(SetPara设置的)自定义参数 /// public void ClearPara() { sob.ClearPara(); } /// /// 清空所有值 /// public void Clear() { sob.Clear(); } /// /// 更新操作的自定义表达式设置。 /// /// 例如a字段值自加1:"a=a+1" public void SetExpression(string updateExpression) { sob.SetExpression(updateExpression); } /// /// UI 操作【WebForm、Winform、WPF等自动取值或赋值】【Orm内部属性】 /// [JsonIgnore] public MActionUI UI { get { return sob.UI; } } } }