mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-04-10 17:15:43 +02:00
Refactor generating SQL "WHERE" clauses
This commit is contained in:
parent
2459c8ee1a
commit
1e6e5c6f92
app/Server/Database/Sqlite
@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DHT.Server.Data.Filters;
|
||||
using DHT.Server.Database.Sqlite.Utils;
|
||||
|
||||
namespace DHT.Server.Database.Sqlite {
|
||||
static class SqliteMessageFilter {
|
||||
@ -9,42 +9,29 @@ namespace DHT.Server.Database.Sqlite {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (tableAlias != null) {
|
||||
tableAlias += ".";
|
||||
}
|
||||
|
||||
List<string> conditions = new();
|
||||
var where = new SqliteWhereGenerator(tableAlias, invert);
|
||||
|
||||
if (filter.StartDate != null) {
|
||||
conditions.Add(tableAlias + "timestamp >= " + new DateTimeOffset(filter.StartDate.Value).ToUnixTimeMilliseconds());
|
||||
where.AddCondition("timestamp >= " + new DateTimeOffset(filter.StartDate.Value).ToUnixTimeMilliseconds());
|
||||
}
|
||||
|
||||
if (filter.EndDate != null) {
|
||||
conditions.Add(tableAlias + "timestamp <= " + new DateTimeOffset(filter.EndDate.Value).ToUnixTimeMilliseconds());
|
||||
where.AddCondition("timestamp <= " + new DateTimeOffset(filter.EndDate.Value).ToUnixTimeMilliseconds());
|
||||
}
|
||||
|
||||
if (filter.ChannelIds != null) {
|
||||
conditions.Add(tableAlias + "channel_id IN (" + string.Join(",", filter.ChannelIds) + ")");
|
||||
where.AddCondition("channel_id IN (" + string.Join(",", filter.ChannelIds) + ")");
|
||||
}
|
||||
|
||||
if (filter.UserIds != null) {
|
||||
conditions.Add(tableAlias + "sender_id IN (" + string.Join(",", filter.UserIds) + ")");
|
||||
where.AddCondition("sender_id IN (" + string.Join(",", filter.UserIds) + ")");
|
||||
}
|
||||
|
||||
if (filter.MessageIds != null) {
|
||||
conditions.Add(tableAlias + "message_id IN (" + string.Join(",", filter.MessageIds) + ")");
|
||||
where.AddCondition("message_id IN (" + string.Join(",", filter.MessageIds) + ")");
|
||||
}
|
||||
|
||||
if (conditions.Count == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (invert) {
|
||||
return " WHERE NOT (" + string.Join(" AND ", conditions) + ")";
|
||||
}
|
||||
else {
|
||||
return " WHERE " + string.Join(" AND ", conditions);
|
||||
}
|
||||
return where.Generate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
31
app/Server/Database/Sqlite/Utils/SqliteWhereGenerator.cs
Normal file
31
app/Server/Database/Sqlite/Utils/SqliteWhereGenerator.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DHT.Server.Database.Sqlite.Utils {
|
||||
sealed class SqliteWhereGenerator {
|
||||
private readonly string? tableAlias;
|
||||
private readonly bool invert;
|
||||
private readonly List<string> conditions = new ();
|
||||
|
||||
public SqliteWhereGenerator(string? tableAlias, bool invert) {
|
||||
this.tableAlias = tableAlias;
|
||||
this.invert = invert;
|
||||
}
|
||||
|
||||
public void AddCondition(string condition) {
|
||||
conditions.Add(tableAlias == null ? condition : tableAlias + '.' + condition);
|
||||
}
|
||||
|
||||
public string Generate() {
|
||||
if (conditions.Count == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (invert) {
|
||||
return " WHERE NOT (" + string.Join(" AND ", conditions) + ")";
|
||||
}
|
||||
else {
|
||||
return " WHERE " + string.Join(" AND ", conditions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user