Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return reduce(lambda acc, val: acc * 256 + _ord(val), reversed(buf), 0)
# exception hierarchy
class Warning(exc_base_class):
pass
class Error(exc_base_class):
pass
TimeoutError = socket.timeout
class InterfaceError(Error):
pass
class DatabaseError(Error):
@property
def message(self):
if self.procname:
return 'SQL Server message %d, severity %d, state %d, ' \
'procedure %s, line %d:\n%s' % (self.number,
self.severity, self.state, self.procname,
self.line, self.text)
else:
return 'SQL Server message %d, severity %d, state %d, ' \
'line %d:\n%s' % (self.number, self.severity,
self.state, self.line, self.text)
def raise_db_exception(self):
""" Raises exception from last server message
This function will skip messages: The statement has been terminated
"""
if not self.messages:
raise Error("Request failed, server didn't send error message")
while True:
msg = self.messages[-1]
if msg['msgno'] == 3621: # the statement has been terminated
self.messages = self.messages[:-1]
else:
break
error_msg = ' '.join(msg['message'] for msg in self.messages)
ex = _create_exception_by_message(msg, error_msg)
raise ex
return 'SQL Server message %d, severity %d, state %d, ' \
'procedure %s, line %d:\n%s' % (self.number,
self.severity, self.state, self.procname,
self.line, self.text)
else:
return 'SQL Server message %d, severity %d, state %d, ' \
'line %d:\n%s' % (self.number, self.severity,
self.state, self.line, self.text)
class ClosedConnectionError(InterfaceError):
def __init__(self):
super(ClosedConnectionError, self).__init__('Server closed connection')
class DataError(Error):
pass
class OperationalError(DatabaseError):
pass
class LoginError(OperationalError):
pass
class IntegrityError(DatabaseError):
pass
class InternalError(DatabaseError):
def querying_context(self, packet_type):
""" Context manager for querying.
Sets state to TDS_QUERYING, and reverts it to TDS_IDLE if exception happens inside managed block,
and to TDS_PENDING if managed block succeeds and flushes buffer.
"""
if self.set_state(TDS_QUERYING) != TDS_QUERYING:
raise Error("Couldn't switch to state")
self._writer.begin_packet(packet_type)
try:
yield
except:
if self.state != TDS_DEAD:
self.set_state(TDS_IDLE)
raise
else:
self.set_state(TDS_PENDING)
self._writer.flush()
if off > size or off + l > size:
self.bad_stream('Invalid offset in PRELOGIN structure')
if type == self.VERSION:
self.conn.server_library_version = prod_version_struct.unpack_from(p, off)
elif type == self.ENCRYPTION and l >= 1:
crypt_flag, = byte_struct.unpack_from(p, off)
elif type == self.MARS:
self.conn._mars_enabled = bool(byte_struct.unpack_from(p, off)[0])
elif type == self.INSTOPT:
# ignore instance name mismatch
pass
i += 5
# if server do not has certificate do normal login
if crypt_flag == 2:
if login.encryption_level >= TDS_ENCRYPTION_REQUIRE:
raise Error('Server required encryption but it is not supported')
return
self._sock = ssl.wrap_socket(self._sock, ssl_version=ssl.PROTOCOL_SSLv3)
class Warning(exc_base_class):
pass
class Error(exc_base_class):
pass
TimeoutError = socket.timeout
class InterfaceError(Error):
pass
class DatabaseError(Error):
@property
def message(self):
if self.procname:
return 'SQL Server message %d, severity %d, state %d, ' \
'procedure %s, line %d:\n%s' % (self.number,
self.severity, self.state, self.procname,
self.line, self.text)
else:
return 'SQL Server message %d, severity %d, state %d, ' \
'line %d:\n%s' % (self.number, self.severity,
self.state, self.line, self.text)
class ClosedConnectionError(InterfaceError):
def __init__(self):
super(ClosedConnectionError, self).__init__('Server closed connection')
def process_auth(self):
""" Reads and processes SSPI stream.
Stream info: http://msdn.microsoft.com/en-us/library/dd302844.aspx
"""
r = self._reader
w = self._writer
pdu_size = r.get_smallint()
if not self.authentication:
raise Error('Got unexpected token')
packet = self.authentication.handle_next(readall(r, pdu_size))
if packet:
w.write(packet)
w.flush()
def fetchone(self):
if self.res_info is None:
raise Error("Previous statement didn't produce any results")
if not self.next_row():
return None
return self.row