using Umbraco.Core.Persistence.Migrations.Model;
namespace Umbraco.Core.Persistence.SqlSyntax
{
///
/// Static class that provides simple access to the Sql Server SqlSyntax Providers singleton
///
internal static class SqlServerSyntax
{
public static ISqlSyntaxProvider Provider { get { return SqlServerSyntaxProvider.Instance; } }
}
///
/// Represents an SqlSyntaxProvider for Sql Server
///
internal class SqlServerSyntaxProvider : SqlSyntaxProviderBase
{
public static SqlServerSyntaxProvider Instance = new SqlServerSyntaxProvider();
private SqlServerSyntaxProvider()
{
StringLengthColumnDefinitionFormat = StringLengthUnicodeColumnDefinitionFormat;
StringColumnDefinition = string.Format(StringLengthColumnDefinitionFormat, DefaultStringLength);
AutoIncrementDefinition = "IDENTITY(1,1)";
StringColumnDefinition = "VARCHAR(8000)";
GuidColumnDefinition = "UniqueIdentifier";
RealColumnDefinition = "FLOAT";
BoolColumnDefinition = "BIT";
DecimalColumnDefinition = "DECIMAL(38,6)";
TimeColumnDefinition = "TIME"; //SQLSERVER 2008+
BlobColumnDefinition = "VARBINARY(MAX)";
InitColumnTypeMap();
}
public override bool DoesTableExist(Database db, string tableName)
{
var result =
db.ExecuteScalar("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @TableName",
new { TableName = tableName });
return result > 0;
}
protected override string FormatIdentity(Migrations.Model.ColumnDefinition column)
{
return column.IsIdentity ? GetIdentityString(column) : string.Empty;
}
private static string GetIdentityString(Migrations.Model.ColumnDefinition column)
{
return "IDENTITY(1,1)";
}
protected override string FormatSystemMethods(SystemMethods systemMethod)
{
switch (systemMethod)
{
case SystemMethods.NewGuid:
return "NEWID()";
case SystemMethods.NewSequentialId:
return "NEWSEQUENTIALID()";
case SystemMethods.CurrentDateTime:
return "GETDATE()";
case SystemMethods.CurrentUTCDateTime:
return "GETUTCDATE()";
}
return null;
}
public override string AddColumn { get { return "ALTER TABLE {0} ADD {1}"; } }
}
}