提交 b33a6474 编写于 作者: S StoneT2000

More documentation and added node async functionality

上级 96d82938
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 {}
})
}
# 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
......@@ -23,25 +25,25 @@ To interact with TDengine, we make use of the [node-gyp[(https://github.com/node
- Xcode
- You also need to install the
- You also need to install the
```
Command Line Tools
```
via Xcode. You can find this under the menu
via Xcode. You can find this under the menu
```
Xcode -> Preferences -> Locations
```
(or by running
(or by running
```
xcode-select --install
```
in your Terminal)
in your Terminal)
- This step will install `gcc` and the related toolchain containing `make`
......@@ -230,4 +232,4 @@ Please follow the [contribution guidelines](https://github.com/taosdata/TDengine
## License
[GNU AGPL v3.0](http://www.gnu.org/licenses/agpl-3.0.html)
\ No newline at end of file
[GNU AGPL v3.0](http://www.gnu.org/licenses/agpl-3.0.html)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册