Exemplo n.º 1
0
        protected virtual string PreprocessScriptContents(SqlScript script, IDictionary <string, string> variables)
        {
            if (variables == null)
            {
                variables = new Dictionary <string, string>();
            }
            if (Schema != null && !variables.ContainsKey("schema"))
            {
                variables.Add("schema", QuoteSqlObjectName(Schema));
            }

            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));

            return(contents);
        }
Exemplo n.º 2
0
 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);
 }
Exemplo n.º 3
0
        /// <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;
            }
        }