OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
heavydb.connection.Connection Class Reference

Public Member Functions

def __init__
 
def __repr__
 
def __del__
 
def __enter__
 
def __exit__
 
def closed
 
def close
 
def commit
 
def execute
 
def cursor
 
def __call__
 
def register_runtime_udfs
 

Public Attributes

 sessionid
 

Private Attributes

 _closed
 
 _user
 
 _password
 
 _host
 
 _port
 
 _dbname
 
 _transport
 
 _protocol
 
 _socket
 
 _tdf
 
 _rbc
 
 _client
 
 _session
 
 _idpsslverify
 

Detailed Description

Connect to your HeavyDB database.

Definition at line 169 of file connection.py.

Constructor & Destructor Documentation

def heavydb.connection.Connection.__init__ (   self,
  uri = None,
  user = None,
  password = None,
  host = None,
  port = 6274,
  dbname = None,
  protocol = 'binary',
  sessionid = None,
  bin_cert_validate = None,
  bin_ca_certs = None,
  idpurl = None,
  idpformusernamefield = 'username',
  idpformpasswordfield = 'password',
  idpsslverify = True 
)

Definition at line 188 of file connection.py.

189  ):
191  self.sessionid = None
192  self._closed = 0
193  if sessionid is not None:
194  if any([user, password, uri, dbname, idpurl]):
195  raise TypeError(
196  "Cannot specify sessionid with user, password,"
197  " dbname, uri, or idpurl"
198  )
199  if uri is not None:
200  if not all(
201  [
202  user is None,
203  password is None,
204  host is None,
205  port == 6274,
206  dbname is None,
207  protocol == 'binary',
208  bin_cert_validate is None,
209  bin_ca_certs is None,
210  idpurl is None,
211  ]
212  ):
213  raise TypeError("Cannot specify both URI and other arguments")
214  (
215  user,
216  password,
217  host,
218  port,
219  dbname,
220  protocol,
221  bin_cert_validate,
222  bin_ca_certs,
223  ) = _parse_uri(uri)
224  if host is None:
225  raise TypeError("`host` parameter is required.")
226  if protocol != 'binary' and not all(
227  [bin_cert_validate is None, bin_ca_certs is None]
228  ):
229  raise TypeError(
230  "Cannot specify bin_cert_validate or bin_ca_certs,"
231  " without binary protocol"
232  )
233  if protocol in ("http", "https"):
234  if not host.startswith(protocol):
235  # the THttpClient expects http[s]://localhost
236  host = '{0}://{1}'.format(protocol, host)
237  transport = THttpClient.THttpClient("{}:{}".format(host, port))
238  proto = TJSONProtocol.TJSONProtocol(transport)
239  socket = None
240  elif protocol == "binary":
241  if any([bin_cert_validate is not None, bin_ca_certs]):
242  socket = TSSLSocket.TSSLSocket(
243  host,
244  port,
245  validate=(bin_cert_validate),
246  ca_certs=bin_ca_certs,
247  )
248  else:
249  socket = TSocket.TSocket(host, port)
250  transport = TTransport.TBufferedTransport(socket)
251  proto = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
252  else:
253  raise ValueError(
254  "`protocol` should be one of"
255  " ['http', 'https', 'binary'],"
256  " got {} instead".format(protocol),
257  )
258  self._user = user
259  self._password = password
260  self._host = host
261  self._port = port
262  self._dbname = dbname
263  self._transport = transport
264  self._protocol = protocol
265  self._socket = socket
266  self._tdf = None
267  self._rbc = None
268  try:
269  self._transport.open()
270  except TTransportException as e:
271  if e.NOT_OPEN:
272  err = OperationalError("Could not connect to database")
273  raise err from e
274  else:
275  raise
276  self._client = Client(proto)
277  try:
278  # If a sessionid was passed, we should validate it
279  if sessionid:
280  self._session = sessionid
281  self._client.get_tables(self._session)
282  self.sessionid = sessionid
283  else:
284  if idpurl:
285  self._user = ''
287  username=user,
288  password=password,
289  idpurl=idpurl,
290  userformfield=idpformusernamefield,
291  passwordformfield=idpformpasswordfield,
292  sslverify=idpsslverify,
293  )
294  self._dbname = ''
295  self._idpsslverify = idpsslverify
296  user = self._user
297  password = self._password
298  dbname = self._dbname
299 
300  self._session = self._client.connect(user, password, dbname)
301  except TDBException as e:
302  raise _translate_exception(e) from e
303  except TTransportException:
304  raise ValueError(
305  f"Connection failed with port {port} and "
306  f"protocol '{protocol}'. Try port 6274 for "
307  "protocol == binary or 6273, 6278 or 443 for "
308  "http[s]"
309  )
310 
311  # if HeavyDB version <4.6, raise RuntimeError, as data import can be
312  # incorrect for columnar date loads
313  # Caused by https://github.com/omnisci/pymapd/pull/188
314  semver = self._client.get_version()
315  if Version(semver.split("-")[0]) < Version("4.6"):
316  raise RuntimeError(
317  f"Version {semver} of HeavyDB detected. "
318  "Please use pymapd <0.11. See release notes "
319  "for more details."
320  )
def heavydb.connection.Connection.__del__ (   self)

