OmniSciDB  a7179b2938
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
omnisci.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 OmniSci database.

Definition at line 172 of file connection.py.

Constructor & Destructor Documentation

def omnisci.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 191 of file connection.py.

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

+ Here is the call graph for this function:

Member Function Documentation

def omnisci.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 386 of file connection.py.

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

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

Definition at line 340 of file connection.py.

341  def __enter__(self):
342  return self
def omnisci.connection.Connection.__exit__ (   self,
  exc_type,
  exc_val,
  exc_tb 
)

Definition at line 343 of file connection.py.

References com.omnisci.jdbc.OmniSciResultSet.close(), Archive.close(), com.omnisci.jdbc.OmniSciStatement.close(), omnisci.connection.Connection.close(), com.omnisci.jdbc.OmniSciConnection.close(), and com.omnisci.jdbc.OmniSciPreparedStatement.close().

344  def __exit__(self, exc_type, exc_val, exc_tb):
345  self.close()

+ Here is the call graph for this function:

def omnisci.connection.Connection.__repr__ (   self)

Definition at line 324 of file connection.py.

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

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

Definition at line 350 of file connection.py.

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

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

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

+ Here is the caller graph for this function:

def omnisci.connection.Connection.closed (   self)

Definition at line 347 of file connection.py.

References omnisci.connection.Connection._closed.

348  def closed(self):
349  return self._closed
def omnisci.connection.Connection.commit (   self)
This is a noop, as OmniSci does not provide transactions.

Implemented to comply with the DBI specification.

Definition at line 360 of file connection.py.

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

Definition at line 382 of file connection.py.

383  def cursor(self):
384  """Create a new :class:`Cursor` object attached to this connection."""
385  return Cursor(self)
def omnisci.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 367 of file connection.py.

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

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

+ Here is the caller graph for this function:

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

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

Definition at line 408 of file connection.py.

References omnisci.connection.Connection._rbc.

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

Member Data Documentation

omnisci.connection.Connection._client
private

Definition at line 278 of file connection.py.

omnisci.connection.Connection._closed
private

Definition at line 194 of file connection.py.

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

omnisci.connection.Connection._dbname
private

Definition at line 264 of file connection.py.

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

omnisci.connection.Connection._host
private

Definition at line 262 of file connection.py.

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

omnisci.connection.Connection._idpsslverify
private

Definition at line 297 of file connection.py.

omnisci.connection.Connection._password
private

Definition at line 261 of file connection.py.

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

omnisci.connection.Connection._port
private

Definition at line 263 of file connection.py.

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

omnisci.connection.Connection._protocol
private

Definition at line 266 of file connection.py.

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

omnisci.connection.Connection._rbc
private

Definition at line 269 of file connection.py.

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

omnisci.connection.Connection._session
private

Definition at line 282 of file connection.py.

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

omnisci.connection.Connection._socket
private

Definition at line 267 of file connection.py.

omnisci.connection.Connection._tdf
private

Definition at line 268 of file connection.py.

omnisci.connection.Connection._transport
private

Definition at line 265 of file connection.py.

omnisci.connection.Connection._user
private

Definition at line 260 of file connection.py.

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

omnisci.connection.Connection.sessionid

Definition at line 193 of file connection.py.

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


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