Migrate guild objects to the patch system

This commit is contained in:
Ske
2020-06-29 15:20:28 +02:00
parent 467ce78522
commit 0598c53f62
11 changed files with 124 additions and 62 deletions

View File

@@ -50,7 +50,7 @@ namespace PluralKit.Core
else _updateFragment.Append(", ");
_updateFragment.Append(fieldName);
_updateFragment.Append("=");
_updateFragment.Append(" = ");
_updateFragment.Append(paramName);
return this;
}
@@ -71,7 +71,7 @@ namespace PluralKit.Core
if (Type == QueryType.Update && _condition != null)
query.Append($" where {_condition}");
if (suffix != null)
if (!string.IsNullOrEmpty(suffix))
query.Append($" {suffix}");
query.Append(";");

View File

@@ -6,34 +6,29 @@ namespace PluralKit.Core
{
public class UpdateQueryBuilder
{
private readonly string _table;
private readonly string _condition;
private readonly QueryBuilder _qb;
private readonly DynamicParameters _params = new DynamicParameters();
private bool _hasFields = false;
private readonly StringBuilder _setClause = new StringBuilder();
public UpdateQueryBuilder(string table, string condition)
private UpdateQueryBuilder(QueryBuilder qb)
{
_table = table;
_condition = condition;
_qb = qb;
}
public static UpdateQueryBuilder Insert(string table) => new UpdateQueryBuilder(QueryBuilder.Insert(table));
public static UpdateQueryBuilder Update(string table, string condition) => new UpdateQueryBuilder(QueryBuilder.Update(table, condition));
public static UpdateQueryBuilder Upsert(string table, string conflictField) => new UpdateQueryBuilder(QueryBuilder.Upsert(table, conflictField));
public UpdateQueryBuilder WithConstant<T>(string name, T value)
{
_params.Add(name, value);
_qb.Constant(name, $"@{name}");
return this;
}
public UpdateQueryBuilder With<T>(string columnName, T value)
{
_params.Add(columnName, value);
if (_hasFields)
_setClause.Append(", ");
else _hasFields = true;
_setClause.Append($"{columnName} = @{columnName}");
_qb.Variable(columnName, $"@{columnName}");
return this;
}
@@ -42,10 +37,9 @@ namespace PluralKit.Core
return partialValue.IsPresent ? With(columnName, partialValue.Value) : this;
}
public (string Query, DynamicParameters Parameters) Build(string append = "")
public (string Query, DynamicParameters Parameters) Build(string suffix = "")
{
var query = $"update {_table} set {_setClause} where {_condition} {append}";
return (query, _params);
return (_qb.Build(suffix), _params);
}
}
}