taosquery.js 2.9 KB
Newer Older
S
StoneT2000 已提交
1 2
var TaosResult = require('./taosresult')
require('./globalfunc.js')
3 4 5 6 7
module.exports = TaosQuery;


/**
 * @class TaosQuery
S
StoneT2000 已提交
8 9 10
 * @classdesc The TaosQuery class is one level above the TDengine Cursor in that it makes sure to generally return promises from functions, and wrap
 * all data with objects such as wrapping a row of data with Taos Row. This is meant to enable an higher level API that allows additional
 * functionality and save time whilst also making it easier to debug and enter less problems with the use of promises.
11
 * @param {string} query - Query to construct object from
S
StoneT2000 已提交
12 13 14 15
 * @param {TDengineCursor} cursor - The cursor from which this query will execute from
 * @param {boolean} execute - Whether or not to immedietely execute the query and fetch all results. Default is false.
 * @return {TaosQuery}
 * @since 1.0.6
16
 */
S
StoneT2000 已提交
17
function TaosQuery(query = "", cursor = null, execute = false) {
18 19
  this._query = query;
  this._cursor = cursor;
S
StoneT2000 已提交
20 21 22 23
  if (execute == true) {
    return this.execute();
  }
  return this;
24 25 26
}

/**
S
StoneT2000 已提交
27
 * Executes the query object and returns a Promise
28
 * @memberof TaosQuery
S
StoneT2000 已提交
29 30
 * @return {Promise<TaosResult>} A promise that resolves with a TaosResult object, or rejects with an error
 * @since 1.0.6
31
 */
S
StoneT2000 已提交
32
TaosQuery.prototype.execute = async function execute() {
33 34
  var taosQuery = this; //store the current instance of taosQuery to avoid async issues?
  var executionPromise = new Promise(function(resolve, reject) {
S
StoneT2000 已提交
35 36 37
    let data = [];
    let fields = [];
    let result;
38 39
    try {
      taosQuery._cursor.execute(taosQuery._query);
S
StoneT2000 已提交
40 41 42
      if (taosQuery._cursor._fields) fields = taosQuery._cursor._fields;
      if (taosQuery._cursor._result != null) data = taosQuery._cursor.fetchall();
      result = new TaosResult(data, fields)
43 44 45 46
    }
    catch(err) {
      reject(err);
    }
S
StoneT2000 已提交
47
    resolve(result)
48 49 50 51 52 53

  });
  return executionPromise;
}

/**
S
StoneT2000 已提交
54 55 56 57 58 59 60 61 62 63 64
 * Bind arguments to the query and automatically parses them into the right format
 * @param {array | ...args} args - A number of arguments to bind to each ? in the query
 * @return {TaosQuery}
 * @example
 * // An example of binding a javascript date and a number to a query
 * var query = cursor.query("select count(*) from meterinfo.meters where ts <= ? and areaid = ?").bind(new Date(), 3);
 * var promise1 = query.execute();
 * promise1.then(function(result) {
 *   result.pretty(); // Log the prettified version of the results.
 * });
 * @since 1.0.6
65
 */
S
StoneT2000 已提交
66 67 68 69
TaosQuery.prototype.bind = function bind(f, ...args) {
  if (typeof f == 'object' && f.constructor.name != 'Array') args.unshift(f); //param is not an array object
  else if (typeof f != 'object') args.unshift(f);
  else { args = f; }
70
  args.forEach(function(arg) {
S
StoneT2000 已提交
71 72 73 74 75 76
    if (arg.constructor.name == 'TaosTimestamp') arg = "\"" + arg.toTaosString() + "\"";
    else if (arg.constructor.name == 'Date') arg = "\"" + toTaosTSString(arg) + "\"";
    else if (typeof arg == 'string') arg = "\"" + arg + "\"";
    this._query = this._query.replace(/\?/,arg);
  }, this);
  return this;
77
}