未验证 提交 9b5a52a3 编写于 作者: S slguan 提交者: GitHub

Merge pull request #179 from StoneT2000/master

Update docs within files, better naming, and added some async functionality
......@@ -11,3 +11,5 @@ debs/
rpms/
*.pyc
*.tmp
src/connector/nodejs/node_modules/
src/connector/nodejs/out/
/**
* C Interface with TDengine Module
* @module CTaosInterface
*/
const ref = require('ref');
const ffi = require('ffi');
const ArrayType = require('ref-array');
const Struct = require('ref-struct');
const fieldTypes = require('./constants');
const FieldTypes = require('./constants');
const errors = require ('./error')
module.exports = CTaosInterface;
......@@ -145,18 +150,18 @@ function convertNchar(data, num_of_rows, nbytes = 0, offset = 0, micro=false) {
return res;
}
//Object with all the relevant converters from pblock data to javascript data
// Object with all the relevant converters from pblock data to javascript readable data
let convertFunctions = {
[fieldTypes.C_BOOL] : convertBool,
[fieldTypes.C_TINYINT] : convertTinyint,
[fieldTypes.C_SMALLINT] : convertSmallint,
[fieldTypes.C_INT] : convertInt,
[fieldTypes.C_BIGINT] : convertBigint,
[fieldTypes.C_FLOAT] : convertFloat,
[fieldTypes.C_DOUBLE] : convertDouble,
[fieldTypes.C_BINARY] : convertBinary,
[fieldTypes.C_TIMESTAMP] : convertTimestamp,
[fieldTypes.C_NCHAR] : convertNchar
[FieldTypes.C_BOOL] : convertBool,
[FieldTypes.C_TINYINT] : convertTinyint,
[FieldTypes.C_SMALLINT] : convertSmallint,
[FieldTypes.C_INT] : convertInt,
[FieldTypes.C_BIGINT] : convertBigint,
[FieldTypes.C_FLOAT] : convertFloat,
[FieldTypes.C_DOUBLE] : convertDouble,
[FieldTypes.C_BINARY] : convertBinary,
[FieldTypes.C_TIMESTAMP] : convertTimestamp,
[FieldTypes.C_NCHAR] : convertNchar
}
// Define TaosField structure
......@@ -168,7 +173,12 @@ TaosField.fields.name.type.size = 64;
TaosField.defineProperty('bytes', ref.types.short);
TaosField.defineProperty('type', ref.types.char);
//The C interface with the Taos TSDB
/**
* Constructor for the C interface with TDengine
* @constructor
* @param {Object} config - Configuration options for the interface
* @return {CTaosInterface}
*/
function CTaosInterface (config = null, pass = false) {
ref.types.char_ptr = ref.refType(ref.types.char);
ref.types.void_ptr = ref.refType(ref.types.void);
......@@ -208,7 +218,7 @@ function CTaosInterface (config = null, pass = false) {
}
else {
try {
this._config = ref.allocCString(config);;
this._config = ref.allocCString(config);
}
catch(err){
throw "Attribute Error: config is expected as a str";
......@@ -219,6 +229,7 @@ function CTaosInterface (config = null, pass = false) {
}
this.libtaos.taos_init();
}
return this;
}
CTaosInterface.prototype.config = function config() {
return this._config;
......@@ -298,7 +309,7 @@ CTaosInterface.prototype.fetchBlock = function fetchBlock(result, fields) {
if (num_of_rows == 0) {
return {block:null, num_of_rows:0};
}
let isMicro = (this.libtaos.taos_result_precision(result) == fieldTypes.C_TIMESTAMP_MICRO)
let isMicro = (this.libtaos.taos_result_precision(result) == FieldTypes.C_TIMESTAMP_MICRO)
let blocks = new Array(fields.length);
blocks.fill(null);
num_of_rows = Math.abs(num_of_rows);
......
......@@ -2,11 +2,14 @@ const TDengineCursor = require('./cursor')
const CTaosInterface = require('./cinterface')
module.exports = TDengineConnection;
/*
* TDengine Connection object
* @param {Object.<string, string>} options - Options for configuring the connection with TDengine
/**
* TDengine Connection Class
* @param {object} options - Options for configuring the connection with TDengine
* @return {TDengineConnection}
*
* @class TDengineConnection
* @constructor
* @example
* var conn = new TDengineConnection({host:"127.0.0.1", user:"root", password:"taosdata", config:"/etc/taos",port:0})
*
*/
function TDengineConnection(options) {
......@@ -18,11 +21,15 @@ function TDengineConnection(options) {
this._port = 0;
this._config = null;
this._chandle = null;
this.config(options)
this._configConn(options)
return this;
}
TDengineConnection.prototype.config = function config(options) {
/**
* Configure the connection to TDengine
* @private
* @memberof TDengineConnection
*/
TDengineConnection.prototype._configConn = function _configConn(options) {
if (options['host']) {
this._host = options['host'];
}
......@@ -44,10 +51,14 @@ TDengineConnection.prototype.config = function config(options) {
this._chandle = new CTaosInterface(this._config);
this._conn = this._chandle.connect(this._host, this._user, this._password, this._database, this._port);
}
/** Close the connection to TDengine */
TDengineConnection.prototype.close = function close() {
return this._chandle.close(this._conn);
this._chandle.close(this._conn);
}
/**
* Initialize a new cursor to interact with TDengine with
* @return {TDengineCursor}
*/
TDengineConnection.prototype.cursor = function cursor() {
//Pass the connection object to the cursor
return new TDengineCursor(this);
......@@ -58,7 +69,11 @@ TDengineConnection.prototype.commit = function commit() {
TDengineConnection.prototype.rollback = function rollback() {
return this;
}
TDengineConnection.prototype.clear_result_set = function clear_result_set() {
/**
* Clear the results from connector
* @private
*/
TDengineConnection.prototype._clearResultSet = function _clearResultSet() {
var result = this._chandle.useResult(this._conn).result;
if (result) {
this._chandle.freeResult(result)
......
/*
* TDengine Field Types
/**
* TDengine Field Types and their type codes
* @typedef {Object} FieldTypes
* @property {number} C_NULL - Null
* @property {number} C_BOOL - Boolean. Note, 0x02 is the C_BOOL_NULL value.
* @property {number} C_TINYINT - Tiny Int, values in the range [-2^7+1, 2^7-1]. Note, -2^7 has been used as the C_TINYINT_NULL value
* @property {number} C_SMALLINT - Small Int, values in the range [-2^15+1, 2^15-1]. Note, -2^15 has been used as the C_SMALLINT_NULL value
* @property {number} C_INT - Int, values in the range [-2^31+1, 2^31-1]. Note, -2^31 has been used as the C_INT_NULL value
* @property {number} C_BIGINT - Big Int, values in the range [-2^59, 2^59].
* @property {number} C_FLOAT - Float, values in the range [-3.4E38, 3.4E38], accurate up to 6-7 decimal places.
* @property {number} C_DOUBLE - Double, values in the range [-1.7E308, 1.7E308], accurate up to 15-16 decimal places.
* @property {number} C_BINARY - Binary, encoded in utf-8.
* @property {number} C_TIMESTAMP - Timestamp in format "YYYY:MM:DD HH:MM:SS.MMM". Measured in number of milliseconds passed after
1970-01-01 08:00:00.000 GMT.
* @property {number} C_NCHAR - NChar field type encoded in ASCII, a wide string.
*
*
*
* @property {number} C_TIMESTAMP_MILLI - The code for millisecond timestamps, as returned by libtaos.taos_result_precision(result).
* @property {number} C_TIMESTAMP_MICRO - The code for microsecond timestamps, as returned by libtaos.taos_result_precision(result).
*/
module.exports = {
// Type Code
C_NULL : 0,
C_BOOL : 1,
C_TINYINT : 2,
......@@ -23,7 +38,10 @@ module.exports = {
C_SMALLINT_NULL : -32768,
C_INT_NULL : -2147483648,
C_BIGINT_NULL : -9223372036854775808,
C_FLOAT_NULL : 2146435072,
C_DOUBLE_NULL : -9223370937343148032,
C_NCHAR_NULL : 4294967295,
C_BINARY_NULL : 255,
C_TIMESTAMP_MILLI : 0,
C_TIMESTAMP_MICRO : 1,
}
const CTaosInterface = require('./cinterface')
const errors = require ('./error')
const TaosQuery = require('./taosquery')
module.exports = TDengineCursor;
/**
* 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
*/
function TDengineCursor(connection=null) {
this._description = null;
this._rowcount = -1;
this._connection = null;
this._result = null;
this._fields = null;
this.data = null;
this.data = [];
this.fieldNames = null;
this.chandle = new CTaosInterface(null, true); //pass through, just need library loaded.
this._chandle = new CTaosInterface(null, true); //pass through, just need library loaded.
if (connection != null) {
this._connection = connection
}
}
/**
* Get the description of the latest query
* @return {string} Description
*/
TDengineCursor.prototype.description = function description() {
return this._description;
}
/**
* Get the row counts of the latest query
* @return {number} Rowcount
*/
TDengineCursor.prototype.rowcount = function rowcount() {
return this._rowcount;
}
TDengineCursor.prototype.callproc = function callproc() {
return;
}
/**
* 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
*/
TDengineCursor.prototype.close = function close() {
if (this._connection == null) {
return false;
}
this._connection.clear_result_set();
this._connection._clearResultSet();
this._reset_result();
this._connection = null;
return true;
}
TDengineCursor.prototype.execute = function execute(operation, params=null) {
/**
*
* @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) {
if (operation == undefined) {
return null;
}
if (this._connection == null) {
throw new errors.ProgrammingError('Cursor is not connected');
}
this._connection.clear_result_set();
this._connection._clearResultSet();
this._reset_result();
let stmt = operation;
if (params != null) {
//why pass?
}
res = this.chandle.query(this._connection._conn, stmt);
res = this._chandle.query(this._connection._conn, stmt);
if (res == 0) {
let fieldCount = this.chandle.fieldsCount(this._connection._conn);
let fieldCount = this._chandle.fieldsCount(this._connection._conn);
if (fieldCount == 0) {
return this.chandle.affectedRows(this._connection._conn); //return num of affected rows, common with insert, use statements
let response = "Query OK"
return this._chandle.affectedRows(this._connection._conn); //return num of affected rows, common with insert, use statements
}
else {
let resAndField = this.chandle.useResult(this._connection._conn, fieldCount)
let resAndField = this._chandle.useResult(this._connection._conn, fieldCount)
this._result = resAndField.result;
this._fields = resAndField.fields;
this.fieldNames = resAndField.fields.map(fieldData => fieldData.name);
return this._handle_result(); //return a pointer
return this._handle_result(); //return a pointer to the result
}
}
else {
throw new errors.ProgrammingError(this.chandle.errStr(this._connection._conn))
throw new errors.ProgrammingError(this._chandle.errStr(this._connection._conn))
}
}
TDengineCursor.prototype.executemany = function executemany() {
......@@ -77,6 +109,15 @@ TDengineCursor.prototype.fetchone = function fetchone() {
TDengineCursor.prototype.fetchmany = function fetchmany() {
}
/**
* 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()
*/
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");
......@@ -86,7 +127,7 @@ TDengineCursor.prototype.fetchall = function fetchall() {
let k = 0;
while(true) {
k+=1;
let blockAndRows = this.chandle.fetchBlock(this._result, this._fields);
let blockAndRows = this._chandle.fetchBlock(this._result, this._fields);
let block = blockAndRows.blocks;
let num_of_rows = blockAndRows.num_of_rows;
......@@ -101,9 +142,9 @@ TDengineCursor.prototype.fetchall = function fetchall() {
}
}
}
this._connection.clear_result_set();
this._connection._clearResultSet();
this.data = data;
return data; //data[i] returns the ith row, with all the data
return data;
}
TDengineCursor.prototype.nextset = function nextset() {
return;
......@@ -119,7 +160,7 @@ TDengineCursor.prototype._reset_result = function _reset_result() {
this._rowcount = -1;
this._result = null;
this._fields = null;
this.data = null;
this.data = [];
this.fieldNames = null;
}
TDengineCursor.prototype._handle_result = function _handle_result() {
......
module.exports = TaosQuery;
/**
* Constructor for TaosQuery object
* @class TaosQuery
* @constructor
* @param {string} query - Query to construct object from
* @param {TDengineCursor} - The cursor from which this query will execute from
*/
function TaosQuery(query = "", cursor = null) {
this._query = query;
this._cursor = cursor;
}
/**
* Executes the query object and returns a Promise object
* @memberof TaosQuery
* @param {function} resultCallback - A callback function that takes the results as input
* @param {function} metaCallback - A callback function that takes the metadata or fields as input
* @return {Promise<results, fields>} A promise that evaluates into an object with keys "results" and "fields"
*/
TaosQuery.prototype.execute = async function execute(resultCallback, metaCallback) {
var taosQuery = this; //store the current instance of taosQuery to avoid async issues?
var executionPromise = new Promise(function(resolve, reject) {
let results = [];
try {
taosQuery._cursor.execute(taosQuery._query);
if (taosQuery._cursor._result != null){
results = taosQuery._cursor.fetchall();
}
if (metaCallback) metaCallback(taosQuery._cursor._fields);
if (resultCallback) resultCallback(results);
}
catch(err) {
reject(err);
}
resolve({results:results, fields:taosQuery._cursor._fields})
});
return executionPromise;
}
/**
*
* @param {...args} args - A
*/
TaosQuery.prototype.bind = function bind(...args) {
args.forEach(function(arg) {
});
}
/**
* TaosResult
* @module TaosResult
*/
module.exports = TaosResult;
/**
* Constructor for a TaosResult object;
* @class TaosResult
* @constructor
* @param {Array<Row>} - Array of result rows
* @param {Array<Field>} - Array of field meta data
* @return {TaosResult}
*/
function TaosResult(result, fields) {
this.result = result;
this.rowcount = this.result.length;
this.fields = parseFields(fields);
}
TaosResult.prototype.parseFields = function parseFields(fields) {
fields.map(function(field) {
return {}
})
}
{
"name": "tdconnector",
"name": "td-connector",
"version": "1.0.5",
"lockfileVersion": 1,
"requires": true,
......@@ -144,22 +144,6 @@
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
"ctypes": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/ctypes/-/ctypes-0.0.1.tgz",
"integrity": "sha1-/C7F5ZmRwG9mNksh7yxy/SkQnLE=",
"requires": {
"array-index": "^1.0.0",
"debug": "^2.2.0",
"es6-weak-map": "^2.0.1",
"ffi": "^2.0.0",
"function-class": "^1.1.0",
"ref": "^1.3.1",
"ref-array": "^1.1.2",
"ref-struct": "^1.0.2",
"setprototypeof": "^1.0.0"
}
},
"d": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/d/-/d-1.0.1.tgz",
......@@ -238,17 +222,6 @@
"es5-ext": "~0.10.14"
}
},
"es6-weak-map": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/es6-weak-map/-/es6-weak-map-2.0.3.tgz",
"integrity": "sha512-p5um32HOTO1kP+w7PRnB+5lQ43Z6muuMuIMffvDN8ZB4GcnjLBV6zGStpbASIMk4DCAvEaamhe2zhyCb/QXXsA==",
"requires": {
"d": "1",
"es5-ext": "^0.10.46",
"es6-iterator": "^2.0.3",
"es6-symbol": "^3.1.1"
}
},
"extend": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz",
......@@ -321,25 +294,6 @@
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8="
},
"function-class": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/function-class/-/function-class-1.1.0.tgz",
"integrity": "sha1-yCK8J5x4py11je/y4kzXwPtYWLo=",
"requires": {
"es6-symbol": "^3.0.2",
"function-name": "^1.0.0",
"setprototypeof": "^1.0.0"
}
},
"function-name": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/function-name/-/function-name-1.0.0.tgz",
"integrity": "sha1-r+4YP9eh3JIi1fZae/htpYNd90U=",
"requires": {
"bindings": "^1.2.1",
"nan": "2"
}
},
"gauge": {
"version": "2.7.4",
"resolved": "https://registry.npmjs.org/gauge/-/gauge-2.7.4.tgz",
......@@ -737,11 +691,6 @@
"resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
"integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc="
},
"setprototypeof": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz",
"integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw=="
},
"signal-exit": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz",
......@@ -845,11 +794,6 @@
"punycode": "^2.1.0"
}
},
"utf8": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/utf8/-/utf8-3.0.0.tgz",
"integrity": "sha512-E8VjFIQ/TyQgp+TZfS6l8yp/xWppSAHzidGiRrqe4bK4XP9pTRyKFgGJpO3SN7zdX4DeomTrwaseCHovfpFcqQ=="
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
......
# TDengine Node.js connector
[![minzip](https://img.shields.io/bundlephobia/minzip/td-connector.svg)](https://github.com/taosdata/TDengine/tree/master/src/connector/nodejs) [![NPM](https://img.shields.io/npm/l/td-connector.svg)](https://github.com/taosdata/TDengine/#what-is-tdengine)
This is the Node.js library that lets you connect to [TDengine](https://www.github.com/taosdata/tdengine).
## Installation
......@@ -9,7 +11,7 @@ To get started, just type in the following to install the connector through [npm
npm install td-connector
```
To interact with TDengine, we make use of the [node-gyp[(https://github.com/nodejs/node-gyp)] library. To install, you will need to install the following depending on platform (the following instructions are quoted from node-gyp)
To interact with TDengine, we make use of the [node-gyp](https://github.com/nodejs/node-gyp) library. To install, you will need to install the following depending on platform (the following instructions are quoted from node-gyp)
### On Unix
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册