Ejemplo n.º 1
0
            protected override void OnDispose(IntPtr value)
            {
#if DEBUG
                XTrace.WriteLine("断开TDengine");
#endif
                TD.Close(value);
            }
Ejemplo n.º 2
0
 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");
 }
Ejemplo n.º 3
0
 public void CloseConnection()
 {
     if (this.conn != IntPtr.Zero)
     {
         TDengine.Close(this.conn);
         Console.WriteLine("connection closed.");
     }
 }
Ejemplo n.º 4
0
 public void InitTDengine()
 {
     TDengine.Options((int)TDengineInitOption.TDDB_OPTION_CONFIGDIR, this.configDir);
     TDengine.Options((int)TDengineInitOption.TDDB_OPTION_SHELL_ACTIVITY_TIMER, "60");
     Console.WriteLine("init...");
     TDengine.Init();
     Console.WriteLine("get connection starting...");
 }
Ejemplo n.º 5
0
        internal TDengineDataReader(TDengineCommand command, CommandBehavior behavior, IntPtr handler)
        {
            _command  = command;
            _behavior = behavior;
            _handler  = handler;

            _metas      = TD.FetchFields(handler);
            _fieldCount = TD.FieldCount(handler);
            _rows       = TD.AffectRows(handler);
        }
Ejemplo n.º 6
0
        /// <summary>读取</summary>
        /// <returns></returns>
        public override Boolean Read()
        {
            if (_handler == IntPtr.Zero)
            {
                throw new InvalidOperationException("读取器已关闭");
            }

            _data = TD.FetchRows(_handler);
            return(_data != IntPtr.Zero);
        }
Ejemplo n.º 7
0
        /// <summary>关闭读取器</summary>
        public override void Close()
        {
            if (_behavior.HasFlag(CommandBehavior.CloseConnection))
            {
                _command.Connection.Close();
            }

            if (_handler != IntPtr.Zero)
            {
                TD.FreeResult(_handler);
            }
            _handler = IntPtr.Zero;
        }
Ejemplo n.º 8
0
        public void ExecuteInsert()
        {
            if (!this.isInsertData)
            {
                return;
            }

            System.DateTime start     = new System.DateTime();
            long            loopCount = this.totalRows / this.batchRows;

            for (int table = 0; table < this.tableCount; ++table)
            {
                for (long loop = 0; loop < loopCount; loop++)
                {
                    StringBuilder sql = new StringBuilder();
                    sql.Append("insert into ").Append(this.tablePrefix).Append(table).Append(" values");
                    for (int batch = 0; batch < this.batchRows; ++batch)
                    {
                        long rows = loop * this.batchRows + batch;
                        sql.Append("(")
                        .Append(this.beginTimestamp + rows)
                        .Append(", 1, 2, 3,")
                        .Append(rows)
                        .Append(", 5, 6, 7, 'abc', 'def')");
                    }
                    IntPtr res = TDengine.Query(this.conn, sql.ToString());
                    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("");
                    }

                    int affectRows = TDengine.AffectRows(res);
                    this.rowsInserted += affectRows;

                    TDengine.FreeResult(res);
                }
            }

            System.DateTime end = new System.DateTime();
            TimeSpan        ts  = end - start;

            Console.Write("Total {0:G} rows inserted, {1:G} rows failed, time spend {2:G} seconds.\n"
                          , this.rowsInserted, this.totalRows * this.tableCount - this.rowsInserted, ts.TotalSeconds);
        }
Ejemplo n.º 9
0
        public void ConnectTDengine()
        {
            string db = "";

            this.conn = TDengine.Connect(this.host, this.user, this.password, db, this.port);
            if (this.conn == IntPtr.Zero)
            {
                Console.WriteLine("connection failed: " + this.host);
                ExitProgram();
            }
            else
            {
                Console.WriteLine("[ OK ] Connection established.");
            }
        }
