项目作者: ballerina-platform

项目描述 :
Oracle Database Connector for Ballerina
高级语言: Ballerina
项目地址: git://github.com/ballerina-platform/module-ballerinax-oracledb.git
创建时间: 2020-12-21T04:36:17Z
项目社区:https://github.com/ballerina-platform/module-ballerinax-oracledb

开源协议:Apache License 2.0

下载


Ballerina OracleDB Library

Build
codecov
Trivy
GraalVM Check
GitHub Last Commit
GitHub issues

This library provides the functionality required to access and manipulate data stored in an Oracle database.

Prerequisite

Add the OracleDB drivers as a dependency to the Ballerina project.

Note: ballerinax/oracledb supports OracleDB driver versions above 12.2.0.1.

You can achieve this by importing the ballerinax/oracledb.driver module,

  1. import ballerinax/oracledb.driver as _;

ballerinax/oracledb.driver package bundles the latest OracleDB driver JARs.

Tip: GraalVM native build is supported when ballerinax/oracledb is used along with the ballerinax/oracledb.driver

If you want to add a OracleDB drivers of specific versions, you can add them as a dependencies in Ballerina.toml.
Follow one of the following ways to add the JARs in the file:

  • Download the JAR and update the path.

    1. [[platform.java21.dependency]]
    2. path = "PATH"
  • Add JAR with the Maven dependency params.

    1. [[platform.java21.dependency]]
    2. groupId = "com.oracle.database.jdbc"
    3. artifactId = "ojdbc11"
    4. version = "12.2.0.1"
    5. [platform.java21.dependency]]
    6. groupId = "com.oracle.database.xml"
    7. artifactId = "xdb"
    8. version = "21.1.0.0"
    9. [platform.java21.dependency]]
    10. groupId = "com.oracle.database.xml"
    11. artifactId = "xmlparserv2"
    12. version = "12.2.0.1"

Client

To access a database, you must first create an
oracledb:Client object.
The samples for creating an OracleDB client can be found below.

Tip: The client should be used throughout the application lifetime.

Create a client

This sample shows the different ways of creating an oracledb:Client.

The client can be created with an empty constructor, and thereby, the client will be initialized with the default properties.

  1. oracledb:Client|sql:Error dbClient = new ();

The oracledb:Client receives the host, username, and password. Since the properties are passed in the same order as they are defined
in the oracledb:Client, you can pass them without named parameters.

  1. oracledb:Client|sql:Error dbClient = new ("localhost", "adminUser", "adminPassword",
  2. "ORCLCDB.localdomain", 1521);

In the example below, the oracledb:Client uses named parameters to pass the attributes since it is skipping some parameters in the constructor.
Further, the oracledb:Options
property is passed to configure the SSL, connection timeout, and a few other additional properties in the OracleDB client.

  1. string clientStorePath = check file:getAbsolutePath("./client-keystore.p12");
  2. string trustStorePath = check file:getAbsolutePath("./client-truststore.p12");
  3. oracledb:Options options = {
  4. ssl: {
  5. key: {
  6. path: clientStorePath,
  7. password: "password"
  8. },
  9. cert: {
  10. path: trustStorePath,
  11. password: "password"
  12. }
  13. },
  14. loginTimeout: 1,
  15. autoCommit: true,
  16. connectTimeout: 30,
  17. socketTimeout: 30
  18. };
  19. oracledb:Client|sql:Error dbClient = new (user = "adminUser", password = "adminPassword",
  20. options = options);

Similarly, in the example below, the oracledb:Client uses the named parameters and it provides an unshared connection pool of the type of
sql:ConnectionPool
to be used within the client.
For more details about connection pooling, see the sql Library.

  1. oracledb:Client|sql:Error dbClient = new (user = "adminUser", password = "adminPassword",
  2. connectionPool = {maxOpenConnections: 5});

Handle connection pools

All database libraries share the same connection pooling concept and there are three possible scenarios for
connection pool handling. For its properties and possible values, see sql:ConnectionPool.

