using System;
using System.Collections.Generic;
using System.Text;
using ScrewTurn.Wiki.Plugins.SqlCommon;
using ScrewTurn.Wiki.PluginFramework;
using System.Data.SqlClient;
namespace ScrewTurn.Wiki.Plugins.SqlServer {
///
/// Implements a SQL Server-based settings storage provider.
///
public class SqlServerSettingsStorageProvider : SqlSettingsStorageProviderBase {
private readonly ComponentInformation info = new ComponentInformation("SQL Server Settings Storage Provider", "ScrewTurn Software", "3.0.0.341", "http://www.screwturn.eu", "http://www.screwturn.eu/Version/SQLServerProv/Settings.txt");
private readonly SqlServerCommandBuilder commandBuilder = new SqlServerCommandBuilder();
private const int CurrentSchemaVersion = 3000;
///
/// Gets a new command with an open connection.
///
/// The connection string.
/// The command.
private SqlCommand GetCommand(string connString) {
return commandBuilder.GetCommand(connString, "select current_user", new List()) as SqlCommand;
}
///
/// Gets a new command builder object.
///
/// The command builder.
protected override ICommandBuilder GetCommandBuilder() {
return commandBuilder;
}
///
/// Validates a connection string.
///
/// The connection string to validate.
/// If the connection string is invalid, the method throws .
protected override void ValidateConnectionString(string connString) {
SqlCommand cmd = null;
try {
cmd = GetCommand(connString);
}
catch(SqlException ex) {
throw new InvalidConfigurationException("Provided connection string is not valid", ex);
}
catch(InvalidOperationException ex) {
throw new InvalidConfigurationException("Provided connection string is not valid", ex);
}
catch(ArgumentException ex) {
throw new InvalidConfigurationException("Provided connection string is not valid", ex);
}
finally {
try {
cmd.Connection.Close();
}
catch { }
}
}
///
/// Detects whether the database schema exists.
///
/// true if the schema exists, false otherwise.
private bool SchemaExists() {
SqlCommand cmd = GetCommand(connString);
cmd.CommandText = "select [Version] from [Version] where [Component] = 'Settings'";
bool exists = false;
try {
int version = ExecuteScalar(cmd, -1);
if(version > CurrentSchemaVersion) throw new InvalidConfigurationException("The version of the database schema is greater than the supported version");
exists = version != -1;
}
catch(SqlException) {
exists = false;
}
finally {
try {
cmd.Connection.Close();
}
catch { }
}
return exists;
}
///
/// Detects whether the database schema needs to be updated.
///
/// true if an update is needed, false otherwise.
private bool SchemaNeedsUpdate() {
SqlCommand cmd = GetCommand(connString);
cmd.CommandText = "select [Version] from [Version] where [Component] = 'Settings'";
bool exists = false;
try {
int version = ExecuteScalar(cmd, -1);
exists = version < CurrentSchemaVersion;
}
catch(SqlException) {
exists = false;
}
finally {
try {
cmd.Connection.Close();
}
catch { }
}
return exists;
}
///
/// Creates the standard database schema.
///
private void CreateStandardSchema() {
SqlCommand cmd = GetCommand(connString);
cmd.CommandText = Properties.Resources.SettingsDatabase;
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
///
/// Creates or updates the database schema if necessary.
///
protected override void CreateOrUpdateDatabaseIfNecessary() {
if(!SchemaExists()) {
CreateStandardSchema();
}
if(SchemaNeedsUpdate()) {
// Run minor update batches...
}
}
///
/// Tries to load the configuration from a corresponding v2 provider.
///
/// The configuration, or an empty string.
protected override string TryLoadV2Configuration() {
return "";
}
///
/// Tries to load the configuration of the corresponding settings storage provider.
///
/// The configuration, or an empty string.
protected override string TryLoadSettingsStorageProviderConfiguration() {
return "";
}
///
/// Gets the Information about the Provider.
///
public override ComponentInformation Information {
get { return info; }
}
///
/// Gets a brief summary of the configuration string format, in HTML. Returns null if no configuration is needed.
///
public override string ConfigHelpHtml {
get { return "Connection string format:
Data Source=Database Address and Instance;Initial Catalog=Database name;User ID=login;Password=password;
"; }
}
///
/// Gets the default users storage provider, when no value is stored in the database.
///
protected override string DefaultUsersStorageProvider {
get { return typeof(SqlServerUsersStorageProvider).FullName; }
}
///
/// Gets the default pages storage provider, when no value is stored in the database.
///
protected override string DefaultPagesStorageProvider {
get { return typeof(SqlServerPagesStorageProvider).FullName; }
}
///
/// Gets the default files storage provider, when no value is stored in the database.
///
protected override string DefaultFilesStorageProvider {
get { return typeof(SqlServerFilesStorageProvider).FullName; }
}
}
}