Advanced

Schemas

Persisted relvars in the same directory form a schema. They can be queried together and any modifications are first checked against constraints in the schema's $constraint relvar.

When Ra is started, the current directory becomes the current schema. This means the directory is used for persistent relvar storage. No other process should access the directory while Ra is running.

Each schema directory can have an optional init.ra file which is executed when Ra first loads a schema. It is executed after any persisted relvars have been loaded from the directory into memory. It can be used to initialise relvars if they don't exist yet, or modify the structure or contents of ones that do exist, according to some logic. For example, a common pattern is to declare the schema's initial relvars if they don't already exist, and otherwise leave them with any subsequent modifications intact. e.g.

if not assigned($S) then begin
	$S := {SNO:str, SNAME:str, STATUS:int, CITY:str}{
		  ["S1",    "Smith",   20,         "London"],
		  ["S2",    "Jones",   10,         "Paris"],
		  ["S3",    "Blake",   30,         "Paris"],
		  ["S4",    "Clark",   20,         "London"],
		  ["S5",    "Adams",   30,         "Athens"],
	}
end

Here, the assigned function returns true if the relvar exists, i.e. was loaded from the schema directory.

Most init.ra schema files should start by creating the $constraint relvar, e.g.

if not assigned($constraint) then begin
	$constraint := {name:str, rule:str}{
	}
end

The current schema can be referenced by the schema tuple. For example, to show the variables in the schema we can use:

heading(schema)
attrtype
$P{PNO:str,PNAME:str,COLOR:str,WEIGHT:int,CITY:str}
$S{SNO:str,SNAME:str,STATUS:int,CITY:str}
$SP{SNO:str,PNO:str,QTY:int}
$constraint{name:str,rule:str}

Any relvars in there with a $ prefix are persisted.