Class QueryEngine<TData>
A QueryEngine defines how to build a query from an input string. It can be customized to support custom filters and operators.
Namespace: Unity.QuickSearch
Syntax
public class QueryEngine<TData>
Type Parameters
Name | Description |
---|---|
TData | The filtered data type. |
Constructors
QueryEngine()
Construct a new QueryEngine.
Declaration
public QueryEngine()
QueryEngine(Boolean)
Construct a new QueryEngine.
Declaration
public QueryEngine(bool validateFilters)
Parameters
Type | Name | Description |
---|---|---|
Boolean | validateFilters | Indicates if the engine must validate filters when parsing the query. |
QueryEngine(QueryValidationOptions)
Construct a new QueryEngine with the specified validation options.
Declaration
public QueryEngine(QueryValidationOptions validationOptions)
Parameters
Type | Name | Description |
---|---|---|
QueryValidationOptions | validationOptions | The validation options to use in this engine. |
Properties
globalStringComparison
Global string comparison options for word matching and filter handling (if not overridden).
Declaration
public StringComparison globalStringComparison { get; }
Property Value
Type | Description |
---|---|
StringComparison |
searchDataCallback
The callback used to get the data to match to the search words.
Declaration
public Func<TData, IEnumerable<string>> searchDataCallback { get; }
Property Value
Type | Description |
---|---|
Func<TData, IEnumerable<String>> |
searchDataOverridesStringComparison
Indicates if word/phrase matching uses searchDataStringComparison or not.
Declaration
public bool searchDataOverridesStringComparison { get; }
Property Value
Type | Description |
---|---|
Boolean |
searchDataStringComparison
String comparison options for word/phrase matching.
Declaration
public StringComparison searchDataStringComparison { get; }
Property Value
Type | Description |
---|---|
StringComparison |
skipUnknownFilters
Boolean indicating if unknown filters should be skipped. If validateFilters is true and skipUnknownFilters is false, unknown filters will generate errors if no default handler is provided.
Declaration
public bool skipUnknownFilters { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
validateFilters
Get of set if the engine must validate filters when parsing the query. Defaults to true.
Declaration
public bool validateFilters { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Methods
AddFilter<TFilter>(String, Func<TData, TFilter>, String[])
Add a new custom filter.
Declaration
public void AddFilter<TFilter>(string token, Func<TData, TFilter> getDataFunc, string[] supportedOperatorType = null)
Parameters
Type | Name | Description |
---|---|---|
String | token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
Func<TData, TFilter> | getDataFunc | Callback used to get the object that is used in the filter. Takes an object of type TData and returns an object of type TFilter. |
String[] | supportedOperatorType | List of supported operator tokens. Null for all operators. |
Type Parameters
Name | Description |
---|---|
TFilter | The type of the data that is compared by the filter. |
AddFilter<TFilter>(String, Func<TData, TFilter>, StringComparison, String[])
Add a new custom filter.
Declaration
public void AddFilter<TFilter>(string token, Func<TData, TFilter> getDataFunc, StringComparison stringComparison, string[] supportedOperatorType = null)
Parameters
Type | Name | Description |
---|---|---|
String | token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
Func<TData, TFilter> | getDataFunc | Callback used to get the object that is used in the filter. Takes an object of type TData and returns an object of type TFilter. |
StringComparison | stringComparison | String comparison options. |
String[] | supportedOperatorType | List of supported operator tokens. Null for all operators. |
Type Parameters
Name | Description |
---|---|
TFilter | The type of the data that is compared by the filter. |
AddFilter<TFilter>(String, Func<TData, String, TFilter, Boolean>, String[])
Add a new custom filter with a custom resolver. Useful when you wish to handle all operators yourself.
Declaration
public void AddFilter<TFilter>(string token, Func<TData, string, TFilter, bool> filterResolver, string[] supportedOperatorType = null)
Parameters
Type | Name | Description |
---|---|---|
String | token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
Func<TData, String, TFilter, Boolean> | filterResolver | Callback used to handle any operators for this filter. Takes an object of type TData, the operator token and the filter value, and returns a boolean indicating if the filter passed or not. |
String[] | supportedOperatorType | List of supported operator tokens. Null for all operators. |
Type Parameters
Name | Description |
---|---|
TFilter | The type of the data that is compared by the filter. |
AddFilter<TParam, TFilter>(String, Func<TData, TParam, TFilter>, Func<String, TParam>, String[])
Add a new custom filter function.
Declaration
public void AddFilter<TParam, TFilter>(string token, Func<TData, TParam, TFilter> getDataFunc, Func<string, TParam> parameterTransformer, string[] supportedOperatorType = null)
Parameters
Type | Name | Description |
---|---|---|
String | token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
Func<TData, TParam, TFilter> | getDataFunc | Callback used to get the object that is used in the filter. Takes an object of type TData and TParam, and returns an object of type TFilter. |
Func<String, TParam> | parameterTransformer | Callback used to convert a string to the type TParam. Used when parsing the query to convert what is passed to the function into the correct format. |
String[] | supportedOperatorType | List of supported operator tokens. Null for all operators. |
Type Parameters
Name | Description |
---|---|
TParam | The type of the constant parameter passed to the function. |
TFilter | The type of the data that is compared by the filter. |
AddFilter<TParam, TFilter>(String, Func<TData, TParam, TFilter>, Func<String, TParam>, StringComparison, String[])
Add a new custom filter function.
Declaration
public void AddFilter<TParam, TFilter>(string token, Func<TData, TParam, TFilter> getDataFunc, Func<string, TParam> parameterTransformer, StringComparison stringComparison, string[] supportedOperatorType = null)
Parameters
Type | Name | Description |
---|---|---|
String | token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
Func<TData, TParam, TFilter> | getDataFunc | Callback used to get the object that is used in the filter. Takes an object of type TData and TParam, and returns an object of type TFilter. |
Func<String, TParam> | parameterTransformer | Callback used to convert a string to the type TParam. Used when parsing the query to convert what is passed to the function into the correct format. |
StringComparison | stringComparison | String comparison options. |
String[] | supportedOperatorType | List of supported operator tokens. Null for all operators. |
Type Parameters
Name | Description |
---|---|
TParam | The type of the constant parameter passed to the function. |
TFilter | The type of the data that is compared by the filter. |
AddFilter<TParam, TFilter>(String, Func<TData, TParam, TFilter>, String[])
Add a new custom filter function.
Declaration
public void AddFilter<TParam, TFilter>(string token, Func<TData, TParam, TFilter> getDataFunc, string[] supportedOperatorType = null)
Parameters
Type | Name | Description |
---|---|---|
String | token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
Func<TData, TParam, TFilter> | getDataFunc | Callback used to get the object that is used in the filter. Takes an object of type TData and TParam, and returns an object of type TFilter. |
String[] | supportedOperatorType | List of supported operator tokens. Null for all operators. |
Type Parameters
Name | Description |
---|---|
TParam | The type of the constant parameter passed to the function. |
TFilter | The type of the data that is compared by the filter. |
AddFilter<TParam, TFilter>(String, Func<TData, TParam, TFilter>, StringComparison, String[])
Add a new custom filter function.
Declaration
public void AddFilter<TParam, TFilter>(string token, Func<TData, TParam, TFilter> getDataFunc, StringComparison stringComparison, string[] supportedOperatorType = null)
Parameters
Type | Name | Description |
---|---|---|
String | token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
Func<TData, TParam, TFilter> | getDataFunc | Callback used to get the object that is used in the filter. Takes an object of type TData and TParam, and returns an object of type TFilter. |
StringComparison | stringComparison | String comparison options. |
String[] | supportedOperatorType | List of supported operator tokens. Null for all operators. |
Type Parameters
Name | Description |
---|---|
TParam | The type of the constant parameter passed to the function. |
TFilter | The type of the data that is compared by the filter. |
AddFilter<TParam, TFilter>(String, Func<TData, TParam, String, TFilter, Boolean>, Func<String, TParam>, String[])
Add a new custom filter function with a custom resolver. Useful when you wish to handle all operators yourself.
Declaration
public void AddFilter<TParam, TFilter>(string token, Func<TData, TParam, string, TFilter, bool> filterResolver, Func<string, TParam> parameterTransformer, string[] supportedOperatorType = null)
Parameters
Type | Name | Description |
---|---|---|
String | token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
Func<TData, TParam, String, TFilter, Boolean> | filterResolver | Callback used to handle any operators for this filter. Takes an object of type TData, an object of type TParam, the operator token and the filter value, and returns a boolean indicating if the filter passed or not. |
Func<String, TParam> | parameterTransformer | Callback used to convert a string to the type TParam. Used when parsing the query to convert what is passed to the function into the correct format. |
String[] | supportedOperatorType | List of supported operator tokens. Null for all operators. |
Type Parameters
Name | Description |
---|---|
TParam | The type of the constant parameter passed to the function. |
TFilter | The type of the data that is compared by the filter. |
AddFilter<TParam, TFilter>(String, Func<TData, TParam, String, TFilter, Boolean>, String[])
Add a new custom filter function with a custom resolver. Useful when you wish to handle all operators yourself.
Declaration
public void AddFilter<TParam, TFilter>(string token, Func<TData, TParam, string, TFilter, bool> filterResolver, string[] supportedOperatorType = null)
Parameters
Type | Name | Description |
---|---|---|
String | token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
Func<TData, TParam, String, TFilter, Boolean> | filterResolver | Callback used to handle any operators for this filter. Takes an object of type TData, an object of type TParam, the operator token and the filter value, and returns a boolean indicating if the filter passed or not. |
String[] | supportedOperatorType | List of supported operator tokens. Null for all operators. |
Type Parameters
Name | Description |
---|---|
TParam | The type of the constant parameter passed to the function. |
TFilter | The type of the data that is compared by the filter. |
AddFiltersFromAttribute<TFilterAttribute, TTransformerAttribute>()
Add all custom filters that are identified with the method attribute TAttribute.
Declaration
public void AddFiltersFromAttribute<TFilterAttribute, TTransformerAttribute>()
where TFilterAttribute : QueryEngineFilterAttribute where TTransformerAttribute : QueryEngineParameterTransformerAttribute
Type Parameters
Name | Description |
---|---|
TFilterAttribute | The type of the attribute of filters to fetch. |
TTransformerAttribute | The attribute type for the parameter transformers associated with the filter attribute. |
AddNestedQueryAggregator<TNestedQueryData>(String, Func<IEnumerable<TNestedQueryData>, IEnumerable<TNestedQueryData>>)
Add a new nested query aggregator. An aggregator is an operation that can be applied on a nested query to aggregate the results of the nested query according to a certain criteria.
Declaration
public void AddNestedQueryAggregator<TNestedQueryData>(string token, Func<IEnumerable<TNestedQueryData>, IEnumerable<TNestedQueryData>> aggregator)
Parameters
Type | Name | Description |
---|---|---|
String | token | Name of the aggregator used when typing the query. This name will be lowercased when parsing the query to speedup the process. |
Func<IEnumerable<TNestedQueryData>, IEnumerable<TNestedQueryData>> | aggregator | Aggregator function. Takes the results of the nested query, and returns an aggregate that contains any number of items. |
Type Parameters
Name | Description |
---|---|
TNestedQueryData | The type of data returned by the nested query. |
AddOperator(String)
Add a custom filter operator.
Declaration
public void AddOperator(string op)
Parameters
Type | Name | Description |
---|---|---|
String | op | The operator identifier. |
AddOperatorHandler<TFilterVariable, TFilterConstant>(String, Func<TFilterVariable, TFilterConstant, Boolean>)
Add a custom filter operator handler.
Declaration
public void AddOperatorHandler<TFilterVariable, TFilterConstant>(string op, Func<TFilterVariable, TFilterConstant, bool> handler)
Parameters
Type | Name | Description |
---|---|---|
String | op | The filter operator. |
Func<TFilterVariable, TFilterConstant, Boolean> | handler | Callback to handle the operation. Takes a TFilterVariable (value returned by the filter handler, will vary for each element) and a TFilterConstant (right hand side value of the operator, which is constant), and returns a boolean indicating if the filter passes or not. |
Type Parameters
Name | Description |
---|---|
TFilterVariable | The operator's left hand side type. This is the type returned by a filter handler. |
TFilterConstant | The operator's right hand side type. |
AddOperatorHandler<TFilterVariable, TFilterConstant>(String, Func<TFilterVariable, TFilterConstant, StringComparison, Boolean>)
Add a custom filter operator handler.
Declaration
public void AddOperatorHandler<TFilterVariable, TFilterConstant>(string op, Func<TFilterVariable, TFilterConstant, StringComparison, bool> handler)
Parameters
Type | Name | Description |
---|---|---|
String | op | The filter operator. |
Func<TFilterVariable, TFilterConstant, StringComparison, Boolean> | handler | Callback to handle the operation. Takes a TFilterVariable (value returned by the filter handler, will vary for each element), a TFilterConstant (right hand side value of the operator, which is constant), a StringComparison option and returns a boolean indicating if the filter passes or not. |
Type Parameters
Name | Description |
---|---|
TFilterVariable | The operator's left hand side type. This is the type returned by a filter handler. |
TFilterConstant | The operator's right hand side type. |
AddTypeParser<TFilterConstant>(Func<String, ParseResult<TFilterConstant>>)
Add a type parser that parse a string and returns a custom type. Used by custom operator handlers.
Declaration
public void AddTypeParser<TFilterConstant>(Func<string, ParseResult<TFilterConstant>> parser)
Parameters
Type | Name | Description |
---|---|---|
Func<String, ParseResult<TFilterConstant>> | parser | Callback used to determine if a string can be converted into TFilterConstant. Takes a string and returns a ParseResult object. This contains the success flag, and the actual converted value if it succeeded. |
Type Parameters
Name | Description |
---|---|
TFilterConstant | The type of the parsed operand that is on the right hand side of the operator. |
Parse(String)
Parse a query string into a Query operation. This Query operation can then be used to filter any data set of type TData.
Declaration
public Query<TData> Parse(string text)
Parameters
Type | Name | Description |
---|---|---|
String | text | The query input string. |
Returns
Type | Description |
---|---|
Query<TData> | Query operation of type TData. |
RemoveFilter(String)
Remove a custom filter.
Declaration
public void RemoveFilter(string token)
Parameters
Type | Name | Description |
---|---|---|
String | token | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
Remarks
You will get a warning if you try to remove a non existing filter.
SetDefaultFilter(Func<TData, String, String, String, Boolean>)
Set the default filter handler for filters that were not registered.
Declaration
public void SetDefaultFilter(Func<TData, string, string, string, bool> handler)
Parameters
Type | Name | Description |
---|---|---|
Func<TData, String, String, String, Boolean> | handler | Callback used to handle the filter. Takes an object of type TData, the filter identifier, the operator and the filter value, and returns a boolean indicating if the filter passed or not. |
SetDefaultParamFilter(Func<TData, String, String, String, String, Boolean>)
Set the default filter handler for function filters that were not registered.
Declaration
public void SetDefaultParamFilter(Func<TData, string, string, string, string, bool> handler)
Parameters
Type | Name | Description |
---|---|---|
Func<TData, String, String, String, String, Boolean> | handler | Callback used to handle the function filter. Takes an object of type TData, the filter identifier, the parameter, the operator and the filter value, and returns a boolean indicating if the filter passed or not. |
SetFilterNestedQueryTransformer<TNestedQueryData, TRhs>(String, Func<TNestedQueryData, TRhs>)
Set a filter's nested query transformer function. This function takes the result of a nested query and extract the necessary data to compare with the filter.
Declaration
public void SetFilterNestedQueryTransformer<TNestedQueryData, TRhs>(string filterToken, Func<TNestedQueryData, TRhs> transformer)
Parameters
Type | Name | Description |
---|---|---|
String | filterToken | The identifier of the filter. Typically what precedes the operator in a filter (i.e. "id" in "id>=2"). |
Func<TNestedQueryData, TRhs> | transformer | The transformer function. |
Type Parameters
Name | Description |
---|---|
TNestedQueryData | The type of data returned by the nested query. |
TRhs | The type expected on the right hand side of the filter. |
SetGlobalStringComparisonOptions(StringComparison)
Set global string comparison options. Used for word matching and filter handling (unless overridden by filter).
Declaration
public void SetGlobalStringComparisonOptions(StringComparison stringComparison)
Parameters
Type | Name | Description |
---|---|---|
StringComparison | stringComparison | String comparison options. |
SetNestedQueryHandler<TNestedQueryData>(Func<String, String, IEnumerable<TNestedQueryData>>)
Set the function that will handle nested queries. Only one handler can be set.
Declaration
public void SetNestedQueryHandler<TNestedQueryData>(Func<string, string, IEnumerable<TNestedQueryData>> handler)
Parameters
Type | Name | Description |
---|---|---|
Func<String, String, IEnumerable<TNestedQueryData>> | handler | The function that handles nested queries. It receives the nested query and the filter token on which the query is applied, and returns an IEnumerable. |
Type Parameters
Name | Description |
---|---|
TNestedQueryData | The type of data returned by the nested query. |
SetSearchDataCallback(Func<TData, IEnumerable<String>>)
Set the callback to be used to fetch the data that will be matched against the search words.
Declaration
public void SetSearchDataCallback(Func<TData, IEnumerable<string>> getSearchDataCallback)
Parameters
Type | Name | Description |
---|---|---|
Func<TData, IEnumerable<String>> | getSearchDataCallback | Callback used to get the data to be matched against the search words. Takes an object of type TData and return an IEnumerable of strings. |
SetSearchDataCallback(Func<TData, IEnumerable<String>>, Func<String, String>, StringComparison)
Set the callback to be used to fetch the data that will be matched against the search words, plus a transformer on those words.
Declaration
public void SetSearchDataCallback(Func<TData, IEnumerable<string>> getSearchDataCallback, Func<string, string> searchWordTransformerCallback, StringComparison stringComparison)
Parameters
Type | Name | Description |
---|---|---|
Func<TData, IEnumerable<String>> | getSearchDataCallback | Callback used to get the data to be matched against the search words. Takes an object of type TData and return an IEnumerable of strings. |
Func<String, String> | searchWordTransformerCallback | Callback used to transform a search word during the query parsing. Useful when doing lowercase or uppercase comparison. Can return null or an empty string to remove the word from the query. |
StringComparison | stringComparison | String comparison options. |
SetSearchDataCallback(Func<TData, IEnumerable<String>>, StringComparison)
Set the callback to be used to fetch the data that will be matched against the search words.
Declaration
public void SetSearchDataCallback(Func<TData, IEnumerable<string>> getSearchDataCallback, StringComparison stringComparison)
Parameters
Type | Name | Description |
---|---|---|
Func<TData, IEnumerable<String>> | getSearchDataCallback | Callback used to get the data to be matched against the search words. Takes an object of type TData and return an IEnumerable of strings. |
StringComparison | stringComparison | String comparison options. |