import ballerina/io;
import ballerina/jsonutils;
import ballerina/xmlutils;type Employee record {
    int id;
    string name;
    float salary;
};type EmployeeSalary record {
    int id;
    float salary;
};public function main() {
    table<Employee> tbEmployee = table {
        { key id, name, salary },
        [ { 1, "Mary",  300.5 },
          { 2, "John",  200.5 },
          { 3, "Jim", 330.5 }
        ]
    };
    io:print("Table Information: ");
    io:println(tbEmployee);
    Employee e1 = { id: 1, name: "Jane", salary: 300.50 };
    Employee e2 = { id: 2, name: "Anne", salary: 100.50 };
    Employee e3 = { id: 3, name: "John", salary: 400.50 };
    Employee e4 = { id: 4, name: "Peter", salary: 150.0 };
    table<Employee> tb = table {
        { key id, name, salary },
        [e1, e2]
    };    Employee[] employees = [e3, e4];
    foreach var emp in employees {
        var ret = tb.add(emp);
        if (ret is ()) {
            io:println("Adding record to table successful");
        } else {
            io:println("Adding to table failed: ", ret.reason());
        }
    }
    io:println("Table Information: ", tb);
    io:println("Using foreach: ");
    foreach var x in tb {
        io:println("Name: ", x.name);
    }
    io:println("Using while loop: ");
    while (tb.hasNext()) {
        var ret = tb.getNext();
        if (ret is Employee) {
            io:println("Name: ", ret.name);
        } else {
            io:println("Error in get employee from table");
        }
    }
    json retValJson = jsonutils:fromTable(tb);
    io:println("JSON: ", retValJson);
    xml retValXml = xmlutils:fromTable(tb);
    io:println("XML: ", retValXml);
}

Table

The type table is a data structure that organizes information in rows and columns. This example demonstrates how to create an in-memory table using a type constraint, insert data to it, and then access/delete the data.

import ballerina/io;
import ballerina/jsonutils;
import ballerina/xmlutils;
type Employee record {
    int id;
    string name;
    float salary;
};
type EmployeeSalary record {
    int id;
    float salary;
};
public function main() {
    table<Employee> tbEmployee = table {
        { key id, name, salary },
        [ { 1, "Mary",  300.5 },
          { 2, "John",  200.5 },
          { 3, "Jim", 330.5 }
        ]
    };

This creates an in-memory table constrained by the Employee type with the id marked as the primary key in the column descriptor. Three data records are inserted into the table. The order of the data values should match the order of the column descriptor.

    io:print("Table Information: ");
    io:println(tbEmployee);

This prints the table data.

    Employee e1 = { id: 1, name: "Jane", salary: 300.50 };
    Employee e2 = { id: 2, name: "Anne", salary: 100.50 };
    Employee e3 = { id: 3, name: "John", salary: 400.50 };
    Employee e4 = { id: 4, name: "Peter", salary: 150.0 };

Creates Employee records.

    table<Employee> tb = table {
        { key id, name, salary },
        [e1, e2]
    };

This creates an in-memory table constrained by the Employee type with the id as the primary key. Two records are inserted into the table.

    Employee[] employees = [e3, e4];
    foreach var emp in employees {
        var ret = tb.add(emp);
        if (ret is ()) {
            io:println("Adding record to table successful");
        } else {
            io:println("Adding to table failed: ", ret.reason());
        }
    }

This adds the created records to the table.

    io:println("Table Information: ", tb);

This prints the table data.

    io:println("Using foreach: ");
    foreach var x in tb {
        io:println("Name: ", x.name);
    }

This accesses rows using the foreach loop.

    io:println("Using while loop: ");
    while (tb.hasNext()) {
        var ret = tb.getNext();
        if (ret is Employee) {
            io:println("Name: ", ret.name);
        } else {
            io:println("Error in get employee from table");
        }
    }

This accesses rows using the while loop.

    json retValJson = jsonutils:fromTable(tb);
    io:println("JSON: ", retValJson);

This converts the table to JSON format.

    xml retValXml = xmlutils:fromTable(tb);
    io:println("XML: ", retValXml);
}

This converts the table to XML format.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command.
$ ballerina run table.bal
Table Information: id=1 name=Mary salary=300.5 id=2 name=John salary=200.5 id=3 name=Jim salary=330.5
Adding record to table successful
Adding record to table successful
Table Information: id=1 name=Jane salary=300.5 id=2 name=Anne salary=100.5 id=3 name=John salary=400.5 id=4 name=Peter salary=150.0
Using foreach:
Name: Jane
Name: Anne
Name: John
Name: Peter
Using while loop:
Name: Jane
Name: Anne
Name: John
Name: Peter
JSON: id=1 name=Jane salary=300.5 id=2 name=Anne salary=100.5 id=3 name=John salary=400.5 id=4 name=Peter salary=150.0
XML: <results><result><id>1</id><name>Jane</name><salary>300.5</salary></result><result><id>2</id><name>Anne</name><salary>100.5</salary></result><result><id>3</id><name>John</name><salary>400.5</salary></result><result><id>4</id><name>Peter</name><salary>150.0</salary></result></results>