Note: Connection pooling is used to optimize opening and closing connections to the database. However, the pool comes with an overhead. It is best to configure the connection pool properties as per the application need to get the best performance.

  1. Global, shareable, default connection pool

    If you do not provide the connectionPool field when creating the database client, a globally-shareable pool will be
    created for your database unless a connection pool matching with the properties you provided already exists.

    1. oracledb:Client|sql:Error dbClient = new (user = "adminUser", password = "adminPassword");
  2. Client-owned, unsharable connection pool

    If you define the connectionPool field inline when creating the database client with the sql:ConnectionPool type,
    an unsharable connection pool will be created.

    1. oracledb:Client|sql:Error dbClient = new (user = "adminUser", password = "adminPassword",
    2. connectionPool = { maxOpenConnections: 5 });
  3. Local, shareable connection pool

    If you create a record of the sql:ConnectionPool type and reuse that in the configuration of multiple clients,
    for each set of clients that connects to the same database instance with the same set of properties, a shared
    connection pool will be used.

    1. sql:ConnectionPool connPool = {maxOpenConnections: 5};
    2. oracledb:Client|sql:Error dbClient1 =
    3. new (user = "adminUser", password = "adminPassword",
    4. connectionPool = connPool);
    5. oracledb:Client|sql:Error dbClient2 =
    6. new (user = "adminUser", password = "adminPassword",
    7. connectionPool = connPool);
    8. oracledb:Client|sql:Error dbClient3 =
    9. new (user = "adminUser", password = "adminPassword",
    10. connectionPool = connPool);

For more details about each property, see the oracledb:Client constructor.

The oracledb:Client references
sql:Client and all the operations
defined by the sql:Client will be supported by the oracledb:Client as well.

Close the client

Once all the database operations are performed, you can close the database client you have created by invoking the close()
operation. This will close the corresponding connection pool if it is not shared by any other database clients.

Note: The client must be closed only at the end of the application lifetime (or closed for graceful stops in a service).

  1. error? e = dbClient.close();

Or

  1. check dbClient.close();

Database operations

Once the client is created, database operations can be executed through that client. This library defines the interface
and common properties that are shared among multiple database clients. It also supports querying, inserting, deleting,
updating, and batch updating data.

Parameterized query

The sql:ParameterizedQuery is used to construct the SQL query to be executed by the client.
You can create a query with constant or dynamic input data as follows.

Query with constant values

  1. sql:ParameterizedQuery query = `SELECT * FROM students
  2. WHERE id < 10 AND age > 12`;

Query with dynamic values

  1. int[] ids = [10, 50];
  2. int age = 12;
  3. sql:ParameterizedQuery query = `SELECT * FROM students
  4. WHERE id < ${ids[0]} AND age > ${age}`;

Moreover, the SQL package has sql:queryConcat() and sql:arrayFlattenQuery() util functions which make it easier
to create a dynamic/constant complex query.

The sql:queryConcat() is used to create a single parameterized query by concatenating a set of parameterized queries.
The sample below shows how to concatenate queries.

  1. int id = 10;
  2. int age = 12;
  3. sql:ParameterizedQuery query = `SELECT * FROM students`;
  4. sql:ParameterizedQuery query1 = ` WHERE id < ${id} AND age > ${age}`;
  5. sql:ParameterizedQuery sqlQuery = sql:queryConcat(query, query1);

The query with the IN operator can be created using the sql:ParameterizedQuery as shown below. Here you need to flatten the array and pass each element separated by a comma.

  1. int[] ids = [1, 2, 3];
  2. sql:ParameterizedQuery query = `SELECT count(*) as total FROM DataTable
  3. WHERE row_id IN (${ids[0]}, ${ids[1]}, ${ids[2]})`;

The sql:arrayFlattenQuery() util function is used to make the array flatten easier. It makes the inclusion of varying array elements into the query easier by flattening the array to return a parameterized query. You can construct the complex dynamic query with the IN operator by using both functions as shown below.

  1. int[] ids = [1, 2];
  2. sql:ParameterizedQuery sqlQuery =
  3. sql:queryConcat(`SELECT * FROM DataTable WHERE id IN (`,
  4. sql:arrayFlattenQuery(ids), `)`);

Create tables

This sample creates a table with three columns. The first column is a primary key of type int
while the second column is of type int and the other is of type varchar.
The CREATE statement is executed via the execute remote method of the client.

  1. // Create the ‘Students’ table with the ‘id’, ‘name‘, and ‘age’ fields.
  2. sql:ExecutionResult result =
  3. check dbClient->execute(`CREATE TABLE student (
  4. id NUMBER GENERATED ALWAYS AS IDENTITY,,
  5. age NUMBER,
  6. name VARCHAR(255),
  7. PRIMARY KEY (id)
  8. )`);
  9. // A value of the `sql:ExecutionResult` type is returned for the `result`.

Insert data

