Cleanup ISqlSyntaxProvider

This commit is contained in:
Stephan
2018-07-17 17:20:40 +02:00
parent 132b729dd9
commit c7c1c7e8fa
8 changed files with 23 additions and 152 deletions

View File

@@ -272,7 +272,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement
if (orderBySystemField == false || orderBy.InvariantEquals(dbfield) == false)
{
// get alias, if aliased
var matches = VersionableRepositoryBaseAliasRegex.For(SqlContext.SqlSyntax).Matches(sql.SQL);
var matches = SqlContext.SqlSyntax.AliasRegex.Matches(sql.SQL);
var match = matches.Cast<Match>().FirstOrDefault(m => m.Groups[1].Value.InvariantEquals(dbfield));
if (match != null) dbfield = match.Groups[2].Value;
@@ -304,7 +304,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement
// note: ContentTypeAlias is not properly managed because it's not part of the query to begin with!
// get alias, if aliased
var matches = VersionableRepositoryBaseAliasRegex.For(SqlContext.SqlSyntax).Matches(sql.SQL);
var matches = SqlContext.SqlSyntax.AliasRegex.Matches(sql.SQL);
var match = matches.Cast<Match>().FirstOrDefault(m => m.Groups[1].Value.InvariantEquals(dbfield));
if (match != null) dbfield = match.Groups[2].Value;

View File

@@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Umbraco.Core.Persistence.SqlSyntax;
namespace Umbraco.Core.Persistence.Repositories.Implement
{
internal static class VersionableRepositoryBaseAliasRegex
{
private static readonly Dictionary<Type, Regex> Regexes = new Dictionary<Type, Regex>();
public static Regex For(ISqlSyntaxProvider sqlSyntax)
{
var type = sqlSyntax.GetType();
Regex aliasRegex;
if (Regexes.TryGetValue(type, out aliasRegex))
return aliasRegex;
var col = Regex.Escape(sqlSyntax.GetQuotedColumnName("column")).Replace("column", @"\w+");
var fld = Regex.Escape(sqlSyntax.GetQuotedTableName("table") + ".").Replace("table", @"\w+") + col;
aliasRegex = new Regex("(" + fld + @")\s+AS\s+(" + col + ")", RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);
Regexes[type] = aliasRegex;
return aliasRegex;
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using NPoco;
using Umbraco.Core.Persistence.DatabaseAnnotations;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
@@ -19,17 +20,6 @@ namespace Umbraco.Core.Persistence.SqlSyntax
string GetStringColumnWildcardComparison(string column, int paramIndex, TextColumnType columnType);
string GetConcat(params string[] args);
[Obsolete("Use the overload with the parameter index instead")]
string GetStringColumnEqualComparison(string column, string value, TextColumnType columnType);
[Obsolete("Use the overload with the parameter index instead")]
string GetStringColumnStartsWithComparison(string column, string value, TextColumnType columnType);
[Obsolete("Use the overload with the parameter index instead")]
string GetStringColumnEndsWithComparison(string column, string value, TextColumnType columnType);
[Obsolete("Use the overload with the parameter index instead")]
string GetStringColumnContainsComparison(string column, string value, TextColumnType columnType);
[Obsolete("Use the overload with the parameter index instead")]
string GetStringColumnWildcardComparison(string column, string value, TextColumnType columnType);
string GetQuotedTableName(string tableName);
string GetQuotedColumnName(string columnName);
string GetQuotedName(string name);
@@ -70,6 +60,14 @@ namespace Umbraco.Core.Persistence.SqlSyntax
string FormatColumnRename(string tableName, string oldName, string newName);
string FormatTableRename(string oldName, string newName);
/// <summary>
/// Gets a regex matching aliased fields.
/// </summary>
/// <remarks>
/// <para>Matches "(table.column) AS (alias)" where table, column and alias are properly escaped.</para>
/// </remarks>
Regex AliasRegex { get; }
Sql<ISqlContext> SelectTop(Sql<ISqlContext> sql, int top);
bool SupportsClustered();

View File

@@ -70,66 +70,6 @@ namespace Umbraco.Core.Persistence.SqlSyntax
}
}
[Obsolete("Use the overload with the parameter index instead")]
public override string GetStringColumnStartsWithComparison(string column, string value, TextColumnType columnType)
{
switch (columnType)
{
case TextColumnType.NVarchar:
return base.GetStringColumnStartsWithComparison(column, value, columnType);
case TextColumnType.NText:
//MSSQL doesn't allow for upper methods with NText columns
return $"{column} LIKE '{value}%'";
default:
throw new ArgumentOutOfRangeException(nameof(columnType));
}
}
[Obsolete("Use the overload with the parameter index instead")]
public override string GetStringColumnEndsWithComparison(string column, string value, TextColumnType columnType)
{
switch (columnType)
{
case TextColumnType.NVarchar:
return base.GetStringColumnEndsWithComparison(column, value, columnType);
case TextColumnType.NText:
//MSSQL doesn't allow for upper methods with NText columns
return $"{column} LIKE '%{value}'";
default:
throw new ArgumentOutOfRangeException(nameof(columnType));
}
}
[Obsolete("Use the overload with the parameter index instead")]
public override string GetStringColumnContainsComparison(string column, string value, TextColumnType columnType)
{
switch (columnType)
{
case TextColumnType.NVarchar:
return base.GetStringColumnContainsComparison(column, value, columnType);
case TextColumnType.NText:
//MSSQL doesn't allow for upper methods with NText columns
return $"{column} LIKE '%{value}%'";
default:
throw new ArgumentOutOfRangeException(nameof(columnType));
}
}
[Obsolete("Use the overload with the parameter index instead")]
public override string GetStringColumnWildcardComparison(string column, string value, TextColumnType columnType)
{
switch (columnType)
{
case TextColumnType.NVarchar:
return base.GetStringColumnContainsComparison(column, value, columnType);
case TextColumnType.NText:
//MSSQL doesn't allow for upper methods with NText columns
return $"{column} LIKE '{value}'";
default:
throw new ArgumentOutOfRangeException(nameof(columnType));
}
}
/// <summary>
/// This uses a the DbTypeMap created and custom mapping to resolve the SqlDbType
/// </summary>

View File

@@ -49,21 +49,6 @@ namespace Umbraco.Core.Persistence.SqlSyntax
return indexType;
}
[Obsolete("Use the overload with the parameter index instead")]
public override string GetStringColumnEqualComparison(string column, string value, TextColumnType columnType)
{
switch (columnType)
{
case TextColumnType.NVarchar:
return base.GetStringColumnEqualComparison(column, value, columnType);
case TextColumnType.NText:
//MSSQL doesn't allow for = comparison with NText columns but allows this syntax
return string.Format("{0} LIKE '{1}'", column, value);
default:
throw new ArgumentOutOfRangeException("columnType");
}
}
public override string GetConcat(params string[] args)
{
return "(" + string.Join("+", args) + ")";

View File

@@ -4,6 +4,7 @@ using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NPoco;
using Umbraco.Core.Persistence.DatabaseAnnotations;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
@@ -40,8 +41,17 @@ namespace Umbraco.Core.Persistence.SqlSyntax
DecimalColumnDefinition = string.Format(DecimalColumnDefinitionFormat, DefaultDecimalPrecision, DefaultDecimalScale);
InitColumnTypeMap();
// ReSharper disable VirtualMemberCallInConstructor
// ok to call virtual GetQuotedXxxName here - they don't depend on any state
var col = Regex.Escape(GetQuotedColumnName("column")).Replace("column", @"\w+");
var fld = Regex.Escape(GetQuotedTableName("table") + ".").Replace("table", @"\w+") + col;
// ReSharper restore VirtualMemberCallInConstructor
AliasRegex = new Regex("(" + fld + @")\s+AS\s+(" + col + ")", RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);
}
public Regex AliasRegex { get; }
public string GetWildcardPlaceholder()
{
return "%";
@@ -137,41 +147,6 @@ namespace Umbraco.Core.Persistence.SqlSyntax
return $"upper({column}) LIKE upper(@{paramIndex})";
}
[Obsolete("Use the overload with the parameter index instead")]
public virtual string GetStringColumnEqualComparison(string column, string value, TextColumnType columnType)
{
//use the 'upper' method to always ensure strings are matched without case sensitivity no matter what the db setting.
return $"upper({column}) = '{value.ToUpper()}'";
}
[Obsolete("Use the overload with the parameter index instead")]
public virtual string GetStringColumnStartsWithComparison(string column, string value, TextColumnType columnType)
{
//use the 'upper' method to always ensure strings are matched without case sensitivity no matter what the db setting.
return $"upper({column}) LIKE '{value.ToUpper()}%'";
}
[Obsolete("Use the overload with the parameter index instead")]
public virtual string GetStringColumnEndsWithComparison(string column, string value, TextColumnType columnType)
{
//use the 'upper' method to always ensure strings are matched without case sensitivity no matter what the db setting.
return $"upper({column}) LIKE '%{value.ToUpper()}'";
}
[Obsolete("Use the overload with the parameter index instead")]
public virtual string GetStringColumnContainsComparison(string column, string value, TextColumnType columnType)
{
//use the 'upper' method to always ensure strings are matched without case sensitivity no matter what the db setting.
return $"upper({column}) LIKE '%{value.ToUpper()}%'";
}
[Obsolete("Use the overload with the parameter index instead")]
public virtual string GetStringColumnWildcardComparison(string column, string value, TextColumnType columnType)
{
//use the 'upper' method to always ensure strings are matched without case sensitivity no matter what the db setting.
return $"upper({column}) LIKE '{value.ToUpper()}'";
}
public virtual string GetConcat(params string[] args)
{
return "concat(" + string.Join(",", args) + ")";

View File

@@ -1206,7 +1206,6 @@
<Compile Include="Persistence\Repositories\Implement\UserGroupRepository.cs" />
<Compile Include="Persistence\Repositories\Implement\UserRepository.cs" />
<Compile Include="Persistence\Repositories\Implement\ContentRepositoryBase.cs" />
<Compile Include="Persistence\Repositories\Implement\VersionableRepositoryBaseAliasRegex.cs" />
<Compile Include="Persistence\SqlContext.cs" />
<Compile Include="Persistence\SqlSyntax\ColumnInfo.cs" />
<Compile Include="Persistence\SqlSyntax\DbTypes.cs" />

View File

@@ -667,9 +667,9 @@ namespace Umbraco.Tests.Persistence.Repositories
}
[Test]
public void RegexAliasTest()
public void AliasRegexTest()
{
var regex = VersionableRepositoryBaseAliasRegex.For(new SqlServerSyntaxProvider(new Lazy<IScopeProvider>(() => null)));
var regex = new SqlServerSyntaxProvider(new Lazy<IScopeProvider>(() => null)).AliasRegex;
Assert.AreEqual(@"(\[\w+]\.\[\w+])\s+AS\s+(\[\w+])", regex.ToString());
const string sql = "SELECT [table].[column1] AS [alias1], [table].[column2] AS [alias2] FROM [table];";
var matches = regex.Matches(sql);