Fixed server crashes from stores' prepared statements.

This commit is contained in:
Tom 2024-10-31 19:09:46 +00:00
parent 5618b28c49
commit 4979553c74

View File

@ -22,36 +22,50 @@ namespace HermesSocketServer.Store
public async Task DoPreparedStatement<V>(Database database, string sql, IEnumerable<V> values, string[] columns) public async Task DoPreparedStatement<V>(Database database, string sql, IEnumerable<V> values, string[] columns)
{ {
await database.Execute(sql, (c) => try
{ {
var valueCounter = 0; await database.Execute(sql, (c) =>
foreach (var value in values)
{ {
foreach (var column in columns) var valueCounter = 0;
foreach (var value in values)
{ {
var propValue = columnPropertyRelations[column]!.GetValue(value); foreach (var column in columns)
c.Parameters.AddWithValue(column.ToLower() + valueCounter, propValue ?? DBNull.Value); {
var propValue = columnPropertyRelations[column]!.GetValue(value);
c.Parameters.AddWithValue(column.ToLower() + valueCounter, propValue ?? DBNull.Value);
}
valueCounter++;
} }
valueCounter++; });
} }
}); catch (Exception ex)
{
_logger.Error(ex, "Failed to execute a prepared statement: " + sql);
}
} }
public async Task DoPreparedStatementRaw<V>(Database database, string sql, IEnumerable<V> values, string[] columns) public async Task DoPreparedStatementRaw<V>(Database database, string sql, IEnumerable<V> values, string[] columns)
{ {
await database.Execute(sql, (c) => try
{ {
var valueCounter = 0; await database.Execute(sql, (c) =>
foreach (var value in values)
{ {
foreach (var column in columns) var valueCounter = 0;
foreach (var value in values)
{ {
object? propValue = value; foreach (var column in columns)
c.Parameters.AddWithValue(column.ToLower() + valueCounter, propValue ?? DBNull.Value); {
object? propValue = value;
c.Parameters.AddWithValue(column.ToLower() + valueCounter, propValue ?? DBNull.Value);
}
valueCounter++;
} }
valueCounter++; });
} }
}); catch (Exception ex)
{
_logger.Error(ex, "Failed to execute a prepared statement: " + sql);
}
} }
public string GenerateInsertSql(string table, IEnumerable<T> values, IEnumerable<string> columns) public string GenerateInsertSql(string table, IEnumerable<T> values, IEnumerable<string> columns)