Skip to content

Queries (OrchardCore.Queries)

查询(OrchardCore.Queries)

The queries module provides a management UI and APIs for querying data.

查询模块提供用于查询数据的管理UI和API。

Creating custom query sources

创建自定义查询源

Query

询问

Create a class inheriting from Query which will represent the state that is necessary to represent this new query.

创建一个继承自Query的类,它将表示表示此新查询所必需的状态。

QuerySource

QuerySource

Create a class implementing IQuerySource in order to expose the new type of query.

创建一个实现IQuerySource的类,以便公开新类型的查询。

The query source can be registered like this:

查询源可以像这样注册:


services.AddScoped<IQuerySource, LuceneQuerySource>();

 <font color=#0099ff size=4 face="黑体">services.AddScoped <IQuerySource,LuceneQuerySource>();</font> 


Editors

编者

Queries are edited by providing a custom implementation of a DisplayDriver for the type Query.

通过为Query类型提供DisplayDriver的自定义实现来编辑查询。


public class LuceneQueryDisplayDriver : DisplayDriver<Query, LuceneQuery>

 <font color=#0099ff size=4 face="黑体">公共类LuceneQueryDisplayDriver:DisplayDriver <Query,LuceneQuery></font> 


{

 <font color=#0099ff size=4 face="黑体">{</font> 


...

 <font color=#0099ff size=4 face="黑体">...</font> 


}

 <font color=#0099ff size=4 face="黑体">}</font> 


Queries dialog

查询对话框

When the list of query types is displayed, a template for the shape Query_Link__[QuerySource] will be used.

当显示查询类型列表时,将使用形状Query_Link __ [QuerySource]的模板。

For instance, if the source is Lucene then the file Query-Lucene.Link.cshtml will be used.

例如,如果源是Lucene,那么将使用文件Query-Lucene.Link.cshtml

Recipe step

食谱步骤

Queries can be created during recipes using the queries step.

可以使用queries步骤在配方期间创建查询。

Here is a sample step:

这是一个示例步骤:


{

 <font color=#0099ff size=4 face="黑体">{</font> 


    "name": "queries",

    "Queries": [ {

        // Common properties

        "Name": "AwesomeQuery",

        "Source": "Lucene",

        // Properties of the concrete query

        ...

        }

    ]

}

 <font color=#0099ff size=4 face="黑体">}</font> 





 <font color=#0099ff size=4 face="黑体"></font> 


Web APIs

Web API

api/queries/{name}

API /查询/(名称)

Executes a query with the specified name.

执行具有指定名称的查询。

Verbs: POST and GET

动词: POST GET

| Parameter | Example | Description |

|参数|示例|说明|

| --------- | ---- |------------ |

| --------- | ---- | ------------ |

| name | myQuery | The name of the query to execute |

| name | myQuery |要执行的查询的名称

| parameters | { size: 3} | A Json object representing the parameters of the query |

| parameters | {size:3}|一个Json对象,表示查询的参数

SQL Queries (OrchardCore.Queries.Sql)

SQL查询(OrchardCore.Queries.Sql)

This feature provide a new type of query targeting the SQL database.

此功能提供了一种针对SQL数据库的新类型的查询。

Queries recipe step

查询配方步骤

Here is an example for creating a SQL query from a Queries recipe step:

以下是从查询配方步骤创建SQL查询的示例:


{

 <font color=#0099ff size=4 face="黑体">{</font> 


    "Source": "Sql",

    "Name": "ContentItems",

    "Template": "select * from ContentItemIndex", // json encoded query template

    "ReturnDocuments": false

}

 <font color=#0099ff size=4 face="黑体">}</font> 


Liquid templates

液体模板

You can access queries from liquid views and templates by using the Queries property. Queries are accessed by name, for example Queries.RecentBlogPosts.

您可以使用Queries属性从液体视图和模板访问查询。查询按名称访问,例如Queries.RecentBlogPosts

query

询问

The query filter provides a way to execute queries.

query过滤器提供了一种执行查询的方法。


