Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.



Note

This API can be used with Profound.js in releases newer than 4.15.2. This API is currently available to use in Profound.js Spaces. 

This API will return an object containing a set of functions that can help dynamically create SQL statements based on the current database connection type.

...

Code Block
languagejs
function demo(request, response) {

  const targetDB = pjs.getDB().driver; // Default connection.

  // Build the "Select Clause"
  let columns = [];
  // This function does check for exact case match, but if not found then finds first match regardless of case.
  columns.push(pjs.sqlHelper.formatColumnName(targetDB, "customerNumber"));
  columns.push(pjs.sqlHelper.formatColumnName(targetDB, "customerName"));


  // Build the "From Clause"
  // This function does check for exact case match, but if not found then finds first match regardless of case.
  let table = pjs.sqlHelper.formatTableName(targetDB, "customers");


  // Build the "Where Clause"
  let custIDField = pjs.sqlHelper.formatColumnName(targetDB, "customerNumber");

  let fromCustID = 100;
  let toCustID = 300;
  let fromCustIDToken = pjs.sqlHelper.getParameterToken(targetDB, fromCustID, "number");
  let toCustIDToken = pjs.sqlHelper.getParameterToken(targetDB, toCustID, "number");


  // Construct entire statement
  let sqlStmt = `SELECT ${columns.join(",")} FROM ${table} WHERE ${custIDField} BETWEEN ${fromCustIDToken} AND ${toCustIDToken} Order by ${custIDField}`;


  // Get the next 5 records, after skipping the first 10 records --> aka 3rd page of 5 records per page
  sqlStmt = pjs.sqlHelper.injectLimits(targetDB, sqlStmt, 5, 10);


  // Some databases (like MSSQL) require these parameters be named instead of the just a simple ?.
  pjs.sqlHelper.finalizeSQL(targetDB, sqlStmt, [fromCustID, toCustID]);


  // Run the statement
  let results = pjs.query(sqlStmt, [fromCustID, toCustID]);


  // Send the results back to browser
  response.send(results);
}

exports.default = demo;

...