pytdengine.py 15.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
import ctypes
import numpy as np
import pandas as pd

class TaosField(ctypes.Structure):
    _fields_ = [('name'  , ctypes.c_char * 64),
                ('bytes' , ctypes.c_short),
                ('type'  , ctypes.c_char)]

class TaosClass(object):
    '''
    '''
    TSDB_DATA_TYPE_NULL      = 0
    TSDB_DATA_TYPE_BOOL      = 1 
    TSDB_DATA_TYPE_TINYINT   = 2 
    TSDB_DATA_TYPE_SMALLINT  = 3 
    TSDB_DATA_TYPE_INT       = 4 
    TSDB_DATA_TYPE_BIGINT    = 5 
    TSDB_DATA_TYPE_FLOAT     = 6 
    TSDB_DATA_TYPE_DOUBLE    = 7 
    TSDB_DATA_TYPE_BINARY    = 8 
    TSDB_DATA_TYPE_TIMESTAMP = 9 
    TSDB_DATA_TYPE_NCHAR     = 10

    libtaos = ctypes.windll.LoadLibrary('taos')

    libtaos.taos_fetch_fields.restype = ctypes.POINTER(TaosField)
    libtaos.taos_init.restype = None
    libtaos.taos_connect.restype = ctypes.c_void_p
    libtaos.taos_use_result.restype = ctypes.c_void_p
    libtaos.taos_fetch_row.restype = ctypes.POINTER(ctypes.c_void_p)
    libtaos.taos_errstr.restype = ctypes.c_char_p

    def __init__(self, host=None, user='root', password='taosdata', db=None, port=0, config=None):
        '''
        Function to initialize the class
        @host     : str, hostname to connect
        @user     : str, username to connect to server
        @password : str, password to connect to server
        @db       : str, default db to use when log in
        @config   : str, config directory

        @rtype    : None
        '''
        self.host          = ctypes.c_char_p(host)
        self.user          = ctypes.c_char_p(user)
        self.password      = ctypes.c_char_p(password)
        self.db            = ctypes.c_char_p(db)
        self.config        = ctypes.c_char_p(config)
        self.port          = ctypes.c_int(port)

        if config != None:
            TaosClass.libtaos.taos_options(2, self.config)

        TaosClass.libtaos.taos_init()

    def connect(self):
        '''
        Function to connect to server
        
        @rtype: c_void_p, TDengine handle
        '''
        connection = ctypes.c_void_p(TaosClass.libtaos.taos_connect(self.host, self.user, self.password, self.db, self.port))

        if connection.value == None:
            print('connect to TDengine failed')
            # sys.exit(1)
        else:
            print('connect to TDengine success')

        return connection

    @staticmethod
    def close(connection):
        '''
        Close the TDengine handle
        '''
        TaosClass.libtaos.taos_close(connection)
        print('connection is closed')

    @staticmethod
    def fetchBlock(result, fields):
        pblock = ctypes.c_void_p(0)
        num_of_rows = TaosClass.libtaos.taos_fetch_block(result, ctypes.byref(pblock))

        if num_of_rows == 0:
            return None, 0

        blocks = [None] * len(fields)
        for i in range(len(fields)):
            data = ctypes.cast(pblock, ctypes.POINTER(ctypes.c_void_p))[i]

            if (fields[i]['type'] == TaosClass.TSDB_DATA_TYPE_BOOL):
                if num_of_rows > 0:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_bool))[:abs(num_of_rows)][::-1]
                else:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_bool))[:abs(num_of_rows)]
            elif (fields[i]['type'] == TaosClass.TSDB_DATA_TYPE_TINYINT):
                if num_of_rows > 0:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_byte))[:abs(num_of_rows)][::-1]
                else:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_byte))[:abs(num_of_rows)]
            elif (fields[i]['type'] == TaosClass.TSDB_DATA_TYPE_SMALLINT):
                if num_of_rows > 0:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_short))[:abs(num_of_rows)][::-1]
                else:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_short))[:abs(num_of_rows)]
            elif (fields[i]['type'] == TaosClass.TSDB_DATA_TYPE_INT):
                if num_of_rows > 0:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_int))[:abs(num_of_rows)][::-1]
                else:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_int))[:abs(num_of_rows)]
            elif (fields[i]['type'] == TaosClass.TSDB_DATA_TYPE_BIGINT):
                if num_of_rows > 0:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_longlong))[:abs(num_of_rows)][::-1]
                else:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_longlong))[:abs(num_of_rows)]
            elif (fields[i]['type'] == TaosClass.TSDB_DATA_TYPE_FLOAT):
                if num_of_rows > 0:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_float))[:abs(num_of_rows)][::-1]
                else:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_float))[:abs(num_of_rows)]
            elif (fields[i]['type'] == TaosClass.TSDB_DATA_TYPE_DOUBLE):
                if num_of_rows > 0:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_double))[:abs(num_of_rows)][::-1]
                else:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_double))[:abs(num_of_rows)]
            elif (fields[i]['type'] == TaosClass.TSDB_DATA_TYPE_TIMESTAMP):
                if num_of_rows > 0:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_longlong))[:abs(num_of_rows)][::-1]
                else:
                    blocks[i] = ctypes.cast(data,  ctypes.POINTER(ctypes.c_longlong))[:abs(num_of_rows)]
            # TODO : Make it more efficient
            elif (fields[i]['type'] == TaosClass.TSDB_DATA_TYPE_BINARY):
                if num_of_rows > 0:
                    blocks[i] = [ele.value for ele in (ctypes.cast(data,  ctypes.POINTER(ctypes.c_char * fields[i]['bytes'])))[:abs(num_of_rows)][::-1]]
                else:
                    blocks[i] = [ele.value for ele in (ctypes.cast(data,  ctypes.POINTER(ctypes.c_char * fields[i]['bytes'])))[:abs(num_of_rows)]]
            elif (fields[i]['type'] == TaosClass.TSDB_DATA_TYPE_NCHAR):
                if num_of_rows > 0:
                    blocks[i] = [ele.value for ele in (ctypes.cast(data,  ctypes.POINTER(ctypes.c_wchar * (fields[i]['bytes']/4))))[:abs(num_of_rows)][::-1]]
                else:
                    blocks[i] = [ele.value for ele in (ctypes.cast(data,  ctypes.POINTER(ctypes.c_wchar * (fields[i]['bytes']/4))))[:abs(num_of_rows)]]

        return blocks, abs(num_of_rows)

    @staticmethod
    def query(connection, sql):
        '''
        Run SQL

        @sql: str, sql string to run

        @rtype: 0 on success and -1 on failure
        '''
        return TaosClass.libtaos.taos_query(connection, ctypes.c_char_p(sql))

    @staticmethod
    def useResult(connection):
        '''
        Use result after calling self.query
        '''
        result = ctypes.c_void_p(TaosClass.libtaos.taos_use_result(connection))
        fields = []
        pfields = TaosClass.fetchFields(result)
        for i in range(TaosClass.fieldsCount(connection)):
            fields.append({'name': pfields[i].name, 'bytes':pfields[i].bytes, 'type': ord(pfields[i].type)})

        return result, fields
 
    @staticmethod
    def freeResult(result):
        TaosClass.libtaos.taos_free_result(result)
        result.value = None

    @staticmethod
    def fieldsCount(connection):
        return TaosClass.libtaos.taos_field_count(connection)

    @staticmethod
    def fetchFields(result):
        return TaosClass.libtaos.taos_fetch_fields(result)

    @staticmethod
    def fetchRow(result, fields):
        l = []
        row = TaosClass.libtaos.taos_fetch_row(result)
        if not row: return None

        for i in range(len(fields)):
            l.append(TaosClass.getDataValue(row[i], fields[i]['type'], fields[i]['bytes']))

        return tuple(l)

    @staticmethod
    def getDataValue(data, dtype, byte):
        '''
        '''
        if not data: return None

        if (dtype == TaosClass.TSDB_DATA_TYPE_BOOL):
            return ctypes.cast(data,  ctypes.POINTER(ctypes.c_bool))[0]
        elif (dtype == TaosClass.TSDB_DATA_TYPE_TINYINT):
            return ctypes.cast(data,  ctypes.POINTER(ctypes.c_byte))[0]
        elif (dtype == TaosClass.TSDB_DATA_TYPE_SMALLINT):
            return ctypes.cast(data,  ctypes.POINTER(ctypes.c_short))[0]
        elif (dtype == TaosClass.TSDB_DATA_TYPE_INT):
            return ctypes.cast(data,  ctypes.POINTER(ctypes.c_int))[0]
        elif (dtype == TaosClass.TSDB_DATA_TYPE_BIGINT):
            return ctypes.cast(data,  ctypes.POINTER(ctypes.c_long))[0]
        elif (dtype == TaosClass.TSDB_DATA_TYPE_FLOAT):
            return ctypes.cast(data,  ctypes.POINTER(ctypes.c_float))[0]
        elif (dtype == TaosClass.TSDB_DATA_TYPE_DOUBLE):
            return ctypes.cast(data,  ctypes.POINTER(ctypes.c_double))[0]
        elif (dtype == TaosClass.TSDB_DATA_TYPE_BINARY):
            return (ctypes.cast(data,  ctypes.POINTER(ctypes.c_char))[0:byte]).rstrip('\x00')
        elif (dtype == TaosClass.TSDB_DATA_TYPE_TIMESTAMP):
            return ctypes.cast(data,  ctypes.POINTER(ctypes.c_long))[0]
        elif (dtype == TaosClass.TSDB_DATA_TYPE_NCHAR):
            return (ctypes.cast(data,  ctypes.c_char_p).value).rstrip('\x00')

    @staticmethod
    def affectedRows(connection):
        return TaosClass.libtaos.taos_affected_rows(connection)

    @staticmethod
    def errno(connection):
        return TaosClass.libtaos.taos_errno(connection)

    @staticmethod
    def errStr(connection):
        return TaosClass.libtaos.taos_errstr(connection)