These samples show the data insertion by executing an INSERT statement using the execute remote method
of the client.

In this sample, the query parameter values are passed directly into the query statement of the execute
remote method.

  1. sql:ExecutionResult result = check dbClient->execute(`INSERT INTO student(age, name)
  2. VALUES (23, 'john')`);

In this sample, the parameter values, which are assigned to local variables are used to parameterize the SQL query in
the execute remote method. This type of a parameterized SQL query can be used with any primitive Ballerina type
such as string, int, float, or boolean and in that case, the corresponding SQL type of the parameter is derived
from the type of the Ballerina variable that is passed in.

  1. string name = "Anne";
  2. int age = 8;
  3. sql:ParameterizedQuery query = `INSERT INTO student(age, name)
  4. VALUES (${age}, ${name})`;
  5. sql:ExecutionResult result = check dbClient->execute(query);

In this sample, the parameter values are passed as an sql:TypedValue to the execute remote method. Use the
corresponding subtype of the sql:TypedValue such as sql:VarcharValue, sql:CharValue, sql:IntegerValue, etc., when you need to
provide more details such as the exact SQL type of the parameter.

  1. sql:VarcharValue name = new ("James");
  2. sql:IntegerValue age = new (10);
  3. sql:ParameterizedQuery query = `INSERT INTO student(age, name)
  4. VALUES (${age}, ${name})`;
  5. sql:ExecutionResult result = check dbClient->execute(query);

Insert data with auto-generated keys

This sample demonstrates inserting data while returning the auto-generated keys. It achieves this by using the
execute remote method to execute the INSERT statement.

  1. int age = 31;
  2. string name = "Kate";
  3. sql:ParameterizedQuery query = `INSERT INTO student(age, name)
  4. VALUES (${age}, ${name})`;
  5. sql:ExecutionResult result = check dbClient->execute(query);
  6. // Number of rows affected by the execution of the query.
  7. int? count = result.affectedRowCount;
  8. // The integer or string generated by the database in response to a query execution.
  9. string|int? generatedKey = result.lastInsertId;

Query data

These samples show how to demonstrate the different usages of the query operation to query the
database table and obtain the results as a stream.

Note: When processing the stream, make sure to consume all fetched data or close the stream.

This sample demonstrates querying data from a table in a database.
First, a type is created to represent the returned result set. This record can be defined as an open or a closed record
according to the requirement. If an open record is defined, the returned stream type will include both defined fields
in the record and additional database columns fetched by the SQL query which are not defined in the record.
Note the mapping of the database column to the returned record’s property is case-insensitive if it is defined in the
record(i.e., the ID column in the result can be mapped to the id property in the record). Additional column names
added to the returned record as in the SQL query. If the record is defined as a closed record, only defined fields in the
record are returned or gives an error when additional columns present in the SQL query. Next, the SELECT query is executed
via the query remote method of the client. Once the query is executed, each data record can be retrieved by iterating through
the result set. The stream returned by the SELECT operation holds a pointer to the actual data in the database, and it
loads data from the table only when it is accessed. This stream can be iterated only once.

  1. // Define an open record type to represent the results.
  2. type Student record {
  3. int id;
  4. int age;
  5. string name;
  6. };
  7. // Select the data from the database table. The query parameters are passed
  8. // directly. Similar to the `execute` samples, parameters can be passed as
  9. // sub types of `sql:TypedValue` as well.
  10. int id = 10;
  11. int age = 12;
  12. sql:ParameterizedQuery query = `SELECT * FROM students
  13. WHERE id < ${id} AND age > ${age}`;
  14. stream<Student, sql:Error?> resultStream = dbClient->query(query);
  15. // Iterating the returned table.
  16. check from Student student in resultStream
  17. do {
  18. // Can perform operations using the `student` record of type `Student`.
  19. };

Defining the return type is optional and you can query the database without providing the result type. Hence,
the above sample can be modified as follows with an open record type as the return type. The property name in the open record
type will be the same as how the column is defined in the database.

  1. // Select the data from the database table. The query parameters are passed
  2. // directly. Similar to the `execute` samples, parameters can be passed as
  3. // sub types of `sql:TypedValue` as well.
  4. int id = 10;
  5. int age = 12;
  6. sql:ParameterizedQuery query = `SELECT * FROM students
  7. WHERE id < ${id} AND age > ${age}`;
  8. stream<record{}, sql:Error?> resultStream = dbClient->query(query);
  9. // Iterating the returned table.
  10. check from record{} student in resultStream
  11. do {
  12. // Can perform operations using the `student` record.
  13. io:println("Student name: ", student.value["name"]);
  14. };

