2012-12-07 13:48:38 -01:00
|
|
|
|
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
|
2012-11-30 15:01:52 -01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Persistence.SqlSyntax
|
2012-10-19 13:20:57 -02:00
|
|
|
|
{
|
2012-10-23 08:09:01 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Static class that provides simple access to the Sql Server SqlSyntax Providers singleton
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal static class SqlServerSyntax
|
|
|
|
|
|
{
|
|
|
|
|
|
public static ISqlSyntaxProvider Provider { get { return SqlServerSyntaxProvider.Instance; } }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents an SqlSyntaxProvider for Sql Server
|
|
|
|
|
|
/// </summary>
|
2012-10-19 13:20:57 -02:00
|
|
|
|
internal class SqlServerSyntaxProvider : SqlSyntaxProviderBase<SqlServerSyntaxProvider>
|
|
|
|
|
|
{
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-12-19 15:23:05 -01:00
|
|
|
|
public override string GetQuotedTableName(string tableName)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format("[{0}]", tableName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override string GetQuotedColumnName(string columnName)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format("[{0}]", columnName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override string GetQuotedName(string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format("[{0}]", name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-19 13:20:57 -02:00
|
|
|
|
public override bool DoesTableExist(Database db, string tableName)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result =
|
|
|
|
|
|
db.ExecuteScalar<long>("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @TableName",
|
|
|
|
|
|
new { TableName = tableName });
|
|
|
|
|
|
|
|
|
|
|
|
return result > 0;
|
|
|
|
|
|
}
|
2012-11-30 15:01:52 -01:00
|
|
|
|
|
2012-12-20 11:36:20 -01:00
|
|
|
|
public override string FormatColumnRename(string tableName, string oldName, string newName)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format(RenameColumn, tableName, oldName, newName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override string FormatTableRename(string oldName, string newName)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format(RenameTable, oldName, newName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-12-07 13:48:38 -01:00
|
|
|
|
protected override string FormatIdentity(ColumnDefinition column)
|
2012-11-30 15:01:52 -01:00
|
|
|
|
{
|
|
|
|
|
|
return column.IsIdentity ? GetIdentityString(column) : string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-12-07 13:48:38 -01:00
|
|
|
|
private static string GetIdentityString(ColumnDefinition column)
|
2012-11-30 15:01:52 -01:00
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-12-20 11:36:20 -01:00
|
|
|
|
public override string DeleteDefaultConstraint
|
|
|
|
|
|
{
|
2012-12-27 18:52:47 -01:00
|
|
|
|
get { return "ALTER TABLE [{0}] DROP CONSTRAINT [DF_{0}_{1}]"; }
|
2012-12-20 11:36:20 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-30 15:01:52 -01:00
|
|
|
|
public override string AddColumn { get { return "ALTER TABLE {0} ADD {1}"; } }
|
2012-12-20 11:36:20 -01:00
|
|
|
|
|
|
|
|
|
|
public override string DropIndex { get { return "DROP INDEX {0} ON {1}"; } }
|
|
|
|
|
|
|
|
|
|
|
|
public override string RenameColumn { get { return "sp_rename '{0}.{1}', '{2}', 'COLUMN'"; } }
|
|
|
|
|
|
|
|
|
|
|
|
public override string RenameTable { get { return "sp_rename '{0}', '{1}'"; } }
|
2012-10-19 13:20:57 -02:00
|
|
|
|
}
|
|
|
|
|
|
}
|