/// <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); }