2019-10-15 00:04:41 +11:00
using System.Data ;
2022-02-24 09:24:56 +01:00
using System.Diagnostics.CodeAnalysis ;
2022-06-02 08:18:31 +02:00
using System.Linq.Expressions ;
2018-07-17 17:20:40 +02:00
using System.Text.RegularExpressions ;
2018-06-29 19:52:40 +02:00
using NPoco ;
2022-01-13 23:46:21 +00:00
using Umbraco.Cms.Core.Persistence ;
2021-02-12 13:36:50 +01:00
using Umbraco.Cms.Infrastructure.Persistence.DatabaseAnnotations ;
using Umbraco.Cms.Infrastructure.Persistence.DatabaseModelDefinitions ;
2018-06-29 19:52:40 +02:00
2022-06-02 08:18:31 +02:00
namespace Umbraco.Cms.Infrastructure.Persistence.SqlSyntax ;
/// <summary>
/// Defines an SqlSyntaxProvider
/// </summary>
public interface ISqlSyntaxProvider
2018-06-29 19:52:40 +02:00
{
2023-10-31 11:38:24 +01:00
string Length { get ; }
string Substring { get ; }
2022-06-02 08:18:31 +02:00
string ProviderName { get ; }
string CreateTable { get ; }
string DropTable { get ; }
string AddColumn { get ; }
string DropColumn { get ; }
string AlterColumn { get ; }
string RenameColumn { get ; }
string RenameTable { get ; }
string CreateSchema { get ; }
string AlterSchema { get ; }
string DropSchema { get ; }
string CreateIndex { get ; }
string DropIndex { get ; }
string InsertData { get ; }
string UpdateData { get ; }
string DeleteData { get ; }
string TruncateTable { get ; }
string CreateConstraint { get ; }
string DeleteConstraint { get ; }
string DeleteDefaultConstraint { get ; }
/// <summary>
/// Gets a regex matching aliased fields.
/// </summary>
/// <remarks>
/// <para>Matches "(table.column) AS (alias)" where table, column and alias are properly escaped.</para>
/// </remarks>
Regex AliasRegex { get ; }
string ConvertIntegerToOrderableString { get ; }
string ConvertDateToOrderableString { get ; }
string ConvertDecimalToOrderableString { get ; }
2025-08-07 14:22:19 +02:00
string ConvertUniqueIdentifierToString = > throw new NotImplementedException ( ) ;
2022-06-02 08:18:31 +02:00
/// <summary>
/// Returns the default isolation level for the database
/// </summary>
IsolationLevel DefaultIsolationLevel { get ; }
string DbProvider { get ; }
IDictionary < Type , IScalarMapper > ? ScalarMappers = > null ;
DatabaseType GetUpdatedDatabaseType ( DatabaseType current , string? connectionString ) = >
current ; // Default implementation.
string EscapeString ( string val ) ;
string GetWildcardPlaceholder ( ) ;
2025-09-15 12:25:11 +02:00
/// <summary>
/// This ensures that GetWildcardPlaceholder() character is surronded by '' when used inside a LIKE statement. E.g. in WhereLike() extension and the defaultConcat is used.
/// </summary>
/// <param name="concatDefault">When provided this overides the GetWildcardPlaceholder() default.</param>
/// <returns></returns>
string GetWildcardConcat ( string concatDefault = "" ) ;
2022-06-02 08:18:31 +02:00
string GetStringColumnEqualComparison ( string column , int paramIndex , TextColumnType columnType ) ;
string GetStringColumnWildcardComparison ( string column , int paramIndex , TextColumnType columnType ) ;
string GetConcat ( params string [ ] args ) ;
string GetColumn ( DatabaseType dbType , string tableName , string columnName , string columnAlias , string? referenceName = null , bool forInsert = false ) ;
string GetQuotedTableName ( string? tableName ) ;
string GetQuotedColumnName ( string? columnName ) ;
string GetQuotedName ( string? name ) ;
bool DoesTableExist ( IDatabase db , string tableName ) ;
string GetIndexType ( IndexTypes indexTypes ) ;
string GetSpecialDbType ( SpecialDbType dbType ) ;
string FormatDateTime ( DateTime date , bool includeTime = true ) ;
string Format ( TableDefinition table ) ;
string Format ( IEnumerable < ColumnDefinition > columns ) ;
List < string > Format ( IEnumerable < IndexDefinition > indexes ) ;
List < string > Format ( IEnumerable < ForeignKeyDefinition > foreignKeys ) ;
string FormatPrimaryKey ( TableDefinition table ) ;
string GetQuotedValue ( string value ) ;
string Format ( ColumnDefinition column ) ;
string Format ( ColumnDefinition column , string tableName , out IEnumerable < string > sqls ) ;
string Format ( IndexDefinition index ) ;
string Format ( ForeignKeyDefinition foreignKey ) ;
string FormatColumnRename ( string? tableName , string? oldName , string? newName ) ;
string FormatTableRename ( string? oldName , string? newName ) ;
void HandleCreateTable ( IDatabase database , TableDefinition tableDefinition , bool skipKeysAndIndexes = false ) ;
Sql < ISqlContext > SelectTop ( Sql < ISqlContext > sql , int top ) ;
bool SupportsClustered ( ) ;
bool SupportsIdentityInsert ( ) ;
IEnumerable < string > GetTablesInSchema ( IDatabase db ) ;
IEnumerable < ColumnInfo > GetColumnsInSchema ( IDatabase db ) ;
/// <summary>
/// Returns all constraints defined in the database (Primary keys, foreign keys, unique constraints...) (does not
/// include indexes)
/// </summary>
/// <param name="db"></param>
/// <returns>
/// A Tuple containing: TableName, ConstraintName
/// </returns>
IEnumerable < Tuple < string , string > > GetConstraintsPerTable ( IDatabase db ) ;
/// <summary>
/// Returns all constraints defined in the database (Primary keys, foreign keys, unique constraints...) (does not
/// include indexes)
/// </summary>
/// <param name="db"></param>
/// <returns>
/// A Tuple containing: TableName, ColumnName, ConstraintName
/// </returns>
IEnumerable < Tuple < string , string , string > > GetConstraintsPerColumn ( IDatabase db ) ;
/// <summary>
/// Returns all defined Indexes in the database excluding primary keys
/// </summary>
/// <param name="db"></param>
/// <returns>
/// A Tuple containing: TableName, IndexName, ColumnName, IsUnique
/// </returns>
IEnumerable < Tuple < string , string , string , bool > > GetDefinedIndexes ( IDatabase db ) ;
/// <summary>
/// Tries to gets the name of the default constraint on a column.
/// </summary>
/// <param name="db">The database.</param>
/// <param name="tableName">The table name.</param>
/// <param name="columnName">The column name.</param>
/// <param name="constraintName">The constraint name.</param>
/// <returns>A value indicating whether a default constraint was found.</returns>
/// <remarks>
/// <para>
/// Some database engines may not have names for default constraints,
/// in which case the function may return true, but <paramref name="constraintName" /> is
/// unspecified.
/// </para>
/// </remarks>
bool TryGetDefaultConstraint ( IDatabase db , string? tableName , string columnName , [ MaybeNullWhen ( false ) ] out string constraintName ) ;
2023-02-07 13:46:14 +01:00
bool DoesPrimaryKeyExist ( IDatabase db , string tableName , string primaryKeyName ) = > throw new NotImplementedException ( ) ;
2022-06-02 08:18:31 +02:00
string GetFieldNameForUpdate < TDto > ( Expression < Func < TDto , object? > > fieldSelector , string? tableAlias = null ) ;
/// <summary>
/// Appends the relevant ForUpdate hint.
/// </summary>
Sql < ISqlContext > InsertForUpdateHint ( Sql < ISqlContext > sql ) ;
/// <summary>
/// Appends the relevant ForUpdate hint.
/// </summary>
Sql < ISqlContext > AppendForUpdateHint ( Sql < ISqlContext > sql ) ;
2018-06-29 19:52:40 +02:00
/// <summary>
2022-06-02 08:18:31 +02:00
/// Handles left join with nested join
2018-06-29 19:52:40 +02:00
/// </summary>
2022-06-02 08:18:31 +02:00
Sql < ISqlContext > . SqlJoinClause < ISqlContext > LeftJoinWithNestedJoin < TDto > (
Sql < ISqlContext > sql ,
Func < Sql < ISqlContext > , Sql < ISqlContext > > nestedJoin ,
string? alias = null ) ;
2018-06-29 19:52:40 +02:00
}