This guide explains how you can use Entity SQL to write queries to pull back the data you need when working with custom reports in SpiraPlan.
The language that we use for creating custom graphs and reports in Spira is called "Entity SQL" (abbreviated to ESQL) and is based on the standard database Structured Query Language (SQL) but modified by Microsoft to work against a conceptual object oriented data structure rather than a traditional relational database. According to the Microsoft Entity SQL website:
Entity SQL is a SQL-like language that enables you to query conceptual models in the Entity Framework. Conceptual models represent data as entities and relationships, and Entity SQL allows you to query those entities and relationships in a format that is familiar to those who have used SQL.
When using ESQL with Spira's reporting system, the entity collections you can use are the ones generated from the 'Add New Query' dropdown discussed in the previous article. For example, you have:
SpiraTestEntities.R_Requirements
SpiraTestEntities.R_TestCases
SpiraTestEntities.R_RequirementTestCases
etc...
The R_xxx prefix is used to distinguish the entities available for reporting from the core entities used by Spira internally for its data access. You will only ever be able query the R_ prefixed entities from within the Spira reporting system.
A simple query used to retrieve all of the requirements in project 1 sorted by hierarchical order then ID would be:
A more complex query that selects specific requirement properties (vs. the entire object), joins to other table (e.g. to get test case object properties as well) would be:
Finally, you can add on an aggregation function and group by to group by one property and aggregate the other properties against this. For example to get a count of the test cases associated with each requirement, instead of the test case names would be:
The select clause of an ESQL query can consist of either:
a single object reference, prefixed by value. This is semantically equivalent to SELECT * in database SQL and means evaluate all of the properties of the object.
a comma separated list of discrete object properties. They need to have their object alias prefixes (e.g. RQ in the examples above)
So for example we could have:
selectvalueRQ
that selects all of the properties in the requirements table (i.e. all the columns).
Alternatively you could select specific properties (columns) from one or more object:
In this case, we omit the value prefix since it's not evaluating all of the properties of an object. Since two of the properties have the same name ("NAME") we are using the as operator to give the property returned a unique name. This is important. If you try and return back two properties with the same name, Spira will give the following error message:
You get this error message because the Entity framework will try and create a name like (NAME #1) that is not allowed by the Spira reporting system. So make sure you used actual named aliases when the same property name is used more than once.
Finally you can use the following aggregations in the SELECT clause to aggregate data from properties that are not being grouped (see later for information on the group by clause):
Note that in this case we recommend you always specify an alias for the result of the aggregation function using the as operator. If you forget, you'll get the same error message as before:
The from clause in ESQL is relatively simple, it contains the primary object collection being queried and an alias that will be used to reference its properties in the other parts of the query:
If you are only going to need to work with the properties from a single object collection then you don't need to have any join clauses in your query. However if you are going to need data from multiple object collections, then you will need to use the join clause to add in those other collections. A simple join clause looks like:
where you add the name of the entity collection being joined, the alias to refer to it with, and the comparison operator (in this case an equality) used to make the join.
Entity SQL supports the following types of join:
inner join or join - Only rows that exist in both sides of the comparison are returned
left outer join or left join - Only rows that exist in the left hand side of the comparison are returned, plus any matching rows from the other side, or NULL if missing.
right outer join or right join - Only rows that exist in the right hand side of the comparison are returned, plus any matching rows from the other side, or NULL if missing.
full outer join or full join - All rows from both sides of the comparison are returned, with NULL values being used for non-matching rows on the alternate side.
cross join - This query expression produces the Cartesian product of the two collections from the left and right hand sides.
The where clause in ESQL lets you filter the results by one or more condition. In addition to the standard ESQL syntax, you can use the special Spira tokens to filter by dynamic data in the system:
${ProjectGroupId} - the current program (formerly known as project group)
${ProjectId} - the current product (formerly known as project)
${ReleaseId} the current release, phase, sprint, or iteration
The where clause consists of a set of conditions that are joined by a boolean operator:
and (used when condition A and condition B are true)
or (used when condition A or condition B are true)
Generally and operators have higher precedence than or operators, so you will need to use parenthesis when you want to have or operators that are higher precedence than an and.
In the discussion of the select clause we mentioned that you can use aggregation functions such as count, sum, min, max, etc. If you use these in the select clause, then any object properties that are not being aggregated need to be included in the group by clause:
groupbyRQ.REQUIREMENT_ID,RQ.NAME
If you don't have any aggregation functions, you can still use a group by clause to simply group similar rows, but generally speaking you omit the group by clause if there are no aggregation functions in the select list.
Finally, you typically want to sort the data by one or more of the object properties, this is done by having an order by clause at the end of the query:
orderbyTEST_CASE_COUNTdesc,RQ.REQUIREMENT_IDasc
The syntax of the order by clause is:
order by
property name (e.g. RQ.REQUIREMENT_ID) or property alias (e.g. TEST_CASE_COUNT). If an alias you don't use the object prefix (e.g. RQ)
asc or desc for ascending or descending order (if omitted, it will default to ascending)
If you sort by a property (e.g. requirement name) that could be held by multiple rows, it is recommended to always add a final sort clause by a guaranteed unique ID such as the primary key REQUIREMENT_ID since that will ensure the results are consistent each time. This is known as 'stable sorting'
Differences Between ESQL and Traditional Database SQL¶
Now that we have covered the basics of writing an Entity SQL (ESQL) query, we'll discuss some of the differences and limitations between ESQL and traditional database SQL.
Database SQL supports the unqualified * syntax as an alias for the entire row, and the qualified * syntax (t.*) as a shortcut for the fields of that table. In addition, database SQL allows for a special count(*) aggregate, which includes nulls.
Entity SQL does not support the * construct. Database SQL queries of the form:
select*fromT
and
selectT1.*fromT1,T2...
can be expressed in Entity SQL as
selectvaluetfromTast
and
selectvaluet1fromT1ast1,T2ast2...
respectively.
Additionally, these constructs handle inheritance (value substitutability), while the select * variants are restricted to top-level properties of the declared type. Entity SQL does not support the count(*) aggregate. Use count(0) instead.
Entity SQL supports aliasing of group by keys. Expressions in the select clause and having clause must refer to the group by keys via these aliases. For example, this Entity SQL syntax:
Collection-based aggregates operate on collections and produce the aggregated result. These can appear anywhere in the query, and do not require a group by clause. For example:
SELECTt.aASa,count({1,2,3})ASbFROMTASt
Entity SQL also supports SQL-style aggregates. For example:
Database SQL allows ORDER BY clauses to be specified only in the topmost SELECT .. FROM .. WHERE block. In Entity SQL you can use a nested ORDER BY expression and it can be placed anywhere in the query, but ordering in a nested query is not preserved.
-- The following query will order the results by the last name SELECTC1.FirstName,C1.LastNameFROMAdventureWorks.ContactASC1ORDERBYC1.LastName
-- In the following query ordering of the nested query is ignored. SELECTC2.FirstName,C2.LastNameFROM(SELECTC1.FirstName,C1.LastNameFROMAdventureWorks.ContactasC1ORDERBYC1.LastName)asC2
In database SQL, identifier comparison is based on the settings of the current database and the database platform being used (SQL Server, Oracle, MySQL, etc.). In Entity SQL, identifiers are always case insensitive and accent sensitive (that is, Entity SQL distinguishes between accented and unaccented characters; for example, 'a' is not equal to 'ấ'). Entity SQL treats versions of letters that appear the same but are from different code pages as different characters.
Entity SQL also imposes additional restrictions on queries involving group by clauses. Expressions in the select clause and having clause of such queries may only refer to the group by keys via their aliases. The following construct is valid in most database SQL variants but are not in Entity SQL:
Referencing Columns (Properties) of Tables (Collections)¶
All column references in Entity SQL must be qualified with the table alias. The following construct (assuming that a is a valid column of table T) is valid in database SQL but not in Entity SQL.
SQL:
SELECTaFROMT
The Entity SQL form is
SELECTt.aASAFROMTASt
The table aliases are optional in the from clause. The name of the table is used as the implicit alias. Entity SQL allows the following form as well:
Database SQL uses the "." notation for referencing columns of (a row of) a table. Entity SQL extends this notation (borrowed from programming languages) to support navigation through properties of an object.
For example, if p is an expression of type Person, the following is the Entity SQL syntax for referencing the city of the address of this person.
In database SQL, if you want to refer to a collection of possible values, you would use an IN clause together with a set of values contained within parenthesis:
SQL
SELECTt.aFROMTastWHEREt.bIN(1,2,3)
In Entity SQL, the syntax for a collection of values is based on braces / curly brackets instead:
There are some differences between how literal values and types are represented in Entity SQL vs. Database SQL:
In database SQL, you typically represent boolean values as 1 or 0 whereas in Entity SQL you use true** and false
Database SQL uses database schema types such as VARCHAR, NVARCHAR and INT, whereas Entity SQL uses Microsoft .NET types such as String and Int32
Integer literals can be of type Int32 (123), UInt32 (123U), Int64 (123L), and UInt64 (123UL)
DateTime literals, both date and time parts are mandatory. There are no default values. For example, a date literal would be:
DATETIME'2006-12-25 01:01:00.000'
There are Unicode and non-Unicode character string literals. Unicode strings are prepended with N. For example, N'hello'.
Typed nulls can be used anywhere. Type inference is not required for typed nulls because the type is known. For example, you can construct a null of type Int16 with the following Entity SQL construct:
(cast(nullasInt16))
Database SQL Functionality Not Available in Entity SQL¶
The following database SQL functionality is not available in Entity SQL.
DML Commands - Entity SQL currently provides no support for DML statements (insert, update, delete).
DDL Commands - Entity SQL provides no support for DDL in the current version.
Imperative Programming - Entity SQL provides no support for imperative programming, unlike Transact-SQL. Use a programming language instead.
Grouping Functions - Entity SQL does not yet provide support for grouping functions (for example, CUBE, ROLLUP, and GROUPING_SET).
Analytic Functions - Entity SQL does not (yet) provide support for analytic functions.
Built-in Functions, Operators - Entity SQL supports a subset of most database SQL's built in functions and operators.