Ballerina Collections: Tables
In my previous article we discussed about preliminary collections supported in Ballerina Language. In this article we will talk about Tables, another structured data type in Ballerina.
Tables are a built-in data-structure which contains an array of records. Each row in the table consists of a record and each row is uniquely identified by a key
. The key
is stored in the record as an immutable field. This is similar to the table concept which we find in SQL-based databases.
We can access elements in a table either by iterating over all the elements as an array or by pointing to the item using the associated key
.
We can derive a key
using multiple-fields.
Since the type of the key
is not restricted to string
, we could use a structured key
as well.
Combining Tables with Language Integrated Queries
Ballerina has a feature called Language Integrated Queries, which will enable users to query and manipulate data in arrays with SQL-like syntax. This is based on the concept of Mathematical Set Builder Notation. We can use this feature in combination with Ballerina Tables.
Apart from using ‘key’ to access a row in a table we could use queries.
Usually when querying a table the result will be in an array format. But, we could also create a table using queries.
Query expressions also support let clause
to define intermediate variables as well.
Ballerina queries can also use the join clause
with table keys.
Ballerina tables combined with ‘Language Integrated Queries’, simplifies the data manipulation logic of any application.
In this article we discussed about the advanced collection support provided with Ballerina tables.
Key Points on Ballerina Tables:
- Contains the ability of both arrays and maps
key
is a part of the value itselfkey
should be unique- Type of the
key
is not restricted tostring
- Table preserves the order of the members