Database
Overview
Module
- package:
org.eclipse.dirigible.sdk.db - source: db/Database.java
Database is the primary entry point for relational-database access in the Eclipse Dirigible Java SDK. It exposes both the JSON-shaped helpers used by scripted callers and a raw Connection for code that prefers PreparedStatements.
Two style choices:
- Pass-through SQL (
query,update) - the simplest form; results come back as a JSON string. Good for read-mostly endpoints and ad-hoc admin tools. - JDBC (
getConnection()) - fullPreparedStatement/ResultSetcontrol, transactions, type-safe parameter binding. Use this from Java controllers and anywhere correctness matters more than terseness.
The datasourceName parameter is optional on every method - when omitted, the default data source applies. Pass the logical name of any .datasource artefact present in the registry to target a non-default DB.
Sequence operations (nextval, createSequence, dropSequence) work across H2, PostgreSQL, Oracle, and MS SQL - the platform translates the call into the appropriate dialect.
Key Features:
- JSON helpers: One-liner
queryandupdatecalls that hand back JSON strings - ideal for HTTP endpoints. - Raw JDBC:
getConnection()returns aDirigibleConnectionfor fullPreparedStatement/ResultSetcontrol and explicit transactions. - Data-source aware: Every operation accepts an optional
datasourceNameto target any registered.datasource. - Named parameters:
queryNamed/updateNamedvariants support:name-style bindings. - Cross-dialect sequences:
nextval,createSequence, anddropSequencetranslate to the underlying database's dialect. - Cross-dialect users and schemas:
createUser,setUserPassword,dropUser,existsUser,createSchema,dropSchemaandexistsSchematranslate the same way - useful when an application manages database principals of its own. - SqlFactory access:
getDefaultSqlFactoryandgetNativeSqlFactoryexpose the fluent SQL builder for dialect-aware query construction.
Example Usage:
import org.eclipse.dirigible.sdk.db.Database;
import org.eclipse.dirigible.components.database.DirigibleConnection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
// JSON-shaped pass-through query
String customers = Database.query("SELECT * FROM CUSTOMER WHERE ACTIVE = ?", "[true]");
// Raw JDBC for full control
try (DirigibleConnection conn = Database.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement("SELECT NAME FROM CUSTOMER WHERE ID = ?")) {
ps.setLong(1, 42L);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
String name = rs.getString("NAME");
}
}
}
}
// Cross-dialect sequence
long nextId = Database.nextval("CUSTOMER_SEQ");Methods
getConnection()
Returns a DirigibleConnection against the default data source.
javapublic static DirigibleConnection getConnection() throws Throwable;Returns
- Type:
DirigibleConnection- Description: A connection against the default data source.
getConnection(datasourceName)
Returns a DirigibleConnection against the named data source.
javapublic static DirigibleConnection getConnection(String datasourceName) throws Throwable;
Parameter Type Description datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
DirigibleConnection- Description: A connection against the named data source.
getDefaultDataSource()
Returns the default DirigibleDataSource.
javapublic static DirigibleDataSource getDefaultDataSource();Returns
- Type:
DirigibleDataSource- Description: The default data source.
getDataSource(datasourceName)
Returns the DirigibleDataSource with the given logical name.
javapublic static DirigibleDataSource getDataSource(String datasourceName);
Parameter Type Description datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
DirigibleDataSource- Description: The named data source.
getDataSources()
Returns the list of available data source names as a JSON string.
javapublic static String getDataSources();Returns
- Type:
String- Description: JSON array of available data source names.
getMetadata()
Returns database metadata for the default data source.
javapublic static String getMetadata() throws Throwable;Returns
- Type:
String- Description: JSON-shaped metadata describing schemas, tables, and columns.
getMetadata(datasourceName)
Returns database metadata for the named data source.
javapublic static String getMetadata(String datasourceName) throws Throwable;
Parameter Type Description datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
String- Description: JSON-shaped metadata describing schemas, tables, and columns.
getProductName()
Returns the product name of the underlying default database system.
javapublic static String getProductName() throws Throwable;Returns
- Type:
String- Description: The database product name (e.g.
H2,PostgreSQL).
getProductName(datasourceName)
Returns the product name of the named database system.
javapublic static String getProductName(String datasourceName) throws Throwable;
Parameter Type Description datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
String- Description: The database product name.
query(sql)
Executes a SQL query against the default data source and returns the result set as a JSON string.
javapublic static String query(String sql) throws Throwable;
Parameter Type Description sqlStringA SQL SELECTstatement.Returns
- Type:
String- Description: JSON array of row objects. |
query(sql, parametersJson)
Executes a parameterised SQL query against the default data source.
javapublic static String query(String sql, String parametersJson) throws Throwable;
Parameter Type Description sqlStringA SQL SELECTstatement with positional?placeholders.parametersJsonStringJSON array of parameter values, in placeholder order. Returns
- Type:
String- Description: JSON array of row objects.
query(sql, parametersJson, datasourceName)
Executes a parameterised SQL query against the named data source.
javapublic static String query(String sql, String parametersJson, String datasourceName) throws Throwable;
Parameter Type Description sqlStringA SQL SELECTstatement with positional?placeholders.parametersJsonStringJSON array of parameter values, in placeholder order. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
String- Description: JSON array of row objects.
queryNamed(sql, parametersJson)
Executes a SQL query with named parameters against the default data source.
javapublic static String queryNamed(String sql, String parametersJson) throws Throwable;
Parameter Type Description sqlStringA SQL SELECTstatement with:name-style placeholders.parametersJsonStringJSON object mapping parameter names to values. Returns
- Type:
String- Description: JSON array of row objects.
queryNamed(sql, parametersJson, datasourceName)
Executes a SQL query with named parameters against the named data source.
javapublic static String queryNamed(String sql, String parametersJson, String datasourceName) throws Throwable;
Parameter Type Description sqlStringA SQL SELECTstatement with:name-style placeholders.parametersJsonStringJSON object mapping parameter names to values. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
String- Description: JSON array of row objects.
update(sql)
Executes a SQL update against the default data source and returns the affected row count.
javapublic static int update(String sql) throws Throwable;
Parameter Type Description sqlStringA SQL INSERT,UPDATE,DELETE, or DDL statement.Returns
- Type:
int- Description: Number of rows affected.
update(sql, parametersJson)
Executes a parameterised SQL update against the default data source.
javapublic static int update(String sql, String parametersJson) throws Throwable;
Parameter Type Description sqlStringA SQL statement with positional ?placeholders.parametersJsonStringJSON array of parameter values, in placeholder order. Returns
- Type:
int- Description: Number of rows affected.
update(sql, parametersJson, datasourceName)
Executes a parameterised SQL update against the named data source.
javapublic static int update(String sql, String parametersJson, String datasourceName) throws Throwable;
Parameter Type Description sqlStringA SQL statement with positional ?placeholders.parametersJsonStringJSON array of parameter values, in placeholder order. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
int- Description: Number of rows affected.
updateNamed(sql)
Executes a SQL update with no parameters against the default data source.
javapublic static int updateNamed(String sql) throws Throwable;
Parameter Type Description sqlStringA SQL statement. Returns
- Type:
int- Description: Number of rows affected.
updateNamed(sql, parametersJson)
Executes a SQL update with named parameters against the default data source.
javapublic static int updateNamed(String sql, String parametersJson) throws Throwable;
Parameter Type Description sqlStringA SQL statement with :name-style placeholders.parametersJsonStringJSON object mapping parameter names to values. Returns
- Type:
int- Description: Number of rows affected.
updateNamed(sql, parametersJson, datasourceName)
Executes a SQL update with named parameters against the named data source.
javapublic static int updateNamed(String sql, String parametersJson, String datasourceName) throws Throwable;
Parameter Type Description sqlStringA SQL statement with :name-style placeholders.parametersJsonStringJSON object mapping parameter names to values. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
int- Description: Number of rows affected.
insert(sql, parametersJson, datasourceName)
Executes a parameterised INSERT and returns the generated keys.
javapublic static List<Map<String, Object>> insert(String sql, String parametersJson, String datasourceName) throws Throwable;
Parameter Type Description sqlStringA SQL INSERTstatement with positional?placeholders.parametersJsonStringJSON array of parameter values, in placeholder order. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
List<Map<String, Object>>- Description: Auto-generated keys from the insert, one map per row.
insertMany(sql, parametersJson, datasourceName)
Executes a batch INSERT over multiple parameter rows and returns the generated keys.
javapublic static List<Map<String, Object>> insertMany(String sql, String parametersJson, String datasourceName) throws Throwable;
Parameter Type Description sqlStringA SQL INSERTstatement with positional?placeholders.parametersJsonStringJSON array of parameter-arrays - one inner array per row. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
List<Map<String, Object>>- Description: Auto-generated keys, one map per inserted row.
insertNamed(sql, parametersJson, datasourceName)
Executes an INSERT with named parameters and returns generated identifiers.
javapublic static List<Long> insertNamed(String sql, String parametersJson, String datasourceName) throws Throwable;
Parameter Type Description sqlStringA SQL INSERTstatement with:name-style placeholders.parametersJsonStringJSON object mapping parameter names to values. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
List<Long>- Description: Auto-generated primary-key values.
nextval(sequence)
Returns the next value of the named sequence against the default data source.
javapublic static long nextval(String sequence) throws Throwable;
Parameter Type Description sequenceStringSequence name. Returns
- Type:
long- Description: The next sequence value.
nextval(sequence, datasourceName)
Returns the next value of the named sequence against the named data source.
javapublic static long nextval(String sequence, String datasourceName) throws Throwable;
Parameter Type Description sequenceStringSequence name. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
long- Description: The next sequence value.
nextval(sequence, datasourceName, tableName)
Returns the next value of the named sequence, associated with a specific table (used on platforms that emulate sequences via a table).
javapublic static long nextval(String sequence, String datasourceName, String tableName) throws Throwable;
Parameter Type Description sequenceStringSequence name. datasourceNameStringLogical name of a registered .datasourceartefact.tableNameStringBacking table name used by sequence emulation. Returns
- Type:
long- Description: The next sequence value.
createSequence(sequence)
Creates a new sequence on the default data source.
javapublic static void createSequence(String sequence) throws Throwable;
Parameter Type Description sequenceStringSequence name. Returns
- Type:
void
createSequence(sequence, start)
Creates a new sequence on the default data source with a specific starting value.
javapublic static void createSequence(String sequence, Integer start) throws Throwable;
Parameter Type Description sequenceStringSequence name. startIntegerInitial value for the sequence. Returns
- Type:
void
createSequence(sequence, start, datasourceName)
Creates a new sequence on the named data source with a specific starting value.
javapublic static void createSequence(String sequence, Integer start, String datasourceName) throws Throwable;
Parameter Type Description sequenceStringSequence name. startIntegerInitial value for the sequence. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
void
dropSequence(sequence)
Drops a sequence on the default data source.
javapublic static void dropSequence(String sequence) throws Throwable;
Parameter Type Description sequenceStringSequence name. Returns
- Type:
void
dropSequence(sequence, datasourceName)
Drops a sequence on the named data source.
javapublic static void dropSequence(String sequence, String datasourceName) throws Throwable;
Parameter Type Description sequenceStringSequence name. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
void
createUser(userId, password)
Creates a database user on the default data source.
Dialects
What a user is differs between databases and the dialect absorbs it. On Microsoft SQL Server a user is a server login plus a database user, so this emits two statements and dropUser removes both.
javapublic static void createUser(String userId, String password) throws Throwable;
Parameter Type Description userIdStringThe user name. passwordStringThe user's password. Returns
- Type:
void
createUser(userId, password, datasourceName)
Creates a database user on the named data source.
javapublic static void createUser(String userId, String password, String datasourceName) throws Throwable;
Parameter Type Description userIdStringThe user name. passwordStringThe user's password. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
void
setUserPassword(userId, password)
Changes the password of an existing database user on the default data source.
javapublic static void setUserPassword(String userId, String password) throws Throwable;
Parameter Type Description userIdStringThe user name. passwordStringThe new password. Returns
- Type:
void
setUserPassword(userId, password, datasourceName)
Changes the password of an existing database user on the named data source.
javapublic static void setUserPassword(String userId, String password, String datasourceName) throws Throwable;
Parameter Type Description userIdStringThe user name. passwordStringThe new password. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
void
dropUser(userId)
Drops a database user from the default data source.
javapublic static void dropUser(String userId) throws Throwable;
Parameter Type Description userIdStringThe user name. Returns
- Type:
void
dropUser(userId, datasourceName)
Drops a database user from the named data source.
javapublic static void dropUser(String userId, String datasourceName) throws Throwable;
Parameter Type Description userIdStringThe user name. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
void
existsUser(userId)
Whether a database user exists on the default data source.
Not portable
SQL standardises information_schema for schemata but not for principals, so every database keeps its users in a catalog of its own. This is implemented for PostgreSQL, H2 and Microsoft SQL Server; on any other database it throws java.sql.SQLFeatureNotSupportedException rather than answering a wrong false.
javapublic static boolean existsUser(String userId) throws Throwable;
Parameter Type Description userIdStringThe user name. Returns
- Type:
boolean- Description:
truewhen the user exists.
existsUser(userId, datasourceName)
Whether a database user exists on the named data source.
javapublic static boolean existsUser(String userId, String datasourceName) throws Throwable;
Parameter Type Description userIdStringThe user name. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
boolean- Description:
truewhen the user exists.
createSchema(schema, owner)
Creates a schema on the default data source, optionally owned by a database user.
javapublic static void createSchema(String schema, String owner) throws Throwable;
Parameter Type Description schemaStringSchema name. ownerStringThe database user to own the schema, or nullto leave ownership to the database.Returns
- Type:
void
createSchema(schema, owner, datasourceName)
Creates a schema on the named data source, optionally owned by a database user.
javapublic static void createSchema(String schema, String owner, String datasourceName) throws Throwable;
Parameter Type Description schemaStringSchema name. ownerStringThe database user to own the schema, or nullto leave ownership to the database.datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
void
dropSchema(schema, cascade)
Drops a schema from the default data source.
javapublic static void dropSchema(String schema, boolean cascade) throws Throwable;
Parameter Type Description schemaStringSchema name. cascadebooleanWhether to drop the objects the schema contains as well. Returns
- Type:
void
dropSchema(schema, cascade, datasourceName)
Drops a schema from the named data source.
javapublic static void dropSchema(String schema, boolean cascade, String datasourceName) throws Throwable;
Parameter Type Description schemaStringSchema name. cascadebooleanWhether to drop the objects the schema contains as well. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
void
existsSchema(schema)
Whether a schema exists on the default data source.
javapublic static boolean existsSchema(String schema) throws Throwable;
Parameter Type Description schemaStringSchema name. Returns
- Type:
boolean- Description:
truewhen the schema exists.
existsSchema(schema, datasourceName)
Whether a schema exists on the named data source.
javapublic static boolean existsSchema(String schema, String datasourceName) throws Throwable;
Parameter Type Description schemaStringSchema name. datasourceNameStringLogical name of a registered .datasourceartefact.Returns
- Type:
boolean- Description:
truewhen the schema exists.
getDefaultSqlFactory()
Returns the default SqlFactory - a fluent SQL-builder bound to the default data source's dialect.
javapublic static SqlFactory getDefaultSqlFactory();Returns
- Type:
SqlFactory- Description: Dialect-aware SQL builder.
getNativeSqlFactory(connection)
Returns a SqlFactory bound to the dialect of the supplied connection.
javapublic static SqlFactory getNativeSqlFactory(Connection connection);
Parameter Type Description connectionConnectionA live JDBC connection whose dialect drives the builder. Returns
- Type:
SqlFactory- Description: Dialect-aware SQL builder bound to the connection's database.