0% found this document useful (0 votes)
8 views9 pages

EntitySpaces Query API Overview

The document describes how to write queries using the EntitySpaces Query API. It provides examples of basic select queries, using default and custom conjunctions, aggregates, grouping, ordering, operators, and mixing AND/OR clauses. It also discusses simplifying queries by caching the query object in a local variable or adding a custom method.

Uploaded by

adikusdianto
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

EntitySpaces Query API Overview

The document describes how to write queries using the EntitySpaces Query API. It provides examples of basic select queries, using default and custom conjunctions, aggregates, grouping, ordering, operators, and mixing AND/OR clauses. It also discusses simplifying queries by caching the query object in a local variable or adding a custom method.

Uploaded by

adikusdianto
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EntitySpaces Query API TM

Basic Select
Query
AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link]();

Yields
SELECT * FROM [AggregateTest]

Default Conjunction
By default "AND" is used, but that can be overridden.

Query
EmployeesCollection emps = new EmployeesCollection();
[Link]([Link]("A%"),
[Link]("A%"));
[Link]();

Yields
SELECT * FROM [Employees]
WHERE ([LastName] LIKE @LastName1 AND [FirstName] LIKE @FirstName2)

Query - Sets the DefaultConjunction to "OR"


EmployeesCollection emps = new EmployeesCollection();
[Link] = [Link];
[Link]([Link]("A%"),
[Link]("A%"));
[Link]();

Yields
SELECT * FROM [Employees]
WHERE ([LastName] LIKE @LastName1 OR [FirstName] LIKE @FirstName2)
Count
Query
AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link]
(
[Link]("Count")
);
[Link]();

Yields
SELECT COUNT([Salary]) AS 'Count' FROM [AggregateTest]

Query
AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link] = true;
[Link] = "Total";
[Link]();

Yields
SELECT COUNT(*) AS 'Total' FROM [AggregateTest]

Aggregates
Avg, Count, Min, Max, Sum, StdDev, and Var) all share the same syntax.

Query - Aggregate with Empty Alias uses ColumnName


AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link]
(
[Link]()
);
[Link]();

Yields
SELECT SUM([Salary]) AS 'Salary' FROM [AggregateTest]

Query - Simple Aggregate with Alias


AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link]
(
[Link]("Avg")
);
[Link]();

Yields
SELECT AVG([Salary]) AS 'Avg' FROM [AggregateTest]
Query - Two Aggregates
AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link]
(
[Link]("Sum"),
[Link]("Min")
);
[Link]();

Yields
SELECT SUM([Salary]) AS 'Sum',MIN([Salary]) AS 'Min'
FROM [AggregateTest]

Query - Aggregate with Distinct


AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link]
(
[Link]("Count", true)
);
[Link]();

Yields
SELECT COUNT(DISTINCT [LastName]) AS 'Count'
FROM [AggregateTest]

Query - Aggregate with Count


AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link]
(
[Link]("Sum")
);
[Link] = true;
[Link] = "Total";
[Link]();

Yields
SELECT SUM([Salary]) AS 'Sum' ,COUNT(*) AS 'Total'
FROM [AggregateTest]
Query - Aggregate with Where clause
AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link] = true;
[Link] = "Total";
[Link]
(
[Link]("true")
);
[Link]();

Yields
SELECT COUNT(*) AS 'Total'
FROM [AggregateTest]
WHERE ([IsActive] = @IsActive1)
Group By
Query
AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link] = true;
[Link] = "Count";
[Link]
.Select ([Link])
.GroupBy([Link]);
[Link]();

Yields
SELECT [IsActive] ,COUNT(*) AS 'Count'
FROM [AggregateTest]
GROUP BY [IsActive]

Query - With two GroupBy's and a Where clause


AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link] = true;
[Link]
.Select
(
[Link],
[Link]
)
.Where
(
[Link](true)
)
.GroupBy
(
[Link],
[Link]
);
[Link]();

Yields
SELECT [IsActive],[DepartmentID] ,COUNT(*) AS 'Count'
FROM [AggregateTest]
WHERE ([IsActive] = @IsActive1 )
GROUP BY [IsActive],[DepartmentID]
Query - Select, Where, GroupBy, and OrderBy
AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link] = true;
[Link]
.Select ([Link],
[Link])
.Where ([Link](true))
.GroupBy([Link],
[Link])
.OrderBy([Link],
[Link]);
[Link]();

