public async Task <bool> UpdateAsync <T>(IDbConnection connection, T entity, IPredicate predicate, IDbTransaction transaction, int?commandTimeout) where T : class { IClassMapper classMap = SqlGenerator.Configuration.GetMap(entity.GetType()); //IClassMapper classMap = SqlGenerator.Configuration.GetMap<T>(); predicate = predicate ?? GetKeyPredicate(classMap, entity); Dictionary <string, object> parameters = new Dictionary <string, object>(); string sql = SqlGenerator.Update(classMap, predicate, parameters); DynamicParameters dynamicParameters = new DynamicParameters(); var columns = classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly || p.KeyType == KeyType.Identity || p.KeyType == KeyType.Assigned)); foreach (var property in ReflectionHelper.GetObjectValues(entity).Where(property => columns.Any(c => c.Name == property.Key))) { dynamicParameters.Add(property.Key, property.Value); } foreach (var parameter in parameters) { dynamicParameters.Add(parameter.Key, parameter.Value); } return(await connection.ExecuteAsync(sql, dynamicParameters, transaction, commandTimeout, CommandType.Text) > 0); }
public T Get <T>(IDbConnection connection, dynamic id, IDbTransaction transaction, int?commandTimeout) where T : class { IClassMapper classMap = GetMap <T>(); string sql = _sqlGenerator.Get(classMap); bool isSimpleType = IsSimpleType(id.GetType()); IDictionary <string, object> paramValues = null; if (!isSimpleType) { paramValues = ReflectionHelper.GetObjectValues(id); } DynamicParameters parameters = new DynamicParameters(); var keys = classMap.Properties.Where(p => p.KeyType != KeyType.NotAKey); foreach (var key in keys) { object value = id; if (!isSimpleType) { value = paramValues[key.Name]; } parameters.Add("@" + key.Name, value); } T result = connection.Query <T>(sql, parameters, transaction, true, commandTimeout, CommandType.Text).SingleOrDefault(); return(result); }
protected IPredicate GetEntityPredicate(IClassMapper classMap, object entity) { var predicateType = typeof(FieldPredicate <>).MakeGenericType(classMap.EntityType); IList <IPredicate> predicates = new List <IPredicate>(); foreach (var kvp in ReflectionHelper.GetObjectValues(entity)) { if (Activator.CreateInstance(predicateType) is IFieldPredicate fieldPredicate) { fieldPredicate.Not = false; fieldPredicate.Operator = Operator.Eq; fieldPredicate.PropertyName = kvp.Key; fieldPredicate.Value = kvp.Value; predicates.Add(fieldPredicate); } } return(predicates.Count == 1 ? predicates[0] : new PredicateGroup { Operator = GroupOperator.And, Predicates = predicates }); }
protected IPredicate GetIdPredicate(IClassMapper classMap, object id) { var isSimpleType = ReflectionHelper.IsSimpleType(id.GetType()); var keys = classMap.Properties.Where(p => p.KeyType != KeyType.NotAKey); IDictionary <string, object> paramValues = null; IList <IPredicate> predicates = new List <IPredicate>(); if (!isSimpleType) { paramValues = ReflectionHelper.GetObjectValues(id); } foreach (var key in keys) { object value = id; if (!isSimpleType) { value = paramValues[key.Name]; } Type predicateType = typeof(FieldPredicate <>).MakeGenericType(classMap.EntityType); var fieldPredicate = Activator.CreateInstance(predicateType) as IFieldPredicate; fieldPredicate.Not = false; fieldPredicate.Operator = Operator.Eq; fieldPredicate.PropertyName = key.Name; fieldPredicate.Value = value; predicates.Add(fieldPredicate); } return(predicates.Count == 1 ? predicates[0] : new PredicateGroup { Operator = GroupOperator.And, Predicates = predicates }); }
public bool Update <T>(IDbConnection connection, T entity, object predicate, IDbTransaction transaction, int?commandTimeout, string tableName, string schemaName = null, bool ignoreAllKeyProperties = false) where T : class { IClassMapper classMap = SqlGenerator.Configuration.GetMap <T>(); IPredicate wherePredicate = predicate == null?GetKeyPredicate <T>(classMap, entity) : GetPredicate(classMap, predicate); Dictionary <string, object> parameters = new Dictionary <string, object>(); string sql = SqlGenerator.Update(classMap, wherePredicate, parameters, schemaName, tableName); DynamicParameters dynamicParameters = new DynamicParameters(); var columns = ignoreAllKeyProperties ? classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly) && p.KeyType == KeyType.NotAKey) : classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly || p.KeyType == KeyType.Identity || p.KeyType == KeyType.Assigned)); foreach (var property in ReflectionHelper.GetObjectValues(entity).Where(property => columns.Any(c => c.Name == property.Key))) { dynamicParameters.Add(property.Key, property.Value); } foreach (var parameter in parameters) { dynamicParameters.Add(parameter.Key, parameter.Value); } return(connection.Execute(sql, dynamicParameters, transaction, commandTimeout, CommandType.Text) > 0); }
public bool Update <T>(IDbConnection connection, T entity, IDbTransaction transaction, int?commandTimeout, bool ignoreAllKeyProperties = false) where T : class { IClassMapper classMap = SqlGenerator.Configuration.GetMap <T>(); IPredicate predicate = GetKeyPredicate <T>(classMap, entity); Dictionary <string, object> parameters = new Dictionary <string, object>(); string sql = SqlGenerator.Update(classMap, predicate, parameters, ignoreAllKeyProperties); var columns = ignoreAllKeyProperties ? classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly) && p.KeyType == KeyType.NotAKey) : classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly || p.KeyType == KeyType.Identity || p.KeyType == KeyType.TriggerIdentity || p.KeyType == KeyType.Assigned)); DynamicParameters dynamicParameters = GetDynamicParameters(parameters); foreach (var property in ReflectionHelper.GetObjectValues(entity).Where(property => columns.Any(c => c.Name == property.Key))) { var parameter = ReflectionHelper.GetParameter(typeof(T), SqlGenerator, property.Key, property.Value); dynamicParameters.Add(parameter.Name, parameter.Value, parameter.DbType, parameter.ParameterDirection, parameter.Size, parameter.Precision, parameter.Scale); } LastExecutedCommand = sql; return(connection.Execute(sql, dynamicParameters, transaction, commandTimeout, CommandType.Text) > 0); }
public bool Update <T>(IDbConnection connection, T entity, IDbTransaction transaction, int?commandTimeout, bool ignoreAllKeyProperties = false) where T : class { IClassMapper classMap = SqlGenerator.Configuration.GetMap <T>(); IPredicate predicate = GetKeyPredicate <T>(classMap, entity); Dictionary <string, object> parameters = new Dictionary <string, object>(); string sql = SqlGenerator.Update(classMap, predicate, parameters, ignoreAllKeyProperties); DynamicParameters dynamicParameters = new DynamicParameters(); var columns = ignoreAllKeyProperties ? classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly) && p.KeyType == KeyType.NotAKey) : classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly || p.KeyType == KeyType.Identity || p.KeyType == KeyType.Assigned)); foreach (var property in ReflectionHelper.GetObjectValues(entity).Where(property => columns.Any(c => c.Name == property.Key))) { if (property.Value is null && entity?.GetType().GetProperties().FirstOrDefault(x => x.Name == property.Key)?.PropertyType == typeof(byte[])) { dynamicParameters.Add(property.Key, property.Value, DbType.Binary); }
protected static IPredicate GetEntityPredicate(IClassMapper classMap, object entity) { var predicateType = typeof(FieldPredicate <>).MakeGenericType(classMap.EntityType); IList <IPredicate> predicates = new List <IPredicate>(); var notIgnoredColumns = classMap.Properties.Where(p => !p.Ignored); foreach (var kvp in ReflectionHelper.GetObjectValues(entity).Where(property => notIgnoredColumns.Any(c => c.Name == property.Key))) { var fieldPredicate = Activator.CreateInstance(predicateType) as IFieldPredicate; fieldPredicate.Not = false; fieldPredicate.Operator = Operator.Eq; fieldPredicate.PropertyName = kvp.Key; fieldPredicate.Value = kvp.Value is Func <object>?kvp.Value() : kvp.Value; predicates.Add(fieldPredicate); } return(ReturnPredicate(predicates)); }
protected IPredicate GetEntityPredicate(IClassMapper classMap, object entity) { Type predicateType = typeof(FieldPredicate <>).MakeGenericType(classMap.EntityType); IList <IPredicate> predicates = new List <IPredicate>(); var notIgnoredColumns = classMap.Properties.Where(p => !p.Ignored); foreach (var kvp in ReflectionHelper.GetObjectValues(entity).Where(property => notIgnoredColumns.Any(c => c.Name == property.Key))) { IFieldPredicate fieldPredicate = Activator.CreateInstance(predicateType) as IFieldPredicate; fieldPredicate.Not = false; fieldPredicate.Operator = Operator.Eq; fieldPredicate.PropertyName = kvp.Key; fieldPredicate.Value = kvp.Value(); predicates.Add(fieldPredicate); } return(predicates.Count == 1 ? predicates[0] : new PredicateGroup { Operator = GroupOperator.And, Predicates = predicates }); }
/// <summary> /// The asynchronous counterpart to <see cref="IDapperImplementor.Update{T}(IDbConnection, T, IDbTransaction, int?)"/>. /// </summary> public async Task <bool> UpdateAsync <T>(IDbConnection connection, T entity, IDbTransaction transaction, List <string> colToUpdate, int?commandTimeout, bool ignoreAllKeyProperties) where T : class { var classMap = SqlGenerator.Configuration.GetMap <T>(); var predicate = GetKeyPredicate <T>(classMap, entity); var parameters = new Dictionary <string, object>(); var sql = SqlGenerator.Update(classMap, predicate, parameters, ignoreAllKeyProperties, colToUpdate); var dynamicParameters = new DynamicParameters(); var columns = ignoreAllKeyProperties ? classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly) && p.KeyType == KeyType.NotAKey) : classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly || p.KeyType == KeyType.Identity || p.KeyType == KeyType.Assigned)); foreach (var property in ReflectionHelper.GetObjectValues(entity).Where(property => columns.Any(c => c.Name == property.Key))) { dynamicParameters.Add(property.Key, property.Value); } foreach (var parameter in parameters) { dynamicParameters.Add(parameter.Key, parameter.Value); } return(await connection.ExecuteAsync(sql, dynamicParameters, transaction, commandTimeout, CommandType.Text).ConfigureAwait(false) > 0); }
public async Task <int> UpdateAsync <T>(IDbConnection connection, object entity, object predicate, IDbTransaction transaction, int?commandTimeout, bool ignoreAllKeyProperties = false) where T : class { var updateFileds = ReflectionHelper.GetObjectValues(entity); return(await UpdateAsync <T>(connection, updateFileds, predicate, transaction, commandTimeout, ignoreAllKeyProperties)); }
public async Task <bool> UpdateAsync <T>(IDbConnection connection, T entity, IDbTransaction transaction, int?commandTimeout, string keyName, Snapshotter.Snapshot snapshot, IEnumerable <string> properties) where T : class { IClassMapper classMap = SqlGenerator.Configuration.GetMap <T>(); keyName = keyName ?? classMap.DefaultUpdateKeyName; DynamicParameters diff = null; if (snapshot != null && !classMap.IsPartialUpdateDisabled) { diff = snapshot.Diff(); var upl = diff.ParameterNames.ToList <string>(); if (upl.Count == 0) { return(true); } properties = upl; } bool updated = classMap.OnUpdate(entity); if (updated && snapshot != null && !classMap.IsPartialUpdateDisabled) { diff = snapshot.Diff(); var upl = diff.ParameterNames.ToList <string>(); if (upl.Count == 0) { return(true); } properties = upl; } IPredicate predicate = GetKeyPredicate <T>(classMap, entity, keyName); Dictionary <string, object> parameters = new Dictionary <string, object>(); string sql = SqlGenerator.Update(classMap, predicate, parameters, keyName, properties); DynamicParameters dynamicParameters = new DynamicParameters(); var columns = classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly || p.IsInsertOnly || p.IsAnyKeyType(KeyType.Identity)) && (properties == null || properties.Contains(p.Name))); if (diff != null) { foreach (var property in diff.ParameterNames.Where(property => columns.Any(c => c.Name == property))) { var val = ((SqlMapper.IParameterLookup)diff)[property]; dynamicParameters.Add(property, val); } } else { foreach (var property in ReflectionHelper.GetObjectValues(entity).Where(property => columns.Any(c => c.Name == property.Key))) { dynamicParameters.Add(property.Key, property.Value); } } foreach (var parameter in parameters) { dynamicParameters.Add(parameter.Key, parameter.Value); } return(await connection.ExecuteAsync(sql, dynamicParameters, transaction, commandTimeout, CommandType.Text) > 0); }