Ejemplo n.º 10
0
        public void ConnectTDengine()
        {
            string db = "";

            this.conn = TDengine.Connect(this.host, this.user, this.password, db, this.port);
            if (this.conn == IntPtr.Zero)
            {
                Console.WriteLine("Connect to TDengine failed");
                ExitProgram();
            }
            else
            {
                Console.WriteLine("Connect to TDengine success");
            }
        }
Ejemplo n.º 11
0
        public void ConnectTDengine()
        {
            string db = "";

            VerbosePrintFormat("host:{0} user:{1}, pass:{2}; db:{3}, port:{4}\n",
                               this.host, this.user, this.password, db, this.port);
            this.conn = TDengine.Connect(this.host, this.user, this.password, db, this.port);
            if (this.conn == IntPtr.Zero)
            {
                Console.WriteLine("Connect to TDengine failed");
                CleanAndExitProgram(1);
            }
            else
            {
                VerbosePrint("Connect to TDengine success\n");
            }
        }
Ejemplo n.º 12
0
        public void dropDatabase()
        {
            StringBuilder sql = new StringBuilder();

            sql.Append("DROP DATABASE IF EXISTS ").Append(this.dbName);
            IntPtr res = TDengine.Query(this.conn, sql.ToString());

            if (res != IntPtr.Zero)
            {
                VerbosePrint(sql.ToString() + " success\n");
            }
            else
            {
                Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
                CleanAndExitProgram(1);
            }
        }
Ejemplo n.º 13
0
        public void execute(string sql)
        {
            DateTime dt1  = DateTime.Now;
            IntPtr   res  = TDengine.Query(this.conn, sql.ToString());
            DateTime dt2  = DateTime.Now;
            TimeSpan span = dt2 - dt1;

            if (res != IntPtr.Zero)
            {
                Console.WriteLine("[OK] time cost: " + span.ToString() + "ms, execute statement ====> " + sql.ToString());
            }
            else
            {
                Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
                ExitProgram();
            }
            TDengine.FreeResult(res);
        }
Ejemplo n.º 14
0
        public void CreateDb()
        {
            StringBuilder sql = new StringBuilder();

            sql.Append("CREATE DATABASE IF NOT EXISTS ").Append(this.dbName).Append(" replica ").Append(this.replica);
            IntPtr res = TDengine.Query(this.conn, sql.ToString());

            if (res != IntPtr.Zero)
            {
                VerbosePrint(sql.ToString() + " success\n");
            }
            else
            {
                Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
                CleanAndExitProgram(1);
            }
            TDengine.FreeResult(res);
        }
Ejemplo n.º 15
0
        // method to get db precision
        public void getPrecision(IntPtr res)
        {
            int psc = TDengine.ResultPrecision(res);

            switch (psc)
            {
            case 0:
                Console.WriteLine("db:[{0:G}]'s precision is {1:G}", this.dbName, "millisecond");
                break;

            case 1:
                Console.WriteLine("db:[{0:G}]'s precision is {1:G}", this.dbName, "microsecond");
                break;

            case 2:
                Console.WriteLine("db:[{0:G}]'s precision is {1:G}", this.dbName, "nanosecond");
                break;
            }
        }
Ejemplo n.º 16
0
        public void CreateStable()
        {
            StringBuilder sql = new StringBuilder();

            sql.Clear();
            sql.Append("CREATE TABLE IF NOT EXISTS ").
            Append(this.dbName).Append(".").Append(this.stablePrefix).
            Append("(ts timestamp, v1 bool, v2 tinyint, v3 smallint, v4 int, v5 bigint, v6 float, v7 double, v8 binary(10), v9 nchar(10)) tags(t1 int)");
            IntPtr res = TDengine.Query(this.conn, sql.ToString());

            if (res != IntPtr.Zero)
            {
                VerbosePrint(sql.ToString() + " success\n");
            }
            else
            {
                Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
                CleanAndExitProgram(1);
            }
            TDengine.FreeResult(res);
        }