+ Here is the call graph for this function:

Member Function Documentation

def heavydb.connection.Connection.__call__ (   self,
  args,
  kwargs 
)
Runtime UDF decorator.

The connection object can be applied to a Python function as
decorator that will add the function to bending registration
list.

Definition at line 383 of file connection.py.

References heavydb.connection.Connection._dbname, heavydb.connection.Connection._host, heavydb.connection.Connection._password, heavydb.connection.Connection._port, heavydb.connection.Connection._rbc, heavydb.connection.Connection._user, and heavydb.connection.Connection.sessionid.

384  def __call__(self, *args, **kwargs):
385  """Runtime UDF decorator.
386 
387  The connection object can be applied to a Python function as
388  decorator that will add the function to bending registration
389  list.
390  """
391  try:
392  from rbc.omniscidb import RemoteOmnisci
393  except ImportError:
394  raise ImportError("The 'rbc' package is required for `__call__`")
395  if self._rbc is None:
396  self._rbc = RemoteOmnisci(
397  user=self._user,
398  password=self._password,
399  host=self._host,
400  port=self._port,
401  dbname=self._dbname,
402  )
403  self._rbc._session_id = self.sessionid
404  return self._rbc(*args, **kwargs)
def heavydb.connection.Connection.__enter__ (   self)

Definition at line 337 of file connection.py.

338  def __enter__(self):
339  return self
def heavydb.connection.Connection.__exit__ (   self,
  exc_type,
  exc_val,
  exc_tb 
)

Definition at line 340 of file connection.py.

References ai.heavy.jdbc.HeavyAIResultSet.close(), Archive.close(), ai.heavy.jdbc.HeavyAIStatement.close(), heavydb.connection.Connection.close(), ai.heavy.jdbc.HeavyAIConnection.close(), and ai.heavy.jdbc.HeavyAIPreparedStatement.close().

341  def __exit__(self, exc_type, exc_val, exc_tb):
342  self.close()

+ Here is the call graph for this function:

def heavydb.connection.Connection.__repr__ (   self)

Definition at line 321 of file connection.py.

References heavydb.connection.Connection._dbname, heavydb.connection.Connection._host, heavydb.connection.Connection._port, heavydb.connection.Connection._protocol, and heavydb.connection.Connection._user.

322  def __repr__(self):
323  tpl = (
324  'Connection(heavydb://{user}:***@{host}:{port}/{dbname}?'
325  'protocol={protocol})'
326  )
327  return tpl.format(
328  user=self._user,
329  host=self._host,
330  port=self._port,
331  dbname=self._dbname,
332  protocol=self._protocol,
333  )
def heavydb.connection.Connection.close (   self)
Disconnect from the database unless created with sessionid

Definition at line 347 of file connection.py.

References heavydb.connection.Connection._closed, heavydb.connection.Connection._rbc, heavydb.connection.Connection._session, and heavydb.connection.Connection.sessionid.

Referenced by heavydb.connection.Connection.__del__(), heavydb.cursor.Cursor.__exit__(), and heavydb.connection.Connection.__exit__().

348  def close(self):
349  """Disconnect from the database unless created with sessionid"""
350  if not self.sessionid and not self._closed:
351  try:
352  self._client.disconnect(self._session)
353  except (TDBException, AttributeError, TypeError):
354  pass
355  self._closed = 1
356  self._rbc = None

