Published by cce on 2011-05-12
Recently we introduced calculated attributes and table methods. Today we introduce references, denoted with a dollar-sign $ to provide a separate aliasing mechanism that cascades through scope boundaries.
In HTSQL, a scope is set of available names, which includes column names, links to other tables, and calculated attributes. Let’s deconstruct scopes in the following example.
Whenever a new scope is introduced, it completely hides the content of any previous scope. Thus, we cannot access attributes of school in the scope of department. It is an intentional limitation since otherwise attributes from different tables would pollute the namespace.
However, occasionally, we may still want to fetch an attribute from a previous scope. For instance, in the example above, we may need to add the school name to the output columns.
To overcome this limitation, we introduce references. A reference is a name that ignores scope boundaries. A reference starts with $ followed by an identifier and could be defined using the regular define() function:
$school_name | name |
---|---|
School of Engineering | Bioengineering |
School of Engineering | Computer Science |
School of Engineering | Electrical Engineering |
School of Engineering | Mechanical Engineering |
Of course in this example, you could use a forward link, school.name and get the same results.
However, this isn’t always the case.
Often a request may have constants which you may want to define separately. For example, suppose you have a query returns all courses offered during a particular academic year.
department_code | no | title | credits | description |
---|---|---|---|---|
acc | 200 | Introduction to Accounting | 3 | The initial course in the theory and practice of financial accounting. Topics emphasized include the preparation, reporting, and analysis of financial data. |
arthis | 102 | History of Art Criticism I | 4 | An introductory survey course of world art from Prehistoric through late-Medieval art history. |
arthis | 209 | Antique Art: Greece and Rome | 6 | Visual art of antique period with emphasis on Greek and Roman art. |
arthis | 224 | Arts of Asia | 3 | An introduction to the history and criticism of Far Eastern art, including the art of China and Japan, fine and decorative arts. |
The problem with this query is that it embeds a hard-coded parameter deep inside the query. To move this parameter up-front, we write:
department_code | no | title | credits | description |
---|---|---|---|---|
acc | 200 | Introduction to Accounting | 3 | The initial course in the theory and practice of financial accounting. Topics emphasized include the preparation, reporting, and analysis of financial data. |
arthis | 102 | History of Art Criticism I | 4 | An introductory survey course of world art from Prehistoric through late-Medieval art history. |
arthis | 209 | Antique Art: Greece and Rome | 6 | Visual art of antique period with emphasis on Greek and Roman art. |
arthis | 224 | Arts of Asia | 3 | An introduction to the history and criticism of Far Eastern art, including the art of China and Japan, fine and decorative arts. |
The reference mechanism is also needed for certain parameterized calculated attributes. Suppose we’ve got a business inquiry:
For each department calculate the average number of credits for freshman, sophomore, junior and senior level courses.
Here it is.
name | freshman | sophomore | junior | senior |
---|---|---|---|---|
Accounting | 2.0 | 3.0 | 4.0 | 3.0 |
Art History | 3.5 | 3.8 | 3.0 | 3.66666666667 |
Astronomy | 3.2 | 3.0 | 3.0 | 2.8 |
Bioengineering | 2.8 | 3.2 | 3.0 | 5.0 |
Bursar’s Office | ||||
In this query we define a calculated attribute avg_credits which depends upon parameter $level. This attribute produces the average credits over courses from the given level.