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-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 />
|
|
|
|
|
public virtual string And(string field, string[] fieldParamNames)
|
|
|
|
|
{
|
|
|
|
|
return $"and {field} in ({string.Join(", ", fieldParamNames)})";
|
|
|
|
|
}
|
2023-06-30 18:56:37 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual string AndNot(string field, string[] fieldParamNames)
|
|
|
|
|
{
|
|
|
|
|
return $"and {field} not in ({string.Join(", ", fieldParamNames)})";
|
|
|
|
|
}
|
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 />
|
|
|
|
|
public virtual string Skip(int count) => $"OFFSET {count}";
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual string Take(int count) => $"LIMIT {count}";
|
|
|
|
|
|
2023-06-06 19:43:53 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string Insert(string table, string[] fields)
|
|
|
|
|
{
|
|
|
|
|
var fieldList = string.Join(", ", fields);
|
|
|
|
|
var fieldParamNames = fields.Select(x => $"@{x}");
|
|
|
|
|
var fieldParamList = string.Join(", ", fieldParamNames);
|
|
|
|
|
return $"INSERT INTO {table} ({fieldList}) VALUES ({fieldParamList});";
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-06 18:49:14 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual string Upsert(string table, string primaryKeyField, string[] fields)
|
|
|
|
|
{
|
|
|
|
|
var fieldList = string.Join(", ", fields);
|
|
|
|
|
var fieldParamNames = fields.Select(x => $"@{x}");
|
|
|
|
|
var fieldParamList = string.Join(", ", fieldParamNames);
|
|
|
|
|
//var updateList = string.Join(", ", fields.Select(x => $"{x} = @{x}"));
|
|
|
|
|
//return $"insert into {table} ({fieldList}) values ({fieldParamList}) on conflict(id) do update set {updateList}";
|
|
|
|
|
return $"INSERT OR REPLACE INTO {table} ({primaryKeyField}, {fieldList}) VALUES (@{primaryKeyField}, {fieldParamList});";
|
|
|
|
|
}
|
|
|
|
|
}
|