From 4c09d0e15928314a5fb1abd6c7c7eb7b243af596 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 22:48:18 +0000 Subject: [PATCH 1/3] Initial plan From 861ebb5bbead19e6d37021b56790b9d54c1562da Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 22:51:51 +0000 Subject: [PATCH 2/3] Reinstate QueryDocumentsAsync with full SQL implementation Co-authored-by: mrdevrobot <12503462+mrdevrobot@users.noreply.github.com> --- .../SqlitePeerStore.cs | 74 ++++++++++++++++++- 1 file changed, 71 insertions(+), 3 deletions(-) diff --git a/src/EntglDb.Persistence.Sqlite/SqlitePeerStore.cs b/src/EntglDb.Persistence.Sqlite/SqlitePeerStore.cs index d132449..eab765b 100644 --- a/src/EntglDb.Persistence.Sqlite/SqlitePeerStore.cs +++ b/src/EntglDb.Persistence.Sqlite/SqlitePeerStore.cs @@ -1001,10 +1001,78 @@ public async Task> GetCollectionsAsync(CancellationToken can return collections; } - public Task> QueryDocumentsAsync(string collection, QueryNode? queryExpression, int? skip = null, int? take = null, string? orderBy = null, bool ascending = true, CancellationToken cancellationToken = default) + public async Task> QueryDocumentsAsync(string collection, QueryNode? queryExpression, int? skip = null, int? take = null, string? orderBy = null, bool ascending = true, CancellationToken cancellationToken = default) { - // Not implemented (Placeholder - simple query not supported by this store yet) - return Task.FromResult(Enumerable.Empty()); + using var connection = new SqliteConnection(_connectionString); + await connection.OpenAsync(cancellationToken); + + await EnsureCollectionTablesAsync(connection, collection); + + var translator = new SqlQueryTranslator(); + string whereClause = "1=1"; + var parameters = new DynamicParameters(); + + if (queryExpression != null) + { + var (w, p) = translator.Translate(queryExpression); + whereClause = w; + parameters = p; + } + + var tableName = GetDocumentTableName(collection); + var usePerCollection = _options?.UsePerCollectionTables == true; + + var sqlBuilder = new System.Text.StringBuilder(); + sqlBuilder.Append($@" + SELECT Key, JsonData, IsDeleted, HlcWall, HlcLogic, HlcNode + FROM {tableName} + WHERE IsDeleted = 0"); + + if (!usePerCollection) + { + sqlBuilder.Append(" AND Collection = @Collection"); + parameters.Add("@Collection", collection); + } + + sqlBuilder.Append(" AND ("); + sqlBuilder.Append(whereClause); + sqlBuilder.Append(")"); + + if (!string.IsNullOrEmpty(orderBy)) + { + if (orderBy.All(c => char.IsLetterOrDigit(c) || c == '.' || c == '_')) + { + string sortField = $"json_extract(JsonData, '$.{orderBy}')"; + sqlBuilder.Append($" ORDER BY {sortField} {(ascending ? "ASC" : "DESC")}"); + } + } + else + { + sqlBuilder.Append(" ORDER BY Key ASC"); + } + + // Paging + if (take.HasValue) + { + sqlBuilder.Append(" LIMIT @Take"); + parameters.Add("@Take", take.Value); + } + + if (skip.HasValue) + { + sqlBuilder.Append(" OFFSET @Skip"); + parameters.Add("@Skip", skip.Value); + } + + var rows = await connection.QueryAsync(sqlBuilder.ToString(), parameters); + + return rows.Select(r => { + var hlc = new HlcTimestamp(r.HlcWall, r.HlcLogic, r.HlcNode); + var content = r.JsonData != null + ? JsonSerializer.Deserialize(r.JsonData) + : default; + return new Document(collection, r.Key, content, hlc, r.IsDeleted != 0); + }); } // --- Snapshotting Implementation --- From d3588696c5f53bc64d52b0da80465bc4ebb41039 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 22:53:09 +0000 Subject: [PATCH 3/3] Fix indentation in QueryDocumentsAsync implementation Co-authored-by: mrdevrobot <12503462+mrdevrobot@users.noreply.github.com> --- src/EntglDb.Persistence.Sqlite/SqlitePeerStore.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/EntglDb.Persistence.Sqlite/SqlitePeerStore.cs b/src/EntglDb.Persistence.Sqlite/SqlitePeerStore.cs index eab765b..ad354ab 100644 --- a/src/EntglDb.Persistence.Sqlite/SqlitePeerStore.cs +++ b/src/EntglDb.Persistence.Sqlite/SqlitePeerStore.cs @@ -1067,11 +1067,11 @@ public async Task> QueryDocumentsAsync(string collection, var rows = await connection.QueryAsync(sqlBuilder.ToString(), parameters); return rows.Select(r => { - var hlc = new HlcTimestamp(r.HlcWall, r.HlcLogic, r.HlcNode); - var content = r.JsonData != null + var hlc = new HlcTimestamp(r.HlcWall, r.HlcLogic, r.HlcNode); + var content = r.JsonData != null ? JsonSerializer.Deserialize(r.JsonData) : default; - return new Document(collection, r.Key, content, hlc, r.IsDeleted != 0); + return new Document(collection, r.Key, content, hlc, r.IsDeleted != 0); }); }