Revert "Temp8 tinymce"
This commit is contained in:
@@ -1,186 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using NPoco;
|
||||
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
|
||||
using Umbraco.Core.Scoping;
|
||||
|
||||
namespace Umbraco.Core.Persistence.SqlSyntax
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an SqlSyntaxProvider for Sql Server.
|
||||
/// </summary>
|
||||
[SqlSyntaxProvider(Constants.DbProviderNames.SqlServer)]
|
||||
[SqlSyntaxProvider(Constants.DatabaseProviders.SqlServer)]
|
||||
public class SqlServerSyntaxProvider : MicrosoftSqlSyntaxProviderBase<SqlServerSyntaxProvider>
|
||||
{
|
||||
// IUmbracoDatabaseFactory to be lazily injected
|
||||
public SqlServerSyntaxProvider(Lazy<IScopeProvider> lazyScopeProvider)
|
||||
/// <summary>
|
||||
/// Gets/sets the version of the current SQL server instance
|
||||
/// </summary>
|
||||
internal SqlServerVersionName GetVersionName(Database database)
|
||||
{
|
||||
_serverVersion = new Lazy<ServerVersionInfo>(() =>
|
||||
{
|
||||
var scopeProvider = lazyScopeProvider.Value;
|
||||
if (scopeProvider == null)
|
||||
throw new InvalidOperationException("Failed to determine Sql Server version (no scope provider).");
|
||||
using (var scope = scopeProvider.CreateScope())
|
||||
{
|
||||
var version = DetermineVersion(scope.Database);
|
||||
scope.Complete();
|
||||
return version;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private readonly Lazy<ServerVersionInfo> _serverVersion;
|
||||
|
||||
internal ServerVersionInfo ServerVersion => _serverVersion.Value;
|
||||
|
||||
internal enum VersionName
|
||||
{
|
||||
Invalid = -1,
|
||||
Unknown = 0,
|
||||
V7 = 1,
|
||||
V2000 = 2,
|
||||
V2005 = 3,
|
||||
V2008 = 4,
|
||||
V2012 = 5,
|
||||
V2014 = 6,
|
||||
V2016 = 7,
|
||||
V2017 = 8,
|
||||
Other = 99
|
||||
}
|
||||
|
||||
internal enum EngineEdition
|
||||
{
|
||||
Unknown = 0,
|
||||
Desktop = 1,
|
||||
Standard = 2,
|
||||
Enterprise = 3,
|
||||
Express = 4,
|
||||
Azure = 5
|
||||
}
|
||||
|
||||
internal class ServerVersionInfo
|
||||
{
|
||||
public string Edition { get; set; }
|
||||
public string InstanceName { get; set; }
|
||||
public string ProductVersion { get; set; }
|
||||
public VersionName ProductVersionName { get; private set; }
|
||||
public EngineEdition EngineEdition { get; set; }
|
||||
public bool IsAzure => EngineEdition == EngineEdition.Azure;
|
||||
public string MachineName { get; set; }
|
||||
public string ProductLevel { get; set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
ProductVersionName = MapProductVersion(ProductVersion);
|
||||
}
|
||||
}
|
||||
|
||||
private static VersionName MapProductVersion(string productVersion)
|
||||
{
|
||||
var firstPart = string.IsNullOrWhiteSpace(productVersion) ? "??" : productVersion.Split('.')[0];
|
||||
switch (firstPart)
|
||||
{
|
||||
case "??":
|
||||
return VersionName.Invalid;
|
||||
case "14":
|
||||
return VersionName.V2017;
|
||||
case "13":
|
||||
return VersionName.V2016;
|
||||
case "12":
|
||||
return VersionName.V2014;
|
||||
case "11":
|
||||
return VersionName.V2012;
|
||||
case "10":
|
||||
return VersionName.V2008;
|
||||
case "9":
|
||||
return VersionName.V2005;
|
||||
case "8":
|
||||
return VersionName.V2000;
|
||||
case "7":
|
||||
return VersionName.V7;
|
||||
default:
|
||||
return VersionName.Other;
|
||||
}
|
||||
}
|
||||
|
||||
private static ServerVersionInfo DetermineVersion(IUmbracoDatabase database)
|
||||
{
|
||||
// Edition: "Express Edition", "Windows Azure SQL Database..."
|
||||
// EngineEdition: 1/Desktop 2/Standard 3/Enterprise 4/Express 5/Azure
|
||||
// ProductLevel: RTM, SPx, CTP...
|
||||
|
||||
const string sql = @"select
|
||||
SERVERPROPERTY('Edition') Edition,
|
||||
SERVERPROPERTY('EditionID') EditionId,
|
||||
SERVERPROPERTY('InstanceName') InstanceName,
|
||||
SERVERPROPERTY('ProductVersion') ProductVersion,
|
||||
SERVERPROPERTY('BuildClrVersion') BuildClrVersion,
|
||||
SERVERPROPERTY('EngineEdition') EngineEdition,
|
||||
SERVERPROPERTY('IsClustered') IsClustered,
|
||||
SERVERPROPERTY('MachineName') MachineName,
|
||||
SERVERPROPERTY('ResourceLastUpdateDateTime') ResourceLastUpdateDateTime,
|
||||
SERVERPROPERTY('ProductLevel') ProductLevel;";
|
||||
if (_versionName.HasValue)
|
||||
return _versionName.Value;
|
||||
|
||||
try
|
||||
{
|
||||
var version = database.Fetch<ServerVersionInfo>(sql).First();
|
||||
version.Initialize();
|
||||
return version;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// can't ignore, really
|
||||
throw new Exception("Failed to determine Sql Server version (see inner exception).", e);
|
||||
}
|
||||
}
|
||||
|
||||
internal static VersionName GetVersionName(string connectionString, string providerName)
|
||||
{
|
||||
var factory = DbProviderFactories.GetFactory(providerName);
|
||||
var connection = factory.CreateConnection();
|
||||
|
||||
if (connection == null)
|
||||
throw new InvalidOperationException($"Could not create a connection for provider \"{providerName}\".");
|
||||
|
||||
connection.ConnectionString = connectionString;
|
||||
using (connection)
|
||||
{
|
||||
try
|
||||
var version = database.ExecuteScalar<string>("SELECT SERVERPROPERTY('productversion')");
|
||||
var firstPart = version.Split('.')[0];
|
||||
switch (firstPart)
|
||||
{
|
||||
connection.Open();
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = "SELECT SERVERPROPERTY('ProductVersion');";
|
||||
var productVersion = command.ExecuteScalar().ToString();
|
||||
connection.Close();
|
||||
return MapProductVersion(productVersion);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return VersionName.Unknown;
|
||||
case "13":
|
||||
_versionName = SqlServerVersionName.V2016;
|
||||
break;
|
||||
case "12":
|
||||
_versionName = SqlServerVersionName.V2014;
|
||||
break;
|
||||
case "11":
|
||||
_versionName = SqlServerVersionName.V2012;
|
||||
break;
|
||||
case "10":
|
||||
_versionName = SqlServerVersionName.V2008;
|
||||
break;
|
||||
case "9":
|
||||
_versionName = SqlServerVersionName.V2005;
|
||||
break;
|
||||
case "8":
|
||||
_versionName = SqlServerVersionName.V2000;
|
||||
break;
|
||||
case "7":
|
||||
_versionName = SqlServerVersionName.V7;
|
||||
break;
|
||||
default:
|
||||
_versionName = SqlServerVersionName.Other;
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_versionName = SqlServerVersionName.Invalid;
|
||||
}
|
||||
|
||||
return _versionName.Value;
|
||||
}
|
||||
|
||||
private SqlServerVersionName? _versionName;
|
||||
|
||||
/// <summary>
|
||||
/// SQL Server stores default values assigned to columns as constraints, it also stores them with named values, this is the only
|
||||
/// server type that does this, therefore this method doesn't exist on any other syntax provider
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<Tuple<string, string, string, string>> GetDefaultConstraintsPerColumn(IDatabase db)
|
||||
public IEnumerable<Tuple<string, string, string, string>> GetDefaultConstraintsPerColumn(Database db)
|
||||
{
|
||||
var items = db.Fetch<dynamic>("SELECT TableName = t.Name, ColumnName = c.Name, dc.Name, dc.[Definition] FROM sys.tables t INNER JOIN sys.default_constraints dc ON t.object_id = dc.parent_object_id INNER JOIN sys.columns c ON dc.parent_object_id = c.object_id AND c.column_id = dc.parent_column_id INNER JOIN sys.schemas as s on t.[schema_id] = s.[schema_id] WHERE s.name = (SELECT SCHEMA_NAME())");
|
||||
return items.Select(x => new Tuple<string, string, string, string>(x.TableName, x.ColumnName, x.Name, x.Definition));
|
||||
}
|
||||
|
||||
public override IEnumerable<string> GetTablesInSchema(IDatabase db)
|
||||
public override IEnumerable<string> GetTablesInSchema(Database db)
|
||||
{
|
||||
var items = db.Fetch<dynamic>("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = (SELECT SCHEMA_NAME())");
|
||||
return items.Select(x => x.TABLE_NAME).Cast<string>().ToList();
|
||||
}
|
||||
|
||||
public override IEnumerable<ColumnInfo> GetColumnsInSchema(IDatabase db)
|
||||
public override IEnumerable<ColumnInfo> GetColumnsInSchema(Database db)
|
||||
{
|
||||
var items = db.Fetch<dynamic>("SELECT TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = (SELECT SCHEMA_NAME())");
|
||||
return
|
||||
@@ -191,7 +89,7 @@ namespace Umbraco.Core.Persistence.SqlSyntax
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override IEnumerable<Tuple<string, string>> GetConstraintsPerTable(IDatabase db)
|
||||
public override IEnumerable<Tuple<string, string>> GetConstraintsPerTable(Database db)
|
||||
{
|
||||
var items =
|
||||
db.Fetch<dynamic>(
|
||||
@@ -200,7 +98,7 @@ namespace Umbraco.Core.Persistence.SqlSyntax
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override IEnumerable<Tuple<string, string, string>> GetConstraintsPerColumn(IDatabase db)
|
||||
public override IEnumerable<Tuple<string, string, string>> GetConstraintsPerColumn(Database db)
|
||||
{
|
||||
var items =
|
||||
db.Fetch<dynamic>(
|
||||
@@ -209,15 +107,15 @@ namespace Umbraco.Core.Persistence.SqlSyntax
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override IEnumerable<Tuple<string, string, string, bool>> GetDefinedIndexes(IDatabase db)
|
||||
public override IEnumerable<Tuple<string, string, string, bool>> GetDefinedIndexes(Database db)
|
||||
{
|
||||
var items =
|
||||
db.Fetch<dynamic>(
|
||||
@"select T.name as TABLE_NAME, I.name as INDEX_NAME, AC.Name as COLUMN_NAME,
|
||||
CASE WHEN I.is_unique_constraint = 1 OR I.is_unique = 1 THEN 1 ELSE 0 END AS [UNIQUE]
|
||||
from sys.tables as T inner join sys.indexes as I on T.[object_id] = I.[object_id]
|
||||
inner join sys.index_columns as IC on IC.[object_id] = I.[object_id] and IC.[index_id] = I.[index_id]
|
||||
inner join sys.all_columns as AC on IC.[object_id] = AC.[object_id] and IC.[column_id] = AC.[column_id]
|
||||
from sys.tables as T inner join sys.indexes as I on T.[object_id] = I.[object_id]
|
||||
inner join sys.index_columns as IC on IC.[object_id] = I.[object_id] and IC.[index_id] = I.[index_id]
|
||||
inner join sys.all_columns as AC on IC.[object_id] = AC.[object_id] and IC.[column_id] = AC.[column_id]
|
||||
inner join sys.schemas as S on T.[schema_id] = S.[schema_id]
|
||||
WHERE S.name = (SELECT SCHEMA_NAME()) AND I.is_primary_key = 0
|
||||
order by T.name, I.name");
|
||||
@@ -226,7 +124,7 @@ order by T.name, I.name");
|
||||
|
||||
}
|
||||
|
||||
public override bool DoesTableExist(IDatabase db, string tableName)
|
||||
public override bool DoesTableExist(Database db, string tableName)
|
||||
{
|
||||
var result =
|
||||
db.ExecuteScalar<long>("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @TableName AND TABLE_SCHEMA = (SELECT SCHEMA_NAME())",
|
||||
@@ -250,9 +148,9 @@ order by T.name, I.name");
|
||||
return column.IsIdentity ? GetIdentityString(column) : string.Empty;
|
||||
}
|
||||
|
||||
public override Sql<ISqlContext> SelectTop(Sql<ISqlContext> sql, int top)
|
||||
public override Sql SelectTop(Sql sql, int top)
|
||||
{
|
||||
return new Sql<ISqlContext>(sql.SqlContext, sql.SQL.Insert(sql.SQL.IndexOf(' '), " TOP " + top), sql.Arguments);
|
||||
return new Sql(sql.SQL.Insert(sql.SQL.IndexOf(' '), " TOP " + top), sql.Arguments);
|
||||
}
|
||||
|
||||
private static string GetIdentityString(ColumnDefinition column)
|
||||
@@ -277,10 +175,14 @@ order by T.name, I.name");
|
||||
return null;
|
||||
}
|
||||
|
||||
public override string DeleteDefaultConstraint => "ALTER TABLE [{0}] DROP CONSTRAINT [DF_{0}_{1}]";
|
||||
public override string DeleteDefaultConstraint
|
||||
{
|
||||
get { return "ALTER TABLE [{0}] DROP CONSTRAINT [DF_{0}_{1}]"; }
|
||||
}
|
||||
|
||||
public override string DropIndex => "DROP INDEX {0} ON {1}";
|
||||
|
||||
public override string RenameColumn => "sp_rename '{0}.{1}', '{2}', 'COLUMN'";
|
||||
public override string DropIndex { get { return "DROP INDEX {0} ON {1}"; } }
|
||||
|
||||
public override string RenameColumn { get { return "sp_rename '{0}.{1}', '{2}', 'COLUMN'"; } }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user