Class Query
- Namespace
- YndigoBlue.Velocity.Model
- Assembly
- YndigoBlue.Velocity.dll
Represents a SQL SELECT query with support for filtering, joining, grouping, ordering, and unions.
public class Query : ISelectItem, IFromItem, IElement
- Inheritance
-
Query
- Implements
Examples
Basic query with filtering and ordering:
Query query = new Query();
query.AddFromItem(usersTable);
query.AddSelectItem(usersTable["username"]);
query.AddSelectItem(usersTable["email"]);
query.AddSelectItem(usersTable["age"]);
query.AddFilter(usersTable["status"], "active");
query.AddFilter(usersTable["age"], ConditionalType.GreaterThan, 18);
query.AddOrderClause(new OrderClause(usersTable["username"], OrderClauseType.Ascending));
using (ResultSet results = manager.Retrieve(query))
{
foreach (Result row in results)
{
Console.WriteLine(row.GetFieldString("username"));
}
}
Fluent API for query building:
Query query = Query.Create()
.From(usersTable)
.Select(usersTable["username"])
.Select(usersTable["email"])
.Where(new Criterion<string>(usersTable["status"], "active"))
.OrderBy(usersTable["username"]);
using (ResultSet results = manager.Retrieve(query))
{
// Process results
}
Query with JOIN:
Join join = new Join(ordersTable, usersTable, "user_id", JoinType.Inner);
Query query = new Query();
query.AddFromItem(join);
query.AddSelectItem(usersTable["username"]);
query.AddSelectItem(ordersTable["order_date"]);
query.AddSelectItem(ordersTable["total"]);
query.AddFilter(ordersTable["total"], ConditionalType.GreaterThan, 100m);
// SQL: SELECT users.username, orders.order_date, orders.total
// FROM orders
// INNER JOIN users ON orders.user_id = users.user_id
// WHERE orders.total > 100
Query with GROUP BY and aggregate functions:
Query query = new Query();
query.AddFromItem(ordersTable);
query.AddSelectItem(ordersTable["user_id"]);
Function countFunc = new Function(FunctionType.Count, ordersTable["id"]);
countFunc.SetAlias("order_count");
query.AddSelectItem(countFunc);
Function sumFunc = new Function(FunctionType.Sum, ordersTable["total"]);
sumFunc.SetAlias("total_spent");
query.AddSelectItem(sumFunc);
query.AddGroupByItem(ordersTable["user_id"]);
Criterion<int> havingCriterion = new Criterion<int>(
countFunc,
ConditionalType.GreaterThan,
5
);
query.SetHavingFilter(new Filter(havingCriterion));
// SQL: SELECT user_id, COUNT(id) AS order_count, SUM(total) AS total_spent
// FROM orders
// GROUP BY user_id
// HAVING COUNT(id) > 5
Subquery in filter:
// Find users with above-average age
Query avgAgeQuery = new Query();
avgAgeQuery.AddFromItem(usersTable);
avgAgeQuery.AddSelectItem(new Function(FunctionType.Avg, usersTable["age"]));
Criterion<Query> ageCriterion = new Criterion<Query>(
usersTable["age"],
ConditionalType.GreaterThan,
avgAgeQuery
);
Query mainQuery = new Query();
mainQuery.AddFromItem(usersTable);
mainQuery.AddSelectItem(usersTable["username"]);
mainQuery.AddSelectItem(usersTable["age"]);
mainQuery.SetWhereFilter(new Filter(ageCriterion));
// SQL: SELECT username, age FROM users
// WHERE age > (SELECT AVG(age) FROM users)
Remarks
Query provides a fluent, type-safe API for building SELECT statements that work across all supported database systems. Queries can be used for data retrieval (Manager.Retrieve or Manager.Search), subqueries in filters, or subqueries in record field values.
The Query class supports:
- SELECT with specific columns or SELECT *
- FROM with tables or joins
- WHERE and HAVING clauses with complex filters
- ORDER BY with multiple columns
- GROUP BY with aggregate functions
- UNION, UNION ALL, INTERSECT, EXCEPT operations
- DISTINCT, TOP/LIMIT, and OFFSET clauses
Constructors
- Query()
Creates a new empty query.
Fields
Properties
- DataType
Gets the data type of the values returned by this select item.
- FromItems
A Stack representing all the IFromItems in a query. Represents all the items in the FROM clause
- Name
Gets the name of the select item as it appears in the result set.
- Precision
Gets the precision for numeric data types.
- Scale
Gets the scale for numeric data types.
- SelectItems
Gets the collection of select items (columns or expressions) available from this FROM item.
- Size
Gets the maximum size for string or binary data types.
- Unions
A list of all the unions in the query
Methods
- Create()
Creates a new empty query. This is a factory method for fluent query construction.
- From(IEnumerable<IFromItem>)
Sets the FROM clause to the specified tables or joins. Fluent API method.
- From(IFromItem)
Sets the FROM clause to the specified table or join. Fluent API method.
- GetSelectItem(string)
Retrieves the select item with the specified name.
- GroupBy(IEnumerable<IGroupItem>)
Adds multiple GROUP BY clauses. Fluent API method.
- GroupBy(IGroupItem)
Adds a GROUP BY clause. Fluent API method.
- HasSelectItem(string)
Determines whether this FROM item contains a select item with the specified name.
- HasSelectItem(ISelectItem)
Determines whether this FROM item contains the specified select item.
- Having(IEnumerable<IFilterItem>)
Sets the HAVING clause from multiple filter items. Fluent API method.
- Having(IFilterItem)
Sets the HAVING clause to a single filter item. Fluent API method.
- Having(Filter)
Sets the HAVING clause for filtering grouped results. Fluent API method.
- OrderBy(IEnumerable<ISelectItem>)
Adds multiple ORDER BY clauses with ascending sort order. Fluent API method.
- OrderBy(IEnumerable<OrderClause>)
Adds multiple ORDER BY clauses. Fluent API method.
- OrderBy(ISelectItem)
Adds an ORDER BY clause with ascending sort order. Fluent API method.
- OrderBy(ISelectItem, OrderClauseType)
Adds an ORDER BY clause with specified sort order. Fluent API method.
- OrderBy(OrderClause)
Adds an ORDER BY clause. Fluent API method.
- Select(IEnumerable<ISelectItem>)
Adds multiple columns or expressions to the SELECT clause. Fluent API method.
- Select(ISelectItem)
Adds a column or expression to the SELECT clause. Fluent API method.
- SelectAll(IEnumerable<IFromItem>)
Configures the query to select all columns from the specified tables or joins.
- SelectAll(IFromItem)
Configures the query to select all columns from the specified table or join.
- Where(IEnumerable<IFilterItem>)
Sets the WHERE clause from multiple filter items. Fluent API method.
- Where(IFilterItem)
Sets the WHERE clause to a single filter item (criterion or nested filter). Fluent API method.
- Where(Filter)
Sets the WHERE clause to the specified filter. Fluent API method.