Table of Contents

Class SqlUpdate

Namespace
Dynamicweb.Updates
Assembly
Dynamicweb.Core.dll

Represents an update which executes a given sql statement.

using System.Reflection;

namespace Dynamicweb.Updates.Examples
{
    /// <summary>
    /// Example UpdateProvider demonstrating usage of SqlUpdate, SettingUpdate, MethodUpdate, and FileUpdate.
    /// </summary>
    public class SqlUpdateSampleProvider : UpdateProvider
    {
        public override IEnumerable<Update> GetUpdates()
        {
            // SQL Update: Add a column to a table if it does not exist
            yield return SqlUpdate.AddColumn(
                id: "d50467a2-0e9b-4c70-a490-7ab4af88851b",
                provider: this,
                tableName: "MyCustomTable",
                columnName: "MyCustomColumn",
                columnDefinition: "NVARCHAR(50) NULL"
            );

            // SettingUpdate: Set a configuration value, only if it does not already exist
            yield return new SettingUpdate(
                id: "99fd48e3-f364-4108-bb19-6355e8b31896",
                provider: this,
                name: "/Globalsettings/System/Url/HighVolumeProductUrls/Enable",
                value: "True"
            );

            // SettingUpdate: Overwrite a configuration value even if it exists
            yield return new SettingUpdate(
                id: "89c68710-c2c1-4e02-8ea5-f712adeea24f",
                provider: this,
                name: "/Globalsettings/System/Url/HighVolumeProductUrls/Enable",
                value: "True",
                overwrite: true
            );

            // MethodUpdate: Execute custom logic during update
            yield return new MethodUpdate(
                id: "30c561dd-5509-4636-b81a-146be8a51e40",
                provider: this,
                method: context =>
                {
                    // Custom logic here
                    Console.WriteLine("Custom method executed as part of update.");
                }
            );

            // FileUpdate: Write a file to disk, only if it does not already exist
            yield return new FileUpdate(
                id: "efe5952c-bf3c-400d-a654-6fe2eb79ca44",
                provider: this,
                filePath: "/Files/Templates/Feeds/CustomFeedTemplate.cshtml",
                fileStreamProvider: () => GetFileStreamFromEmbeddedResource("CustomFeedTemplate.cshtml"),
                overwrite: false
            );

            // FileUpdate: Overwrite an existing file
            yield return new FileUpdate(
                id: "27c24328-285b-4cae-b62b-23130b6fcde5",
                provider: this,
                filePath: "/Files/Templates/Feeds/CustomFeedTemplate.cshtml",
                fileStreamProvider: () => GetFileStreamFromEmbeddedResource("CustomFeedTemplate.cshtml"),
                overwrite: true
            );
        }

        private Stream GetFileStreamFromEmbeddedResource(string resourceName)
        {
            string resourceFullName = $"CustomAssembly.{resourceName}";
            var assembly = Assembly.GetAssembly(this.GetType());
            var stream = assembly?.GetManifestResourceStream(resourceName);

            if (stream is null)
                throw new FileNotFoundException($"Embedded resource '{resourceFullName}' not found.");

            return stream;
        }
    }
}
public class SqlUpdate : Update
Inheritance
SqlUpdate
Inherited Members

Constructors

SqlUpdate(string, UpdateProvider, string)

public SqlUpdate(string id, UpdateProvider provider, string sql)

Parameters

id string
provider UpdateProvider
sql string

SqlUpdate(string, UpdateProvider, string, string)

public SqlUpdate(string id, UpdateProvider provider, string sql, string sqlConditional)

Parameters

id string
provider UpdateProvider
sql string
sqlConditional string

Methods

AddColumn(string, UpdateProvider, string, string, string)

Creates an update that adds a column to a table if the column does not already exist.

public static Update AddColumn(string id, UpdateProvider provider, string tableName, string columnName, string columnDefinition)

Parameters

id string

The unique identifier for the update.

provider UpdateProvider

The update provider that owns this update.

tableName string

The name of the table to modify.

columnName string

The name of the column to add.

columnDefinition string

The SQL definition of the column.

Returns

Update

An Update that adds the column if it does not exist.

AddIndex(string, UpdateProvider, string, string, string)

Creates an update that adds an index to a table if the index does not already exist.

public static Update AddIndex(string id, UpdateProvider provider, string tableName, string indexName, string indexDefinition)

Parameters

id string

The unique identifier for the update.

provider UpdateProvider

The update provider that owns this update.

tableName string

The name of the table to add the index to.

indexName string

The name of the index to add.

indexDefinition string

The SQL definition of the index.

Returns

Update

An Update that creates the index if it does not exist.

AddTable(string, UpdateProvider, string, string)

Creates an update that adds a table if the table does not already exist.

public static Update AddTable(string id, UpdateProvider provider, string tableName, string tableDefinition)

Parameters

id string

The unique identifier for the update.

provider UpdateProvider

The update provider that owns this update.

tableName string

The name of the table to add.

tableDefinition string

The SQL definition of the table.

Returns

Update

An Update that creates the table if it does not exist.

ChangeColumn(string, UpdateProvider, string, string, string)

Creates an update that changes the definition of a column if the column exists in the table.

public static Update ChangeColumn(string id, UpdateProvider provider, string tableName, string columnName, string columnDefinition)

Parameters

id string

The unique identifier for the update.

provider UpdateProvider

The update provider that owns this update.

tableName string

The name of the table to modify.

columnName string

The name of the column to change.

columnDefinition string

The new SQL definition for the column.

Returns

Update

An Update that alters the column if it exists.

ChangeColumnValue(string, UpdateProvider, string, string, int, int)

Creates an update that changes all occurrences of a column value from oldValue to newValue.

public static Update ChangeColumnValue(string id, UpdateProvider provider, string tableName, string columnName, int oldValue, int newValue)

Parameters

id string

The unique identifier for the update.

provider UpdateProvider

The update provider that owns this update.

tableName string

The name of the table to update.

columnName string

The name of the column to update.

oldValue int

The integer value to search for in the column.

newValue int

The integer value to replace oldValue with.

Returns

Update

An Update that updates the column values.

ChangeColumnValue(string, UpdateProvider, string, string, string, string)

Creates an update that changes all occurrences of a column value from oldValue to newValue.

public static Update ChangeColumnValue(string id, UpdateProvider provider, string tableName, string columnName, string oldValue, string newValue)

Parameters

id string

The unique identifier for the update.

provider UpdateProvider

The update provider that owns this update.

tableName string

The name of the table to update.

columnName string

The name of the column to update.

oldValue string

The value to search for in the column.

newValue string

The value to replace oldValue with.

Returns

Update

An Update that updates the column values.

DropColumn(string, UpdateProvider, string, string)

Creates an update that removes a column from a table if the column exists.

public static Update DropColumn(string id, UpdateProvider provider, string tableName, string columnName)

Parameters

id string

The unique identifier for the update.

provider UpdateProvider

The update provider that owns this update.

tableName string

The name of the table to modify.

columnName string

The name of the column to remove.

Returns

Update

An Update that drops the column if it exists.

Execute(UpdateContext)

public override void Execute(UpdateContext context)

Parameters

context UpdateContext

InitializeDatabase(UpdateProvider, string)

Creates an update that initializes the database by executing a default script for the specified provider, if the given SQL condition does not return a value greater than 0.

public static Update InitializeDatabase(UpdateProvider provider, string sqlConditional)

Parameters

provider UpdateProvider

The update provider that owns this update.

sqlConditional string

A SQL statement used to determine if the update should be executed. If the statement returns a value greater than 0, the update is skipped.

Returns

Update

An Update that performs the database initialization.

To top