“3206461eafb4ef39ed9eb044a6ff5d5a08f94c6a”上不存在“source/git@gitcode.net:taosdata/tdengine.git”
taosquery.js 4.1 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
 * @param {TDengineCursor} cursor - The cursor from which this query will execute from
13 14
 * @param {boolean} execute - Whether or not to immedietely execute the query synchronously and fetch all results. Default is false.
 * @property {string} query - The current query in string format the TaosQuery object represents
S
StoneT2000 已提交
15 16
 * @return {TaosQuery}
 * @since 1.0.6
17
 */
S
StoneT2000 已提交
18
function TaosQuery(query = "", cursor = null, execute = false) {
19
  this.query = query;
20
  this._cursor = cursor;
S
StoneT2000 已提交
21 22 23 24
  if (execute == true) {
    return this.execute();
  }
  return this;
25 26 27
}

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

  });
  return executionPromise;
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/**
 * Executes the query object asynchronously and returns a Promise. Completes query to completion.
 * @memberof TaosQuery
 * @param {Object} options - Execution options
 * @return {Promise<TaosResult>} A promise that resolves with a TaosResult object, or rejects with an error
 * @since 1.2.0
 */
TaosQuery.prototype.execute_a = async function execute_a(options = {}) {
  var executionPromise =  new Promise( (resolve, reject) => {

  });
  var fres;
  var frej;
  var fetchPromise =  new Promise( (resolve, reject) => {
    fres = resolve;
    frej = reject;
  });
  let asyncCallbackFetchall = async function(param, res, numOfRows, blocks) {
    if (numOfRows > 0) {
73 74
      // Likely a query like insert
      fres();
75 76 77 78 79 80 81 82 83 84 85 86 87
    }
    else {
      fres(new TaosResult(blocks.data, blocks.fields));
    }
  }
  let asyncCallback = async function(param, res, code) {
    //upon success, we fetchall results
    this._cursor.fetchall_a(res, options, asyncCallbackFetchall, {});
  }
  this._cursor.execute_a(this.query, asyncCallback.bind(this), {});
  return fetchPromise;
}

88
/**
S
StoneT2000 已提交
89 90 91 92 93 94 95 96 97 98 99
 * 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
100
 */
S
StoneT2000 已提交
101 102 103 104
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; }
105
  args.forEach(function(arg) {
S
StoneT2000 已提交
106 107 108
    if (arg.constructor.name == 'TaosTimestamp') arg = "\"" + arg.toTaosString() + "\"";
    else if (arg.constructor.name == 'Date') arg = "\"" + toTaosTSString(arg) + "\"";
    else if (typeof arg == 'string') arg = "\"" + arg + "\"";
109
    this.query = this.query.replace(/\?/,arg);
S
StoneT2000 已提交
110 111
  }, this);
  return this;
112
}