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
Changes from all 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
@@ -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;
@@ -71,7 +72,32 @@ 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);

string column = null;

documentIds = queryResult.Select(row =>
{
var rowDictionary = (IDictionary<string, object>)row;

if (column == null)
{
if (rowDictionary.ContainsKey(nameof(ContentItemIndex.DocumentId)))
{
column = nameof(ContentItemIndex.DocumentId);
}
else
{
column = rowDictionary
.FirstOrDefault(kv => kv.Value is long).Key
?? rowDictionary.First().Key;
}
}

return rowDictionary.TryGetValue(column, out var documentIdObject) && documentIdObject is long documentId
? documentId
: 0;
});

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

4 changes: 2 additions & 2 deletions src/OrchardCore.Themes/TheBlogTheme/Recipes/blog.recipe.json
Original file line number Diff line number Diff line change
@@ -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
}
]
},
Original file line number Diff line number Diff line change
@@ -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
}
]
}
3 changes: 3 additions & 0 deletions src/docs/reference/modules/Queries/README.md
Original file line number Diff line number Diff line change
@@ -100,6 +100,9 @@ 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. This is commonly available in the `DocumentId` column, but check the tables you're querying.

Here is an example of a custom Query from a manually added table in a database :

```sql