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)
{
await database.Execute(sql, (c) =>
try
{
var valueCounter = 0;
foreach (var value in values)
await database.Execute(sql, (c) =>
{
foreach (var column in columns)
var valueCounter = 0;
foreach (var value in values)
{
var propValue = columnPropertyRelations[column]!.GetValue(value);
c.Parameters.AddWithValue(column.ToLower() + valueCounter, propValue ?? DBNull.Value);
foreach (var column in columns)
{
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)
{
await database.Execute(sql, (c) =>
try
{
var valueCounter = 0;
foreach (var value in values)
await database.Execute(sql, (c) =>
{
foreach (var column in columns)
var valueCounter = 0;
foreach (var value in values)
{
object? propValue = value;
c.Parameters.AddWithValue(column.ToLower() + valueCounter, propValue ?? DBNull.Value);
foreach (var column in columns)
{
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)