Ejemplo n.º 17
0
            public void ThreadMain()
            {
                VerbosePrintFormat("CreateTable {0} from {1} to {2}\n", id, start, end);

                StringBuilder sql = new StringBuilder();

                for (long tableId = start; tableId <= end; tableId++)
                {
                    sql.Clear();
                    sql = sql.Append("CREATE TABLE IF NOT EXISTS ").
                          Append(this.dbName).Append(".").Append(this.tablePrefix).Append(tableId);
                    if (useStable == true)
                    {
                        sql = sql.Append(" USING ").Append(this.dbName).Append(".").Append(this.stablePrefix).
                              Append(" TAGS(").Append(tableId).Append(")");
                    }
                    else
                    {
                        sql = sql.Append("(ts timestamp, v1 bool, v2 tinyint, v3 smallint, v4 int, v5 bigint, v6 float, v7 double, v8 binary(10), v9 nchar(10))");
                    }
                    IntPtr res = TDengine.Query(this.conn, sql.ToString());
                    if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
                    {
                        Console.Write(sql.ToString() + " failure, ");
                        if (res != IntPtr.Zero)
                        {
                            Console.Write("reason: " + TDengine.Error(res));
                        }
                        Console.WriteLine("");
                        CleanAndExitProgram(1);
                    }
                    else
                    {
                        VerbosePrint(sql.ToString() + " success\n");
                    }
                    TDengine.FreeResult(res);
                }
            }
Ejemplo n.º 18
0
        public void ExecuteInsert()
        {
            if (!this.isInsertData)
            {
                return;
            }

            System.DateTime start     = new System.DateTime();
            long            loopCount = this.totalRows / this.batchRows;

            for (int table = 0; table < this.tableCount; ++table)
            {
                for (long loop = 0; loop < loopCount; loop++)
                {
                    StringBuilder sql = new StringBuilder();
                    sql.Append("insert into ").Append(this.tablePrefix).Append(table).Append(" values");
                    for (int batch = 0; batch < this.batchRows; ++batch)
                    {
                        long rows = loop * this.batchRows + batch;
                        sql.Append("(").Append(this.beginTimestamp + rows).Append(",").Append(rows).Append(")");
                    }
                    int code = TDengine.Query(conn, sql.ToString());
                    if (code != TDengine.TSDB_CODE_SUCCESS)
                    {
                        Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(conn));
                    }

                    int affectRows = TDengine.AffectRows(conn);
                    this.rowsInserted += affectRows;
                }
            }

            System.DateTime end = new System.DateTime();
            TimeSpan        ts  = end - start;

            Console.Write("Total {0:G} rows inserted, {1:G} rows failed, time spend {2:G} seconds.\n"
                          , this.rowsInserted, this.totalRows * this.tableCount - this.rowsInserted, ts.TotalSeconds);
        }
Ejemplo n.º 19
0
        public void execute(string sql)
        {
            DateTime dt1  = DateTime.Now;
            IntPtr   res  = TDengine.Query(this.conn, sql.ToString());
            DateTime dt2  = DateTime.Now;
            TimeSpan span = dt2 - dt1;

            if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
            {
                Console.Write(sql.ToString() + " failure, ");
                if (res != IntPtr.Zero)
                {
                    Console.Write("reason: " + TDengine.Error(res));
                }
                Console.WriteLine("");
                ExitProgram();
            }
            else
            {
                Console.WriteLine(sql.ToString() + " success");
            }
            TDengine.FreeResult(res);
        }
Ejemplo n.º 20
0
        static TDengineConnection()
        {
            var configPath = "C:/TDengine/cfg";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                configPath = "/etc/taos";
            }

            TD.Options((Int32)TDengineInitOption.TDDB_OPTION_CONFIGDIR, configPath);
            TD.Options((Int32)TDengineInitOption.TDDB_OPTION_SHELL_ACTIVITY_TIMER, "60");
            TD.Init();

            var h = TD.GetClientInfo();

            if (h != IntPtr.Zero)
            {
                var str = Marshal.PtrToStringAnsi(h);
                XTrace.WriteLine("TDengine v{0}", str);
            }

            AppDomain.CurrentDomain.DomainUnload += (s, e) => TD.Cleanup();
        }
