2024-10-18 21:50:46 -04:00
|
|
|
using System.Reflection;
|
|
|
|
using System.Text;
|
2024-10-29 08:13:38 -04:00
|
|
|
using HermesSocketLibrary.db;
|
2024-10-18 21:50:46 -04:00
|
|
|
|
|
|
|
namespace HermesSocketServer.Store
|
|
|
|
{
|
|
|
|
public class GroupSaveSqlGenerator<T>
|
|
|
|
{
|
|
|
|
private readonly IDictionary<string, PropertyInfo?> columnPropertyRelations;
|
2024-10-29 08:13:38 -04:00
|
|
|
private readonly Serilog.ILogger _logger;
|
2024-10-18 21:50:46 -04:00
|
|
|
|
2024-10-29 08:13:38 -04:00
|
|
|
public GroupSaveSqlGenerator(IDictionary<string, string> columnsToProperties, Serilog.ILogger logger)
|
2024-10-18 21:50:46 -04:00
|
|
|
{
|
|
|
|
columnPropertyRelations = columnsToProperties.ToDictionary(p => p.Key, p => typeof(T).GetProperty(p.Value));
|
2024-10-29 08:13:38 -04:00
|
|
|
_logger = logger;
|
2024-10-18 21:50:46 -04:00
|
|
|
|
|
|
|
var nullProperties = columnPropertyRelations.Where(p => p.Value == null)
|
|
|
|
.Select(p => columnsToProperties[p.Key]);
|
|
|
|
if (nullProperties.Any())
|
|
|
|
throw new ArgumentException("Some properties do not exist on the values given: " + string.Join(", ", nullProperties));
|
|
|
|
}
|
|
|
|
|
2024-10-29 08:13:38 -04:00
|
|
|
public async Task DoPreparedStatement<V>(Database database, string sql, IEnumerable<V> values, string[] columns)
|
|
|
|
{
|
|
|
|
await database.Execute(sql, (c) =>
|
|
|
|
{
|
|
|
|
var valueCounter = 0;
|
|
|
|
foreach (var value in values)
|
|
|
|
{
|
|
|
|
foreach (var column in columns)
|
|
|
|
{
|
|
|
|
var propValue = columnPropertyRelations[column]!.GetValue(value);
|
2024-10-31 11:42:43 -04:00
|
|
|
c.Parameters.AddWithValue(column.ToLower() + valueCounter, propValue ?? DBNull.Value);
|
|
|
|
}
|
|
|
|
valueCounter++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task DoPreparedStatementRaw<V>(Database database, string sql, IEnumerable<V> values, string[] columns)
|
|
|
|
{
|
|
|
|
await database.Execute(sql, (c) =>
|
|
|
|
{
|
|
|
|
var valueCounter = 0;
|
|
|
|
foreach (var value in values)
|
|
|
|
{
|
|
|
|
foreach (var column in columns)
|
|
|
|
{
|
|
|
|
object? propValue = value;
|
2024-10-29 08:13:38 -04:00
|
|
|
c.Parameters.AddWithValue(column.ToLower() + valueCounter, propValue ?? DBNull.Value);
|
|
|
|
}
|
|
|
|
valueCounter++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-18 21:50:46 -04:00
|
|
|
public string GenerateInsertSql(string table, IEnumerable<T> values, IEnumerable<string> columns)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(table))
|
|
|
|
throw new ArgumentException("Value is either null or whitespace-filled.", nameof(table));
|
|
|
|
if (values == null)
|
|
|
|
throw new ArgumentNullException(nameof(values));
|
|
|
|
if (!values.Any())
|
|
|
|
throw new ArgumentException("Empty list given.", nameof(values));
|
|
|
|
if (columns == null)
|
|
|
|
throw new ArgumentNullException(nameof(columns));
|
|
|
|
if (!columns.Any())
|
|
|
|
throw new ArgumentException("Empty list given.", nameof(columns));
|
|
|
|
|
|
|
|
var ctp = columns.ToDictionary(c => c, c => columnPropertyRelations[c]);
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
sb.Append($"INSERT INTO \"{table}\" (\"{string.Join("\", \"", columns)}\") VALUES ");
|
|
|
|
foreach (var value in values)
|
|
|
|
{
|
|
|
|
sb.Append("(");
|
|
|
|
foreach (var column in columns)
|
|
|
|
{
|
|
|
|
var propValue = columnPropertyRelations[column]!.GetValue(value);
|
|
|
|
var propType = columnPropertyRelations[column]!.PropertyType;
|
2024-10-29 08:13:38 -04:00
|
|
|
WriteValue(sb, propValue ?? DBNull.Value, propType);
|
2024-10-18 21:50:46 -04:00
|
|
|
sb.Append(",");
|
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 1, 1)
|
2024-10-29 08:13:38 -04:00
|
|
|
.Append("),");
|
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 1, 1)
|
|
|
|
.Append(';');
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GeneratePreparedInsertSql(string table, int rows, IEnumerable<string> columns)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(table))
|
|
|
|
throw new ArgumentException("Value is either null or whitespace-filled.", nameof(table));
|
|
|
|
if (columns == null)
|
|
|
|
throw new ArgumentNullException(nameof(columns));
|
|
|
|
if (!columns.Any())
|
|
|
|
throw new ArgumentException("Empty list given.", nameof(columns));
|
|
|
|
|
|
|
|
var ctp = columns.ToDictionary(c => c, c => columnPropertyRelations[c]);
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
var columnsLower = columns.Select(c => c.ToLower());
|
|
|
|
sb.Append($"INSERT INTO \"{table}\" (\"{string.Join("\", \"", columns)}\") VALUES ");
|
|
|
|
for (var row = 0; row < rows; row++)
|
|
|
|
{
|
|
|
|
sb.Append("(");
|
|
|
|
foreach (var column in columnsLower)
|
|
|
|
{
|
|
|
|
sb.Append('@')
|
|
|
|
.Append(column)
|
|
|
|
.Append(row)
|
|
|
|
.Append(", ");
|
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 2, 2)
|
|
|
|
.Append("),");
|
2024-10-18 21:50:46 -04:00
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 1, 1)
|
|
|
|
.Append(';');
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GenerateUpdateSql(string table, IEnumerable<T> values, IEnumerable<string> keyColumns, IEnumerable<string> updateColumns)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(table))
|
|
|
|
throw new ArgumentException("Value is either null or whitespace-filled.", nameof(table));
|
|
|
|
if (values == null)
|
|
|
|
throw new ArgumentNullException(nameof(values));
|
|
|
|
if (!values.Any())
|
|
|
|
throw new ArgumentException("Empty list given.", nameof(values));
|
|
|
|
if (keyColumns == null)
|
|
|
|
throw new ArgumentNullException(nameof(keyColumns));
|
|
|
|
if (!keyColumns.Any())
|
|
|
|
throw new ArgumentException("Empty list given.", nameof(keyColumns));
|
|
|
|
if (updateColumns == null)
|
|
|
|
throw new ArgumentNullException(nameof(updateColumns));
|
|
|
|
if (!updateColumns.Any())
|
|
|
|
throw new ArgumentException("Empty list given.", nameof(updateColumns));
|
|
|
|
|
|
|
|
var columns = keyColumns.Union(updateColumns);
|
|
|
|
var ctp = columns.ToDictionary(c => c, c => columnPropertyRelations[c]);
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
sb.Append($"UPDATE \"{table}\" as t SET {string.Join(", ", updateColumns.Select(c => "\"" + c + "\" = c.\"" + c + "\""))} FROM (VALUES ");
|
|
|
|
foreach (var value in values)
|
|
|
|
{
|
|
|
|
sb.Append("(");
|
|
|
|
foreach (var column in columns)
|
|
|
|
{
|
|
|
|
var propValue = columnPropertyRelations[column]!.GetValue(value);
|
|
|
|
var propType = columnPropertyRelations[column]!.PropertyType;
|
|
|
|
WriteValue(sb, propValue, propType);
|
|
|
|
sb.Append(",");
|
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 1, 1)
|
|
|
|
.Append("),");
|
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 1, 1)
|
2024-10-21 16:44:20 -04:00
|
|
|
.Append($") AS c(\"{string.Join("\", \"", columns)}\") WHERE ")
|
|
|
|
.Append(string.Join(" AND ", keyColumns.Select(c => "t.\"" + c + "\" = c.\"" + c + "\"")))
|
|
|
|
.Append(";");
|
2024-10-18 21:50:46 -04:00
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
2024-10-29 08:13:38 -04:00
|
|
|
public string GeneratePreparedUpdateSql(string table, int rows, IEnumerable<string> keyColumns, IEnumerable<string> updateColumns)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(table))
|
|
|
|
throw new ArgumentException("Value is either null or whitespace-filled.", nameof(table));
|
|
|
|
if (keyColumns == null)
|
|
|
|
throw new ArgumentNullException(nameof(keyColumns));
|
|
|
|
if (!keyColumns.Any())
|
|
|
|
throw new ArgumentException("Empty list given.", nameof(keyColumns));
|
|
|
|
if (updateColumns == null)
|
|
|
|
throw new ArgumentNullException(nameof(updateColumns));
|
|
|
|
if (!updateColumns.Any())
|
|
|
|
throw new ArgumentException("Empty list given.", nameof(updateColumns));
|
|
|
|
|
|
|
|
var columns = keyColumns.Union(updateColumns);
|
|
|
|
var ctp = columns.ToDictionary(c => c, c => columnPropertyRelations[c]);
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
sb.Append($"UPDATE \"{table}\" as t SET {string.Join(", ", updateColumns.Select(c => "\"" + c + "\" = c.\"" + c + "\""))} FROM (VALUES ");
|
|
|
|
for (var row = 0; row < rows; row++)
|
|
|
|
{
|
|
|
|
sb.Append("(");
|
|
|
|
foreach (var column in columns)
|
|
|
|
{
|
|
|
|
sb.Append('@')
|
|
|
|
.Append(column)
|
|
|
|
.Append(row)
|
|
|
|
.Append(", ");
|
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 2, 2)
|
|
|
|
.Append("),");
|
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 1, 1)
|
|
|
|
.Append($") AS c(\"{string.Join("\", \"", columns)}\") WHERE ")
|
|
|
|
.Append(string.Join(" AND ", keyColumns.Select(c => "t.\"" + c + "\" = c.\"" + c + "\"")))
|
|
|
|
.Append(";");
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
2024-10-18 21:50:46 -04:00
|
|
|
public string GenerateDeleteSql(string table, IEnumerable<string> keys, IEnumerable<string> keyColumns)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(table))
|
|
|
|
throw new ArgumentException("Value is either null or whitespace-filled.", nameof(table));
|
|
|
|
if (keys == null)
|
|
|
|
throw new ArgumentNullException(nameof(keys));
|
|
|
|
if (!keys.Any())
|
|
|
|
throw new ArgumentException("Empty list given.", nameof(keys));
|
|
|
|
if (keyColumns == null)
|
|
|
|
throw new ArgumentNullException(nameof(keyColumns));
|
|
|
|
if (!keyColumns.Any())
|
|
|
|
throw new ArgumentException("Empty list given.", nameof(keyColumns));
|
|
|
|
|
|
|
|
var ctp = keyColumns.ToDictionary(c => c, c => columnPropertyRelations[c]);
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
sb.Append($"DELETE FROM \"{table}\" WHERE (\"{string.Join("\", \"", keyColumns)}\") IN (");
|
|
|
|
foreach (var k in keys)
|
|
|
|
{
|
|
|
|
sb.Append("(");
|
|
|
|
foreach (var column in keyColumns)
|
|
|
|
{
|
|
|
|
var propType = columnPropertyRelations[column]!.PropertyType;
|
|
|
|
WriteValue(sb, k, propType);
|
|
|
|
sb.Append(",");
|
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 1, 1)
|
|
|
|
.Append("),");
|
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 1, 1)
|
|
|
|
.Append(");");
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
2024-10-29 08:13:38 -04:00
|
|
|
public string GeneratePreparedDeleteSql(string table, int rows, IEnumerable<string> keyColumns)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(table))
|
|
|
|
throw new ArgumentException("Value is either null or whitespace-filled.", nameof(table));
|
|
|
|
if (keyColumns == null)
|
|
|
|
throw new ArgumentNullException(nameof(keyColumns));
|
|
|
|
if (!keyColumns.Any())
|
|
|
|
throw new ArgumentException("Empty list given.", nameof(keyColumns));
|
|
|
|
|
|
|
|
var ctp = keyColumns.ToDictionary(c => c, c => columnPropertyRelations[c]);
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
sb.Append($"DELETE FROM \"{table}\" WHERE (\"{string.Join("\", \"", keyColumns)}\") IN (");
|
|
|
|
for (var row = 0; row < rows; row++)
|
|
|
|
{
|
|
|
|
sb.Append("(");
|
|
|
|
foreach (var column in keyColumns)
|
|
|
|
{
|
|
|
|
sb.Append('@')
|
|
|
|
.Append(column)
|
|
|
|
.Append(row)
|
|
|
|
.Append(", ");
|
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 2, 2)
|
|
|
|
.Append("),");
|
|
|
|
}
|
|
|
|
sb.Remove(sb.Length - 1, 1)
|
|
|
|
.Append(");");
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
2024-10-18 21:50:46 -04:00
|
|
|
private void WriteValue(StringBuilder sb, object? value, Type type)
|
|
|
|
{
|
|
|
|
if (type == typeof(string))
|
|
|
|
sb.Append("'")
|
|
|
|
.Append(value)
|
|
|
|
.Append("'");
|
2024-10-21 16:44:20 -04:00
|
|
|
else if (type == typeof(Guid))
|
2024-10-29 08:13:38 -04:00
|
|
|
sb.Append("uuid('")
|
2024-10-21 16:44:20 -04:00
|
|
|
.Append(value?.ToString())
|
2024-10-29 08:13:38 -04:00
|
|
|
.Append("')");
|
2024-10-21 16:44:20 -04:00
|
|
|
else if (type == typeof(TimeSpan))
|
|
|
|
sb.Append(((TimeSpan)value).TotalMilliseconds);
|
2024-10-18 21:50:46 -04:00
|
|
|
else
|
|
|
|
sb.Append(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|