public override int Update <TEntity>(Expression <Func <TEntity, bool> > condition, Expression <Func <TEntity, TEntity> > content, string table) { Utils.CheckNull(condition); Utils.CheckNull(content); TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(typeof(TEntity)); Dictionary <MemberInfo, Expression> updateColumns = InitMemberExtractor.Extract(content); DbTable explicitDbTable = null; if (table != null) { explicitDbTable = new DbTable(table, typeDescriptor.Table.Schema); } DefaultExpressionParser expressionParser = typeDescriptor.GetExpressionParser(explicitDbTable); DbExpression conditionExp = expressionParser.ParseFilterPredicate(condition); DbUpdateExpression e = new DbUpdateExpression(explicitDbTable ?? typeDescriptor.Table, conditionExp); foreach (var kv in updateColumns) { MemberInfo key = kv.Key; MappingMemberDescriptor memberDescriptor = typeDescriptor.TryGetMappingMemberDescriptor(key); if (memberDescriptor == null) { throw new ChloeException(string.Format("The member '{0}' does not map any column.", key.Name)); } if (memberDescriptor.IsPrimaryKey) { throw new ChloeException(string.Format("Could not update the primary key '{0}'.", memberDescriptor.Column.Name)); } SequenceAttribute attr = (SequenceAttribute)memberDescriptor.GetCustomAttribute(typeof(SequenceAttribute)); if (attr != null) { throw new ChloeException(string.Format("Could not update the column '{0}',because it's mapping member has define a sequence.", memberDescriptor.Column.Name)); } e.UpdateColumns.Add(memberDescriptor.Column, expressionParser.Parse(kv.Value)); } if (e.UpdateColumns.Count == 0) { return(0); } return(this.ExecuteSqlCommand(e)); }
static bool HasSequenceAttribute(MappingMemberDescriptor memberDescriptor, out string sequenceName) { sequenceName = null; SequenceAttribute attr = (SequenceAttribute)memberDescriptor.GetCustomAttribute(typeof(SequenceAttribute)); if (attr != null) { if (string.IsNullOrEmpty(attr.Name)) { throw new ChloeException("Sequence name can not be empty."); } sequenceName = attr.Name; return(true); } return(false); }
public override int Update <T>(Expression <Func <T, T> > body, Expression <Func <T, bool> > condition) { Utils.CheckNull(body); Utils.CheckNull(condition); TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(typeof(T)); Dictionary <MemberInfo, Expression> updateColumns = InitMemberExtractor.Extract(body); DbExpression conditionExp = typeDescriptor.Visitor.VisitFilterPredicate(condition); DbUpdateExpression e = new DbUpdateExpression(typeDescriptor.Table, conditionExp); foreach (var kv in updateColumns) { MemberInfo key = kv.Key; MappingMemberDescriptor memberDescriptor = typeDescriptor.TryGetMappingMemberDescriptor(key); if (memberDescriptor == null) { throw new ChloeException(string.Format("The member '{0}' does not map any column.", key.Name)); } if (memberDescriptor.IsPrimaryKey) { throw new ChloeException(string.Format("Could not update the primary key '{0}'.", memberDescriptor.Column.Name)); } AutoIncrementAttribute attr = (AutoIncrementAttribute)memberDescriptor.GetCustomAttribute(typeof(AutoIncrementAttribute)); if (attr != null) { throw new ChloeException(string.Format("Could not update the identity column '{0}'.", memberDescriptor.Column.Name)); } e.UpdateColumns.Add(memberDescriptor.Column, typeDescriptor.Visitor.Visit(kv.Value)); } return(this.ExecuteSqlCommand(e)); }
public override int Update <TEntity>(TEntity entity, string table) { Utils.CheckNull(entity); TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(entity.GetType()); EnsureMappingTypeHasPrimaryKey(typeDescriptor); MappingMemberDescriptor keyMemberDescriptor = typeDescriptor.PrimaryKey; MemberInfo keyMember = keyMemberDescriptor.MemberInfo; object keyVal = null; IEntityState entityState = this.TryGetTrackedEntityState(entity); Dictionary <MappingMemberDescriptor, DbExpression> updateColumns = new Dictionary <MappingMemberDescriptor, DbExpression>(); foreach (var kv in typeDescriptor.MappingMemberDescriptors) { MemberInfo member = kv.Key; MappingMemberDescriptor memberDescriptor = kv.Value; if (member == keyMember) { keyVal = memberDescriptor.GetValue(entity); keyMemberDescriptor = memberDescriptor; continue; } SequenceAttribute attr = (SequenceAttribute)memberDescriptor.GetCustomAttribute(typeof(SequenceAttribute)); if (attr != null) { continue; } object val = memberDescriptor.GetValue(entity); if (entityState != null && !entityState.HasChanged(memberDescriptor, val)) { continue; } DbExpression valExp = DbExpression.Parameter(val, memberDescriptor.MemberInfoType); updateColumns.Add(memberDescriptor, valExp); } if (keyVal == null) { throw new ChloeException(string.Format("The primary key '{0}' could not be null.", keyMember.Name)); } if (updateColumns.Count == 0) { return(0); } DbTable dbTable = table == null ? typeDescriptor.Table : new DbTable(table, typeDescriptor.Table.Schema); DbExpression left = new DbColumnAccessExpression(dbTable, keyMemberDescriptor.Column); DbExpression right = DbExpression.Parameter(keyVal, keyMemberDescriptor.MemberInfoType); DbExpression conditionExp = new DbEqualExpression(left, right); DbUpdateExpression e = new DbUpdateExpression(dbTable, conditionExp); foreach (var item in updateColumns) { e.UpdateColumns.Add(item.Key.Column, item.Value); } int ret = this.ExecuteSqlCommand(e); if (entityState != null) { entityState.Refresh(); } return(ret); }
public override int Update <TEntity>(TEntity entity, string table) { Utils.CheckNull(entity); TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(entity.GetType()); EnsureMappingTypeHasPrimaryKey(typeDescriptor); Dictionary <MappingMemberDescriptor, object> keyValueMap = CreateKeyValueMap(typeDescriptor); IEntityState entityState = this.TryGetTrackedEntityState(entity); Dictionary <MappingMemberDescriptor, DbExpression> updateColumns = new Dictionary <MappingMemberDescriptor, DbExpression>(); foreach (var kv in typeDescriptor.MappingMemberDescriptors) { MappingMemberDescriptor memberDescriptor = kv.Value; if (keyValueMap.ContainsKey(memberDescriptor)) { keyValueMap[memberDescriptor] = memberDescriptor.GetValue(entity); continue; } SequenceAttribute attr = (SequenceAttribute)memberDescriptor.GetCustomAttribute(typeof(SequenceAttribute)); if (attr != null) { continue; } object val = memberDescriptor.GetValue(entity); if (entityState != null && !entityState.HasChanged(memberDescriptor, val)) { continue; } DbExpression valExp = DbExpression.Parameter(val, memberDescriptor.MemberInfoType); updateColumns.Add(memberDescriptor, valExp); } if (updateColumns.Count == 0) { return(0); } DbTable dbTable = table == null ? typeDescriptor.Table : new DbTable(table, typeDescriptor.Table.Schema); DbExpression conditionExp = MakeCondition(keyValueMap, dbTable); DbUpdateExpression e = new DbUpdateExpression(dbTable, conditionExp); foreach (var item in updateColumns) { e.UpdateColumns.Add(item.Key.Column, item.Value); } int ret = this.ExecuteSqlCommand(e); if (entityState != null) { entityState.Refresh(); } return(ret); }