Ejemplo n.º 21
0
            protected override IntPtr OnCreate()
            {
                var connStr = ConnectionString;

                if (connStr.IsNullOrEmpty())
                {
                    throw new InvalidOperationException("未设置连接字符串");
                }

                var builder    = new ConnectionStringBuilder(connStr);
                var dataSource = builder["DataSource"] ?? builder["Server"];
                var port       = builder["Port"].ToInt();
                //if (port <= 0) port = 6030;

                var user = builder["username"] ?? builder["user"] ?? builder["uid"];
                var pass = builder["password"] ?? builder["pass"] ?? builder["pwd"];
                var db   = builder["database"] ?? builder["db"];

                var uri = new NetUri(dataSource);

                if (port > 0)
                {
                    uri.Port = port;
                }
#if DEBUG
                XTrace.WriteLine("连接TDengine:server={0};user={1};pass={2};db={3}", dataSource, user, pass, db);
#endif

                var handler = TD.Connect(uri.Address + "", user, pass, db, (Int16)uri.Port);
                if (handler == IntPtr.Zero)
                {
                    throw new XCodeException("打开数据库连接失败!");
                }

                return(handler);
            }
Ejemplo n.º 22
0
            public void ThreadMain()
            {
                VerbosePrintFormat("InsertDataThread {0} from {1} to {2}\n", id, start, end);
                StringBuilder sql = new StringBuilder();

                DateTime now = DateTime.Now;
                int      h   = now.Hour;
                int      m   = now.Minute;
                int      s   = now.Second;

                long baseTimestamp = 1609430400000;    // 2021/01/01 0:0:0

                VerbosePrintFormat("beginTime is {0} + {1}h:{2}m:{3}s\n", baseTimestamp, h, m, s);
                long   beginTimestamp = baseTimestamp + ((h * 60 + m) * 60 + s) * 1000;
                Random random         = new Random();

                long rowsInserted = 0;

                long i = 0;

                while (i < recordsPerTable)
                {
                    for (long table = start; table <= end; ++table)
                    {
                        long inserted = i;

                        sql.Clear();
                        sql.Append("INSERT INTO ").
                        Append(this.dbName).Append(".").Append(this.tablePrefix).Append(table).
                        Append(" VALUES");
                        if (recordsPerTable < batchRows)
                        {
                            batchRows = recordsPerTable;
                        }
                        for (int batch = 0; batch < batchRows; ++batch)
                        {
                            long writeTimeStamp = beginTimestamp + i + batch;
                            int  rnd            = 100;
                            if (this.order == false)
                            {
                                rnd = random.Next(1, 100);
                                if (rnd <= this.rateOfOutorder)
                                {
                                    writeTimeStamp = writeTimeStamp + rnd * 10000;
                                    DebugPrint("### ");
                                }
                                DebugPrintFormat("order:{0} rnd:{1} timestamp:{2}\n", this.order, rnd, writeTimeStamp);
                            }
                            else
                            {
                                DebugPrintFormat("order:{0} timestamp:{1}\n", this.order, writeTimeStamp);
                            }

                            sql.Append("(")
                            .Append(writeTimeStamp)
                            .Append(", 1, 2, 3,")
                            .Append(i + batch)
                            .Append(", 5, 6, 7, 'abc', 'def')");
                        }
                        IntPtr res = TDengine.Query(this.conn, sql.ToString());
                        if (res == IntPtr.Zero)
                        {
                            VerbosePrint(sql.ToString() + " failure, reason: " + TDengine.Error(res) + "\n");
                        }

                        inserted += this.batchRows;

                        int affectRows = TDengine.AffectRows(res);
                        rowsInserted += affectRows;

                        TDengine.FreeResult(res);
                        if (table == end)
                        {
                            i = inserted;
                        }
                    }
                }
            }
