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
|
from ..client import Client
from .. import defines
from .cursor import Cursor
from .errors import InterfaceError
class Connection(object):
"""
Creates new Connection for accessing ClickHouse database.
Connection is just wrapper for handling multiple cursors (clients) and
do not initiate actual connections to the ClickHouse server.
See parameters description in
:data:`~clickhouse_driver.connection.Connection`.
"""
def __init__(self, dsn=None, host=None,
user=defines.DEFAULT_USER, password=defines.DEFAULT_PASSWORD,
port=defines.DEFAULT_PORT, database=defines.DEFAULT_DATABASE,
**kwargs):
self.cursors = []
self.dsn = dsn
self.user = user
self.password = password
self.host = host
self.port = port
self.database = database
self.connection_kwargs = kwargs
self.is_closed = False
self._hosts = None
super(Connection, self).__init__()
def __repr__(self):
return '<connection object at 0x{0:x}; closed: {1:}>'.format(
id(self), self.is_closed
)
# Context manager integrations.
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def _make_client(self):
"""
:return: a new Client instance.
"""
if self.dsn is not None:
return Client.from_url(self.dsn)
return Client(self.host, port=self.port,
user=self.user, password=self.password,
database=self.database, **self.connection_kwargs)
def close(self):
"""
Close the connection now. The connection will be unusable from this
point forward; an :data:`~clickhouse_driver.dbapi.Error` (or subclass)
exception will be raised if any operation is attempted with the
connection. The same applies to all cursor objects trying to use the
connection.
"""
for cursor in self.cursors:
cursor.close()
self.is_closed = True
def commit(self):
"""
Do nothing since ClickHouse has no transactions.
"""
pass
def rollback(self):
"""
Do nothing since ClickHouse has no transactions.
"""
pass
def cursor(self, cursor_factory=None):
"""
:param cursor_factory: Argument can be used to create non-standard
cursors.
:return: a new cursor object using the connection.
"""
if self.is_closed:
raise InterfaceError('connection already closed')
client = self._make_client()
if self._hosts is None:
self._hosts = client.connection.hosts
else:
client.connection.hosts = self._hosts
cursor_factory = cursor_factory or Cursor
cursor = cursor_factory(client, self)
self.cursors.append(cursor)
return cursor
|