sebastienros / yessql

A .NET document database working on any RDBMS
MIT License
1.21k stars 197 forks source link

SqlBuilder: GroupBy / Having randomly added when creating query #573

Open MikeKry opened 3 months ago

MikeKry commented 3 months ago

I have run into some unexpected behavior of SqlBuilder.

When I create sql builder from scratch, sometimes it returns empty group by / having clauses from ToSqlString() method.

"SELECT [LocalizedContentItemIndex_a1].[LocalizationSet] FROM [LocalizedContentItemIndex] AS [b] WHERE [b].[Culture] = 'cs-cz' AND [b].[Published] = 1 AND [b].[Published] = 1 AND [LocalizedContentItemIndex_a1].[LocalizationSet] = [b].[LocalizationSet] GROUP BY HAVING "

It can be fixed by calling ClearGroupBy(), which sets internal _group and _having property to null instead of empty array.

image

I do not understand why it happens, as it does happens unpredictably.

Here is full code: image

It seems that it happens always when sqlBuilder object is viewed in debugger

Fix could be using Count instead of null comparison here https://github.com/sebastienros/yessql/blob/01fea28df4f091bd8c0904c6bf514c2ad7fdd663/src/YesSql.Core/Sql/SqlBuilder.cs#L383