connection.py 2.3 KB
Newer Older
weixin_48148422's avatar
weixin_48148422 已提交
1 2 3 4
from .cursor import TDengineCursor
from .subscription import TDengineSubscription
from .cinterface import CTaosInterface

5

weixin_48148422's avatar
weixin_48148422 已提交
6 7 8
class TDengineConnection(object):
    """ TDengine connection object
    """
9

weixin_48148422's avatar
weixin_48148422 已提交
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
    def __init__(self, *args, **kwargs):
        self._conn = None
        self._host = None
        self._user = "root"
        self._password = "taosdata"
        self._database = None
        self._port = 0
        self._config = None
        self._chandle = None

        if len(kwargs) > 0:
            self.config(**kwargs)

    def config(self, **kwargs):
        # host
        if 'host' in kwargs:
            self._host = kwargs['host']

        # user
        if 'user' in kwargs:
            self._user = kwargs['user']

        # password
        if 'password' in kwargs:
            self._password = kwargs['password']
35

weixin_48148422's avatar
weixin_48148422 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
        # database
        if 'database' in kwargs:
            self._database = kwargs['database']

        # port
        if 'port' in kwargs:
            self._port = kwargs['port']

        # config
        if 'config' in kwargs:
            self._config = kwargs['config']

        self._chandle = CTaosInterface(self._config)
49 50 51 52 53 54
        self._conn = self._chandle.connect(
            self._host,
            self._user,
            self._password,
            self._database,
            self._port)
weixin_48148422's avatar
weixin_48148422 已提交
55 56 57 58 59 60 61 62 63 64 65

    def close(self):
        """Close current connection.
        """
        return CTaosInterface.close(self._conn)

    def subscribe(self, restart, topic, sql, interval):
        """Create a subscription.
        """
        if self._conn is None:
            return None
66 67
        sub = CTaosInterface.subscribe(
            self._conn, restart, topic, sql, interval)
weixin_48148422's avatar
weixin_48148422 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        return TDengineSubscription(sub)

    def cursor(self):
        """Return a new Cursor object using the connection.
        """
        return TDengineCursor(self)

    def commit(self):
        """Commit any pending transaction to the database.

        Since TDengine do not support transactions, the implement is void functionality.
        """
        pass

    def rollback(self):
        """Void functionality
        """
        pass

    def clear_result_set(self):
        """Clear unused result set on this connection.
        """
90
        pass
weixin_48148422's avatar
weixin_48148422 已提交
91

92

weixin_48148422's avatar
weixin_48148422 已提交
93 94 95
if __name__ == "__main__":
    conn = TDengineConnection(host='192.168.1.107')
    conn.close()
96
    print("Hello world")