Table of Contents

Class SqlHelpers

Namespace
LfrlAnvil.Sql.Internal
Assembly
LfrlAnvil.Sql.Core.dll

Contains various SQL helpers.

public static class SqlHelpers
Inheritance
SqlHelpers
Inherited Members

Fields

BlobMarker

public const char BlobMarker = 'X'

Field Value

char

DateFormat

public const string DateFormat = "yyyy-MM-dd"

Field Value

string

DateFormatQuoted

public const string DateFormatQuoted = "\\'yyyy-MM-dd\\'"

Field Value

string

DateTimeFormatMicrosecond

public const string DateTimeFormatMicrosecond = "yyyy-MM-dd HH:mm:ss.ffffff"

Field Value

string

DateTimeFormatMicrosecondQuoted

public const string DateTimeFormatMicrosecondQuoted = "\\'yyyy-MM-dd HH:mm:ss.ffffff\\'"

Field Value

string

DateTimeFormatTick

public const string DateTimeFormatTick = "yyyy-MM-dd HH:mm:ss.fffffff"

Field Value

string

DateTimeFormatTickQuoted

public const string DateTimeFormatTickQuoted = "\\'yyyy-MM-dd HH:mm:ss.fffffff\\'"

Field Value

string

DateTimeOffsetFormat

public const string DateTimeOffsetFormat = "yyyy-MM-dd HH:mm:ss.fffffffzzz"

Field Value

string

DateTimeOffsetFormatQuoted

public const string DateTimeOffsetFormatQuoted = "\\'yyyy-MM-dd HH:mm:ss.fffffffzzz\\'"

Field Value

string

DecimalFormat

public const string DecimalFormat = "0.0###########################"

Field Value

string

DecimalFormatQuoted

public const string DecimalFormatQuoted = "\\'0.0###########################\\'"

Field Value

string

DefaultNamesCreator

Default creator of SqlDefaultObjectNameProvider instances.

public static readonly SqlDefaultObjectNameProviderCreator<SqlDefaultObjectNameProvider> DefaultNamesCreator

Field Value

SqlDefaultObjectNameProviderCreator<SqlDefaultObjectNameProvider>

EmptyBlobLiteral

public const string EmptyBlobLiteral = "X''"

Field Value

string

EmptyTextLiteral

public const string EmptyTextLiteral = "''"

Field Value

string

ExecuteBoolScalarDelegate

Scalar IDbCommand executor delegate that returns bool value.

public static readonly Func<IDbCommand, bool> ExecuteBoolScalarDelegate

Field Value

Func<IDbCommand, bool>

ExecuteNonQueryDelegate

Non-query IDbCommand executor delegate.

public static readonly Func<IDbCommand, int> ExecuteNonQueryDelegate

Field Value

Func<IDbCommand, int>

NameComparer

SQL object name comparer. Equivalent to OrdinalIgnoreCase.

public static readonly StringComparer NameComparer

Field Value

StringComparer

StackallocThreshold

public const int StackallocThreshold = 64

Field Value

int

TextDelimiter

public const char TextDelimiter = '\''

Field Value

char

TimeFormatMicrosecond

public const string TimeFormatMicrosecond = "HH:mm:ss.ffffff"

Field Value

string

TimeFormatMicrosecondQuoted

public const string TimeFormatMicrosecondQuoted = "\\'HH:mm:ss.ffffff\\'"

Field Value

string

TimeFormatTick

public const string TimeFormatTick = "HH:mm:ss.fffffff"

Field Value

string

TimeFormatTickQuoted

public const string TimeFormatTickQuoted = "\\'HH:mm:ss.fffffff\\'"

Field Value

string

VersionHistoryCommitDateUtcName

public const string VersionHistoryCommitDateUtcName = "CommitDateUtc"

Field Value

string

VersionHistoryCommitDurationInTicksName

public const string VersionHistoryCommitDurationInTicksName = "CommitDurationInTicks"

Field Value

string

VersionHistoryDescriptionName

public const string VersionHistoryDescriptionName = "Description"

Field Value

string

VersionHistoryName

public const string VersionHistoryName = "__VersionHistory"

Field Value

string

VersionHistoryOrdinalName

public const string VersionHistoryOrdinalName = "Ordinal"

Field Value

string

VersionHistoryVersionBuildName

public const string VersionHistoryVersionBuildName = "VersionBuild"

Field Value

string

VersionHistoryVersionMajorName

public const string VersionHistoryVersionMajorName = "VersionMajor"

Field Value

string

VersionHistoryVersionMinorName

public const string VersionHistoryVersionMinorName = "VersionMinor"

Field Value

string

VersionHistoryVersionRevisionName

public const string VersionHistoryVersionRevisionName = "VersionRevision"

Field Value

string

Methods

AssertForeignKey(SqlTableBuilder, SqlIndexBuilder, SqlIndexBuilder)

Validates foreign key constraint.

public static void AssertForeignKey(SqlTableBuilder table, SqlIndexBuilder originIndex, SqlIndexBuilder referencedIndex)

