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
idstringproviderUpdateProvidersqlstring
SqlUpdate(string, UpdateProvider, string, string)
public SqlUpdate(string id, UpdateProvider provider, string sql, string sqlConditional)
Parameters
idstringproviderUpdateProvidersqlstringsqlConditionalstring
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
idstringThe unique identifier for the update.
providerUpdateProviderThe update provider that owns this update.
tableNamestringThe name of the table to modify.
columnNamestringThe name of the column to add.
columnDefinitionstringThe SQL definition of the column.
Returns
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
idstringThe unique identifier for the update.
providerUpdateProviderThe update provider that owns this update.
tableNamestringThe name of the table to add the index to.
indexNamestringThe name of the index to add.
indexDefinitionstringThe SQL definition of the index.
Returns
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
idstringThe unique identifier for the update.
providerUpdateProviderThe update provider that owns this update.
tableNamestringThe name of the table to add.
tableDefinitionstringThe SQL definition of the table.
Returns
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
idstringThe unique identifier for the update.
providerUpdateProviderThe update provider that owns this update.
tableNamestringThe name of the table to modify.
columnNamestringThe name of the column to change.
columnDefinitionstringThe new SQL definition for the column.
Returns
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
idstringThe unique identifier for the update.
providerUpdateProviderThe update provider that owns this update.
tableNamestringThe name of the table to update.
columnNamestringThe name of the column to update.
oldValueintThe integer value to search for in the column.
newValueintThe integer value to replace
oldValuewith.
Returns
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
idstringThe unique identifier for the update.
providerUpdateProviderThe update provider that owns this update.
tableNamestringThe name of the table to update.
columnNamestringThe name of the column to update.
oldValuestringThe value to search for in the column.
newValuestringThe value to replace
oldValuewith.
Returns
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
idstringThe unique identifier for the update.
providerUpdateProviderThe update provider that owns this update.
tableNamestringThe name of the table to modify.
columnNamestringThe name of the column to remove.
Returns
Execute(UpdateContext)
public override void Execute(UpdateContext context)
Parameters
contextUpdateContext
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
providerUpdateProviderThe update provider that owns this update.
sqlConditionalstringA SQL statement used to determine if the update should be executed. If the statement returns a value greater than 0, the update is skipped.