There are situations in which you may not want to iterate through the database and in that case, you may decide
to use the queryRow() operation. If the provided return type is a record, this method returns only the first row
retrieved by the query as a record.

  1. int id = 10;
  2. sql:ParameterizedQuery query = `SELECT * FROM students WHERE id = ${id}`;
  3. Student retrievedStudent = check dbClient->queryRow(query);

The queryRow() operation can also be used to retrieve a single value from the database (e.g., when querying using
COUNT() and other SQL aggregation functions). If the provided return type is not a record (i.e., a primitive data type)
, this operation will return the value of the first column of the first row retrieved by the query.

  1. int age = 12;
  2. sql:ParameterizedQuery query = `SELECT COUNT(*) FROM students WHERE age < ${age}`;
  3. int youngStudents = check dbClient->queryRow(query);

Update data

This sample demonstrates modifying data by executing an UPDATE statement via the execute remote method of
the client.

  1. int age = 23;
  2. sql:ParameterizedQuery query = `UPDATE students SET name = 'John' WHERE age = ${age}`;
  3. sql:ExecutionResult result = check dbClient->execute(query);

Delete data

This sample demonstrates deleting data by executing a DELETE statement via the execute remote method of
the client.

  1. string name = "John";
  2. sql:ParameterizedQuery query = `DELETE from students WHERE name = ${name}`;
  3. sql:ExecutionResult result = check dbClient->execute(query);

Batch update data

This sample demonstrates how to insert multiple records with a single INSERT statement that is executed via the
batchExecute remote method of the client. This is done by creating a table with multiple records and
parameterized SQL query as same as the above execute operations.

  1. // Create the table with the records that need to be inserted.
  2. var data = [
  3. { name: "John", age: 25 },
  4. { name: "Peter", age: 24 },
  5. { name: "jane", age: 22 }
  6. ];
  7. // Do the batch update by passing the batches.
  8. sql:ParameterizedQuery[] batch = from var row in data
  9. select `INSERT INTO students ('name', 'age')
  10. VALUES (${row.name}, ${row.age})`;
  11. sql:ExecutionResult[] result = check dbClient->batchExecute(batch);

Execute SQL stored procedures

This sample demonstrates how to execute a stored procedure with a single INSERT statement that is executed via the
call remote method of the client.

  1. int uid = 10;
  2. sql:IntegerOutParameter insertId = new;
  3. sql:ProcedureCallResult result =
  4. check dbClient->call(`call InsertPerson(${uid}, ${insertId})`);
  5. stream<record{}, sql:Error?>? resultStr = result.queryResult;
  6. if resultStr is stream<record{}, sql:Error?> {
  7. check from record{} result in resultStr
  8. do {
  9. // Can perform operations using the `result` record.
  10. };
  11. }
  12. check result.close();

Note: Once the results are processed, the close method on the sql:ProcedureCallResult must be called.

OracleDB specific custom data types

Interval types

OracleDB has two INTERVAL data types: INTERVAL YEAR TO MONTH and INTERVAL DAY TO SECOND to store the various INTERVAL periods.

The equivalent types in Ballerina are as follows.

  1. public type Sign +1|-1;
  2. public type IntervalYearToMonth record {|
  3. Sign sign = +1;
  4. int:Unsigned32 years?;
  5. int:Unsigned32 months?;
  6. |};
  7. public type IntervalDayToSecond record {|
  8. Sign sign = +1;
  9. int:Unsigned32 days?;
  10. int:Unsigned32 hours?;
  11. int:Unsigned32 minutes?;
  12. decimal seconds?;
  13. |};