Parameters

table SqlTableBuilder

SqlTableBuilder that the foreign key belongs to.

originIndex SqlIndexBuilder

SqlIndexBuilder from which the foreign key originates.

referencedIndex SqlIndexBuilder

SqlIndexBuilder which the foreign key references.

Remarks

Indexes must not be the same. Origin index cannot contain expressions. Referenced index must be unique and cannot be partial, and cannot contain nullable or generated columns. Both origin and referenced index must contain the same number of columns and their runtime types must be sequentially equal.

Exceptions

SqlObjectBuilderException

When foreign key is not considered valid.

AssertIndexColumns(SqlTableBuilder, SqlIndexBuilderColumns<SqlColumnBuilder>, bool)

Validates index constraint columns.

public static void AssertIndexColumns(SqlTableBuilder table, SqlIndexBuilderColumns<SqlColumnBuilder> columns, bool isUnique)

Parameters

table SqlTableBuilder

SqlTableBuilder that the index belongs to.

columns SqlIndexBuilderColumns<SqlColumnBuilder>

Collection of columns that belong to the index.

isUnique bool

Specifies whether or not the index is unique.

Remarks

Index must contain at least one column and columns must be distinct. When index is unique, then it cannot contain expressions.

Exceptions

SqlObjectBuilderException

When index columns are not considered valid.

AssertPrimaryKey(SqlTableBuilder, SqlIndexBuilder)

Validates primary key constraint.

public static void AssertPrimaryKey(SqlTableBuilder table, SqlIndexBuilder index)

Parameters

table SqlTableBuilder

SqlTableBuilder that the primary key belongs to.

index SqlIndexBuilder

SqlIndexBuilder that is the underlying index of the primary key.

Remarks

Underlying index must be unique and cannot be partial. It also cannot contain nullable columns or columns that are generated.

Exceptions

SqlObjectBuilderException

When primary key is not considered valid.

CastOrThrow<T>(ISqlDatabaseBuilder, object)

Type casts the provided object to the desired type.

[Pure]
public static T CastOrThrow<T>(ISqlDatabaseBuilder database, object obj)

Parameters

database ISqlDatabaseBuilder

SQL database builder with which the object is associated.

obj object

Object to cast.

Returns

T

obj cast to the desired type.

Type Parameters

T

Desired type.

Exceptions

SqlObjectCastException

When obj is not of the desired type.

CastOrThrow<T>(SqlDialect, object)

Type casts the provided object to the desired type.

[Pure]
public static T CastOrThrow<T>(SqlDialect dialect, object obj)

Parameters

dialect SqlDialect

SQL dialect with which the object is associated.

obj object

Object to cast.

Returns

T

obj cast to the desired type.

Type Parameters

T

Desired type.

Exceptions

SqlObjectCastException

When obj is not of the desired type.

CreateObjectBuilderException(ISqlDatabaseBuilder, Chain<string>)

Creates a new SqlObjectBuilderException instance.

[Pure]
public static SqlObjectBuilderException CreateObjectBuilderException(ISqlDatabaseBuilder database, Chain<string> errors)

Parameters

database ISqlDatabaseBuilder

Source SQL database builder.

errors Chain<string>

Collection of error messages.

Returns

SqlObjectBuilderException

New SqlObjectBuilderException instance.

CreateObjectBuilderException(ISqlDatabaseBuilder, string)

Creates a new SqlObjectBuilderException instance.

[Pure]
public static SqlObjectBuilderException CreateObjectBuilderException(ISqlDatabaseBuilder database, string error)

Parameters

database ISqlDatabaseBuilder

Source SQL database builder.

error string

Error message.

Returns

SqlObjectBuilderException

New SqlObjectBuilderException instance.

CreateObjectCastException(ISqlDatabase, Type, Type)

Creates a new SqlObjectCastException instance.

[Pure]
public static SqlObjectCastException CreateObjectCastException(ISqlDatabase database, Type expected, Type actual)

Parameters

database ISqlDatabase

Source SQL database.

expected Type

Expected object type.

actual Type

Actual object type.

Returns

SqlObjectCastException

New SqlObjectCastException instance.

CreateObjectCastException(ISqlDatabaseBuilder, Type, Type)

Creates a new SqlObjectCastException instance.

[Pure]
public static SqlObjectCastException CreateObjectCastException(ISqlDatabaseBuilder database, Type expected, Type actual)

Parameters

database ISqlDatabaseBuilder

Source SQL database builder.

expected Type

Expected object type.

actual Type

Actual object type.

Returns

SqlObjectCastException

New SqlObjectCastException instance.

GetDbLiteral(bool)

Converts value to a DB literal.

[Pure]
public static string GetDbLiteral(bool value)

Parameters

value bool

Value to convert.

Returns

string

"1" when value is equal to true, otherwise "0".

GetDbLiteral(char)

Converts value to a DB literal.

[Pure]
public static string GetDbLiteral(char value)

Parameters

value char

Value to convert.

Returns

string

string representation of the provided value.

GetDbLiteral(decimal)

