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

5

S
slguan 已提交
6 7 8
class TDengineConnection(object):
    """ TDengine connection object
    """
9

S
slguan 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    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

        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']
34

S
slguan 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
        # 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)
48 49 50 51 52 53
        self._conn = self._chandle.connect(
            self._host,
            self._user,
            self._password,
            self._database,
            self._port)
S
slguan 已提交
54 55 56 57 58 59

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

weixin_48148422's avatar
weixin_48148422 已提交
60 61 62 63 64
    def subscribe(self, restart, topic, sql, interval):
        """Create a subscription.
        """
        if self._conn is None:
            return None
65 66
        sub = CTaosInterface.subscribe(
            self._conn, restart, topic, sql, interval)
weixin_48148422's avatar
weixin_48148422 已提交
67 68
        return TDengineSubscription(sub)

S
slguan 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    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.
        """
89
        pass
S
slguan 已提交
90

91

S
slguan 已提交
92 93 94
if __name__ == "__main__":
    conn = TDengineConnection(host='192.168.1.107')
    conn.close()
95
    print("Hello world")