internal static WhereParameters GetRemoveWhere <TEntity>(this IDbEngine engine, object entityOrPKValues) { if (engine == null) { throw new ArgumentNullException(nameof(engine)); } if (entityOrPKValues == null) { throw new ArgumentNullException(nameof(entityOrPKValues)); } /* * 1、Remove(val) * 2、Remove({key=val}) * 3、Remove({key=val1},val2) * 4、Remove([{key1=val2,key2=val2},{key1=val3,key2=val4}]) */ var mapper = TypeMapper.Instance <TEntity> .Mapper.ThrowWithNotFoundKeys(); if (!(entityOrPKValues is string) && entityOrPKValues is Collections.IEnumerable) { entityOrPKValues = ((Collections.IEnumerable)entityOrPKValues).Cast <object>().ToArray(); } var type = entityOrPKValues.GetType(); if (mapper.KeyProperties.Length == 1) {//- 单个主键,1,2,3 var keyName = mapper.KeyProperties[0].Name; if (type.IsArray) {//-3 var array = (Array)entityOrPKValues; List <object> newValues = new List <object>(array.Length); foreach (var item in array) { var itemType = item.GetType(); if (itemType.IsSimpleType()) { newValues.Add(item); } else { var keyValue = TypeMapper.Create(itemType)[keyName]?.GetValue(item, true); if (keyValue == null) { throw CreateKeyNotFoundException <TEntity>(keyName); } newValues.Add(keyValue); } } var builder = new SqlBuilder(engine); builder.Where(keyName, keyName, newValues.ToArray()); return(new WhereParameters(builder.WhereText, builder.Parameters)); } if (!type.IsSimpleType()) {//-2 entityOrPKValues = TypeMapper.Create(type)[keyName]?.GetValue(entityOrPKValues, true); if (entityOrPKValues == null) { throw CreateKeyNotFoundException <TEntity>(keyName); } } //- 1,2 var ps = new ExecuteParameterCollection(keyName, entityOrPKValues); return(new WhereParameters(CreateWhere(engine, ps), ps)); } else {//- 多个主键,4 if (!type.IsArray) { throw new NotSupportedException($"类型“{typeof(TEntity).FullName}”执行删除命令时,{nameof(entityOrPKValues)} 参数必须是一个数组。"); } var array = (Array)entityOrPKValues; var builder = new SqlBuilder(engine); var factory = engine.Provider.SqlFactory; builder.Where(); foreach (var item in array) { var itemType = item.GetType(); if (itemType.IsSimpleType()) { throw new NotSupportedException($"类型“{typeof(TEntity).FullName}”包含多个主键,{nameof(entityOrPKValues)} 参数所有值都必须是一个复杂对象。"); } else { var itemMapper = TypeMapper.Create(itemType); builder.Or().BeginGroup(); foreach (var keyProp in mapper.KeyProperties) { var tmpKV = itemMapper[keyProp.Name]?.GetValue(item, true); if (tmpKV == null) { throw CreateKeyNotFoundException <TEntity>(keyProp.Name); } builder.AndValue(keyProp.Name, tmpKV); } builder.EndGroup(); } } return(new WhereParameters(builder.WhereText, builder.Parameters)); } }
private static CompareResult Compare(string name, Type type, object t1, object t2) { type = type.GetNullableType(); switch (Type.GetTypeCode(type)) { case TypeCode.Object: if (t1 == null || t2 == null) { goto default; } if (type.IsSubclassOf(Types.Exception) || type == Types.Exception) { t1 = t1.ToString(); t2 = t2.ToString(); goto default; } if (type.IsArray) { var a1 = t1 as Array; var a2 = t2 as Array; if (a1 == null || a2 == null) { goto default; } if (a1.Length != a2.Length) { return new CompareResult() { Name = "数组长度", Value1 = a1.Length, Value2 = a2.Length } } ; for (int i = 0; i < a1.Length; i++) { var r = Compare(a1.GetValue(i), a2.GetValue(i)); if (r != null) { return(r); } } } else if (type.IsSubclassOf(Types.IDictionary)) { var a1 = t1 as IDictionary; var a2 = t2 as IDictionary; if (a1 == null || a2 == null) { goto default; } if (a1.Count != a2.Count) { return new CompareResult() { Name = "字典大小", Value1 = a1.Count, Value2 = a2.Count } } ; foreach (DictionaryEntry item in a1) { if (!a2.Contains(item.Key)) { return new CompareResult() { Name = "字典键", Value1 = item.Key, Value2 = null } } ; var r = Compare(item.Value, a2[item.Key]); if (r != null) { return(r); } } } else if (type.IsSubclassOf(Types.IEnumerable)) { var a1 = new ArrayList(); foreach (var item in t1 as IEnumerable) { a1.Add(item); } var a2 = new ArrayList(); foreach (var item in t2 as IEnumerable) { a2.Add(item); } if (a1.Count != a2.Count) { return new CompareResult() { Name = "枚举大小", Value1 = a1.Count, Value2 = a2.Count } } ; for (int i = 0; i < a1.Count; i++) { var r = Compare(a1[i], a2[i]); if (r != null) { return(r); } } } var mp = TypeMapper.Create(type); foreach (var p in mp.Properties) { try { var v1 = p.GetValue(t1); var v2 = p.GetValue(t2); var r = Compare(p.Property.Name, p.Property.PropertyType, v1, v2); if (r != null) { return(null); } } catch (Exception) { throw; } } break; default: if (!object.Equals(t1, t2)) { return(new CompareResult() { Name = name, Value1 = t1, Value2 = t2 }); } break; } return(null); }