Yields
SELECT [IsActive],[DepartmentID] ,COUNT(*) AS 'Count'
FROM [AggregateTest]
WHERE ([IsActive] = @IsActive1 )
GROUP BY [IsActive],[DepartmentID]
ORDER BY [DepartmentID] ASC,[IsActive] ASC

Query - Select, Where, GroupBy WithRollup, and OrderBy


AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link] = true;
[Link]
.Select ([Link],
[Link])
.Where ([Link](true))
.GroupBy([Link],
[Link])
.OrderBy([Link],
[Link]);
[Link] = true;
[Link]();

Yields
SELECT [IsActive],[DepartmentID] ,COUNT(*) AS 'Count'
FROM [AggregateTest]
WHERE ([IsActive] = @IsActive1 )
GROUP BY [IsActive],[DepartmentID] WITH ROLLUP
ORDER BY [DepartmentID] ASC,[IsActive] ASC
Operators in Queries
Query
AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link]([Link] == true);
[Link]();

Yields
SELECT * FROM [AggregateTest] WHERE ([IsActive] = @IsActive1)

Query
AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link]([Link] >= 30.00);
[Link]();

Yields
SELECT * FROM [AggregateTest] WHERE ([Salary] >= @Salary1)

Distinct and Top in Queries


Query – Distinct Rows
AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link]
(
[Link],
[Link]
);
[Link] = true;
[Link]();

Yields
SELECT DISTINCT [LastName],[FirstName] FROM [AggregateTest]

Query – Top 5
AggregateTestCollection aggTestColl = new AggregateTestCollection();
[Link]([Link]);
[Link] = 5;
[Link]();

Yields
SELECT TOP 5 * FROM [AggregateTest] ORDER BY [Salary] DESC
Mixing AND / OR in Queries
Query
EmployeesCollection emps = new EmployeesCollection();
[Link]
.Select
(
[Link],
[Link],
[Link]
)
.Where
(
[Link]
(
[Link]("%A%"),
[Link]("%O%")
),
[Link]("1/1/1940", "1/1/2006")
)
.OrderBy
(
[Link],
[Link]
);
[Link]();

Yields
SELECT [EmployeeID],[FirstName],[LastName]
FROM [Employees]
WHERE (([LastName] LIKE @LastName1 OR [LastName] LIKE @LastName2)
AND [BirthDate] BETWEEN @BirthDate3 AND @BirthDate4)
ORDER BY [LastName] DESC,[FirstName] ASC
Simplifying Queries
Let's simplify the query above to something more readable. Of course, the queries below yield
the same sql so it's not re-listed.

Query - Cache the Query object in a local variable


EmployeesCollection emps = new EmployeesCollection();
EmployeesQuery q = [Link];
[Link]([Link], [Link], [Link])
.Where
(
[Link]([Link]("%A%"), [Link]("%O%")),
[Link]("1/1/1940", "1/1/2006")
)
.OrderBy([Link], [Link]);
[Link]();

Query - Better yet, add a method directly to your concrete query class.
public class EmployeesQuery : esEmployeesQuery
{
public bool CustomLoad()
{
Select(EmployeeID, FirstName, LastName, TitleOfCourtesy)
.Where
(
Or([Link]("%A%"), [Link]("%O%")),
[Link]("1/1/1940", "1/1/2006")
)
.OrderBy([Link], [Link]);
return [Link]();
}
}

Common questions

Powered by AI

EntitySpaces allows for improved readability of complex WHERE clauses by using a caching technique where queries are saved into a local variable, enabling more straightforward and clearer expressions. Additionally, creating custom methods in query classes can encapsulate conditions. For instance, complex conditions using both 'AND' and 'OR' are simplified by using query object caching or method encapsulation, helping manage logical operations in a more readable manner .

Implementing entity-based query classes and custom methods in data retrieval facilitates modular, reusable, and maintainable code practices in complex systems. By encapsulating complex queries within specific classes, developers can streamline query modifications and enhance code readability. For instance, custom methods allow for direct linkage of query logic to specific business needs, increasing flexibility and promoting code reuse. This approach reduces error susceptibility, improves performance through optimized query logic, and aligns with object-oriented design principles, simplifying debugging and future system enhancements .

