From 4979553c74dbb0bbe8fa6e89340e7ad14f007823 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 31 Oct 2024 19:09:46 +0000 Subject: [PATCH] Fixed server crashes from stores' prepared statements. --- Store/GroupSaveSqlGenerator.cs | 50 ++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/Store/GroupSaveSqlGenerator.cs b/Store/GroupSaveSqlGenerator.cs index b913dc4..d9df6fe 100644 --- a/Store/GroupSaveSqlGenerator.cs +++ b/Store/GroupSaveSqlGenerator.cs @@ -22,36 +22,50 @@ namespace HermesSocketServer.Store public async Task DoPreparedStatement(Database database, string sql, IEnumerable 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(Database database, string sql, IEnumerable 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 values, IEnumerable columns)