/// <summary> /// 插入一个记录 /// </summary> /// <param name="obj"></param> /// <returns></returns> public int Insert(T obj, ValueSetList setList, bool fillIdentity) { int ret = -1; ret = DoInsert(obj, setList, fillIdentity); return(ret); }
/// <summary> /// 范围更新(慎用) /// </summary> /// <param name="lstValue">设置值</param> /// <param name="lstScope">条件</param> /// <returns></returns> public int UpdateByScope(T obj, ValueSetList lstValue, ScopeList lstScope) { int ret = 0; bool conUpdate = this.obj.BeforeUpdateByScope(obj, lstScope, lstValue, false); if (conUpdate) { ret = GetBaseContext().Update(obj, lstScope, lstValue, false); } this.obj.AfterUpdateByScope(ret, obj, lstScope, lstValue, false); return(ret); }
/// <summary> /// 更新实体 /// </summary> /// <param name="lstValue">强制设置值</param> /// <param name="optimisticConcurrency">是否并发控制</param> /// <returns></returns> public virtual int Update(ValueSetList lstValue, bool optimisticConcurrency) { DataAccessSetBase dal = GetBaseDataAccess(); foreach (EntityPropertyInfo epPk in dal.EntityInfo.PrimaryProperty) { object id = epPk.GetValue(this); if (DefaultType.IsDefaultValue(id)) { throw new Exception("主键:" + epPk.PropertyName + "的值不能为空"); } } return(dal.Update(this, null, lstValue, optimisticConcurrency)); }
/// 插入一条数据 /// </summary> /// <param name="entity">对象</param> /// <returns>大于0:插入完毕,小于0:插入失败</returns> public virtual object Insert(T entity, ValueSetList setList, bool fillIdentity) { DataAccessModel <T> entityDao = new DataAccessModel <T>(); object ret = Exists(entity); if (ret != null) { return(ret); } _affectedRows = entityDao.Insert(entity, setList, fillIdentity); return(ret); }
/// <summary> /// 更新 /// </summary> /// <param name="entity">对象</param> /// <param name="scorpList">范围更新的列表</param> /// <param name="lstValue">set实体</param> /// <param name="optimisticConcurrency">并发控制</param> /// <returns></returns> public virtual object Update(T entity, ScopeList scorpList, ValueSetList lstValue, bool optimisticConcurrency) { DataAccessModel <T> entityDao = new DataAccessModel <T>(); object ret = Exists(entity); if (ret != null) { return(ret); } _affectedRows = entityDao.Update(entity, scorpList, lstValue, optimisticConcurrency); return(ret); }
// <summary> // 根据条件获取第一条记录 // </summary> // <param name="scopeList">查询信息</param> // <returns></returns> //public T GetUnique(ScopeList scopeList) //{ // if (scopeList.HasInner) // { // return _cdal.GetUnique<T>(scopeList); // } // ParamList list = null; // T ret = default(T); // list = new ParamList(); // string sql = null; // SelectCondition sc = GetSelectContant(list, scopeList, GetSelectParams(scopeList)); // sql = CurEntityInfo.DBInfo.CurrentDbAdapter.GetTopSelectSql(sc, 1); // Dictionary<string,bool> cacheTables=null; // if(scopeList.UseCache) // { // cacheTables=_oper.DBInfo.QueryCache.CreateMap(CurEntityInfo.TableName); // } // using (IDataReader reader = _oper.Query(sql, list, cacheTables)) // { // if (reader.Read()) // { // ret = LoadFromReader(reader); // } // } // return ret; //} /// <summary> /// 修改记录 /// </summary> /// <param name="obj">修改的对象</param> /// <param name="scopeList">条件列表</param> /// <param name="optimisticConcurrency">是否进行并发控制</param> /// <returns></returns> public int Update(T obj, ScopeList scopeList, ValueSetList lstValue, bool optimisticConcurrency) { return(base.Update(obj, scopeList, lstValue, optimisticConcurrency)); }
/// <summary> /// 修改记录 /// </summary> /// <param name="obj">修改的对象</param> /// <param name="scopeList">条件列表</param> /// <param name="setList">Set值列表</param> /// <param name="optimisticConcurrency">是否进行并发控制</param> /// <returns></returns> public int Update(EntityBase obj, ScopeList scopeList, ValueSetList setList, bool optimisticConcurrency) { StringBuilder sql = new StringBuilder(500); ParamList list = new ParamList(); StringBuilder where = new StringBuilder(500); //where.Append("1=1"); Type type = EntityInfo.EntityType; List <VersionInfo> lstVersionInfo = null; int index = 0; KeyWordInfomation keyinfo = BQLValueItem.GetKeyInfo().Clone() as KeyWordInfomation; keyinfo.ParamList = list; keyinfo.OutPutModle = false; if (obj != null) { if (!(obj is IEntityProxy)) { throw new System.InvalidCastException("Update的实体类型必须为代理类,请用CH.Create<T>创建实体或者使用查询出来的实体来更新"); } ///读取属性别名 foreach (EntityPropertyInfo info in EntityInfo.PropertyInfo) { object curValue = info.GetValue(obj); if (!info.ReadOnly) { if (optimisticConcurrency == true && info.IsVersion) //并发控制 { object newValue = FillUpdateConcurrency(sql, info, list, curValue, ref index); FillWhereConcurrency(where, info, list, curValue, ref index); if (lstVersionInfo == null) { lstVersionInfo = new List <VersionInfo>(); } lstVersionInfo.Add(new VersionInfo(info, curValue, newValue));//添加信息 } else { //string paramVal = CurEntityInfo.DBInfo.CurrentDbAdapter.FormatValueName(DataAccessCommon.FormatParam(info.ParamName, index)); //string paramKey = CurEntityInfo.DBInfo.CurrentDbAdapter.FormatParamKeyName(DataAccessCommon.FormatParam(info.ParamName, index)); if (info.IsNormal) { //if (obj._dicUpdateProperty___ == null || obj._dicUpdateProperty___.Count == 0) //{ // //if (DefaultType.IsDefaultValue(curValue)) // //{ // continue; // //} //} if (!obj.HasPropertyChange(info.PropertyName)) { continue; } if (setList != null) { BQLValueItem bvalue = null; if (setList.TryGetValue(info.PropertyName, out bvalue)) { continue; } } sql.Append(","); sql.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatParam(info.ParamName)); sql.Append("="); if (curValue != null) { DBParameter dbPrm = list.NewParameter(info.SqlType, curValue, EntityInfo.DBInfo); sql.Append(dbPrm.ValueName); } else { sql.Append("null"); } } } } if (info.IsPrimaryKey && scopeList == null)//当不强制指定条件时候,用主键做更新条件 { //if (DefaultType.IsDefaultValue(curValue)) //{ // continue; //} //if (obj._dicUpdateProperty___ != null) //{ // if (!obj._dicUpdateProperty___.ContainsKey(info.PropertyName)) // { // continue; // } //} DBParameter dbPrm = list.NewParameter(info.SqlType, curValue, EntityInfo.DBInfo); where.Append(" and "); where.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatParam(info.ParamName)); where.Append("="); where.Append(dbPrm.ValueName); //primaryKeyValue=curValue; } index++; } } if (setList != null) { foreach (KeyValuePair <string, BQLValueItem> kvp in setList) { EntityPropertyInfo epinfo = EntityInfo.PropertyInfo[kvp.Key]; if (epinfo == null) { throw new Exception("实体:" + EntityInfo.EntityType.FullName + " 找不到属性:" + kvp.Key); } sql.Append(","); sql.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatParam(epinfo.ParamName)); sql.Append("="); sql.Append(kvp.Value.DisplayValue(keyinfo)); } } where.Append(DataAccessCommon.FillCondition(EntityInfo, list, scopeList)); if (sql.Length <= 0) { return(0); } else { sql.Remove(0, 1); } UpdateCondition con = new UpdateCondition(EntityInfo.DBInfo); con.Tables.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(EntityInfo.TableName)); con.UpdateSetValue.Append(sql); con.Condition.Append("1=1"); con.Condition.Append(where); int ret = -1; Dictionary <string, bool> cacheTables = null; cacheTables = _oper.DBInfo.QueryCache.CreateMap(EntityInfo.TableName); ret = ExecuteCommand(con.GetSql(true), list, CommandType.Text, cacheTables); if (obj != null && obj._dicUpdateProperty___ != null) { obj._dicUpdateProperty___.Clear(); } if (lstVersionInfo != null && lstVersionInfo.Count > 0) { foreach (VersionInfo info in lstVersionInfo) { info.Info.SetValue(obj, info.NewValue); } } return(ret); }
/// <summary> /// 进行插入操作 /// </summary> /// <param name="obj">要插入的对象</param> /// <param name="fillIdentity">是否要填充刚插入的实体的ID</param> /// <returns></returns> protected internal int DoInsert(EntityBase obj, ValueSetList setList, bool fillIdentity) { StringBuilder sqlParams = new StringBuilder(1000); StringBuilder sqlValues = new StringBuilder(1000); ParamList list = new ParamList(); string param = null; string svalue = null; List <EntityPropertyInfo> identityInfo = new List <EntityPropertyInfo>(); KeyWordInfomation keyinfo = BQLValueItem.GetKeyInfo().Clone() as KeyWordInfomation; keyinfo.ParamList = list; foreach (EntityPropertyInfo info in EntityInfo.PropertyInfo) { //EntityPropertyInfo info = enums.Current.Value; object curValue = info.GetValue(obj); if (info.Identity) { if (info.SqlType == DbType.Guid && info.FieldType == DefaultType.GUIDType) { curValue = Guid.NewGuid(); info.SetValue(obj, curValue); DBParameter prm = list.NewParameter(info.SqlType, curValue, EntityInfo.DBInfo); sqlParams.Append(","); sqlParams.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatParam(info.ParamName)); sqlValues.Append(","); sqlValues.Append(prm.ValueName); continue; } else { if (fillIdentity) { identityInfo.Add(info); } param = EntityInfo.DBInfo.CurrentDbAdapter.GetIdentityParamName(info); if (!string.IsNullOrEmpty(param)) { sqlParams.Append(","); sqlParams.Append(param); } svalue = EntityInfo.DBInfo.CurrentDbAdapter.GetIdentityParamValue(EntityInfo, info); if (!string.IsNullOrEmpty(svalue)) { sqlValues.Append(","); sqlValues.Append(svalue); } continue; } } else if (info.IsVersion) //版本初始值 { object conValue = curValue; if (conValue == null) { conValue = GetDefaultConcurrency(info); } if (conValue != null) { DBParameter prm = list.NewParameter(info.SqlType, conValue, EntityInfo.DBInfo); sqlParams.Append(","); sqlParams.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatParam(info.ParamName)); sqlValues.Append(","); sqlValues.Append(prm.ValueName); continue; } } else { BQLValueItem bvalue = null; if (setList != null && setList.TryGetValue(info.PropertyName, out bvalue)) { sqlParams.Append(","); sqlParams.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatParam(info.ParamName)); sqlValues.Append(","); sqlValues.Append(bvalue.DisplayValue(keyinfo)); continue; } else if (curValue == null) { continue; } else { DBParameter prmValue = list.NewParameter(info.SqlType, curValue, EntityInfo.DBInfo); sqlParams.Append(","); sqlParams.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatParam(info.ParamName)); sqlValues.Append(","); sqlValues.Append(prmValue.ValueName); } } } if (sqlParams.Length > 0) { sqlParams.Remove(0, 1); } else { return(0); } if (sqlValues.Length > 0) { sqlValues.Remove(0, 1); } else { return(0); } InsertCondition con = new InsertCondition(EntityInfo.DBInfo); con.Tables.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(EntityInfo.TableName)); con.SqlParams.Append(sqlParams.ToString()); con.SqlValues.Append(sqlValues.ToString()); int ret = -1; con.DbParamList = list; using (BatchAction ba = _oper.StarBatchAction()) { string sql = con.GetSql(true); Dictionary <string, bool> cacheTables = null; cacheTables = _oper.DBInfo.QueryCache.CreateMap(EntityInfo.TableName); ret = ExecuteCommand(sql, list, CommandType.Text, cacheTables); if (identityInfo.Count > 0 && fillIdentity) { foreach (EntityPropertyInfo pkInfo in identityInfo) { sql = EntityInfo.DBInfo.CurrentDbAdapter.GetIdentitySQL(pkInfo); using (IDataReader reader = _oper.Query(sql, new ParamList(), null)) { if (reader.Read()) { if (!reader.IsDBNull(0)) { EntityInfo.DBInfo.CurrentDbAdapter.SetObjectValueFromReader(reader, 0, obj, pkInfo, !pkInfo.TypeEqual(reader, 0)); //obj.PrimaryKeyChange(); ret = 1; } } } } } } return(ret); }
/// <summary> /// 更新 /// </summary> /// <param name="entity">对象</param> /// <param name="scorpList">范围更新的列表</param> /// <returns>大于0:更新完毕,小于0:更新失败</returns> public object Update(T entity, ScopeList scorpList, ValueSetList lstValue) { return(Update(entity, scorpList, lstValue, false)); }
/// <summary> /// 范围更新后触发 /// </summary> /// <param name="affected">影响行数</param> /// <param name="entity">实体</param> /// <param name="scorpList">范围更新条件</param> /// <param name="lstValue">赋值</param> /// <param name="optimisticConcurrency">是否用并发锁</param> protected internal virtual void AfterUpdateByScope(int affected, EntityBase entity, ScopeList scorpList, ValueSetList lstValue, bool optimisticConcurrency) { }
/// <summary> /// 范围更新前触发 /// </summary> /// <param name="entity">实体</param> /// <param name="scorpList">范围更新条件</param> /// <param name="lstValue">赋值</param> /// <param name="optimisticConcurrency">是否用并发锁</param> /// <returns>是否继续执行更新</returns> protected internal virtual bool BeforeUpdateByScope(EntityBase entity, ScopeList scorpList, ValueSetList lstValue, bool optimisticConcurrency) { return(true); }
/// <summary> /// 保存实体并填充ID /// </summary> /// <param name="fillPrimaryKey">是否填充实体</param> /// <returns></returns> public virtual int Insert(ValueSetList setList, bool fillPrimaryKey) { DataAccessSetBase dal = GetBaseDataAccess(); return(dal.DoInsert(this, setList, fillPrimaryKey)); }
/// <summary> /// 修改记录 /// </summary> /// <param name="entity">实体</param> /// <param name="scopeList">范围更新的集合</param> /// <param name="valueList">值集合</param> /// <param name="optimisticConcurrency">是否并发</param> /// <returns></returns> public int Update(T entity, ScopeList scopeList, ValueSetList valueList, bool optimisticConcurrency) { return(base.Update(entity, scopeList, valueList, optimisticConcurrency)); }