Section |
---|
Column |
---|
| API OverviewThe pjs.query() API is a shortcut for executing an SQL statement and automatically fetching the results. The results are returned by default as a primitive JavaScript array or object. However, if the 4th parameter is specified, the results are instead returned into a set of strongly defined fields, a strongly defined array, or a strongly defined data structure. This API first prepares the SQL statement, binds parameters (if any), executes the statement, and then fetches the records. This API also has the capability to INSERT or UPDATE records using a primitive JavaScript object that represent fields and their corresponding values. Note |
---|
If the SQL statement is an INSERT or an UPDATE , parameter 3 and parameter 4 are not applicable. |
|
Column |
---|
| Contents Info |
---|
| Table of Contents |
---|
maxLevel | 6 |
---|
minLevel | 2 |
---|
indent | 5px15px |
---|
classstyle | h3none |
---|
separator | braces |
---|
|
|
HTML Comment |
---|
| This column is for a table of contents or notice. If neither are needed, delete this column. If only one is needed, delete the other. If more are needed, you can add what you like, remember to check how long it becomes by clicking "Preview" in the bottom right corner. While preview, the page should be checked at half page and maximize to check that the formatting is not distorted. You may need to make some adjustments to compensate. |
|
|
Warning |
---|
|
This When using an IBM i database, this API requires the Profound.js Connector module. Some of the Examples listed below are not available in all versions of Profound.js. |
Section |
---|
Column |
---|
|
|
Parameters
SQL statement string
Parameters
Info |
---|
This API can accept an optional database connection definition object to select between multiple database connections. To select the database connection, call pjs.getDB() and pass the result as the first parameter, followed by the parameters documented below. If a database connection definition object is not passed, the default database connection is used. |
...
- Database Connection (optional/omittable)
Pass an object returned by pjs.getDB() to use an alternate database connection. If not passed, the default connection will be used. - Query Options (optional/omittable)
Pass an object containing statement attributes to set before executing the query. - SQL statement string
The SQL statement must be valid for the database you are using - parameter or array of parameters to bind (optional)
- number of records to fetch (optional)
This parameter only works with the IBM i DB2 database driver.
If omitted, SQL_FETCH_ALL
is assumed. - one or more Profound.js strongly typed field names (optional)
This parameter only works with the IBM i DB2 database driver.
If field names are passed, they will be bound to columns in the resulting rows and populated by this API if the fetch is successful.
...
- number of rows per INSERT SQL request on a multi-insert action (optional; if not specified, the default is 100; available in Profound.s version 5.2.0). This parameter is applicable only for an INSERT statement.
Return Value
- If
...
- parameter 4 is specified, the API does not return a value.
- If 1 record is requested, the API returns a JavaScript object that represents the fetched record or null if no record was found.
- If more than 1 record is requested, the API returns an array of JavaScript objects, where each Object represents a record. If no records were fetched, an empty array is returned.
...
Examples
16MB of data is the limit for the whole result from a pjs.query() request with the IBM i Connector. (The limit is not 16MB per column). |
Examples
Load a Grid
Code Block |
---|
language | javascript |
---|
title | Load a grid where screen fields match database fields |
---|
|
pjs.defineDisplay("display", "mydisplay.json");
var records = pjs.query("SELECT * FROM ORDERS");
display.grid1.replaceRecords(records); |
Retrieve Records by Criteria
Code Block |
---|
language | javascript |
---|
title | Retrieve records that match search critertia |
---|
|
search = '%' + search.trim() + '%'; // adjust for LIKE search
var records = pjs.query("SELECT * FROM EMPLOYEES WHERE firstname LIKE ? OR LASTNAME LIKE ?", [search.trim(), search.trim()]); |
Examples with Primitive JavaScript Objects
Retrieve a Description
Code Block |
---|
language | javascript |
---|
title | Retrieve product description through a primitive JavaScript object |
---|
|
var productId = 123;
var record = pjs.query("SELECT prdesc* FROM PRODUCTS WHERE prid = ?", productId, 1);
if (record != null) {
var description = record.prdesc;
} |
Add a Record
Code Block |
---|
language | javascript |
---|
title | Add a record using a primitive JavaScript object |
---|
|
var record = {
id: counter,
firstname: fname,
lastname: lname
}
pjs.query("INSERT INTO table SET ?", record); |
Add multiple records with one INSERT statement (available in Profound.js version 5.0.0)
Code Block |
---|
title | Add multiple records using an array of primitive JavaScript objects |
---|
|
var record1 = {flda: "AAA", fldb: "BBB"};
var record2 = {flda: "XXX", fldb: "YYY"};
var recordArray = [record1, record2] ;
// 5th parameter is not specified, so default of 100 is used; there will be 100 rows per INSERT SQL request
pjs.query("INSERT INTO table SET ?", recordArray);
// 5th parameter is specified as 50, so there will be 50 rows per INSERT SQL request
pjs.query("INSERT INTO table SET ?", recordArray, null, null, 50);
|
Add a Record
Code Block |
---|
title | Load various types of data values into a table with four columns |
---|
|
var version = "12345";
var record = {};
record.OneField = version; // variable
record.TwoField = data[1].__SOMEFIELD; // object property
record.RedField = "Suspended"; // literal
record.BlueField = cvtDate (data[1].DUEDATE); // function call
pjs.query("INSERT INTO table SET ?", record) |
Update a Record
Code Block |
---|
language | javascript |
---|
title | Update a record using a primitive JavaScript object |
---|
|
var fieldsToUpdate = {};
fieldsToUpdate.firstname = fname;
fieldsToUpdate.lastname = lname;
pjs.query("UPDATE table SET ? WHERE id = ?", [ fieldsToUpdate, currentId ]); |
Examples with Strongly Typed Fields
Retrieve a Description
Code Block |
---|
language | javascript |
---|
title | Retrieve product description into a strongly typed field |
---|
|
var productId = 123;
pjs.define("prdesc", { type: "char", length: 60 });
pjs.query("SELECT prdesc FROM PRODUCTS WHERE prid = ?", productId, 1, prdesc);
var description = prdesc; |
Retrieve Multiple Fields
Code Block |
---|
language | javascript |
---|
title | Retrieve multiple strongly typed fields |
---|
|
var productId = 123;
pjs.define("prdesc", { type: "char", length: 60 });
pjs.define("prprice", { type: "packed", length: 7, decimals: 2 });
pjs.query("SELECT prdesc, *prprice FROM PRODUCTS WHERE prid = ?", productId, 1, prdesc, prrice); |
Case of column names in result set for IBMi database
For IBMi database, prior to Profound.js release 5.7.0, the names of the columns in the result set returned by pjs.query() and other Profound.js APIs are always in lowercase. Since Profound.js release 5.7.0, you can use either configuration keepColNameCase (in effect for the entire instance ) or API pjs.setOptions() (in effect for the current Profound.js session) to instruct pjs.query() and other SQL APIs to keep the case of the columns names as is, as resulted from the SQL statement. For backward compatibility and to avoid breaking current user code
...
language | javascript |
---|
title | Retrieve records that match search critertia |
---|
...
search = '%' + search.trim() + '%'; // adjust for LIKE search
var records = pjs.query("SELECT * FROM EMPLOYEES WHERE firstname LIKE ? OR LASTNAME LIKE ?", [search.trim(), search.trim()]);
Code Block |
---|
language | javascript |
---|
title | Add a record using a primitive JavaScript object |
---|
|
var record = {
id: counter,
firstname: fname,
lastname: lname
}
pjs.query("INSERT INTO table SET ?", record); |
The following example is available with Profound.js version 3.0.3 and later.
...
language | javascript |
---|
title | Update a record using a primitive JavaScript object |
---|
...
, the default behavior is the "old" behavior; that is, the column names are always in lowercase.
Default behavior: column names are in lowercase
Code Block |
---|
title | Default behavior: column names are in lowercase |
---|
|
// Example web service
function app(req, res) {
let result = pjs.query(`select name as "custName", custno as "custNumber"
from custadrp
where custno = 1234`);
res.json(result[0]);
}
exports.run = app;
// data returned:
{
"custname": "My Company",
"custnumber": 1234
} |
To keep case of column names by using pjs.setOptions()
Code Block |
---|
title | To keep case of column names by using pjs.setOptions() |
---|
|
// Example web service
function app(req, res) {
pjs.setOptions({keepColNameCase: true}); // to keep case of column names as is; in effect for this session
let result = pjs.query(`select name as "custName", custno as "custNumber"
from custadrp
where custno = 1234`);
res.json(result[0]);
}
exports.run = app;
// data returned:
{
"custName": "My Company",
"custNumber": 1234
} |
To keep case of column names, for entire instance
Code Block |
---|
title | To keep case of column names, for entire instance |
---|
|
// Set configuration setting "keepColNameCase" in config.js to "true", to enable that behavior for entire instance
keepColNameCase: true |