fastdb_Arrays
来源:互联网 发布:技术分析 知乎 编辑:程序博客网 时间:2024/05/22 07:50
Arrays
FastDB accepts arrays with dynamic length as components of records. Multidimensional arrays are not supported, but it is possible to define an array of arrays. It is possible to sort records in the result set by length of array field. FastDB provides a set of special constructions for dealing with arrays:
- It is possible to get the number of elements in the array by the
length()
function. - Array elements can be fetched by the
[]
operator. If an index expression is out of array range, an exception will be raised. - The operator
in
can be used to check if an array contains a value specified by the left operand. This operation can be used only for arrays of atomic type: with boolean, numeric, reference or string components. - Array can be updated using
update
method which creates copy of the array and returns non-constant reference. - Iteration through array elements is performed by the
exists
operator. A variable specified after theexists
keyword can be used as an index in arrays in the expression preceeded by theexists
quantor. This index variable will iterate through all possible array index values, until the value of the expression will becometrue
or the index runs out of range. The conditionexists i: (contract[i].company.location = 'US')
will select all details which are shipped by companies located in 'US', while the querynot exists i: (contract[i].company.location = 'US')
will select all details which are shipped from companies outside 'US'.Nested
exists
clauses are allowed. Using nestedexists
quantors is equivalent to nested loops using the correspondent index variables. For example the queryexists column: (exists row: (matrix[column][row] = 0))
will select all records, containing 0 in elements of amatrix
field, which has type array of array of integer. This construction is equivalent to the following two nested loops:bool result = false; for (int column = 0; column < matrix.length(); column++) { for (int row = 0; row < matrix[column].length(); row++) { if (matrix[column][row] == 0) { result = true; break; } } }
The order of using indices is essential! The result of the following query executionexists row: (exists column: (matrix[column][row] = 0))
数组
FastDB将动态数组作为记录的成员来看待。FastDB并不支持多维数组,但是可以通过定义数组中的数组来实现。可以根据数组字段的长度来给结果集中的记录排序。FastDB提供了一系列特定的函数来处理数组:
1、 使用函数length()获得数组元素的数目。
2、 可以通过操作符[]来取得数组的成员。如果中括号中的数组元素的下标超过了数组的范围,将会产生一个意外。
3、 可以使用操作符in 来确定在数组中是否包含in 操作符左边的操作数。
这个操作符号只能用在原子类型的数组中,即成员类型是:布尔类型、数值类型、指针类型或字符串类型。
4、数组可以通过Update()方法获得更新,该方法创建数组的拷贝并返回非常量指针。
5、 通过操作符exists来实现数组元素的循环。
关键字exists之后的变量可以看作是exists 操作符后表达式中数组的下标序号。这个下标变量将会叠代所有可能的数组下标值,直到表达式为真或者下标序号越界。
如下面: exists i: (contract[i].company.location = 'US')
将会把所有坐落在US 的公司运输过来的货物信息选择出来,而查询语句: not exists i: (contract[i].company.location = 'US') 将会把所有坐落在US 之外的公司运输过来的货物信息选择出来。 exists允许嵌套,其嵌套的语句等同于使用相应下标变量的嵌套循环。 例如下面的查询语句: exists column: (exists row: (matrix[column][row] = 0)) 将会选择出所有matrix字段元素中为0的记录,matrix字段实际上就是整型数组的数组类型。 这句语句和下面的嵌套循环作用相同: 和上面查询语句的执行结果将会完全不同。 最后,程序只有在空矩阵中死循环的时候才会挂起。bool result = false;
for (int column = 0; column < matrix.length(); column++) {
for (int row = 0; row < matrix[column].length(); row++) {
if (matrix[column][row] == 0) {
result = true;
break;
}
}
}
不可以改变使用的变量的顺序。下面查询语句的执行结果
exists row: (exists column: (matrix[column][row] = 0))