{% assign recentBlogPosts = Queries.RecentBlogPosts | query %}

 <font color=#0099ff size=4 face="黑体">{%assign recentBlogPosts = Queries.RecentBlogPosts |查询%}</font> 


{% for item in recentBlogPosts %}

 <font color=#0099ff size=4 face="黑体">{%for recentBlogPosts%}中的项目</font> 


{{ item | display_text }}

 <font color=#0099ff size=4 face="黑体">{{item | display_text}}</font> 


{% endfor %}

 <font color=#0099ff size=4 face="黑体">{%endfor%}</font> 


The example above will iterate over all the results of the query name RecentBlogPosts and display the text representing the content item.

上面的示例将迭代查询名称“RecentBlogPosts”的所有结果,并显示表示内容项的文本。

Any available property on the results of the queries can be used. This example assumes the results will be content items.

可以使用查询结果的任何可用属性。此示例假定结果将是内容项。

Parameters

参数

The query filter allows you to pass in parameters to your paramterized queries. For example, a query called ContentItems that has two parameters (contentType and limit) can be called like this:

query过滤器允许您将参数传递给您的参数化查询。例如,一个名为ContentItems的查询有两个参数(contentTypelimit)可以像这样调用:


{% assign fiveBlogPosts = Queries.ContentItems | query: contentType: "BlogPost", limit: 5 %}

 <font color=#0099ff size=4 face="黑体">{%assign fiveBlogPosts = Queries.ContentItems |查询:contentType:“BlogPost”,限制:5%}</font> 


Razor Helpers

剃刀助手

The QueryAsync and ContentQueryAsync Orchard Helper extension methods (in the OrchardCore.Queries and OrchardCore.ContentManagement namespaces respectively) allow you to run queries directly from razor pages.

QueryAsyncContentQueryAsync Orchard Helper扩展方法(分别在OrchardCore.QueriesOrchardCore.ContentManagement命名空间中)允许您直接从razor页面运行查询。

You can use the DisplayAsync extension method (also in OrchardCore.ContentManagement) to display the content items returned from ContentQueryAsync.

您可以使用DisplayAsync扩展方法(也在OrchardCore.ContentManagement中)来显示从ContentQueryAsync返回的内容项。

For example, to run a query called LatestBlogPosts, and display the results:

例如,要运行名为LatestBlogPosts的查询,并显示结果:


@foreach (var contentItem in await OrchardCore.ContentQueryAsync("AllContent"))

 <font color=#0099ff size=4 face="黑体">@foreach(var contentItem in await OrchardCore.ContentQueryAsync(“AllContent”))</font> 


{

 <font color=#0099ff size=4 face="黑体">{</font> 


    @await Orchard.DisplayAsync(contentItem)

}

 <font color=#0099ff size=4 face="黑体">}</font> 


The Razor Helper is accessible on the Orchard property if the view is using Orchard Core's Razor base class, or by injecting OrchardCore.IOrchardHelper in all other cases.

如果视图使用Orchard Core的Razor基类,或者在所有其他情况下注入OrchardCore.IOrchardHelper,则可以在Orchard属性访问Razor Helper。

Executing SQL Queries

执行SQL查询

RDBMS support

RDBMS支持

Because RDMBS vendors support different SQL flavors this module will analyze the query you defined and render a specific one based on the RDBMS that is used.

由于RDMBS供应商支持不同的SQL风格,因此该模块将分析您定义的查询,并根据使用的RDBMS呈现特定的查询。

This also allows the queries to be exported and shared across website instances even if they run on different RDBMS.

这也允许在网站实例中导出和共享查询,即使它们在不同的RDBMS上运行。

Examples

例子

Here is an example of a query that returns all published Blog Posts:

以下是返回所有已发布的博客帖子的查询示例:


    select DocumentId

    from ContentItemIndex 

    where Published = true and ContentType = 'BlogPost'

By selecting the "Return documents" options, the content items associated with the resulting DocumentId values are loaded.

通过选择“返回文档”选项,将加载与生成的“DocumentId”值相关联的内容项。

The example below returns a custom set of values instead of content items:

以下示例返回一组自定义值而不是内容项:


