Zum Hauptinhalt springen

Database

Represents a Database connection

Deprecated​

in v23.0.0, Please use SelectBuilder

See​

SelectBuilder New syntax for Queries:

context.enableModules();
const { SelectBuilder } = require("ou.sp.SelectBuilder");

const dbUser = util.getEnvironment("OUSP_DATABASE_USER");
const dbPassword = util.getEnvironment("OUSP_DATABASE_PASSWORD");
const db = new DBConnection("odbc", "ousp", dbUser, dbPassword);
try {
const rows = SelectBuilder.from("costcenter", db)
.select("costcenterName", "string")
.select("id", "number")
.execute();

context.returnValue = JSON.stringify(rows);
} catch (error) {
context.errorMessage = error.message;
context.returnValue = -1;
} finally {
db.close();
}

New syntax for Inserts/Updates/Deletes:

context.enableModules();
const { InsertBuilder } = require("ou.sp.InsertBuilder");

const dbUser = util.getEnvironment("OUSP_DATABASE_USER");
const dbPassword = util.getEnvironment("OUSP_DATABASE_PASSWORD");
const db = new DBConnection("odbc", "ousp", dbUser, dbPassword);
try {
const insertStatement = InsertBuilder.into("costcenter")
.addRange([
["recipient", "R0100"],
["costcenter", "K30101"],
["costcenterName", "GmbH Entwicklung Testlab"],
])
.toSQL();

const result = db.executeStatement(insertStatement);
if (!result && db.getLastError()) {
throw new Error("DBConnection error: " + db.getLastError());
}
} catch (error) {
context.errorMessage = error.message;
context.returnValue = -1;
} finally {
db.close();
}

Constructors​

Constructor​

new Database(connection, autoClose?): Database

Creates an instance of Database.

Parameters​

connection​

DBConnection

the Documents DBConnection Class

autoClose?​

boolean

Closes the connection when table.SaveChanges is called. Default value is false

Returns​

Database

Member Of​

Database

Properties​

autoClose​

autoClose: boolean

Methods​

executeStatement()​

executeStatement(statement): boolean

Execute any SQL statement on the external database.

Parameters​

statement​

the sql Statement or StatementBuilder instance to execute.

string | StatementBuilder

Returns​

boolean

  • true if successful, false in case of any error

Member Of​

Database


executeQuery()​

executeQuery(query, customTypeMapper?): Table

Execute a SELECT statement and retrieve a Table-Class containing the result rows found by the statement.

Parameters​

query​

QueryBuilder

The QueryBuilder instance with the statement

customTypeMapper?​

(column, value) => string | number | Date

Custom Mapper for types. Returns the specified type for a value in a cell

Returns​

Table

  • Returns a instance of Table with the data.

Deprecated​

in v23.0.0 Please use SelectBuilder

See​

SelectBuilder

Member Of​

Database


executeQueryStatement()​

executeQueryStatement(query, customTypeMapper?): Table

DO NOT USE THIS FUNCTION! Execute a SELECT statement and retrieve a Table-Class containing the result rows found by the statement.

Parameters​

query​

StatementBuilder

The StatementBuilder instance with the statement

customTypeMapper?​

(column, value) => string | number | Date

Custom Mapper for types. Returns the specified type for a value in a cell

Returns​

Table

  • Returns a instance of Table with the data.

Deprecated​

since version 1.2.2

Member Of​

Database


getLastError()​

getLastError(): string

Returns the last known Error

Returns​

string

  • returns a string containing the last known error

Member Of​

Database


close()​

close(): boolean

Closes the current database connection instance

Returns​

boolean

  • true if successful otherwise false

Member Of​

Database