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<Values>
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
_db.date(text: string) : java.sql.Date
Description
With the text content you get the date object to be used in database operations.
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| text | string | Text containing date in the format: yyyy-MM-dd |
Return
( java.sql.Date )
Date obtained from the text.
_db.date(instant: java.time.Instant) : java.util.Date
Description
Using the Instant object, it creates a new Date object with java.sql.Date.from, to be used in database operations.
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| instant | java.time.Instant | Object of type: java.time.Instant |
Return
( java.util.Date )
New object of type: java.util.Date
_db.date(localDate: java.time.LocalDate) : java.sql.Date
Description
Using the LocalDateTime object, it creates a new Date object to be used in database operations.
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| localDate | java.time.LocalDate | Object of type: java.time.LocalDate |
Return
( java.sql.Date )
New object of type: java.sql.Date
delete
_db.delete(table: string, id: int) : int
Description
Performs the deletion of records in the database based on the ID.
How To Use
// Performs data deletion via ID
const id = 1;
const result = _db.delete(
"client",
id
);
_out.json(
"result": result
);
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| table | string | Table's name in the database. |
| id | int | Record's ID to be deleted. |
Return
( int )
Number of records affected by the deletion.
_db.delete(table: string, uid: string) : int
Description
Performs the deletion of records in the database based on the UID.
How To Use
// Executa a eliminação através do uid
const uid = "1d8722f4-fa28-4a08-8098-6dd5cab1b212";
const result = _db.delete(
"client",
uid
);
_out.json(
"result": result
);
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| table | string | Table's name in the database. |
| uid | string | Record's UID to be deleted. |
Return
( int )
Number of records affected by the deletion.
_db.delete(table: string, data: Values | DataSchema | java.util.Map) : int
Description
Performs the deletion of records in the database based on the ID or UID passed on the data object.
How To Use
// Performs data deletion via ID or UID
const result = _db.delete(
"client",
_val.map().set("id", 1)
);
_out.json(
"result": result
);
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| table | string | Table name in the database. |
| data | _Values | DataSchema | java.util.Map_ |
Return
( int )
Number of records affected by deletion.
_db.delete(table: string, data: Values) : int
Description
Performs the deletion of records in the database based on the ID or UID passed on the data object.
How To Use
// Performs data deletion via ID or UID
const result = _db.delete(
"client",
_val.map().set("id", 1)
);
_out.json(
"result": result
);
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| table | string | Table name in the database. |
| data | Values | Data structure that should be deleted based on your ID or UID. |
Return
( int )
Number of records affected by deletion.
deleteMany
_db.deleteMany(table: string, dataItems: java.lang.Object[]) : int[]
Description
Executes the update of multiple rows into a table of an Array of Objects or a List of Values. It's necessary that the object has id or uid for the data that will be updated.
How To Use
var deletedData = _db.deleteMany(
'worker',
_val.list()
.add(_val.map()
.set('id', 1)
)
.add(_val.map()
.set('id', 2)
)
);
_out.json({deletedData: deletedData})
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| table | string | Name of the table in the database that should receive the data to be entered. |
| dataItems | java.lang.Object[] | Array or list of objects with the data structure to be inserted. |
Return
( int[] )
Array with number of records affected by the deletion for each object inside the list.
_db.deleteMany(table: string, dataItems: java.util.List) : int[]
Description
Executes the update of multiple rows into a table of an Array of Objects or a List of Values. It's necessary that the object has id or uid for the data that will be updated.
How To Use
var deletedData = _db.deleteMany(
'worker',
_val.list()
.add(_val.map()
.set('id', 1)
)
.add(_val.map()
.set('id', 2)
)
);
_out.json({deletedData: deletedData})
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| table | string | Name of the table in the database that should receive the data to be entered. |
| dataItems | java.util.List | Array or list of objects with the data structure to be inserted. |
Return
( int[] )
Array with number of records affected by the deletion for each object inside the list.
_db.deleteMany(table: string, dataItems: Values) : int[]
Description
Executes the update of multiple rows into a table of an Array of Objects or a List of Values. It's necessary that the object has id or uid for the data that will be updated.
How To Use
var deletedData = _db.deleteMany(
'worker',
_val.list()
.add(_val.map()
.set('id', 1)
)
.add(_val.map()
.set('id', 2)
)
);
_out.json({deletedData: deletedData})
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| table | string | Name of the table in the database that should receive the data to be entered. |
| dataItems | Values | Array or list of objects with the data structure to be inserted. |
Return
( int[] )
Array with number of records affected by the deletion for each object inside the list.
escape
_db.escape(data: string) : string
Description
It guarantees the encoding to define names in the database, usually quotation marks (").
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| data | string | Name that needs to be used in the database, such as table or column name |
Return
( string )
Safe name to use in databases, as in tables and columns.
escapeEnd
_db.escapeEnd() : string
Description
Gets the end encoding for defining names in the database, usually quotes (").
Return
( string )
End of names in database.
escapeStart
_db.escapeStart() : string
Description
Gets the start encoding for defining names in the database, usually quotes (").
Return
( string )
Beginning of names in database.
execute
_db.execute(sqlCommand: string) : int
Description
Execute commands directly on the database, commands such as inserts and updates can be executed as required.
How To Use
const rowsAffected = _db.execute(`
insert into product(id, uid, name, price, active)
values(nextval('product_id'), '${_uid.generate()}', '${_db.sanitize('Netuno Insert Test 1')}', 3.2, true)
`)
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| sqlCommand | string | SQL command that will be executed directly on the database. |
Return
( int )
Number of lines affected by the executed command.
_db.execute(sqlCommand: string, params: java.lang.Object[]) : int
Description
Execute commands directly on the database, commands such as inserts and updates can be executed as required.
How To Use
const rowsAffected = _db.execute(`
insert into product(id, uid, name, price, active)
values(nextval('product_id'), ?, ?, ?, true)
`, _uid.generate(), "Netuno Insert Test 1", 3.2)
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| sqlCommand | string | SQL command that will be executed directly on the database. |
| params | java.lang.Object[] | The sequence of parameter values that are injected into the command. |
Return
( int )
Number of lines affected by the executed command.
_db.execute(sqlCommand: string, params: java.util.List) : int
Description
Execute commands directly on the database, commands such as inserts and updates can be executed as required.
How To Use
const rowsAffected = _db.execute(`
insert into product(id, uid, name, price, active)
values(nextval('product_id'), ?, ?, ?, true)
`, _val.list()
.add(_uid.generate())
.add("Netuno Insert Test 1")
.add(3.2)
)
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| sqlCommand | string | SQL command that will be executed directly on the database. |
| params | java.util.List | List of parameter values that are injected into the command. |
Return
( int )
Number of lines affected by the executed command.
_db.execute(sqlCommand: string, params: Values) : int
Description
Execute commands directly on the database, commands such as inserts and updates can be executed as required.
How To Use
const rowsAffected = _db.execute(`
insert into product(id, uid, name, price, active)
values(nextval('product_id'), ?, ?, ?, true)
`, _val.list()
.add(_uid.generate())
.add("Netuno Insert Test 1")
.add(3.2)
)
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| sqlCommand | string | SQL command that will be executed directly on the database. |
| params | Values | List of parameter values that are injected into the command. |
Return
( int )
Number of lines affected by the executed command.
find
_db.find(table: string, params: Values) : java.util.List<Values>
Description
From an object that has the structure similar to an SQL query, you get a list of the data found in the search conditions. Build the query compatible with any type of database. Allows conditions, ordering, avoids SQL Injection, among others. Example that demonstrates how to define columns, conditions, ordering and pagination:
How To Use
const list = _db.find(
"pessoa",
_val.map()
.set(
"columns",
_val.list()
.add("uid")
.add(
_val.map()
.set("name", "John")
.set("surname", "Lennon")
)
.add("email")
)
.set(
"where",
_val.map()
.set("grupo", 1)
.set(
"email",
_val.map()
.set("operator", "like")
.set("value", "%@e-mail.sample")
)
)
.set(
"order",
_val.list()
.add("name")
.add("surname")
)
.set("limit", 10)
.set("offset", 5)
)
_out.json(
list
)
Attributes
| NAME | TYPE | DESCRIPTION |
|---|---|---|
| table | string | Table name. |
| params | Values | Query definition, supports limiting columns (columns), adding conditions (where), ordering (order), among others. |