| 1 |
<HTML> |
| 2 |
|
| 3 |
<HEAD> |
| 4 |
<TITLE>AQL - Language overview (Version 4_8b)</TITLE> |
| 5 |
<LINK REL="stylesheet" TYPE="text/css" HREF="./aql.css" TITLE="Style sheet for AQL docs"> |
| 6 |
</HEAD> |
| 7 |
<BODY> |
| 8 |
|
| 9 |
|
| 10 |
<H2>AQL - Language overview (Version 4_8b)</H2> |
| 11 |
|
| 12 |
<DL><DD><!-- indent section --> |
| 13 |
|
| 14 |
<P>Here is a simple AQL query that lists English authors with their |
| 15 |
email addresses, with an example of a possible resulting table:-</P> |
| 16 |
|
| 17 |
<CODE><EM>select</EM> a, a->Email <EM>from</EM> a <EM>in class</EM> Author <EM>where</EM> a->Mail = "England"</CODE> |
| 18 |
|
| 19 |
<PRE> |
| 20 |
---> |
| 21 |
"Durbin_R" "[email protected]" |
| 22 |
"Durbin_R" "[email protected]" |
| 23 |
"Wobus_F" "[email protected]" |
| 24 |
</PRE> |
| 25 |
|
| 26 |
<P>Conventional database systems store their data in tables, whereas acedb organizes |
| 27 |
its data in tree structured objects. However, the results of an AQL query are |
| 28 |
always returned in tables made up by rows and columns.</P> |
| 29 |
</DD></DL> |
| 30 |
|
| 31 |
<HR> |
| 32 |
|
| 33 |
|
| 34 |
<H3>1. Basic query structure</H3> |
| 35 |
|
| 36 |
<DL><DD><!-- indent section --> |
| 37 |
|
| 38 |
<P>Like in SQL the basic query construction comes in three parts:-</P> |
| 39 |
|
| 40 |
<CENTER> |
| 41 |
<TABLE WIDTH=80%> |
| 42 |
<TR><TD><CODE><EM>select</EM> a, a->Email</CODE></TD> <TD><A HREF="#SELECT_CLAUSE">select-clause</A></TD></TR> |
| 43 |
<TR><TD><CODE><EM>from</EM> a <EM>in class</EM> Author</CODE></TD> <TD><A HREF="#FROM_CLAUSE">from-clause</A></TD></TR> |
| 44 |
<TR><TD><CODE><EM>where</EM> a->Mail = "England"</CODE></TD> <TD><A HREF="#WHERE_CLAUSE">where-clause</A></TD></TR> |
| 45 |
</TABLE> |
| 46 |
</CENTER> |
| 47 |
</DD></DL> |
| 48 |
|
| 49 |
|
| 50 |
<A NAME="SELECT_CLAUSE"><H4>1.1. Select-clause</H4></A> |
| 51 |
<DL><DD><!-- indent section --> |
| 52 |
|
| 53 |
<P>The select-clause defines one or more fields that each will make up a column in the result. |
| 54 |
Whether or not those columns contain useful values and the number of rows will depend entirely |
| 55 |
on the data that matches the query.</P> |
| 56 |
|
| 57 |
<P>One can think of the whole select-from-where query as an expression whose |
| 58 |
return value is a table with rows and columns. |
| 59 |
Every field in the select clause is an expression and for each iteration |
| 60 |
cycle the result value will be copied to the result table. |
| 61 |
These expressions usually extract data values from objects or combine them using |
| 62 |
arithmetic operators or functions. |
| 63 |
The variables that are referenced by select-field expressions |
| 64 |
have to be declared in the <A HREF="#FROM_CLAUSE">from-clause</A> or in |
| 65 |
an enclosing scope.</P> |
| 66 |
|
| 67 |
<A NAME="ALIAS_OP"><H4>Field naming</H4></A> |
| 68 |
|
| 69 |
<P>More advanced AQL features need to refer to a specific field in a row. |
| 70 |
They can do this either by name or number. |
| 71 |
The second select-field in the above query can be named |
| 72 |
automatically by the tag that it uses, but if we want to use a |
| 73 |
name for the first field we must assign it explicitly using |
| 74 |
the field naming operator <TT>'::'</TT>.</P> |
| 75 |
|
| 76 |
<CODE><EM>select</EM> Author::a, a->Email <EM>from</EM> ...</CODE> |
| 77 |
|
| 78 |
<P>The use of expressions in the select-clause also requires |
| 79 |
those fields to be explicitly named, if they are to be |
| 80 |
referred to by name. Note the second select is again automatically |
| 81 |
named by it's tag.</P> |
| 82 |
|
| 83 |
<CODE><EM>select</EM> Age::<EM>yeardiff</EM>(a->date_of_birth, today), a->full_name <EM>from</EM> ...</CODE> |
| 84 |
|
| 85 |
</DD></DL> |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
<A NAME="FROM_CLAUSE"><H4>1.2. From-clause</H4></A> |
| 90 |
<DL><DD><!-- indent section --> |
| 91 |
<P>This part of the query declares all iterator variables and thereby defines the source |
| 92 |
of the data. Acedb stores all data in objects that belong to classes. The example query |
| 93 |
declares the iterator <TT>a</TT> to range over the objects |
| 94 |
in class <I>Author</I>. The preferred AQL notation for a class declaration is</P> |
| 95 |
|
| 96 |
<DL><DD><I>IteratorName</I> <EM>in class</EM> <I>ClassName</I></DL> |
| 97 |
|
| 98 |
<P>although</P> |
| 99 |
|
| 100 |
<DL> |
| 101 |
<DD><EM>class</EM> <I>ClassName</I> <EM>as</EM> <I>IteratorName</I></DD> |
| 102 |
<DD><EM>class</EM> <I>ClassName</I> <I>IteratorName</I></DD> |
| 103 |
</DL> |
| 104 |
|
| 105 |
<P>are valid variations that are borrowed from OQL.</P> |
| 106 |
|
| 107 |
<P>Class declarations will cause the query to fill the iterator variable <TT>a</TT> |
| 108 |
with values from the <I>Author</I> class. |
| 109 |
Other iterators can be defined based on previously defined iterators:-</P> |
| 110 |
|
| 111 |
<NOBR><CODE>... <EM>from</EM> a <EM>in class</EM> author, p <EM>in</EM> a->Paper ...</CODE></NOBR> |
| 112 |
|
| 113 |
<A NAME="OBJ_CONSTRUCTOR"> |
| 114 |
<P>There are many cases where a specific object is wanted |
| 115 |
and we want to refer to that object directly without searching through the entire class. |
| 116 |
The declaration:- </P> |
| 117 |
</A> |
| 118 |
|
| 119 |
<NOBR><CODE>... <EM>from</EM> s <EM>in object</EM>("Sequence","F15H10")->Subsequence ...</CODE></NOBR> |
| 120 |
|
| 121 |
<P>finds the object F15H10 in class <I>Sequence</I> directly and then declares the iterator variable |
| 122 |
<TT>s</TT> to loop over the objects that are listed under the |
| 123 |
<I>Subsequence</I> tag in F15H10.</P> |
| 124 |
|
| 125 |
<P>Variables can also be declared to be based on the result of any expression (e.g. the table-function <EM>count</EM>):-</P> |
| 126 |
|
| 127 |
<NOBR><CODE>... <EM>from</EM> num <EM>in count</EM>(a->paper) ...</CODE></NOBR> |
| 128 |
|
| 129 |
<P>This notation can be very useful if the value for <TT>num</TT> needs to be used |
| 130 |
in the select-clause as well as in a comparison in the where-clause. Just referring |
| 131 |
to the variable num saves having to calculate the <EM>count</EM>-function twice.</P> |
| 132 |
|
| 133 |
<P><STRONG>Note</STRONG> that the from-where-clause is optional. It is not needed if the |
| 134 |
select fields don't refer to any iterators that require a declaration (in a from-clause), |
| 135 |
i.e. where there are no iterators in the query or where any iterators that are present have |
| 136 |
already been declared in an enclosing scope, which can happen inside a nested query.</P> |
| 137 |
</DD></DL> |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
<A NAME="WHERE_CLAUSE"><H4>1.3. Where-clause</H4></A> |
| 143 |
<DL><DD><!-- indent section --> |
| 144 |
<P>The WHERE clause is a <A HREF="#COMPARISON">boolean condition</A> which can |
| 145 |
be attached to any declaration to act as a filter.</P> |
| 146 |
|
| 147 |
<P>In order to evaluate the where-clause in the example query, the <I>Text</I>-data stored under the |
| 148 |
<I>Address</I> tag in every <I>Author</I> object is compared with "England". |
| 149 |
The range of <I>Object</I>-values for the iterator <TT>a</TT> in the from-clause will therefore be restricted |
| 150 |
to those objects where the comparison is TRUE.</P> |
| 151 |
|
| 152 |
<P>A query can have multiple where-clauses and in queries with multiple from-clause declarations |
| 153 |
it is useful to apply the boolean condition as early as possible. The from-clauses are evaluated |
| 154 |
in turn and a FALSE where-clause will prevent any subsequent declarations to be evaluated |
| 155 |
in any cycle of the execution loop. Clever where-clauses that filter the declarations |
| 156 |
early on can therefore increase the execution speed of a query. |
| 157 |
<BR> |
| 158 |
For example the following query finds the papers of authors who |
| 159 |
live in England. In the example, the second declaration which fetches |
| 160 |
the authors papers doesn't have to be execuetd for any author where |
| 161 |
the where-clause on the first declaration (test on english address) |
| 162 |
has already failed.</P> |
| 163 |
|
| 164 |
<CODE><EM>select</EM> a, b <EM>from</EM> a <EM>in class</EM> Author <EM>where</EM> a->Address = "England", b <EM>in</EM> a->Paper</CODE> |
| 165 |
|
| 166 |
</DD></DL> |
| 167 |
|
| 168 |
|
| 169 |
<HR> |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
<H3>2. Quoting value literals</H3> |
| 174 |
|
| 175 |
<DL><DD><!-- indent section --> |
| 176 |
<P>The database schema, known in acedb as the |
| 177 |
<A HREF="http://www.sanger.ac.uk/Software/Acedb/Acedocs/models_guide.html">models</A> |
| 178 |
(stored in <B>wspec/models.wrm</B>), specifies a type |
| 179 |
for each value that is stored. An iterator in AQL will assume that type when it binds to the |
| 180 |
value in the database. </P> |
| 181 |
<P>Values can also be quoted as literals in an AQL query, e.g. in |
| 182 |
comparisons with iterator values. Every expression in an AQL query (e.g. in a select-field) |
| 183 |
that evaluates correctly will produce a result of a specific type.</P> |
| 184 |
|
| 185 |
<P>These are the legal value types as used in the acedb database schema and examples of how they |
| 186 |
might be quoted in an AQL query.</P> |
| 187 |
|
| 188 |
<TABLE CELLPADDING=5> |
| 189 |
<TR BGCOLOR=#c0c0c0> |
| 190 |
<TD>Int</TD> <TD><TT>1024<BR>-12</TT></TD> |
| 191 |
</TR><TR BGCOLOR=#e0e0e0> |
| 192 |
<TD>Float</TD> <TD><TT>3.1415<BR>-5e3<BR>4.2E-10<BR>.2</TT></TD> |
| 193 |
</TR><TR BGCOLOR=#c0c0c0> |
| 194 |
<TD>Text<BR></TD> <TD><TT>"England"<BR>"Durbin RM"</TT></TD> |
| 195 |
</TR><TR BGCOLOR=#e0e0e0> |
| 196 |
<TD>DateType</TD> <TD><TT>`1998-08-04_10:21:46<BR>`1989-11-09</TT></TD> |
| 197 |
</TABLE> |
| 198 |
|
| 199 |
<P>To quote an object one has to use the <A HREF="#OBJ_CONSTRUCTOR"><EM>object</EM> constructor</A> to define an iterator, |
| 200 |
whose value will be that object. If the iterator is printed to the result table, the object name will be |
| 201 |
shown as a text-string.</P> |
| 202 |
|
| 203 |
</DD></DL> |
| 204 |
|
| 205 |
<A NAME="DATE_TIME"><H4>Dates and Times</H4></A> |
| 206 |
<DL><DD><!-- indent section --> |
| 207 |
<P>The AQL syntax requires DateType literals to be prefixed by a <TT>`</TT> character. |
| 208 |
A DateType literal may specify any level of detail from just the year down to the seconds.</P> |
| 209 |
|
| 210 |
<P>These are valid DateType literals:-</P> |
| 211 |
|
| 212 |
<PRE><TT> |
| 213 |
`1998-08-04_10:27:32 |
| 214 |
`1998-08-04_10:27 |
| 215 |
`1998-08-04_10 |
| 216 |
`1998-08-04 |
| 217 |
`1998-08 |
| 218 |
`1998 |
| 219 |
</TT></PRE> |
| 220 |
|
| 221 |
<P>The literals <TT>`1998-08-04_</TT> or <TT>`1998-55</TT> would be rejected.</P> |
| 222 |
|
| 223 |
<P>The reserved keywords <EM>today</EM> and |
| 224 |
<EM>now</EM> are used to refer to the current |
| 225 |
date/time and current date respectively. For example at this |
| 226 |
moment these keywords replace the following date/time quotations:-</P> |
| 227 |
|
| 228 |
<TABLE CELLPADDING=5> |
| 229 |
<TR> |
| 230 |
<TD><EM>today</EM></TD> <TD>---></TD> |
| 231 |
<TD><TT>1999-12-06_11:13:20</TT></TD> |
| 232 |
</TR> |
| 233 |
|
| 234 |
<TR> |
| 235 |
<TD><EM>now</EM></TD> <TD>---></TD> |
| 236 |
<TD><TT>1999-12-06</TT></TD> |
| 237 |
</TR> |
| 238 |
</TABLE> |
| 239 |
</DD></DL> |
| 240 |
|
| 241 |
<A NAME="NULL_VALUES"><H4>Null values</H4></A> |
| 242 |
<DL><DD><!-- indent section --> |
| 243 |
<P>The definition of Null values in AQL is borrowed from Computer Science. |
| 244 |
The possible values for any type include the valid range of the type and the value NULL. |
| 245 |
Values usually become NULL if an iterator couldn't be evaluated, e.g. a tag is not present in an object. |
| 246 |
Rather than reporting 0 or an empty string - AQL will report NULL to indicate that no valid value could be found. |
| 247 |
This is because 0 and the empty string are valid values for Integer and String-types respectivly. |
| 248 |
</P> |
| 249 |
<P>Expressions containing NULL values will fail and their result itself becomes NULL. This explains the |
| 250 |
little quirk of boolean values, which can't only be TRUE or FALSE but also NULL. |
| 251 |
<BR>Example : |
| 252 |
<CODE> <EM>select</EM> ->0, ->subsequence[1], <EM>abs</EM> (->subsequence[2] - ->subsequence[3]) > 1000 <EM>from class</EM> Sequence</CODE> |
| 253 |
<BR> |
| 254 |
The third column is a boolean value, but it can only be evaluated if the <I>Subsequence</I> tag and its values exist for |
| 255 |
a given object. The result will be TRUE or FALSE based on the actual numbers, but NULL of the tag didn't exist and the |
| 256 |
expression couldn't be evaluated.</P> |
| 257 |
<P>Note that in a boolean context (e.g. comparison, boolean operator) Integer values are converted to booleans - |
| 258 |
zero is FALSE and non-zero is TRUE.</P> |
| 259 |
|
| 260 |
</DD></DL> |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
<HR> |
| 265 |
|
| 266 |
|
| 267 |
<H3>3. Variables</H3> |
| 268 |
|
| 269 |
<A NAME="LOCATOR"><H4>3.1. Iterators</H4></A> |
| 270 |
<DL><DD><!-- indent section --> |
| 271 |
<P>The most important part of any useful query is the ability |
| 272 |
to bind a variable to a database value. Iterator variables serve |
| 273 |
this purpose. Iterators are declared in <A HREF="#FROM_CLAUSE">from clauses</A> as ranging |
| 274 |
over a set of values. Object-valued iterators can then be dereferenced using the |
| 275 |
<TT>-></TT> operator to extract data from inside the objects.</P> |
| 276 |
|
| 277 |
<P>AQL maintains the notion of a scope for each iterator |
| 278 |
within each select-from-where expression. Any identifier can only |
| 279 |
be declared once within a single scope. An inner nested query can redeclare |
| 280 |
the identifier name, shadowing the outer declaration. |
| 281 |
The identifier is out of scope beyond the select-from-where expression |
| 282 |
in which it was declared.</P> |
| 283 |
|
| 284 |
<A NAME=DEF_LOC><H4>Extracting data from objects</H4></A> |
| 285 |
|
| 286 |
<P>There are two different operators that can be used to extract data from inside an |
| 287 |
object. The most commonly used is the arrow operator <TT>-></TT>, which will |
| 288 |
dereference an object valued expression, looking inside that object starting at its root |
| 289 |
(remember that acedb objects are trees). We have seen many examples of this, such as |
| 290 |
<TT>a->Email</TT>. The other data access operator is a pair of square brackets |
| 291 |
<TT>[ ]</TT>, which are used to look further within the same object. This is |
| 292 |
most often seen in compound constructions, such as <TT>a->Map[Position]</TT> |
| 293 |
which extracts the Position that follows the Map entry inside the <TT>a</TT> object. |
| 294 |
i.e. this expects the <TT>a</TT> object to contain data looking something like |
| 295 |
<pre> |
| 296 |
a = Locus unc-3 |
| 297 |
Map "X" Position 18.5633 |
| 298 |
</pre> |
| 299 |
In contrast, <TT>a->Map->Position</TT> would give the Position in the Map |
| 300 |
object, i.e. the Position field within object "X" in the above example.</P> |
| 301 |
|
| 302 |
<P>Either type of operator can use a tag name, as above, or a (non-negative) integer. If a |
| 303 |
tag name is used, it is interpreted according to the schema (model) of the relevant acedb |
| 304 |
class. If a number is used, it is interpreted as a number of positions rightwards in the |
| 305 |
tree representation of the object. In practice, tags are normally used with the arrow |
| 306 |
operator, and numbers with the brackets operator, because often the first action is to |
| 307 |
extract a field by its tag name, and then further information may be stored in subsequent |
| 308 |
fields to the right of it in the acedb model, which can be accessed by [1], [2] |
| 309 |
etc. e.g. <CODE><EM>select</EM> a->Subsequence, a->Subsequence[1], a->Subsequence[2]</CODE> |
| 310 |
will extract the names, start and end points of subsequences according to the standard |
| 311 |
acedb Sequence class model.</P> |
| 312 |
|
| 313 |
<A NAME="DEF_ITERATOR"><H4>The unnamed (default) iterator</H4></A> |
| 314 |
|
| 315 |
<P>The iterator identifier can be omitted for one declaration only, so many simple queries |
| 316 |
can be abbreviated like:-</P> |
| 317 |
|
| 318 |
<CODE><EM>select</EM> ->Paper <EM>from</EM> <EM>class</EM> Author <EM>where</EM> ->Address[Mail] = "England"</CODE> |
| 319 |
|
| 320 |
<P>The default iterator that is declared by a shorthand declaration has no |
| 321 |
name, therefore we can't refer to it by name. We can however refer to |
| 322 |
using <TT>->0</TT> or <TT>[0]</TT>. Both mean exactly the same - |
| 323 |
the object itself (in this case the default iterator).</P> |
| 324 |
|
| 325 |
<P>In a simple select clause which reports only the values of the iterator itself, |
| 326 |
the keyword <EM>all</EM> can be used to represent the unidentified iterator. |
| 327 |
In this case no other select-field are allowed, but the keyword <EM>from</EM> |
| 328 |
also becomes optional:-</P> |
| 329 |
|
| 330 |
<P>To list all sequence object containing the <I>DNA</I>-tag, the query shortens drastically to:-</P> |
| 331 |
|
| 332 |
<CODE><EM>select all class</EM> Sequence <EM>where exists_tag</EM> ->DNA</CODE> |
| 333 |
|
| 334 |
<P>This query is identical to |
| 335 |
<CODE><EM>select all from class</EM> Sequence <EM>where exists_tag</EM> ->DNA</CODE> |
| 336 |
<BR> |
| 337 |
<CODE><EM>select</EM> ->0 <EM>from class</EM> Sequence <EM>where exists</EM> ->DNA[0]</CODE> |
| 338 |
<BR> |
| 339 |
<CODE><EM>select</EM> [0] <EM>from class</EM> Sequence <EM>where</EM> ->DNA[0]</CODE> |
| 340 |
<BR> |
| 341 |
<CODE><EM>select</EM> s <EM>from</EM> s <EM>in class</EM> Sequence <EM>where exists_tag</EM> s->DNA</CODE> |
| 342 |
<BR> |
| 343 |
|
| 344 |
</DD></DL> |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
<A NAME="TABLE_VAR"><H4>3.2. Table variables</H4></A> |
| 350 |
<DL><DD><!-- indent section --> |
| 351 |
The result of a table-expression (e.g. a select-from-where query) can be |
| 352 |
assigned to a variable. The variable name starts with <TT>@</TT> |
| 353 |
followed by an identifier (like a list object in perl). The values in |
| 354 |
that table can be re-used in subsequent query.</P> |
| 355 |
|
| 356 |
<P>Multiple table-expressions within one query are concatenated by the |
| 357 |
semi-colon <TT>;</TT>. AQL always returns the result of the |
| 358 |
last table-expression of a query.</P> |
| 359 |
|
| 360 |
<P>Example:-</P> |
| 361 |
|
| 362 |
<PRE><CODE> |
| 363 |
@AH6subseq := <EM>select</EM> seq::x, x[1], x[2] |
| 364 |
<EM>from</EM> x <EM>in object</EM>("Sequence","AH6")->Subsequence ; |
| 365 |
<EM>select</EM> r:seq, (r:3 - r:2) |
| 366 |
<EM>from</EM> r <EM>in</EM> @AH6subseq <EM>where</EM> r:3 > r:2 |
| 367 |
</CODE></PRE> |
| 368 |
|
| 369 |
<P>The table called <TT>@AH6subseq</TT> is filled with the results |
| 370 |
of a <I>select</I> table-expression. This table contains three column, |
| 371 |
while the first column has explicitly been <A HREF="#ALIAS_OP">renamed</A> |
| 372 |
to '<TT>seq</TT>' for clarification.</P> |
| 373 |
|
| 374 |
<P>The from-clause of the second table-expression defines a row-variable |
| 375 |
iterator <TT>r</TT> to loop over rows in the table |
| 376 |
<TT>@AH6subseq</TT>. In every evaluation cycle the iterator |
| 377 |
<TT>r</TT> will point to the next row in that table. </P> |
| 378 |
|
| 379 |
<P>A field in a row is referred to by the row-variable name followed by |
| 380 |
a colon <TT>:</TT> followed by either the column name or the column number.</P> |
| 381 |
|
| 382 |
<P>The select-clause then generates a result table with 2 columns by |
| 383 |
extracting the '<TT>seq</TT>' field from each row and the |
| 384 |
result of subtracting the second column (<TT>r:2</TT>) from |
| 385 |
the third column (<TT>r:3</TT>).</P> |
| 386 |
|
| 387 |
<P>In the same way fields are specified in row-variables, they can |
| 388 |
be used to extract a whole column from a table variable. |
| 389 |
This is another re-use of the same table definition:-<P> |
| 390 |
|
| 391 |
<PRE><CODE> |
| 392 |
@AH6subseq := <EM>select</EM> seq::x, x[1], x[2] |
| 393 |
<EM>from</EM> x <EM>in object</EM>("Sequence","AH6")->Subsequence ; |
| 394 |
<EM>select</EM> s, s->source |
| 395 |
<EM>from</EM> s <EM>in</EM> @AH6subseq:seq |
| 396 |
</CODE></PRE> |
| 397 |
|
| 398 |
<P>The from-clause of the second expression declares a table based iterator |
| 399 |
<TT>s</TT> that will take values (of type <I>Object</I>) from the |
| 400 |
'<TT>seq</TT>' column of the <TT>@AH6subseq</TT> table. |
| 401 |
This column contains the objects that follow Subsequence in AH6, and it can therefore |
| 402 |
be dereferenced in the second select-clause expression.</P> |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
<HR ALIGN=CENTER WIDTH=50%> |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
<H4>The unnamed (default) row-variable iterator</H4> |
| 411 |
|
| 412 |
<P>The default iterator from the <TT><EM>select all</EM></TT> construct |
| 413 |
can also be used with table variables. Based on the definition of |
| 414 |
<TT>@AH6subseq</TT> from above, these two queries are equivalent, |
| 415 |
but the second query removes the need for the declaration of a row-variable |
| 416 |
for each column:-</P> |
| 417 |
|
| 418 |
<P><CODE><EM>select</EM> <I>def</I>:1, <I>def</I>:2, <I>def</I>:3 <EM>from</EM> <I>def</I> <EM>in</EM> @AH6subseq</CODE></P> |
| 419 |
|
| 420 |
<P><CODE><EM>select all</EM> @AH6subseq</CODE></P> |
| 421 |
|
| 422 |
<P>The default iterator that is substituted into the select-clause is not |
| 423 |
visible in the <TT><EM>select all</EM></TT> construct, but it can be |
| 424 |
referenced in the same way as normal default iterators.</P> |
| 425 |
|
| 426 |
<CODE><EM>select all</EM> @AH6subseq <EM>where</EM> :3 > :2</CODE> |
| 427 |
|
| 428 |
<P>lists all rows in the table, where the the third field is greater |
| 429 |
than the second field. Columns can be referenced by name |
| 430 |
(see <A HREF="#ALIAS_OP">field naming</A>) or by number as usual.</P> |
| 431 |
</DD></DL> |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
<HR ALIGN=CENTER WIDTH=50%> |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
<A NAME=SCALAR_VAR><H4>3.3. Scalar variables</H4></A> |
| 440 |
<DL><DD><!-- indent section --> |
| 441 |
|
| 442 |
<P>Just as the results table-expressions can be assigned to variables, |
| 443 |
the results of scalar expressions can be assigned to scalar variables, |
| 444 |
i.e. single-valued objects. The variable name of a scalar variable |
| 445 |
starts with a <TT>$</TT> followed by an identifier (like an |
| 446 |
ordinary variable in perl). |
| 447 |
<BR> |
| 448 |
It can be of any scalar type (Integer, Float, Text, Date) and will |
| 449 |
assume the value and type of the result of evaluating the given expression.</P> |
| 450 |
|
| 451 |
<P>Example:-</P> |
| 452 |
|
| 453 |
<PRE><CODE> |
| 454 |
$x := <EM>avg</EM>(<EM>select</EM> x2 - x1 |
| 455 |
<EM>from</EM> s <EM>in object</EM>("Sequence","AH6")->Subsequence, |
| 456 |
x1 in a[1], x2 in a[2] |
| 457 |
<EM>where</EM> x2 > x1) ; |
| 458 |
<EM>select</EM> s, x2-x1 |
| 459 |
<EM>from s <EM>in</EM> object</EM>("Sequence","AH6")->Subsequence, |
| 460 |
x1 in s[1], x2 in s[2] |
| 461 |
<EM>where</EM> x2 - x1 > $x |
| 462 |
</CODE></PRE> |
| 463 |
|
| 464 |
<P>The first part of this query is a table-expression to find all |
| 465 |
positive sizes of subsequences of AH6. Their average if then |
| 466 |
assigned to the scalar variable $x. The second part then lists |
| 467 |
those subsequences with sizes that are bigger than average. |
| 468 |
It thereby re-uses the single value for the average computed in part I.</P> |
| 469 |
|
| 470 |
<P>A scalar can be assigned to the result of any |
| 471 |
<A HREF="syntax.shtml#EXPR">valid expression</A>, e.g. |
| 472 |
<A HREF="#TABLE_FUNC">table-functions</A>, <A HREF="#DATE_FUNC">date-functions</A> |
| 473 |
or arithmetic expressions.</P> |
| 474 |
|
| 475 |
<P>The result of a singular scalar variable assign operation will be |
| 476 |
a single column table with one row to contain the expression value. Therefore</P> |
| 477 |
|
| 478 |
<P><CODE>$x := 3.1415 * 3</CODE><P> |
| 479 |
|
| 480 |
<P>is perfectly valid as an AQL query, although slightly |
| 481 |
inappropriate for a query language to answer.</P> |
| 482 |
</DD></DL> |
| 483 |
|
| 484 |
|
| 485 |
<HR> |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
<A NAME="EXPR_ARITH"><H3>4. Expressions and arithmetic</H3></A> |
| 490 |
|
| 491 |
<A NAME="COMPARISON"><H4>4.1. Comparisons</H4></A> |
| 492 |
<DL><DD><!-- indent section --> |
| 493 |
|
| 494 |
<P>Comparisons are boolean expressions, which means that the |
| 495 |
result is either TRUE or FALSE. Iterator variables can be compared |
| 496 |
to other iterators or to value literals or vice versa. It is important |
| 497 |
to understand the notion of value types, as incompatibilities lead to |
| 498 |
an error when the query is processed.</P> |
| 499 |
|
| 500 |
<P>In general only variables or literals of equal types can be compared |
| 501 |
to each other. For simplicity however, a few automatic conversions are |
| 502 |
performed that make the language <I>less fussy</I>:-</P> |
| 503 |
|
| 504 |
<UL> |
| 505 |
<LI>Integers are promoted to Floats in numeric comparisons. |
| 506 |
<LI>Tags or Objects are promoted to Text if required.<BR> |
| 507 |
<I>Remember</I> : These are database internal codes anyway, so text is the |
| 508 |
only way to quote them literally. When objects are compared to <U>each other</U>, |
| 509 |
though, their internal identifiers are used, not their names as Text. |
| 510 |
</UL> |
| 511 |
|
| 512 |
<P>During evaluation a iterator will assume the value-types of |
| 513 |
the database-field that they are bound to. Therefore in most |
| 514 |
cases the query has to be run against the database to perform |
| 515 |
all the type-checking.</P> |
| 516 |
|
| 517 |
<P>AQL will allow the following comparison operators with their usual meaning:-</P> |
| 518 |
|
| 519 |
<PRE> |
| 520 |
= != > < >= <= <EM>like</EM> |
| 521 |
</PRE> |
| 522 |
|
| 523 |
<P><STRONG>Note :</STRONG> Objects and Tags, when compared to each other |
| 524 |
can only be tested for equality or inequality (i.e. = and != respectively).</P> |
| 525 |
|
| 526 |
<P>When <I>Text</I>s are compared to Objects/Tags or each other, all operators are valid. |
| 527 |
The <EM>like</EM> comparator performs case-insensitive wildcard matching on <I>Text</I> values.<P> |
| 528 |
|
| 529 |
<DL><DD> |
| 530 |
<CODE>"Cambridge, England, UK" <EM>like</EM> "*england*"</CODE> --> TRUE |
| 531 |
</DD></DL> |
| 532 |
|
| 533 |
<P>Possible wildcards are the usual <TT>*</TT> and <TT>?</TT>, |
| 534 |
where <TT>*</TT> matches any number of characters |
| 535 |
and <TT>?</TT> matches any <U>one</U> character.</P> |
| 536 |
|
| 537 |
<P>Apart from the <EM>like</EM> operator, all others work |
| 538 |
on Int, Float (as usual) and DateType (see next section) values.</P> |
| 539 |
|
| 540 |
<A NAME="DATE_COMPARISON"><H4>Comparison of dates</H4></A> |
| 541 |
|
| 542 |
<P>It is easy to compare two <I>DateType</I> values, if they both specify the same level |
| 543 |
of detail. In this case the concept of later/earlier is unambiguous.</P> |
| 544 |
|
| 545 |
<PRE> |
| 546 |
<TT>1996-03 < 1997-04</TT> --> TRUE |
| 547 |
<TT>1998-06-07 > 1998-06-12</TT> --> FALSE |
| 548 |
<TT>1998-06-07 = 1998-06-07</TT> --> TRUE |
| 549 |
</PRE> |
| 550 |
|
| 551 |
<P>If the two dates in question however, vary in the level of detail given, |
| 552 |
the outcome of the comparison expression becomes more difficult to understand. |
| 553 |
<BR> |
| 554 |
A comparison will only become TRUE, if the levels of detail given in the |
| 555 |
two dates allows an unambiguous decision. The expression |
| 556 |
<TT>1998-06 < 1998-07-09_09:51:23</TT> is TRUE, |
| 557 |
which decided at <I>month</I>-level.</P> |
| 558 |
|
| 559 |
<P>By the same rational <TT>1990 = 1990-05-02</TT> is TRUE, |
| 560 |
as both dates are equal in as much information as is given in either date-value.</P> |
| 561 |
|
| 562 |
<P>An equality comparison asks if the lesser detailed date |
| 563 |
is completely contained within the other, and the above |
| 564 |
comparison evaluates TRUE, because May 2nd 1990 is in the year 1990.</P> |
| 565 |
|
| 566 |
<P>A comparison involving greater-than or less-than is based on |
| 567 |
the amount of detail, that the decision can be based on:-<BR> |
| 568 |
<TT>1998-07 < 1998-07-09</TT> is FALSE, |
| 569 |
because one date gives a specific day in July 1998, but as the |
| 570 |
other doesn't specify the day, so we can't decide whether it is earlier |
| 571 |
and the comparison evaluates FALSE.</P> |
| 572 |
|
| 573 |
<P>To demonstrate this behavior, imagine a movie database, that contains |
| 574 |
movies titles and the date on which they were released. |
| 575 |
The movie "City Hall" was released in the USA on 1996-02-16.</P> |
| 576 |
|
| 577 |
<CODE><EM>select all class</EM> Movie <EM>where</EM> ->Released < `1996-02</CODE> |
| 578 |
<BR> |
| 579 |
<CODE><EM>select all class</EM> Movie <EM>where</EM> ->Released > `1996-02</CODE> |
| 580 |
|
| 581 |
<P>will both <B>EX</B>clude the movie 'City Hall', while</P> |
| 582 |
|
| 583 |
<CODE><EM>select all class</EM> Movie <EM>where</EM> ->Released <= `1996-02</CODE> |
| 584 |
<BR> |
| 585 |
<CODE><EM>select all class</EM> Movie <EM>where</EM> ->Released >= `1996-02</CODE> |
| 586 |
<BR> |
| 587 |
<CODE><EM>select all class</EM> Movie <EM>where</EM> ->Released < `1996-02-17</CODE> |
| 588 |
<BR> |
| 589 |
<CODE><EM>select all class</EM> Movie <EM>where</EM> ->Released > `1996-02-15</CODE> |
| 590 |
<BR> |
| 591 |
<CODE><EM>select all class</EM> Movie <EM>where</EM> ->Released <= `1996-02-16</CODE> |
| 592 |
<BR> |
| 593 |
<CODE><EM>select all class</EM> Movie <EM>where</EM> ->Released >= `1996-02-16</CODE> |
| 594 |
|
| 595 |
<P>will <B>IN</B>clude 'City Hall'.</P> |
| 596 |
|
| 597 |
</DD></DL> |
| 598 |
|
| 599 |
|
| 600 |
<HR ALIGN=CENTER WIDTH=50%> |
| 601 |
|
| 602 |
|
| 603 |
<A NAME="BOOL_OP"><H4>4.2. Boolean operators</H4></A> |
| 604 |
<DL><DD><!-- indent section --> |
| 605 |
|
| 606 |
<P>In addition to boolean comparisons, there are other boolean operators |
| 607 |
that can be evaluated in the where-clause.</P> |
| 608 |
|
| 609 |
<P>The following operators are supported for boolean expressions:-</P> |
| 610 |
|
| 611 |
<CENTER> |
| 612 |
<TABLE CELLPADDING=3 WIDTH=80%> |
| 613 |
<TR BGCOLOR=#bfbfbf> |
| 614 |
<TH>operator</TH><TH></TH><TH>boolean value</TH> |
| 615 |
</TR><TR> |
| 616 |
<TD><EM>exists</EM> <I>obj->tag</I></TD> <TD> - </TD> <TD>TRUE if the tag has a value<BR>(if obj->tag[1] is non-NULL)</TD> |
| 617 |
</TR><TR> |
| 618 |
<TD><EM>exists_tag</EM> <I>obj->tag</I></TD> <TD> - </TD> <TD>TRUE if the tag is present<BR>(if obj->tag[0] is non-NULL)</TD> |
| 619 |
</TR><TR> |
| 620 |
<TD><EM>not</EM> <I>expr</I></TD> <TD> - </TD> <TD>TRUE if FALSE, FALSE if TRUE</TD> |
| 621 |
</TR><TR> |
| 622 |
<TD><I>expr</I> <EM>and</EM> <I>expr</I></TD> <TD> - </TD> <TD>TRUE if both are TRUE</TD> |
| 623 |
</TR><TR> |
| 624 |
<TD><I>expr</I> <EM>or</EM> <I>expr</I></TD> <TD> - </TD> <TD>TRUE if one is TRUE</TD> |
| 625 |
</TR><TR> |
| 626 |
<TD><I>expr</I> <EM>xor</EM> <I>expr</I></TD> <TD> - </TD> <TD>TRUE if one or the other is TRUE, but not both</TD> |
| 627 |
</TR> |
| 628 |
</TABLE> |
| 629 |
</CENTER> |
| 630 |
</DD></DL> |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
<HR ALIGN=CENTER WIDTH=50%> |
| 635 |
|
| 636 |
|
| 637 |
<A NAME="ARITH"><H4>4.3. Arithmetic</H4></A> |
| 638 |
<DL><DD><!-- indent section --> |
| 639 |
|
| 640 |
|
| 641 |
<P>All standard arithmetic expressions are supported, both in the |
| 642 |
<A HREF="#SELECT_CLAUSE">select-clause</A> to produce columns of |
| 643 |
calculated values and in the <A HREF="#WHERE_CLAUSE">where-clause </A> |
| 644 |
to compare expression results.</P> |
| 645 |
|
| 646 |
<P>The following operators are supported for Integer and Floating point values types:-</P> |
| 647 |
|
| 648 |
<CENTER> |
| 649 |
<TABLE CELLPADDING=3 WIDTH=80%> |
| 650 |
<TR BGCOLOR=#bfbfbf> |
| 651 |
<TH>operator</TH><TH></TH><TH>expression value</TH> |
| 652 |
</TR><TR> |
| 653 |
<TD><TT>-</TT> <I>expr</I></TD> <TD> - </TD> <TD>negative value</TD> |
| 654 |
</TR><TR> |
| 655 |
<TD><EM>abs</EM> <TT>(</TT> <I>expr<I> <TT>)</TT></TD> <TD> - </TD> <TD>absolute value</TD> |
| 656 |
</TR><TR> |
| 657 |
<TD><EM>mod</EM> <TT>(</TT> <I>expr</I>, <I>expr</I> <TT>)</TT></TD> <TD> - </TD> <TD>modulus (positive remainder)</TD> |
| 658 |
</TR><TR> |
| 659 |
<TD><I>expr</I> <TT>+</TT> <I>expr</I></TD> <TD> - </TD> <TD>Addition |
| 660 |
</TR><TR> |
| 661 |
<TD><I>expr</I> <TT>-</TT> <I>expr</I></TD> <TD> - </TD> <TD>Subtraction</TD> |
| 662 |
</TR><TR> |
| 663 |
<TD><I>expr</I> <TT>*</TT> <I>expr</I></TD> <TD> - </TD> <TD>Multiplication</TD> |
| 664 |
</TR><TR> |
| 665 |
<TD><I>expr</I> <TT>/</TT> <I>expr</I></TD> <TD> - </TD> <TD>Division</TD> |
| 666 |
</TR> |
| 667 |
</TABLE> |
| 668 |
</CENTER> |
| 669 |
|
| 670 |
<P>An expression that consist of Integers only will have an Integer |
| 671 |
result, but in expressions that contain Float values, all Integers are |
| 672 |
promoted to Float. An <A HREF="errorcodes.shtml#ERRNO896">error</A> is given |
| 673 |
if any side of an expression is not a numeric value type.</P> |
| 674 |
</DD></DL> |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
<HR ALIGN=CENTER WIDTH=50%> |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
<A NAME="TABLE_FUNC"><H4>4.4. Table functions</H4></A> |
| 683 |
<DL><DD><!-- indent section --> |
| 684 |
|
| 685 |
<P>As well as functions on individual expression value, a range of |
| 686 |
functions are provided to calculate aggregate values over |
| 687 |
lists of values. The table functions will only consider <B>non-NULL</B> values from the list. |
| 688 |
<BR> |
| 689 |
Such a list is a single column in a table. Table functions therefore work on tables |
| 690 |
with only one column, or on table expressions |
| 691 |
where the selected column is explicitly specified.</P> |
| 692 |
|
| 693 |
|
| 694 |
<P>In order to construct a table for the table function to work on, we have |
| 695 |
had to introduce another, internal select statement. These can either be |
| 696 |
valid on their own, or be dependent on variables defined in an outer query. |
| 697 |
The argument for a table function can also be a table variable, previously assigned.</P> |
| 698 |
|
| 699 |
<CENTER> |
| 700 |
<TABLE CELLPADDING=3 WIDTH=80%> |
| 701 |
<TR BGCOLOR=#bfbfbf> |
| 702 |
<TH>Table function</TH><TH></TH><TH>return value</TH> |
| 703 |
</TR><TR> |
| 704 |
<TD><EM>count</EM> </TD><TD> - </TD> <TD>number of values</TD> |
| 705 |
</TR><TR> |
| 706 |
<TD><EM>sum</EM> </TD><TD> - </TD> <TD>sum of (Int,Float)values</TD> |
| 707 |
</TR><TR> |
| 708 |
<TD><EM>avg</EM> </TD><TD> - </TD> <TD>average over (Int,Float) values</TD> |
| 709 |
</TR><TR> |
| 710 |
<TD><EM>min</EM> </TD><TD> - </TD> <TD>smallest/earliest (Int,Float,DateType) value</TD> |
| 711 |
</TR><TR> |
| 712 |
<TD><EM>max</EM> </TD><TD> - </TD> <TD>greatest/latest (Int,Float,DateType) value</TD> |
| 713 |
</TR><TR> |
| 714 |
<TD><EM>first</EM> </TD><TD> - </TD> <TD>first value in the list</TD> |
| 715 |
</TR><TR> |
| 716 |
<TD><EM>last</EM> </TD><TD> - </TD> <TD>last value in the list</TD> |
| 717 |
</TR> |
| 718 |
</TABLE> |
| 719 |
</CENTER> |
| 720 |
|
| 721 |
<P>These 2 examples demonstrate the use of table functions:-</P> |
| 722 |
|
| 723 |
<P><CODE><EM>select</EM> a, a->paper <EM>from</EM> a <EM>in class</EM> author |
| 724 |
<EM>where</EM> <EM>count</EM>(<EM>select</EM> a->Paper) > 5</CODE></P> |
| 725 |
|
| 726 |
<P><CODE>$x := <EM>sum</EM>(<EM>select</EM> b->price <EM>from</EM> b <EM>in class</EM> Books)</CODE></P> |
| 727 |
|
| 728 |
<P>The query :-</P> |
| 729 |
|
| 730 |
<P><CODE>@AH6subseq := <EM>select</EM> seq::x, size::(x[2]-x[1]) |
| 731 |
<BR> |
| 732 |
<EM>from object</EM>("Sequence","AH6")->Subsequence <EM>as</EM> x ; |
| 733 |
<EM>select min</EM> @AH6subseq:size</CODE></P> |
| 734 |
|
| 735 |
<P>is equivalent to :</P> |
| 736 |
|
| 737 |
<P><CODE><EM>select min</EM> (<EM>select</EM> seq::x, size::(x[2]-x[1])) |
| 738 |
<BR> |
| 739 |
<EM>from object</EM>("Sequence","AH6")->Subsequence <EM>as</EM> x</CODE></P> |
| 740 |
|
| 741 |
</DD></DL> |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
<HR ALIGN=CENTER WIDTH=50%> |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
<A NAME="LOC_METHOD"><H4>4.5. Methods</H4></A> |
| 751 |
<DL><DD><!-- indent section --> |
| 752 |
|
| 753 |
<P>Objects and certain value types have builtin methods. Here is an example:-</P> |
| 754 |
|
| 755 |
<P><CODE><EM>select</EM> a->Full_name <EM>from</EM> a <EM>in class</EM> author <EM>where</EM> a->Full_Name.<EM>length</EM> > 10</CODE></P> |
| 756 |
|
| 757 |
<CENTER> |
| 758 |
<TABLE CELLPADDING=3 WIDTH=80%> |
| 759 |
<TR BGCOLOR=#bfbfbf> |
| 760 |
<TH>method name</TH><TH>locator type</TH><TH>return value</TH> |
| 761 |
</TR><TR> |
| 762 |
<TD><EM>name</EM></TD> <TD> <I>Object</I> </TD> <TD><I>Text</I>-value for the object's name</TD> |
| 763 |
</TR><TR> |
| 764 |
<TD><EM>class</EM></TD> <TD> <I>Object</I> </TD> <TD><I>Text</I>-value for the object's class</TD> |
| 765 |
</TR><TR> |
| 766 |
<TD><EM>length</EM></TD> <TD> <I>Text</I> </TD> <TD><I>Int</I>-value for length of the Text-string</TD> |
| 767 |
</TR><TR> |
| 768 |
<TD><EM>create_session</EM></TD> <TD> <I>Object</I> </TD> <TD>UserSession <I>Object</I> of the session in which the object was first saved</TD> |
| 769 |
</TR><TR> |
| 770 |
<TD><EM>create_time</EM></TD> <TD> <I>Object</I> </TD> <TD><I>DateType</I>-value when the object was first created</TD> |
| 771 |
</TR><TR> |
| 772 |
<TD><EM>update_session</EM></TD> <TD> <I>Object</I> </TD> <TD>UserSession <I>Object</I> of the session in which the object was last updated</TD> |
| 773 |
</TR><TR> |
| 774 |
<TD><EM>update_time</EM></TD> <TD> <I>Object</I> </TD> <TD><I>DateType</I>-value when the object was last updated</TD> |
| 775 |
</TR><TR> |
| 776 |
<TD><EM>node_session</EM></TD> <TD> <I>Tag</I> </TD> <TD>UserSession <I>Object</I> of the session when the tag was last added/updated</TD> |
| 777 |
</TR><TR> |
| 778 |
<TD><EM>node_time</EM></TD> <TD> <I>Tag</I> </TD> <TD><I>DateType</I>-value when the tag was last added/updated</TD> |
| 779 |
</TR> |
| 780 |
</TABLE> |
| 781 |
</CENTER> |
| 782 |
|
| 783 |
<P><STRONG>Note :</STRONG> Objects are always shown by their name in the result table, which basically |
| 784 |
implies a <EM>.name</EM> method for each <I>Object</I>-iterator in the select-clause. It is therefor not |
| 785 |
needed explicitly.</P> |
| 786 |
|
| 787 |
<A NAME="TIMESTAMP"><H4>Timestamp methods</H4></A> |
| 788 |
|
| 789 |
<P>The timestamp methods are a powerful tool to track changes in the database. The underlying mechanism |
| 790 |
has been added to the Acedb-lernel at version 4.8c. All changes to the database have to be |
| 791 |
made with database code Version 4.8c or greater for the timestamping to work correctly.</P> |
| 792 |
|
| 793 |
<P>The objects themselves and each tag carry timestamps. A timestamp can be returned either as a |
| 794 |
<I>DateType</I>-value or as a UserSession <I>Object</I>. The date values can be used with |
| 795 |
the <A HREF="#DATE_FUNC">date functions</A> like <CODE>yeardiff</CODE>, <CODE>daydiff</CODE>. |
| 796 |
The <CODE>?UserSession</CODE> object belongs to a built-in system class and has the following |
| 797 |
model :-</P> |
| 798 |
<PRE> |
| 799 |
?UserSession Session UNIQUE ?Session |
| 800 |
Start UNIQUE DateType |
| 801 |
Finish UNIQUE DateType |
| 802 |
User UNIQUE Text |
| 803 |
</PRE> |
| 804 |
|
| 805 |
<P>The two <I>DateType</I>-values <CODE>Start</CODE> and <CODE>Finish</CODE> in this object |
| 806 |
denote the time from gaining write-access and saving the changes in any |
| 807 |
acedb-session. Those times are always in the most complete date/time format |
| 808 |
YYYY-MM-DD_HH:MM:SS. |
| 809 |
<BR> |
| 810 |
The tag <CODE>Session</CODE> links to another object of the system class |
| 811 |
<CODE>?Session</CODE> whcih has the following built-in system model:-</P> |
| 812 |
|
| 813 |
<PRE> |
| 814 |
?Session Session UNIQUE Int |
| 815 |
Permanent_session // If set, prevents automatic destruction |
| 816 |
Date UNIQUE Text |
| 817 |
User UNIQUE Text |
| 818 |
Session_Title UNIQUE Text |
| 819 |
CodeRelease UNIQUE Int UNIQUE Int |
| 820 |
DataRelease UNIQUE Int UNIQUE Int |
| 821 |
Created_from UNIQUE ?Session |
| 822 |
Up_linked_to UNIQUE ?Session |
| 823 |
Destroyed_by_session UNIQUE Int |
| 824 |
GlobalLex UNIQUE Int |
| 825 |
SessionLex UNIQUE Int UNIQUE Int UNIQUE Int UNIQUE Int |
| 826 |
VocLex UNIQUE Int UNIQUE Int UNIQUE Int UNIQUE Int |
| 827 |
GlobalBat UNIQUE Int UNIQUE Int // address # blocks used |
| 828 |
BatPlus UNIQUE ?Bat UNIQUE Int // # block set |
| 829 |
BatMinus UNIQUE ?Bat UNIQUE Int // # blocks freed |
| 830 |
IndexVersion UNIQUE Int // automatic indexing |
| 831 |
</PRE> |
| 832 |
|
| 833 |
</DD></DL> |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
<HR ALIGN=CENTER WIDTH=50%> |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
<A NAME="DATE_FUNC"><H4>4.6. Date functions</H4></A> |
| 842 |
<DL><DD><!-- indent section --> |
| 843 |
|
| 844 |
<P>Dates in acedb are extremely flexible. Any level of detail |
| 845 |
(always starting with a year) can be specified. So <TT>`1998-08-12</TT> |
| 846 |
is not automatically treated as <TT>`1998-08-12_12:07:36</TT>, |
| 847 |
it just says that the time of day on the 12th of August is not known or not important.</P> |
| 848 |
|
| 849 |
<P>This leveling, however imposes the need for functionality to access specific parts of the |
| 850 |
Date/Time structure. <A HREF="#DATE_COMPARISON">Section 4.1</A> showed how tricky it |
| 851 |
can be to determine whether a date is earlier/later than another. Using comparison operators |
| 852 |
it is decided upon the deepest detail level that is shared by both dates.</P> |
| 853 |
|
| 854 |
<P>In order to calculate <B>time spans</B> or make comparative decisions based on specific |
| 855 |
Date/Time elements a set of functions is provided. Both arguments are expressions, that |
| 856 |
will evaluate to a <I>DateType</I>-value. |
| 857 |
The result value is always of type integer; the result is positive, if the first argument is earlier |
| 858 |
than the second one.</P> |
| 859 |
|
| 860 |
<CODE><EM>select</EM> a, Age::<EM>yeardiff</EM>(a->Date_of_birth, <EM>today</EM>) <EM>from</EM> a <EM>in class</EM> Author</CODE> |
| 861 |
|
| 862 |
<P><STRONG>Note</STRONG> that the evaluation fails, if either date doesn't specify the requested |
| 863 |
level of detail, no value is returned in this case. e.g. |
| 864 |
<TT><EM>monthdiff</EM>(`1997-10-12, `1998)</TT> --> NULL</P> |
| 865 |
|
| 866 |
<P>The following date functions are provided:-</P> |
| 867 |
|
| 868 |
<DL><DD><!-- indent the table --> |
| 869 |
<TABLE CELLPADDING=3 WIDTH=30%> |
| 870 |
<TR BGCOLOR=#bfbfbf> |
| 871 |
<TH>Date function</TH> |
| 872 |
</TR><TR> |
| 873 |
<TD><EM>yeardiff</EM></TD> |
| 874 |
</TR><TR> |
| 875 |
<TD><EM>monthdiff</EM></TD> |
| 876 |
</TR><TR> |
| 877 |
<TD><EM>daydiff</EM></TD> |
| 878 |
</TR><TR> |
| 879 |
<TD><EM>hourdiff</EM></TD> |
| 880 |
</TR><TR> |
| 881 |
<TD><EM>mindiff</EM></TD> |
| 882 |
</TR><TR> |
| 883 |
<TD><EM>secdiff</EM></TD> |
| 884 |
</TR> |
| 885 |
</TABLE> |
| 886 |
</DD></DL> |
| 887 |
|
| 888 |
</DD></DL> |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
<HR> |
| 893 |
|
| 894 |
|
| 895 |
<H3>5. Advanced features</H3> |
| 896 |
|
| 897 |
<A NAME="ORDER_BY"><H4>5.1. Table sorting</H4></A> |
| 898 |
<DL><DD><!-- indent section --> |
| 899 |
|
| 900 |
<P>The keyword <EM>order</EM> can be specified after any select-from-where table expression. It can have |
| 901 |
an optional parameter <EM>asc</EM> or <EM>desc</EM>. The default is ascending order. |
| 902 |
If the <EM>order</EM> keyword is used on its own like that the column sorting precedence is left to right. |
| 903 |
</P> |
| 904 |
|
| 905 |
<P>With the addition of the <EM>by</EM> keyword one can specify the precise column precedence of the |
| 906 |
table sorting. The arguments of <EM>by</EM> |
| 907 |
is a list of sort columns, specified by a colon '<TT>:</TT>', followed by the column number or name. |
| 908 |
</P> |
| 909 |
|
| 910 |
<P>This example uses the <A HREF="#ALIAS_OP">field naming operator</A> |
| 911 |
assign a name to the second select-field expression, |
| 912 |
which is then used by the <EM>order by</EM>-clause to refer to that column:-</P> |
| 913 |
|
| 914 |
<CODE><EM>select</EM> s, size::s[2]-s[1]<BR> |
| 915 |
<EM>from</EM> s <EM>in object</EM>("Sequence","LINK_AH10")->Subsequence<BR> |
| 916 |
<EM>order by</EM> :size</CODE> |
| 917 |
|
| 918 |
<P>If more than one sort column is specified (separated by a comma), the left-most is the |
| 919 |
most significant - equal values in that column will be arranged according to the next |
| 920 |
sort criterion etc. </P> |
| 921 |
|
| 922 |
<P>Additionally every sort criterion can have an optional parameter |
| 923 |
<EM>asc</EM> or <EM>desc</EM> to specify sorting in ascending or descending order.</P> |
| 924 |
|
| 925 |
<P>This is an example using a fictional personell database:-</P> |
| 926 |
|
| 927 |
<CODE><EM>select</EM> s->First_Name, s->Last_Name<BR> |
| 928 |
<EM>from</EM> s <EM>in</EM> <EM>class</EM> Staff<BR> |
| 929 |
<EM>order by</EM> :2 <EM>asc</EM>, :1 <EM>desc</EM></CODE> |
| 930 |
|
| 931 |
<P>might produce the following:-</P> |
| 932 |
|
| 933 |
<PRE> |
| 934 |
Walter Adams |
| 935 |
Peter Adams |
| 936 |
John Adams |
| 937 |
Nigel Clark |
| 938 |
Jim Clark |
| 939 |
John Cleese |
| 940 |
... |
| 941 |
</PRE> |
| 942 |
|
| 943 |
</DD></DL> |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
<HR ALIGN=CENTER WIDTH=50%> |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
<A NAME="TABLE_OP"><H4>5.2. Table operators</H4></A> |
| 953 |
<DL><DD><!-- indent section --> |
| 954 |
|
| 955 |
<P>Any two tables (either select-from-where expressions or table variables) |
| 956 |
can be combined using standard set operators.</P> |
| 957 |
|
| 958 |
<P>The result is again a table with all duplicates removed (where a row occurs in both tables).</P> |
| 959 |
|
| 960 |
<P>Both tables have to be precisely <B>column-compatible</B> - they have |
| 961 |
to have the same number of columns and the value-types of the columns must |
| 962 |
match. The table operation is performed after both tables have been evaluated |
| 963 |
individually, which can take quite long sometimes.</P> |
| 964 |
|
| 965 |
<CENTER> |
| 966 |
<TABLE CELLPADDING=3 WIDTH=80%> |
| 967 |
<TR BGCOLOR=#bfbfbf> |
| 968 |
<TH>Table operator</TH><TH></TH><TH>result</TH> |
| 969 |
</TR><TR> |
| 970 |
<TD><I>table</I> <EM>union</EM> <I>table</I></TD> <TD> - </TD> <TD>all rows from both tables</TD> |
| 971 |
</TR><TR> |
| 972 |
<TD><I>table</I> <EM>diff</EM> <I>table</I></TD> <TD> - </TD> <TD>rows from the first table except |
| 973 |
the ones that are also in the second table, a.k.a. <EM>except</EM></TD> |
| 974 |
</TR><TR> |
| 975 |
<TD><I>table</I> <EM>intersect</EM> <I>table</I></TD> <TD> - </TD> <TD>rows that are in one table but not the other</TD> |
| 976 |
</TR> |
| 977 |
</TABLE> |
| 978 |
</CENTER> |
| 979 |
</DD></DL> |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
<P><HR ALIGN=CENTER WIDTH=50%></P> |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
<A NAME="CONTEXT_VAR"><H4>5.3. Context variables</H4></A> |
| 989 |
<DL><DD><!-- indent section --> |
| 990 |
<P>It is possible for the program that uses AQL functionality to provide |
| 991 |
a context for a query. The context is a set of predefined variables, |
| 992 |
whose value is readily available for any subsequent query.<BR> |
| 993 |
<STRONG>Note</STRONG> that this is an implementation dependent feature of AQL.</P> |
| 994 |
|
| 995 |
<H4>Tace and the active KeySet</H4> |
| 996 |
|
| 997 |
In <TT>tace</TT>, every query has access to a preset <A HREF="#TABLE_VAR">table variable</A> |
| 998 |
<TT>@active</TT>. It is a table of one column of <I>Object</I>-values |
| 999 |
that represent the KeySet from the previous query.</P> |
| 1000 |
|
| 1001 |
</DD></DL> |
| 1002 |
|
| 1003 |
<PRE> |
| 1004 |
acedb> find author |
| 1005 |
|
| 1006 |
// Found 5178 objects in this class |
| 1007 |
// 5178 Active Objects |
| 1008 |
acedb> aql <CODE><EM>select</EM> :1 <EM>from</EM> @active <EM>where</EM> :1->Mail = "U.K."</CODE> |
| 1009 |
"Berks M" |
| 1010 |
"Sulston JE" |
| 1011 |
... |
| 1012 |
"Wolstenholme AJ" |
| 1013 |
"Yeung M" |
| 1014 |
// 33 Active Objects |
| 1015 |
acedb> |
| 1016 |
</PRE> |
| 1017 |
|
| 1018 |
<DL><DD><!-- indent section --> |
| 1019 |
|
| 1020 |
After each AQL query the new active KeySet will be the first column |
| 1021 |
with <I>Object</I>-values from the results table of the last query. |
| 1022 |
|
| 1023 |
|
| 1024 |
<H4>Command line arguments in taql</H4> |
| 1025 |
|
| 1026 |
In <TT>taql</TT>, the user can pass in the value of a scalar variable |
| 1027 |
using the command line (the <TT>-param</TT> argument). This enable sthe use of shell or perl |
| 1028 |
script to supply different values, and the pre-formed query |
| 1029 |
will stay the same, because it references a scalar variable. |
| 1030 |
<BR> |
| 1031 |
Type <TT>taql -h</TT> for help on command line options. |
| 1032 |
</DD></DL> |
| 1033 |
|
| 1034 |
|
| 1035 |
<HR> |
| 1036 |
<ADDRESS><A HREF="mailto:[email protected]">[email protected]</A></ADDRESS> |
| 1037 |
|
| 1038 |
</BODY> |
| 1039 |
</HTML> |