Lesson 3: Reading data
Fetching simple data
-
Fetch queries are used to retrieve data in JSON format. They comprise a
matchclause andfetchclause.match # match clause fetch { # fetch clause }; -
Variables are declared with a
$prefix. -
An
isastatement is used to specify the type of a variable.$entity isa entity-type; -
Data objects can be either entities or relations.
-
Relations have roles. An entity that plays a role is a roleplayer. All relations must have at least one roleplayer.
$rel isa relation-type, links ($role-1: $a, $role-2: $b); -
A
hasstatement is used to specify the value of an entity or relation’s attribute.$entity has attribute-type "attribute value";
Fetching polymorphic data
-
Type inference allows the database to infer the possible types of a variable without having to explicitly specify them all.
-
Inheritance polymorphism allows us to query data of multiple types through a common supertype.
-
Interface polymorphism allows us to query data of multiple types through a common implemented interface: either a role in a relation or ownership of an attribute.
-
Parametric polymorphism allows us to query data of multiple types through the structure of the data alone. Purely parametric queries can be run on any schema.
-
Different types of polymorphism can be combined to produce complex queries.
Using functions
-
Functions allow you to encode modular and re-usuable sub-queries and logic in either queries or even the schema
Fetching schema types
-
Types can be variablized in the same way as data instances, by using a variable in place of a type label in statements.
$entity isa $entity-type; -
An
isa!statement is used to specify the exact type of a data variable.$entity isa! exact-type; -
A
substatement is used to specify a supertype of a type.$type sub supertype; -
A
sub!statement is used to specify the direct supertype of a type.$type sub! direct-supertype; -
An
ownsstatement is used to specify an owner of an attribute type.$entity-type owns attribute-type; -
A
relatesstatement is used to specify a role of a relation type.relation-type relates $role; -
A
playsstatement is used to specify a roleplayer of a role.$entity-type plays $role;
Query validation
-
All queries are validated against the schema.
-
Running a query that does not conform to the schema will cause an exception to be thrown.
-
This is like a compilation failure in a strongly-typed programming language!