Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SqlTableJournal"/> class.
 /// </summary>
 /// <param name="connectionManager">The connection manager.</param>
 /// <param name="logger">The log.</param>
 /// <param name="schema">The schema that contains the table.</param>
 /// <param name="table">The table name.</param>
 /// <example>
 /// var journal = new TableJournal("Server=server;Database=database;Trusted_Connection=True", "dbo", "MyVersionTable");
 /// </example>
 public SqlTableJournal(Func <IConnectionManager> connectionManager, Func <IUpgradeLog> logger, string schema, string table)
 {
     schemaTableName = string.IsNullOrEmpty(schema)
         ? SqlObjectParser.QuoteSqlObjectName(table)
         : SqlObjectParser.QuoteSqlObjectName(schema) + "." + SqlObjectParser.QuoteSqlObjectName(table);
     this.connectionManager = connectionManager;
     log = logger;
 }
Exemple #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SqlTableJournal"/> class.
 /// </summary>
 /// <param name="connectionFactory">The connection factory.</param>
 /// <param name="schema">The schema that contains the table.</param>
 /// <param name="table">The table name.</param>
 /// <param name="logger">The log.</param>
 /// <example>
 /// var journal = new TableJournal("Server=server;Database=database;Trusted_Connection=True", "dbo", "MyVersionTable");
 /// </example>
 public SqlTableJournal(Func <IDbConnection> connectionFactory, string schema, string table, IUpgradeLog logger)
 {
     this.connectionFactory = connectionFactory;
     schemaTableName        = tableName = SqlObjectParser.QuoteSqlObjectName(table);
     if (!string.IsNullOrEmpty(schema))
     {
         schemaTableName = SqlObjectParser.QuoteSqlObjectName(schema) + "." + tableName;
     }
     log = logger;
 }
Exemple #3
0
 /// <summary>Convert the <c>table</c> value into an appropriately-quoted identifier for the journal table's unique primary key.</summary>
 /// <param name="table">Desired table name</param>
 /// <returns>Quoted journal table primary key identifier</returns>
 protected virtual string CreatePrimaryKeyName(string table)
 {
     return(SqlObjectParser.QuoteSqlObjectName("PK_" + table + "_Id"));
 }
Exemple #4
0
 /// <summary>Combine the <c>schema</c> and <c>table</c> values into an appropriately-quoted identifier for the journal table.</summary>
 /// <param name="schema">Desired schema name supplied by configuration or <c>NULL</c></param>
 /// <param name="table">Desired table name</param>
 /// <returns>Quoted journal table identifier</returns>
 protected virtual string CreateTableName(string schema, string table)
 {
     return(string.IsNullOrEmpty(schema)
         ? SqlObjectParser.QuoteSqlObjectName(table)
         : SqlObjectParser.QuoteSqlObjectName(schema) + "." + SqlObjectParser.QuoteSqlObjectName(table));
 }
Exemple #5
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;
            }
        }