def begin(self): DEBUG_OUTPUT("Connection::begin()") if not self.sock: raise InternalError("Missing socket") if self._transaction is None: self._transaction = Transaction(self, self._autocommit) self._transaction.begin()
def _wait_for_event(self, timeout): event_names = {} event_id = 0 while True: b4 = self.recv_channel(4) if b4 is None: return None op_code = bytes_to_bint(b4) if op_code == self.op_dummy: pass elif op_code == self.op_exit or op_code == self.op_disconnect: break elif op_code == self.op_event: bytes_to_int(self.recv_channel(4)) # db_handle ln = bytes_to_bint(self.recv_channel(4)) b = self.recv_channel(ln, word_alignment=True) assert byte_to_int(b[0]) == 1 i = 1 while i < len(b): ln = byte_to_int(b[i]) s = self.connection.bytes_to_str(b[i + 1:i + 1 + ln]) n = bytes_to_int(b[i + 1 + ln:i + 1 + ln + 4]) event_names[s] = n i += ln + 5 self.recv_channel(8) # ignore AST info event_id = bytes_to_bint(self.recv_channel(4)) break else: raise InternalError("_wait_for_event:op_code = %d" % (op_code, )) return (event_id, event_names)
def bytes_to_bint(b, u=False): # Read as big endian if u: fmtmap = {1: 'B', 2: '>H', 4: '>L', 8: '>Q'} else: fmtmap = {1: 'b', 2: '>h', 4: '>l', 8: '>q'} fmt = fmtmap.get(len(b)) if fmt is None: raise InternalError("Invalid data length") return struct.unpack(fmt, b)[0]
def _op_fetch_response(self, stmt_handle, xsqlda): op_code = bytes_to_bint(self.recv_channel(4)) while op_code == self.op_dummy: op_code = bytes_to_bint(self.recv_channel(4)) while op_code == self.op_response and self.lazy_response_count: self.lazy_response_count -= 1 h, oid, buf = self._parse_op_response() op_code = bytes_to_bint(self.recv_channel(4)) if op_code != self.op_fetch_response: if op_code == self.op_response: self._parse_op_response() raise InternalError("op_fetch_response:op_code = %d" % (op_code, )) b = self.recv_channel(8) status = bytes_to_bint(b[:4]) count = bytes_to_bint(b[4:8]) rows = [] while count: r = [None] * len(xsqlda) if self.accept_version < PROTOCOL_VERSION13: for i in range(len(xsqlda)): x = xsqlda[i] if x.io_length() < 0: b = self.recv_channel(4) ln = bytes_to_bint(b) else: ln = x.io_length() raw_value = self.recv_channel(ln, word_alignment=True) if self.recv_channel(4) == bs([0]) * 4: # Not NULL r[i] = x.value(raw_value) else: # PROTOCOL_VERSION13 n = len(xsqlda) // 8 if len(xsqlda) % 8 != 0: n += 1 null_indicator = 0 for c in reversed(self.recv_channel(n, word_alignment=True)): null_indicator <<= 8 null_indicator += c if PYTHON_MAJOR_VER == 3 else ord(c) for i in range(len(xsqlda)): x = xsqlda[i] if null_indicator & (1 << i): continue if x.io_length() < 0: b = self.recv_channel(4) ln = bytes_to_bint(b) else: ln = x.io_length() raw_value = self.recv_channel(ln, word_alignment=True) r[i] = x.value(raw_value) rows.append(r) b = self.recv_channel(12) op_code = bytes_to_bint(b[:4]) status = bytes_to_bint(b[4:8]) count = bytes_to_bint(b[8:]) return rows, status != 100
def _op_response(self): b = self.recv_channel(4) while bytes_to_bint(b) == self.op_dummy: b = self.recv_channel(4) op_code = bytes_to_bint(b) while op_code == self.op_response and self.lazy_response_count: self.lazy_response_count -= 1 h, oid, buf = self._parse_op_response() b = self.recv_channel(4) if op_code == self.op_cont_auth: raise OperationalError('Unauthorized') elif op_code != self.op_response: raise InternalError("_op_response:op_code = %d" % (op_code, )) return self._parse_op_response()
def _op_sql_response(self, xsqlda): b = self.recv_channel(4) while bytes_to_bint(b) == self.op_dummy: b = self.recv_channel(4) op_code = bytes_to_bint(b) if op_code != self.op_sql_response: if op_code == self.op_response: self._parse_op_response() raise InternalError("_op_sql_response:op_code = %d" % (op_code, )) b = self.recv_channel(4) count = bytes_to_bint(b[:4]) r = [] if count == 0: return [] if self.accept_version < PROTOCOL_VERSION13: for i in range(len(xsqlda)): x = xsqlda[i] if x.io_length() < 0: b = self.recv_channel(4) ln = bytes_to_bint(b) else: ln = x.io_length() raw_value = self.recv_channel(ln, word_alignment=True) if self.recv_channel(4) == bs([0]) * 4: # Not NULL r.append(x.value(raw_value)) else: r.append(None) else: n = len(xsqlda) // 8 if len(xsqlda) % 8 != 0: n += 1 null_indicator = 0 for c in reversed(self.recv_channel(n, word_alignment=True)): null_indicator <<= 8 null_indicator += c if PYTHON_MAJOR_VER == 3 else ord(c) for i in range(len(xsqlda)): x = xsqlda[i] if null_indicator & (1 << i): r.append(None) else: if x.io_length() < 0: b = self.recv_channel(4) ln = bytes_to_bint(b) else: ln = x.io_length() raw_value = self.recv_channel(ln, word_alignment=True) r.append(x.value(raw_value)) return r
def _op_event(self): b = self.recv_channel(4) while bytes_to_bint(b) == self.op_dummy: b = self.recv_channel(4) op_code = bytes_to_bint(b) if op_code == self.op_response and self.lazy_response_count: self.lazy_response_count -= 1 self._parse_op_response() b = self.recv_channel(4) if op_code == self.op_exit or bytes_to_bint(b) == self.op_exit: raise DisconnectByPeer if op_code != self.op_event: if op_code == self.op_response: self._parse_op_response() raise InternalError("_op_event:op_code = %d" % (op_code, )) return self._parse_op_event()
def bytes_to_bint(b, u=False): # Read as big endian if u: fmtmap = {1: 'B', 2: '>H', 4: '>L', 8: '>Q'} else: fmtmap = {1: 'b', 2: '>h', 4: '>l', 8: '>q'} fmt = fmtmap.get(len(b)) if fmt is None: if len(b) == 16: if u: a, b = struct.unpack('>QQ', b) else: a, b = struct.unpack('>qq', b) return (a << 64) | b raise InternalError("Invalid bytes length:%d" % (len(b), )) return struct.unpack(fmt, b)[0]
def bytes_to_uint(b): # Read as little endian unsigned int. fmtmap = {1: 'B', 2: '<H', 4: '<L', 8: '<Q'} fmt = fmtmap.get(len(b)) if fmt is None: raise InternalError("Invalid bytes length:%d" % (len(b), )) return struct.unpack(fmt, b)[0]
def bytes_to_int(b): # Read as little endian. fmtmap = {1: 'b', 2: '<h', 4: '<l', 8: '<q'} fmt = fmtmap.get(len(b)) if fmt is None: raise InternalError("Invalid bytes length:%d" % (len(b), )) return struct.unpack(fmt, b)[0]