ORDER BY is used in conjunction with GROUP BY to sort the final set of results, typically after grouping operations. The significance of using ORDER BY with GROUP BY is to ensure the grouping results appear in a specific order, aiding in the readability and analysis of the grouped data. In the query 'SELECT [IsActive],[DepartmentID], COUNT(*) AS 'Count'... ORDER BY [DepartmentID] ASC,[IsActive] ASC', ORDER BY ensures the grouped information is organized first by DepartmentID, then by IsActive status in ascending order, which aids in comparing grouped sections and facilitates better insights .

ROLLUP with GROUP BY in SQL queries generates aggregated totals for all combinations of a specified set of columns, creating subtotals and a grand total. It is beneficial for creating hierarchical report data as it computes cumulative totals along a dimension. When ROLLUP is used in the query 'SELECT [IsActive],[DepartmentID], COUNT(*) AS 'Count' FROM [AggregateTest] GROUP BY [IsActive],[DepartmentID] WITH ROLLUP', it provides a comprehensive summary by adding higher-level summary rows to the results for each group created by the IsActive and DepartmentID fields, beneficial for insights across different data levels .

The WHERE clause in SQL filters rows before any aggregation functions such as COUNT are applied, impacting the results by limiting which data is considered in the aggregation. For instance, in the query 'SELECT COUNT(*) AS 'Total' FROM [AggregateTest] WHERE ([IsActive] = @IsActive1)', the WHERE clause filters the rows to only those where IsActive is true. This filtering ensures that the COUNT function only considers active records, thereby influencing the outcome based on specific conditions, rather than evaluating all rows in the dataset .

DISTINCT and WHERE clauses serve different purposes in SQL queries and are suitable for various scenarios. DISTINCT removes duplicates from the results, effectively providing unique records for specified columns, ideal when summarizing or compiling non-repetitive data views. In contrast, the WHERE clause filters records based on specific conditions, precision-targeting the dataset for specific criteria evaluation. WHERE is favored when conditions are needed to pinpoint a subset of data for analysis, while DISTINCT is apt for data deduplication scenarios. The trade-off involves choosing between detailed conditional filtering versus deduplicating records, depending on the data retrieval objective .

Using aggregate functions without specifying aliases in SQL queries results in the use of default naming conventions based on the column names. This might lead to unclear column names in the result set, which can be less user-friendly and harder to interpret, particularly in analyses or when data is displayed in applications. For example, 'SELECT SUM([Salary])' without an alias results in a column named 'Salary' by default, whereas specifying an alias 'AS 'Sum'' clearly indicates the aggregation purpose. Naming conventions without explicit aliases might obscure the intent and processing of aggregate results, complicating data handling .

The SELECT TOP clause is beneficial in scenarios where you need to limit the number of rows returned from a query, such as when you retrieve the top N records with the highest values or for pagination purposes. In EntitySpaces queries, SELECT TOP is used by setting the Top property to a specific number of top rows you want to retrieve, filtering the result set to the specified top records. For example, the query specifying 'SELECT TOP 5 * FROM [AggregateTest] ORDER BY [Salary] DESC' retrieves the top 5 highest salaries, minimizing data processing and retrieval .

Using COUNT with a DISTINCT clause in a query counts only unique values for the specified column, effectively filtering out duplicates, which provides a count of unique records. In contrast, using COUNT alone counts all occurrences without regard to duplicates. For instance, in the query using COUNT(DISTINCT [LastName]), it yields SELECT COUNT(DISTINCT [LastName]) AS 'Count' FROM [AggregateTest], which counts the distinct last names, while COUNT(*) would count all rows .

Setting the DefaultConjunction to 'OR' in an EntitySpaces query changes the logical conjunction between conditions in the WHERE clause from 'AND' to 'OR'. This means that the resulting SQL statement is executed with a logical 'OR' between the specified conditions, affecting how rows are filtered based on those conditions. For example, in the EmployeesCollection query, when DefaultConjunction is set to 'OR', the SQL statement becomes SELECT * FROM [Employees] WHERE ([LastName] LIKE @LastName1 OR [FirstName] LIKE @FirstName2), allowing rows to match if either condition is true .

You might also like