public int Update(AdoAdapter adapter, string tableName, IList<IDictionary<string, object>> data, IDbTransaction transaction, IList<string> keyFields) { int count = 0; if (data == null) throw new ArgumentNullException("data"); if (data.Count < 2) throw new ArgumentException("UpdateMany requires more than one record."); if (keyFields.Count == 0) throw new NotSupportedException("Adapter does not support key-based update for this object."); if (!AllRowsHaveSameKeys(data)) throw new SimpleDataException("Records have different structures. Bulk updates are only valid on consistent records."); var table = adapter.GetSchema().FindTable(tableName); var exampleRow = new Dictionary<string, object>(data.First(), HomogenizedEqualityComparer.DefaultInstance); var commandBuilder = new UpdateHelper(adapter.GetSchema()).GetUpdateCommand(tableName, exampleRow, ExpressionHelper.CriteriaDictionaryToExpression( tableName, GetCriteria(keyFields, exampleRow))); using (var connectionScope = ConnectionScope.Create(transaction, adapter.CreateConnection)) using (var command = commandBuilder.GetRepeatableCommand(connectionScope.Connection)) { var propertyToParameterMap = CreatePropertyToParameterMap(data, table, command); foreach (var row in data) { foreach (var kvp in row) { propertyToParameterMap[kvp.Key].Value = kvp.Value ?? DBNull.Value; } count += command.ExecuteNonQuery(); } } return count; }
private IDictionary<string, object> Upsert(string tableName, IDictionary<string, object> data, SimpleExpression criteria, bool resultRequired, IDbConnection connection) { var finder = _transaction == null ? new AdoAdapterFinder(_adapter, connection) : new AdoAdapterFinder(_adapter, _transaction); var existing = finder.FindOne(tableName, criteria); if (existing != null) { // Don't update columns used as criteria var keys = criteria.GetOperandsOfType<ObjectReference>().Select(o => o.GetName().Homogenize()); var updateData = data.Where(kvp => keys.All(k => k != kvp.Key.Homogenize())).ToDictionary(); if (updateData.Count == 0) { return existing; } var commandBuilder = new UpdateHelper(_adapter.GetSchema()).GetUpdateCommand(tableName, updateData, criteria); if (_transaction == null) { _adapter.Execute(commandBuilder, connection); } else { _adapter.Execute(commandBuilder, _transaction); } return resultRequired ? finder.FindOne(tableName, criteria) : null; } var inserter = _transaction == null ? new AdoAdapterInserter(_adapter, connection) : new AdoAdapterInserter(_adapter, _transaction); return inserter.Insert(tableName, data, resultRequired); }
public int Update(AdoAdapter adapter, string tableName, IList <IDictionary <string, object> > data, IEnumerable <string> criteriaFieldNames, IDbTransaction transaction) { int count = 0; if (data == null) { throw new ArgumentNullException("data"); } if (data.Count < 2) { throw new ArgumentException("UpdateMany requires more than one record."); } var criteriaFieldNameList = criteriaFieldNames.ToList(); if (criteriaFieldNameList.Count == 0) { throw new NotSupportedException("Adapter does not support key-based update for this object."); } if (!AllRowsHaveSameKeys(data)) { throw new SimpleDataException("Records have different structures. Bulk updates are only valid on consistent records."); } var table = adapter.GetSchema().FindTable(tableName); var exampleRow = new Dictionary <string, object>(data.First(), HomogenizedEqualityComparer.DefaultInstance); var commandBuilder = new UpdateHelper(adapter.GetSchema()).GetUpdateCommand(tableName, exampleRow, ExpressionHelper.CriteriaDictionaryToExpression( tableName, GetCriteria(criteriaFieldNameList, exampleRow))); using (var connectionScope = ConnectionScope.Create(transaction, adapter.CreateConnection)) using (var command = commandBuilder.GetRepeatableCommand(connectionScope.Connection)) { var propertyToParameterMap = CreatePropertyToParameterMap(data, table, command); foreach (var row in data) { foreach (var kvp in row) { propertyToParameterMap[kvp.Key].Value = kvp.Value ?? DBNull.Value; } count += command.ExecuteNonQuery(); } } return(count); }
public int Update(AdoAdapter adapter, string tableName, IList<IDictionary<string, object>> data, IEnumerable<string> criteriaFieldNames, IDbTransaction transaction) { int count = 0; if (data == null || !data.Any()) return count; var criteriaFieldNameList = criteriaFieldNames.ToList(); if (criteriaFieldNameList.Count == 0) throw new NotSupportedException("Adapter does not support key-based update for this object."); if (!AllRowsHaveSameKeys(data)) throw new SimpleDataException("Records have different structures. Bulk updates are only valid on consistent records."); var table = adapter.GetSchema().FindTable(tableName); var exampleRow = new Dictionary<string, object>(data.First(), HomogenizedEqualityComparer.DefaultInstance); var commandBuilder = new UpdateHelper(adapter.GetSchema()).GetUpdateCommand(tableName, exampleRow, ExpressionHelper.CriteriaDictionaryToExpression( tableName, GetCriteria(criteriaFieldNameList, exampleRow))); var connection = adapter.CreateConnection(); using (connection.MaybeDisposable()) using (var command = commandBuilder.GetRepeatableCommand(connection)) { if (transaction != null) { command.Transaction = transaction; } connection.OpenIfClosed(); var propertyToParameterMap = CreatePropertyToParameterMap(data, table, command); foreach (var row in data) { foreach (var kvp in row) { if (propertyToParameterMap.ContainsKey(kvp.Key)) { propertyToParameterMap[kvp.Key].Value = kvp.Value ?? DBNull.Value; } } count += command.ExecuteNonQuery(); } } return count; }
private IDictionary <string, object> Upsert(string tableName, IDictionary <string, object> data, SimpleExpression criteria, bool resultRequired, IDbConnection connection) { var finder = _transaction == null ? new AdoAdapterFinder(_adapter, connection) : new AdoAdapterFinder(_adapter, _transaction); var existing = finder.FindOne(tableName, criteria); if (existing != null) { // Don't update columns used as criteria var keys = criteria.GetOperandsOfType <ObjectReference>().Select(o => o.GetName().Homogenize()); var updateData = data.Where(kvp => keys.All(k => k != kvp.Key.Homogenize())).ToDictionary(); if (updateData.Count == 0) { return(existing); } var commandBuilder = new UpdateHelper(_adapter.GetSchema()).GetUpdateCommand(tableName, updateData, criteria); if (_transaction == null) { _adapter.Execute(commandBuilder, connection); } else { _adapter.Execute(commandBuilder, _transaction); } return(resultRequired ? finder.FindOne(tableName, criteria) : null); } var inserter = _transaction == null ? new AdoAdapterInserter(_adapter, connection) : new AdoAdapterInserter(_adapter, _transaction); return(inserter.Insert(tableName, data, resultRequired)); }
public int Update(string tableName, IDictionary<string, object> data, SimpleExpression criteria, IAdapterTransaction transaction) { ICommandBuilder commandBuilder = new UpdateHelper(_schema).GetUpdateCommand(tableName, data, criteria); return Execute(commandBuilder, transaction); }
public override int Update(string tableName, IDictionary<string, object> data, SimpleExpression criteria) { ICommandBuilder commandBuilder = new UpdateHelper(_schema).GetUpdateCommand(tableName, data, criteria); return Execute(commandBuilder); }
public override int Update(string tableName, IDictionary <string, object> data, SimpleExpression criteria) { ICommandBuilder commandBuilder = new UpdateHelper(_schema).GetUpdateCommand(tableName, data, criteria); return(Execute(commandBuilder)); }
public int Update(string tableName, IDictionary <string, object> data, SimpleExpression criteria, IAdapterTransaction transaction) { var commandBuilder = new UpdateHelper(_schema).GetUpdateCommand(tableName, data, criteria); return(Execute(commandBuilder, transaction)); }