class TaosCursor():
    '''
    Object in TDengine python client which the same as a connection to TDengine server.
    '''
    def __init__(self, connection):
        self.connection = connection
        # self.buffered   = buffered;
        self.result     = ctypes.c_void_p(0)
        self.fields     = []

        self.buffer     = None
        self.iter       = 0

    # def __iter__(self):
    #     self.iter = 0
    #     return self
    #
    # def next(self):
    #     if self.buffered:
    #         if self.iter >= len(self.buffer[0]):
    #             raise StopIteration
    #         else:
    #             return tuple(row[self.iter] for row in self.buffer)
    #     else:
    #         if self.iter >= len(self.buffer[0]):
    #             self.buffer, num_of_fields = TaosClass.fetchBlock(self.result, self.fields)
    #             if num_of_fields == 0:
    #                 raise StopIteration
    #             else:
    #                 self.iter = 1
    #                 return tuple(row[self.iter-1] for row in self.buffer)
    #         else:
    #             self.iter += 1
    #             l = tuple(row[self.iter-1] for row in self.buffer)

    def fetchall(self, format=list):
        '''
        Fetch data after run commands like 'show/select/describe' TaosCursor.execute.

        @format: list -> return a list of list, default and the fastest
                 dict -> return a dictionary with the name of each column as the key
                 numpy.array->return an array
                 pandas.DataFrame->return data as the form of pandas.DataFram

        @rtype: depends on the format
        '''
        if TaosClass.fieldsCount(self.connection) != 0:
            # select or show command
            self.result, self.fields = TaosClass.useResult(self.connection)
            self.iter = 0
            # if self.buffered:
            self.buffer = [[] for i in range(len(self.fields))]
            while True:
                block, num_of_fields = TaosClass.fetchBlock(self.result, self.fields)
                if num_of_fields == 0: break;
                for i in range(len(self.fields)):
                    self.buffer[i].extend(block[i])
            self.freeResult()

            if format == list:
                return self.buffer
            elif format == dict:
                return dict(zip(self.columns(), self.buffer))
            elif format == np.array:
                return [np.asarray(self.buffer[i]) for i in range(len(self.columns()))]
            elif format == pd.DataFrame:
                l = [np.asarray(self.buffer[i]) for i in range(len(self.columns()))]
                return pd.DataFrame.from_records(dict(zip(self.columns(), l)))
            else:
                return None
        else:
            return None

    def execute(self, sql):
        '''
        run sql command

        @rtype: int, 0 for succeed and others for failure
        '''
        # release previous result
        self.freeResult()

        res = TaosClass.query(self.connection, sql)
        if res != 0: return res
        else: return 0

    def freeResult(self):
        if self.result.value != None:
            TaosClass.freeResult(self.result)

    def columns(self):
        '''
        return the column names when query using TaosCursor.execute.

        @rtype: list of str
        '''
        return [self.fields[col]['name'] for col in range(len(self.fields))]

    def error(self):
        '''
        return error string of if execute is wrong

        @rtype: str
        '''
        return TaosClass.errStr(self.connection)

    def close(self):
        self.freeResult()
        TaosClass.close(self.connection)
        self.connection.value = None

class TaosConnection:
    '''
    TDengine connection object
    '''
    def __init__(self, host=None, user='root', passwd='taosdata', database=None, port=0, config=None):
        '''
        @host     : IP address of the TDengine server host
        @user     : user name to log in
        @password : password used to log in
        @database : database to use when logging in
        @port     : port number
        @config   : configuration directory
        '''
        self.taos = TaosClass(host, user, passwd, database, port, config)
        self.cursors = []

    def cursor(self):
        '''
        Generate a TaosCursor object, each object is the same as a connection to TDengine

        @rtype: TaosCursor
        '''
        self.cursors.append(TaosCursor(self.taos.connect()))
        return self.cursors[-1]

    def close(self):
        '''
        Close the connection
        '''
        for cur in self.cursors:
            cur.close()
        
def connector(host=None, user='root', passwd='taosdata', database=None, port=0, config=None):
    '''
    Function to create a TaosConnection object

    @host     : str, ipaddr of the TDengine server
    @user     : str, username used to login
    @passwd   : str, password used to login
    @database : str, database to use when connect, if database is not None and not exists on server, it will result in a connection failure
    @port     : port number
    @config   : configuration directory

    @rtype    : TaosConnection object
    '''
    return TaosConnection(host, user, passwd, database, port, config)