Ejemplo n.º 23
0
        public void ExecuteQuery()
        {
            long queryRows = 0;

            for (int i = 0; i < this.numOfTables; ++i)
            {
                string sql;

                if (query == "DEFAULT")
                {
                    sql = "select * from " + this.dbName + "." + tablePrefix + i;
                }
                else
                {
                    sql = query;
                }
                DebugPrintFormat("query: {0}, sql:{1}\n", query, sql);

                IntPtr res = TDengine.Query(conn, sql);
                DebugPrintFormat("res: {0}\n", res);
                if (res == IntPtr.Zero)
                {
                    Console.WriteLine(sql + " failure, reason: " + TDengine.Error(res));
                    CleanAndExitProgram(1);
                }

                int fieldCount = TDengine.FieldCount(res);
                DebugPrint("field count: " + fieldCount + "\n");

                List <TDengineMeta> metas = TDengine.FetchFields(res);
                for (int j = 0; j < metas.Count; j++)
                {
                    TDengineMeta meta = (TDengineMeta)metas[j];
                    DebugPrint("index:" + j + ", type:" + meta.type + ", typename:" + meta.TypeName() + ", name:" + meta.name + ", size:" + meta.size + "\n");
                }

                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("---");

                    VerbosePrint(builder.ToString() + "\n");
                    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);
            }
        }
Ejemplo n.º 24
0
        public void CreateDbAndTable()
        {
            if (!this.isInsertData)
            {
                return;
            }

            StringBuilder sql = new StringBuilder();

            sql.Append("create database if not exists ").Append(this.dbName);
            int code = TDengine.Query(this.conn, sql.ToString());

            if (code == TDengine.TSDB_CODE_SUCCESS)
            {
                Console.WriteLine(sql.ToString() + " success");
            }
            else
            {
                Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(conn));
                ExitProgram();
            }

            sql.Clear();
            sql.Append("use ").Append(this.dbName);
            code = TDengine.Query(this.conn, sql.ToString());
            if (code == TDengine.TSDB_CODE_SUCCESS)
            {
                Console.WriteLine(sql.ToString() + " success");
            }
            else
            {
                Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(this.conn));
                ExitProgram();
            }

            sql.Clear();
            sql.Append("create table if not exists ").Append(this.stableName).Append("(ts timestamp, v1 int) tags(t1 int)");
            code = TDengine.Query(this.conn, sql.ToString());
            if (code == TDengine.TSDB_CODE_SUCCESS)
            {
                Console.WriteLine(sql.ToString() + " success");
            }
            else
            {
                Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(this.conn));
                ExitProgram();
            }

            for (int i = 0; i < this.tableCount; i++)
            {
                sql.Clear();
                sql = sql.Append("create table if not exists ").Append(this.tablePrefix).Append(i)
                      .Append(" using ").Append(this.stableName).Append(" tags(").Append(i).Append(")");
                code = TDengine.Query(this.conn, sql.ToString());
                if (code == TDengine.TSDB_CODE_SUCCESS)
                {
                    Console.WriteLine(sql.ToString() + " success");
                }
                else
                {
                    Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(this.conn));
                    ExitProgram();
                }
            }

            Console.WriteLine("create db and table success");
        }