Here, oracledb:Sign is used to mark the sign of the interval period and period values are always ZERO or positive integers.

  1. //INTERVAL '120-11' YEAR(3) TO MONTH
  2. oracledb:IntervalYearToMonth intervalYM = {years: 120, months: 11};
  3. //INTERVAL '120' YEAR(3)
  4. oracledb:IntervalYearToMonth intervalYM2 = {years: 120};
  5. //INTERVAL '-11' MONTH(3)
  6. oracledb:IntervalYearToMonth intervalYM3 = {months: 11, sign: -1};
  7. //INTERVAL '-120-11' YEAR(3) TO MONTH
  8. oracledb:IntervalYearToMonth intervalYM4 = {years: 120, months: 11, sign: -1};
  9. //INTERVAL '11 10:09:08.555' DAY TO SECOND(3)
  10. oracledb:IntervalDayToSecond intervalDS = {days: 11, hours: 10, minutes: 9, seconds: 8.555};
  11. //INTERVAL '-11 10:09:08.555' DAY TO SECOND(3)
  12. oracledb:IntervalDayToSecond intervalDS2 = {days: 11, hours: 10, minutes: 9, seconds: 8.555, sign: -1};
  13. //INTERVAL '-10:09:08.555' HOUR TO SECOND(3)
  14. oracledb:IntervalDayToSecond intervalDS3 = {hours: 10, minutes: 9, seconds: 8.555, sign: -1};
  15. //INTERVAL '11 00:09:08.55578' DAY TO SECOND(5)
  16. oracledb:IntervalDayToSecond intervalDS4 = {days: 11, minutes: 9, seconds: 8.55578};

VARRAY types

OracleDB has support for VARRAY data type and VARRAY consists a type name and elements attributes.

The VARRAY equivalent type in Ballerina is as follows.

  1. type ArrayValueType string?[]|int?[]|boolean?[]|float?[]|decimal?[]|byte[]?[];
  2. public type Varray record {|
  3. string name;
  4. ArrayValueType? elements;
  5. |};

Here, oracledb:Varray has two fields to set the type name and elements of the varray. In OracleDB, a VARRAY type can be created as follows.

  1. CREATE OR REPLACE TYPE CharArrayType AS VARRAY(6) OF VARCHAR(100);
  2. CREATE TABLE TestVarrayTable(
  3. PK NUMBER GENERATED ALWAYS AS IDENTITY,
  4. COL_CHARARR CharArrayType,
  5. PRIMARY KEY(PK)
  6. );

In Ballerina, oracledb:Varray can be used to pass values for VARRAY data type as follows.

  1. string?[] charArray = [null, "Hello", "World"];
  2. Varray charVarray = { name:"CharArrayType", elements: charArray };

Note: The default thread pool size used in Ballerina is: the number of processors available * 2. You can configure the thread pool size by using the BALLERINA_MAX_POOL_SIZE environment variable.

Issues and projects

Issues and Projects tabs are disabled for this repository as this is part of the Ballerina standard library. To report bugs, request new features, start new discussions, view project boards, etc. please visit Ballerina Standard Library parent repository.

This repository only contains the source code for the package.

Build from the source

Set up the prerequisites

  1. Download and install the Java SE Development Kit (JDK) version 21 (from one of the following locations).

  2. Download and install Docker.

  3. Export your GitHub personal access token with the read package permissions as follows.

    1. export packageUser=<Username>
    2. export packagePAT=<Personal access token>

Build the source

Execute the commands below to build from the source.

  1. To build the library:

    1. ./gradlew clean build
  2. To run the integration tests:

    1. ./gradlew clean test
  3. To build the package without tests:

    1. ./gradlew clean build -x test
  4. To run only specific tests:

    1. ./gradlew clean build -Pgroups=<Comma separated groups/test cases>

    Tip: The following groups of test cases are available.

    Groups | Test cases
    —-| —-
    connection | connection-init
    pool | pool
    transaction | local-transaction
    execute | execute-basic
    execute-params
    batch-execute | batch-execute
    query | query-simple-params
    query-complex-params
    procedures | procedures
    datetime | datetime
    custom-object | custom-object
    custom-varray | custom-varray

  5. To disable some specific groups during the test:

    1. ./gradlew clean build -Pdisable-groups=<Comma separated groups/test cases>
  6. To debug the tests:

    1. ./gradlew clean build -Pdebug=<port>
    2. ./gradlew clean test -Pdebug=<port>
  7. To debug the package with the Ballerina language:

    1. ./gradlew clean build -PbalJavaDebug=<port>
    2. ./gradlew clean test -PbalJavaDebug=<port>
  8. Publish the ZIP artifact to the local .m2 repository:

    1. ./gradlew clean build publishToMavenLocal
  9. Publish the generated artifacts to the local Ballerina central repository:

    1. ./gradlew clean build -PpublishToLocalCentral=true
  10. Publish the generated artifacts to the Ballerina central repository:

    1. ./gradlew clean build -PpublishToCentral=true

Contribute to Ballerina

As an open source project, Ballerina welcomes contributions from the community.

For more information, go to the contribution guidelines.

Code of conduct

All contributors are encouraged to read the Ballerina code of conduct.