connection.js 2.3 KB
Newer Older
1 2 3 4
const TDengineCursor = require('./cursor')
const CTaosInterface = require('./cinterface')
module.exports = TDengineConnection;

5 6 7
/**
 * TDengine Connection Class
 * @param {object} options - Options for configuring the connection with TDengine
8
 * @return {TDengineConnection}
9 10 11
 * @class TDengineConnection
 * @constructor
 * @example
S
StoneT2000 已提交
12
 * //Initialize a new connection
13
 * var conn = new TDengineConnection({host:"127.0.0.1", user:"root", password:"taosdata", config:"/etc/taos",port:0})
14 15 16 17 18 19 20 21 22 23 24
 *
 */
function TDengineConnection(options) {
  this._conn = null;
  this._host = null;
  this._user = "root"; //The default user
  this._password = "taosdata"; //The default password
  this._database = null;
  this._port = 0;
  this._config = null;
  this._chandle = null;
25
  this._configConn(options)
26 27
  return this;
}
28 29 30 31 32 33
/**
 * Configure the connection to TDengine
 * @private
 * @memberof TDengineConnection
 */
TDengineConnection.prototype._configConn = function _configConn(options) {
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  if (options['host']) {
    this._host = options['host'];
  }
  if (options['user']) {
    this._user = options['user'];
  }
  if (options['password']) {
    this._password = options['password'];
  }
  if (options['database']) {
    this._database = options['database'];
  }
  if (options['port']) {
    this._port = options['port'];
  }
  if (options['config']) {
    this._config = options['config'];
  }
  this._chandle = new CTaosInterface(this._config);
  this._conn = this._chandle.connect(this._host, this._user, this._password, this._database, this._port);
}
55
/** Close the connection to TDengine */
56
TDengineConnection.prototype.close = function close() {
57
  this._chandle.close(this._conn);
58
}
59 60 61 62
/**
 * Initialize a new cursor to interact with TDengine with
 * @return {TDengineCursor}
 */
63 64 65 66 67 68 69 70 71 72
TDengineConnection.prototype.cursor = function cursor() {
  //Pass the connection object to the cursor
  return new TDengineCursor(this);
}
TDengineConnection.prototype.commit = function commit() {
  return this;
}
TDengineConnection.prototype.rollback = function rollback() {
  return this;
}
73 74 75 76 77
/**
 * Clear the results from connector
 * @private
 */
TDengineConnection.prototype._clearResultSet = function _clearResultSet() {
78 79 80 81 82
  var result = this._chandle.useResult(this._conn).result;
  if (result) {
    this._chandle.freeResult(result)
  }
}