select 

 <font color=#0099ff size=4 face="黑体">选择</font> 


    month(CreatedUtc) as [Month], 

    year(CreatedUtc) as [Year],

    day(CreatedUtc) as [Day],

    count(*) as [Count]

from ContentItemIndex 

 <font color=#0099ff size=4 face="黑体">来自ContentItemIndex</font> 


where Published = true and ContentType = 'BlogPost'

 <font color=#0099ff size=4 face="黑体">其中Published = true且ContentType ='BlogPost'</font> 


group by day(CreatedUtc), month(CreatedUtc), year(CreatedUtc)

 <font color=#0099ff size=4 face="黑体">按天分组(CreatedUtc),月份(CreatedUtc),年份(CreatedUtc)</font> 


Parameters

参数

Parameters can be provided when running queries.

运行查询时可以提供参数。

Parameters are safe to use as they will always be parsed before being included in a query.

参数可以安全使用,因为它们在被包含在查询中之前将始终被解析。

The syntax of a parameter is

参数的语法是

@name:default_value

Where name is the name of the parameter, and default_value an expression (usually a literal) to use in case

name是参数的名称,default_value是一个表达式(通常是文字),用于案例

the parameter is not defined.

参数未定义。

The following example loads the document ids for a parameterized content type.

以下示例为参数化内容类型加载文档ID。


select DocumentId

 <font color=#0099ff size=4 face="黑体">选择DocumentId</font> 


from ContentItemIndex 

 <font color=#0099ff size=4 face="黑体">来自ContentItemIndex</font> 


where Published = true and ContentType = @contenttype:'BlogPost'

 <font color=#0099ff size=4 face="黑体">其中Published = true且ContentType = @contenttype:'BlogPost'</font> 


If the contenttype parameter is not passed when the query is invoked, then the default value is used.

如果在调用查询时未传递contenttype参数,则使用默认值。

Parameter names are case-sensitive.

参数名称区分大小写。

Templates

模板

A SQL query is actually a Liquid template. This allows your queries to be shaped based on the parameters it gets.

SQL查询实际上是一个Liquid模板。这允许您的查询根据其获得的参数进行整形。

When injecting user-provided values, be sure to encode these such that they can't be exploited.

注入用户提供的值时,请务必对其进行编码,使其无法被利用。

It is recommended to use parameters to inject values in the queries, and only use Liquid templates to change the shape of the query.

建议使用参数在查询中注入值,并仅使用Liquid模板更改查询的形状。

This example checks that a limit parameter is provided and if so uses it:

此示例检查是否提供了limit参数,如果是,则使用它:


{% if limit > 0 %}

 <font color=#0099ff size=4 face="黑体">{%if limit> 0%}</font> 


    select ... limit @limit

{% else %}

 <font color=#0099ff size=4 face="黑体">{%else%}</font> 


    select ... 

{% endif %}

 <font color=#0099ff size=4 face="黑体">{% 万一 %}</font> 


Paging

分页

Use LIMIT [number] and OFFSET [number] to define paged results.

使用LIMIT [number]OFFSET [number]来定义分页结果。

These statements will be converted automatically based on the RDBMS in use.

这些语句将根据使用的RDBMS自动转换。

Helper functions

助手功能

The SQL parser is also able to convert some specific functions to the intended dialect.

SQL解析器还能够将某些特定函数转换为预期的方言。

| Name | Description |

|名称|说明|

| ---------------- |----------------------------------- |

| ---------------- | --------------------------------- - |

| second(_date_) | Returns the seconds part of a date |

| second(_date_)|返回日期的秒部分

| minute(_date_) | Returns the minutes part of a date |

| minute(_date_)|返回日期的分钟部分

| hour(_date_) | Returns the hours part of a date |

| hour(_date_)|返回日期的小时部分

| day(_date_) | Returns the days part of a date |

| day(_date_)|返回日期的日期部分

| month(_date_) | Returns the months part of a date |

| 月(_date_)|返回日期的月份部分

| year(_date_) | Returns the years part of a date |

| 年(_date_)|返回日期的年份部分