Converts value to a DB literal.

[Pure]
public static string GetDbLiteral(decimal value)

Parameters

value decimal

Value to convert.

Returns

string

string representation of the provided value.

GetDbLiteral(double)

Converts value to a DB literal.

[Pure]
public static string GetDbLiteral(double value)

Parameters

value double

Value to convert.

Returns

string

string representation of the provided value.

GetDbLiteral(long)

Converts value to a DB literal.

[Pure]
public static string GetDbLiteral(long value)

Parameters

value long

Value to convert.

Returns

string

string representation of the provided value.

GetDbLiteral(ReadOnlySpan<byte>)

Converts value to a DB literal.

[Pure]
public static string GetDbLiteral(ReadOnlySpan<byte> value)

Parameters

value ReadOnlySpan<byte>

Value to convert.

Returns

string

string representation of the provided value.

GetDbLiteral(ReadOnlySpan<char>)

Converts value to a DB literal.

[Pure]
public static string GetDbLiteral(ReadOnlySpan<char> value)

Parameters

value ReadOnlySpan<char>

Value to convert.

Returns

string

string representation of the provided value.

Remarks

Escapes TextDelimiter occurrences.

GetDbLiteral(float)

Converts value to a DB literal.

[Pure]
public static string GetDbLiteral(float value)

Parameters

value float

Value to convert.

Returns

string

string representation of the provided value.

GetDbLiteral(ulong)

Converts value to a DB literal.

[Pure]
public static string GetDbLiteral(ulong value)

Parameters

value ulong

Value to convert.

Returns

string

string representation of the provided value.

GetDefaultCheckName(ISqlTableBuilder)

Creates a default check constraint name.

[Pure]
public static string GetDefaultCheckName(ISqlTableBuilder table)

Parameters

table ISqlTableBuilder

ISqlTableBuilder that the check belongs to.

Returns

string

Default check constraint name.

GetDefaultForeignKeyName(ISqlIndexBuilder, ISqlIndexBuilder)

Creates a default foreign key constraint name.

[Pure]
public static string GetDefaultForeignKeyName(ISqlIndexBuilder originIndex, ISqlIndexBuilder referencedIndex)

Parameters

originIndex ISqlIndexBuilder

ISqlIndexBuilder from which the foreign key originates.

referencedIndex ISqlIndexBuilder

ISqlIndexBuilder which the foreign key references.

Returns

string

Default foreign key constraint name.

GetDefaultIndexName(ISqlTableBuilder, SqlIndexBuilderColumns<ISqlColumnBuilder>, bool)

Creates a default index constraint name.

[Pure]
public static string GetDefaultIndexName(ISqlTableBuilder table, SqlIndexBuilderColumns<ISqlColumnBuilder> columns, bool isUnique)

Parameters

table ISqlTableBuilder

ISqlTableBuilder that the index belongs to.

columns SqlIndexBuilderColumns<ISqlColumnBuilder>

Collection of columns that belong to the index.

isUnique bool

Specifies whether or not the index is unique.

Returns

string

Default index constraint name.

GetDefaultPrimaryKeyName(ISqlTableBuilder)

Creates a default primary key constraint name.

[Pure]
public static string GetDefaultPrimaryKeyName(ISqlTableBuilder table)

Parameters

table ISqlTableBuilder

ISqlTableBuilder that the primary key belongs to.

Returns

string

Default primary key constraint name.

GetFullName(string, string, char)

Returns the full name of an SQL object.

[Pure]
public static string GetFullName(string schemaName, string name, char separator = '.')

Parameters

schemaName string

SQL schema name.

name string

SQL object name.

separator char

Name separator. Equal to . by default.

Returns

string

Full SQL object name.

GetFullName(string, string, string, char, char)

Returns the full name of an SQL field.

[Pure]
public static string GetFullName(string schemaName, string recordSetName, string name, char firstSeparator = '.', char secondSeparator = '.')

Parameters

schemaName string

SQL schema name.

recordSetName string

SQL record set name.

name string

SQL field name.

firstSeparator char

schemaName and recordSetName separator. Equal to . by default.

secondSeparator char

recordSetName and name separator. Equal to . by default.

Returns

string

Full SQL field name.

GetReferencingObjectsInOrderOfCreation(SqlObjectBuilder, Func<SqlObjectBuilderReference<SqlObjectBuilder>, bool>?)

Extracts ReferencingObjects from the provided obj, on order of their creation.

public static RentedMemorySequence<SqlObjectBuilder> GetReferencingObjectsInOrderOfCreation(SqlObjectBuilder obj, Func<SqlObjectBuilderReference<SqlObjectBuilder>, bool>? filter = null)

Parameters

obj SqlObjectBuilder

SqlObjectBuilder instance to extract references from.

filter Func<SqlObjectBuilderReference<SqlObjectBuilder>, bool>

Optional SQL object builder reference filter. References that return false will be ignored. Equal to null by default.

Returns

RentedMemorySequence<SqlObjectBuilder>

New RentedMemorySequence<T> instance.