taosresult.js 2.4 KB
Newer Older
S
StoneT2000 已提交
1 2 3 4
require('./globalfunc.js')
const TaosObjects = require('./taosobjects');
const TaosRow = TaosObjects.TaosRow;
const TaosField = TaosObjects.TaosField;
5

S
StoneT2000 已提交
6
module.exports = TaosResult;
7 8
/**
 * @class TaosResult
S
StoneT2000 已提交
9 10 11 12 13
 * @classdesc A TaosResult class consts of the row data and the fields metadata, all wrapped under various objects for higher functionality.
 * @param {Array<TaosRow>} data - Array of result rows
 * @param {Array<TaosField>} fields - Array of field meta data
 * @property {Array<TaosRow>} data - Array of TaosRows forming the result data (this does not include field meta data)
 * @property {Array<TaosField>} fields - Array of TaosFields forming the fields meta data array.
14
 * @return {TaosResult}
S
StoneT2000 已提交
15
 * @since 1.0.6
16
 */
S
StoneT2000 已提交
17 18 19 20
function TaosResult(data, fields) {
  this.data = data.map(row => new TaosRow(row));
  this.rowcount = this.data.length;
  this.fields = fields.map(field => new TaosField(field));
21
}
S
StoneT2000 已提交
22 23 24 25
/**
 * Pretty print data and the fields meta data as if you were using the taos shell
 * @memberof TaosResult
 * @function pretty
26
 * @since 1.0.6
S
StoneT2000 已提交
27
 */
28

S
StoneT2000 已提交
29 30 31 32 33 34 35 36 37 38
TaosResult.prototype.pretty = function pretty() {
  let fieldsStr = "";
  let sizing = [];
  this.fields.forEach((field,i) => {
    if (field._field.type == 8 || field._field.type == 10){
      sizing.push(Math.max(field.name.length, field._field.bytes));
    }
    else {
      sizing.push(Math.max(field.name.length, suggestedMinWidths[field._field.type]));
    }
39
    fieldsStr += fillEmpty(Math.floor(sizing[i]/2 - field.name.length / 2)) + field.name + fillEmpty(Math.ceil(sizing[i]/2 - field.name.length / 2)) + " | ";
S
StoneT2000 已提交
40 41 42 43 44 45 46 47 48 49
  });
  var sumLengths = sizing.reduce((a,b)=> a+=b,(0)) + sizing.length * 3;

  console.log("\n" + fieldsStr);
  console.log(printN("=",sumLengths));
  this.data.forEach(row => {
    let rowStr = "";
    row.data.forEach((entry, i) => {
      if (this.fields[i]._field.type == 9) {
        entry = entry.toTaosString();
50
      } else {
51
        entry = entry == null ? 'null' : entry.toString();
S
StoneT2000 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      }
      rowStr += entry
      rowStr += fillEmpty(sizing[i] - entry.length) + " | ";
    });
    console.log(rowStr);
  });
}
const suggestedMinWidths = {
  0: 4,
  1: 4,
  2: 4,
  3: 6,
  4: 11,
  5: 12,
  6: 24,
  7: 24,
  8: 10,
  9: 25,
  10: 10,
}
function printN(s, n) {
  let f = "";
  for (let i = 0; i < n; i ++) {
    f += s;
  }
  return f;
}
function fillEmpty(n) {
  let str = "";
  for (let i = 0; i < n; i++) {
    str += " ";
  }
  return str;
85
}