Hotfix/sangshuduo/td 3877 c sharp query result (#5866)
* [TD-3877]<fix>: C# query result determination mechanism changed. * [TD-3877]<fix>: C# query result determination mechanism changed. change taosdemo and C#checker. Co-authored-by: Shuduo Sang <sdsang@taosdata.com>
This commit is contained in:
parent
3281a5639c
commit
808019af4d
|
@ -20,358 +20,367 @@ using System.Runtime.InteropServices;
|
|||
using System.Collections;
|
||||
|
||||
namespace TDengineDriver
|
||||
{
|
||||
class TDengineTest
|
||||
{
|
||||
//connect parameters
|
||||
private string host;
|
||||
private string configDir;
|
||||
private string user;
|
||||
private string password;
|
||||
private short port = 0;
|
||||
|
||||
//sql parameters
|
||||
private string dbName;
|
||||
private string tbName;
|
||||
|
||||
|
||||
private bool isInsertData;
|
||||
private bool isQueryData;
|
||||
|
||||
private long tableCount;
|
||||
private long totalRows;
|
||||
private long batchRows;
|
||||
private long beginTimestamp = 1551369600000L;
|
||||
|
||||
private IntPtr conn = IntPtr.Zero;
|
||||
private long rowsInserted = 0;
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
TDengineTest tester = new TDengineTest();
|
||||
tester.ReadArgument(args);
|
||||
|
||||
|
||||
tester.InitTDengine();
|
||||
tester.ConnectTDengine();
|
||||
{
|
||||
class TDengineTest
|
||||
{
|
||||
//connect parameters
|
||||
private string host;
|
||||
private string configDir;
|
||||
private string user;
|
||||
private string password;
|
||||
private short port = 0;
|
||||
|
||||
//sql parameters
|
||||
private string dbName;
|
||||
private string tbName;
|
||||
|
||||
|
||||
private bool isInsertData;
|
||||
private bool isQueryData;
|
||||
|
||||
private long tableCount;
|
||||
private long totalRows;
|
||||
private long batchRows;
|
||||
private long beginTimestamp = 1551369600000L;
|
||||
|
||||
private IntPtr conn = IntPtr.Zero;
|
||||
private long rowsInserted = 0;
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
TDengineTest tester = new TDengineTest();
|
||||
tester.ReadArgument(args);
|
||||
|
||||
|
||||
tester.InitTDengine();
|
||||
tester.ConnectTDengine();
|
||||
tester.createDatabase();
|
||||
tester.useDatabase();
|
||||
tester.useDatabase();
|
||||
tester.checkDropTable();
|
||||
tester.createTable();
|
||||
tester.checkInsert();
|
||||
tester.checkSelect();
|
||||
tester.checkDropTable();
|
||||
|
||||
tester.CloseConnection();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public long GetArgumentAsLong(String[] argv, String argName, int minVal, int maxVal, int defaultValue)
|
||||
{
|
||||
int argc = argv.Length;
|
||||
for (int i = 0; i < argc; ++i)
|
||||
{
|
||||
if (argName != argv[i])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (i < argc - 1)
|
||||
{
|
||||
String tmp = argv[i + 1];
|
||||
if (tmp[0] == '-')
|
||||
{
|
||||
Console.WriteLine("option {0:G} requires an argument", tmp);
|
||||
ExitProgram();
|
||||
}
|
||||
|
||||
long tmpVal = Convert.ToInt64(tmp);
|
||||
if (tmpVal < minVal || tmpVal > maxVal)
|
||||
{
|
||||
Console.WriteLine("option {0:G} should in range [{1:G}, {2:G}]", argName, minVal, maxVal);
|
||||
ExitProgram();
|
||||
}
|
||||
|
||||
return tmpVal;
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public String GetArgumentAsString(String[] argv, String argName, String defaultValue)
|
||||
{
|
||||
int argc = argv.Length;
|
||||
for (int i = 0; i < argc; ++i)
|
||||
{
|
||||
if (argName != argv[i])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (i < argc - 1)
|
||||
{
|
||||
String tmp = argv[i + 1];
|
||||
if (tmp[0] == '-')
|
||||
{
|
||||
Console.WriteLine("option {0:G} requires an argument", tmp);
|
||||
ExitProgram();
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public void PrintHelp(String[] argv)
|
||||
{
|
||||
for (int i = 0; i < argv.Length; ++i)
|
||||
{
|
||||
if ("--help" == argv[i])
|
||||
{
|
||||
String indent = " ";
|
||||
Console.WriteLine("taosTest is simple example to operate TDengine use C# Language.\n");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-h");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "TDEngine server IP address to connect");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-u");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "The TDEngine user name to use when connecting to the server, default is root");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-p");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "The TDEngine user name to use when connecting to the server, default is taosdata");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-d");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Database used to create table or import data, default is db");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-s");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Super Tables used to create table, default is mt");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-t");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Table prefixs, default is t");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-w");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Whether to insert data");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-r");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Whether to query data");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-n");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "How many Tables to create, default is 10");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-b");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "How many rows per insert batch, default is 10");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-i");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "How many rows to insert, default is 100");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-c");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Configuration directory");
|
||||
|
||||
ExitProgram();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ReadArgument(String[] argv)
|
||||
{
|
||||
PrintHelp(argv);
|
||||
host = this.GetArgumentAsString(argv, "-h", "127.0.0.1");
|
||||
user = this.GetArgumentAsString(argv, "-u", "root");
|
||||
password = this.GetArgumentAsString(argv, "-p", "taosdata");
|
||||
dbName = this.GetArgumentAsString(argv, "-db", "test");
|
||||
tbName = this.GetArgumentAsString(argv, "-s", "weather");
|
||||
|
||||
isInsertData = this.GetArgumentAsLong(argv, "-w", 0, 1, 1) != 0;
|
||||
isQueryData = this.GetArgumentAsLong(argv, "-r", 0, 1, 1) != 0;
|
||||
tableCount = this.GetArgumentAsLong(argv, "-n", 1, 10000, 10);
|
||||
batchRows = this.GetArgumentAsLong(argv, "-b", 1, 1000, 500);
|
||||
totalRows = this.GetArgumentAsLong(argv, "-i", 1, 10000000, 10000);
|
||||
configDir = this.GetArgumentAsString(argv, "-c", "C:/TDengine/cfg");
|
||||
}
|
||||
|
||||
public void InitTDengine()
|
||||
{
|
||||
TDengine.Options((int)TDengineInitOption.TDDB_OPTION_CONFIGDIR, this.configDir);
|
||||
TDengine.Options((int)TDengineInitOption.TDDB_OPTION_SHELL_ACTIVITY_TIMER, "60");
|
||||
TDengine.Init();
|
||||
Console.WriteLine("get connection starting...");
|
||||
}
|
||||
|
||||
public void ConnectTDengine()
|
||||
{
|
||||
string db = "";
|
||||
this.conn = TDengine.Connect(this.host, this.user, this.password, db, this.port);
|
||||
if (this.conn == IntPtr.Zero)
|
||||
{
|
||||
Console.WriteLine("connection failed: " + this.host);
|
||||
ExitProgram();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("[ OK ] Connection established.");
|
||||
}
|
||||
}
|
||||
public void createDatabase()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("create database if not exists ").Append(this.dbName);
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void useDatabase()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("use ").Append(this.dbName);
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void checkSelect()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("select * from test.weather");
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void createTable()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("create table if not exists ").Append(this.dbName).Append(".").Append(this.tbName).Append("(ts timestamp, temperature float, humidity int)");
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void checkInsert()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("insert into test.weather (ts, temperature, humidity) values(now, 20.5, 34)");
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void checkDropTable()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("drop table if exists ").Append(this.dbName).Append(".").Append(this.tbName).Append("");
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void execute(string sql)
|
||||
{
|
||||
DateTime dt1 = DateTime.Now;
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
DateTime dt2 = DateTime.Now;
|
||||
TimeSpan span = dt2 - dt1;
|
||||
|
||||
if (res != IntPtr.Zero)
|
||||
{
|
||||
Console.WriteLine("[OK] time cost: " + span.ToString() + "ms, execute statement ====> " + sql.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
ExitProgram();
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
}
|
||||
|
||||
public void ExecuteQuery(string sql)
|
||||
{
|
||||
|
||||
DateTime dt1 = DateTime.Now;
|
||||
long queryRows = 0;
|
||||
IntPtr res = TDengine.Query(conn, sql);
|
||||
if (res == IntPtr.Zero)
|
||||
{
|
||||
Console.WriteLine(sql + " failure, reason: " + TDengine.Error(res));
|
||||
ExitProgram();
|
||||
}
|
||||
DateTime dt2 = DateTime.Now;
|
||||
TimeSpan span = dt2 - dt1;
|
||||
Console.WriteLine("[OK] time cost: " + span.ToString() + "ms, execute statement ====> " + sql.ToString());
|
||||
int fieldCount = TDengine.FieldCount(res);
|
||||
|
||||
List<TDengineMeta> metas = TDengine.FetchFields(res);
|
||||
for (int j = 0; j < metas.Count; j++)
|
||||
{
|
||||
TDengineMeta meta = (TDengineMeta)metas[j];
|
||||
}
|
||||
|
||||
IntPtr rowdata;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
while ((rowdata = TDengine.FetchRows(res)) != IntPtr.Zero)
|
||||
{
|
||||
queryRows++;
|
||||
for (int fields = 0; fields < fieldCount; ++fields)
|
||||
{
|
||||
TDengineMeta meta = metas[fields];
|
||||
int offset = IntPtr.Size * fields;
|
||||
IntPtr data = Marshal.ReadIntPtr(rowdata, offset);
|
||||
|
||||
builder.Append("---");
|
||||
|
||||
if (data == IntPtr.Zero)
|
||||
{
|
||||
builder.Append("NULL");
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ((TDengineDataType)meta.type)
|
||||
{
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BOOL:
|
||||
bool v1 = Marshal.ReadByte(data) == 0 ? false : true;
|
||||
builder.Append(v1);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TINYINT:
|
||||
byte v2 = Marshal.ReadByte(data);
|
||||
builder.Append(v2);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_SMALLINT:
|
||||
short v3 = Marshal.ReadInt16(data);
|
||||
builder.Append(v3);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_INT:
|
||||
int v4 = Marshal.ReadInt32(data);
|
||||
builder.Append(v4);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BIGINT:
|
||||
long v5 = Marshal.ReadInt64(data);
|
||||
builder.Append(v5);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_FLOAT:
|
||||
float v6 = (float)Marshal.PtrToStructure(data, typeof(float));
|
||||
builder.Append(v6);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_DOUBLE:
|
||||
double v7 = (double)Marshal.PtrToStructure(data, typeof(double));
|
||||
builder.Append(v7);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BINARY:
|
||||
string v8 = Marshal.PtrToStringAnsi(data);
|
||||
builder.Append(v8);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
long v9 = Marshal.ReadInt64(data);
|
||||
builder.Append(v9);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_NCHAR:
|
||||
string v10 = Marshal.PtrToStringAnsi(data);
|
||||
builder.Append(v10);
|
||||
break;
|
||||
}
|
||||
}
|
||||
builder.Append("---");
|
||||
|
||||
if (queryRows <= 10)
|
||||
{
|
||||
Console.WriteLine(builder.ToString());
|
||||
}
|
||||
builder.Clear();
|
||||
}
|
||||
|
||||
if (TDengine.ErrorNo(res) != 0)
|
||||
{
|
||||
Console.Write("Query is not complete, Error {0:G}", TDengine.ErrorNo(res), TDengine.Error(res));
|
||||
}
|
||||
|
||||
TDengine.FreeResult(res);
|
||||
|
||||
}
|
||||
|
||||
public void CloseConnection()
|
||||
{
|
||||
if (this.conn != IntPtr.Zero)
|
||||
{
|
||||
TDengine.Close(this.conn);
|
||||
Console.WriteLine("connection closed.");
|
||||
}
|
||||
}
|
||||
|
||||
static void ExitProgram()
|
||||
{
|
||||
TDengine.Cleanup();
|
||||
System.Environment.Exit(0);
|
||||
}
|
||||
tester.checkDropTable();
|
||||
|
||||
tester.CloseConnection();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public long GetArgumentAsLong(String[] argv, String argName, int minVal, int maxVal, int defaultValue)
|
||||
{
|
||||
int argc = argv.Length;
|
||||
for (int i = 0; i < argc; ++i)
|
||||
{
|
||||
if (argName != argv[i])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (i < argc - 1)
|
||||
{
|
||||
String tmp = argv[i + 1];
|
||||
if (tmp[0] == '-')
|
||||
{
|
||||
Console.WriteLine("option {0:G} requires an argument", tmp);
|
||||
ExitProgram();
|
||||
}
|
||||
|
||||
long tmpVal = Convert.ToInt64(tmp);
|
||||
if (tmpVal < minVal || tmpVal > maxVal)
|
||||
{
|
||||
Console.WriteLine("option {0:G} should in range [{1:G}, {2:G}]", argName, minVal, maxVal);
|
||||
ExitProgram();
|
||||
}
|
||||
|
||||
return tmpVal;
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public String GetArgumentAsString(String[] argv, String argName, String defaultValue)
|
||||
{
|
||||
int argc = argv.Length;
|
||||
for (int i = 0; i < argc; ++i)
|
||||
{
|
||||
if (argName != argv[i])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (i < argc - 1)
|
||||
{
|
||||
String tmp = argv[i + 1];
|
||||
if (tmp[0] == '-')
|
||||
{
|
||||
Console.WriteLine("option {0:G} requires an argument", tmp);
|
||||
ExitProgram();
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public void PrintHelp(String[] argv)
|
||||
{
|
||||
for (int i = 0; i < argv.Length; ++i)
|
||||
{
|
||||
if ("--help" == argv[i])
|
||||
{
|
||||
String indent = " ";
|
||||
Console.WriteLine("taosTest is simple example to operate TDengine use C# Language.\n");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-h");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "TDEngine server IP address to connect");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-u");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "The TDEngine user name to use when connecting to the server, default is root");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-p");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "The TDEngine user name to use when connecting to the server, default is taosdata");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-d");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Database used to create table or import data, default is db");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-s");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Super Tables used to create table, default is mt");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-t");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Table prefixs, default is t");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-w");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Whether to insert data");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-r");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Whether to query data");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-n");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "How many Tables to create, default is 10");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-b");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "How many rows per insert batch, default is 10");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-i");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "How many rows to insert, default is 100");
|
||||
Console.WriteLine("{0:G}{1:G}", indent, "-c");
|
||||
Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Configuration directory");
|
||||
|
||||
ExitProgram();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ReadArgument(String[] argv)
|
||||
{
|
||||
PrintHelp(argv);
|
||||
host = this.GetArgumentAsString(argv, "-h", "127.0.0.1");
|
||||
user = this.GetArgumentAsString(argv, "-u", "root");
|
||||
password = this.GetArgumentAsString(argv, "-p", "taosdata");
|
||||
dbName = this.GetArgumentAsString(argv, "-db", "test");
|
||||
tbName = this.GetArgumentAsString(argv, "-s", "weather");
|
||||
|
||||
isInsertData = this.GetArgumentAsLong(argv, "-w", 0, 1, 1) != 0;
|
||||
isQueryData = this.GetArgumentAsLong(argv, "-r", 0, 1, 1) != 0;
|
||||
tableCount = this.GetArgumentAsLong(argv, "-n", 1, 10000, 10);
|
||||
batchRows = this.GetArgumentAsLong(argv, "-b", 1, 1000, 500);
|
||||
totalRows = this.GetArgumentAsLong(argv, "-i", 1, 10000000, 10000);
|
||||
configDir = this.GetArgumentAsString(argv, "-c", "C:/TDengine/cfg");
|
||||
}
|
||||
|
||||
public void InitTDengine()
|
||||
{
|
||||
TDengine.Options((int)TDengineInitOption.TDDB_OPTION_CONFIGDIR, this.configDir);
|
||||
TDengine.Options((int)TDengineInitOption.TDDB_OPTION_SHELL_ACTIVITY_TIMER, "60");
|
||||
TDengine.Init();
|
||||
Console.WriteLine("get connection starting...");
|
||||
}
|
||||
|
||||
public void ConnectTDengine()
|
||||
{
|
||||
string db = "";
|
||||
this.conn = TDengine.Connect(this.host, this.user, this.password, db, this.port);
|
||||
if (this.conn == IntPtr.Zero)
|
||||
{
|
||||
Console.WriteLine("connection failed: " + this.host);
|
||||
ExitProgram();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("[ OK ] Connection established.");
|
||||
}
|
||||
}
|
||||
public void createDatabase()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("create database if not exists ").Append(this.dbName);
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void useDatabase()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("use ").Append(this.dbName);
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void checkSelect()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("select * from test.weather");
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void createTable()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("create table if not exists ").Append(this.dbName).Append(".").Append(this.tbName).Append("(ts timestamp, temperature float, humidity int)");
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void checkInsert()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("insert into test.weather (ts, temperature, humidity) values(now, 20.5, 34)");
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void checkDropTable()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("drop table if exists ").Append(this.dbName).Append(".").Append(this.tbName).Append("");
|
||||
execute(sql.ToString());
|
||||
}
|
||||
public void execute(string sql)
|
||||
{
|
||||
DateTime dt1 = DateTime.Now;
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
DateTime dt2 = DateTime.Now;
|
||||
TimeSpan span = dt2 - dt1;
|
||||
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
ExitProgram();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " success");
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
}
|
||||
|
||||
public void ExecuteQuery(string sql)
|
||||
{
|
||||
|
||||
DateTime dt1 = DateTime.Now;
|
||||
long queryRows = 0;
|
||||
IntPtr res = TDengine.Query(conn, sql);
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
ExitProgram();
|
||||
}
|
||||
DateTime dt2 = DateTime.Now;
|
||||
TimeSpan span = dt2 - dt1;
|
||||
Console.WriteLine("[OK] time cost: " + span.ToString() + "ms, execute statement ====> " + sql.ToString());
|
||||
int fieldCount = TDengine.FieldCount(res);
|
||||
|
||||
List<TDengineMeta> metas = TDengine.FetchFields(res);
|
||||
for (int j = 0; j < metas.Count; j++)
|
||||
{
|
||||
TDengineMeta meta = (TDengineMeta)metas[j];
|
||||
}
|
||||
|
||||
IntPtr rowdata;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
while ((rowdata = TDengine.FetchRows(res)) != IntPtr.Zero)
|
||||
{
|
||||
queryRows++;
|
||||
for (int fields = 0; fields < fieldCount; ++fields)
|
||||
{
|
||||
TDengineMeta meta = metas[fields];
|
||||
int offset = IntPtr.Size * fields;
|
||||
IntPtr data = Marshal.ReadIntPtr(rowdata, offset);
|
||||
|
||||
builder.Append("---");
|
||||
|
||||
if (data == IntPtr.Zero)
|
||||
{
|
||||
builder.Append("NULL");
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ((TDengineDataType)meta.type)
|
||||
{
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BOOL:
|
||||
bool v1 = Marshal.ReadByte(data) == 0 ? false : true;
|
||||
builder.Append(v1);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TINYINT:
|
||||
byte v2 = Marshal.ReadByte(data);
|
||||
builder.Append(v2);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_SMALLINT:
|
||||
short v3 = Marshal.ReadInt16(data);
|
||||
builder.Append(v3);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_INT:
|
||||
int v4 = Marshal.ReadInt32(data);
|
||||
builder.Append(v4);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BIGINT:
|
||||
long v5 = Marshal.ReadInt64(data);
|
||||
builder.Append(v5);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_FLOAT:
|
||||
float v6 = (float)Marshal.PtrToStructure(data, typeof(float));
|
||||
builder.Append(v6);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_DOUBLE:
|
||||
double v7 = (double)Marshal.PtrToStructure(data, typeof(double));
|
||||
builder.Append(v7);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BINARY:
|
||||
string v8 = Marshal.PtrToStringAnsi(data);
|
||||
builder.Append(v8);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
long v9 = Marshal.ReadInt64(data);
|
||||
builder.Append(v9);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_NCHAR:
|
||||
string v10 = Marshal.PtrToStringAnsi(data);
|
||||
builder.Append(v10);
|
||||
break;
|
||||
}
|
||||
}
|
||||
builder.Append("---");
|
||||
|
||||
if (queryRows <= 10)
|
||||
{
|
||||
Console.WriteLine(builder.ToString());
|
||||
}
|
||||
builder.Clear();
|
||||
}
|
||||
|
||||
if (TDengine.ErrorNo(res) != 0)
|
||||
{
|
||||
Console.Write("Query is not complete, Error {0:G}", TDengine.ErrorNo(res), TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
|
||||
TDengine.FreeResult(res);
|
||||
|
||||
}
|
||||
|
||||
public void CloseConnection()
|
||||
{
|
||||
if (this.conn != IntPtr.Zero)
|
||||
{
|
||||
TDengine.Close(this.conn);
|
||||
Console.WriteLine("connection closed.");
|
||||
}
|
||||
}
|
||||
|
||||
static void ExitProgram()
|
||||
{
|
||||
TDengine.Cleanup();
|
||||
System.Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -165,7 +165,7 @@ namespace TDengineDriver
|
|||
public void ReadArgument(String[] argv)
|
||||
{
|
||||
PrintHelp(argv);
|
||||
host = this.GetArgumentAsString(argv, "-h", "192.168.100.128");
|
||||
host = this.GetArgumentAsString(argv, "-h", "127.0.0.1");
|
||||
user = this.GetArgumentAsString(argv, "-u", "root");
|
||||
password = this.GetArgumentAsString(argv, "-p", "taosdata");
|
||||
dbName = this.GetArgumentAsString(argv, "-d", "db");
|
||||
|
@ -212,42 +212,54 @@ namespace TDengineDriver
|
|||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("create database if not exists ").Append(this.dbName);
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " success");
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
ExitProgram();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
ExitProgram();
|
||||
Console.WriteLine(sql.ToString() + " success");
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
|
||||
sql.Clear();
|
||||
sql.Append("use ").Append(this.dbName);
|
||||
res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " success");
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
ExitProgram();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
ExitProgram();
|
||||
Console.WriteLine(sql.ToString() + " success");
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
|
||||
sql.Clear();
|
||||
sql.Append("create table if not exists ").Append(this.stableName).Append("(ts timestamp, v1 bool, v2 tinyint, v3 smallint, v4 int, v5 bigint, v6 float, v7 double, v8 binary(10), v9 nchar(10)) tags(t1 int)");
|
||||
res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " success");
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
ExitProgram();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
ExitProgram();
|
||||
Console.WriteLine(sql.ToString() + " success");
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
|
||||
|
@ -257,14 +269,18 @@ namespace TDengineDriver
|
|||
sql = sql.Append("create table if not exists ").Append(this.tablePrefix).Append(i)
|
||||
.Append(" using ").Append(this.stableName).Append(" tags(").Append(i).Append(")");
|
||||
res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " success");
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
ExitProgram();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
ExitProgram();
|
||||
Console.WriteLine(sql.ToString() + " success");
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
}
|
||||
|
@ -298,9 +314,13 @@ namespace TDengineDriver
|
|||
.Append(", 5, 6, 7, 'abc', 'def')");
|
||||
}
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res == IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
}
|
||||
|
||||
int affectRows = TDengine.AffectRows(res);
|
||||
|
@ -326,16 +346,20 @@ namespace TDengineDriver
|
|||
|
||||
System.DateTime start = new System.DateTime();
|
||||
long queryRows = 0;
|
||||
|
||||
|
||||
for (int i = 0; i < 1/*this.tableCount*/; ++i)
|
||||
{
|
||||
String sql = "select * from " + this.dbName + "." + tablePrefix + i;
|
||||
Console.WriteLine(sql);
|
||||
|
||||
IntPtr res = TDengine.Query(conn, sql);
|
||||
if (res == IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
Console.WriteLine(sql + " failure, reason: " + TDengine.Error(res));
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
ExitProgram();
|
||||
}
|
||||
|
||||
|
@ -423,8 +447,10 @@ namespace TDengineDriver
|
|||
|
||||
if (TDengine.ErrorNo(res) != 0)
|
||||
{
|
||||
Console.Write("Query is not complete, Error {0:G}", TDengine.ErrorNo(res), TDengine.Error(res));
|
||||
Console.Write("Query is not complete, Error {0:G}",
|
||||
TDengine.ErrorNo(res), TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
|
||||
TDengine.FreeResult(res);
|
||||
}
|
||||
|
|
|
@ -370,16 +370,19 @@ namespace TDengineDriver
|
|||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("DROP DATABASE IF EXISTS ").Append(this.dbName);
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
VerbosePrint(sql.ToString() + " success\n");
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
CleanAndExitProgram(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
CleanAndExitProgram(1);
|
||||
VerbosePrint(sql.ToString() + " success\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void CreateDb()
|
||||
|
@ -387,14 +390,18 @@ namespace TDengineDriver
|
|||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("CREATE DATABASE IF NOT EXISTS ").Append(this.dbName).Append(" replica ").Append(this.replica);
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
VerbosePrint(sql.ToString() + " success\n");
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
CleanAndExitProgram(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
CleanAndExitProgram(1);
|
||||
VerbosePrint(sql.ToString() + " success\n");
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
}
|
||||
|
@ -408,14 +415,18 @@ namespace TDengineDriver
|
|||
Append(this.dbName).Append(".").Append(this.stablePrefix).
|
||||
Append("(ts timestamp, v1 bool, v2 tinyint, v3 smallint, v4 int, v5 bigint, v6 float, v7 double, v8 binary(10), v9 nchar(10)) tags(t1 int)");
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
VerbosePrint(sql.ToString() + " success\n");
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
CleanAndExitProgram(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
CleanAndExitProgram(1);
|
||||
VerbosePrint(sql.ToString() + " success\n");
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
}
|
||||
|
@ -495,9 +506,13 @@ namespace TDengineDriver
|
|||
|
||||
IntPtr res = TDengine.Query(conn, sql);
|
||||
DebugPrintFormat("res: {0}\n", res);
|
||||
if (res == IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
Console.WriteLine(sql + " failure, reason: " + TDengine.Error(res));
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
CleanAndExitProgram(1);
|
||||
}
|
||||
|
||||
|
@ -761,9 +776,13 @@ namespace TDengineDriver
|
|||
|
||||
}
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res == IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
VerbosePrint(sql.ToString() + " failure, reason: " + TDengine.Error(res) + "\n");
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
}
|
||||
|
||||
inserted += this.batchRows;
|
||||
|
@ -840,14 +859,18 @@ namespace TDengineDriver
|
|||
sql = sql.Append("(ts timestamp, v1 bool, v2 tinyint, v3 smallint, v4 int, v5 bigint, v6 float, v7 double, v8 binary(10), v9 nchar(10))");
|
||||
}
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
|
||||
{
|
||||
VerbosePrint(sql.ToString() + " success\n");
|
||||
Console.Write(sql.ToString() + " failure, ");
|
||||
if (res != IntPtr.Zero) {
|
||||
Console.Write("reason: " + TDengine.Error(res));
|
||||
}
|
||||
Console.WriteLine("");
|
||||
CleanAndExitProgram(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
VerbosePrint(sql.ToString() + " failure, reason: " + TDengine.Error(res) + "\n");
|
||||
CleanAndExitProgram(1);
|
||||
VerbosePrint(sql.ToString() + " success\n");
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue