提交 8db31201 编写于 作者: S Shengliang Guan

TD-1236 native c# examples for 2.0

上级 00aaa44c
...@@ -93,7 +93,7 @@ DLL_EXPORT int taos_result_precision(TAOS_RES *res); // get the time precision ...@@ -93,7 +93,7 @@ DLL_EXPORT int taos_result_precision(TAOS_RES *res); // get the time precision
DLL_EXPORT void taos_free_result(TAOS_RES *res); DLL_EXPORT void taos_free_result(TAOS_RES *res);
DLL_EXPORT int taos_field_count(TAOS_RES *tres); DLL_EXPORT int taos_field_count(TAOS_RES *tres);
DLL_EXPORT int taos_num_fields(TAOS_RES *res); DLL_EXPORT int taos_num_fields(TAOS_RES *res);
DLL_EXPORT int taos_affected_rows(TAOS_RES *taos); DLL_EXPORT int taos_affected_rows(TAOS_RES *res);
DLL_EXPORT TAOS_FIELD *taos_fetch_fields(TAOS_RES *res); DLL_EXPORT TAOS_FIELD *taos_fetch_fields(TAOS_RES *res);
DLL_EXPORT int taos_select_db(TAOS *taos, const char *db); DLL_EXPORT int taos_select_db(TAOS *taos, const char *db);
DLL_EXPORT int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields); DLL_EXPORT int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields);
......
...@@ -20,16 +20,17 @@ using System.Runtime.InteropServices; ...@@ -20,16 +20,17 @@ using System.Runtime.InteropServices;
namespace TDengineDriver namespace TDengineDriver
{ {
enum TDengineDataType { enum TDengineDataType {
TSDB_DATA_TYPE_BOOL = 1, TSDB_DATA_TYPE_NULL = 0, // 1 bytes
TSDB_DATA_TYPE_TINYINT = 2, TSDB_DATA_TYPE_BOOL = 1, // 1 bytes
TSDB_DATA_TYPE_SMALLINT = 3, TSDB_DATA_TYPE_TINYINT = 2, // 1 bytes
TSDB_DATA_TYPE_INT = 4, TSDB_DATA_TYPE_SMALLINT = 3, // 2 bytes
TSDB_DATA_TYPE_BIGINT = 5, TSDB_DATA_TYPE_INT = 4, // 4 bytes
TSDB_DATA_TYPE_FLOAT = 6, TSDB_DATA_TYPE_BIGINT = 5, // 8 bytes
TSDB_DATA_TYPE_DOUBLE = 7, TSDB_DATA_TYPE_FLOAT = 6, // 4 bytes
TSDB_DATA_TYPE_BINARY = 8, TSDB_DATA_TYPE_DOUBLE = 7, // 8 bytes
TSDB_DATA_TYPE_TIMESTAMP = 9, TSDB_DATA_TYPE_BINARY = 8, // string
TSDB_DATA_TYPE_NCHAR = 10 TSDB_DATA_TYPE_TIMESTAMP = 9,// 8 bytes
TSDB_DATA_TYPE_NCHAR = 10 // unicode string
} }
enum TDengineInitOption enum TDengineInitOption
...@@ -83,50 +84,49 @@ namespace TDengineDriver ...@@ -83,50 +84,49 @@ namespace TDengineDriver
[DllImport("taos.dll", EntryPoint = "taos_init", CallingConvention = CallingConvention.StdCall)] [DllImport("taos.dll", EntryPoint = "taos_init", CallingConvention = CallingConvention.StdCall)]
static extern public void Init(); static extern public void Init();
[DllImport("taos.dll", EntryPoint = "taos_cleanup", CallingConvention = CallingConvention.StdCall)]
static extern public void Cleanup();
[DllImport("taos.dll", EntryPoint = "taos_options", CallingConvention = CallingConvention.StdCall)] [DllImport("taos.dll", EntryPoint = "taos_options", CallingConvention = CallingConvention.StdCall)]
static extern public void Options(int option, string value); static extern public void Options(int option, string value);
[DllImport("taos.dll", EntryPoint = "taos_connect", CallingConvention = CallingConvention.StdCall)] [DllImport("taos.dll", EntryPoint = "taos_connect", CallingConvention = CallingConvention.StdCall)]
static extern public long Connect(string ip, string user, string password, string db, int port); static extern public long Connect(string ip, string user, string password, string db, short port);
[DllImport("taos.dll", EntryPoint = "taos_errstr", CallingConvention = CallingConvention.StdCall)] [DllImport("taos.dll", EntryPoint = "taos_errstr", CallingConvention = CallingConvention.StdCall)]
static extern private IntPtr taos_errstr(long taos); static extern private IntPtr taos_errstr(long res);
static public string Error(long conn) static public string Error(long res)
{ {
IntPtr errPtr = taos_errstr(conn); IntPtr errPtr = taos_errstr(res);
return Marshal.PtrToStringAnsi(errPtr); return Marshal.PtrToStringAnsi(errPtr);
} }
[DllImport("taos.dll", EntryPoint = "taos_errno", CallingConvention = CallingConvention.StdCall)] [DllImport("taos.dll", EntryPoint = "taos_errno", CallingConvention = CallingConvention.StdCall)]
static extern public int ErrorNo(long taos); static extern public int ErrorNo(long res);
[DllImport("taos.dll", EntryPoint = "taos_query", CallingConvention = CallingConvention.StdCall)] [DllImport("taos.dll", EntryPoint = "taos_query", CallingConvention = CallingConvention.StdCall)]
static extern public int Query(long taos, string sqlstr); static extern public long Query(long conn, string sqlstr);
[DllImport("taos.dll", EntryPoint = "taos_affected_rows", CallingConvention = CallingConvention.StdCall)] [DllImport("taos.dll", EntryPoint = "taos_affected_rows", CallingConvention = CallingConvention.StdCall)]
static extern public int AffectRows(long taos); static extern public int AffectRows(long res);
[DllImport("taos.dll", EntryPoint = "taos_use_result", CallingConvention = CallingConvention.StdCall)]
static extern public long UseResult(long taos);
[DllImport("taos.dll", EntryPoint = "taos_field_count", CallingConvention = CallingConvention.StdCall)] [DllImport("taos.dll", EntryPoint = "taos_field_count", CallingConvention = CallingConvention.StdCall)]
static extern public int FieldCount(long taos); static extern public int FieldCount(long res);
[DllImport("taos.dll", EntryPoint = "taos_fetch_fields", CallingConvention = CallingConvention.StdCall)] [DllImport("taos.dll", EntryPoint = "taos_fetch_fields", CallingConvention = CallingConvention.StdCall)]
static extern private IntPtr taos_fetch_fields(long res); static extern private IntPtr taos_fetch_fields(long res);
static public List<TDengineMeta> FetchFields(long taos) static public List<TDengineMeta> FetchFields(long res)
{ {
const int fieldSize = 68; const int fieldSize = 68;
List<TDengineMeta> metas = new List<TDengineMeta>(); List<TDengineMeta> metas = new List<TDengineMeta>();
long result = TDengine.UseResult(taos); if (res == 0)
if (result == 0)
{ {
return metas; return metas;
} }
int fieldCount = FieldCount(taos); int fieldCount = FieldCount(res);
IntPtr fieldsPtr = taos_fetch_fields(result); IntPtr fieldsPtr = taos_fetch_fields(res);
for (int i = 0; i < fieldCount; ++i) for (int i = 0; i < fieldCount; ++i)
{ {
...@@ -134,8 +134,8 @@ namespace TDengineDriver ...@@ -134,8 +134,8 @@ namespace TDengineDriver
TDengineMeta meta = new TDengineMeta(); TDengineMeta meta = new TDengineMeta();
meta.name = Marshal.PtrToStringAnsi(fieldsPtr + offset); meta.name = Marshal.PtrToStringAnsi(fieldsPtr + offset);
meta.size = Marshal.ReadInt16(fieldsPtr + offset + 64); meta.type = Marshal.ReadByte(fieldsPtr + offset + 65);
meta.type = Marshal.ReadByte(fieldsPtr + offset + 66); meta.size = Marshal.ReadInt16(fieldsPtr + offset + 66);
metas.Add(meta); metas.Add(meta);
} }
......
...@@ -28,7 +28,7 @@ namespace TDengineDriver ...@@ -28,7 +28,7 @@ namespace TDengineDriver
private string configDir; private string configDir;
private string user; private string user;
private string password; private string password;
private int port = 0; private short port = 0;
//sql parameters //sql parameters
private string dbName; private string dbName;
...@@ -211,58 +211,62 @@ namespace TDengineDriver ...@@ -211,58 +211,62 @@ namespace TDengineDriver
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.Append("create database if not exists ").Append(this.dbName); sql.Append("create database if not exists ").Append(this.dbName);
int code = TDengine.Query(this.conn, sql.ToString()); long res = TDengine.Query(this.conn, sql.ToString());
if (code == TDengine.TSDB_CODE_SUCCESS) if (res != 0)
{ {
Console.WriteLine(sql.ToString() + " success"); Console.WriteLine(sql.ToString() + " success");
} }
else else
{ {
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(conn)); Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
ExitProgram(); ExitProgram();
} }
TDengine.FreeResult(res);
sql.Clear(); sql.Clear();
sql.Append("use ").Append(this.dbName); sql.Append("use ").Append(this.dbName);
code = TDengine.Query(this.conn, sql.ToString()); res = TDengine.Query(this.conn, sql.ToString());
if (code == TDengine.TSDB_CODE_SUCCESS) if (res != 0)
{ {
Console.WriteLine(sql.ToString() + " success"); Console.WriteLine(sql.ToString() + " success");
} }
else else
{ {
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(this.conn)); Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
ExitProgram(); ExitProgram();
} }
TDengine.FreeResult(res);
sql.Clear(); sql.Clear();
sql.Append("create table if not exists ").Append(this.stableName).Append("(ts timestamp, v1 int) tags(t1 int)"); 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)");
code = TDengine.Query(this.conn, sql.ToString()); res = TDengine.Query(this.conn, sql.ToString());
if (code == TDengine.TSDB_CODE_SUCCESS) if (res != 0)
{ {
Console.WriteLine(sql.ToString() + " success"); Console.WriteLine(sql.ToString() + " success");
} }
else else
{ {
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(this.conn)); Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
ExitProgram(); ExitProgram();
} }
TDengine.FreeResult(res);
for (int i = 0; i < this.tableCount; i++) for (int i = 0; i < this.tableCount; i++)
{ {
sql.Clear(); sql.Clear();
sql = sql.Append("create table if not exists ").Append(this.tablePrefix).Append(i) sql = sql.Append("create table if not exists ").Append(this.tablePrefix).Append(i)
.Append(" using ").Append(this.stableName).Append(" tags(").Append(i).Append(")"); .Append(" using ").Append(this.stableName).Append(" tags(").Append(i).Append(")");
code = TDengine.Query(this.conn, sql.ToString()); res = TDengine.Query(this.conn, sql.ToString());
if (code == TDengine.TSDB_CODE_SUCCESS) if (res != 0)
{ {
Console.WriteLine(sql.ToString() + " success"); Console.WriteLine(sql.ToString() + " success");
} }
else else
{ {
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(this.conn)); Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
ExitProgram(); ExitProgram();
} }
TDengine.FreeResult(res);
} }
Console.WriteLine("create db and table success"); Console.WriteLine("create db and table success");
...@@ -287,16 +291,22 @@ namespace TDengineDriver ...@@ -287,16 +291,22 @@ namespace TDengineDriver
for (int batch = 0; batch < this.batchRows; ++batch) for (int batch = 0; batch < this.batchRows; ++batch)
{ {
long rows = loop * this.batchRows + batch; long rows = loop * this.batchRows + batch;
sql.Append("(").Append(this.beginTimestamp + rows).Append(",").Append(rows).Append(")"); sql.Append("(")
.Append(this.beginTimestamp + rows)
.Append(", 1, 2, 3,")
.Append(rows)
.Append(", 5, 6, 7, 'abc', 'def')");
} }
int code = TDengine.Query(conn, sql.ToString()); long res = TDengine.Query(conn, sql.ToString());
if (code != TDengine.TSDB_CODE_SUCCESS) if (res == 0)
{ {
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(conn)); Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
} }
int affectRows = TDengine.AffectRows(conn); int affectRows = TDengine.AffectRows(res);
this.rowsInserted += affectRows; this.rowsInserted += affectRows;
TDengine.FreeResult(res);
} }
} }
...@@ -322,32 +332,25 @@ namespace TDengineDriver ...@@ -322,32 +332,25 @@ namespace TDengineDriver
String sql = "select * from " + this.dbName + "." + tablePrefix + i; String sql = "select * from " + this.dbName + "." + tablePrefix + i;
Console.WriteLine(sql); Console.WriteLine(sql);
int code = TDengine.Query(conn, sql); long res = TDengine.Query(conn, sql);
if (code != TDengine.TSDB_CODE_SUCCESS) if (res == 0)
{ {
Console.WriteLine(sql + " failure, reason: " + TDengine.Error(conn)); Console.WriteLine(sql + " failure, reason: " + TDengine.Error(res));
ExitProgram(); ExitProgram();
} }
int fieldCount = TDengine.FieldCount(conn); int fieldCount = TDengine.FieldCount(res);
//Console.WriteLine("field count: " + fieldCount); Console.WriteLine("field count: " + fieldCount);
List<TDengineMeta> metas = TDengine.FetchFields(conn); List<TDengineMeta> metas = TDengine.FetchFields(res);
for (int j = 0; j < metas.Count; j++) for (int j = 0; j < metas.Count; j++)
{ {
TDengineMeta meta = (TDengineMeta)metas[j]; TDengineMeta meta = (TDengineMeta)metas[j];
//Console.WriteLine("index:" + j + ", type:" + meta.type + ", typename:" + meta.TypeName() + ", name:" + meta.name + ", size:" + meta.size); Console.WriteLine("index:" + j + ", type:" + meta.type + ", typename:" + meta.TypeName() + ", name:" + meta.name + ", size:" + meta.size);
}
long result = TDengine.UseResult(conn);
if (result == 0)
{
Console.WriteLine(sql + " result set is null");
return;
} }
IntPtr rowdata; IntPtr rowdata;
while ((rowdata = TDengine.FetchRows(result)) != IntPtr.Zero) while ((rowdata = TDengine.FetchRows(res)) != IntPtr.Zero)
{ {
queryRows++; queryRows++;
for (int fields = 0; fields < fieldCount; ++fields) for (int fields = 0; fields < fieldCount; ++fields)
...@@ -411,12 +414,12 @@ namespace TDengineDriver ...@@ -411,12 +414,12 @@ namespace TDengineDriver
//Console.WriteLine("---"); //Console.WriteLine("---");
} }
if (TDengine.ErrorNo(conn) != 0) if (TDengine.ErrorNo(res) != 0)
{ {
Console.Write("Query is not complete, Error {0:G}", TDengine.ErrorNo(conn), TDengine.Error(conn)); Console.Write("Query is not complete, Error {0:G}", TDengine.ErrorNo(res), TDengine.Error(res));
} }
TDengine.FreeResult(result); TDengine.FreeResult(res);
} }
System.DateTime end = new System.DateTime(); System.DateTime end = new System.DateTime();
...@@ -436,6 +439,7 @@ namespace TDengineDriver ...@@ -436,6 +439,7 @@ namespace TDengineDriver
static void ExitProgram() static void ExitProgram()
{ {
TDengine.Cleanup();
System.Environment.Exit(0); System.Environment.Exit(0);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册