Database Service

The Database Service DSL is a wrapper for the Database Service module provided by CSW. You can refer to detailed documentation of the Database Service provided by CSW here.

This DSL provides APIs to create the connection to database. It uses Jooq library underneath for crating database connections and query.

Note

Jooq is a Java library that provides a higher level API for accessing data i.e. DDL support, DML support, fetch, batch execution, prepared statements, safety against sql injection, connection pooling, etc. To know more about Jooq and its features, please refer to this link.

Create Database Connection (Read Access)

This API allows creating connection to database with default read access. The username and password for read access is picked from environment variables set on individual’s machine i.e. DB_READ_USERNAME and DB_READ_PASSWORD. It is expected that developers set these variables before calling this method. It returns Jooq’s DSLContext or fails with DatabaseException. DSLContext provide methods like fetchAsync, executeAsync, executeBatch, etc. Moreover, see JooqHelper in CSW which provides wrapper methods on Jooq’s DSLContext. For methods returning Future, script will need to await for sequential flow.

Following example shows creating database connection and querying database.

Kotlin
val readDslContext = makeDatabaseService(dbName = "IRIS_db")

onSetup("setup-iris") {
    val query = readDslContext
            .resultQuery("SELECT filter_key FROM filter_table")

    //await to get result of query to achieve sequential flow of execution
    val filterKeys = JooqHelper.fetchAsync(query, String::class.java).await()

    // do something with filter keys
}

Create Database Connection (with provided access)

This API allows creating the connection to database with credentials picked from environment variables. Names of these environment variables is expected as method parameters and developers are expected to set these variables before calling this method. If user provides names for write credentials environment variables, then database connection will be created with write access. It returns Jooq’s DSLContext or fails with DatabaseException. DSLContext provide methods like fetchAsync, executeAsync, executeBatch, etc. Moreover, see JooqHelper in CSW which provides wrapper methods on Jooq’s DSLContext. For methods returning Future, script will need to await for sequential flow.

Following example shows creating database connection with write access.

Kotlin
val context = makeDatabaseService("IRIS_db", "db_write_username", "db_write_password")

Source code for examples

Note

After getting handle to DSLContext, methods like fetchAsync, executeAsync etc are available for querying database. If these methods return Future then script writer will need to await for sequential flow.