litedb-org / LiteDB.Studio

A GUI tool for viewing and editing documents for LiteDB v5
MIT License
806 stars 203 forks source link

Paramaterized queries not working? #86

Closed lxman closed 5 months ago

lxman commented 5 months ago

I'm using 5.0.2 version on .NET 8.

using LiteDB;
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously

namespace LiteDbQueryingTest
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            LiteDatabase db = new(":memory:");
            ILiteCollection<BsonDocument> testDb = db.GetCollection("test");
            BsonDocument newDoc = new()
            {
                ["PartitionKey"] = "PartitionKey",
                ["RowKey"] = "RowKey",
                ["Name"] = "Fred"
            };
            testDb.Insert(newDoc);
            newDoc = new BsonDocument
            {
                ["PartitionKey"] = "PartitionKey",
                ["RowKey"] = "RowKey2",
                ["Name"] = "George"
            };
            testDb.Insert(newDoc);
            IBsonDataReader? reader = db.Execute("SELECT $ FROM test WHERE Name = @p0", new BsonDocument(new Dictionary<string, BsonValue> { ["@p0"] = new("'Fred'") }));
            IAsyncEnumerable<string> result = ReadContentsAsync(reader);
            await foreach (string line in result)
            {
                Console.WriteLine(line);
            }
        }

        private static async IAsyncEnumerable<string> ReadContentsAsync(IBsonDataReader reader)
        {
            var count = 1;
            while (reader.Read())
            {
                yield return $"Document {count++}";
                BsonDocument? cast = reader.Current.AsDocument;
                foreach (string key in cast.Keys)
                {
                    yield return $"\tKey: {key}, Value: {cast[key]}";
                }
            }
        }
    }
}

If I run this it returns no results.

If I change my query to SELECT $ FROM test WHERE Name = 'Fred' and leave out the BsonDocument, it returns what I would expect. Is parameterized querying not working, or am I doing something incorrect here?

lxman commented 5 months ago

Just realized I posted this in the wrong place. Closing and I'll repost correctly.