Ejemplo n.º 25
0
        public void CreateDbAndTable()
        {
            if (!this.isInsertData)
            {
                return;
            }

            StringBuilder sql = new StringBuilder();

            sql.Append("create database if not exists ").Append(this.dbName);
            IntPtr res = TDengine.Query(this.conn, sql.ToString());

            if ((res == IntPtr.Zero) || (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);

            sql.Clear();
            sql.Append("use ").Append(this.dbName);
            res = TDengine.Query(this.conn, sql.ToString());
            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);

            sql.Clear();
            sql.Append("create table if not exists ").Append(this.stableName).Append("(ts timestamp, v1 bool, v2 tinyint, v3 smallint, v4 int, v5 bigint, v6 float, v7 double, v8 binary(10), v9 nchar(10)) tags(t1 int)");
            res = TDengine.Query(this.conn, sql.ToString());
            if ((res == IntPtr.Zero) || (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);

            for (int i = 0; i < this.tableCount; i++)
            {
                sql.Clear();
                sql = sql.Append("create table if not exists ").Append(this.tablePrefix).Append(i)
                      .Append(" using ").Append(this.stableName).Append(" tags(").Append(i).Append(")");
                res = TDengine.Query(this.conn, sql.ToString());
                if ((res == IntPtr.Zero) || (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);
            }

            Console.WriteLine("create db and table success");
        }
Ejemplo n.º 26
0
        public void ExecuteQuery()
        {
            if (!this.isQueryData)
            {
                return;
            }

            System.DateTime start     = new System.DateTime();
            long            queryRows = 0;

            for (int i = 0; i < 1 /*this.tableCount*/; ++i)
            {
                String sql = "select * from " + this.dbName + "." + tablePrefix + i;
                Console.WriteLine(sql);

                IntPtr res = TDengine.Query(conn, sql);
                if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
                {
                    Console.Write(sql.ToString() + " failure, ");
                    if (res != IntPtr.Zero)
                    {
                        Console.Write("reason: " + TDengine.Error(res));
                    }
                    Console.WriteLine("");
                    ExitProgram();
                }

                int fieldCount = TDengine.FieldCount(res);
                Console.WriteLine("field count: " + fieldCount);

                List <TDengineMeta> metas = TDengine.FetchFields(res);
                for (int j = 0; j < metas.Count; j++)
                {
                    TDengineMeta meta = (TDengineMeta)metas[j];
                    Console.WriteLine("index:" + j + ", type:" + meta.type + ", typename:" + meta.TypeName() + ", name:" + meta.name + ", size:" + meta.size);
                }

                IntPtr        rowdata;
                StringBuilder builder = new StringBuilder();
                while ((rowdata = TDengine.FetchRows(res)) != IntPtr.Zero)
                {
                    queryRows++;
                    for (int fields = 0; fields < fieldCount; ++fields)
                    {
                        TDengineMeta meta   = metas[fields];
                        int          offset = IntPtr.Size * fields;
                        IntPtr       data   = Marshal.ReadIntPtr(rowdata, offset);

                        builder.Append("---");

                        if (data == IntPtr.Zero)
                        {
                            builder.Append("NULL");
                            continue;
                        }

                        switch ((TDengineDataType)meta.type)
                        {
                        case TDengineDataType.TSDB_DATA_TYPE_BOOL:
                            bool v1 = Marshal.ReadByte(data) == 0 ? false : true;
                            builder.Append(v1);
                            break;

                        case TDengineDataType.TSDB_DATA_TYPE_TINYINT:
                            byte v2 = Marshal.ReadByte(data);
                            builder.Append(v2);
                            break;

                        case TDengineDataType.TSDB_DATA_TYPE_SMALLINT:
                            short v3 = Marshal.ReadInt16(data);
                            builder.Append(v3);
                            break;

                        case TDengineDataType.TSDB_DATA_TYPE_INT:
                            int v4 = Marshal.ReadInt32(data);
                            builder.Append(v4);
                            break;

                        case TDengineDataType.TSDB_DATA_TYPE_BIGINT:
                            long v5 = Marshal.ReadInt64(data);
                            builder.Append(v5);
                            break;

                        case TDengineDataType.TSDB_DATA_TYPE_FLOAT:
                            float v6 = (float)Marshal.PtrToStructure(data, typeof(float));
                            builder.Append(v6);
                            break;

                        case TDengineDataType.TSDB_DATA_TYPE_DOUBLE:
                            double v7 = (double)Marshal.PtrToStructure(data, typeof(double));
                            builder.Append(v7);
                            break;

                        case TDengineDataType.TSDB_DATA_TYPE_BINARY:
                            string v8 = Marshal.PtrToStringAnsi(data);
                            builder.Append(v8);
                            break;

                        case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP:
                            long v9 = Marshal.ReadInt64(data);
                            builder.Append(v9);
                            break;

                        case TDengineDataType.TSDB_DATA_TYPE_NCHAR:
                            string v10 = Marshal.PtrToStringAnsi(data);
                            builder.Append(v10);
                            break;
                        }
                    }
                    builder.Append("---");

                    if (queryRows <= 10)
                    {
                        Console.WriteLine(builder.ToString());
                    }
                    builder.Clear();
                }

                if (TDengine.ErrorNo(res) != 0)
                {
                    Console.Write("Query is not complete, Error {0:G}",
                                  TDengine.ErrorNo(res), TDengine.Error(res));
                }
                Console.WriteLine("");

                TDengine.FreeResult(res);
            }

            System.DateTime end = new System.DateTime();
            TimeSpan        ts  = end - start;

            Console.Write("Total {0:G} rows inserted, {1:G} rows query, time spend {2:G} seconds.\n"
                          , this.rowsInserted, queryRows, ts.TotalSeconds);
        }
Ejemplo n.º 27
0
 static void ExitProgram()
 {
     TDengine.Cleanup();
     System.Environment.Exit(0);
 }
Ejemplo n.º 28
0
 static void CleanAndExitProgram(int ret)
 {
     TDengine.Cleanup();
     System.Environment.Exit(ret);
 }
Ejemplo n.º 29
0
            public void ThreadMain()
            {
                VerbosePrintFormat("InsertDataThread {0} from {1} to {2}\n", id, start, end);
                StringBuilder sql = new StringBuilder();

                DateTime now = DateTime.Now;
                int      h   = now.Hour;
                int      m   = now.Minute;
                int      s   = now.Second;

                long baseTimestamp = -16094340000;   // 1969-06-29 01:21:00

                VerbosePrintFormat("beginTime is {0} + {1}h:{2}m:{3}s\n", baseTimestamp, h, m, s);
                long   beginTimestamp = baseTimestamp + ((h * 60 + m) * 60 + s) * 1000;
                Random random         = new Random();

                long rowsInserted = 0;

                long i = 0;

                while (i < recordsPerTable)
                {
                    for (long table = start; table <= end; ++table)
                    {
                        long inserted = i;

                        sql.Clear();
                        sql.Append("INSERT INTO ").
                        Append(this.dbName).Append(".").Append(this.tablePrefix).Append(table).
                        Append(" VALUES");
                        if (recordsPerTable < batchRows)
                        {
                            batchRows = recordsPerTable;
                        }
                        for (int batch = 0; batch < batchRows; ++batch)
                        {
                            long writeTimeStamp = beginTimestamp + i + batch;
                            int  rnd            = 100;
                            if (this.order == false)
                            {
                                rnd = random.Next(1, 100);
                                if (rnd <= this.rateOfOutorder)
                                {
                                    writeTimeStamp = writeTimeStamp + rnd * 10000;
                                    DebugPrint("### ");
                                }
                                DebugPrintFormat("order:{0} rnd:{1} timestamp:{2}\n", this.order, rnd, writeTimeStamp);
                            }
                            else
                            {
                                DebugPrintFormat("order:{0} timestamp:{1}\n", this.order, writeTimeStamp);
                            }

                            sql.Append("(")
                            .Append(writeTimeStamp)
                            .Append(", 1, -2, -3,")
                            .Append(i + batch - 127)
                            .Append(", -5, -6, -7, 'abc', 'def', 254, 65534,")
                            .Append(4294967294 - (uint)i - (uint)batch)
                            .Append(",")
                            .Append(18446744073709551614 - (ulong)i - (ulong)batch)
                            .Append(")");
                        }
                        VerbosePrint(sql.ToString() + "\n");
                        IntPtr res = TDengine.Query(this.conn, sql.ToString());
                        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("");
                        }

                        inserted += this.batchRows;

                        int affectRows = TDengine.AffectRows(res);
                        rowsInserted += affectRows;

                        TDengine.FreeResult(res);
                        if (table == end)
                        {
                            i = inserted;
                        }
                    }
                }
            }
Ejemplo n.º 30
0
        public void ExecuteQuery(string sql)
        {
            DateTime dt1       = DateTime.Now;
            long     queryRows = 0;
            IntPtr   res       = TDengine.Query(conn, sql);

            getPrecision(res);
            if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
            {
                Console.Write(sql.ToString() + " failure, ");
                if (res != IntPtr.Zero)
                {
                    Console.Write("reason: " + TDengine.Error(res));
                }
                Console.WriteLine("");
                ExitProgram();
            }
            DateTime dt2  = DateTime.Now;
            TimeSpan span = dt2 - dt1;

            Console.WriteLine("[OK] time cost: " + span.ToString() + "ms, execute statement ====> " + sql.ToString());
            int fieldCount = TDengine.FieldCount(res);

            List <TDengineMeta> metas = TDengine.FetchFields(res);

            for (int j = 0; j < metas.Count; j++)
            {
                TDengineMeta meta = (TDengineMeta)metas[j];
            }

            IntPtr        rowdata;
            StringBuilder builder = new StringBuilder();

            while ((rowdata = TDengine.FetchRows(res)) != IntPtr.Zero)
            {
                queryRows++;
                for (int fields = 0; fields < fieldCount; ++fields)
                {
                    TDengineMeta meta   = metas[fields];
                    int          offset = IntPtr.Size * fields;
                    IntPtr       data   = Marshal.ReadIntPtr(rowdata, offset);

                    builder.Append("---");

                    if (data == IntPtr.Zero)
                    {
                        builder.Append("NULL");
                        continue;
                    }

                    switch ((TDengineDataType)meta.type)
                    {
                    case TDengineDataType.TSDB_DATA_TYPE_BOOL:
                        bool v1 = Marshal.ReadByte(data) == 0 ? false : true;
                        builder.Append(v1);
                        break;

                    case TDengineDataType.TSDB_DATA_TYPE_TINYINT:
                        byte v2 = Marshal.ReadByte(data);
                        builder.Append(v2);
                        break;

                    case TDengineDataType.TSDB_DATA_TYPE_SMALLINT:
                        short v3 = Marshal.ReadInt16(data);
                        builder.Append(v3);
                        break;

                    case TDengineDataType.TSDB_DATA_TYPE_INT:
                        int v4 = Marshal.ReadInt32(data);
                        builder.Append(v4);
                        break;

                    case TDengineDataType.TSDB_DATA_TYPE_BIGINT:
                        long v5 = Marshal.ReadInt64(data);
                        builder.Append(v5);
                        break;

                    case TDengineDataType.TSDB_DATA_TYPE_FLOAT:
                        float v6 = (float)Marshal.PtrToStructure(data, typeof(float));
                        builder.Append(v6);
                        break;

                    case TDengineDataType.TSDB_DATA_TYPE_DOUBLE:
                        double v7 = (double)Marshal.PtrToStructure(data, typeof(double));
                        builder.Append(v7);
                        break;

                    case TDengineDataType.TSDB_DATA_TYPE_BINARY:
                        string v8 = Marshal.PtrToStringAnsi(data);
                        builder.Append(v8);
                        break;

                    case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP:
                        long v9 = Marshal.ReadInt64(data);
                        builder.Append(v9);
                        break;

                    case TDengineDataType.TSDB_DATA_TYPE_NCHAR:
                        string v10 = Marshal.PtrToStringAnsi(data);
                        builder.Append(v10);
                        break;
                    }
                }
                builder.Append("---");

                if (queryRows <= 10)
                {
                    Console.WriteLine(builder.ToString());
                }
                builder.Clear();
            }

            if (TDengine.ErrorNo(res) != 0)
            {
                Console.Write("Query is not complete, Error {0:G}", TDengine.ErrorNo(res), TDengine.Error(res));
            }
            Console.WriteLine("");

            TDengine.FreeResult(res);
        }