2023-06-06 18:49:14 +00:00
|
|
|
using Elsa.Common.Entities;
|
|
|
|
|
using Elsa.Dapper.Contracts;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Dapper.Abstractions;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides a base implementation of <see cref="ISqlDialect"/>, where the dialect defaults to SQLite.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class SqlDialectBase : ISqlDialect
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual string From(string table) => From(table, "*");
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual string From(string table, params string[] fields)
|
|
|
|
|
{
|
|
|
|
|
var fieldList = string.Join(", ", fields);
|
|
|
|
|
return $"select {fieldList} from {table} where 1=1";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string Delete(string table) => $"delete from {table} where 1=1";
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-06-29 20:08:10 +00:00
|
|
|
public string Count(string table) => Count("*", table);
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string Count(string fieldExpression, string table) => $"select COUNT({fieldExpression}) from {table} where 1=1";
|
2023-06-06 18:49:14 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual string And(string field) => $"and {field} = @{field}";
|
2023-07-23 19:05:31 +00:00
|
|
|
|
2023-06-30 18:56:37 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual string AndNot(string field) => $"and not {field} = @{field}";
|
2023-06-06 18:49:14 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-07-23 19:05:31 +00:00
|
|
|
public virtual string And(string field, string[] fieldParamNames) => $"and {field} in ({string.Join(", ", fieldParamNames)})";
|
|
|
|
|
|
2023-06-30 18:56:37 +00:00
|
|
|
/// <inheritdoc />
|
2023-07-23 19:05:31 +00:00
|
|
|
public virtual string AndNot(string field, string[] fieldParamNames) => $"and {field} not in ({string.Join(", ", fieldParamNames)})";
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string IsNull(string field) => $"and {field} is null";
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string IsNotNull(string field) => $"and {field} is not null";
|
2023-06-06 18:49:14 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual string OrderBy(string field, OrderDirection direction)
|
|
|
|
|
{
|
|
|
|
|
var directionString = direction == OrderDirection.Ascending ? "asc" : "desc";
|
|
|
|
|
return $"order by {field} {directionString}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-08-30 20:01:25 +00:00
|
|
|
public virtual string Skip(int count) => $"offset {count}";
|
2023-06-06 18:49:14 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-08-30 20:01:25 +00:00
|
|
|
public virtual string Take(int count) => $"limit {count}";
|
2023-06-06 18:49:14 +00:00
|
|
|
|
2023-06-06 19:43:53 +00:00
|
|
|
/// <inheritdoc />
|
2023-08-30 20:01:25 +00:00
|
|
|
public string Insert(string table, string[] fields, Func<string, string>? getParamName = default)
|
2023-06-06 19:43:53 +00:00
|
|
|
{
|
2023-08-30 20:01:25 +00:00
|
|
|
getParamName ??= x => x;
|
2023-06-06 19:43:53 +00:00
|
|
|
var fieldList = string.Join(", ", fields);
|
2023-08-30 20:01:25 +00:00
|
|
|
var fieldParamNames = fields.Select(x => $"@{getParamName(x)}");
|
2023-06-06 19:43:53 +00:00
|
|
|
var fieldParamList = string.Join(", ", fieldParamNames);
|
|
|
|
|
return $"INSERT INTO {table} ({fieldList}) VALUES ({fieldParamList});";
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-06 18:49:14 +00:00
|
|
|
/// <inheritdoc />
|
2023-08-30 20:01:25 +00:00
|
|
|
public virtual string Upsert(string table, string primaryKeyField, string[] fields, Func<string, string>? getParamName = default)
|
2023-06-06 18:49:14 +00:00
|
|
|
{
|
2023-08-30 20:01:25 +00:00
|
|
|
getParamName ??= x => x;
|
2023-06-06 18:49:14 +00:00
|
|
|
var fieldList = string.Join(", ", fields);
|
2023-08-30 20:01:25 +00:00
|
|
|
var fieldParamNames = fields.Select(x => $"@{getParamName(x)}");
|
2023-06-06 18:49:14 +00:00
|
|
|
var fieldParamList = string.Join(", ", fieldParamNames);
|
2023-08-30 20:01:25 +00:00
|
|
|
return $"INSERT OR REPLACE INTO {table} ({primaryKeyField}, {fieldList}) VALUES (@{getParamName(primaryKeyField)}, {fieldParamList});";
|
2023-06-06 18:49:14 +00:00
|
|
|
}
|
|
|
|
|
}
|