Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 16 Next »

API Overview

The 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.

If the SQL statement is an INSERT or an UPDATE, parameter 3 and parameter 4 are not applicable.

Contents

Important!

When using an IBM i database, this API requires the Profound.js Connector module.

Parameters

  1. SQL statement string
    1. The SQL statement must be valid for the database you are using
  2. parameter or array of parameters to bind (optional)
  3. number of records to fetch (optional)
    (warning) This parameter only works with the IBM i DB2 database driver.
    (info) If omitted, SQL_FETCH_ALL is assumed.
  4. one or more Profound.js strongly typed field names (optional)
    (warning) This parameter only works with the IBM i DB2 database driver.
    (info) 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.

Return Value

  1. If parameter 4 is specified, the API does not return a value.
  2. If 1 record is requested, the API returns a JavaScript object that represents the fetched record or null if no record was found.
  3. 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

Load a Grid
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
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
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
Add a record using a primitive JavaScript object
var record = {
  id: counter,
  firstname: fname,
  lastname: lname
}
pjs.query("INSERT INTO table SET ?", record);
Add a Record
Add a record using a primitive JavaScript object
// 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
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
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
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);
  • No labels