import ballerina/io;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 = Employee.convert(tb.getNext());
        if (ret is Employee) {
            io:println("Name: " + ret.name);
        } else {
            io:println("Error in get employee from table: "
                                  + ret.reason());
        }
    }
    float lowerAvgSal = tb.filter(isLowerSalary).map(getSalary).average();
    io:println("Average of Low salary: " + lowerAvgSal);
    table<EmployeeSalary> salaryTable = tb.select(getEmployeeSalary);
    int count = salaryTable.count();
    io:println("Selected row count: " + count);
    io:println(salaryTable);
    var ret = tb.remove(isLowerSalary);
    if (ret is int) {
        io:println("Deleted row count: " + ret);
    } else {
        io:println("Error in removing employees from table: "
                               + ret.reason());
    }
    io:println("After Delete: ", tb);
    var retValJson = json.convert(tb);
    if (retValJson is json) {
        io:println("JSON: ", retValJson);
    } else {
        io:println("Error in table to json conversion");
    }
    var retValXml = xml.convert(tb);
    if (retValXml is xml) {
        io:println("XML: ", retValXml);
    } else {
        io:println("Error in table to xml conversion");
    }
}function isLowerSalary(Employee p) returns boolean {
    return p.salary < 200;
}function getSalary(Employee p) returns float {
    return p.salary;
}function getEmployeeSalary(Employee e) returns (EmployeeSalary) {
    EmployeeSalary s = { id: e.id, salary: e.salary };
    return s;
}

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;
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 id marked as the primary key in the column descriptor. Three data records are inserted to the table. 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 };

Create Employee records.

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

This creates an in-memory table constrained by the Employee type with id as the primary key. Two records are inserted to 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 = Employee.convert(tb.getNext());
        if (ret is Employee) {
            io:println("Name: " + ret.name);
        } else {
            io:println("Error in get employee from table: "
                                  + ret.reason());
        }
    }

This accesses rows using the while loop.

    float lowerAvgSal = tb.filter(isLowerSalary).map(getSalary).average();
    io:println("Average of Low salary: " + lowerAvgSal);

This derives the average salary using the iterable operations.

    table<EmployeeSalary> salaryTable = tb.select(getEmployeeSalary);

This selects a subset of columns from the table.

    int count = salaryTable.count();
    io:println("Selected row count: " + count);
    io:println(salaryTable);

This fetches the table row count using the count operation.

    var ret = tb.remove(isLowerSalary);
    if (ret is int) {
        io:println("Deleted row count: " + ret);
    } else {
        io:println("Error in removing employees from table: "
                               + ret.reason());
    }
    io:println("After Delete: ", tb);

This deletes the rows that match a given criteria.

    var retValJson = json.convert(tb);
    if (retValJson is json) {
        io:println("JSON: ", retValJson);
    } else {
        io:println("Error in table to json conversion");
    }

This converts the table to JSON format.

    var retValXml = xml.convert(tb);
    if (retValXml is xml) {
        io:println("XML: ", retValXml);
    } else {
        io:println("Error in table to xml conversion");
    }
}

This converts the table to XML format.

function isLowerSalary(Employee p) returns boolean {
    return p.salary < 200;
}
function getSalary(Employee p) returns float {
    return p.salary;
}
function getEmployeeSalary(Employee e) returns (EmployeeSalary) {
    EmployeeSalary s = { id: e.id, salary: e.salary };
    return s;
}
# 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: table<Employee> {index: [], primaryKey: ["id"], data: [{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: table<Employee> {index: [], primaryKey: ["id"], data: [{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
Average of Low salary: 125.25
Selected row count: 4
table<EmployeeSalary> {index: [], primaryKey: [], data: [{id:1, salary:300.5}, {id:2, salary:100.5}, {id:3, salary:400.5}, {id:4, salary:150.0}]}
Deleted row count: 2
After Delete: table<Employee> {index: [], primaryKey: ["id"], data: [{id:1, name:"Jane", salary:300.5}, {id:3, name:"John", salary:400.5}]}
JSON: [{"id":1, "name":"Jane", "salary":300.5}, {"id":3, "name":"John", "salary":400.5}]
XML: <results><result><id>1</id><name>Jane</name><salary>300.5</salary></result><result><id>3</id><name>John</name><salary>400.5</salary></result></results>