DB
Datasource loading resource. This resource allows you to load the datasource that suits you best, supports connection to MariaDB, MSSQLServer, PostgreSQL, H2 and Oracle.
// Be careful when entering parameters in queries,
// you must not use the string concatenation
// to ensure security against SQL Injection as it follows:
const NOK = _db.query(
'select * from client where id = '+ _req.getString('id')
);
// WARNING: The above example is incorrect.
// Follow the pattern below to ensure safety
// when injecting parameters:
const OK = _db.query(
'select * from client where id = ?', _val.list().add( _req.getString('id') )
);
all
_db.all(table: string) : java.util.List
Description
Selects all the data from a table.
How To Use
_db.all('client');
Attributes
NAME | TYPE | DESCRIPTION |
---|---|---|
table | string | Table's name where the query is going to be executed. |
Return
( java.util.List )
The data found on the table or null if it does not exist.
batch
_db.batch() : DBBatch
Description
Starts batch processing of database executions.
How To Use
const batchParameters = _db.batch(`
insert into product(id, uid, name, price, active)
values(nextval('product_id'), ?, ?, ?, true)
`)
.put(_uid.generate(), "Netuno Batch 1", 3.2)
.put(_uid.generate(), "Netuno Batch 2", 5.4)
const results = batchParameters.execute()
Return
( DBBatch )
Batch execution manager.
_db.batch(sqlCommand: string) : DBBatch
Description
Starts the batch processing of executions in the database, based on a single command that will be executed multiple times with variation of the data.
How To Use
const batchParameters = _db.batch(`
insert into product(id, uid, name, price, active)
values(nextval('product_id'), ?, ?, ?, true)
`)
.put(_uid.generate(), "Netuno Batch 1", 3.2)
.put(_uid.generate(), "Netuno Batch 2", 5.4)
const results = batchParameters.execute()
Attributes
NAME | TYPE | DESCRIPTION |
---|---|---|
sqlCommand | string | SQL command that will be used as the basis for all interactions. |
Return
( DBBatch )
Batch execution manager.
checkExists
_db.checkExists() : CheckExists
Description
Checks if sequences, tables, columns and indexes exist in the database.
How To Use
if (!_db.checkExists().table("client")) {
_db.table().create(
"client",
_db.column().setName("id").setType("int").setPrimaryKey(true),
_db.column().setName("name").setType("varchar").setNotNull(true).setDefault()
);
}
Return
( CheckExists )
column
_db.column() : Column
Description
Performs the manipulation of columns in the database.
How To Use
if (!_db.checkExists().column("client", "description")) {
_db.column().rename(
"client", // Table
"description", // Old Name
"name" // New Name
);
}
Return
( Column )
config
_db.config() : Values
Description
Gets the connection configuration to the database being used. The connection details are defined in the application environment configuration document, more information in the Multiple Databases tutorial.
How To Use
_header.contentTypePlain()
const db_DEFAULT_Config = _db.getConfig()
_out.print(`The DEFAULT DB connection is: ${db_DEFAULT_Config.toJSON()}\n`)
const db_OTHER_Config = _db.init("countries").getConfig()
_out.print(`The OTHER DB connection is: ${db_OTHER_Config.toJSON()}\n`)
Return
( Values )
Configuration of the connection to the database being used.
date
_db.date() : java.sql.Date
Description
Gets the current date to be used in database operations.
Return
( java.sql.Date )
Current date.
_db.date(time: long) : java.sql.Date
Description
Through the long number that identifies the exact date, it creates a new Date object to be used in database operations.
Attributes
NAME | TYPE | DESCRIPTION |
---|---|---|
time | long | Long number for the exact date. |
Return
( java.sql.Date )
New object of type: java.sql.Date