Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SQL query source more forgiving for returning content items #16234

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Fluid.Values;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Records;
using OrchardCore.Data;
using OrchardCore.Json;
using OrchardCore.Liquid;
Expand Down Expand Up @@ -71,7 +72,20 @@ public async Task<IQueryResults> ExecuteQueryAsync(Query query, IDictionary<stri
IEnumerable<long> documentIds;

using var transaction = await connection.BeginTransactionAsync(_session.Store.Configuration.IsolationLevel);
documentIds = await connection.QueryAsync<long>(rawQuery, parameters, transaction);
var queryResult = await connection.QueryAsync(rawQuery, parameters, transaction);

documentIds = queryResult.Select(row =>
{
var rowDict = (IDictionary<string, object>)row;
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

if (rowDict.TryGetValue(nameof(ContentItemIndex.DocumentId), out var docIdObj) &&
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
docIdObj is long docId)
{
return docId;
}

return rowDict.FirstOrDefault(kv => kv.Value is long).Value as long? ?? 0;
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
});

sqlQueryResults.Items = await _session.GetAsync<ContentItem>(documentIds.ToArray());

Expand Down
4 changes: 2 additions & 2 deletions src/OrchardCore.Themes/TheBlogTheme/Recipes/blog.recipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -1177,9 +1177,9 @@
{
"Source": "Sql",
"Name": "RecentBlogPosts",
"Template": "SELECT * FROM ContentItemIndex WHERE ContentType='BlogPost' ORDER BY CreatedUtc DESC LIMIT 3",
"Template": "SELECT DocumentId FROM ContentItemIndex WHERE ContentType='BlogPost' ORDER BY CreatedUtc DESC LIMIT 3",
"Schema": "[js:base64('ew0KICAgICJ0eXBlIjogIkNvbnRlbnRJdGVtL0Jsb2dQb3N0Ig0KfQ==')]",
"ReturnContentItems": true
"ReturnDocuments": true
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
{
"Source": "Sql",
"Name": "RecentBlogPosts",
"Template": "SELECT * FROM ContentItemIndex WHERE ContentType='BlogPost' ORDER BY CreatedUtc DESC LIMIT 3",
"Template": "SELECT DocumentId FROM ContentItemIndex WHERE ContentType='BlogPost' ORDER BY CreatedUtc DESC LIMIT 3",
"Schema": "[js:base64('ew0KICAgICJ0eXBlIjogIkNvbnRlbnRJdGVtL0Jsb2dQb3N0Ig0KfQ==')]",
"ReturnContentItems": true
"ReturnDocuments": true
}
]
}
Expand Down
4 changes: 4 additions & 0 deletions src/docs/reference/modules/Queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ For Lucene queries with custom object schema, you are limited to elements stored

For SQL queries, you can expose any column where property name is a column alias from the query.

!!! note
When returning documents from a SQL query, make sure your query returns a list of document IDs.
Piedone marked this conversation as resolved.
Show resolved Hide resolved


gvkries marked this conversation as resolved.
Show resolved Hide resolved
Here is an example of a custom Query from a manually added table in a database :

```sql
Expand Down