def _set_guc(self, name, value): """Set the value of a configuration parameter.""" if value.lower() != 'default': value = util.quote_string(self, value) else: value = b'default' self._execute_command(ascii_to_bytes('SET %s TO ' % name) + value)
def parse_time(value, length, cursor): if value is None: return None if isinstance(value, six.text_type): value = ascii_to_bytes(value) try: return datetime.time(*_parse_time_to_args(value, cursor)) except (TypeError, ValueError): raise DataError("bad datetime: '%s'" % value)
def _get_guc(self, name): """Return the value of a configuration parameter.""" with self._lock: query = 'SHOW %s' % name if _green_callback: pgres = self._execute_green(query) else: pgres = libpq.PQexec(self._pgconn, ascii_to_bytes(query)) if not pgres or libpq.PQresultStatus(pgres) != libpq.PGRES_TUPLES_OK: raise exceptions.OperationalError("can't fetch %s" % name) rv = bytes_to_ascii(ffi.string(libpq.PQgetvalue(pgres, 0, 0))) libpq.PQclear(pgres) return rv
def parse_date(value, length, cursor): if value is None: return None if isinstance(value, six.text_type): value = ascii_to_bytes(value) if value == b'infinity': return datetime.date.max elif value == b'-infinity': return datetime.date.min try: return datetime.date(*[int(x) for x in value.split(b'-')]) except (TypeError, ValueError): if value.endswith(b'BC'): raise ValueError('BC dates not supported') raise DataError("bad datetime: '%s'" % bytes_to_ascii(value))
def _connect_async(self): """Create an async connection. The connection will be completed banging on poll(): First with self._conn_poll_connecting() that will finish connection, then with self._poll_setup_async() that will do the same job of self._setup(). """ self._pgconn = libpq.PQconnectStart(ascii_to_bytes(self.dsn)) if not self._pgconn: raise exceptions.OperationalError('PQconnectStart() failed') elif libpq.PQstatus(self._pgconn) == libpq.CONNECTION_BAD: raise self._create_exception() libpq.PQsetNoticeProcessor(self._pgconn, self._notice_callback, ffi.NULL)
def _connect_async(self): """Create an async connection. The connection will be completed banging on poll(): First with self._conn_poll_connecting() that will finish connection, then with self._poll_setup_async() that will do the same job of self._setup(). """ self._pgconn = libpq.PQconnectStart(ascii_to_bytes(self.dsn)) if not self._pgconn: raise exceptions.OperationalError('PQconnectStart() failed') elif libpq.PQstatus(self._pgconn) == libpq.CONNECTION_BAD: raise self._create_exception() libpq.PQsetNoticeProcessor( self._pgconn, self._notice_callback, ffi.NULL)
def _execute_command(self, command): with self._lock: if _green_callback: pgres = self._execute_green(command) else: pgres = libpq.PQexec(self._pgconn, ascii_to_bytes(command)) if not pgres: raise self._create_exception() try: pgstatus = libpq.PQresultStatus(pgres) if pgstatus != libpq.PGRES_COMMAND_OK: exc = self._create_exception(pgres=pgres) pgres = None # ownership transferred to exc raise exc finally: if pgres: libpq.PQclear(pgres)
def parse_datetime(value, length, cursor): if value is None: return None if isinstance(value, six.text_type): value = ascii_to_bytes(value) if value == b'infinity': return datetime.datetime.max elif value == b'-infinity': return datetime.datetime.min try: date, time = value.split(b' ') date_args = date.split(b'-') return datetime.datetime(int(date_args[0]), int(date_args[1]), int(date_args[2]), *_parse_time_to_args(time, cursor)) except (TypeError, ValueError): if value.endswith(b'BC'): raise ValueError('BC dates not supported') raise DataError("bad datetime: '%s'" % bytes_to_ascii(value))
def parse_datetime(value, length, cursor): if value is None: return None if isinstance(value, six.text_type): value = ascii_to_bytes(value) if value == b'infinity': return datetime.datetime.max elif value == b'-infinity': return datetime.datetime.min try: date, time = value.split(b' ') date_args = date.split(b'-') return datetime.datetime( int(date_args[0]), int(date_args[1]), int(date_args[2]), *_parse_time_to_args(time, cursor)) except (TypeError, ValueError): if value.endswith(b'BC'): raise ValueError('BC dates not supported') raise DataError("bad datetime: '%s'" % bytes_to_ascii(value))
def parse_interval(value, length, cursor): """Typecast an interval to a datetime.timedelta instance. For example, the value '2 years 1 mon 3 days 10:01:39.100' is converted to `datetime.timedelta(763, 36099, 100)`. """ if value is None: return None if isinstance(value, six.text_type): value = ascii_to_bytes(value) m = _re_interval.match(value) if not m: raise ValueError("failed to parse interval: '%s'" % value) years, months, days, sign, hours, mins, secs, frac = m.groups() days = int(days) if days is not None else 0 if months is not None: days += int(months) * 30 if years is not None: days += int(years) * 365 if hours is not None: secs = int(hours) * 3600 + int(mins) * 60 + int(secs) if frac is not None: micros = int((frac + (b'0' * (6 - len(frac))))[:6]) else: micros = 0 if sign == b'-': secs = -secs micros = -micros else: secs = micros = 0 return datetime.timedelta(days, secs, micros)
def _execute_green(self, query): """Execute version for green threads""" if self._async_cursor: raise exceptions.ProgrammingError( "a single async query can be executed on the same connection") self._async_cursor = True if not libpq.PQsendQuery(self._pgconn, ascii_to_bytes(query)): self._async_cursor = None return self._async_status = consts.ASYNC_WRITE try: _green_callback(self) except Exception: self.close() raise else: return util.pq_get_last_result(self._pgconn) finally: self._async_cursor = None self._async_status = consts.ASYNC_DONE
def getquoted(self): return b''.join([b"'", ascii_to_bytes(str(self._uuid)), b"'::uuid"])
def _execute_tpc_command(self, command, xid): cmd = b' '.join( [ascii_to_bytes(command), util.quote_string(self, str(xid))]) self._execute_command(cmd) self._mark += 1
def get_parameter_status(self, parameter): p = libpq.PQparameterStatus(self._pgconn, ascii_to_bytes(parameter)) return bytes_to_ascii(ffi.string(p)) if p != ffi.NULL else None
def _execute_tpc_command(self, command, xid): cmd = b' '.join([ ascii_to_bytes(command), util.quote_string(self, str(xid))]) self._execute_command(cmd) self._mark += 1