How to use the pg8000.ProgrammingError function in pg8000

To help you get started, we’ve selected a few pg8000 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mfenniak / pg8000 / tests / test_connection.py View on Github external
cur.execute("drop role " + username)
        except pg8000.ProgrammingError:
            cur.execute("rollback")

        cur.execute(
            "create user " + username + " with password '" + password + "';")
        cur.execute('commit;')
        db.close()

        data = db_connect.copy()
        data['user'] = username
        data['password'] = password.encode('utf8')
        data['database'] = 'pg8000_md5'
        if PY2:
            self.assertRaises(
                pg8000.ProgrammingError, pg8000.connect, **data)
        else:
            self.assertRaisesRegex(
                pg8000.ProgrammingError, '3D000', pg8000.connect, **data)

        db = pg8000.connect(**db_connect)
        cur = db.cursor()
        cur.execute("drop role " + username)
        cur.execute("commit;")
        db.close()
github postgrespro / testgres / testgres / testgres.py View on Github external
if expected is None and res is None:
                    return  # done

                if res is None:
                    raise QueryException('Query returned None')

                if len(res) == 0:
                    raise QueryException('Query returned 0 rows')

                if len(res[0]) == 0:
                    raise QueryException('Query returned 0 columns')

                if res[0][0]:
                    return  # done

            except pglib.ProgrammingError as e:
                if raise_programming_error:
                    raise e

            except pglib.InternalError as e:
                if raise_internal_error:
                    raise e

            time.sleep(sleep_time)
            attempts += 1

        raise TimeoutException('Query timeout')
github mfenniak / pg8000 / pg8000.py View on Github external
def iterate_tuple(self):
        if self._stmt == None:
            raise ProgrammingError("attempting to read from unexecuted cursor")
        return self._stmt.iterate_tuple()
github smartsdk / ngsi-timeseries-api / src / translators / timescale.py View on Github external
def _should_insert_original_entities(self, insert_error: Exception) -> bool:
        return isinstance(insert_error, pg8000.ProgrammingError)
github DataDog / integrations-core / postgres / datadog_checks / postgres / postgres.py View on Github external
def _get_pg_attrs(self, instance):
        if _is_affirmative(instance.get('use_psycopg2', False)):
            if psycopg2 is None:
                self.log.error("Unable to import psycopg2, falling back to pg8000")
            else:
                return psycopg2_connect, psycopg2.InterfaceError, psycopg2.ProgrammingError

        # Let's use pg8000
        return pg8000.connect, pg8000.InterfaceError, pg8000.ProgrammingError
github mfenniak / pg8000 / pg8000.py View on Github external
if end_idx == -1:
                                raise ProgrammingError("began pyformat dict read, but couldn't find end of name")
                            else:
                                name = query[i:end_idx]
                                i = end_idx + 1
                                if i < len(query) and query[i] == "s":
                                    i += 1
                                    idx = mapping_to_idx.get(name)
                                    if idx == None:
                                        idx = len(output_args)
                                        output_args.append(args[name])
                                        idx += 1
                                        mapping_to_idx[name] = idx
                                    output_query += "$" + str(idx)
                                else:
                                    raise ProgrammingError("format not specified or not supported (only %(...)s supported)")
                        elif query[i] == "%":
                            output_query += "%"
                else:
                    i += 1
                    output_query += c
            elif state == 1:
                output_query += c
                i += 1
                if c == "'":
                    # Could be a double ''
                    if i < len(query) and query[i] == "'":
                        # is a double quote.
                        output_query += query[i]
                        i += 1
                    else:
                        state = 0
github mfenniak / pg8000 / pg8000.py View on Github external
def read_tuple(self):
        if self._stmt == None:
            raise ProgrammingError("attempting to read from unexecuted cursor")
        return self._stmt.read_tuple()
github mfenniak / pg8000 / pg8000.py View on Github external
idx += 1
                        mapping_to_idx[name] = idx
                    output_query += "$" + str(idx)
                elif src_style == "format" and c == "%":
                    i += 1
                    if i < len(query) and i > 1:
                        if query[i] == "s":
                            param_idx = len(output_args)
                            if param_idx == len(args):
                                raise ProgrammingError("too many parameter fields, not enough parameters")
                            output_args.append(args[param_idx])
                            output_query += "$" + str(param_idx + 1)
                        elif query[i] == "%":
                            output_query += "%"
                        else:
                            raise ProgrammingError("Only %s and %% are supported")
                        i += 1
                    else:
                        raise ProgrammingError("numeric parameter : does not have numeric arg")
                elif src_style == "pyformat" and c == "%":
                    i += 1
                    if i < len(query) and i > 1:
                        if query[i] == "(":
                            i += 1
                            # begin mapping name
                            end_idx = query.find(')', i)
                            if end_idx == -1:
                                raise ProgrammingError("began pyformat dict read, but couldn't find end of name")
                            else:
                                name = query[i:end_idx]
                                i = end_idx + 1
                                if i < len(query) and query[i] == "s":
github mfenniak / pg8000 / pg8000.py View on Github external
def createException(self):
            return ProgrammingError(self.severity, self.code, self.msg)
github serverdensity / sd-agent / checks.d / postgres.py View on Github external
# we must remember that order to parse results

                try:
                    # if this is a relation-specific query, we need to list all relations last
                    if scope['relation'] and len(relations) > 0:
                        relnames = relations_config.keys()
                        query = scope['query'] % (", ".join(cols), "%s")  # Keep the last %s intact
                        self.log.debug("Running query: %s with relations: %s" % (query, relnames))
                        cursor.execute(query, (relnames, ))
                    else:
                        query = scope['query'] % (", ".join(cols))
                        self.log.debug("Running query: %s" % query)
                        cursor.execute(query.replace(r'%', r'%%'))

                    results = cursor.fetchall()
                except ProgrammingError, e:
                    log_func("Not all metrics may be available: %s" % str(e))
                    continue

                if not results:
                    continue

                if scope in custom_metrics and len(results) > MAX_CUSTOM_RESULTS:
                    self.warning(
                        "Query: {0} returned more than {1} results ({2}). Truncating"
                        .format(query, MAX_CUSTOM_RESULTS, len(results))
                    )
                    results = results[:MAX_CUSTOM_RESULTS]

                # FIXME this cramps my style
                if scope == self.DB_METRICS:
                    self.gauge("postgresql.db.count", len(results),