ballerina/mysql package

Package overview

This package provides the functionality required to access and manipulate data stored in a MySQL database.

Endpoint

To access a database, you must first create an endpoint, which is a virtual representation of the physical endpoint of the MySQL database that you are trying to connect to. Create an endpoint of the MySQL client type (i.e., mysql:Client) and provide the necessary connection parameters. This will create a pool of connections to the given MySQL database. A sample for creating an endpoint with MySQL client can be found below.

Database operations

Once the endpoint is created, database operations can be executed through that endpoint. This package provides support for creating tables and executing stored procedures. It also supports selecting, inserting, deleting, updating, and batch updating data. For more details on the supported actions refer to the sql package. Also the details of the SQL data types and query parameters relevant to these database operations can be found in the documentation of the sql package.

Samples

Creating an endpoint

endpoint mysql:Client testDB {
    host: "localhost",
    port: 3306,
    name: "testdb",
    username: "root",
    password: "root",
    poolOptions: { maximumPoolSize: 5 },
    dbOptions: { "useSSL": false }
};

The full list of endpoint properties can be found in the sql:PoolOptions type.

Records Summary

Record Description
ClientEndpointConfiguration The Client endpoint configuration for mysql databases.

Endpoints Summary

Endpoint Description
Client

Represents an MySQL client endpoint.

public type ClientEndpointConfiguration record

The Client endpoint configuration for mysql databases.

Field Name Data Type Default Value Description
host string

The host name of the database to connect

port int 3306

The port of the database to connect

name string

The name of the database to connect

username string

Username for the database connection

password string

Password for the database connection

poolOptions sql:PoolOptions

Properties for the connection pool configuration. Refer sql:PoolOptions for more details

dbOptions map

A map of DB specific properties

Endpoint Client

Represents an MySQL client endpoint.

  • <Client> call(string sqlQuery, typedesc[]? recordType) returns (table[]|error?)

    The call operation implementation for SQL connector to invoke stored procedures/functions.

    Parameter Name Data Type Default Value Description
    sqlQuery string
    recordType typedesc[]?
    Return Type Description
    table[]|error?
  • <Client> select(string sqlQuery, typedesc? recordType, boolean loadToMemory) returns (table|error)

    The select operation implementation for SQL connector to select data from tables.

    Parameter Name Data Type Default Value Description
    sqlQuery string
    recordType typedesc?
    loadToMemory boolean
    Return Type Description
    table|error
  • <Client> update(string sqlQuery) returns (int|error)

    The update operation implementation for SQL connector to update data and schema of the database.

    Parameter Name Data Type Default Value Description
    sqlQuery string
    Return Type Description
    int|error
  • <Client> batchUpdate(string sqlQuery) returns (int[]|error)

    The batchUpdate operation implementation for SQL connector to batch data insert.

    Parameter Name Data Type Default Value Description
    sqlQuery string
    Return Type Description
    int[]|error
  • <Client> updateWithGeneratedKeys(string sqlQuery, string[]? keyColumns) returns ((int,string[])|error)

    The updateWithGeneratedKeys operation implementation for SQL connector which returns the auto generated keys during the update action.

    Parameter Name Data Type Default Value Description
    sqlQuery string
    keyColumns string[]?
    Return Type Description
    (int,string[])|error
  • <Client> getProxyTable(string tableName, typedesc recordType) returns (table|error)

    The getProxyTable operation implementation for SQL connector which acts as a proxy for a database table that allows performing select/update operations over the actual database table.

    Parameter Name Data Type Default Value Description
    tableName string
    recordType typedesc
    Return Type Description
    table|error