ballerina/h2 package
Package overview
This package provides the functionality required to access and manipulate data stored in an H2 database.
Endpoint
To access a database, you must first create an endpoint
, which is a virtual representation of the physical endpoint of the H2 database that you are trying to connect to. Create an endpoint of the H2 client type (i.e., h2:Client
) and provide the necessary connection parameters. This will create a pool of connections to the given H2 database. A sample for creating an endpoint with an H2 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 for the sql
package.
Samples
Creating an endpoint
endpoint h2:Client testDB {
path: "/home/ballerina/test/",
name: "testdb",
username: "SA",
password: "",
poolOptions: { maximumPoolSize: 5 }
};
The full list of endpoint properties can be found in the sql:PoolOptions
type.
Records Summary
Record | Description | ||
---|---|---|---|
ClientEndpointConfiguration | The Client endpoint configuration for h2 databases. |
Endpoints Summary
Endpoint | Description | ||
---|---|---|---|
Client | Represents an H2 client endpoint. |
public type ClientEndpointConfiguration record
The Client endpoint configuration for h2 databases.
Field Name | Data Type | Default Value | Description |
---|---|---|---|
host | string | The host name of the database to connect (in case of server based DB) |
|
path | string | The path of the database connection (in case of file based DB) |
|
port | int | 9092 | The port of the database to connect (in case of server based DB) |
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 |
|
dbOptions | map | A map of DB specific properties |
Endpoint Client
Represents an H2 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