| 1 |
<!--#set var="title" value="AQL - Movie DB Tutorial" --> |
| 2 |
<!--#set var="icon" value="aql.gif" --> |
| 3 |
<!--#set var="author" value="fw" --> |
| 4 |
|
| 5 |
<!--#exec cgi="/cgi-bin/header" --> |
| 6 |
|
| 7 |
<H2 ALIGN=CENTER>AQL - Movie DB Tutorial</H2> |
| 8 |
|
| 9 |
|
| 10 |
<HR ALIGN=LEFT WIDTH=60%> |
| 11 |
<H3>Simple key selection</H3> |
| 12 |
|
| 13 |
<DL COMPACT> |
| 14 |
<DT>(1) |
| 15 |
<DD><CODE><B>select all from class movie</B></CODE> |
| 16 |
<DD><CODE><B>select all class movie</B></CODE> (the same, just shorter) <BR> |
| 17 |
<DD><SUB>(<I>list the key-values of all movies</I>)</SUB> |
| 18 |
<P><DL><DD> |
| 19 |
List all keys in the class <I>movie</I>. <BR> |
| 20 |
Note the system is not case sensitive when referring to |
| 21 |
database related terms, such as class names or tags. |
| 22 |
<BR><A HREF=result1.shtml>results</A> |
| 23 |
</DL> |
| 24 |
|
| 25 |
<P><DT>(2) |
| 26 |
<DD><CODE><B>select p from p in class person</B></CODE> |
| 27 |
<DD><SUB>(<I>list the key-values of all persons</I>)</SUB> |
| 28 |
<P><DL><DD> |
| 29 |
A locator-variable <I>p</I> is defined to loop over the key-values in class <I>person</I>. |
| 30 |
The query works exactly like the (1), but this query can be extended easily, |
| 31 |
as <I>p</I> can be referred to for tag-dereferencing, column-wise sorting etc.. |
| 32 |
<BR><A HREF=result2.shtml>results</A> |
| 33 |
</DL> |
| 34 |
|
| 35 |
</DL> |
| 36 |
|
| 37 |
<P><HR ALIGN=LEFT WIDTH=60%> |
| 38 |
<H3>Tag dereferencing to access object attributes</H3> |
| 39 |
<DL COMPACT> |
| 40 |
|
| 41 |
<P><DT>(3) |
| 42 |
<DD><CODE><B>select m->Title, m->Director from m in class Movie</B></CODE> |
| 43 |
<DD><SUB>(<I>list the titles of movies and the key-values of their directors</I>)</SUB> |
| 44 |
<P><DL><DD> |
| 45 |
The <U>from-clause</U> defines a locator variable <I>m</I> that loops over all key-values of class movie.<BR> |
| 46 |
The <U>select-clause</U> defines two <U>select-fields</U>, that are tags in the <I>movie</I> class. |
| 47 |
The <I>Title</I> tag returns a string, whereas the <I>Director</I> tag returns another key from the <I>Person</I> |
| 48 |
class. This key can be dereferenced further to gain access to the tags in the <I>Person</I> |
| 49 |
class for all the stored values. |
| 50 |
<BR><A HREF=result3.shtml>results</A> |
| 51 |
</DL> |
| 52 |
|
| 53 |
|
| 54 |
<P><DT>(4) |
| 55 |
<DD><CODE><B>select m->Title, m->Director->Full_name from m in class Movie</B></CODE> |
| 56 |
<DD><SUB>(<I>list the titles of movies and the names of their directors</I>)</SUB> |
| 57 |
<P><DL><DD> |
| 58 |
Basically the same as (3), but the result of <I>m->Director</I> has been dereferenced to |
| 59 |
get the <I>Full_name</I> tag value for the particular key from the <I>Person</I> class. |
| 60 |
<BR><A HREF=result4.shtml>results</A> |
| 61 |
</DL> |
| 62 |
|
| 63 |
|
| 64 |
<P><DT>(5) |
| 65 |
<DD><CODE><B>select a->Full_name from m in class Movie, a in m->Based_on->Author</B></CODE> |
| 66 |
<DD><SUB>(<I>list the names of authors of books that movies are based on</I>)</SUB> |
| 67 |
<P><DL><DD> |
| 68 |
The <U>from-clause</U> defines two locators - <I>m</I> loops over the <I>Movie</I> class |
| 69 |
and <I>a</I> returns the <I>Person</I>-key, that is stored with the <I>Book</I>-key |
| 70 |
for movies that are based on novels or plays.<BR> |
| 71 |
The <U>select-clause</U> defines one result column that lists the <I>Full_name</I> |
| 72 |
tag values for the key-values in <I>a</I>. Some authors names will be listed more than once, if |
| 73 |
they've written a few novels on which movies were based. |
| 74 |
<BR> |
| 75 |
Note that is isn't important where the dereferencing is done. The above query is the same as |
| 76 |
</DL> |
| 77 |
<DD><CODE><B>select m->Based_on->Author->Full_name from m in class Movie</B></CODE> |
| 78 |
<DL><DD> |
| 79 |
which performs the same without the need for an extra variable <I>a</I>. |
| 80 |
<BR><A HREF=result5.shtml>results</A> |
| 81 |
</DL> |
| 82 |
|
| 83 |
<P><DT>(6) |
| 84 |
<DD><CODE><B>select p->Full_name, p->Address[2] from p in class person</B></CODE> |
| 85 |
<DD><SUB>(<I>list all people and all their address info</I>)</SUB> |
| 86 |
<P><DL><DD> |
| 87 |
This query makes use of <U>tag-positioning</U>. You will notice that the fields just right of the |
| 88 |
<I>Address</I> tag in the <I>Person</I> class contain no actual data, they just introduce |
| 89 |
some more structure. This query will therefor return all lines that are stored 2 positions |
| 90 |
to the right of <I>Address</I>, and list all <I>Mail</I>, <I>Email</I> or <I>Phone</I> information that is stored. |
| 91 |
<BR><A HREF=result6.shtml>results</A> |
| 92 |
</DL> |
| 93 |
|
| 94 |
|
| 95 |
<P><DT>(7) |
| 96 |
<DD><CODE><B>select p->Full_name, p->Phone from p in class person</B></CODE> |
| 97 |
<DD><SUB>(<I>list all people and their phone numbers</I>)</SUB> |
| 98 |
<P><DL><DD> |
| 99 |
In order to access specific items in the <I>Address</I> sub-field, |
| 100 |
The required tag can be stated without having to refer to <I>Address</I>. |
| 101 |
Such shortcuts come in handy in deeply nested class models, but the design |
| 102 |
of the model needs to make sure that tag names aren't duplicated within a model. |
| 103 |
<BR><A HREF=result7.shtml>results</A> |
| 104 |
</DL> |
| 105 |
</DL> |
| 106 |
|
| 107 |
<P><HR ALIGN=LEFT WIDTH=60%> |
| 108 |
<H3>Conditional expressions in the where-clause</H3> |
| 109 |
<DL COMPACT> |
| 110 |
|
| 111 |
<P><DT>(8) |
| 112 |
<DD><CODE><B>select m->Title, d->Full_name from m in class Movie, d in m->Director where exists d</B></CODE> |
| 113 |
<DD><SUB>(<I>for all movies that have directors, list with their title and the director's name</I>)</SUB> |
| 114 |
<P><DL><DD> |
| 115 |
Similar to (4), but omitting the <I>Movie</I>-keys in <I>m</I> for which the <I>Director</I>-tag doesn't exist. |
| 116 |
As shown in (5), it is not necessary to define another locator <I>d</I> :-<P> |
| 117 |
</DL> |
| 118 |
<DD><CODE><B>select m->Title, m->Director->Full_name from m in class movie where exists m->Director</B></CODE> |
| 119 |
<DL><DD> |
| 120 |
<P> |
| 121 |
Note the difference to the output of (4) - this time AQL only lists movies where a director tag exists. |
| 122 |
This demonstrates one of the basic philosophies about data storage in ACeDB - if a tag doesn't exist for an object |
| 123 |
it doesn't store that fact (i.e. a NULL value for that tag) it simple doens't store it. |
| 124 |
<BR><A HREF=result8.shtml>results</A> |
| 125 |
</DL> |
| 126 |
|
| 127 |
|
| 128 |
<P><DT>(9) |
| 129 |
<DD><CODE><B>select m->Cast->Full_name from m in class Movie where m = "truerom"</B></CODE> |
| 130 |
<DD><SUB>(<I>list the names of cast members in "True Romance"</I>)</SUB> |
| 131 |
<P><DL><DD> |
| 132 |
By finding the key "truerom" in the <I>movie</I> class, it finds all <I>Person</I>-keys stored |
| 133 |
under the <I>Cast</I>-tag. Those keys are dereferenced to be displayed as the full names. |
| 134 |
The same result can be gathered by finding all the people that have starred in "True Romance".<BR> |
| 135 |
Note that keys are compared to by their string value - i.e. in quotes. |
| 136 |
</DL> |
| 137 |
<DD><CODE><B>select p->Full_name from p in class person where p->Stars_in->Title = "True Romance"</B></CODE> |
| 138 |
<DL><DD> |
| 139 |
<P> |
| 140 |
This shows an interesting aspect of AQL - It ispossible to get the same result using totally different queries. |
| 141 |
The same result can also be expressed using the <B>object</B> construct which will be explained later. |
| 142 |
<DD><CODE><B>select object("movie", "truerom")->cast->full_name</B></CODE> |
| 143 |
<BR><A HREF=result9.shtml>results</A> |
| 144 |
</DL> |
| 145 |
|
| 146 |
|
| 147 |
<P><DT>(10) |
| 148 |
<DD><CODE><B>select m->Title, year from m in class Movie, year in m->Released where year > `1990</B></CODE> |
| 149 |
<DD><SUB>(<I>list the movies that have been relased after 1990, i.e. in 1991 and from thereon</I>)</SUB> |
| 150 |
<P><DL><DD> |
| 151 |
This condition uses a date literal which is stated using the <CODE><B>`</B></CODE> syntax. |
| 152 |
This query assumes automatically an <CODE><B>exists year</B></CODE> expression in the <U>where-clause</U>, |
| 153 |
because it couldn't otherwise evaluate the expression <CODE><B>year > `1990</B></CODE> |
| 154 |
for movies that have no <I>Released</I> date value.<BR> |
| 155 |
Look at (14) for how AQL processes that query internally. |
| 156 |
<BR><A HREF=result10.shtml>results</A> |
| 157 |
</DL> |
| 158 |
|
| 159 |
<P><DT>(11) |
| 160 |
<DD><CODE><B>select m, m->Released from m in class movie where not exists m->Released</B></CODE> |
| 161 |
<DD><SUB>(<I>list the keys of all movies for which no release year information is stored</I>)</SUB> |
| 162 |
<P><DL><DD> |
| 163 |
Use of the <CODE><B>not</B></CODE> keyword negates the value of <CODE><B>exists m->Released</B></CODE> |
| 164 |
and thereby shows all movies that failed the <U>where-clause</U> evaluation check of (10). |
| 165 |
<BR><A HREF=result11.shtml>results</A> |
| 166 |
</DL> |
| 167 |
|
| 168 |
</DL> |
| 169 |
<P> |
| 170 |
The subject of expression evaluation in the <U>where-clause</U> is properly explored in the |
| 171 |
<A HREF=../semantics.shtml>AQL semantics</A> document, which explores relationships between different value types |
| 172 |
and their use in expressions, such as comparisons and arithmetic expressions. |
| 173 |
|
| 174 |
|
| 175 |
<P><HR ALIGN=LEFT WIDTH=60%> |
| 176 |
<H3>Date functions</H3> |
| 177 |
<DL COMPACT> |
| 178 |
|
| 179 |
<P><DT>(12) |
| 180 |
<DD><CODE><B>select p->Full_name, yeardiff(p->Born, today) from p in class Person</B></CODE> |
| 181 |
<DD><SUB>(<I>list all people and their age</I>)</SUB> |
| 182 |
<P><DL><DD> |
| 183 |
This query uses the AQL keyword <CODE><B>today</B></CODE>, which will be rendered to |
| 184 |
<CODE><B><!--#config timefmt="%C%y-%m-%d" --><!--#echo var="DATE_LOCAL" --></B></CODE> |
| 185 |
(i.e. the current date) during evalaution. If time-of-day information is important, |
| 186 |
the keyword <CODE><B>now</B></CODE>, will be rendered to |
| 187 |
<CODE><B><!--#config timefmt="%C%y-%m-%d_%H:%M:%S" --><!--#echo var="DATE_LOCAL" --></B></CODE>.<BR> |
| 188 |
The function <CODE><B>yeardiff</B></CODE> acts on two date values and returns an integer. |
| 189 |
<BR><A HREF=result12.shtml>results</A> |
| 190 |
</DL> |
| 191 |
|
| 192 |
<P><DT>(13) |
| 193 |
<DD><CODE><B>select p->Full_name, yeardiff(p->Born, today) from p in class Person where yeardiff(p->Born, today) > 55</B></CODE> |
| 194 |
<DD><SUB>(<I>for all people over 55, list their name and age</I>)</SUB> |
| 195 |
<P><DL><DD> |
| 196 |
Same as (12), but restricts the list to people with ages greater than 55. <BR> |
| 197 |
Note that only values for <I>p</I> can be included for which the <I>Born</I> tag exists. |
| 198 |
<BR><A HREF=result13.shtml>results</A> |
| 199 |
</DL> |
| 200 |
|
| 201 |
<P><DT>(14) |
| 202 |
<DD><CODE><B>select m->Title, year from m in class Movie, year in m->Released where yeardiff (year,`1990) > 0</B></CODE> |
| 203 |
<DD><SUB>(<I>list the movies that have been relased after 1990, i.e. in 1991 and from thereon</I>)</SUB> |
| 204 |
<P><DL><DD> |
| 205 |
Variation on (10), but this time using yeardiff explicitly. |
| 206 |
<BR><A HREF=result10.shtml>results</A> |
| 207 |
</DL> |
| 208 |
|
| 209 |
</DL> |
| 210 |
<P> |
| 211 |
Possible date-functions are :- |
| 212 |
<CODE><B> |
| 213 |
<UL> |
| 214 |
<LI>yeardiff |
| 215 |
<LI>monthdiff |
| 216 |
<LI>weekdiff |
| 217 |
<LI>daydiff |
| 218 |
<LI>hourdiff |
| 219 |
<LI>mindiff |
| 220 |
<LI>secdiff |
| 221 |
</UL> |
| 222 |
</B></CODE>. |
| 223 |
|
| 224 |
These functions will return a sensible result of the date/time value specifies the appropriate portion that the function requests. |
| 225 |
<P> |
| 226 |
No sensible result will be returned for <CODE><B>monthdiff (m->released, `1996)</B></CODE>, because although some |
| 227 |
movies feature release dates with month information, |
| 228 |
the date literal <CODE><B>'1996</B></CODE> does not and therefore the evaluation will always fail. |
| 229 |
<P> |
| 230 |
All these functions return an integer value, which is positive if left argument is the smaller (i.e. earlier) date. |
| 231 |
|
| 232 |
|
| 233 |
<P><HR ALIGN=LEFT WIDTH=60%> |
| 234 |
<H3>Table functions</H3> |
| 235 |
<DL COMPACT> |
| 236 |
|
| 237 |
<P><DT>(15) |
| 238 |
<DD><CODE><B>select p->Full_name, count(select p->Stars_in) from p in class Person</B></CODE> |
| 239 |
<DD><SUB>(<I>list all people and how many movies they've starred in</I>)</SUB> |
| 240 |
<P><DL><DD> |
| 241 |
The <U>table-function</U> <CODE><B>count</B></CODE> acts on the subquery <CODE><B>select p->Stars_in</B></CODE> |
| 242 |
and returns an integer value for all objects where the <I>Stars_in</I>-tag exists. It simply counts |
| 243 |
the number of times the <I>Stars_in</I>-tag is contained in each object <I>p</I>. |
| 244 |
<BR><A HREF=result15.shtml>results</A> |
| 245 |
</DL> |
| 246 |
|
| 247 |
<P><DT>(16) |
| 248 |
<DD><CODE><B>select m->Title from m in class Movie where count(select m->Cast) > 3</B></CODE> |
| 249 |
<DD><SUB>(<I>list all movies with more than 3 cast members</I>)</SUB> |
| 250 |
<P><DL><DD> |
| 251 |
The <U>table-function</U> <CODE><B>count</B></CODE> acts on the subquery <CODE><B>select p->Stars_in</B></CODE> |
| 252 |
and returns an integer value for all objects where the <I>Stars_in</I>-tag exists. |
| 253 |
<BR><A HREF=result16.shtml>results</A> |
| 254 |
</DL> |
| 255 |
|
| 256 |
<P><DT>(17) |
| 257 |
<DD><CODE><B>select avg(select yeardiff(p->Born, now) from p in class Actor)</B></CODE> |
| 258 |
<DD><SUB>(<I>returns the average age of all the actors</I>)</SUB> |
| 259 |
<P><DL><DD> |
| 260 |
This time a table function acts on the return-value of a table expression. |
| 261 |
The inner <U>select-query</U> returns a list of ages, over which the average value |
| 262 |
is computed. |
| 263 |
<BR><A HREF=result17.shtml>results</A> |
| 264 |
</DL> |
| 265 |
|
| 266 |
<P><DT>(18) |
| 267 |
<DD><CODE><B>select max(select m->released from m in class movie)</B></CODE> |
| 268 |
<DD><SUB>(<I>returns latest movie release date</I>)</SUB> |
| 269 |
<P><DL><DD> |
| 270 |
Similar to (17), <CODE><B>max</B></CODE> acts on a list of dates. |
| 271 |
<BR><A HREF=result18.shtml>results</A> |
| 272 |
</DL> |
| 273 |
|
| 274 |
</DL> |
| 275 |
<P> |
| 276 |
Possible table-functions are :- |
| 277 |
<CODE><B> |
| 278 |
<UL> |
| 279 |
<LI>count (all tables) |
| 280 |
<LI>min (Int, Float, DateType) **should allow Strings |
| 281 |
<LI>max (Int, Float, DateType) |
| 282 |
<LI>avg (Int, Float) |
| 283 |
<LI>sum (Int, Float) |
| 284 |
<LI>first (nyi) |
| 285 |
<LI>last (nyi) |
| 286 |
</UL> |
| 287 |
</B></CODE> |
| 288 |
|
| 289 |
All table functions will work on either accumulated object attributes <SUP>(15),(16)</SUP> or on the table |
| 290 |
values of whole <U>select-queries</U><SUP>(17),(18)</SUP>. All such tables expression have to produce a table with only one column, |
| 291 |
and in most cases they have to be of a specific value-type for the <U>table-function</U> to work. |
| 292 |
|
| 293 |
|
| 294 |
<P><HR ALIGN=LEFT WIDTH=60%> |
| 295 |
<H3>Scalar variables</H3> |
| 296 |
<DL COMPACT> |
| 297 |
|
| 298 |
<P><DT>(19) |
| 299 |
<DD><CODE><B>$x := count(select all class movie)</B></CODE> |
| 300 |
<DD><SUB>(<I>how many movies are stored</I>)</SUB> |
| 301 |
<P><DL><DD> |
| 302 |
Scalar variable have a perl-like '$' syntax and can assume just one value. |
| 303 |
This value is usually the return value of a table-function expression. |
| 304 |
A variable assignment is treated as a whole query expression in itself. |
| 305 |
<BR> |
| 306 |
The result will be a table with one column and one row containing the single |
| 307 |
return value that the variable <CODE><B>$x</B></CODE> will assume. |
| 308 |
The query is therefore equivalent to |
| 309 |
</DL><BR> |
| 310 |
<DD><CODE><B>select count(select all class movie)</B></CODE> |
| 311 |
<DL><DD> |
| 312 |
<BR><A HREF=result19.shtml>results</A> |
| 313 |
</DL> |
| 314 |
|
| 315 |
<P><DT>(20) |
| 316 |
<DD><CODE><B>$x := avg(select m->rating from m in class movie); <BR> |
| 317 |
select m->title, r from m in class movie, r in m->rating where r > $x</B></CODE> |
| 318 |
<DD><SUB>(<I>list movies with an higher than average rating</I>)</SUB> |
| 319 |
<P><DL><DD> |
| 320 |
The first query assigns a scalar variable to be the average rating of movie (ignoring |
| 321 |
movie for which no such value is stored). |
| 322 |
The second query then sues this value to list all movies for which the rating value is higher |
| 323 |
that that. |
| 324 |
<BR> |
| 325 |
This demonstrates how queries can be concatinated using the semi-colon ';'. Usually |
| 326 |
one or more queries will calculate variables, whereas the last query is a table expression |
| 327 |
that uses those variables to generate the output. |
| 328 |
<BR><A HREF=result20.shtml>results</A> |
| 329 |
</DL> |
| 330 |
|
| 331 |
</DL> |
| 332 |
|
| 333 |
<P><HR ALIGN=LEFT WIDTH=60%> |
| 334 |
<H3>Table variables</H3> |
| 335 |
<DL COMPACT> |
| 336 |
|
| 337 |
<!--#config timefmt="%d-%b-%C%y, %I:%M %p" --> |
| 338 |
<!--#exec cgi="/cgi-bin/footer" --> |