protected virtual string RunPreProcessors(SqlScript script, IDictionary<string, string> variables, IEnumerable<IScriptPreprocessor> scriptPreprocessors) { string contents = script.Contents; //do variable replacement if necessary contents = new VariableSubstitutionPreprocessor(variables).Process(contents); //run all the script pre processors contents = scriptPreprocessors.Aggregate(contents, (current, additionalScriptPreprocessor) => additionalScriptPreprocessor.Process(current)); return contents; }
/// <summary> /// Executes the specified script against a database at a given connection string. /// </summary> /// <param name="script">The script.</param> /// <param name="variables">Variables to replace in the script</param> public void Execute(SqlScript script, IDictionary<string, string> variables) { if (variables == null) variables = new Dictionary<string, string>(); if (Schema != null && !variables.ContainsKey("schema")) variables.Add("schema", SqlObjectParser.QuoteSqlObjectName(Schema)); log().WriteInformation("Executing SQL Server script '{0}'", script.Name); var contents = script.Contents; if (string.IsNullOrEmpty(Schema)) contents = new StripSchemaPreprocessor().Process(contents); if (variablesEnabled()) contents = new VariableSubstitutionPreprocessor(variables).Process(contents); contents = (scriptPreprocessors??new IScriptPreprocessor[0]) .Aggregate(contents, (current, additionalScriptPreprocessor) => additionalScriptPreprocessor.Process(current)); var scriptStatements = connectionManager().SplitScriptIntoCommands(contents); var index = -1; try { connectionManager().ExecuteCommandsWithManagedConnection(dbCommandFactory => { foreach (var statement in scriptStatements) { index++; using (var command = dbCommandFactory()) { command.CommandText = statement; if (ExecutionTimeoutSeconds != null) command.CommandTimeout = ExecutionTimeoutSeconds.Value; command.ExecuteNonQuery(); } } }); } catch (SqlException sqlException) { log().WriteInformation("SQL exception has occured in script: '{0}'", script.Name); log().WriteError("Script block number: {0}; Block line {1}; Message: {2}", index, sqlException.LineNumber, sqlException.Procedure, sqlException.Number, sqlException.Message); log().WriteError(sqlException.ToString()); throw; } catch (DbException sqlException) { log().WriteInformation("DB exception has occured in script: '{0}'", script.Name); log().WriteError("Script block number: {0}; Error code {1}; Message: {2}", index, sqlException.ErrorCode, sqlException.Message); log().WriteError(sqlException.ToString()); throw; } catch (Exception ex) { log().WriteInformation("Exception has occured in script: '{0}'", script.Name); log().WriteError(ex.ToString()); throw; } }
private string Preprocess(string query) { var variables = new Dictionary<string, string> {{"schema", schema}}; query = new VariableSubstitutionPreprocessor(variables).Process(query); query = additionalScriptPreprocessors.Aggregate(query, (current, additionalScriptPreprocessor) => additionalScriptPreprocessor.Process(current)); return query; }
private string Preprocess(string query) { if (string.IsNullOrEmpty(Schema)) query = new StripSchemaPreprocessor().Process(query); if (!string.IsNullOrEmpty(Schema) && !variables.ContainsKey("schema")) variables.Add("schema", SqlObjectParser.QuoteSqlObjectName(Schema)); if (variablesEnabled()) query = new VariableSubstitutionPreprocessor(variables).Process(query); query = additionalScriptPreprocessors.Aggregate(query, (current, additionalScriptPreprocessor) => additionalScriptPreprocessor.Process(current)); return query; }
/// <summary> /// Executes the specified script against a database at a given connection string. /// </summary> /// <param name="script">The script.</param> /// <param name="variables">Variables to replace in the script</param> public void Execute(SqlScript script, IDictionary<string, string> variables) { if (variables == null) variables = new Dictionary<string, string>(); if (!variables.ContainsKey("schema")) variables.Add("schema", schema); log().WriteInformation("Executing SQL Server script '{0}'", script.Name); var contents = new VariableSubstitutionPreprocessor(variables).Process(script.Contents); contents = scriptPreprocessors.Aggregate(contents, (current, additionalScriptPreprocessor) => additionalScriptPreprocessor.Process(current)); var scriptStatements = SplitByGoStatements(contents); var index = -1; try { using (var connection = connectionFactory()) { connection.Open(); foreach (var statement in scriptStatements) { index++; var command = connection.CreateCommand(); command.CommandText = statement; if (ExecutionTimeoutSeconds != null) command.CommandTimeout = ExecutionTimeoutSeconds.Value; command.ExecuteNonQuery(); } } } catch (SqlException sqlException) { log().WriteInformation("SQL exception has occured in script: '{0}'", script.Name); log().WriteError("Script block number: {0}; Block line {1}; Message: {2}", index, sqlException.LineNumber, sqlException.Procedure, sqlException.Number, sqlException.Message); log().WriteError(sqlException.ToString()); throw; } catch (DbException sqlException) { log().WriteInformation("DB exception has occured in script: '{0}'", script.Name); log().WriteError("Script block number: {0}; Error code {1}; Message: {2}", index, sqlException.ErrorCode, sqlException.Message); log().WriteError(sqlException.ToString()); throw; } catch (Exception ex) { log().WriteInformation("Exception has occured in script: '{0}'", script.Name); log().WriteError(ex.ToString()); throw; } }