Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
except ImportError:
try:
import pg8000 as pglib
except ImportError:
raise ImportError("You must have psycopg2 or pg8000 modules installed")
from .enums import IsolationLevel
from .defaults import \
default_dbname, \
default_username
from .exceptions import QueryException
# export these exceptions
InternalError = pglib.InternalError
ProgrammingError = pglib.ProgrammingError
class NodeConnection(object):
"""
Transaction wrapper returned by Node
"""
def __init__(self, node, dbname=None, username=None, password=None):
# Set default arguments
dbname = dbname or default_dbname()
username = username or default_username()
self._node = node
def ok(self, conn, user, **kwargs):
raise InternalError("ok method should be overridden on AuthenticationRequest instance")
elif isinstance(msg, Protocol.CommandComplete):
self._send(Protocol.ClosePortal(portal))
self._send(Protocol.Sync())
while 1:
msg = self._read_message()
if isinstance(msg, Protocol.ReadyForQuery):
# ready to move on with life...
self._state = "ready"
break
elif isinstance(msg, Protocol.CloseComplete):
# ok, great!
pass
elif isinstance(msg, Protocol.ErrorResponse):
raise msg.createException()
else:
raise InternalError("unexpected response msg %r" % msg)
end_of_data = True
break
elif isinstance(msg, Protocol.ErrorResponse):
raise msg.createException()
else:
raise InternalError("Unexpected response msg %r" % msg)
return end_of_data, rows
finally:
self._sock_lock.release()
def _fill_cache(self):
self._lock.acquire()
try:
if self._cached_rows:
raise InternalError("attempt to fill cache that isn't empty")
end_of_data, rows = self.c.fetch_rows(self._portal_name, self.row_cache_size, self._row_desc)
self._cached_rows = rows
if end_of_data:
self._command_complete = True
finally:
self._lock.release()
def pg_value(v, fc, **kwargs):
typ = type(v)
data = Types.py_types.get(typ)
if data == None:
raise NotSupportedError("type %r not mapped to pg type" % typ)
elif data.get("tid") == -1:
# special case: NULL values
return None
if fc == 0:
func = data.get("txt_out")
elif fc == 1:
func = data.get("bin_out")
else:
raise InternalError("unrecognized format code %r" % fc)
if func == None:
raise NotSupportedError("type %r, format code %r not supported" % (typ, fc))
return func(v, **kwargs)
pg_value = staticmethod(pg_value)
self._send(Protocol.Execute(portal, 0))
self._send(Protocol.Sync())
exc = None
while 1:
msg = self._read_message()
if isinstance(msg, Protocol.CommandComplete):
# more good news!
pass
elif isinstance(msg, Protocol.ReadyForQuery):
if exc != None:
raise exc
break
elif isinstance(msg, Protocol.ErrorResponse):
exc = msg.createException()
else:
raise InternalError("unexpected response")
return None
elif isinstance(msg, Protocol.RowDescription):
# Return the new row desc, since it will have the format
# types we asked for
return msg
elif isinstance(msg, Protocol.ErrorResponse):
raise msg.createException()
else:
raise InternalError("Unexpected response msg %r" % (msg))
finally:
self._sock_lock.release()
def verifyState(self, state):
if self._state != state:
raise InternalError("connection state must be %s, is %s" % (state, self._state))
def ok(self, conn, user, password=None, **kwargs):
if password == None:
raise InterfaceError("server requesting MD5 password authentication, but no password was provided")
pwd = "md5" + md5.new(md5.new(password + user).hexdigest() + self.salt).hexdigest()
conn._send(Protocol.PasswordMessage(pwd))
msg = conn._read_message()
if isinstance(msg, Protocol.AuthenticationRequest):
return msg.ok(conn, user)
elif isinstance(msg, Protocol.ErrorResponse):
if msg.code == "28000":
raise InterfaceError("md5 password authentication failed")
else:
raise InternalError("server returned unexpected error %r" % msg)
else:
raise InternalError("server returned unexpected response %r" % msg)
def __init__(self, typ, name):
if len(typ) != 1:
raise InternalError("Describe typ must be 1 char")
self.typ = typ
self.name = name