public static List <SqlBatch> FormBatches(IEnumerable <string> sqlScripts) { var batches = new List <SqlBatch>(); SqlBatch currentBatch = null; foreach (string sqlScript in sqlScripts) { bool scriptUsesTransaction = !sqlScript.StartsWith(SqlUtility.NoTransactionTag); if (currentBatch == null || currentBatch.UseTransacion != scriptUsesTransaction) { currentBatch = new SqlBatch { UseTransacion = scriptUsesTransaction }; batches.Add(currentBatch); } if (!string.IsNullOrWhiteSpace(sqlScript.Replace(SqlUtility.NoTransactionTag, ""))) { currentBatch.Add(sqlScript); } } return(batches.Where(batch => batch.Count > 0).ToList()); }
protected void ApplyChangesToDatabase( List <ConceptApplication> oldApplications, List <NewConceptApplication> newApplications, List <ConceptApplication> toBeRemoved, List <NewConceptApplication> toBeInserted) { var stopwatch = Stopwatch.StartNew(); int estimatedNumberOfQueries = (toBeRemoved.Count() + toBeInserted.Count()) * 3; var sqlScripts = new List <string>(estimatedNumberOfQueries); sqlScripts.AddRange(ApplyChangesToDatabase_Remove(toBeRemoved, oldApplications)); _performanceLogger.Write(stopwatch, "DatabaseGenerator.ApplyChangesToDatabase: Prepared SQL scripts for removing concept applications."); sqlScripts.AddRange(ApplyChangesToDatabase_Unchanged(toBeInserted, newApplications, oldApplications)); _performanceLogger.Write(stopwatch, "DatabaseGenerator.ApplyChangesToDatabase: Prepared SQL scripts for updating unchanged concept applications' metadata."); sqlScripts.AddRange(ApplyChangesToDatabase_Insert(toBeInserted, newApplications)); _performanceLogger.Write(stopwatch, "DatabaseGenerator.ApplyChangesToDatabase: Prepared SQL scripts for inserting concept applications."); foreach (var batchOfScripts in SqlBatch.FormBatches(sqlScripts.Where(sql => !string.IsNullOrWhiteSpace(sql)))) { _sqlExecuter.ExecuteSql(batchOfScripts, batchOfScripts.UseTransacion); } _performanceLogger.Write(stopwatch, "DatabaseGenerator.ApplyChangesToDatabase: Executed " + sqlScripts.Count + " SQL scripts."); }