Versions Compared

Key

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

...

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;

...