private List <IDbCommand> GetInsertCommands(Object obj, ColumnCollectionInfo dataInfo, CollectionInfo collectionInfo, IDbConnection connection) { List <IDbCommand> result = new List <IDbCommand>(); IEnumerable targets = collectionInfo.GetValue(obj); if (targets != null) { foreach (var target in targets) { IDbCommand command = connection.CreateCommand(); command.CommandText = GetInsertStatement(obj, target, dataInfo, collectionInfo, command); result.Add(command); } } return(result); }
private List <IDbCommand> GetUpdateCommands(Object obj, ColumnCollectionInfo dataInfo, CollectionInfo collectionInfo, IDbConnection conn) { List <IDbCommand> commands = new List <IDbCommand>(); IEnumerable colllectionValue = collectionInfo.GetValue(obj); if (colllectionValue != null) { var newTargets = colllectionValue.Cast <Object>().ToList(); if (newTargets.Count == 0) { commands.Add(GetClearCollectionCommand(obj, dataInfo, collectionInfo, conn)); } else { var targetDataInfo = ColumnCollectionInfo.GetInfo(collectionInfo.TargetType); ColumnInfo targetPrimaryKeyInfo = targetDataInfo.PrimaryKeys.Values.OfType <ColumnInfo>().Single(); var oldPrimaryKeys = GetCollectionTargets(obj, dataInfo, collectionInfo, targetPrimaryKeyInfo.GetValue(newTargets.First()), conn); foreach (var newTarget in newTargets) { if (!oldPrimaryKeys.Contains(targetPrimaryKeyInfo.GetValue(newTarget))) { IDbCommand command = conn.CreateCommand(); command.CommandText = GetInsertStatement(obj, newTarget, dataInfo, collectionInfo, command); commands.Add(command); } } var newPrimaryKeys = newTargets.Select(t => targetPrimaryKeyInfo.GetValue(t)); foreach (var primaryKey in oldPrimaryKeys) { if (newPrimaryKeys.Where(t => t.Equals(primaryKey)).Count() == 0) { commands.Add(GetClearCollectionCommand(obj, dataInfo, collectionInfo, conn, targetPrimaryKeyInfo, primaryKey)); } } } } return(commands); }