61 lines
2.3 KiB
C#
61 lines
2.3 KiB
C#
using NPoco;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
|
|
|
|
namespace Umbraco.Core.Persistence.SqlSyntax
|
|
{
|
|
internal static class SqlSyntaxProviderExtensions
|
|
{
|
|
public static IEnumerable<DbIndexDefinition> GetDefinedIndexesDefinitions(this ISqlSyntaxProvider sql, IDatabase db)
|
|
{
|
|
return sql.GetDefinedIndexes(db)
|
|
.Select(x => new DbIndexDefinition
|
|
{
|
|
TableName = x.Item1,
|
|
IndexName = x.Item2,
|
|
ColumnName = x.Item3,
|
|
IsUnique = x.Item4
|
|
}).ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the quotes tableName.columnName combo
|
|
/// </summary>
|
|
/// <param name="sql"></param>
|
|
/// <param name="tableName"></param>
|
|
/// <param name="columnName"></param>
|
|
/// <returns></returns>
|
|
public static string GetQuotedColumn(this ISqlSyntaxProvider sql, string tableName, string columnName)
|
|
{
|
|
return sql.GetQuotedTableName(tableName) + "." + sql.GetQuotedColumnName(columnName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// This is used to generate a delete query that uses a sub-query to select the data, it is required because there's a very particular syntax that
|
|
/// needs to be used to work for all servers: MySql, SQLCE and MSSQL
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// See: http://issues.umbraco.org/issue/U4-3876
|
|
/// </remarks>
|
|
public static Sql GetDeleteSubquery(this ISqlSyntaxProvider sqlProvider, string tableName, string columnName, Sql subQuery, WhereInType whereInType = WhereInType.In)
|
|
{
|
|
|
|
return
|
|
new Sql(string.Format(
|
|
whereInType == WhereInType.In
|
|
? @"DELETE FROM {0} WHERE {1} IN (SELECT {1} FROM ({2}) x)"
|
|
: @"DELETE FROM {0} WHERE {1} NOT IN (SELECT {1} FROM ({2}) x)",
|
|
sqlProvider.GetQuotedTableName(tableName),
|
|
sqlProvider.GetQuotedColumnName(columnName),
|
|
subQuery.SQL), subQuery.Arguments);
|
|
}
|
|
}
|
|
|
|
internal enum WhereInType
|
|
{
|
|
In,
|
|
NotIn
|
|
}
|
|
} |