+ Here is the caller graph for this function:

def heavydb.connection.Connection.closed (   self)

Definition at line 344 of file connection.py.

References heavydb.connection.Connection._closed.

345  def closed(self):
346  return self._closed
def heavydb.connection.Connection.commit (   self)
This is a noop, as HeavyDB does not provide transactions.

Implemented to comply with the DBI specification.

Definition at line 357 of file connection.py.

358  def commit(self):
359  """This is a noop, as HeavyDB does not provide transactions.
360 
361  Implemented to comply with the DBI specification.
362  """
363  return None
def heavydb.connection.Connection.cursor (   self)
Create a new :class:`Cursor` object attached to this connection.

Definition at line 379 of file connection.py.

380  def cursor(self):
381  """Create a new :class:`Cursor` object attached to this connection."""
382  return Cursor(self)
def heavydb.connection.Connection.execute (   self,
  operation,
  parameters = None 
)
Execute a SQL statement

Parameters
----------
operation: str
    A SQL statement to exucute

Returns
-------
c: Cursor

Definition at line 364 of file connection.py.

Referenced by heavydb.cursor.Cursor.executemany().

365  def execute(self, operation, parameters=None):
366  """Execute a SQL statement
367 
368  Parameters
369  ----------
370  operation: str
371  A SQL statement to exucute
372 
373  Returns
374  -------
375  c: Cursor
376  """
377  c = Cursor(self)
378  return c.execute(operation, parameters=parameters)

+ Here is the caller graph for this function:

def heavydb.connection.Connection.register_runtime_udfs (   self)
Register any bending Runtime UDF functions in HeavyDB server.

If no Runtime UDFs have been defined, the call to this method
is noop.

Definition at line 405 of file connection.py.

References heavydb.connection.Connection._rbc.

406  def register_runtime_udfs(self):
407  """Register any bending Runtime UDF functions in HeavyDB server.
408 
409  If no Runtime UDFs have been defined, the call to this method
410  is noop.
411  """
412  if self._rbc is not None:
413  self._rbc.register()

Member Data Documentation

heavydb.connection.Connection._client
private

Definition at line 275 of file connection.py.

heavydb.connection.Connection._closed
private

Definition at line 191 of file connection.py.

Referenced by heavydb.connection.Connection.close(), and heavydb.connection.Connection.closed().

heavydb.connection.Connection._dbname
private

Definition at line 261 of file connection.py.

Referenced by heavydb.connection.Connection.__call__(), and heavydb.connection.Connection.__repr__().

heavydb.connection.Connection._host
private

Definition at line 259 of file connection.py.

Referenced by heavydb.connection.Connection.__call__(), and heavydb.connection.Connection.__repr__().

heavydb.connection.Connection._idpsslverify
private

Definition at line 294 of file connection.py.

heavydb.connection.Connection._password
private

Definition at line 258 of file connection.py.

Referenced by heavydb.connection.Connection.__call__().

heavydb.connection.Connection._port
private

Definition at line 260 of file connection.py.

Referenced by heavydb.connection.Connection.__call__(), and heavydb.connection.Connection.__repr__().

heavydb.connection.Connection._protocol
private

Definition at line 263 of file connection.py.

Referenced by heavydb.connection.Connection.__repr__().

heavydb.connection.Connection._rbc
private

Definition at line 266 of file connection.py.

Referenced by heavydb.connection.Connection.__call__(), heavydb.connection.Connection.close(), and heavydb.connection.Connection.register_runtime_udfs().

heavydb.connection.Connection._session
private

Definition at line 279 of file connection.py.

Referenced by heavydb.connection.Connection.close().

heavydb.connection.Connection._socket
private

Definition at line 264 of file connection.py.

heavydb.connection.Connection._tdf
private

Definition at line 265 of file connection.py.

heavydb.connection.Connection._transport
private

Definition at line 262 of file connection.py.

heavydb.connection.Connection._user
private

Definition at line 257 of file connection.py.

Referenced by heavydb.connection.Connection.__call__(), and heavydb.connection.Connection.__repr__().

heavydb.connection.Connection.sessionid

Definition at line 190 of file connection.py.

Referenced by heavydb.connection.Connection.__call__(), and heavydb.connection.Connection.close().


The documentation for this class was generated from the following file: