nodejsChecker.js 1.7 KB
Newer Older
sangshuduo's avatar
sangshuduo 已提交
1 2
//const taos = require('td2.0-connector');
const taos = require('../../../src/connector/nodejs/');
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46


var host = null;
var port = 6030;
for(var i = 2; i < global.process.argv.length; i++){
	var key = global.process.argv[i].split("=")[0];
	var value = global.process.argv[i].split("=")[1];
	
	if("host" == key){
		host = value;
	}
	if("port" == key){
		port = value;
	}
}

if(host == null){
	console.log("Usage: node nodejsChecker.js host=<hostname> port=<port>");
	process.exit(0);
}

// establish connection
var conn = taos.connect({host:host, user:"root", password:"taosdata",port:port});
var cursor = conn.cursor(); 
// create database
executeSql("create database if not exists test", 0);
// use db
executeSql("use test", 0);
// drop table
executeSql("drop table if exists test.weather", 0);
// create table
executeSql("create table if not exists test.weather(ts timestamp, temperature float, humidity int)", 0);
// insert
executeSql("insert into test.weather (ts, temperature, humidity) values(now, 20.5, 34)", 1);
// select
executeQuery("select * from test.weather");
// close connection
conn.close();

function executeQuery(sql){
	var start = new Date().getTime();
	var promise = cursor.query(sql, true);
	var end = new Date().getTime();
	promise.then(function(result){
47
		printSql(sql, result != null,(end - start));
48 49 50 51 52 53 54 55 56 57 58 59 60 61
		result.pretty();
	});
}

function executeSql(sql, affectRows){
	var start = new Date().getTime();
	var promise = cursor.execute(sql);
	var end = new Date().getTime();
	printSql(sql, promise == affectRows, (end - start));
}

function printSql(sql, succeed, cost){
	console.log("[ "+(succeed ? "OK" : "ERROR!")+" ] time cost: " + cost + " ms, execute statement ====> " + sql);
}