2016-04-12 15:11:07 +02:00
|
|
|
using System.Collections;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Reflection;
|
2016-11-03 10:31:44 +01:00
|
|
|
using System.Text;
|
2019-02-20 15:01:32 +01:00
|
|
|
using System.Text.RegularExpressions;
|
2016-04-12 15:11:07 +02:00
|
|
|
using NPoco;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Querying;
|
2022-06-02 08:18:31 +02:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2021-02-12 13:36:50 +01:00
|
|
|
namespace Umbraco.Extensions
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2019-05-20 17:57:28 +02:00
|
|
|
public static partial class NPocoSqlExtensions
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
|
|
|
|
#region Where
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a WHERE clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="predicate">A predicate to transform and append to the Sql statement.</param>
|
2017-11-21 14:19:16 +01:00
|
|
|
/// <param name="alias">An optional alias for the table.</param>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public static Sql<ISqlContext> Where<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, bool>> predicate, string? alias = null)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
(string s, object[] a) = sql.SqlContext.VisitDto(predicate, alias);
|
2018-09-17 13:06:20 +02:00
|
|
|
return sql.Where(s, a);
|
|
|
|
|
}
|
2018-07-04 14:48:44 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a WHERE clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto1">The type of Dto 1.</typeparam>
|
|
|
|
|
/// <typeparam name="TDto2">The type of Dto 2.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="predicate">A predicate to transform and append to the Sql statement.</param>
|
|
|
|
|
/// <param name="alias1">An optional alias for Dto 1 table.</param>
|
|
|
|
|
/// <param name="alias2">An optional alias for Dto 2 table.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public static Sql<ISqlContext> Where<TDto1, TDto2>(this Sql<ISqlContext> sql, Expression<Func<TDto1, TDto2, bool>> predicate, string? alias1 = null, string? alias2 = null)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
(string s, object[] a) = sql.SqlContext.VisitDto(predicate, alias1, alias2);
|
2018-09-17 13:06:20 +02:00
|
|
|
return sql.Where(s, a);
|
2017-09-22 15:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:29:55 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a WHERE clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto1">The type of Dto 1.</typeparam>
|
|
|
|
|
/// <typeparam name="TDto2">The type of Dto 2.</typeparam>
|
|
|
|
|
/// <typeparam name="TDto3">The type of Dto 3.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="predicate">A predicate to transform and append to the Sql statement.</param>
|
|
|
|
|
/// <param name="alias1">An optional alias for Dto 1 table.</param>
|
|
|
|
|
/// <param name="alias2">An optional alias for Dto 2 table.</param>
|
|
|
|
|
/// <param name="alias3">An optional alias for Dto 3 table.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
public static Sql<ISqlContext> Where<TDto1, TDto2, TDto3>(this Sql<ISqlContext> sql, Expression<Func<TDto1, TDto2, TDto3, bool>> predicate, string? alias1 = null, string? alias2 = null, string? alias3 = null)
|
|
|
|
|
{
|
|
|
|
|
var (s, a) = sql.SqlContext.VisitDto(predicate, alias1, alias2, alias3);
|
|
|
|
|
return sql.Where(s, a);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a WHERE IN clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="field">An expression specifying the field.</param>
|
|
|
|
|
/// <param name="values">The values.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> WhereIn<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field, IEnumerable? values)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
2018-09-17 13:06:20 +02:00
|
|
|
var fieldName = sql.SqlContext.SqlSyntax.GetFieldName(field);
|
2016-11-03 10:31:44 +01:00
|
|
|
sql.Where(fieldName + " IN (@values)", new { values });
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a WHERE IN clause to the Sql statement.
|
2024-09-10 00:49:18 +09:00
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="field">An expression specifying the field.</param>
|
|
|
|
|
/// <param name="values">The values.</param>
|
2024-10-02 14:32:31 +02:00
|
|
|
/// <param name="alias"></param>
|
2024-09-10 00:49:18 +09:00
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
public static Sql<ISqlContext> WhereIn<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field, IEnumerable? values, string alias)
|
|
|
|
|
{
|
|
|
|
|
var fieldName = sql.SqlContext.SqlSyntax.GetFieldName(field, alias);
|
|
|
|
|
sql.Where(fieldName + " IN (@values)", new { values });
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a WHERE IN clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="field">An expression specifying the field.</param>
|
|
|
|
|
/// <param name="values">A subquery returning the value.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> WhereIn<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field, Sql<ISqlContext>? values)
|
2016-11-03 10:31:44 +01:00
|
|
|
{
|
2022-03-07 22:46:16 +01:00
|
|
|
return WhereIn(sql, field, values, false, null);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 09:14:12 +01:00
|
|
|
public static Sql<ISqlContext> WhereIn<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field, Sql<ISqlContext>? values, string tableAlias)
|
2022-03-07 22:46:16 +01:00
|
|
|
{
|
|
|
|
|
return sql.WhereIn(field, values, false, tableAlias);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-03-17 09:14:12 +01:00
|
|
|
public static Sql<ISqlContext> WhereLike<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> fieldSelector, Sql<ISqlContext>? valuesSql)
|
2022-03-07 22:46:16 +01:00
|
|
|
{
|
|
|
|
|
var fieldName = sql.SqlContext.SqlSyntax.GetFieldName(fieldSelector);
|
2022-03-17 09:14:12 +01:00
|
|
|
sql.Where(fieldName + " LIKE (" + valuesSql?.SQL + ")", valuesSql?.Arguments);
|
2022-03-07 22:46:16 +01:00
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 15:55:29 +02:00
|
|
|
public static Sql<ISqlContext> Union(this Sql<ISqlContext> sql, Sql<ISqlContext> sql2)
|
|
|
|
|
{
|
|
|
|
|
return sql.Append( " UNION ").Append(sql2);
|
|
|
|
|
}
|
2022-06-02 20:45:24 +02:00
|
|
|
|
2022-05-31 15:55:29 +02:00
|
|
|
public static Sql<ISqlContext>.SqlJoinClause<ISqlContext> InnerJoinNested(this Sql<ISqlContext> sql, Sql<ISqlContext> nestedQuery, string alias)
|
|
|
|
|
{
|
|
|
|
|
return new Sql<ISqlContext>.SqlJoinClause<ISqlContext>(sql.Append("INNER JOIN (").Append(nestedQuery)
|
|
|
|
|
.Append($") [{alias}]"));
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 09:14:12 +01:00
|
|
|
public static Sql<ISqlContext> WhereLike<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> fieldSelector, string likeValue)
|
2022-03-07 22:46:16 +01:00
|
|
|
{
|
|
|
|
|
var fieldName = sql.SqlContext.SqlSyntax.GetFieldName(fieldSelector);
|
|
|
|
|
sql.Where(fieldName + " LIKE ('" + likeValue + "')");
|
|
|
|
|
return sql;
|
2017-09-22 15:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a WHERE NOT IN clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="field">An expression specifying the field.</param>
|
|
|
|
|
/// <param name="values">The values.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> WhereNotIn<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field, IEnumerable values)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
2018-09-17 13:06:20 +02:00
|
|
|
var fieldName = sql.SqlContext.SqlSyntax.GetFieldName(field);
|
2017-09-22 15:23:46 +02:00
|
|
|
sql.Where(fieldName + " NOT IN (@values)", new { values });
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a WHERE NOT IN clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="field">An expression specifying the field.</param>
|
|
|
|
|
/// <param name="values">A subquery returning the value.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> WhereNotIn<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field, Sql<ISqlContext> values)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
|
|
|
|
return sql.WhereIn(field, values, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends multiple OR WHERE IN clauses to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="fields">Expressions specifying the fields.</param>
|
|
|
|
|
/// <param name="values">The values.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql WhereAnyIn<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>>[] fields, IEnumerable values)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
ISqlSyntaxProvider sqlSyntax = sql.SqlContext.SqlSyntax;
|
2018-09-17 13:06:20 +02:00
|
|
|
var fieldNames = fields.Select(x => sqlSyntax.GetFieldName(x)).ToArray();
|
2016-11-03 10:31:44 +01:00
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
sb.Append("(");
|
|
|
|
|
for (var i = 0; i < fieldNames.Length; i++)
|
|
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (i > 0)
|
|
|
|
|
{
|
|
|
|
|
sb.Append(" OR ");
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
sb.Append(fieldNames[i]);
|
|
|
|
|
sql.Append(" IN (@values)");
|
|
|
|
|
}
|
|
|
|
|
sb.Append(")");
|
|
|
|
|
sql.Where(sb.ToString(), new { values });
|
2016-04-12 15:11:07 +02:00
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
private static Sql<ISqlContext> WhereIn<T>(this Sql<ISqlContext> sql, Expression<Func<T, object?>> fieldSelector, Sql? valuesSql, bool not)
|
2017-09-13 14:40:10 +02:00
|
|
|
{
|
2022-03-07 22:46:16 +01:00
|
|
|
return WhereIn(sql, fieldSelector, valuesSql, not, null);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 09:14:12 +01:00
|
|
|
private static Sql<ISqlContext> WhereIn<T>(this Sql<ISqlContext> sql, Expression<Func<T, object?>> fieldSelector, Sql? valuesSql, bool not, string? tableAlias)
|
2022-03-07 22:46:16 +01:00
|
|
|
{
|
|
|
|
|
var fieldName = sql.SqlContext.SqlSyntax.GetFieldName(fieldSelector, tableAlias);
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
sql.Where(fieldName + (not ? " NOT" : string.Empty) + " IN (" + valuesSql?.SQL + ")", valuesSql?.Arguments);
|
2017-09-13 14:40:10 +02:00
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 14:19:16 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends multiple OR WHERE clauses to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="predicates">The WHERE predicates.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
public static Sql<ISqlContext> WhereAny(this Sql<ISqlContext> sql, params Func<Sql<ISqlContext>, Sql<ISqlContext>>[] predicates)
|
|
|
|
|
{
|
|
|
|
|
var wsql = new Sql<ISqlContext>(sql.SqlContext);
|
|
|
|
|
|
|
|
|
|
wsql.Append("(");
|
|
|
|
|
for (var i = 0; i < predicates.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i > 0)
|
2022-06-02 08:18:31 +02:00
|
|
|
{
|
2017-11-21 14:19:16 +01:00
|
|
|
wsql.Append(") OR (");
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-21 14:19:16 +01:00
|
|
|
var temp = new Sql<ISqlContext>(sql.SqlContext);
|
|
|
|
|
temp = predicates[i](temp);
|
2024-11-13 09:27:29 +01:00
|
|
|
wsql.Append(temp.SQL.TrimStart("WHERE "), temp.Arguments);
|
2017-11-21 14:19:16 +01:00
|
|
|
}
|
|
|
|
|
wsql.Append(")");
|
|
|
|
|
|
|
|
|
|
return sql.Where(wsql.SQL, wsql.Arguments);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a WHERE NOT NULL clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="field">Expression specifying the field.</param>
|
|
|
|
|
/// <param name="tableAlias">An optional alias for the table.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> WhereNotNull<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field, string? tableAlias = null)
|
2017-11-21 14:19:16 +01:00
|
|
|
{
|
|
|
|
|
return sql.WhereNull(field, tableAlias, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a WHERE [NOT] NULL clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="field">Expression specifying the field.</param>
|
|
|
|
|
/// <param name="tableAlias">An optional alias for the table.</param>
|
|
|
|
|
/// <param name="not">A value indicating whether to NOT NULL.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> WhereNull<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field, string? tableAlias = null, bool not = false)
|
2017-11-21 14:19:16 +01:00
|
|
|
{
|
|
|
|
|
var column = sql.GetColumns(columnExpressions: new[] { field }, tableAlias: tableAlias, withAlias: false).First();
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
return sql.Where("(" + column + " IS " + (not ? "NOT " : string.Empty) + "NULL)");
|
2017-11-21 14:19:16 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region From
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a FROM clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
2017-11-10 11:27:12 +01:00
|
|
|
/// <param name="alias">An optional table alias</param>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public static Sql<ISqlContext> From<TDto>(this Sql<ISqlContext> sql, string? alias = null)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
Type type = typeof (TDto);
|
2016-11-03 10:31:44 +01:00
|
|
|
var tableName = type.GetTableName();
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-11-10 11:27:12 +01:00
|
|
|
var from = sql.SqlContext.SqlSyntax.GetQuotedTableName(tableName);
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(alias))
|
2022-06-02 08:18:31 +02:00
|
|
|
{
|
2017-11-10 11:27:12 +01:00
|
|
|
from += " " + sql.SqlContext.SqlSyntax.GetQuotedTableName(alias);
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-10 11:27:12 +01:00
|
|
|
sql.From(from);
|
|
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region OrderBy, GroupBy
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an ORDER BY clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="field">An expression specifying the field.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> OrderBy<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2018-09-17 13:06:20 +02:00
|
|
|
return sql.OrderBy("(" + sql.SqlContext.SqlSyntax.GetFieldName(field) + ")");
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-02 20:53:16 +02:00
|
|
|
public static Sql<ISqlContext> OrderBy<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field, string alias)
|
2022-05-31 15:55:29 +02:00
|
|
|
{
|
|
|
|
|
return sql.OrderBy("(" + sql.SqlContext.SqlSyntax.GetFieldName(field, alias) + ")");
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an ORDER BY clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="fields">Expression specifying the fields.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> OrderBy<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[] fields)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
ISqlSyntaxProvider sqlSyntax = sql.SqlContext.SqlSyntax;
|
2017-09-22 15:23:46 +02:00
|
|
|
var columns = fields.Length == 0
|
|
|
|
|
? sql.GetColumns<TDto>(withAlias: false)
|
2018-09-17 13:06:20 +02:00
|
|
|
: fields.Select(x => sqlSyntax.GetFieldName(x)).ToArray();
|
2017-09-22 15:23:46 +02:00
|
|
|
return sql.OrderBy(columns);
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an ORDER BY DESC clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="field">An expression specifying the field.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> OrderByDescending<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2022-02-28 23:06:59 +01:00
|
|
|
return sql.OrderByDescending(sql.SqlContext.SqlSyntax.GetFieldName(field));
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an ORDER BY DESC clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="fields">Expression specifying the fields.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> OrderByDescending<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[] fields)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
ISqlSyntaxProvider sqlSyntax = sql.SqlContext.SqlSyntax;
|
2017-09-22 15:23:46 +02:00
|
|
|
var columns = fields.Length == 0
|
|
|
|
|
? sql.GetColumns<TDto>(withAlias: false)
|
2018-09-17 13:06:20 +02:00
|
|
|
: fields.Select(x => sqlSyntax.GetFieldName(x)).ToArray();
|
2022-02-28 23:06:59 +01:00
|
|
|
return sql.OrderByDescending(columns);
|
2017-09-22 15:23:46 +02:00
|
|
|
}
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an ORDER BY DESC clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
2018-10-18 14:16:54 +02:00
|
|
|
/// <param name="fields">Fields.</param>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> OrderByDescending(this Sql<ISqlContext> sql, params string?[] fields)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
|
|
|
|
return sql.Append("ORDER BY " + string.Join(", ", fields.Select(x => x + " DESC")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a GROUP BY clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="field">An expression specifying the field.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> GroupBy<TDto>(this Sql<ISqlContext> sql, Expression<Func<TDto, object?>> field)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
2018-09-17 13:06:20 +02:00
|
|
|
return sql.GroupBy(sql.SqlContext.SqlSyntax.GetFieldName(field));
|
2017-09-22 15:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a GROUP BY clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="fields">Expression specifying the fields.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> GroupBy<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[] fields)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
ISqlSyntaxProvider sqlSyntax = sql.SqlContext.SqlSyntax;
|
2017-09-22 15:23:46 +02:00
|
|
|
var columns = fields.Length == 0
|
|
|
|
|
? sql.GetColumns<TDto>(withAlias: false)
|
2018-09-17 13:06:20 +02:00
|
|
|
: fields.Select(x => sqlSyntax.GetFieldName(x)).ToArray();
|
2017-09-22 15:23:46 +02:00
|
|
|
return sql.GroupBy(columns);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 09:39:56 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a GROUP BY clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="tableAlias">A table alias.</param>
|
|
|
|
|
/// <param name="fields">Expression specifying the fields.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
public static Sql<ISqlContext> GroupBy<TDto>(
|
|
|
|
|
this Sql<ISqlContext> sql,
|
|
|
|
|
string tableAlias,
|
|
|
|
|
params Expression<Func<TDto, object?>>[] fields)
|
|
|
|
|
{
|
|
|
|
|
ISqlSyntaxProvider sqlSyntax = sql.SqlContext.SqlSyntax;
|
|
|
|
|
var columns = fields.Length == 0
|
|
|
|
|
? sql.GetColumns<TDto>(withAlias: false)
|
|
|
|
|
: fields.Select(x => sqlSyntax.GetFieldName(x, tableAlias)).ToArray();
|
|
|
|
|
return sql.GroupBy(columns);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends more ORDER BY or GROUP BY fields to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="fields">Expressions specifying the fields.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> AndBy<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[] fields)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
ISqlSyntaxProvider sqlSyntax = sql.SqlContext.SqlSyntax;
|
2017-09-22 15:23:46 +02:00
|
|
|
var columns = fields.Length == 0
|
|
|
|
|
? sql.GetColumns<TDto>(withAlias: false)
|
2018-09-17 13:06:20 +02:00
|
|
|
: fields.Select(x => sqlSyntax.GetFieldName(x)).ToArray();
|
2017-09-22 15:23:46 +02:00
|
|
|
return sql.Append(", " + string.Join(", ", columns));
|
2024-03-18 10:46:03 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Sql<ISqlContext> AndBy<TDto>(this Sql<ISqlContext> sql, string tableAlias,
|
|
|
|
|
params Expression<Func<TDto, object?>>[] fields)
|
|
|
|
|
{
|
|
|
|
|
ISqlSyntaxProvider sqlSyntax = sql.SqlContext.SqlSyntax;
|
|
|
|
|
var columns = fields.Length == 0
|
|
|
|
|
? sql.GetColumns<TDto>(withAlias: false)
|
|
|
|
|
: fields.Select(x => sqlSyntax.GetFieldName(x, tableAlias)).ToArray();
|
|
|
|
|
return sql.Append(", " + string.Join(", ", columns));
|
2017-09-22 15:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends more ORDER BY DESC fields to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="fields">Expressions specifying the fields.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> AndByDescending<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[] fields)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
ISqlSyntaxProvider sqlSyntax = sql.SqlContext.SqlSyntax;
|
2017-09-22 15:23:46 +02:00
|
|
|
var columns = fields.Length == 0
|
|
|
|
|
? sql.GetColumns<TDto>(withAlias: false)
|
2018-09-17 13:06:20 +02:00
|
|
|
: fields.Select(x => sqlSyntax.GetFieldName(x)).ToArray();
|
2017-09-22 15:23:46 +02:00
|
|
|
return sql.Append(", " + string.Join(", ", columns.Select(x => x + " DESC")));
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Joins
|
|
|
|
|
|
2018-09-25 10:55:06 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a CROSS JOIN clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="alias">An optional alias for the joined table.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public static Sql<ISqlContext> CrossJoin<TDto>(this Sql<ISqlContext> sql, string? alias = null)
|
2018-09-25 10:55:06 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
Type type = typeof(TDto);
|
2018-09-25 10:55:06 +02:00
|
|
|
var tableName = type.GetTableName();
|
|
|
|
|
var join = sql.SqlContext.SqlSyntax.GetQuotedTableName(tableName);
|
2022-06-02 08:18:31 +02:00
|
|
|
if (alias != null)
|
|
|
|
|
{
|
|
|
|
|
join += " " + sql.SqlContext.SqlSyntax.GetQuotedTableName(alias);
|
|
|
|
|
}
|
2018-09-25 10:55:06 +02:00
|
|
|
|
|
|
|
|
return sql.Append("CROSS JOIN " + join);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an INNER JOIN clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="alias">An optional alias for the joined table.</param>
|
|
|
|
|
/// <returns>A SqlJoin statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public static Sql<ISqlContext>.SqlJoinClause<ISqlContext> InnerJoin<TDto>(this Sql<ISqlContext> sql, string? alias = null)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
Type type = typeof(TDto);
|
2016-11-03 10:31:44 +01:00
|
|
|
var tableName = type.GetTableName();
|
2017-09-22 15:23:46 +02:00
|
|
|
var join = sql.SqlContext.SqlSyntax.GetQuotedTableName(tableName);
|
2022-06-02 08:18:31 +02:00
|
|
|
if (alias != null)
|
|
|
|
|
{
|
|
|
|
|
join += " " + sql.SqlContext.SqlSyntax.GetQuotedTableName(alias);
|
|
|
|
|
}
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
return sql.InnerJoin(join);
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-21 14:47:27 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an INNER JOIN clause using a nested query.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The SQL statement.</param>
|
|
|
|
|
/// <param name="nestedSelect">The nested sql query.</param>
|
|
|
|
|
/// <param name="alias">An optional alias for the joined table.</param>
|
|
|
|
|
/// <returns>A SqlJoin statement.</returns>
|
2022-04-22 12:37:20 +02:00
|
|
|
public static Sql<ISqlContext>.SqlJoinClause<ISqlContext> InnerJoin(this Sql<ISqlContext> sql, Sql<ISqlContext> nestedSelect, string? alias = null)
|
2022-04-21 14:47:27 +02:00
|
|
|
{
|
|
|
|
|
var join = $"({nestedSelect.SQL})";
|
|
|
|
|
if (alias is not null)
|
|
|
|
|
{
|
|
|
|
|
join += " " + sql.SqlContext.SqlSyntax.GetQuotedTableName(alias);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sql.InnerJoin(join);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
2017-11-15 19:44:32 +01:00
|
|
|
/// Appends a LEFT JOIN clause to the Sql statement.
|
2017-09-22 15:23:46 +02:00
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="alias">An optional alias for the joined table.</param>
|
|
|
|
|
/// <returns>A SqlJoin statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public static Sql<ISqlContext>.SqlJoinClause<ISqlContext> LeftJoin<TDto>(this Sql<ISqlContext> sql, string? alias = null)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
Type type = typeof(TDto);
|
2016-11-03 10:31:44 +01:00
|
|
|
var tableName = type.GetTableName();
|
2017-09-22 15:23:46 +02:00
|
|
|
var join = sql.SqlContext.SqlSyntax.GetQuotedTableName(tableName);
|
2022-06-02 08:18:31 +02:00
|
|
|
if (alias != null)
|
|
|
|
|
{
|
|
|
|
|
join += " " + sql.SqlContext.SqlSyntax.GetQuotedTableName(alias);
|
|
|
|
|
}
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
return sql.LeftJoin(join);
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
2017-11-15 19:44:32 +01:00
|
|
|
/// Appends a LEFT JOIN clause to the Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="nestedJoin">A nested join statement.</param>
|
|
|
|
|
/// <param name="alias">An optional alias for the joined table.</param>
|
|
|
|
|
/// <returns>A SqlJoin statement.</returns>
|
|
|
|
|
/// <remarks>Nested statement produces LEFT JOIN xxx JOIN yyy ON ... ON ...</remarks>
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
public static Sql<ISqlContext>.SqlJoinClause<ISqlContext> LeftJoin<TDto>(
|
|
|
|
|
this Sql<ISqlContext> sql,
|
|
|
|
|
Func<Sql<ISqlContext>, Sql<ISqlContext>> nestedJoin,
|
2022-03-16 14:39:28 +01:00
|
|
|
string? alias = null) =>
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
sql.SqlContext.SqlSyntax.LeftJoinWithNestedJoin<TDto>(sql, nestedJoin, alias);
|
2017-11-15 19:44:32 +01:00
|
|
|
|
2022-04-21 14:47:27 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an LEFT JOIN clause using a nested query.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The SQL statement.</param>
|
|
|
|
|
/// <param name="nestedSelect">The nested sql query.</param>
|
|
|
|
|
/// <param name="alias">An optional alias for the joined table.</param>
|
|
|
|
|
/// <returns>A SqlJoin statement.</returns>
|
2022-04-22 12:37:20 +02:00
|
|
|
public static Sql<ISqlContext>.SqlJoinClause<ISqlContext> LeftJoin(this Sql<ISqlContext> sql, Sql<ISqlContext> nestedSelect, string? alias = null)
|
2022-04-21 14:47:27 +02:00
|
|
|
{
|
|
|
|
|
var join = $"({nestedSelect.SQL})";
|
|
|
|
|
if (alias is not null)
|
|
|
|
|
{
|
|
|
|
|
join += " " + sql.SqlContext.SqlSyntax.GetQuotedTableName(alias);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 09:39:56 +02:00
|
|
|
sql.Append("LEFT JOIN " + join, nestedSelect.Arguments);
|
|
|
|
|
return new Sql<ISqlContext>.SqlJoinClause<ISqlContext>(sql);
|
2022-04-21 14:47:27 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-15 19:44:32 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends a RIGHT JOIN clause to the Sql statement.
|
2017-09-22 15:23:46 +02:00
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="alias">An optional alias for the joined table.</param>
|
|
|
|
|
/// <returns>A SqlJoin statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public static Sql<ISqlContext>.SqlJoinClause<ISqlContext> RightJoin<TDto>(this Sql<ISqlContext> sql, string? alias = null)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
Type type = typeof(TDto);
|
2016-11-03 10:31:44 +01:00
|
|
|
var tableName = type.GetTableName();
|
2017-09-22 15:23:46 +02:00
|
|
|
var join = sql.SqlContext.SqlSyntax.GetQuotedTableName(tableName);
|
2022-06-02 08:18:31 +02:00
|
|
|
if (alias != null)
|
|
|
|
|
{
|
|
|
|
|
join += " " + sql.SqlContext.SqlSyntax.GetQuotedTableName(alias);
|
|
|
|
|
}
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
return sql.RightJoin(join);
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an ON clause to a SqlJoin statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TLeft">The type of the left Dto.</typeparam>
|
|
|
|
|
/// <typeparam name="TRight">The type of the right Dto.</typeparam>
|
|
|
|
|
/// <param name="sqlJoin">The Sql join statement.</param>
|
|
|
|
|
/// <param name="leftField">An expression specifying the left field.</param>
|
|
|
|
|
/// <param name="rightField">An expression specifying the right field.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
public static Sql<ISqlContext> On<TLeft, TRight>(
|
|
|
|
|
this Sql<ISqlContext>.SqlJoinClause<ISqlContext> sqlJoin,
|
|
|
|
|
Expression<Func<TLeft, object?>> leftField,
|
|
|
|
|
Expression<Func<TRight, object?>> rightField)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2019-01-26 09:42:14 -05:00
|
|
|
// TODO: ugly - should define on SqlContext!
|
2017-09-22 15:23:46 +02:00
|
|
|
|
2017-09-22 18:48:58 +02:00
|
|
|
var xLeft = new Sql<ISqlContext>(sqlJoin.SqlContext).Columns(leftField);
|
|
|
|
|
var xRight = new Sql<ISqlContext>(sqlJoin.SqlContext).Columns(rightField);
|
2017-09-22 15:23:46 +02:00
|
|
|
return sqlJoin.On(xLeft + " = " + xRight);
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
//var sqlSyntax = clause.SqlContext.SqlSyntax;
|
2016-11-03 10:31:44 +01:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
//var leftType = typeof (TLeft);
|
|
|
|
|
//var rightType = typeof (TRight);
|
|
|
|
|
//var leftTableName = leftType.GetTableName();
|
|
|
|
|
//var rightTableName = rightType.GetTableName();
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
//var leftColumn = ExpressionHelper.FindProperty(leftMember) as PropertyInfo;
|
|
|
|
|
//var rightColumn = ExpressionHelper.FindProperty(rightMember) as PropertyInfo;
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
//var leftColumnName = leftColumn.GetColumnName();
|
|
|
|
|
//var rightColumnName = rightColumn.GetColumnName();
|
|
|
|
|
|
|
|
|
|
//string onClause = $"{sqlSyntax.GetQuotedTableName(leftTableName)}.{sqlSyntax.GetQuotedColumnName(leftColumnName)} = {sqlSyntax.GetQuotedTableName(rightTableName)}.{sqlSyntax.GetQuotedColumnName(rightColumnName)}";
|
|
|
|
|
//return clause.On(onClause);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an ON clause to a SqlJoin statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sqlJoin">The Sql join statement.</param>
|
|
|
|
|
/// <param name="on">A Sql fragment to use as the ON clause body.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2017-09-22 18:48:58 +02:00
|
|
|
public static Sql<ISqlContext> On(this Sql<ISqlContext>.SqlJoinClause<ISqlContext> sqlJoin, Func<Sql<ISqlContext>, Sql<ISqlContext>> on)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
2017-09-22 18:48:58 +02:00
|
|
|
var sql = new Sql<ISqlContext>(sqlJoin.SqlContext);
|
2017-09-22 15:23:46 +02:00
|
|
|
sql = on(sql);
|
2024-11-13 09:27:29 +01:00
|
|
|
var text = sql.SQL.Trim().TrimStart("WHERE").Trim();
|
2017-11-15 08:53:20 +01:00
|
|
|
return sqlJoin.On(text, sql.Arguments);
|
2017-09-22 15:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an ON clause to a SqlJoin statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto1">The type of Dto 1.</typeparam>
|
|
|
|
|
/// <typeparam name="TDto2">The type of Dto 2.</typeparam>
|
|
|
|
|
/// <param name="sqlJoin">The SqlJoin statement.</param>
|
|
|
|
|
/// <param name="predicate">A predicate to transform and use as the ON clause body.</param>
|
2017-11-10 11:27:12 +01:00
|
|
|
/// <param name="aliasLeft">An optional alias for Dto 1 table.</param>
|
|
|
|
|
/// <param name="aliasRight">An optional alias for Dto 2 table.</param>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public static Sql<ISqlContext> On<TDto1, TDto2>(this Sql<ISqlContext>.SqlJoinClause<ISqlContext> sqlJoin, Expression<Func<TDto1, TDto2, bool>> predicate, string? aliasLeft = null, string? aliasRight = null)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
2017-11-10 11:27:12 +01:00
|
|
|
var expresionist = new PocoToSqlExpressionVisitor<TDto1, TDto2>(sqlJoin.SqlContext, aliasLeft, aliasRight);
|
2017-09-22 15:23:46 +02:00
|
|
|
var onExpression = expresionist.Visit(predicate);
|
|
|
|
|
return sqlJoin.On(onExpression, expresionist.GetSqlParameters());
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
2018-09-25 10:55:06 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends an ON clause to a SqlJoin statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto1">The type of Dto 1.</typeparam>
|
|
|
|
|
/// <typeparam name="TDto2">The type of Dto 2.</typeparam>
|
|
|
|
|
/// <typeparam name="TDto3">The type of Dto 3.</typeparam>
|
|
|
|
|
/// <param name="sqlJoin">The SqlJoin statement.</param>
|
|
|
|
|
/// <param name="predicate">A predicate to transform and use as the ON clause body.</param>
|
|
|
|
|
/// <param name="aliasLeft">An optional alias for Dto 1 table.</param>
|
|
|
|
|
/// <param name="aliasRight">An optional alias for Dto 2 table.</param>
|
|
|
|
|
/// <param name="aliasOther">An optional alias for Dto 3 table.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public static Sql<ISqlContext> On<TDto1, TDto2, TDto3>(this Sql<ISqlContext>.SqlJoinClause<ISqlContext> sqlJoin, Expression<Func<TDto1, TDto2, TDto3, bool>> predicate, string? aliasLeft = null, string? aliasRight = null, string? aliasOther = null)
|
2018-09-25 10:55:06 +02:00
|
|
|
{
|
|
|
|
|
var expresionist = new PocoToSqlExpressionVisitor<TDto1, TDto2, TDto3>(sqlJoin.SqlContext, aliasLeft, aliasRight, aliasOther);
|
|
|
|
|
var onExpression = expresionist.Visit(predicate);
|
|
|
|
|
return sqlJoin.On(onExpression, expresionist.GetSqlParameters());
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Select
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Alters a Sql statement to return a maximum amount of rows.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <param name="count">The maximum number of rows to return.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2017-09-22 18:48:58 +02:00
|
|
|
public static Sql<ISqlContext> SelectTop(this Sql<ISqlContext> sql, int count)
|
2016-11-03 10:31:44 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
return sql.SqlContext.SqlSyntax.SelectTop(sql, count);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a SELECT COUNT(*) Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
2018-11-20 13:24:06 +01:00
|
|
|
/// <param name="alias">An optional alias.</param>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public static Sql<ISqlContext> SelectCount(this Sql<ISqlContext> sql, string? alias = null)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 13:24:06 +01:00
|
|
|
var text = "COUNT(*)";
|
2022-06-02 08:18:31 +02:00
|
|
|
if (alias != null)
|
|
|
|
|
{
|
|
|
|
|
text += " AS " + sql.SqlContext.SqlSyntax.GetQuotedColumnName(alias);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 13:24:06 +01:00
|
|
|
return sql.Select(text);
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-20 20:06:46 +02:00
|
|
|
/// <summary>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// Creates a SELECT COUNT Sql statement.
|
2017-09-20 20:06:46 +02:00
|
|
|
/// </summary>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <typeparam name="TDto">The type of the DTO to count.</typeparam>
|
2017-09-20 20:06:46 +02:00
|
|
|
/// <param name="sql">The origin sql.</param>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <param name="fields">Expressions indicating the columns to count.</param>
|
2017-09-20 20:06:46 +02:00
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
/// <remarks>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <para>If <paramref name="fields"/> is empty, all columns are counted.</para>
|
2017-09-20 20:06:46 +02:00
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> SelectCount<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[] fields)
|
2018-11-20 13:24:06 +01:00
|
|
|
=> sql.SelectCount(null, fields);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a SELECT COUNT Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the DTO to count.</typeparam>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
|
|
|
|
/// <param name="alias">An alias.</param>
|
|
|
|
|
/// <param name="fields">Expressions indicating the columns to count.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>If <paramref name="fields"/> is empty, all columns are counted.</para>
|
|
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> SelectCount<TDto>(this Sql<ISqlContext> sql, string? alias, params Expression<Func<TDto, object?>>[] fields)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ISqlSyntaxProvider sqlSyntax = sql.SqlContext.SqlSyntax;
|
2017-09-22 15:23:46 +02:00
|
|
|
var columns = fields.Length == 0
|
|
|
|
|
? sql.GetColumns<TDto>(withAlias: false)
|
2018-09-17 13:06:20 +02:00
|
|
|
: fields.Select(x => sqlSyntax.GetFieldName(x)).ToArray();
|
2018-11-20 13:24:06 +01:00
|
|
|
var text = "COUNT (" + string.Join(", ", columns) + ")";
|
2022-06-02 08:18:31 +02:00
|
|
|
if (alias != null)
|
|
|
|
|
{
|
|
|
|
|
text += " AS " + sql.SqlContext.SqlSyntax.GetQuotedColumnName(alias);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 13:24:06 +01:00
|
|
|
return sql.Select(text);
|
2017-09-20 20:06:46 +02:00
|
|
|
}
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a SELECT * Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2017-09-22 18:48:58 +02:00
|
|
|
public static Sql<ISqlContext> SelectAll(this Sql<ISqlContext> sql)
|
2017-09-20 20:06:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
return sql.Select("*");
|
2017-09-20 20:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a SELECT Sql statement.
|
|
|
|
|
/// </summary>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <typeparam name="TDto">The type of the DTO to select.</typeparam>
|
2017-09-20 20:06:46 +02:00
|
|
|
/// <param name="sql">The origin sql.</param>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <param name="fields">Expressions indicating the columns to select.</param>
|
2017-09-20 20:06:46 +02:00
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
/// <remarks>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <para>If <paramref name="fields"/> is empty, all columns are selected.</para>
|
2017-09-20 20:06:46 +02:00
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> Select<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[] fields)
|
2017-09-20 20:06:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
return sql.Select(sql.GetColumns(columnExpressions: fields));
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
2018-12-13 09:15:29 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a SELECT DISTINCT Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the DTO to select.</typeparam>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
|
|
|
|
/// <param name="fields">Expressions indicating the columns to select.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>If <paramref name="fields"/> is empty, all columns are selected.</para>
|
|
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> SelectDistinct<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[] fields)
|
2018-12-13 09:15:29 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-13 09:15:29 +01:00
|
|
|
var columns = sql.GetColumns(columnExpressions: fields);
|
|
|
|
|
sql.Append("SELECT DISTINCT " + string.Join(", ", columns));
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 09:39:56 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a SELECT DISTINCT Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the DTO to select.</typeparam>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
|
|
|
|
/// <param name="tableAlias">A table alias.</param>
|
|
|
|
|
/// <param name="fields">Expressions indicating the columns to select.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>If <paramref name="fields"/> is empty, all columns are selected.</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static Sql<ISqlContext> SelectDistinct<TDto>(this Sql<ISqlContext> sql, string tableAlias, params Expression<Func<TDto, object?>>[] fields)
|
|
|
|
|
{
|
|
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var columns = sql.GetColumns(tableAlias: tableAlias, columnExpressions: fields);
|
|
|
|
|
sql.Append("SELECT DISTINCT " + string.Join(", ", columns));
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 11:21:49 +01:00
|
|
|
public static Sql<ISqlContext> SelectDistinct(this Sql<ISqlContext> sql, params object[] columns)
|
|
|
|
|
{
|
|
|
|
|
sql.Append("SELECT DISTINCT " + string.Join(", ", columns));
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-13 09:15:29 +01:00
|
|
|
//this.Append("SELECT " + string.Join(", ", columns), new object[0]);
|
|
|
|
|
|
2017-11-15 08:53:20 +01:00
|
|
|
/// <summary>
|
2017-11-21 14:19:16 +01:00
|
|
|
/// Creates a SELECT Sql statement.
|
2017-11-15 08:53:20 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the DTO to select.</typeparam>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
2017-11-21 14:19:16 +01:00
|
|
|
/// <param name="tableAlias">A table alias.</param>
|
|
|
|
|
/// <param name="fields">Expressions indicating the columns to select.</param>
|
2017-11-15 08:53:20 +01:00
|
|
|
/// <returns>The Sql statement.</returns>
|
2017-11-21 14:19:16 +01:00
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>If <paramref name="fields"/> is empty, all columns are selected.</para>
|
|
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> Select<TDto>(this Sql<ISqlContext> sql, string tableAlias, params Expression<Func<TDto, object?>>[] fields)
|
2017-11-15 08:53:20 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 14:19:16 +01:00
|
|
|
return sql.Select(sql.GetColumns(tableAlias: tableAlias, columnExpressions: fields));
|
2017-11-15 08:53:20 +01:00
|
|
|
}
|
|
|
|
|
|
2018-10-04 13:06:07 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds columns to a SELECT Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
2018-10-18 14:16:54 +02:00
|
|
|
/// <param name="fields">Columns to select.</param>
|
2018-10-04 13:06:07 +02:00
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
public static Sql<ISqlContext> AndSelect(this Sql<ISqlContext> sql, params string[] fields)
|
|
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-04 13:06:07 +02:00
|
|
|
return sql.Append(", " + string.Join(", ", fields));
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-15 08:53:20 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds columns to a SELECT Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the DTO to select.</typeparam>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
|
|
|
|
/// <param name="fields">Expressions indicating the columns to select.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>If <paramref name="fields"/> is empty, all columns are selected.</para>
|
|
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> AndSelect<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[] fields)
|
2017-11-15 08:53:20 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-15 08:53:20 +01:00
|
|
|
return sql.Append(", " + string.Join(", ", sql.GetColumns(columnExpressions: fields)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-11-21 14:19:16 +01:00
|
|
|
/// Adds columns to a SELECT Sql statement.
|
2017-11-15 08:53:20 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the DTO to select.</typeparam>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
2017-11-21 14:19:16 +01:00
|
|
|
/// <param name="tableAlias">A table alias.</param>
|
|
|
|
|
/// <param name="fields">Expressions indicating the columns to select.</param>
|
2017-11-15 08:53:20 +01:00
|
|
|
/// <returns>The Sql statement.</returns>
|
2017-11-21 14:19:16 +01:00
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>If <paramref name="fields"/> is empty, all columns are selected.</para>
|
|
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> AndSelect<TDto>(this Sql<ISqlContext> sql, string tableAlias, params Expression<Func<TDto, object?>>[] fields)
|
2017-11-15 08:53:20 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 14:19:16 +01:00
|
|
|
return sql.Append(", " + string.Join(", ", sql.GetColumns(tableAlias: tableAlias, columnExpressions: fields)));
|
2017-11-15 08:53:20 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-20 13:24:06 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a COUNT(*) to a SELECT Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
|
|
|
|
/// <param name="alias">An optional alias.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public static Sql<ISqlContext> AndSelectCount(this Sql<ISqlContext> sql, string? alias = null)
|
2018-11-20 13:24:06 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 13:24:06 +01:00
|
|
|
var text = ", COUNT(*)";
|
2022-06-02 08:18:31 +02:00
|
|
|
if (alias != null)
|
|
|
|
|
{
|
|
|
|
|
text += " AS " + sql.SqlContext.SqlSyntax.GetQuotedColumnName(alias);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 13:24:06 +01:00
|
|
|
return sql.Append(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a COUNT to a SELECT Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the DTO to count.</typeparam>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
|
|
|
|
/// <param name="fields">Expressions indicating the columns to count.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>If <paramref name="fields"/> is empty, all columns are counted.</para>
|
|
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> AndSelectCount<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[] fields)
|
2018-11-20 13:24:06 +01:00
|
|
|
=> sql.AndSelectCount(null, fields);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a COUNT to a SELECT Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the DTO to count.</typeparam>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
|
|
|
|
/// <param name="alias">An alias.</param>
|
|
|
|
|
/// <param name="fields">Expressions indicating the columns to count.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>If <paramref name="fields"/> is empty, all columns are counted.</para>
|
|
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static Sql<ISqlContext> AndSelectCount<TDto>(this Sql<ISqlContext> sql, string? alias = null, params Expression<Func<TDto, object?>>[] fields)
|
2018-11-20 13:24:06 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ISqlSyntaxProvider sqlSyntax = sql.SqlContext.SqlSyntax;
|
2018-11-20 13:24:06 +01:00
|
|
|
var columns = fields.Length == 0
|
|
|
|
|
? sql.GetColumns<TDto>(withAlias: false)
|
|
|
|
|
: fields.Select(x => sqlSyntax.GetFieldName(x)).ToArray();
|
|
|
|
|
var text = ", COUNT (" + string.Join(", ", columns) + ")";
|
2022-06-02 08:18:31 +02:00
|
|
|
if (alias != null)
|
|
|
|
|
{
|
|
|
|
|
text += " AS " + sql.SqlContext.SqlSyntax.GetQuotedColumnName(alias);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 13:24:06 +01:00
|
|
|
return sql.Append(text);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a SELECT Sql statement with a referenced Dto.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto to select.</typeparam>
|
|
|
|
|
/// <param name="sql">The origin Sql.</param>
|
|
|
|
|
/// <param name="reference">An expression specifying the reference.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
2017-09-22 18:48:58 +02:00
|
|
|
public static Sql<ISqlContext> Select<TDto>(this Sql<ISqlContext> sql, Func<SqlRef<TDto>, SqlRef<TDto>> reference)
|
2017-09-20 20:06:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
2018-04-27 17:36:22 +02:00
|
|
|
|
2017-09-20 20:06:46 +02:00
|
|
|
sql.Select(sql.GetColumns<TDto>());
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
reference?.Invoke(new SqlRef<TDto>(sql, null));
|
2017-09-20 20:06:46 +02:00
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
/// <summary>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// Creates a SELECT Sql statement with a referenced Dto.
|
2016-04-12 15:11:07 +02:00
|
|
|
/// </summary>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <typeparam name="TDto">The type of the Dto to select.</typeparam>
|
|
|
|
|
/// <param name="sql">The origin Sql.</param>
|
2019-01-22 18:03:39 -05:00
|
|
|
/// <param name="reference">An expression specifying the reference.</param>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <param name="sqlexpr">An expression to apply to the Sql statement before adding the reference selection.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
|
|
|
|
/// <remarks>The <paramref name="sqlexpr"/> expression applies to the Sql statement before the reference selection
|
|
|
|
|
/// is added, so that it is possible to add (e.g. calculated) columns to the referencing Dto.</remarks>
|
2017-09-22 18:48:58 +02:00
|
|
|
public static Sql<ISqlContext> Select<TDto>(this Sql<ISqlContext> sql, Func<SqlRef<TDto>, SqlRef<TDto>> reference, Func<Sql<ISqlContext>, Sql<ISqlContext>> sqlexpr)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
2018-04-27 17:36:22 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
sql.Select(sql.GetColumns<TDto>());
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
sql = sqlexpr(sql);
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
reference(new SqlRef<TDto>(sql, null));
|
|
|
|
|
return sql;
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 09:01:32 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a SELECT CASE WHEN EXISTS query, which returns 1 if the sub query returns any results, and 0 if not.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The original SQL.</param>
|
|
|
|
|
/// <param name="nestedSelect">The nested select to run the query against.</param>
|
|
|
|
|
/// <returns>The updated Sql statement.</returns>
|
|
|
|
|
public static Sql<ISqlContext> SelectAnyIfExists(this Sql<ISqlContext> sql, Sql<ISqlContext> nestedSelect)
|
|
|
|
|
{
|
|
|
|
|
sql.Append("SELECT CASE WHEN EXISTS (");
|
|
|
|
|
sql.Append(nestedSelect);
|
|
|
|
|
sql.Append(")");
|
|
|
|
|
sql.Append("THEN 1 ELSE 0 END");
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-20 20:06:46 +02:00
|
|
|
/// <summary>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// Represents a Dto reference expression.
|
2017-09-20 20:06:46 +02:00
|
|
|
/// </summary>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <typeparam name="TDto">The type of the referencing Dto.</typeparam>
|
|
|
|
|
public class SqlRef<TDto>
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new Dto reference expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The original Sql expression.</param>
|
|
|
|
|
/// <param name="prefix">The current Dtos prefix.</param>
|
2022-02-24 09:24:56 +01:00
|
|
|
public SqlRef(Sql<ISqlContext> sql, string? prefix)
|
2016-04-12 15:11:07 +02:00
|
|
|
{
|
|
|
|
|
Sql = sql;
|
|
|
|
|
Prefix = prefix;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the original Sql expression.
|
|
|
|
|
/// </summary>
|
2017-09-22 18:48:58 +02:00
|
|
|
public Sql<ISqlContext> Sql { get; }
|
2017-09-22 15:23:46 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the current Dtos prefix.
|
|
|
|
|
/// </summary>
|
2022-02-24 09:24:56 +01:00
|
|
|
public string? Prefix { get; }
|
2016-04-12 15:11:07 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Appends fields for a referenced Dto.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TRefDto">The type of the referenced Dto.</typeparam>
|
|
|
|
|
/// <param name="field">An expression specifying the referencing field.</param>
|
|
|
|
|
/// <param name="reference">An optional expression representing a nested reference selection.</param>
|
|
|
|
|
/// <returns>A SqlRef statement.</returns>
|
2022-02-18 14:32:51 +01:00
|
|
|
public SqlRef<TDto> Select<TRefDto>(Expression<Func<TDto, TRefDto>> field, Func<SqlRef<TRefDto>, SqlRef<TRefDto>>? reference = null)
|
2017-09-22 15:23:46 +02:00
|
|
|
=> Select(field, null, reference);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends fields for a referenced Dto.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TRefDto">The type of the referenced Dto.</typeparam>
|
|
|
|
|
/// <param name="field">An expression specifying the referencing field.</param>
|
|
|
|
|
/// <param name="tableAlias">The referenced Dto table alias.</param>
|
|
|
|
|
/// <param name="reference">An optional expression representing a nested reference selection.</param>
|
|
|
|
|
/// <returns>A SqlRef statement.</returns>
|
2022-02-24 09:24:56 +01:00
|
|
|
public SqlRef<TDto> Select<TRefDto>(Expression<Func<TDto, TRefDto>> field, string? tableAlias, Func<SqlRef<TRefDto>, SqlRef<TRefDto>>? reference = null)
|
2017-09-20 20:06:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
PropertyInfo? property = field == null ? null : ExpressionHelper.FindProperty(field).Item1 as PropertyInfo;
|
2017-09-22 15:23:46 +02:00
|
|
|
return Select(property, tableAlias, reference);
|
2017-09-20 20:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Selects referenced DTOs.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TRefDto">The type of the referenced DTOs.</typeparam>
|
|
|
|
|
/// <param name="field">An expression specifying the referencing field.</param>
|
|
|
|
|
/// <param name="reference">An optional expression representing a nested reference selection.</param>
|
|
|
|
|
/// <returns>A referenced DTO expression.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>The referencing property has to be a <c>List{<typeparamref name="TRefDto"/>}</c>.</para>
|
|
|
|
|
/// </remarks>
|
2022-02-18 14:32:51 +01:00
|
|
|
public SqlRef<TDto> Select<TRefDto>(Expression<Func<TDto, List<TRefDto>>> field, Func<SqlRef<TRefDto>, SqlRef<TRefDto>>? reference = null)
|
2017-09-22 15:23:46 +02:00
|
|
|
=> Select(field, null, reference);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Selects referenced DTOs.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TRefDto">The type of the referenced DTOs.</typeparam>
|
|
|
|
|
/// <param name="field">An expression specifying the referencing field.</param>
|
|
|
|
|
/// <param name="tableAlias">The DTO table alias.</param>
|
|
|
|
|
/// <param name="reference">An optional expression representing a nested reference selection.</param>
|
|
|
|
|
/// <returns>A referenced DTO expression.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>The referencing property has to be a <c>List{<typeparamref name="TRefDto"/>}</c>.</para>
|
|
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public SqlRef<TDto> Select<TRefDto>(Expression<Func<TDto, List<TRefDto>>> field, string? tableAlias, Func<SqlRef<TRefDto>, SqlRef<TRefDto>>? reference = null)
|
2017-09-20 20:06:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
PropertyInfo? property = field == null ? null : ExpressionHelper.FindProperty(field).Item1 as PropertyInfo;
|
2017-09-22 15:23:46 +02:00
|
|
|
return Select(property, tableAlias, reference);
|
2017-09-20 20:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
private SqlRef<TDto> Select<TRefDto>(PropertyInfo? propertyInfo, string? tableAlias, Func<SqlRef<TRefDto>, SqlRef<TRefDto>>? nested = null)
|
2017-09-20 20:06:46 +02:00
|
|
|
{
|
2017-09-22 15:23:46 +02:00
|
|
|
var referenceName = propertyInfo?.Name ?? typeof (TDto).Name;
|
2022-06-02 08:18:31 +02:00
|
|
|
if (Prefix != null)
|
|
|
|
|
{
|
|
|
|
|
referenceName = Prefix + PocoData.Separator + referenceName;
|
|
|
|
|
}
|
2017-09-20 20:06:46 +02:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
var columns = Sql.GetColumns<TRefDto>(tableAlias, referenceName);
|
2017-09-20 20:06:46 +02:00
|
|
|
Sql.Append(", " + string.Join(", ", columns));
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
nested?.Invoke(new SqlRef<TRefDto>(Sql, referenceName));
|
2017-09-20 20:06:46 +02:00
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// Gets fields for a Dto.
|
2017-09-20 20:06:46 +02:00
|
|
|
/// </summary>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
2017-09-20 20:06:46 +02:00
|
|
|
/// <param name="sql">The origin sql.</param>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <param name="fields">Expressions specifying the fields.</param>
|
|
|
|
|
/// <returns>The comma-separated list of fields.</returns>
|
2017-09-20 20:06:46 +02:00
|
|
|
/// <remarks>
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <para>If <paramref name="fields"/> is empty, all fields are selected.</para>
|
2017-09-20 20:06:46 +02:00
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static string Columns<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[] fields)
|
2017-09-20 20:06:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
return string.Join(", ", sql.GetColumns(columnExpressions: fields, withAlias: false));
|
|
|
|
|
}
|
2017-09-20 20:06:46 +02:00
|
|
|
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets fields for a Dto.
|
|
|
|
|
/// </summary>
|
2022-03-17 09:14:12 +01:00
|
|
|
public static string ColumnsForInsert<TDto>(this Sql<ISqlContext> sql, params Expression<Func<TDto, object?>>[]? fields)
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
return string.Join(", ", sql.GetColumns(columnExpressions: fields, withAlias: false, forInsert: true));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets fields for a Dto.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto">The type of the Dto.</typeparam>
|
|
|
|
|
/// <param name="sql">The origin sql.</param>
|
|
|
|
|
/// <param name="alias">The Dto table alias.</param>
|
|
|
|
|
/// <param name="fields">Expressions specifying the fields.</param>
|
|
|
|
|
/// <returns>The comma-separated list of fields.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>If <paramref name="fields"/> is empty, all fields are selected.</para>
|
|
|
|
|
/// </remarks>
|
2022-02-24 09:24:56 +01:00
|
|
|
public static string Columns<TDto>(this Sql<ISqlContext> sql, string alias, params Expression<Func<TDto, object?>>[] fields)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (sql == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
return string.Join(", ", sql.GetColumns(columnExpressions: fields, withAlias: false, tableAlias: alias));
|
2017-09-20 20:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2017-11-10 11:27:12 +01:00
|
|
|
#region Delete
|
|
|
|
|
|
|
|
|
|
public static Sql<ISqlContext> Delete(this Sql<ISqlContext> sql)
|
|
|
|
|
{
|
|
|
|
|
sql.Append("DELETE");
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Sql<ISqlContext> Delete<TDto>(this Sql<ISqlContext> sql)
|
|
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
Type type = typeof(TDto);
|
2017-11-10 11:27:12 +01:00
|
|
|
var tableName = type.GetTableName();
|
|
|
|
|
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
// FROM optional SQL server, but not elsewhere.
|
|
|
|
|
sql.Append($"DELETE FROM {sql.SqlContext.SqlSyntax.GetQuotedTableName(tableName)}");
|
2017-11-10 11:27:12 +01:00
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Update
|
|
|
|
|
|
|
|
|
|
public static Sql<ISqlContext> Update(this Sql<ISqlContext> sql)
|
|
|
|
|
{
|
|
|
|
|
sql.Append("UPDATE");
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Sql<ISqlContext> Update<TDto>(this Sql<ISqlContext> sql)
|
|
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
Type type = typeof(TDto);
|
2017-11-10 11:27:12 +01:00
|
|
|
var tableName = type.GetTableName();
|
|
|
|
|
|
|
|
|
|
sql.Append($"UPDATE {sql.SqlContext.SqlSyntax.GetQuotedTableName(tableName)}");
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Sql<ISqlContext> Update<TDto>(this Sql<ISqlContext> sql, Func<SqlUpd<TDto>, SqlUpd<TDto>> updates)
|
|
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
Type type = typeof(TDto);
|
2017-11-10 11:27:12 +01:00
|
|
|
var tableName = type.GetTableName();
|
|
|
|
|
|
|
|
|
|
sql.Append($"UPDATE {sql.SqlContext.SqlSyntax.GetQuotedTableName(tableName)} SET");
|
|
|
|
|
|
|
|
|
|
var u = new SqlUpd<TDto>(sql.SqlContext);
|
|
|
|
|
u = updates(u);
|
2018-05-18 17:20:18 +02:00
|
|
|
var first = true;
|
2022-06-02 08:18:31 +02:00
|
|
|
foreach (Tuple<string, object?> setExpression in u.SetExpressions)
|
2017-11-10 11:27:12 +01:00
|
|
|
{
|
2018-05-18 17:20:18 +02:00
|
|
|
switch (setExpression.Item2)
|
|
|
|
|
{
|
|
|
|
|
case null:
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
sql.Append((first ? string.Empty : ",") + " " + setExpression.Item1 + "=NULL");
|
2018-05-18 17:20:18 +02:00
|
|
|
break;
|
|
|
|
|
case string s when s == string.Empty:
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
sql.Append((first ? string.Empty : ",") + " " + setExpression.Item1 + "=''");
|
2018-05-18 17:20:18 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
sql.Append((first ? string.Empty : ",") + " " + setExpression.Item1 + "=@0", setExpression.Item2);
|
2018-05-18 17:20:18 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
first = false;
|
2017-11-10 11:27:12 +01:00
|
|
|
}
|
|
|
|
|
|
2018-05-18 17:20:18 +02:00
|
|
|
if (!first)
|
2022-06-02 08:18:31 +02:00
|
|
|
{
|
2018-05-18 17:20:18 +02:00
|
|
|
sql.Append(" ");
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2018-05-18 17:20:18 +02:00
|
|
|
|
2017-11-10 11:27:12 +01:00
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SqlUpd<TDto>
|
|
|
|
|
{
|
|
|
|
|
private readonly ISqlContext _sqlContext;
|
|
|
|
|
|
|
|
|
|
public SqlUpd(ISqlContext sqlContext)
|
|
|
|
|
{
|
|
|
|
|
_sqlContext = sqlContext;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
public SqlUpd<TDto> Set(Expression<Func<TDto, object?>> fieldSelector, object? value)
|
2017-11-10 11:27:12 +01:00
|
|
|
{
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
var fieldName = _sqlContext.SqlSyntax.GetFieldNameForUpdate(fieldSelector);
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
SetExpressions.Add(new Tuple<string, object?>(fieldName, value));
|
2017-11-10 11:27:12 +01:00
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
public List<Tuple<string, object?>> SetExpressions { get; } = [];
|
2017-11-10 11:27:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2018-05-29 18:30:37 +02:00
|
|
|
#region Hints
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Appends the relevant ForUpdate hint.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql">The Sql statement.</param>
|
|
|
|
|
/// <returns>The Sql statement.</returns>
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
/// <remarks>
|
|
|
|
|
/// NOTE: This method will not work for all queries, only simple ones!
|
|
|
|
|
/// </remarks>
|
2018-05-29 18:30:37 +02:00
|
|
|
public static Sql<ISqlContext> ForUpdate(this Sql<ISqlContext> sql)
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
=> sql.SqlContext.SqlSyntax.InsertForUpdateHint(sql);
|
2018-05-29 18:30:37 +02:00
|
|
|
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
public static Sql<ISqlContext> AppendForUpdateHint(this Sql<ISqlContext> sql)
|
|
|
|
|
=> sql.SqlContext.SqlSyntax.AppendForUpdateHint(sql);
|
2018-05-29 18:30:37 +02:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2019-02-20 15:01:32 +01:00
|
|
|
#region Aliasing
|
|
|
|
|
|
|
|
|
|
internal static string GetAliasedField(this Sql<ISqlContext> sql, string field)
|
|
|
|
|
{
|
|
|
|
|
// get alias, if aliased
|
|
|
|
|
//
|
|
|
|
|
// regex looks for pattern "([\w+].[\w+]) AS ([\w+])" ie "(field) AS (alias)"
|
|
|
|
|
// and, if found & a group's field matches the field name, returns the alias
|
|
|
|
|
//
|
|
|
|
|
// so... if query contains "[umbracoNode].[nodeId] AS [umbracoNode__nodeId]"
|
|
|
|
|
// then GetAliased for "[umbracoNode].[nodeId]" returns "[umbracoNode__nodeId]"
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
MatchCollection matches = sql.SqlContext.SqlSyntax.AliasRegex.Matches(sql.SQL);
|
|
|
|
|
Match? match = matches.Cast<Match>().FirstOrDefault(m => m.Groups[1].Value.InvariantEquals(field));
|
2019-02-20 15:01:32 +01:00
|
|
|
return match == null ? field : match.Groups[2].Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2017-09-20 20:06:46 +02:00
|
|
|
#region Utilities
|
|
|
|
|
|
2025-07-07 14:53:42 +02:00
|
|
|
public static Sql<ISqlContext> AppendSubQuery(this Sql<ISqlContext> sql, Sql<ISqlContext> subQuery, string alias)
|
|
|
|
|
{
|
|
|
|
|
// Append the subquery as a derived table with an alias
|
|
|
|
|
sql.Append("(").Append(subQuery.SQL, subQuery.Arguments).Append($") AS {alias}");
|
|
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 14:39:28 +01:00
|
|
|
private static string[] GetColumns<TDto>(this Sql<ISqlContext> sql, string? tableAlias = null, string? referenceName = null, Expression<Func<TDto, object?>>[]? columnExpressions = null, bool withAlias = true, bool forInsert = false)
|
2017-09-20 20:06:46 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
PocoData? pd = sql.SqlContext.PocoDataFactory.ForType(typeof (TDto));
|
2017-09-22 15:23:46 +02:00
|
|
|
var tableName = tableAlias ?? pd.TableInfo.TableName;
|
2018-10-04 14:15:54 +02:00
|
|
|
var queryColumns = pd.QueryColumns.ToList();
|
2017-09-22 15:23:46 +02:00
|
|
|
|
2022-02-18 14:32:51 +01:00
|
|
|
Dictionary<string, string>? aliases = null;
|
2017-11-21 14:19:16 +01:00
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
if (columnExpressions != null && columnExpressions.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var names = columnExpressions.Select(x =>
|
|
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
(MemberInfo member, var alias) = ExpressionHelper.FindProperty(x);
|
2017-11-21 14:19:16 +01:00
|
|
|
var field = member as PropertyInfo;
|
2022-02-18 14:32:51 +01:00
|
|
|
var fieldName = field?.GetColumnName();
|
|
|
|
|
if (alias != null && fieldName is not null)
|
2017-11-21 14:19:16 +01:00
|
|
|
{
|
Resolved more warnings, and marked more warning types as errors (#16991)
* Fix warnings SA1111, SA1028, SA1500, IDE1270 in Umbraco.Web.Website, and updated rules.
* Remove warnings: IDE0270: Null check can be simplified
* More SqlServer project warnings resolved
* CS0105 namespace appeared already
* Suppress warning until implementation:
#pragma warning disable CS0162 // Unreachable code detected
#pragma warning disable CS0618 // Type or member is obsolete
CS0162 remove unreachable code
SA1028 remove trailing whitespace
SA1106 no empty statements
CS1570 malformed XML
CS1572 corrected xml parameter
CS1573 param tag added
IDE0007 var not explicit
IDE0008 explicit not var
IDE0057 simplify substring
IDE0074 compound assignment
CA1825 array.empty
Down to 3479 warnings
* - SA1116, SA117 params on same line
- IDE0057 substring simplified
Specific warnings for Umbraco.Tests.Benchmarks
* Fixed IDE0074 compound assignment and added specific warnings for Umbraco.Tests.Common
* Specific warnings for Umbraco.Tests.Integration and Umbraco.Tests.Common
Fixed:
- SA1111, SA1116, SA117 params and line formatting (not all as there are many)
- SA1122 string.Empty
- IDE0057 simplify substring
- IDE0044,IDE0044 make field readonly
- IDE1006 naming rule violation (add _)
- SA1111 closing parenthesis on line of last parameter
- SA1649 filename match type name
- SA1312,SA1306 lowercase variable and field names
* Fixed various warnings where they are more straight-forward, including:
- SA1649 file name match type name
- SA111 parenthesis on line of last parameter
- IDE0028 simplify collection initializer
- SA1306 lower-case letter field
- IDE044 readonly field
- SA1122 string.Empty
- SA1116 params same line
- IDE1006 upper casing
- IDE0041 simplify null check
Updated the following projects to only list their remaining specific warning codes:
- Umbraco.Tests.UnitTests
Typo in `Umbraco.Web.Website` project
* Reverted test change
* Now 1556 warnings.
Fixed various warnings where they are more straight-forward, including:
- SA1111/SA1116/SA1119 parenthesis
- SA1117 params
- SA1312 lowercase variable
- SA1121 built-in type
- SA1500/SA1513/SA1503 formatting braces
- SA1400 declare access modifier
- SA1122 string.Empty
- SA1310 no underscore
- IDE0049 name simplified
- IDE0057 simplify substring
- IDE0074 compound assignment
- IDE0032 use auto-property
- IDE0037 simplify member name
- IDE0008 explicit type not var
- IDE0016/IDE0270/IDE0041 simplify null checks
- IDE0048/SA1407 clarity in arithmetic
- IDE1006 correct param names
- IDE0042 deconstruct variable
- IDE0044 readonly
- IDE0018 inline variable declarations
- IDE0074/IDE0054 compound assignment
- IDE1006 naming
- CS1573 param XML
- CS0168 unused variable
Comment formatting in project files for consistency.
Updated all projects to only list remaining specific warning codes as warnings instead of errors (errors is now default).
* Type not var, and more warning exceptions
* Tweaked merge issue, readded comment about rollback
* Readded comment re rollback.
* Readded comments
* Comment tweak
* Comment tweak
2024-09-24 12:56:28 +01:00
|
|
|
aliases ??= new Dictionary<string, string>();
|
2017-11-21 14:19:16 +01:00
|
|
|
aliases[fieldName] = alias;
|
|
|
|
|
}
|
2017-09-22 15:23:46 +02:00
|
|
|
return fieldName;
|
|
|
|
|
}).ToArray();
|
|
|
|
|
|
2018-10-04 14:15:54 +02:00
|
|
|
//only get the columns that exist in the selected names
|
|
|
|
|
queryColumns = queryColumns.Where(x => names.Contains(x.Key)).ToList();
|
|
|
|
|
|
|
|
|
|
//ensure the order of the columns in the expressions is the order in the result
|
|
|
|
|
queryColumns.Sort((a, b) => names.IndexOf(a.Key).CompareTo(names.IndexOf(b.Key)));
|
2017-09-22 15:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
string? GetAlias(PocoColumn column)
|
2017-11-21 14:19:16 +01:00
|
|
|
{
|
|
|
|
|
if (aliases != null && aliases.TryGetValue(column.ColumnName, out var alias))
|
2022-06-02 08:18:31 +02:00
|
|
|
{
|
2017-11-21 14:19:16 +01:00
|
|
|
return alias;
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2017-11-21 14:19:16 +01:00
|
|
|
|
|
|
|
|
return withAlias ? (string.IsNullOrEmpty(column.ColumnAlias) ? column.MemberInfoKey : column.ColumnAlias) : null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-30 10:16:21 +02:00
|
|
|
return queryColumns
|
2022-03-17 09:14:12 +01:00
|
|
|
.Select(x => sql.SqlContext.SqlSyntax.GetColumn(sql.SqlContext.DatabaseType, tableName, x.Value.ColumnName, GetAlias(x.Value)!, referenceName, forInsert: forInsert))
|
2018-03-30 10:16:21 +02:00
|
|
|
.ToArray();
|
2017-09-20 20:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
v10 SQLite support + distributed locking abstractions (#11922)
* Created Persistence.SQLite project skeleton.
* SQLite database initialization
* Various changes and hacks to make things work.
* WIP integration tests
* Fix thread safety tests
* Fix tests that relied on tie breaker sorting.
Spent a fair amount of time looking for a less lazy fix but gave up.
* Convert right join to left join ContentTypeRepository.PerformGetByQuery
SQLite doesn't support right join
* Fix test Can_Generate_Delete_SubQuery_Statement
Worth noting that NPoco.DatabaseTypes.SQLiteDatabaseType doesn't override
EscapeSqlIdentifier so NPoco will escape with [].
SQLite docs say > "A keyword enclosed in square brackets is an identifier.
This is not standard SQL.
This quoting mechanism is used by MS Access and SQL Server and is
included in SQLite for compatibility."
Also could have updated SqliteSyntaxProvider to match npoco but
decided against it.
* Fixes for paginated custom order by
* Fix tests broken by lack of unique indexes.
* Fix SqlServerTableByTableTest tests.
These tests didn't actually do anything as the tables already exist so schema creator just returned.
Did however point out that the default implementation for DoesTableExist just returns false so added a default naive implementation.
* Fix ValidateLoginSession - SelectTop must come later
* dry up database cleanup
* Fix up db migration tests.
We can't drop pk in sqlite without recreating table.
Test looks to be testing that add column works as intended which we can test.
* Prevent schema creation errors.
* SQLite ignore lock tests, WAL back on.
* Fix package schema tests
* Fix NPocoFetchTests - case sensitivity not under test
* Fix AdvancedMigrationTests (where possible)
Migrations probably need a good look later.
Maybe nuke old migrations and only support moving to v10 from v9.
If we do that can do some cleanup.
* Cleanup test database configuration
* Run integration tests against SQLite on build agent.
* Drop MS.Data.SQLite
System.Data.SQLite was quicker to roll out due to more CLR type mapping
* YAML
* Skip Umbraco.Tests.Integration.SqlCe
* Drop SqlServerTableByTable tests.
Until this week they did nothing anyway as they with NewSchemaPerTest
so the tests all passed as CreateTable was no op (already exists).
Also all of the tables are created in an empty database by SchemaValidationTest.cs
DatabaseSchemaCreation_Produces_DatabaseSchemaResult_With_Zero_Errors
* Might aswell run against macOS also.
* Copy azure pipelines task header layout
* Delete SQLCe projects
* Remove SQL CE specific code.
* Remove SQL CE NuSpec, template params, build script setup
* Delete umbraco-netcore-only.sln
* Add SkipTests solution configuration and use for codeql
* Remove reference to deleted nuspec file.
* Refactor ConnectionStrings WRT DataDirectory placeholder & ProviderName.
At this point you can try out SQLite support by setting the following
in appsettings.json and then completing the install process.
"ConnectionStrings": {
"umbracoDbDSN": "Data Source=|DataDirectory|/umbraco.sqlite",
"umbracoDbDSN_ProviderName": "System.Data.SQLite"
},
Not currently possible via installer UI without provider name pre-set in
configuration.
* Switch to Microsoft.Data.Sqlite
Some gross hacks but will be good to find out if this works
with apple silicon.
* Enable selection of SQLite via installer UI (also quick install)
* Remove SqlServerDbProviderFactoryCreator to cleanup a TODO
* Move SQL Server support to its own class library
* Add persistence dependencies to Umbraco.CMS metapackage
* Bugfix packages delete query
Created invalid query for SQLite.
* Try out cypress tests Linux + SQLite
* Prevent cypress test artifact upload failure on attempt 2+
* LocalDb bugfixes
* Drop redundant enum
* Move SqlClient constant
* Misc whitespace
* Remove IsSqlCe extension (TODO: drop non 9->10 migrations later).
* Umbraco.Persistence.* -> Umbraco.Cms.Persistence.*
* Display quick install defaults and per provider default database name.
* Misc remove old comment
* little re-arrange
* Remove almost all usages of IsSqlite extension.
* visual adjustments
* Custom Database Configuration is last step and should then say Install.
* use text instead of disabled inputs
* move legend, rename to Install
* Update SqlMainDomLock to work without distributed locks.
* Added IDistributedLockingMechanism interface and in memory impl.
* Drop locking from ISqlSyntaxProvider & wire up scope to abstraction.
* Added SqlServerDistributedLockingMechanism
* Move distributed locking interfaces and exceptions to Core + xmldocs.
* Fix tests, Misc cleanup, Add SQL distributed locking integration tests
* Provide mechanism to specify DistributedLockingMechanism in config
(even if added by composer)
* Nomplementation -> NoImplementation
* Fix misleading comment
* Integration tests use SqlServerDistributedLockingMechanism when possible
* Handle up-gradable locks SqlServerDistributedLockingMechanism.
TODO: InMemoryDistributedLockingMechanism.
Note: Nuked SqlServerDistributedLockingMechanismTests, will still sleep
at night.
Is covered by Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.LockTests
* Make tests pass for InMemoryDistributedLockingMechanism, pretty hacky.
* Tweak constraints on WithCollectionBuilder so i can drop bad constructor
* Added SqliteDistributedLockingMechanism
* Dropped InMemoryDistributedMechanism + magic
InMemoryDistributedMechanism was pretty rubbish and now we have
a decent implementation for SQLite as we no longer block readers
see 8d1f42b.
Also drop the CollectionBuilder setup, instead do the same as we do
for syntax providers etc, it's more automagical so we never require an
explicit selection although we are allowing for it.
However keeping the optional IUmbracoBuilder constructor param for
CollectionBuilders as it's extremely useful.
* Fix quick install "" database name.
* Hide Database Configuration section when a connection string is pre-set.
Doesn't seem worth it to extract db name from connection string.
* Ensure wal test 2+
* Fix logging inconsistencies.
* Ensure in transaction when obtaining locks + no-op the SQLite read lock.
There's no point in running the query just to make a single test pass.
* Fix installer database display names
* Allow SQLite shared cache without losing deferred transactions
* Opt into shared cache for new SQLite databases + fix filename
* Fix misc inconsistency in .gitignore
* Prefer our interceptor interface
* Restore DEBUG_DATABASES code OnConnectionOpened in case it's used.
* Back to private cache.
* Added retry strategy for SQLite + refactor out SQL server specific stuff
* Fix SQL server tests.
* Misc - Orphaned comment, incorrect casing.
* InMemory SQLite test database & turn shared cache back on everywhere.
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
2022-03-11 16:14:20 +00:00
|
|
|
public static string GetTableName(this Type type)
|
2016-11-03 10:31:44 +01:00
|
|
|
{
|
2019-01-26 09:42:14 -05:00
|
|
|
// TODO: returning string.Empty for now
|
2016-11-03 10:31:44 +01:00
|
|
|
// BUT the code bits that calls this method cannot deal with string.Empty so we
|
|
|
|
|
// should either throw, or fix these code bits...
|
2022-06-02 08:18:31 +02:00
|
|
|
TableNameAttribute? attr = type.FirstAttribute<TableNameAttribute>();
|
2016-11-03 10:31:44 +01:00
|
|
|
return string.IsNullOrWhiteSpace(attr?.Value) ? string.Empty : attr.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetColumnName(this PropertyInfo column)
|
|
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
ColumnAttribute? attr = column.FirstAttribute<ColumnAttribute>();
|
2016-11-03 10:31:44 +01:00
|
|
|
return string.IsNullOrWhiteSpace(attr?.Name) ? column.Name : attr.Name;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 08:11:23 +01:00
|
|
|
public static string ToText(this Sql sql)
|
2018-07-04 14:48:44 +02:00
|
|
|
{
|
|
|
|
|
var text = new StringBuilder();
|
|
|
|
|
sql.ToText(text);
|
|
|
|
|
return text.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 08:11:23 +01:00
|
|
|
public static void ToText(this Sql sql, StringBuilder text)
|
2018-07-04 14:48:44 +02:00
|
|
|
{
|
|
|
|
|
ToText(sql.SQL, sql.Arguments, text);
|
|
|
|
|
}
|
2018-12-06 13:12:07 +11:00
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
public static void ToText(string? sql, object[]? arguments, StringBuilder text)
|
2017-09-22 15:23:46 +02:00
|
|
|
{
|
2018-07-04 14:48:44 +02:00
|
|
|
text.AppendLine(sql);
|
|
|
|
|
|
2018-12-04 18:50:21 +01:00
|
|
|
if (arguments == null || arguments.Length == 0)
|
2022-06-02 08:18:31 +02:00
|
|
|
{
|
2018-07-04 14:48:44 +02:00
|
|
|
return;
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2018-07-04 14:48:44 +02:00
|
|
|
|
|
|
|
|
text.Append(" --");
|
|
|
|
|
|
2017-09-22 15:23:46 +02:00
|
|
|
var i = 0;
|
2018-07-04 14:48:44 +02:00
|
|
|
foreach (var arg in arguments)
|
|
|
|
|
{
|
|
|
|
|
text.Append(" @");
|
|
|
|
|
text.Append(i++);
|
|
|
|
|
text.Append(":");
|
|
|
|
|
text.Append(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
text.AppendLine();
|
2017-09-22 15:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
#endregion
|
2016-04-12 15:11:07 +02:00
|
|
|
}
|
|
|
|
|
}
|