From 2b84d45098ed79df9cbc74665b54abd95530540f Mon Sep 17 00:00:00 2001 From: liu0x54 Date: Thu, 30 Jul 2020 13:58:36 +0000 Subject: [PATCH] [TD-1001] adapt nodejs connector --- src/connector/nodejs/nodetaos/cinterface.js | 30 ++++++++------------- src/connector/nodejs/nodetaos/cursor.js | 10 +++---- src/connector/nodejs/test/test.js | 17 ++++++------ 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/connector/nodejs/nodetaos/cinterface.js b/src/connector/nodejs/nodetaos/cinterface.js index c8ba31dc68..656741ea16 100644 --- a/src/connector/nodejs/nodetaos/cinterface.js +++ b/src/connector/nodejs/nodetaos/cinterface.js @@ -346,7 +346,7 @@ CTaosInterface.prototype.useResult = function useResult(result) { }) } } - return {fields:fields} + return fields; } CTaosInterface.prototype.fetchBlock = function fetchBlock(result, fields) { let pblock = ref.ref(ref.ref(ref.NULL)); // equal to our raw data @@ -355,7 +355,6 @@ CTaosInterface.prototype.fetchBlock = function fetchBlock(result, fields) { return {block:null, num_of_rows:0}; } var fieldL = this.libtaos.taos_fetch_lengths(result); - var numoffields = this.libtaos.taos_field_count(result); let isMicro = (this.libtaos.taos_result_precision(result) == FieldTypes.C_TIMESTAMP_MICRO); @@ -363,26 +362,25 @@ CTaosInterface.prototype.fetchBlock = function fetchBlock(result, fields) { if (ref.isNull(fieldL) == false) { - for (let i = 0; i < numoffields; i ++) { + for (let i = 0; i < fields.length; i ++) { let plen = ref.reinterpret(fieldL, 4, i*4); let len = plen.readInt32LE(0); fieldlens.push(len); } } - let blocks = new Array(numoffields); + let blocks = new Array(fields.length); blocks.fill(null); num_of_rows = Math.abs(num_of_rows); let offset = 0; pblock = pblock.deref(); - for (let i = 0; i < numoffields; i++) { - - if (!convertFunctions[fields['fields'][i]['type']] ) { + for (let i = 0; i < fields.length; i++) { + pdata = ref.reinterpret(pblock,8,i*8); + pdata = ref.ref(pdata.readPointer()); + if (!convertFunctions[fields[i]['type']] ) { throw new errors.DatabaseError("Invalid data type returned from database"); } - blocks[i] = convertFunctions[fields['fields'][i]['type']](pblock, num_of_rows, fieldlens[i], offset, isMicro); - console.log(blocks[i]); - offset += fields['fields'][i]['bytes'] * num_of_rows; + blocks[i] = convertFunctions[fields[i]['type']](pdata, 1, fieldlens[i], offset, isMicro); } return {blocks: blocks, num_of_rows:Math.abs(num_of_rows)} } @@ -429,9 +427,7 @@ CTaosInterface.prototype.fetch_rows_a = function fetch_rows_a(result, callback, let asyncCallbackWrapper = function (param2, result2, numOfRows2) { // Data preparation to pass to cursor. Could be bottleneck in query execution callback times. let row = cti.libtaos.taos_fetch_row(result2); - console.log(row); let fields = cti.fetchFields_a(result2); - let isMicro = (cti.libtaos.taos_result_precision(result2) == FieldTypes.C_TIMESTAMP_MICRO); let blocks = new Array(fields.length); @@ -446,21 +442,17 @@ CTaosInterface.prototype.fetch_rows_a = function fetch_rows_a(result, callback, let plen = ref.reinterpret(fieldL, 8, i*8); let len = ref.get(plen,0,ref.types.int32); fieldlens.push(len); - console.log('11111111111111111111'); - console.log(fields.length); - console.log(len); } } - if (numOfRows2 > 0){ for (let i = 0; i < fields.length; i++) { if (!convertFunctions[fields[i]['type']] ) { throw new errors.DatabaseError("Invalid data type returned from database"); } let prow = ref.reinterpret(row,8,i*8); - //blocks[i] = convertFunctions[fields[i]['type']](ref.get(prow,0,ref.types.void_ptr), numOfRows2, fieldlens[i], 0, isMicro); - console.log(prow); - blocks[i] = convertFunctions[fields[i]['type']](ref.readPointer(prow), numOfRows2, fieldlens[i], 0, isMicro); + prow = prow.readPointer(); + prow = ref.ref(prow); + blocks[i] = convertFunctions[fields[i]['type']](prow, 1, fieldlens[i], offset, isMicro); //offset += fields[i]['bytes'] * numOfRows2; } } diff --git a/src/connector/nodejs/nodetaos/cursor.js b/src/connector/nodejs/nodetaos/cursor.js index 390e1becbc..8296a2132c 100644 --- a/src/connector/nodejs/nodetaos/cursor.js +++ b/src/connector/nodejs/nodetaos/cursor.js @@ -133,8 +133,6 @@ TDengineCursor.prototype.execute = function execute(operation, options, callback else { this._fields = this._chandle.useResult(this._result); this.fields = this._fields; - console.log('++++++++++++++++++++++++++'); - console.log(this._result); wrapCB(callback); return this._result; //return a pointer to the result @@ -205,10 +203,12 @@ TDengineCursor.prototype.fetchall = function fetchall(options, callback) { break; } this._rowcount += num_of_rows; + let numoffields = this._fields.length; for (let i = 0; i < num_of_rows; i++) { data.push([]); - let rowBlock = new Array(this._fields.length); - for (let j = 0; j < this._fields.length; j++) { + + let rowBlock = new Array(numoffields); + for (let j = 0; j < numoffields; j++) { rowBlock[j] = block[j][i]; } data[data.length-1] = (rowBlock); @@ -268,7 +268,7 @@ TDengineCursor.prototype.execute_a = function execute_a (operation, options, cal let fieldCount = cr._chandle.numFields(res2); if (fieldCount == 0) { cr._chandle.freeResult(res2); - } + } else { return res2; } diff --git a/src/connector/nodejs/test/test.js b/src/connector/nodejs/test/test.js index b8a8bcd151..db86178da1 100644 --- a/src/connector/nodejs/test/test.js +++ b/src/connector/nodejs/test/test.js @@ -28,7 +28,7 @@ c1.execute('create table if not exists stabletest (ts timestamp, v1 int, v2 int, // Shell Test : The following uses the cursor to imitate the taos shell // Insert -for (let i = 0; i < 100; i++) { +for (let i = 0; i < 1000; i++) { let insertData = ["now+" + i + "s", // Timestamp parseInt( R(-Math.pow(2,31) + 1 , Math.pow(2,31) - 1) ), // Int parseInt( R(-Math.pow(2,31) + 1 , Math.pow(2,31) - 1) ), // BigInt @@ -40,18 +40,18 @@ for (let i = 0; i < 100; i++) { randomBool(), "\"Nchars\""]; // Bool c1.execute('insert into td_connector_test.all_types values(' + insertData.join(',') + ' );', {quiet:true}); - if (i % 10 == 0) { + if (i % 100 == 0) { console.log("Insert # " , i); } } // Select console.log('select * from td_connector_test.all_types limit 3 offset 100;'); -c1.execute('select * from td_connector_test.all_types limit 1 offset 100;'); +c1.execute('select * from td_connector_test.all_types limit 2 offset 100;'); var d = c1.fetchall(); console.log(c1.fields); console.log(d); -/* + // Functions console.log('select count(*), avg(_int), sum(_float), max(_bigint), min(_double) from td_connector_test.all_types;') c1.execute('select count(*), avg(_int), sum(_float), max(_bigint), min(_double) from td_connector_test.all_types;'); @@ -61,12 +61,14 @@ console.log(d); // Immediate Execution like the Shell -c1.query('select count(*), stddev(_double), min(_tinyint) from all_types where _tinyint > 50 and _int < 0;', true).then(function(result){ - result.pretty(); -}) +//c1.query('select count(*), stddev(_double), min(_tinyint) from all_types where _tinyint > 50 and _int < 0;', true).then(function(result){ +// result.pretty(); +//}) + c1.query('select _tinyint, _bool from all_types where _tinyint > 50 and _int < 0 limit 50;', true).then(function(result){ result.pretty(); }) + c1.query('select stddev(_double), stddev(_bigint), stddev(_float) from all_types;', true).then(function(result){ result.pretty(); }) @@ -136,4 +138,3 @@ setTimeout(function(){ c1.query('drop database td_connector_test;'); },2000); conn.close(); -*/ \ No newline at end of file -- GitLab