cursor.js 5.4 KB
Newer Older
1 2
const CTaosInterface = require('./cinterface')
const errors = require ('./error')
3
const TaosQuery = require('./taosquery')
4 5
module.exports = TDengineCursor;

6 7 8 9 10 11 12 13
/**
 * Constructor for a TDengine Cursor
 * @class TDengineCursor
 * @constructor
 * @param {TDengineConnection} - The TDengine Connection this cursor uses to interact with TDengine
 * @property {data} - Latest retrieved data from query execution. It is an empty array by default
 * @property {fieldNames} - Array of the field names in order from left to right of the latest data retrieved
 */
14 15 16 17 18 19
function TDengineCursor(connection=null) {
  this._description = null;
  this._rowcount = -1;
  this._connection = null;
  this._result = null;
  this._fields = null;
20
  this.data = [];
21
  this.fieldNames = null;
22
  this._chandle = new CTaosInterface(null, true); //pass through, just need library loaded.
23 24 25 26 27
  if (connection != null) {
    this._connection = connection
  }

}
28 29 30 31
/**
 * Get the description of the latest query
 * @return {string} Description
 */
32 33 34
TDengineCursor.prototype.description = function description() {
  return this._description;
}
35 36 37 38
/**
 * Get the row counts of the latest query
 * @return {number} Rowcount
 */
39 40 41 42 43 44
TDengineCursor.prototype.rowcount = function rowcount() {
  return this._rowcount;
}
TDengineCursor.prototype.callproc = function callproc() {
  return;
}
45 46 47 48
/**
 * Close the cursor by setting its connection to null and freeing results from the connection and resetting the results it has stored
 * @return {boolean} Whether or not the cursor was succesfully closed
 */
49 50 51 52
TDengineCursor.prototype.close = function close() {
  if (this._connection == null) {
    return false;
  }
53
  this._connection._clearResultSet();
54 55 56 57
  this._reset_result();
  this._connection = null;
  return true;
}
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/**
 *
 * @return {TaosQuery} - A TaosQuery object
 */
TDengineCursor.prototype.query = function query(operation) {
  return new TaosQuery(operation, this);
}

/**
 * Execute a query. Also stores all the field names returned from the query into cursor.fieldNames;
 * @param {string} operation -  The query operation to execute in the taos shell
 * @param {number} verbose - A verbosity of 0 silences any logs from this function. A verbosity of 1 means this function will log query statues
 * @return {number | buffer} Number of affected rows or a buffer that points to the results of the query
 */
TDengineCursor.prototype.execute = function execute(operation, verbose) {
73 74 75 76 77 78
  if (operation == undefined) {
    return null;
  }
  if (this._connection == null) {
    throw new errors.ProgrammingError('Cursor is not connected');
  }
79
  this._connection._clearResultSet();
80 81 82
  this._reset_result();

  let stmt = operation;
83
  res = this._chandle.query(this._connection._conn, stmt);
84
  if (res == 0) {
85
    let fieldCount = this._chandle.fieldsCount(this._connection._conn);
86
    if (fieldCount == 0) {
87 88
      let response = "Query OK"
      return this._chandle.affectedRows(this._connection._conn); //return num of affected rows, common with insert, use statements
89 90
    }
    else {
91
      let resAndField = this._chandle.useResult(this._connection._conn, fieldCount)
92 93 94
      this._result = resAndField.result;
      this._fields = resAndField.fields;
      this.fieldNames = resAndField.fields.map(fieldData => fieldData.name);
95
      return this._handle_result(); //return a pointer to the result
96 97 98
    }
  }
  else {
99
    throw new errors.ProgrammingError(this._chandle.errStr(this._connection._conn))
100 101 102 103 104 105 106 107 108 109 110 111
  }

}
TDengineCursor.prototype.executemany = function executemany() {

}
TDengineCursor.prototype.fetchone = function fetchone() {

}
TDengineCursor.prototype.fetchmany = function fetchmany() {

}
112 113 114 115 116 117 118 119 120
/**
 * Fetches all results from a query and also stores results into cursor.data
 *
 * @return {Array<Row>} The resultant array, with entries corresponding to each retreived row from the query results, sorted in
 * order by the field name ordering in the table.
 * @example
 * cursor.execute('select * from db.table');
 * var data = cursor.fetchall()
 */
121 122 123 124 125 126 127 128 129
TDengineCursor.prototype.fetchall = function fetchall() {
  if (this._result == null || this._fields == null) {
    throw new errors.OperationalError("Invalid use of fetchall, either result or fields from query are null");
  }
  let data = [];
  this._rowcount = 0;
  let k = 0;
  while(true) {
    k+=1;
130
    let blockAndRows = this._chandle.fetchBlock(this._result, this._fields);
131 132 133 134 135 136 137 138 139 140 141 142 143 144
    let block = blockAndRows.blocks;
    let num_of_rows = blockAndRows.num_of_rows;

    if (num_of_rows == 0) {
      break;
    }
    this._rowcount += num_of_rows;
    for (let i = 0; i < num_of_rows; i++) {
      data.push([]);
      for (let j = 0; j < this._fields.length; j++) {
        data[data.length-1].push(block[j][i]);
      }
    }
  }
145
  this._connection._clearResultSet();
146
  this.data = data;
147
  return data;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
}
TDengineCursor.prototype.nextset = function nextset() {
  return;
}
TDengineCursor.prototype.setinputsize = function setinputsize() {
  return;
}
TDengineCursor.prototype.setoutputsize = function setoutputsize(size, column=null) {
  return;
}
TDengineCursor.prototype._reset_result = function _reset_result() {
  this._description = null;
  this._rowcount = -1;
  this._result = null;
  this._fields = null;
163
  this.data = [];
164 165 166 167 168 169 170 171 172
  this.fieldNames = null;
}
TDengineCursor.prototype._handle_result = function _handle_result() {
  this._description = [];
  for (let field of this._fields) {
    this._description.push([field.name, field.type, null, null, null, null, false]);
  }
  return this._result;
}