Subsections


Array Deferencing

OQL provides polymorphic single and range deferencing. The single deferencing is used to get one element in an ordered collection or a character in a string or one element in a non-collection array. The range deferencing is used to get several elements.
The deferencing of ordered collections is introduced in more details in Section 5.24.


Single Deferencing

The single deferencing operator takes as its first operand an atom of type string, an indexed (or ordered) collection (list or array) or a non-collection array. The second operand must be of type integer. Depending on the type of the first operand, the returned atom is as follows:
  1. if the first operand is a string, the returned atom is the #expr character of the string where expr denotes the second operand. If expr is equal to the length to the string the character '\000' is returned. If it greater than the length the string an out of bounds error is raised.
  2. for an ordered collection, the returned atom is the #expr item of the collection. If expr is greater than or is equal to the size of the collection, an out of bounds error is raised.
  3. if the first operand is an non-collection array, the returned atom is the #expr item of the array. If expr is greater than or is equal to the size of the array, an out of bounds error is raised.
The single deferencing operator may be used as a left value, that means that a single deferencing expression is assignable. For instance the sequence of statements:
s := "hello";
s[1] := 'E';
s[4] := 'O';
set the variable s to "hEllO".

The single deferencing operator may be used everywhere in a path expression. For instance, first(select Person).other_addrs[2].street[3], denotes the character #3 of the street attribute in the #2 other_addrs non-collection array attribute of the first Person instance.
General Information
Operator []
Syntaxe expr [expr ]
Type binary
Commutative no
Operand Types first operand: string, indexed collection (list or array) or non-collection array, second operand: integer
Result Type char if first operand is a string, otherwise type of the returned item in the indexed collection or non-collection array.
Functions [expr ] : returns the character (or item in the indexed collection or in the non-collection array) number expr
Note this operator may be used in the composition of a left value.

Expression Examples
expression result  
"hello"[0] 'h'  
a := "hello"; a[1] 'e'  
a[3] l  
a[6] raises an error  
a[0] := 'H' 'H' a equals "Hello"
list(1, 2, "hello", 4)[3] "hello"  
list(1, 2, "hello", 4)[4] raises an error  
first(select Person).name[2] := 'X' 'X'  


Range Deferencing

The range deferencing operators, [:] and [?], takes as their first operand an atom of type string, an indexed (or ordered) collection (list or array) or a non-collection array. The other operands must be of type integer. The [?] may have also an unordered collection (set or bag as its first operand.

The operator syntax and semantics are as follows: Contrary to the single deferencing operator, the range deferencing operator cannot be used as a left value.

The range deferencing operators may be used everywhere in a path expression. For instance, first(select Person).children[?].name[?], denotes the list of all characters of the name attribute in all the children of the first Person instance.
General Information
Operators [:]
  [?]
Syntaxes expr [expr :expr ]
  expr [?]
Type ternary or unary
Operand Types first operand: string or indexed collections (list or array), second operand and third operand: integer
Result Type a list of char if first operand is a string, otherwise a list of returned items in the indexed collection or non-collection array.
Functions [expr1:expr2] : returns a lits of characters (or items in collection) indexed from expr1 to expr2
  [?] : returns a list of all characters (or items in collection)

Expression Examples
expression result
"hello"[0:2] list('h', 'e', 'l')
"hello"[?] list('h', 'e', 'l', 'l', 'o', '\000')
list(1, 2, "hello", 4)[2:3] list("hello", 4)
array(1, 2, "hello", 4)[?] list(1, 2, "hello", 4)
first(select Person).name[?] list('j', 'o', 'h', 'n', '\000')
list(select class.type = "user")[0:4].name list("Employee", "Address", "Person")

EyeDB manual