From 7a05996a8cf7aa182c8ebe3a41c55e2cc74b61af Mon Sep 17 00:00:00 2001 From: xiaolei li <85657333+xleili@users.noreply.github.com> Date: Thu, 9 Dec 2021 12:37:35 +0800 Subject: [PATCH] [TD-10522]:csharp support stmt (#8924) * [TD-10522]:csharp support stmt * submit twice * add fetchfeild test case * fix singlebind error in linux * delete extra writeline --- src/connector/C#/TDengineDriver.cs | 174 ---- src/connector/C#/csharpTaos.sln | 72 ++ .../C#/src/TDengineDriver/TDengineDriver.cs | 399 ++++++++ .../src/TDengineDriver/TDengineDriver.csproj | 7 + .../C#/src/TDengineDriver/TaosBind.cs | 332 +++++++ .../C#/src/TDengineDriver/TaosMultiBind.cs | 616 +++++++++++++ src/connector/C#/src/test/Cases/Cases.csproj | 12 + src/connector/C#/src/test/Cases/DataSource.cs | 103 +++ src/connector/C#/src/test/Cases/Program.cs | 59 ++ .../C#/src/test/Cases/StmtNormalTable.cs | 89 ++ src/connector/C#/src/test/Cases/StmtQuery.cs | 49 + src/connector/C#/src/test/Cases/StmtStable.cs | 76 ++ src/connector/C#/src/test/Cases/StmtUtil.cs | 198 ++++ src/connector/C#/src/test/Cases/TaosFeild.cs | 37 + src/connector/C#/src/test/Cases/Utils.cs | 166 ++++ .../C#/src/test/XUnitTest/TestTDengineMeta.cs | 177 ++++ .../C#/src/test/XUnitTest/TestTaosBind.cs | 863 ++++++++++++++++++ .../C#/src/test/XUnitTest/XUnitTest.csproj | 30 + tests/examples/C#/stmt/StmtDemo.cs | 549 +++++++++++ tests/examples/C#/stmt/stmt.csproj | 12 + 20 files changed, 3846 insertions(+), 174 deletions(-) delete mode 100644 src/connector/C#/TDengineDriver.cs create mode 100644 src/connector/C#/csharpTaos.sln create mode 100644 src/connector/C#/src/TDengineDriver/TDengineDriver.cs create mode 100644 src/connector/C#/src/TDengineDriver/TDengineDriver.csproj create mode 100644 src/connector/C#/src/TDengineDriver/TaosBind.cs create mode 100644 src/connector/C#/src/TDengineDriver/TaosMultiBind.cs create mode 100644 src/connector/C#/src/test/Cases/Cases.csproj create mode 100644 src/connector/C#/src/test/Cases/DataSource.cs create mode 100644 src/connector/C#/src/test/Cases/Program.cs create mode 100644 src/connector/C#/src/test/Cases/StmtNormalTable.cs create mode 100644 src/connector/C#/src/test/Cases/StmtQuery.cs create mode 100644 src/connector/C#/src/test/Cases/StmtStable.cs create mode 100644 src/connector/C#/src/test/Cases/StmtUtil.cs create mode 100644 src/connector/C#/src/test/Cases/TaosFeild.cs create mode 100644 src/connector/C#/src/test/Cases/Utils.cs create mode 100644 src/connector/C#/src/test/XUnitTest/TestTDengineMeta.cs create mode 100644 src/connector/C#/src/test/XUnitTest/TestTaosBind.cs create mode 100644 src/connector/C#/src/test/XUnitTest/XUnitTest.csproj create mode 100644 tests/examples/C#/stmt/StmtDemo.cs create mode 100644 tests/examples/C#/stmt/stmt.csproj diff --git a/src/connector/C#/TDengineDriver.cs b/src/connector/C#/TDengineDriver.cs deleted file mode 100644 index 14fb240d0c..0000000000 --- a/src/connector/C#/TDengineDriver.cs +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -using System; -using System.Collections.Generic; -using System.Runtime.InteropServices; - -namespace TDengineDriver -{ - public enum TDengineDataType - { - TSDB_DATA_TYPE_NULL = 0, // 1 bytes - TSDB_DATA_TYPE_BOOL = 1, // 1 bytes - TSDB_DATA_TYPE_TINYINT = 2, // 1 bytes - TSDB_DATA_TYPE_SMALLINT = 3, // 2 bytes - TSDB_DATA_TYPE_INT = 4, // 4 bytes - TSDB_DATA_TYPE_BIGINT = 5, // 8 bytes - TSDB_DATA_TYPE_FLOAT = 6, // 4 bytes - TSDB_DATA_TYPE_DOUBLE = 7, // 8 bytes - TSDB_DATA_TYPE_BINARY = 8, // string - TSDB_DATA_TYPE_TIMESTAMP = 9,// 8 bytes - TSDB_DATA_TYPE_NCHAR = 10, // unicode string - TSDB_DATA_TYPE_UTINYINT = 11,// 1 byte - TSDB_DATA_TYPE_USMALLINT = 12,// 2 bytes - TSDB_DATA_TYPE_UINT = 13, // 4 bytes - TSDB_DATA_TYPE_UBIGINT = 14 // 8 bytes - } - - public enum TDengineInitOption - { - TSDB_OPTION_LOCALE = 0, - TSDB_OPTION_CHARSET = 1, - TSDB_OPTION_TIMEZONE = 2, - TDDB_OPTION_CONFIGDIR = 3, - TDDB_OPTION_SHELL_ACTIVITY_TIMER = 4 - } - - public class TDengineMeta - { - public string name; - public short size; - public byte type; - public string TypeName() - { - switch ((TDengineDataType)type) - { - case TDengineDataType.TSDB_DATA_TYPE_BOOL: - return "BOOL"; - case TDengineDataType.TSDB_DATA_TYPE_TINYINT: - return "TINYINT"; - case TDengineDataType.TSDB_DATA_TYPE_SMALLINT: - return "SMALLINT"; - case TDengineDataType.TSDB_DATA_TYPE_INT: - return "INT"; - case TDengineDataType.TSDB_DATA_TYPE_BIGINT: - return "BIGINT"; - case TDengineDataType.TSDB_DATA_TYPE_UTINYINT: - return "TINYINT UNSIGNED"; - case TDengineDataType.TSDB_DATA_TYPE_USMALLINT: - return "SMALLINT UNSIGNED"; - case TDengineDataType.TSDB_DATA_TYPE_UINT: - return "INT UNSIGNED"; - case TDengineDataType.TSDB_DATA_TYPE_UBIGINT: - return "BIGINT UNSIGNED"; - case TDengineDataType.TSDB_DATA_TYPE_FLOAT: - return "FLOAT"; - case TDengineDataType.TSDB_DATA_TYPE_DOUBLE: - return "DOUBLE"; - case TDengineDataType.TSDB_DATA_TYPE_BINARY: - return "STRING"; - case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP: - return "TIMESTAMP"; - case TDengineDataType.TSDB_DATA_TYPE_NCHAR: - return "NCHAR"; - default: - return "undefine"; - } - } - } - - public class TDengine - { - public const int TSDB_CODE_SUCCESS = 0; - - [DllImport("taos", EntryPoint = "taos_init", CallingConvention = CallingConvention.Cdecl)] - static extern public void Init(); - - [DllImport("taos", EntryPoint = "taos_cleanup", CallingConvention = CallingConvention.Cdecl)] - static extern public void Cleanup(); - - [DllImport("taos", EntryPoint = "taos_options", CallingConvention = CallingConvention.Cdecl)] - static extern public void Options(int option, string value); - - [DllImport("taos", EntryPoint = "taos_connect", CallingConvention = CallingConvention.Cdecl)] - static extern public IntPtr Connect(string ip, string user, string password, string db, short port); - - [DllImport("taos", EntryPoint = "taos_errstr", CallingConvention = CallingConvention.Cdecl)] - static extern private IntPtr taos_errstr(IntPtr res); - static public string Error(IntPtr res) - { - IntPtr errPtr = taos_errstr(res); - return Marshal.PtrToStringAnsi(errPtr); - } - - [DllImport("taos", EntryPoint = "taos_errno", CallingConvention = CallingConvention.Cdecl)] - static extern public int ErrorNo(IntPtr res); - - [DllImport("taos", EntryPoint = "taos_query", CallingConvention = CallingConvention.Cdecl)] - static extern public IntPtr Query(IntPtr conn, string sqlstr); - - [DllImport("taos", EntryPoint = "taos_affected_rows", CallingConvention = CallingConvention.Cdecl)] - static extern public int AffectRows(IntPtr res); - - [DllImport("taos", EntryPoint = "taos_field_count", CallingConvention = CallingConvention.Cdecl)] - static extern public int FieldCount(IntPtr res); - - [DllImport("taos", EntryPoint = "taos_fetch_fields", CallingConvention = CallingConvention.Cdecl)] - static extern private IntPtr taos_fetch_fields(IntPtr res); - static public List FetchFields(IntPtr res) - { - const int fieldSize = 68; - - List metas = new List(); - if (res == IntPtr.Zero) - { - return metas; - } - - int fieldCount = FieldCount(res); - IntPtr fieldsPtr = taos_fetch_fields(res); - - for (int i = 0; i < fieldCount; ++i) - { - int offset = i * fieldSize; - - TDengineMeta meta = new TDengineMeta(); - meta.name = Marshal.PtrToStringAnsi(fieldsPtr + offset); - meta.type = Marshal.ReadByte(fieldsPtr + offset + 65); - meta.size = Marshal.ReadInt16(fieldsPtr + offset + 66); - metas.Add(meta); - } - - return metas; - } - - [DllImport("taos", EntryPoint = "taos_fetch_row", CallingConvention = CallingConvention.Cdecl)] - static extern public IntPtr FetchRows(IntPtr res); - - [DllImport("taos", EntryPoint = "taos_free_result", CallingConvention = CallingConvention.Cdecl)] - static extern public IntPtr FreeResult(IntPtr res); - - [DllImport("taos", EntryPoint = "taos_close", CallingConvention = CallingConvention.Cdecl)] - static extern public int Close(IntPtr taos); - //get precision in restultset - [DllImport("taos", EntryPoint = "taos_result_precision", CallingConvention = CallingConvention.Cdecl)] - static extern public int ResultPrecision(IntPtr taos); - - //schemaless API - [DllImport("taos",SetLastError = true, EntryPoint = "taos_schemaless_insert", CallingConvention = CallingConvention.Cdecl)] - static extern public IntPtr SchemalessInsert(IntPtr taos, string[] lines, int numLines, int protocol, int precision); - } -} diff --git a/src/connector/C#/csharpTaos.sln b/src/connector/C#/csharpTaos.sln new file mode 100644 index 0000000000..b18ca23001 --- /dev/null +++ b/src/connector/C#/csharpTaos.sln @@ -0,0 +1,72 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30114.105 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A1FB5B66-E32F-4789-9BE9-042E5BD21087}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TDengineDriver", "src\TDengineDriver\TDengineDriver.csproj", "{5BED7402-0A65-4ED9-A491-C56BFB518045}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{CB8E6458-31E1-4351-B704-1B918E998654}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitTest", "src\test\XUnitTest\XUnitTest.csproj", "{64C0A478-2591-4459-9F8F-A70F37976A41}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cases", "src\test\Cases\Cases.csproj", "{19A69D26-66BF-4227-97BE-9B087BC76B2F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Debug|x64.ActiveCfg = Debug|Any CPU + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Debug|x64.Build.0 = Debug|Any CPU + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Debug|x86.ActiveCfg = Debug|Any CPU + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Debug|x86.Build.0 = Debug|Any CPU + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Release|Any CPU.Build.0 = Release|Any CPU + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Release|x64.ActiveCfg = Release|Any CPU + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Release|x64.Build.0 = Release|Any CPU + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Release|x86.ActiveCfg = Release|Any CPU + {5BED7402-0A65-4ED9-A491-C56BFB518045}.Release|x86.Build.0 = Release|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Debug|Any CPU.Build.0 = Debug|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Debug|x64.ActiveCfg = Debug|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Debug|x64.Build.0 = Debug|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Debug|x86.ActiveCfg = Debug|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Debug|x86.Build.0 = Debug|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Release|Any CPU.ActiveCfg = Release|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Release|Any CPU.Build.0 = Release|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Release|x64.ActiveCfg = Release|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Release|x64.Build.0 = Release|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Release|x86.ActiveCfg = Release|Any CPU + {64C0A478-2591-4459-9F8F-A70F37976A41}.Release|x86.Build.0 = Release|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Debug|x64.ActiveCfg = Debug|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Debug|x64.Build.0 = Debug|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Debug|x86.ActiveCfg = Debug|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Debug|x86.Build.0 = Debug|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Release|Any CPU.Build.0 = Release|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Release|x64.ActiveCfg = Release|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Release|x64.Build.0 = Release|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Release|x86.ActiveCfg = Release|Any CPU + {19A69D26-66BF-4227-97BE-9B087BC76B2F}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {5BED7402-0A65-4ED9-A491-C56BFB518045} = {A1FB5B66-E32F-4789-9BE9-042E5BD21087} + {CB8E6458-31E1-4351-B704-1B918E998654} = {A1FB5B66-E32F-4789-9BE9-042E5BD21087} + {64C0A478-2591-4459-9F8F-A70F37976A41} = {CB8E6458-31E1-4351-B704-1B918E998654} + {19A69D26-66BF-4227-97BE-9B087BC76B2F} = {CB8E6458-31E1-4351-B704-1B918E998654} + EndGlobalSection +EndGlobal diff --git a/src/connector/C#/src/TDengineDriver/TDengineDriver.cs b/src/connector/C#/src/TDengineDriver/TDengineDriver.cs new file mode 100644 index 0000000000..1bfb8eae07 --- /dev/null +++ b/src/connector/C#/src/TDengineDriver/TDengineDriver.cs @@ -0,0 +1,399 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace TDengineDriver +{ + public enum TDengineDataType + { + TSDB_DATA_TYPE_NULL = 0, // 1 bytes + TSDB_DATA_TYPE_BOOL = 1, // 1 bytes + TSDB_DATA_TYPE_TINYINT = 2, // 1 bytes + TSDB_DATA_TYPE_SMALLINT = 3, // 2 bytes + TSDB_DATA_TYPE_INT = 4, // 4 bytes + TSDB_DATA_TYPE_BIGINT = 5, // 8 bytes + TSDB_DATA_TYPE_FLOAT = 6, // 4 bytes + TSDB_DATA_TYPE_DOUBLE = 7, // 8 bytes + TSDB_DATA_TYPE_BINARY = 8, // string + TSDB_DATA_TYPE_TIMESTAMP = 9,// 8 bytes + TSDB_DATA_TYPE_NCHAR = 10, // unicode string + TSDB_DATA_TYPE_UTINYINT = 11,// 1 byte + TSDB_DATA_TYPE_USMALLINT = 12,// 2 bytes + TSDB_DATA_TYPE_UINT = 13, // 4 bytes + TSDB_DATA_TYPE_UBIGINT = 14 // 8 bytes + } + + public enum TDengineInitOption + { + TSDB_OPTION_LOCALE = 0, + TSDB_OPTION_CHARSET = 1, + TSDB_OPTION_TIMEZONE = 2, + TDDB_OPTION_CONFIGDIR = 3, + TDDB_OPTION_SHELL_ACTIVITY_TIMER = 4 + } + + enum TaosField + { + STRUCT_SIZE = 68, + NAME_LENGTH = 65, + TYPE_OFFSET = 65, + BYTES_OFFSET = 66, + + } + public class TDengineMeta + { + public string name; + public short size; + public byte type; + public string TypeName() + { + switch ((TDengineDataType)type) + { + case TDengineDataType.TSDB_DATA_TYPE_BOOL: + return "BOOL"; + case TDengineDataType.TSDB_DATA_TYPE_TINYINT: + return "TINYINT"; + case TDengineDataType.TSDB_DATA_TYPE_SMALLINT: + return "SMALLINT"; + case TDengineDataType.TSDB_DATA_TYPE_INT: + return "INT"; + case TDengineDataType.TSDB_DATA_TYPE_BIGINT: + return "BIGINT"; + case TDengineDataType.TSDB_DATA_TYPE_UTINYINT: + return "TINYINT UNSIGNED"; + case TDengineDataType.TSDB_DATA_TYPE_USMALLINT: + return "SMALLINT UNSIGNED"; + case TDengineDataType.TSDB_DATA_TYPE_UINT: + return "INT UNSIGNED"; + case TDengineDataType.TSDB_DATA_TYPE_UBIGINT: + return "BIGINT UNSIGNED"; + case TDengineDataType.TSDB_DATA_TYPE_FLOAT: + return "FLOAT"; + case TDengineDataType.TSDB_DATA_TYPE_DOUBLE: + return "DOUBLE"; + case TDengineDataType.TSDB_DATA_TYPE_BINARY: + return "STRING"; + case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP: + return "TIMESTAMP"; + case TDengineDataType.TSDB_DATA_TYPE_NCHAR: + return "NCHAR"; + default: + return "undefine"; + } + } + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public struct TAOS_BIND + { + // column type + public int buffer_type; + // one column value + public IntPtr buffer; + // unused + public Int32 buffer_length; + // actual value length in buffer + public IntPtr length; + // indicates the column value is null or not + public IntPtr is_null; + // unused + public int is_unsigned; + // unused + public IntPtr error; + public Int64 u; + public uint allocated; + } + + + [StructLayout(LayoutKind.Sequential)] + public struct TAOS_MULTI_BIND + { + // column type + public int buffer_type; + + // array, one or more lines column value + public IntPtr buffer; + + //length of element in TAOS_MULTI_BIND.buffer (for binary and nchar it is the longest element's length) + public ulong buffer_length; + + //array, actual data length for each value + public IntPtr length; + + //array, indicates each column value is null or not + public IntPtr is_null; + + // line number, or the values number in buffer + public int num; + } + + + public class TDengine + { + public const int TSDB_CODE_SUCCESS = 0; + + [DllImport("taos", EntryPoint = "taos_init", CallingConvention = CallingConvention.Cdecl)] + static extern public void Init(); + + [DllImport("taos", EntryPoint = "taos_cleanup", CallingConvention = CallingConvention.Cdecl)] + static extern public void Cleanup(); + + [DllImport("taos", EntryPoint = "taos_options", CallingConvention = CallingConvention.Cdecl)] + static extern public void Options(int option, string value); + + [DllImport("taos", EntryPoint = "taos_connect", CallingConvention = CallingConvention.Cdecl)] + static extern public IntPtr Connect(string ip, string user, string password, string db, short port); + + [DllImport("taos", EntryPoint = "taos_errstr", CallingConvention = CallingConvention.Cdecl)] + static extern private IntPtr taos_errstr(IntPtr res); + static public string Error(IntPtr res) + { + IntPtr errPtr = taos_errstr(res); + return Marshal.PtrToStringAnsi(errPtr); + } + + [DllImport("taos", EntryPoint = "taos_errno", CallingConvention = CallingConvention.Cdecl)] + static extern public int ErrorNo(IntPtr res); + + [DllImport("taos", EntryPoint = "taos_query", CallingConvention = CallingConvention.Cdecl)] + static extern public IntPtr Query(IntPtr conn, string sqlstr); + + [DllImport("taos", EntryPoint = "taos_affected_rows", CallingConvention = CallingConvention.Cdecl)] + static extern public int AffectRows(IntPtr res); + + [DllImport("taos", EntryPoint = "taos_field_count", CallingConvention = CallingConvention.Cdecl)] + static extern public int FieldCount(IntPtr res); + + [DllImport("taos", EntryPoint = "taos_fetch_fields", CallingConvention = CallingConvention.Cdecl)] + static extern private IntPtr taos_fetch_fields(IntPtr res); + static public List FetchFields(IntPtr res) + { + // const int fieldSize = 68; + + List metas = new List(); + if (res == IntPtr.Zero) + { + return metas; + } + + int fieldCount = FieldCount(res); + IntPtr fieldsPtr = taos_fetch_fields(res); + + for (int i = 0; i < fieldCount; ++i) + { + int offset = i * (int)TaosField.STRUCT_SIZE; + TDengineMeta meta = new TDengineMeta(); + meta.name = Marshal.PtrToStringAnsi(fieldsPtr + offset); + meta.type = Marshal.ReadByte(fieldsPtr + offset + (int)TaosField.TYPE_OFFSET); + meta.size = Marshal.ReadInt16(fieldsPtr + offset + (int)TaosField.BYTES_OFFSET); + metas.Add(meta); + } + + return metas; + } + + [DllImport("taos", EntryPoint = "taos_fetch_row", CallingConvention = CallingConvention.Cdecl)] + static extern public IntPtr FetchRows(IntPtr res); + + [DllImport("taos", EntryPoint = "taos_free_result", CallingConvention = CallingConvention.Cdecl)] + static extern public IntPtr FreeResult(IntPtr res); + + [DllImport("taos", EntryPoint = "taos_close", CallingConvention = CallingConvention.Cdecl)] + static extern public int Close(IntPtr taos); + + //get precision of restultset + [DllImport("taos", EntryPoint = "taos_result_precision", CallingConvention = CallingConvention.Cdecl)] + static extern public int ResultPrecision(IntPtr taos); + + + + //stmt APIs: + /// + /// init a TAOS_STMT object for later use. + /// + /// a valid taos connection + /// + /// Not NULL returned for success, NULL for failure. And it should be freed with taos_stmt_close. + /// + [DllImport("taos", EntryPoint = "taos_stmt_init", CallingConvention = CallingConvention.Cdecl)] + static extern public IntPtr StmtInit(IntPtr taos); + + /// + /// prepare a sql statement,'sql' should be a valid INSERT/SELECT statement. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// sql string,used to bind parameters with + /// no used + /// 0 for success, non-zero for failure. + [DllImport("taos", EntryPoint = "taos_stmt_prepare", CallingConvention = CallingConvention.Cdecl)] + static extern public int StmtPrepare(IntPtr stmt, string sql); + + /// + /// For INSERT only. Used to bind table name as a parmeter for the input stmt object. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// table name you want to bind + /// 0 for success, non-zero for failure. + [DllImport("taos", EntryPoint = "taos_stmt_set_tbname", CallingConvention = CallingConvention.Cdecl)] + static extern public int StmtSetTbname(IntPtr stmt, string name); + + /// + /// For INSERT only. + /// Set a table name for binding table name as parameter. Only used for binding all tables + /// in one stable, user application must call 'loadTableInfo' API to load all table + /// meta before calling this API. If the table meta is not cached locally, it will return error. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// table name which is belong to an stable + /// 0 for success, non-zero for failure. + [DllImport("taos", EntryPoint = "taos_stmt_set_sub_tbname", CallingConvention = CallingConvention.Cdecl)] + static extern public int StmtSetSubTbname(IntPtr stmt, string name); + + /// + /// For INSERT only. + /// set a table name for binding table name as parameter and tag values for all tag parameters. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// use to set table name + /// + /// is an array contains all tag values,each item in the array represents a tag column's value. + /// the item number and sequence should keep consistence with that in stable tag definition. + /// + /// 0 for success, non-zero for failure. + [DllImport("taos", EntryPoint = "taos_stmt_set_tbname_tags", CallingConvention = CallingConvention.Cdecl)] + static extern public int StmtSetTbnameTags(IntPtr stmt, string name, TAOS_BIND[] tags); + + /// + /// For both INSERT and SELECT. + /// bind a whole line data. + /// The usage of structure TAOS_BIND is the same with MYSQL_BIND in MySQL. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// + /// points to an array contains the whole line data. + /// the item number and sequence should keep consistence with columns in sql statement. + /// + /// 0 for success, non-zero for failure. + [DllImport("taos", EntryPoint = "taos_stmt_bind_param", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + static extern public int StmtBindParam(IntPtr stmt, TAOS_BIND[] bind); + + /// + /// bind a single column's data, INTERNAL used and for INSERT only. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// points to a column's data which could be the one or more lines. + /// the column's index in prepared sql statement, it starts from 0. + /// 0 for success, non-zero for failure. + [DllImport("taos", EntryPoint = "taos_stmt_bind_single_param_batch", CallingConvention = CallingConvention.Cdecl)] + static extern public int StmtBindSingleParamBatch(IntPtr stmt, ref TAOS_MULTI_BIND bind, int colIdx); + + /// + /// for INSERT only + /// bind one or multiple lines data. The parameter 'bind' + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// + /// points to an array contains one or more lines data.Each item in array represents a column's value(s), + /// the item number and sequence should keep consistence with columns in sql statement. + /// + /// 0 for success, non-zero for failure. + [DllImport("taos", EntryPoint = "taos_stmt_bind_param_batch", CallingConvention = CallingConvention.Cdecl)] + static extern public int StmtBindParamBatch(IntPtr stmt, [In, Out] TAOS_MULTI_BIND[] bind); + + /// + /// For INSERT only. + /// add all current bound parameters to batch process. Must be called after each call to + /// StmtBindParam/StmtBindSingleParamBatch, or all columns binds for one or more lines + /// with StmtBindSingleParamBatch. User application can call any bind parameter + /// API again to bind more data lines after calling to this API. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// 0 for success, non-zero for failure. + [DllImport("taos", EntryPoint = "taos_stmt_add_batch", CallingConvention = CallingConvention.Cdecl)] + static extern public int StmtAddBatch(IntPtr stmt); + + /// + /// actually execute the INSERT/SELECT sql statement. + /// User application can continue to bind new data after calling to this API. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// + [DllImport("taos", EntryPoint = "taos_stmt_execute", CallingConvention = CallingConvention.Cdecl)] + static extern public int StmtExecute(IntPtr stmt); + + /// + /// For SELECT only,getting the query result. User application should free it with API 'FreeResult' at the end. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// Not NULL for success, NULL for failure. + [DllImport("taos", EntryPoint = "taos_stmt_use_result", CallingConvention = CallingConvention.Cdecl)] + static extern public IntPtr StmtUseResult(IntPtr stmt); + + /// + /// close STMT object and free resources. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// 0 for success, non-zero for failure. + [DllImport("taos", EntryPoint = "taos_stmt_close", CallingConvention = CallingConvention.Cdecl)] + static extern public int StmtClose(IntPtr stmt); + + [DllImport("taos", EntryPoint = "taos_load_table_info", CallingConvention = CallingConvention.Cdecl)] + /// + /// user application must call this API to load all tables meta, + /// + /// taos connection + /// tablelist + /// + static extern private int LoadTableInfoDll(IntPtr taos, string tableList); + + /// + /// user application call this API to load all tables meta,this method call the native + /// method LoadTableInfoDll. + /// this method must be called before StmtSetSubTbname(IntPtr stmt, string name); + /// + /// taos connection + /// tables need to load meta info are form in an array + /// + static public int LoadTableInfo(IntPtr taos, string[] tableList) + { + string listStr = string.Join(",", tableList); + return LoadTableInfoDll(taos, listStr); + } + + /// + /// get detail error message when got failure for any stmt API call. If not failure, the result + /// returned in this API is unknown. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// piont the error message + [DllImport("taos", EntryPoint = "taos_stmt_errstr", CallingConvention = CallingConvention.Cdecl)] + static extern private IntPtr StmtErrPtr(IntPtr stmt); + + /// + /// get detail error message when got failure for any stmt API call. If not failure, the result + /// returned in this API is unknown. + /// + /// could be the value returned by 'StmtInit', that may be a valid object or NULL. + /// error string + static public string StmtErrorStr(IntPtr stmt) + { + IntPtr stmtErrPrt = StmtErrPtr(stmt); + return Marshal.PtrToStringAnsi(stmtErrPrt); + } + } +} diff --git a/src/connector/C#/src/TDengineDriver/TDengineDriver.csproj b/src/connector/C#/src/TDengineDriver/TDengineDriver.csproj new file mode 100644 index 0000000000..f208d303c9 --- /dev/null +++ b/src/connector/C#/src/TDengineDriver/TDengineDriver.csproj @@ -0,0 +1,7 @@ + + + + net5.0 + + + diff --git a/src/connector/C#/src/TDengineDriver/TaosBind.cs b/src/connector/C#/src/TDengineDriver/TaosBind.cs new file mode 100644 index 0000000000..694dcd900b --- /dev/null +++ b/src/connector/C#/src/TDengineDriver/TaosBind.cs @@ -0,0 +1,332 @@ +using System; +using System.Runtime.InteropServices; + + +namespace TDengineDriver +{ + /// + /// this class used to get an instance of struct of TAO_BIND or TAOS_MULTI_BIND + /// And the instance is corresponding with TDengine data type. For example, calling + /// "bindBinary" will return a TAOS_BIND object that is corresponding with TDengine's + /// binary type. + /// + public class TaosBind + { + public static TAOS_BIND BindBool(bool val) + { + TAOS_BIND bind = new TAOS_BIND(); + byte[] boolByteArr = BitConverter.GetBytes(val); + int boolByteArrSize = Marshal.SizeOf(boolByteArr[0]) * boolByteArr.Length; + IntPtr bo = Marshal.AllocHGlobal(1); + Marshal.Copy(boolByteArr, 0, bo, boolByteArr.Length); + + int length = sizeof(Boolean); + IntPtr lengPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_BOOL; + bind.buffer = bo; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + public static TAOS_BIND BindTinyInt(sbyte val) + { + TAOS_BIND bind = new TAOS_BIND(); + + byte[] tinyIntByteArr = BitConverter.GetBytes(val); + int tinyIntByteArrSize = Marshal.SizeOf(tinyIntByteArr[0]) * tinyIntByteArr.Length; + IntPtr uManageTinyInt = Marshal.AllocHGlobal(tinyIntByteArrSize); + Marshal.Copy(tinyIntByteArr, 0, uManageTinyInt, tinyIntByteArr.Length); + + int length = sizeof(sbyte); + IntPtr lengPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_TINYINT; + bind.buffer = uManageTinyInt; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + return bind; + + } + + public static TAOS_BIND BindSmallInt(short val) + { + + TAOS_BIND bind = new TAOS_BIND(); + IntPtr uManageSmallInt = Marshal.AllocHGlobal(sizeof(short)); + Marshal.WriteInt16(uManageSmallInt, val); + + int length = sizeof(short); + IntPtr lengPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_SMALLINT; + bind.buffer = uManageSmallInt; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + + public static TAOS_BIND BindInt(int val) + { + TAOS_BIND bind = new TAOS_BIND(); + IntPtr uManageInt = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(uManageInt, val); + + int length = sizeof(int); + IntPtr lengPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_INT; + bind.buffer = uManageInt; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + + public static TAOS_BIND BindBigInt(long val) + { + + TAOS_BIND bind = new TAOS_BIND(); + IntPtr uManageBigInt = Marshal.AllocHGlobal(sizeof(long)); + Marshal.WriteInt64(uManageBigInt, val); + + int length = sizeof(long); + IntPtr lengPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_BIGINT; + bind.buffer = uManageBigInt; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + + public static TAOS_BIND BindUTinyInt(byte val) + { + TAOS_BIND bind = new TAOS_BIND(); + + IntPtr uManageTinyInt = Marshal.AllocHGlobal(sizeof(byte)); + Marshal.WriteByte(uManageTinyInt, val); + + int length = sizeof(byte); + IntPtr lengPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_UTINYINT; + bind.buffer = uManageTinyInt; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + + public static TAOS_BIND BindUSmallInt(UInt16 val) + { + TAOS_BIND bind = new TAOS_BIND(); + + byte[] uSmallIntByteArr = BitConverter.GetBytes(val); + int usmallSize = Marshal.SizeOf(uSmallIntByteArr[0]) * uSmallIntByteArr.Length; + IntPtr uManageUnsignSmallInt = Marshal.AllocHGlobal(usmallSize); + Marshal.Copy(uSmallIntByteArr, 0, uManageUnsignSmallInt, uSmallIntByteArr.Length); + + int length = sizeof(UInt16); + IntPtr lengPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_USMALLINT; + bind.buffer = uManageUnsignSmallInt; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + + public static TAOS_BIND BindUInt(uint val) + { + TAOS_BIND bind = new TAOS_BIND(); + + byte[] uManageIntByteArr = BitConverter.GetBytes(val); + int usmallSize = Marshal.SizeOf(uManageIntByteArr[0]) * uManageIntByteArr.Length; + IntPtr uManageInt = Marshal.AllocHGlobal(usmallSize); + Marshal.Copy(uManageIntByteArr, 0, uManageInt, uManageIntByteArr.Length); + + int length = sizeof(uint); + IntPtr lengPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_UINT; + bind.buffer = uManageInt; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + + public static TAOS_BIND BindUBigInt(ulong val) + { + TAOS_BIND bind = new TAOS_BIND(); + + byte[] uManageBigIntByteArr = BitConverter.GetBytes(val); + int usmallSize = Marshal.SizeOf(uManageBigIntByteArr[0]) * uManageBigIntByteArr.Length; + IntPtr uManageBigInt = Marshal.AllocHGlobal(usmallSize); + Marshal.Copy(uManageBigIntByteArr, 0, uManageBigInt, uManageBigIntByteArr.Length); + + int length = sizeof(ulong); + IntPtr lengPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_UBIGINT; + bind.buffer = uManageBigInt; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + + public static TAOS_BIND BindFloat(float val) + { + TAOS_BIND bind = new TAOS_BIND(); + + byte[] floatByteArr = BitConverter.GetBytes(val); + int floatByteArrSize = Marshal.SizeOf(floatByteArr[0]) * floatByteArr.Length; + IntPtr uManageFloat = Marshal.AllocHGlobal(floatByteArrSize); + Marshal.Copy(floatByteArr, 0, uManageFloat, floatByteArr.Length); + + int length = sizeof(float); + IntPtr lengPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_FLOAT; + bind.buffer = uManageFloat; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + + public static TAOS_BIND BindDouble(Double val) + { + TAOS_BIND bind = new TAOS_BIND(); + + byte[] doubleByteArr = BitConverter.GetBytes(val); + int doubleByteArrSize = Marshal.SizeOf(doubleByteArr[0]) * doubleByteArr.Length; + IntPtr uManageDouble = Marshal.AllocHGlobal(doubleByteArrSize); + Marshal.Copy(doubleByteArr, 0, uManageDouble, doubleByteArr.Length); + + int length = sizeof(Double); + IntPtr lengPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_DOUBLE; + bind.buffer = uManageDouble; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + + public static TAOS_BIND BindBinary(String val) + { + + TAOS_BIND bind = new TAOS_BIND(); + IntPtr umanageBinary = Marshal.StringToHGlobalAnsi(val); + + int leng = val.Length; + IntPtr lenPtr = Marshal.AllocHGlobal(sizeof(ulong)); + Marshal.WriteInt64(lenPtr, leng); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_BINARY; + bind.buffer = umanageBinary; + bind.buffer_length = leng; + bind.length = lenPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + public static TAOS_BIND BindNchar(String val) + { + TAOS_BIND bind = new TAOS_BIND(); + IntPtr umanageNchar = (IntPtr)Marshal.StringToHGlobalAnsi(val); + + int leng = val.Length; + IntPtr lenPtr = Marshal.AllocHGlobal(sizeof(ulong)); + Marshal.WriteInt64(lenPtr, leng); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_NCHAR; + bind.buffer = umanageNchar; + bind.buffer_length = leng; + bind.length = lenPtr; + bind.is_null = IntPtr.Zero; + + return bind; + } + + public static TAOS_BIND BindNil() + { + TAOS_BIND bind = new TAOS_BIND(); + + int isNull = 1;//IntPtr.Size; + IntPtr lenPtr = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(lenPtr, isNull); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_NULL; + bind.is_null = lenPtr; + return bind; + } + + public static TAOS_BIND BindTimestamp(long ts) + { + + TAOS_BIND bind = new TAOS_BIND(); + IntPtr uManageTs = Marshal.AllocHGlobal(sizeof(long)); + Marshal.WriteInt64(uManageTs, ts); + + int length = sizeof(long); + IntPtr lengPtr = Marshal.AllocHGlobal(4); + Marshal.WriteInt32(lengPtr, length); + + bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP; + bind.buffer = uManageTs; + bind.buffer_length = length; + bind.length = lengPtr; + bind.is_null = IntPtr.Zero; + + return bind; + + } + + public static void FreeTaosBind(TAOS_BIND[] binds) + { + foreach (TAOS_BIND bind in binds) + { + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + if (bind.is_null != IntPtr.Zero) + { + // Console.WriteLine(bind.is_null); + Marshal.FreeHGlobal(bind.is_null); + } + + } + } + } + +} \ No newline at end of file diff --git a/src/connector/C#/src/TDengineDriver/TaosMultiBind.cs b/src/connector/C#/src/TDengineDriver/TaosMultiBind.cs new file mode 100644 index 0000000000..e01558caeb --- /dev/null +++ b/src/connector/C#/src/TDengineDriver/TaosMultiBind.cs @@ -0,0 +1,616 @@ +using System; +using System.Text; +using System.Runtime.InteropServices; + + +namespace TDengineDriver +{ + public class TaosMultiBind + { + public static TAOS_MULTI_BIND MultiBindBool(bool?[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + //the size of buffer array element + int typeSize = sizeof(bool); + //size of int + int intSize = sizeof(int); + int byteSize = sizeof(byte); + + //TAOS_MULTI_BIND.buffer + IntPtr unmanagedBoolArr = Marshal.AllocHGlobal(elementCount * typeSize); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + for (int i = 0; i < elementCount; i++) + { + //set TAOS_MULTI_BIND.buffer + Marshal.WriteByte(unmanagedBoolArr, typeSize * i, Convert.ToByte(arr[i] ?? false)); + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(arr[i].Equals(null) ? 1 : 0)); + } + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_BOOL; + multiBind.buffer = unmanagedBoolArr; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + public static TAOS_MULTI_BIND MultiBindTinyInt(sbyte?[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + //the size of buffer array element + int typeSize = sizeof(byte); + int byteSize = sizeof(byte); + //size of int + int intSize = sizeof(int); + + //TAOS_MULTI_BIND.buffer + IntPtr unmanagedTintIntArr = Marshal.AllocHGlobal(elementCount); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(intSize * elementCount); + + for (int i = 0; i < elementCount; i++) + { + Byte[] toByteArr = BitConverter.GetBytes(arr[i] ?? sbyte.MinValue); + + //set TAOS_MULTI_BIND.buffer + Marshal.WriteByte(unmanagedTintIntArr, typeSize * i, toByteArr[0]); + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(arr[i].Equals(null) ? 1 : 0)); + } + + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_TINYINT; + multiBind.buffer = unmanagedTintIntArr; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + public static TAOS_MULTI_BIND MultiBindSmallInt(short?[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + //the size of buffer array element + int typeSize = sizeof(short); + //size of int + int intSize = sizeof(int); + int byteSize = sizeof(byte); + + //TAOS_MULTI_BIND.buffer + IntPtr unmanagedSmallIntArr = Marshal.AllocHGlobal(elementCount * typeSize); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + for (int i = 0; i < elementCount; i++) + { + //set TAOS_MULTI_BIND.buffer + Marshal.WriteInt16(unmanagedSmallIntArr, typeSize * i, arr[i] ?? short.MinValue); + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(arr[i].Equals(null) ? 1 : 0)); + + } + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_SMALLINT; + multiBind.buffer = unmanagedSmallIntArr; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + public static TAOS_MULTI_BIND MultiBindInt(int?[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + int typeSize = sizeof(int); + int intSize = sizeof(int); + int byteSize = sizeof(byte); + + //TAOS_MULTI_BIND.buffer + IntPtr intBuff = Marshal.AllocHGlobal(typeSize * elementCount); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + for (int i = 0; i < elementCount; i++) + { + //set TAOS_MULTI_BIND.buffer + Marshal.WriteInt32(intBuff, typeSize * i, arr[i] ?? int.MinValue); + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(arr[i].Equals(null) ? 1 : 0)); + + } + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_INT; + multiBind.buffer = intBuff; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + public static TAOS_MULTI_BIND MultiBindBigint(long?[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + int typeSize = sizeof(long); + int intSize = sizeof(int); + int byteSize = sizeof(byte); + + //TAOS_MULTI_BIND.buffer + IntPtr intBuff = Marshal.AllocHGlobal(typeSize * elementCount); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + for (int i = 0; i < elementCount; i++) + { + //set TAOS_MULTI_BIND.buffer + Marshal.WriteInt64(intBuff, typeSize * i, arr[i] ?? long.MinValue); + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(arr[i].Equals(null) ? 1 : 0)); + + + } + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_BIGINT; + multiBind.buffer = intBuff; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + public static TAOS_MULTI_BIND MultiBindFloat(float?[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + int typeSize = sizeof(float); + int intSize = sizeof(int); + int byteSize = sizeof(byte); + //used to replace null + float[] arrTmp = new float[elementCount]; + + //TAOS_MULTI_BIND.buffer + IntPtr floatBuff = Marshal.AllocHGlobal(typeSize * elementCount); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + + for (int i = 0; i < elementCount; i++) + { + arrTmp[i] = arr[i] ?? float.MinValue; + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(arr[i].Equals(null) ? 1 : 0)); + } + //set TAOS_MULTI_BIND.buffer + Marshal.Copy(arrTmp, 0, floatBuff, elementCount); + + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_FLOAT; + multiBind.buffer = floatBuff; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + public static TAOS_MULTI_BIND MultiBindDouble(double?[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + int typeSize = sizeof(double); + int intSize = sizeof(int); + int byteSize = sizeof(byte); + //used to replace null + double[] arrTmp = new double[elementCount]; + + //TAOS_MULTI_BIND.buffer + IntPtr doubleBuff = Marshal.AllocHGlobal(typeSize * elementCount); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + + for (int i = 0; i < elementCount; i++) + { + arrTmp[i] = arr[i] ?? double.MinValue; + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(arr[i].Equals(null) ? 1 : 0)); + } + //set TAOS_MULTI_BIND.buffer + Marshal.Copy(arrTmp, 0, doubleBuff, elementCount); + + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_DOUBLE; + multiBind.buffer = doubleBuff; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + public static TAOS_MULTI_BIND MultiBindUTinyInt(byte?[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + int typeSize = sizeof(byte); + int intSize = sizeof(int); + int byteSize = sizeof(byte); + //used to replace null + + //TAOS_MULTI_BIND.buffer + IntPtr uTinyIntBuff = Marshal.AllocHGlobal(typeSize * elementCount); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + + for (int i = 0; i < elementCount; i++) + { + //set TAOS_MULTI_BIND.buffer + Marshal.WriteByte(uTinyIntBuff, typeSize * i, arr[i] ?? byte.MaxValue); + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(arr[i].Equals(null) ? 1 : 0)); + } + + + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_UTINYINT; + multiBind.buffer = uTinyIntBuff; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + public static TAOS_MULTI_BIND MultiBindUSmallInt(ushort?[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + int typeSize = sizeof(ushort); + int intSize = sizeof(int); + int byteSize = sizeof(byte); + //used to replace null + + //TAOS_MULTI_BIND.buffer + IntPtr uSmallIntBuff = Marshal.AllocHGlobal(typeSize * elementCount); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + + for (int i = 0; i < elementCount; i++) + { + byte[] byteArr = BitConverter.GetBytes(arr[i] ?? ushort.MaxValue); + for (int j = 0; j < byteArr.Length; j++) + { + //set TAOS_MULTI_BIND.buffer + Marshal.WriteByte(uSmallIntBuff, typeSize * i + j * byteSize, byteArr[j]); + } + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(arr[i].Equals(null) ? 1 : 0)); + } + + + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_USMALLINT; + multiBind.buffer = uSmallIntBuff; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + public static TAOS_MULTI_BIND MultiBindUInt(uint?[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + int typeSize = sizeof(uint); + int intSize = sizeof(int); + int byteSize = sizeof(byte); + //used to replace null + + //TAOS_MULTI_BIND.buffer + IntPtr uIntBuff = Marshal.AllocHGlobal(typeSize * elementCount); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + + for (int i = 0; i < elementCount; i++) + { + byte[] byteArr = BitConverter.GetBytes(arr[i] ?? uint.MaxValue); + for (int j = 0; j < byteArr.Length; j++) + { + //set TAOS_MULTI_BIND.buffer + Marshal.WriteByte(uIntBuff, typeSize * i + j * byteSize, byteArr[j]); + } + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(arr[i].Equals(null) ? 1 : 0)); + } + + + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_UINT; + multiBind.buffer = uIntBuff; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + public static TAOS_MULTI_BIND MultiBindUBigInt(ulong?[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + int typeSize = sizeof(ulong); + int intSize = sizeof(int); + int byteSize = sizeof(byte); + //used to replace null + + //TAOS_MULTI_BIND.buffer + IntPtr uBigIntBuff = Marshal.AllocHGlobal(typeSize * elementCount); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + + for (int i = 0; i < elementCount; i++) + { + byte[] byteArr = BitConverter.GetBytes(arr[i] ?? ulong.MaxValue); + for (int j = 0; j < byteArr.Length; j++) + { + //set TAOS_MULTI_BIND.buffer + Marshal.WriteByte(uBigIntBuff, typeSize * i + j * byteSize, byteArr[j]); + } + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(arr[i].Equals(null) ? 1 : 0)); + } + + + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_UBIGINT; + multiBind.buffer = uBigIntBuff; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + public static TAOS_MULTI_BIND MultiBindBinary(string[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + int typeSize = MaxElementLength(arr); + int intSize = sizeof(int); + int byteSize = sizeof(byte); + StringBuilder arrStrBuilder = new StringBuilder(); ; + + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + for (int i = 0; i < elementCount; i++) + { + int itemLength = 0; + // if element if not null and element length is less then typeSize + // fill the memory with default char.Since arr element memory need align. + if (!String.IsNullOrEmpty(arr[i]) && typeSize <= arr[i].Length) + { + itemLength = arr[i].Length; + arrStrBuilder.Append(arr[i]); + } + else if (!String.IsNullOrEmpty(arr[i]) && typeSize > arr[i].Length) + { + itemLength = arr[i].Length; + arrStrBuilder.Append(arr[i]); + arrStrBuilder.Append(AlignCharArr(typeSize - arr[i].Length)); + } + else + { + // if is null value,fill the memory with default values. + itemLength = 0; + arrStrBuilder.Append(AlignCharArr(typeSize)); + } + + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, itemLength); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(String.IsNullOrEmpty(arr[i]) ? 1 : 0)); + } + //set TAOS_MULTI_BIND.buffer + IntPtr uBinaryBuff = (IntPtr)Marshal.StringToHGlobalAnsi(arrStrBuilder.ToString()); + + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_BINARY; + multiBind.buffer = uBinaryBuff; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + + public static TAOS_MULTI_BIND MultiBindNchar(string[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + int typeSize = MaxElementLength(arr); + int intSize = sizeof(int); + int byteSize = sizeof(byte); + StringBuilder arrStrBuilder = new StringBuilder(); ; + + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + for (int i = 0; i < elementCount; i++) + { + int itemLength = 0; + // if element if not null and element length is less then typeSize + // fill the memory with default char.Since arr element memory need align. + if (!String.IsNullOrEmpty(arr[i]) && typeSize <= arr[i].Length) + { + itemLength = arr[i].Length; + arrStrBuilder.Append(arr[i]); + } + else if (!String.IsNullOrEmpty(arr[i]) && typeSize > arr[i].Length) + { + itemLength = arr[i].Length; + arrStrBuilder.Append(arr[i]); + arrStrBuilder.Append(AlignCharArr(typeSize - arr[i].Length)); + } + else + { + // if is null value,fill the memory with default values. + itemLength = 0; + arrStrBuilder.Append(AlignCharArr(typeSize)); + } + + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, itemLength); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, Convert.ToByte(String.IsNullOrEmpty(arr[i]) ? 1 : 0)); + } + //set TAOS_MULTI_BIND.buffer + IntPtr uNcharBuff = (IntPtr)Marshal.StringToHGlobalAnsi(arrStrBuilder.ToString()); + + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_NCHAR; + multiBind.buffer = uNcharBuff; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + + public static TAOS_MULTI_BIND MultiBindTimestamp(long[] arr) + { + TAOS_MULTI_BIND multiBind = new TAOS_MULTI_BIND(); + int elementCount = arr.Length; + int typeSize = sizeof(long); + int intSize = sizeof(int); + int byteSize = sizeof(byte); + //TAOS_MULTI_BIND.buffer + IntPtr unmanagedTsArr = Marshal.AllocHGlobal(typeSize * elementCount); + //TAOS_MULTI_BIND.length + IntPtr lengthArr = Marshal.AllocHGlobal(intSize * elementCount); + //TAOS_MULTI_BIND.is_null + IntPtr nullArr = Marshal.AllocHGlobal(byteSize * elementCount); + + for (int i = 0; i < elementCount; i++) + { + //set TAOS_MULTI_BIND.buffer + Marshal.WriteInt64(unmanagedTsArr, typeSize * i, arr[i]); + //set TAOS_MULTI_BIND.length + Marshal.WriteInt32(lengthArr, intSize * i, typeSize); + //set TAOS_MULTI_BIND.is_null + Marshal.WriteByte(nullArr, byteSize * i, 0); + } + + //config TAOS_MULTI_BIND + multiBind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP; + multiBind.buffer = unmanagedTsArr; + multiBind.buffer_length = (ulong)typeSize; + multiBind.length = lengthArr; + multiBind.is_null = nullArr; + multiBind.num = elementCount; + + return multiBind; + } + + public static void FreeTaosBind(TAOS_MULTI_BIND[] mBinds) + { + foreach (TAOS_MULTI_BIND bind in mBinds) + { + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + Marshal.FreeHGlobal(bind.is_null); + } + } + + private static char[] AlignCharArr(int offSet) + { + char[] alignChar = new char[offSet]; + for (int i = 0; i < offSet; i++) + { + alignChar[i] = char.MinValue; + } + return alignChar; + } + + private static int MaxElementLength(String[] strArr) + { + int max = 0; + for (int i = 0; i < strArr.Length; i++) + { + if (!String.IsNullOrEmpty(strArr[i]) && max < strArr[i].Length) + { + max = strArr[i].Length; + } + } + return max; + } + } + +} \ No newline at end of file diff --git a/src/connector/C#/src/test/Cases/Cases.csproj b/src/connector/C#/src/test/Cases/Cases.csproj new file mode 100644 index 0000000000..f2ae6938fb --- /dev/null +++ b/src/connector/C#/src/test/Cases/Cases.csproj @@ -0,0 +1,12 @@ + + + + + + + + Exe + net5.0 + + + diff --git a/src/connector/C#/src/test/Cases/DataSource.cs b/src/connector/C#/src/test/Cases/DataSource.cs new file mode 100644 index 0000000000..e422c70bf1 --- /dev/null +++ b/src/connector/C#/src/test/Cases/DataSource.cs @@ -0,0 +1,103 @@ +using System; +using Test.UtilsTools; +using TDengineDriver; + +namespace Test.UtilsTools.DataSource +{ + public class DataSource + { + public static long[] tsArr = new long[5] { 1637064040000, 1637064041000, 1637064042000, 1637064043000, 1637064044000 }; + public static bool?[] boolArr = new bool?[5] { true, false, null, true, true }; + public static sbyte?[] tinyIntArr = new sbyte?[5] { -127, 0, null, 8, 127 }; + public static short?[] shortArr = new short?[5] { short.MinValue + 1, -200, null, 100, short.MaxValue }; + public static int?[] intArr = new int?[5] { -200, -100, null, 0, 300 }; + public static long?[] longArr = new long?[5] { long.MinValue + 1, -2000, null, 1000, long.MaxValue }; + public static float?[] floatArr = new float?[5] { float.MinValue + 1, -12.1F, null, 0F, float.MaxValue }; + public static double?[] doubleArr = new double?[5] { double.MinValue + 1, -19.112D, null, 0D, double.MaxValue }; + public static byte?[] uTinyIntArr = new byte?[5] { byte.MinValue, 12, null, 89, byte.MaxValue - 1 }; + public static ushort?[] uShortArr = new ushort?[5] { ushort.MinValue, 200, null, 400, ushort.MaxValue - 1 }; + public static uint?[] uIntArr = new uint?[5] { uint.MinValue, 100, null, 2, uint.MaxValue - 1 }; + public static ulong?[] uLongArr = new ulong?[5] { ulong.MinValue, 2000, null, 1000, long.MaxValue - 1 }; + public static string[] binaryArr = new string[5] { "1234567890~!@#$%^&*()_+=-`[]{}:,./<>?", String.Empty, null, "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890~!@#$%^&*()_+=-`[]{}:,./<>?" }; + public static string[] ncharArr = new string[5] { "1234567890~!@#$%^&*()_+=-`[]{}:,./<>?", null, "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890~!@#$%^&*()_+=-`[]{}:,./<>?", string.Empty }; + + + public static TAOS_BIND[] getTags() + { + TAOS_BIND[] binds = new TAOS_BIND[13]; + binds[0] = TaosBind.BindBool(true); + binds[1] = TaosBind.BindTinyInt(-2); + binds[2] = TaosBind.BindSmallInt(short.MaxValue); + binds[3] = TaosBind.BindInt(int.MaxValue); + binds[4] = TaosBind.BindBigInt(Int64.MaxValue); + binds[5] = TaosBind.BindUTinyInt(byte.MaxValue - 1); + binds[6] = TaosBind.BindUSmallInt(UInt16.MaxValue - 1); + binds[7] = TaosBind.BindUInt(uint.MinValue + 1); + binds[8] = TaosBind.BindUBigInt(UInt64.MinValue + 1); + binds[9] = TaosBind.BindFloat(11.11F); + binds[10] = TaosBind.BindDouble(22.22D); + binds[11] = TaosBind.BindBinary("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKZXCVBNM`1234567890-=+_)(*&^%$#@!~[];,./<>?:{}"); + binds[12] = TaosBind.BindNchar("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKZXCVBNM`1234567890-=+_)(*&^%$#@!~[];,./<>?:{}"); + return binds; + } + public static TAOS_BIND[] getNtableRow() + { + TAOS_BIND[] binds = new TAOS_BIND[15]; + binds[0] = TaosBind.BindTimestamp(1637064040000); + binds[1] = TaosBind.BindTinyInt(-2); + binds[2] = TaosBind.BindSmallInt(short.MaxValue); + binds[3] = TaosBind.BindInt(int.MaxValue); + binds[4] = TaosBind.BindBigInt(Int64.MaxValue); + binds[5] = TaosBind.BindUTinyInt(byte.MaxValue - 1); + binds[6] = TaosBind.BindUSmallInt(UInt16.MaxValue - 1); + binds[7] = TaosBind.BindUInt(uint.MinValue + 1); + binds[8] = TaosBind.BindUBigInt(UInt64.MinValue + 1); + binds[9] = TaosBind.BindFloat(11.11F); + binds[10] = TaosBind.BindDouble(22.22D); + binds[11] = TaosBind.BindBinary("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKZXCVBNM`1234567890-=+_)(*&^%$#@!~[];,./<>?:{}"); + binds[12] = TaosBind.BindNchar("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKZXCVBNM`1234567890-=+_)(*&^%$#@!~[];,./<>?:{}"); + binds[13] = TaosBind.BindBool(true); + binds[14] = TaosBind.BindNil(); + return binds; + } + + public static TAOS_MULTI_BIND[] GetMultiBindArr() + { + TAOS_MULTI_BIND[] mBinds = new TAOS_MULTI_BIND[14]; + mBinds[0] = TaosMultiBind.MultiBindTimestamp(tsArr); + mBinds[1] = TaosMultiBind.MultiBindBool(boolArr); + mBinds[2] = TaosMultiBind.MultiBindTinyInt(tinyIntArr); + mBinds[3] = TaosMultiBind.MultiBindSmallInt(shortArr); + mBinds[4] = TaosMultiBind.MultiBindInt(intArr); + mBinds[5] = TaosMultiBind.MultiBindBigint(longArr); + mBinds[6] = TaosMultiBind.MultiBindFloat(floatArr); + mBinds[7] = TaosMultiBind.MultiBindDouble(doubleArr); + mBinds[8] = TaosMultiBind.MultiBindUTinyInt(uTinyIntArr); + mBinds[9] = TaosMultiBind.MultiBindUSmallInt(uShortArr); + mBinds[10] = TaosMultiBind.MultiBindUInt(uIntArr); + mBinds[11] = TaosMultiBind.MultiBindUBigInt(uLongArr); + mBinds[12] = TaosMultiBind.MultiBindBinary(binaryArr); + mBinds[13] = TaosMultiBind.MultiBindNchar(ncharArr); + return mBinds; + } + public static TAOS_BIND[] GetQueryCondition() + { + TAOS_BIND[] queryCondition = new TAOS_BIND[2]; + queryCondition[0] = TaosBind.BindTinyInt(0); + queryCondition[1] = TaosBind.BindInt(1000); + return queryCondition; + + } + public static void FreeTaosBind(TAOS_BIND[] binds) + { + TaosBind.FreeTaosBind(binds); + } + + public static void FreeTaosMBind(TAOS_MULTI_BIND[] mbinds) + { + TaosMultiBind.FreeTaosBind(mbinds); + } + + + } +} \ No newline at end of file diff --git a/src/connector/C#/src/test/Cases/Program.cs b/src/connector/C#/src/test/Cases/Program.cs new file mode 100644 index 0000000000..3d3b765b5b --- /dev/null +++ b/src/connector/C#/src/test/Cases/Program.cs @@ -0,0 +1,59 @@ +using System; +using Test.UtilsTools; +using Cases; + +namespace Cases.EntryPoint +{ + class Program + { + + static void Main(string[] args) + { + IntPtr conn = IntPtr.Zero; + IntPtr stmt = IntPtr.Zero; + IntPtr res = IntPtr.Zero; + + conn = UtilsTools.TDConnection("127.0.0.1", "root", "taosdata", "", 0); + UtilsTools.ExecuteQuery(conn, "drop database if exists csharp"); + UtilsTools.ExecuteQuery(conn, "create database if not exists csharp keep 3650"); + UtilsTools.ExecuteQuery(conn, "use csharp"); + + Console.WriteLine("====================StableColumnByColumn==================="); + StableColumnByColumn columnByColumn = new StableColumnByColumn(); + columnByColumn.Test(conn, "stablecolumnbycolumn"); + Console.WriteLine("====================StmtStableQuery==================="); + StmtStableQuery stmtStableQuery = new StmtStableQuery(); + stmtStableQuery.Test(conn, "stablecolumnbycolumn"); + + Console.WriteLine("====================StableMutipleLine==================="); + StableMutipleLine mutipleLine = new StableMutipleLine(); + mutipleLine.Test(conn, "stablemutipleline"); + + //================================================================================ + + Console.WriteLine("====================NtableSingleLine==================="); + NtableSingleLine ntableSingleLine = new NtableSingleLine(); + ntableSingleLine.Test(conn, "stablesingleline"); + + Console.WriteLine("====================NtableMutipleLine==================="); + NtableMutipleLine ntableMutipleLine = new NtableMutipleLine(); + ntableMutipleLine.Test(conn, "ntablemutipleline"); + Console.WriteLine("====================StmtNtableQuery==================="); + StmtNtableQuery stmtNtableQuery = new StmtNtableQuery(); + stmtNtableQuery.Test(conn, "ntablemutipleline"); + + Console.WriteLine("====================NtableColumnByColumn==================="); + NtableColumnByColumn ntableColumnByColumn = new NtableColumnByColumn(); + ntableColumnByColumn.Test(conn, "ntablecolumnbycolumn"); + + Console.WriteLine("====================fetchfeilds==================="); + FetchFields fetchFields = new FetchFields(); + fetchFields.Test(conn, "fetchfeilds"); + + UtilsTools.ExecuteQuery(conn, "drop database if exists csharp"); + UtilsTools.CloseConnection(conn); + UtilsTools.ExitProgram(); + + } + } +} diff --git a/src/connector/C#/src/test/Cases/StmtNormalTable.cs b/src/connector/C#/src/test/Cases/StmtNormalTable.cs new file mode 100644 index 0000000000..a918f6bada --- /dev/null +++ b/src/connector/C#/src/test/Cases/StmtNormalTable.cs @@ -0,0 +1,89 @@ +using System; +using Test.UtilsTools; +using TDengineDriver; +using Test.UtilsTools.DataSource; + +namespace Cases +{ + public class NtableSingleLine + { + + public void Test(IntPtr conn, string tableName) + { + String createTb = "create table " + tableName + "(ts timestamp,tt tinyint,si smallint,ii int,bi bigint,tu tinyint unsigned,su smallint unsigned,iu int unsigned,bu bigint unsigned,ff float ,dd double ,bb binary(200),nc nchar(200),bo bool,nullVal int);"; + String insertSql = "insert into ? values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; + TAOS_BIND[] valuesRow = DataSource.getNtableRow(); + UtilsTools.ExecuteQuery(conn, createTb); + + IntPtr stmt = StmtUtilTools.StmtInit(conn); + StmtUtilTools.StmtPrepare(stmt, insertSql); + StmtUtilTools.SetTableName(stmt, tableName); + StmtUtilTools.BindParam(stmt, valuesRow); + StmtUtilTools.AddBatch(stmt); + StmtUtilTools.StmtExecute(stmt); + StmtUtilTools.StmtClose(stmt); + DataSource.FreeTaosBind(valuesRow); + } + } + + public class NtableMutipleLine + { + TAOS_MULTI_BIND[] mbind = DataSource.GetMultiBindArr(); + public void Test(IntPtr conn, string tableName) + { + String createTb = "create table " + tableName + " (ts timestamp ,b bool,v1 tinyint,v2 smallint,v4 int,v8 bigint,f4 float,f8 double,u1 tinyint unsigned,u2 smallint unsigned,u4 int unsigned,u8 bigint unsigned,bin binary(200),blob nchar(200));"; + String insertSql = "insert into ? values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; + + UtilsTools.ExecuteQuery(conn, createTb); + String[] loadList = { tableName }; + IntPtr stmt = StmtUtilTools.StmtInit(conn); + StmtUtilTools.loadTableInfo(conn, loadList); + StmtUtilTools.StmtPrepare(stmt, insertSql); + StmtUtilTools.SetTableName(stmt, tableName); + StmtUtilTools.BindParamBatch(stmt, mbind); + StmtUtilTools.AddBatch(stmt); + StmtUtilTools.StmtExecute(stmt); + StmtUtilTools.StmtClose(stmt); + DataSource.FreeTaosMBind(mbind); + } + } + public class NtableColumnByColumn + { + DataSource data = new DataSource(); + TAOS_MULTI_BIND[] mbind = DataSource.GetMultiBindArr(); + public void Test(IntPtr conn, string tableName) + { + String createTb = "create table " + tableName + " (ts timestamp ,b bool,v1 tinyint,v2 smallint,v4 int,v8 bigint,f4 float,f8 double,u1 tinyint unsigned,u2 smallint unsigned,u4 int unsigned,u8 bigint unsigned,bin binary(200),blob nchar(200));"; + String insertSql = "insert into ? values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; + + + UtilsTools.ExecuteQuery(conn, createTb); + IntPtr stmt = StmtUtilTools.StmtInit(conn); + + StmtUtilTools.StmtPrepare(stmt, insertSql); + + StmtUtilTools.SetTableName(stmt, tableName); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[0], 0); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[1], 1); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[2], 2); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[3], 3); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[4], 4); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[5], 5); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[6], 6); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[7], 7); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[8], 8); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[9], 9); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[10], 10); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[11], 11); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[12], 12); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[13], 13); + + StmtUtilTools.AddBatch(stmt); + StmtUtilTools.StmtExecute(stmt); + StmtUtilTools.StmtClose(stmt); + + DataSource.FreeTaosMBind(mbind); + + } + } +} \ No newline at end of file diff --git a/src/connector/C#/src/test/Cases/StmtQuery.cs b/src/connector/C#/src/test/Cases/StmtQuery.cs new file mode 100644 index 0000000000..7909376ad8 --- /dev/null +++ b/src/connector/C#/src/test/Cases/StmtQuery.cs @@ -0,0 +1,49 @@ +using System; +using Test.UtilsTools; +using TDengineDriver; +using Test.UtilsTools.DataSource; + +namespace Cases +{ + public class StmtStableQuery + { + public void Test(IntPtr conn, string tableName) + { + string selectSql = "SELECT * FROM " + tableName + " WHERE v1 > ? AND v4 < ?"; + TAOS_BIND[] queryCondition = DataSource.GetQueryCondition(); + + IntPtr stmt = StmtUtilTools.StmtInit(conn); + StmtUtilTools.StmtPrepare(stmt, selectSql); + + StmtUtilTools.BindParam(stmt, queryCondition); + StmtUtilTools.StmtExecute(stmt); + IntPtr res = StmtUtilTools.StmtUseResult(stmt); + UtilsTools.DisplayRes(res); + + StmtUtilTools.StmtClose(stmt); + DataSource.FreeTaosBind(queryCondition); + + } + } + + public class StmtNtableQuery + { + public void Test(IntPtr conn, string tableName) + { + string selectSql = "SELECT * FROM " + tableName + " WHERE v1 > ? AND v4 < ?"; + TAOS_BIND[] queryCondition = DataSource.GetQueryCondition(); + + IntPtr stmt = StmtUtilTools.StmtInit(conn); + StmtUtilTools.StmtPrepare(stmt, selectSql); + + StmtUtilTools.BindParam(stmt, queryCondition); + StmtUtilTools.StmtExecute(stmt); + IntPtr res = StmtUtilTools.StmtUseResult(stmt); + UtilsTools.DisplayRes(res); + + StmtUtilTools.StmtClose(stmt); + DataSource.FreeTaosBind(queryCondition); + + } + } +} \ No newline at end of file diff --git a/src/connector/C#/src/test/Cases/StmtStable.cs b/src/connector/C#/src/test/Cases/StmtStable.cs new file mode 100644 index 0000000000..f6024909d0 --- /dev/null +++ b/src/connector/C#/src/test/Cases/StmtStable.cs @@ -0,0 +1,76 @@ +using System; +using Test.UtilsTools; +using TDengineDriver; +using Test.UtilsTools.DataSource; + +namespace Cases +{ + + public class StableMutipleLine + { + TAOS_BIND[] tags = DataSource.getTags(); + TAOS_MULTI_BIND[] mbind = DataSource.GetMultiBindArr(); + public void Test(IntPtr conn, string tableName) + { + String createTb = "create stable " + tableName + " (ts timestamp ,b bool,v1 tinyint,v2 smallint,v4 int,v8 bigint,f4 float,f8 double,u1 tinyint unsigned,u2 smallint unsigned,u4 int unsigned,u8 bigint unsigned,bin binary(200),blob nchar(200))tags(bo bool,tt tinyint,si smallint,ii int,bi bigint,tu tinyint unsigned,su smallint unsigned,iu int unsigned,bu bigint unsigned,ff float ,dd double ,bb binary(200),nc nchar(200));"; + String insertSql = "insert into ? using " + tableName + " tags(?,?,?,?,?,?,?,?,?,?,?,?,?) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; + + UtilsTools.ExecuteQuery(conn, createTb); + String[] loadList = { tableName }; + IntPtr stmt = StmtUtilTools.StmtInit(conn); + StmtUtilTools.loadTableInfo(conn, loadList); + + StmtUtilTools.StmtPrepare(stmt, insertSql); + StmtUtilTools.SetTableNameTags(stmt, tableName + "_t1", tags); + StmtUtilTools.BindParamBatch(stmt, mbind); + StmtUtilTools.AddBatch(stmt); + StmtUtilTools.StmtExecute(stmt); + + StmtUtilTools.StmtClose(stmt); + DataSource.FreeTaosBind(tags); + DataSource.FreeTaosMBind(mbind); + } + } + public class StableColumnByColumn + { + DataSource data = new DataSource(); + + TAOS_BIND[] tags = DataSource.getTags(); + TAOS_MULTI_BIND[] mbind = DataSource.GetMultiBindArr(); + public void Test(IntPtr conn, string tableName) + { + String createTb = "create stable " + tableName + " (ts timestamp ,b bool,v1 tinyint,v2 smallint,v4 int,v8 bigint,f4 float,f8 double,u1 tinyint unsigned,u2 smallint unsigned,u4 int unsigned,u8 bigint unsigned,bin binary(200),blob nchar(200))tags(bo bool,tt tinyint,si smallint,ii int,bi bigint,tu tinyint unsigned,su smallint unsigned,iu int unsigned,bu bigint unsigned,ff float ,dd double ,bb binary(200),nc nchar(200));"; + String insertSql = "insert into ? using " + tableName + " tags(?,?,?,?,?,?,?,?,?,?,?,?,?) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; + + + UtilsTools.ExecuteQuery(conn, createTb); + IntPtr stmt = StmtUtilTools.StmtInit(conn); + + StmtUtilTools.StmtPrepare(stmt, insertSql); + + StmtUtilTools.SetTableNameTags(stmt, tableName + "_t1", tags); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[0], 0); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[1], 1); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[2], 2); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[3], 3); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[4], 4); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[5], 5); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[6], 6); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[7], 7); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[8], 8); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[9], 9); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[10], 10); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[11], 11); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[12], 12); + StmtUtilTools.BindSingleParamBatch(stmt, mbind[13], 13); + StmtUtilTools.AddBatch(stmt); + StmtUtilTools.StmtExecute(stmt); + StmtUtilTools.StmtClose(stmt); + + DataSource.FreeTaosBind(tags); + DataSource.FreeTaosMBind(mbind); + + } + } + +} \ No newline at end of file diff --git a/src/connector/C#/src/test/Cases/StmtUtil.cs b/src/connector/C#/src/test/Cases/StmtUtil.cs new file mode 100644 index 0000000000..8f68157e09 --- /dev/null +++ b/src/connector/C#/src/test/Cases/StmtUtil.cs @@ -0,0 +1,198 @@ +using System; +using TDengineDriver; +using System.Runtime.InteropServices; + +namespace Test.UtilsTools +{ + public class StmtUtilTools + { + public static IntPtr StmtInit(IntPtr conn) + { + IntPtr stmt = TDengine.StmtInit(conn); + if (stmt == IntPtr.Zero) + { + Console.WriteLine("Init stmt failed"); + UtilsTools.CloseConnection(conn); + UtilsTools.ExitProgram(); + } + else + { + Console.WriteLine("Init stmt success"); + } + return stmt; + } + + public static void StmtPrepare(IntPtr stmt, string sql) + { + int res = TDengine.StmtPrepare(stmt, sql); + if (res == 0) + { + Console.WriteLine("stmt prepare success"); + } + else + { + Console.WriteLine("stmt prepare failed " + TDengine.StmtErrorStr(stmt)); + StmtClose(stmt); + } + } + + public static void SetTableName(IntPtr stmt, String tableName) + { + int res = TDengine.StmtSetTbname(stmt, tableName); + if (res == 0) + { + Console.WriteLine("set_tbname success"); + } + else + { + Console.Write("set_tbname failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(stmt); + } + } + + public static void SetTableNameTags(IntPtr stmt, String tableName, TAOS_BIND[] tags) + { + int res = TDengine.StmtSetTbnameTags(stmt, tableName, tags); + if (res == 0) + { + Console.WriteLine("set tbname && tags success"); + + } + else + { + Console.Write("set tbname && tags failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(stmt); + } + } + + public static void SetSubTableName(IntPtr stmt, string name) + { + int res = TDengine.StmtSetSubTbname(stmt, name); + if (res == 0) + { + Console.WriteLine("set subtable name success"); + } + else + { + Console.Write("set subtable name failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(stmt); + } + + } + + public static void BindParam(IntPtr stmt, TAOS_BIND[] binds) + { + int res = TDengine.StmtBindParam(stmt, binds); + if (res == 0) + { + Console.WriteLine("bind para success"); + } + else + { + Console.Write("bind para failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(stmt); + } + } + + public static void BindSingleParamBatch(IntPtr stmt, TAOS_MULTI_BIND bind, int index) + { + int res = TDengine.StmtBindSingleParamBatch(stmt, ref bind, index); + if (res == 0) + { + Console.WriteLine("single bind batch success"); + } + else + { + Console.Write("single bind batch failed: " + TDengine.StmtErrorStr(stmt)); + StmtClose(stmt); + } + } + + public static void BindParamBatch(IntPtr stmt, TAOS_MULTI_BIND[] bind) + { + int res = TDengine.StmtBindParamBatch(stmt, bind); + if (res == 0) + { + Console.WriteLine("bind parameter batch success"); + } + else + { + Console.WriteLine("bind parameter batch failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(stmt); + } + } + + public static void AddBatch(IntPtr stmt) + { + int res = TDengine.StmtAddBatch(stmt); + if (res == 0) + { + Console.WriteLine("stmt add batch success"); + } + else + { + Console.Write("stmt add batch failed,reason: " + TDengine.StmtErrorStr(stmt)); + StmtClose(stmt); + } + } + public static void StmtExecute(IntPtr stmt) + { + int res = TDengine.StmtExecute(stmt); + if (res == 0) + { + Console.WriteLine("Execute stmt success"); + } + else + { + Console.Write("Execute stmt failed,reason: " + TDengine.StmtErrorStr(stmt)); + StmtClose(stmt); + } + } + public static void StmtClose(IntPtr stmt) + { + int res = TDengine.StmtClose(stmt); + if (res == 0) + { + Console.WriteLine("close stmt success"); + } + else + { + Console.WriteLine("close stmt failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(stmt); + } + } + + public static IntPtr StmtUseResult(IntPtr stmt) + { + IntPtr res = TDengine.StmtUseResult(stmt); + if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0)) + { + if (res != IntPtr.Zero) + { + Console.Write("reason: " + TDengine.Error(res)); + } + Console.WriteLine(""); + StmtClose(stmt); + } + else + { + Console.WriteLine("StmtUseResult success"); + + } + return res; + } + + public static void loadTableInfo(IntPtr conn, string[] arr) + { + if (TDengine.LoadTableInfo(conn, arr) == 0) + { + Console.WriteLine("load table info success"); + } + else + { + Console.WriteLine("load table info failed"); + } + } + + } +} \ No newline at end of file diff --git a/src/connector/C#/src/test/Cases/TaosFeild.cs b/src/connector/C#/src/test/Cases/TaosFeild.cs new file mode 100644 index 0000000000..b8c6d37ef5 --- /dev/null +++ b/src/connector/C#/src/test/Cases/TaosFeild.cs @@ -0,0 +1,37 @@ +using System; +using Test.UtilsTools; +using TDengineDriver; +using System.Collections.Generic; +using System.Runtime.InteropServices; +namespace Cases +{ + public class FetchFields + { + public void Test(IntPtr conn, string tableName) + { + IntPtr res = IntPtr.Zero; + String createTb = "create stable " + tableName + " (ts timestamp ,b bool,v1 tinyint,v2 smallint,v4 int,v8 bigint,f4 float,f8 double,u1 tinyint unsigned,u2 smallint unsigned,u4 int unsigned,u8 bigint unsigned,bin binary(200),blob nchar(200))tags(id int);"; + String insertSql = "insert into " + tableName + "_t1 using " + tableName + " tags(1) values(1637064040000,true,1,2,3,4,5,6,7,8,9,10,'XI','XII')"; + String selectSql = "select * from " + tableName; + String dropSql = "drop table " + tableName; + UtilsTools.ExecuteQuery(conn, createTb); + UtilsTools.ExecuteQuery(conn, insertSql); + res = UtilsTools.ExecuteQuery(conn, selectSql); + UtilsTools.ExecuteQuery(conn, dropSql); + + List metas = new List(); + metas = TDengine.FetchFields(res); + if (metas.Capacity == 0) + { + Console.WriteLine("empty result"); + } + else + { + foreach(TDengineMeta meta in metas){ + Console.WriteLine("col_name:{0},col_type_code:{1},col_type:{2}({3})",meta.name,meta.type,meta.TypeName(),meta.size); + } + } + + } + } +} \ No newline at end of file diff --git a/src/connector/C#/src/test/Cases/Utils.cs b/src/connector/C#/src/test/Cases/Utils.cs new file mode 100644 index 0000000000..26414c91f9 --- /dev/null +++ b/src/connector/C#/src/test/Cases/Utils.cs @@ -0,0 +1,166 @@ +using System; +using TDengineDriver; +using System.Runtime.InteropServices; +using System.Text; +using System.Collections.Generic; +namespace Test.UtilsTools +{ + public class UtilsTools + { + + static string configDir = "C:/TDengine/cfg"; + + public static IntPtr TDConnection(string ip, string user, string password, string db, short port) + { + TDengine.Options((int)TDengineInitOption.TDDB_OPTION_CONFIGDIR, configDir); + TDengine.Options((int)TDengineInitOption.TDDB_OPTION_SHELL_ACTIVITY_TIMER, "60"); + TDengine.Init(); + return TDengine.Connect(ip, user, password, db, port); + } + + public static IntPtr ExecuteQuery(IntPtr conn, String sql) + { + 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(); + } + else + { + Console.WriteLine(sql.ToString() + " success"); + + } + return res; + } + public static void DisplayRes(IntPtr res) + { + long queryRows = 0; + if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0)) + { + if (res != IntPtr.Zero) + { + Console.Write("reason: " + TDengine.Error(res)); + } + Console.WriteLine(""); + ExitProgram(); + } + + int fieldCount = TDengine.FieldCount(res); + List 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); Console.WriteLine(""); + } + + public static void CloseConnection(IntPtr conn) + { + if (conn != IntPtr.Zero) + { + if (TDengine.Close(conn) == 0) + { + Console.WriteLine("close connection sucess"); + } + else + { + Console.WriteLine("close Connection failed"); + } + } + } + public static List getField(IntPtr res) + { + List metas = TDengine.FetchFields(res); + return metas; + } + public static void ExitProgram() + { + TDengine.Cleanup(); + System.Environment.Exit(0); + } + } +} \ No newline at end of file diff --git a/src/connector/C#/src/test/XUnitTest/TestTDengineMeta.cs b/src/connector/C#/src/test/XUnitTest/TestTDengineMeta.cs new file mode 100644 index 0000000000..fcf86c994e --- /dev/null +++ b/src/connector/C#/src/test/XUnitTest/TestTDengineMeta.cs @@ -0,0 +1,177 @@ +using System; +using Xunit; +using TDengineDriver; + +namespace TDengineDriver.Test +{ + public class TestTDengineMeta + { + [Fact] + public void TestTypeNameBool() + { + string typeName = "BOOL"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 1; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + + [Fact] + public void TestTypeNameTINYINT() + { + string typeName = "TINYINT"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 2; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameSMALLINT() + { + string typeName = "SMALLINT"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 3; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameINT() + { + string typeName = "INT"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 4; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameBIGINT() + { + string typeName = "BIGINT"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 5; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameUTINYINT() + { + string typeName = "TINYINT UNSIGNED"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 11; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameUSMALLINT() + { + string typeName = "SMALLINT UNSIGNED"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 12; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameUINT() + { + string typeName = "INT UNSIGNED"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 13; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameUBIGINT() + { + string typeName = "BIGINT UNSIGNED"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 14; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + + [Fact] + public void TestTypeNameFLOAT() + { + string typeName = "FLOAT"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 6; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameDOUBLE() + { + string typeName = "DOUBLE"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 7; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameSTRING() + { + string typeName = "STRING"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 8; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameTIMESTAMP() + { + string typeName = "TIMESTAMP"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 9; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameNCHAR() + { + string typeName = "NCHAR"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + meta.type = 10; + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + [Fact] + public void TestTypeNameUndefined() + { + string typeName = "undefine"; + TDengineDriver.TDengineMeta meta = new TDengineDriver.TDengineMeta(); + + string metaTypeName = meta.TypeName(); + + Assert.Equal(metaTypeName, typeName); + + } + } +} diff --git a/src/connector/C#/src/test/XUnitTest/TestTaosBind.cs b/src/connector/C#/src/test/XUnitTest/TestTaosBind.cs new file mode 100644 index 0000000000..208bdcc02c --- /dev/null +++ b/src/connector/C#/src/test/XUnitTest/TestTaosBind.cs @@ -0,0 +1,863 @@ +using System; +using Xunit; +using TDengineDriver; +using System.Runtime.InteropServices; + +namespace TDengineDriver.Test +{ + public class TestTaosBind + { + [Fact] + public void TestBindBoolTrue() + { + int bufferType = 1; + bool buffer = true; + int bufferLength = sizeof(bool); + int length = sizeof(bool); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindBool(true); + int BindLengPtr = Marshal.ReadInt32(bind.length); + bool bindBuffer = Convert.ToBoolean(Marshal.ReadByte(bind.buffer)); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + + } + + [Fact] + public void TestBindBoolFalse() + { + int bufferType = 1; + bool buffer = false; + int bufferLength = sizeof(bool); + int length = sizeof(bool); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindBool(false); + int BindLengPtr = Marshal.ReadInt32(bind.length); + bool bindBuffer = Convert.ToBoolean(Marshal.ReadByte(bind.buffer)); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + + } + + [Fact] + public void TestBindTinyIntZero() + { + + int bufferType = 2; + sbyte buffer = 0; + int bufferLength = sizeof(sbyte); + int length = sizeof(sbyte); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindTinyInt(0); + int BindLengPtr = Marshal.ReadInt32(bind.length); + sbyte bindBuffer = Convert.ToSByte(Marshal.ReadByte(bind.buffer)); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindTinyIntPositive() + { + + int bufferType = 2; + sbyte buffer = sbyte.MaxValue; + int bufferLength = sizeof(sbyte); + int length = sizeof(sbyte); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindTinyInt(sbyte.MaxValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + sbyte bindBuffer = Convert.ToSByte(Marshal.ReadByte(bind.buffer)); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindTinyIntNegative() + { + + int bufferType = 2; + short buffer = sbyte.MinValue; + int bufferLength = sizeof(sbyte); + int length = sizeof(sbyte); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindTinyInt(sbyte.MinValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + short bindBuffer = Marshal.ReadInt16(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindSmallIntNegative() + { + + int bufferType = 3; + short buffer = short.MinValue; + int bufferLength = sizeof(short); + int length = sizeof(short); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindSmallInt(short.MinValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + short bindBuffer = Marshal.ReadInt16(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindSmallIntZero() + { + + int bufferType = 3; + short buffer = 0; + int bufferLength = sizeof(short); + int length = sizeof(short); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindSmallInt(0); + int BindLengPtr = Marshal.ReadInt32(bind.length); + short bindBuffer = Marshal.ReadInt16(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindSmallIntPositive() + { + + int bufferType = 3; + short buffer = short.MaxValue; + int bufferLength = sizeof(short); + int length = sizeof(short); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindSmallInt(short.MaxValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + short bindBuffer = Marshal.ReadInt16(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindIntNegative() + { + + int bufferType = 4; + int buffer = int.MinValue; + int bufferLength = sizeof(int); + int length = sizeof(int); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindInt(int.MinValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + int bindBuffer = Marshal.ReadInt32(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindIntZero() + { + + int bufferType = 4; + int buffer = 0; + int bufferLength = sizeof(int); + int length = sizeof(int); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindInt(0); + int BindLengPtr = Marshal.ReadInt32(bind.length); + int bindBuffer = Marshal.ReadInt32(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindIntPositive() + { + + int bufferType = 4; + int buffer = int.MaxValue; + int bufferLength = sizeof(int); + int length = sizeof(int); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindInt(int.MaxValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + int bindBuffer = Marshal.ReadInt32(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindBigIntNegative() + { + + int bufferType = 5; + long buffer = long.MinValue; + int bufferLength = sizeof(long); + int length = sizeof(long); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindBigInt(long.MinValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + long bindBuffer = Marshal.ReadInt64(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + [Fact] + public void TestBindBigIntZero() + { + + int bufferType = 5; + long buffer = 0; + int bufferLength = sizeof(long); + int length = sizeof(long); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindBigInt(0); + int BindLengPtr = Marshal.ReadInt32(bind.length); + long bindBuffer = Marshal.ReadInt64(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindBigIntPositive() + { + + int bufferType = 5; + long buffer = long.MaxValue; + int bufferLength = sizeof(long); + int length = sizeof(long); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindBigInt(long.MaxValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + long bindBuffer = Marshal.ReadInt64(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindUTinyZero() + { + + + int bufferType = 11; + byte buffer = 0; + int bufferLength = sizeof(sbyte); + int length = sizeof(sbyte); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindUTinyInt(0); + int BindLengPtr = Marshal.ReadInt32(bind.length); + byte bindBuffer = Marshal.ReadByte(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindUTinyPositive() + { + + + int bufferType = 11; + byte buffer = byte.MaxValue; + int bufferLength = sizeof(sbyte); + int length = sizeof(sbyte); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindUTinyInt(byte.MaxValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + byte bindBuffer = Marshal.ReadByte(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindUSmallIntZero() + { + + int bufferType = 12; + ushort buffer = ushort.MinValue; + int bufferLength = sizeof(ushort); + int length = sizeof(ushort); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindUSmallInt(ushort.MinValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + ushort bindBuffer = (ushort)Marshal.ReadInt16(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + [Fact] + public void TestBindUSmallIntPositive() + { + + int bufferType = 12; + ushort buffer = ushort.MaxValue; + int bufferLength = sizeof(ushort); + int length = sizeof(ushort); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindUSmallInt(ushort.MaxValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + ushort bindBuffer = (ushort)Marshal.ReadInt16(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindUIntZero() + { + int bufferType = 13; + uint buffer = uint.MinValue; + int bufferLength = sizeof(uint); + int length = sizeof(uint); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindUInt(uint.MinValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + uint bindBuffer = (uint)Marshal.ReadInt32(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindUIntPositive() + { + int bufferType = 13; + uint buffer = uint.MaxValue; + int bufferLength = sizeof(uint); + int length = sizeof(uint); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindUInt(uint.MaxValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + uint bindBuffer = (uint)Marshal.ReadInt32(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindUBigIntZero() + { + int bufferType = 14; + ulong buffer = ulong.MinValue; + int bufferLength = sizeof(ulong); + int length = sizeof(ulong); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindUBigInt(ulong.MinValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + ulong bindBuffer = (ulong)Marshal.ReadInt64(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindUBigIntPositive() + { + int bufferType = 14; + ulong buffer = ulong.MaxValue; + int bufferLength = sizeof(ulong); + int length = sizeof(ulong); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindUBigInt(ulong.MaxValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + ulong bindBuffer = (ulong)Marshal.ReadInt64(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindFloatNegative() + { + int bufferType = 6; + float buffer = float.MinValue; + int bufferLength = sizeof(float); + int length = sizeof(float); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindFloat(float.MinValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + float[] bindBufferArr = new float[1]; + Marshal.Copy(bind.buffer, bindBufferArr, 0, bindBufferArr.Length); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBufferArr[0], buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindFloatZero() + { + int bufferType = 6; + float buffer = 0; + int bufferLength = sizeof(float); + int length = sizeof(float); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindFloat(0F); + int BindLengPtr = Marshal.ReadInt32(bind.length); + float[] bindBufferArr = new float[1]; + Marshal.Copy(bind.buffer, bindBufferArr, 0, bindBufferArr.Length); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBufferArr[0], buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindFloatPositive() + { + int bufferType = 6; + float buffer = float.MaxValue; + int bufferLength = sizeof(float); + int length = sizeof(float); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindFloat(float.MaxValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + float[] bindBufferArr = new float[1]; + Marshal.Copy(bind.buffer, bindBufferArr, 0, bindBufferArr.Length); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBufferArr[0], buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindDoubleZero() + { + int bufferType = 7; + double buffer = 0; + int bufferLength = sizeof(double); + int length = sizeof(double); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindDouble(0D); + int BindLengPtr = Marshal.ReadInt32(bind.length); + double[] bindBufferArr = new double[1]; + Marshal.Copy(bind.buffer, bindBufferArr, 0, bindBufferArr.Length); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBufferArr[0], buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindDoublePositive() + { + int bufferType = 7; + double buffer = double.MaxValue; + int bufferLength = sizeof(double); + int length = sizeof(double); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindDouble(double.MaxValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + double[] bindBufferArr = new double[1]; + Marshal.Copy(bind.buffer, bindBufferArr, 0, bindBufferArr.Length); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBufferArr[0], buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindDoubleNegative() + { + int bufferType = 7; + double buffer = double.MinValue; + int bufferLength = sizeof(double); + int length = sizeof(double); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindDouble(double.MinValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + double[] bindBufferArr = new double[1]; + Marshal.Copy(bind.buffer, bindBufferArr, 0, bindBufferArr.Length); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBufferArr[0], buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindBinaryEn() + { + int bufferType = 8; + String buffer = "qwertyuiopasdghjklzxcvbnm<>?:\"{}+_)(*&^%$#@!~QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./`1234567890-="; + int bufferLength = buffer.Length; + int length = buffer.Length; + + TDengineDriver.TAOS_BIND bind = TaosBind.BindBinary("qwertyuiopasdghjklzxcvbnm<>?:\"{}+_)(*&^%$#@!~QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./`1234567890-="); + int BindLengPtr = Marshal.ReadInt32(bind.length); + string bindBuffer = Marshal.PtrToStringAnsi(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindBinaryCn() + { + int bufferType = 8; + String buffer = "一二两三四五六七八九十廿毛另壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分零整1234567890`~!@#$%^&*()_+[]{};':<>?,./"; + int bufferLength = buffer.Length; + int length = buffer.Length; + + TDengineDriver.TAOS_BIND bind = TaosBind.BindBinary("一二两三四五六七八九十廿毛另壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分零整1234567890`~!@#$%^&*()_+[]{};':<>?,./"); + int BindLengPtr = Marshal.ReadInt32(bind.length); + string bindBuffer = Marshal.PtrToStringAnsi(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindBinaryCnAndEn() + { + int bufferType = 8; + String buffer = "一二两三四五六七八九十廿毛另壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分零整1234567890`~!@#$%^&*()_+[]{};':<>?,./qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"; + int bufferLength = buffer.Length; + int length = buffer.Length; + + TDengineDriver.TAOS_BIND bind = TaosBind.BindBinary("一二两三四五六七八九十廿毛另壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分零整1234567890`~!@#$%^&*()_+[]{};':<>?,./qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"); + int BindLengPtr = Marshal.ReadInt32(bind.length); + string bindBuffer = Marshal.PtrToStringAnsi(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindNcharEn() + { + int bufferType = 10; + String buffer = "qwertyuiopasdghjklzxcvbnm<>?:\"{}+_)(*&^%$#@!~QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./`1234567890-="; + int bufferLength = buffer.Length; + int length = buffer.Length; + + TDengineDriver.TAOS_BIND bind = TaosBind.BindNchar("qwertyuiopasdghjklzxcvbnm<>?:\"{}+_)(*&^%$#@!~QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./`1234567890-="); + int BindLengPtr = Marshal.ReadInt32(bind.length); + string bindBuffer = Marshal.PtrToStringAnsi(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + [Fact] + public void TestBindNcharCn() + { + int bufferType = 10; + String buffer = "一二两三四五六七八九十廿毛另壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分零整1234567890`~!@#$%^&*()_+[]{};':<>?,./"; + int bufferLength = buffer.Length; + int length = buffer.Length; + + TDengineDriver.TAOS_BIND bind = TaosBind.BindNchar("一二两三四五六七八九十廿毛另壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分零整1234567890`~!@#$%^&*()_+[]{};':<>?,./"); + int BindLengPtr = Marshal.ReadInt32(bind.length); + string bindBuffer = Marshal.PtrToStringAnsi(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + [Fact] + public void TestBindNcharCnAndEn() + { + int bufferType = 10; + String buffer = "一二两三四五六七八九十廿毛另壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分零整1234567890`~!@#$%^&*()_+[]{};':<>?,./qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"; + int bufferLength = buffer.Length; + int length = buffer.Length; + + TDengineDriver.TAOS_BIND bind = TaosBind.BindNchar("一二两三四五六七八九十廿毛另壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分零整1234567890`~!@#$%^&*()_+[]{};':<>?,./qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"); + int BindLengPtr = Marshal.ReadInt32(bind.length); + string bindBuffer = Marshal.PtrToStringAnsi(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindNil() + { + int bufferType = 0; + int isNull = 1; + + TDengineDriver.TAOS_BIND bind = TaosBind.BindNil(); + + int bindIsNull = Marshal.ReadInt32(bind.is_null); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindIsNull, isNull); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + [Fact] + public void TestBindTimestampNegative() + { + int bufferType = 9; + long buffer = long.MinValue; + int bufferLength = sizeof(long); + int length = sizeof(long); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindTimestamp(long.MinValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + long bindBuffer = Marshal.ReadInt64(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + [Fact] + public void TestBindTimestampZero() + { + int bufferType = 9; + long buffer = 0; + int bufferLength = sizeof(long); + int length = sizeof(long); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindTimestamp(0); + int BindLengPtr = Marshal.ReadInt32(bind.length); + long bindBuffer = Marshal.ReadInt64(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + [Fact] + public void TestBindTimestampPositive() + { + int bufferType = 9; + long buffer = long.MaxValue; + int bufferLength = sizeof(long); + int length = sizeof(long); + + TDengineDriver.TAOS_BIND bind = TaosBind.BindTimestamp(long.MaxValue); + int BindLengPtr = Marshal.ReadInt32(bind.length); + long bindBuffer = Marshal.ReadInt64(bind.buffer); + Console.WriteLine("bind.buffer_type:{0},bufferType:{1}", bind.buffer_type, bufferType); + + Assert.Equal(bind.buffer_type, bufferType); + Assert.Equal(bindBuffer, buffer); + Assert.Equal(bind.buffer_length, bufferLength); + Assert.Equal(BindLengPtr, length); + + Marshal.FreeHGlobal(bind.buffer); + Marshal.FreeHGlobal(bind.length); + } + + } +} \ No newline at end of file diff --git a/src/connector/C#/src/test/XUnitTest/XUnitTest.csproj b/src/connector/C#/src/test/XUnitTest/XUnitTest.csproj new file mode 100644 index 0000000000..97d13e5e9e --- /dev/null +++ b/src/connector/C#/src/test/XUnitTest/XUnitTest.csproj @@ -0,0 +1,30 @@ + + + + net5.0 + + false + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/tests/examples/C#/stmt/StmtDemo.cs b/tests/examples/C#/stmt/StmtDemo.cs new file mode 100644 index 0000000000..c2b2991409 --- /dev/null +++ b/tests/examples/C#/stmt/StmtDemo.cs @@ -0,0 +1,549 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +using System; +using System.Text; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Collections; +namespace TDengineDriver +{ + public class StmtDemo + { + //connection parameters + private string host = "127.0.0.1"; + private string configDir = "C:/TDengine/cfg"; + private string user = "root"; + private string passwd = "taosdata"; + private short port = 0; + + private IntPtr conn = IntPtr.Zero; + private IntPtr stmt = IntPtr.Zero; + + static void Main(string[] args) + { + string dropDB = "drop database if exists csharp"; + string createDB = "create database if not exists csharp keep 36500"; + string selectDB = "use csharp"; + string stmtSql = "insert into ? using stmtdemo tags(?,?,?,?,?,?,?,?,?,?,?,?,?) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; + string createTable = "create stable stmtdemo (ts timestamp " + + ",b bool" + + ",v1 tinyint" + + ",v2 smallint" + + ",v4 int" + + ",v8 bigint" + + ",f4 float" + + ",f8 double" + + ",u1 tinyint unsigned" + + ",u2 smallint unsigned" + + ",u4 int unsigned" + + ",u8 bigint unsigned" + + ",bin binary(200)" + + ",blob nchar(200)" + + ")tags(" + + "bo bool" + + ",tt tinyint" + + ",si smallint" + + ",ii int" + + ",bi bigint" + + ",tu tinyint unsigned" + + ",su smallint unsigned" + + ",iu int unsigned" + + ",bu bigint unsigned" + + ",ff float " + + ",dd double " + + ",bb binary(200)" + + ",nc nchar(200)" + + ")"; + + string dropTable = "drop table if exists stmtdemo"; + + string tableName = "t1"; + StmtDemo stmtDemo = new StmtDemo(); + Console.WriteLine("---------------------------------------------------------------"); + Console.WriteLine("Start StmtDemo insert Testing..."); + Console.WriteLine("---------------------------------------------------------------"); + stmtDemo.InitTDengine(); + //TDengine connect + stmtDemo.ConnectTDengine(); + + //before stmt + stmtDemo.ExecuteQuery(dropDB); + stmtDemo.ExecuteQuery(createDB); + stmtDemo.ExecuteQuery(selectDB); + stmtDemo.ExecuteQuery(dropTable); + stmtDemo.ExecuteQuery(createTable); + + stmtDemo.StmtInit(); + string[] tableList = { "stmtdemo" }; + stmtDemo.loadTableInfo(tableList); + + stmtDemo.StmtPrepare(stmtSql); + TAOS_BIND[] binds = stmtDemo.InitBindArr(); + TAOS_MULTI_BIND[] mbinds = stmtDemo.InitMultBindArr(); + stmtDemo.SetTableNameTags(tableName, binds); + stmtDemo.BindParamBatch(mbinds); + stmtDemo.AddBatch(); + stmtDemo.StmtExecute(); + TaosBind.FreeTaosBind(binds); + TaosMultiBind.FreeTaosBind(mbinds); + stmtDemo.StmtClose(); + + Console.WriteLine("---------------------------------------------------------------"); + Console.WriteLine("start StmtDemo select Testing..."); + Console.WriteLine("---------------------------------------------------------------"); + + stmtDemo.StmtInit(); + string selectSql = "SELECT * FROM stmtdemo WHERE v1 > ? AND v4 < ?"; + + stmtDemo.StmtPrepare(selectSql); + + TAOS_BIND[] queryCondition = new TAOS_BIND[2]; + queryCondition[0] = TaosBind.BindTinyInt(0); + queryCondition[1] = TaosBind.BindInt(1000); + + Console.WriteLine(selectSql); + stmtDemo.BindParam(queryCondition); + stmtDemo.StmtExecute(); + + stmtDemo.StmtUseResult(); + + stmtDemo.StmtClose(); + TaosBind.FreeTaosBind(queryCondition); + Console.WriteLine("---------------------------------------------------------------"); + Console.WriteLine("Stop StmtDemo Testing..."); + Console.WriteLine("---------------------------------------------------------------"); + + stmtDemo.CloseConnection(); + } + public TAOS_BIND[] InitBindArr() + { + TAOS_BIND[] binds = new TAOS_BIND[13]; + binds[0] = TaosBind.BindBool(true); + binds[1] = TaosBind.BindTinyInt(-2); + binds[2] = TaosBind.BindSmallInt(short.MaxValue); + binds[3] = TaosBind.BindInt(int.MaxValue); + binds[4] = TaosBind.BindBigInt(Int64.MaxValue); + binds[5] = TaosBind.BindUTinyInt(byte.MaxValue - 1); + binds[6] = TaosBind.BindUSmallInt(UInt16.MaxValue - 1); + binds[7] = TaosBind.BindUInt(uint.MinValue + 1); + binds[8] = TaosBind.BindUBigInt(UInt64.MinValue + 1); + binds[9] = TaosBind.BindFloat(11.11F); + binds[10] = TaosBind.BindDouble(22.22D); + binds[11] = TaosBind.BindBinary("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM`1234567890-=+_)(*&^%$#@!~[];,./<>?:{}"); + binds[12] = TaosBind.BindNchar("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKZXCVBNM`1234567890-=+_)(*&^%$#@!~[];,./<>?:{}"); + return binds; + } + + public TAOS_MULTI_BIND[] InitMultBindArr() + { + TAOS_MULTI_BIND[] mBinds = new TAOS_MULTI_BIND[14]; + long[] tsArr = new long[5] { 1637064040000, 1637064041000, 1637064042000, 1637064043000, 1637064044000 }; + bool?[] boolArr = new bool?[5] { true, false, null, true, true }; + sbyte?[] tinyIntArr = new sbyte?[5] { -127, 0, null, 8, 127 }; + short?[] shortArr = new short?[5] { short.MinValue + 1, -200, null, 100, short.MaxValue }; + int?[] intArr = new int?[5] { -200, -100, null, 0, 300 }; + long?[] longArr = new long?[5] { long.MinValue + 1, -2000, null, 1000, long.MaxValue }; + float?[] floatArr = new float?[5] { float.MinValue + 1, -12.1F, null, 0F, float.MaxValue }; + double?[] doubleArr = new double?[5] { double.MinValue + 1, -19.112D, null, 0D, double.MaxValue }; + byte?[] uTinyIntArr = new byte?[5] { byte.MinValue, 12, null, 89, byte.MaxValue - 1 }; + ushort?[] uShortArr = new ushort?[5] { ushort.MinValue, 200, null, 400, ushort.MaxValue - 1 }; + uint?[] uIntArr = new uint?[5] { uint.MinValue, 100, null, 2, uint.MaxValue - 1 }; + ulong?[] uLongArr = new ulong?[5] { ulong.MinValue, 2000, null, 1000, long.MaxValue - 1 }; + string[] binaryArr = new string[5] { "1234567890~!@#$%^&*()_+=-`[]{}:,./<>?", String.Empty, null, "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890~!@#$%^&*()_+=-`[]{}:,./<>?" }; + string[] ncharArr = new string[5] { "1234567890~!@#$%^&*()_+=-`[]{}:,./<>?", null, "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890~!@#$%^&*()_+=-`[]{}:,./<>?", string.Empty }; + mBinds[0] = TaosMultiBind.MultiBindTimestamp(tsArr); + mBinds[1] = TaosMultiBind.MultiBindBool(boolArr); + mBinds[2] = TaosMultiBind.MultiBindTinyInt(tinyIntArr); + mBinds[3] = TaosMultiBind.MultiBindSmallInt(shortArr); + mBinds[4] = TaosMultiBind.MultiBindInt(intArr); + mBinds[5] = TaosMultiBind.MultiBindBigint(longArr); + mBinds[6] = TaosMultiBind.MultiBindFloat(floatArr); + mBinds[7] = TaosMultiBind.MultiBindDouble(doubleArr); + mBinds[8] = TaosMultiBind.MultiBindUTinyInt(uTinyIntArr); + mBinds[9] = TaosMultiBind.MultiBindUSmallInt(uShortArr); + mBinds[10] = TaosMultiBind.MultiBindUInt(uIntArr); + mBinds[11] = TaosMultiBind.MultiBindUBigInt(uLongArr); + mBinds[12] = TaosMultiBind.MultiBindBinary(binaryArr); + mBinds[13] = TaosMultiBind.MultiBindNchar(ncharArr); + return mBinds; + } + + public void loadTableInfo(string[] arr) + { + if (TDengine.LoadTableInfo(this.conn, arr) == 0) + { + Console.WriteLine("load table info success"); + } + else + { + Console.WriteLine("load table info failed"); + ExitProgram(); + } + } + + 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("TDengine Initialization finished"); + } + + public void ConnectTDengine() + { + string db = ""; + this.conn = TDengine.Connect(this.host, this.user, this.passwd, db, this.port); + if (this.conn == IntPtr.Zero) + { + Console.WriteLine("Connect to TDengine failed"); + ExitProgram(); + } + else + { + Console.WriteLine("Connect to TDengine success"); + } + } + + public void ExecuteQuery(String sql) + { + 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(); + } + else + { + Console.WriteLine(sql.ToString() + " success"); + } + TDengine.FreeResult(res); + } + + public void StmtInit() + { + this.stmt = TDengine.StmtInit(conn); + if (this.stmt == IntPtr.Zero) + { + Console.WriteLine("Init stmt failed"); + ExitProgram(); + } + else + { + Console.WriteLine("Init stmt success"); + } + } + + public void StmtPrepare(string sql) + { + int res = TDengine.StmtPrepare(this.stmt, sql); + if (res == 0) + { + Console.WriteLine("stmt prepare success"); + } + else + { + Console.WriteLine("stmt prepare failed " + TDengine.StmtErrorStr(stmt)); + ExitProgram(); + } + } + + public void SetTableName(String tableName) + { + int res = TDengine.StmtSetTbname(this.stmt, tableName); + Console.WriteLine("setTableName():" + res); + if (res == 0) + { + Console.WriteLine("set_tbname success"); + } + else + { + Console.Write("set_tbname failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(); + ExitProgram(); + } + } + + public void SetTableNameTags(String tableName, TAOS_BIND[] tags) + { + int res = TDengine.StmtSetTbnameTags(this.stmt, tableName, tags); + if (res == 0) + { + Console.WriteLine("set tbname && tags success"); + + } + else + { + Console.Write("set tbname && tags failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(); + ExitProgram(); + } + } + + public void SetSubTableName(string name) + { + int res = TDengine.StmtSetSubTbname(this.stmt, name); + if (res == 0) + { + Console.WriteLine("set subtable name success"); + } + else + { + Console.Write("set subtable name failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(); + ExitProgram(); + } + + } + + public void BindParam(TAOS_BIND[] binds) + { + Console.WriteLine("in bindParam()"); + + int res = TDengine.StmtBindParam(this.stmt, binds); + if (res == 0) + { + Console.WriteLine("bind para success"); + } + else + { + Console.Write("bind para failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(); + ExitProgram(); + } + } + + public void BindSingleParamBatch(TAOS_MULTI_BIND bind, int index) + { + int res = TDengine.StmtBindSingleParamBatch(this.stmt,ref bind, index); + if (res == 0) + { + Console.WriteLine("single bind batch success"); + } + else + { + Console.Write("single bind batch failed: " + TDengine.StmtErrorStr(stmt)); + StmtClose(); + ExitProgram(); + } + } + + public void BindParamBatch(TAOS_MULTI_BIND[] bind) + { + int res = TDengine.StmtBindParamBatch(this.stmt, bind); + if (res == 0) + { + Console.WriteLine("bind parameter batch success"); + } + else + { + Console.WriteLine("bind parameter batch failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(); + ExitProgram(); + } + } + + public void AddBatch() + { + int res = TDengine.StmtAddBatch(this.stmt); + if (res == 0) + { + Console.WriteLine("stmt add batch success"); + } + else + { + Console.Write("stmt add batch failed,reason: " + TDengine.StmtErrorStr(stmt)); + StmtClose(); + ExitProgram(); + } + } + public void StmtExecute() + { + int res = TDengine.StmtExecute(this.stmt); + if (res == 0) + { + Console.WriteLine("Execute stmt success"); + } + else + { + Console.Write("Execute stmt failed,reason: " + TDengine.StmtErrorStr(stmt)); + StmtClose(); + ExitProgram(); + } + } + public void StmtClose() + { + int res = TDengine.StmtClose(this.stmt); + if (res == 0) + { + Console.WriteLine("close stmt success"); + } + else + { + Console.WriteLine("close stmt failed, " + TDengine.StmtErrorStr(stmt)); + StmtClose(); + ExitProgram(); + } + } + public void CloseConnection() + { + if (this.conn != IntPtr.Zero) + { + if (TDengine.Close(this.conn) == 0) + { + Console.WriteLine("close connection sucess"); + } + else + { + Console.WriteLine("close Connection failed"); + } + } + } + + //select only + public void StmtUseResult() + { + IntPtr res = TDengine.StmtUseResult(this.stmt); + if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0)) + { + if (res != IntPtr.Zero) + { + Console.Write("reason: " + TDengine.Error(res)); + } + Console.WriteLine(""); + StmtClose(); + CloseConnection(); + ExitProgram(); + } + else + { + Console.WriteLine("{0},query success"); + DisplayRes(res); + TDengine.FreeResult(res); + } + + } + + public void DisplayRes(IntPtr res) + { + + long queryRows = 0; + if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0)) + { + if (res != IntPtr.Zero) + { + Console.Write("reason: " + TDengine.Error(res)); + } + Console.WriteLine(""); + ExitProgram(); + } + + int fieldCount = TDengine.FieldCount(res); + List 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(""); + + } + public static void ExitProgram() + { + TDengine.Cleanup(); + System.Environment.Exit(0); + } + } +} diff --git a/tests/examples/C#/stmt/stmt.csproj b/tests/examples/C#/stmt/stmt.csproj new file mode 100644 index 0000000000..bc14850edb --- /dev/null +++ b/tests/examples/C#/stmt/stmt.csproj @@ -0,0 +1,12 @@ + + + + + + + + Exe + net5.0 + + + -- GitLab