1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
from __future__ import unicode_literals
import six
from psycopg2cffi._impl import exceptions
from psycopg2cffi._impl.libpq import libpq
from psycopg2cffi._impl.adapters import QuotedString, ascii_to_bytes, \
bytes_to_ascii
def pq_set_non_blocking(pgconn, arg, raise_exception=False):
ret = libpq.PQsetnonblocking(pgconn, arg)
if ret != 0 and raise_exception:
raise exceptions.OperationalError('PQsetnonblocking() failed')
return ret
def pq_clear_async(conn):
pgconn = conn._pgconn
while True:
pgres = libpq.PQgetResult(pgconn)
if not pgres:
break
if libpq.PQresultStatus(pgres) == libpq.PGRES_FATAL_ERROR:
raise conn._create_exception()
libpq.PQclear(pgres)
def pq_get_last_result(pgconn):
pgres_next = None
pgres = libpq.PQgetResult(pgconn)
if not pgres:
return
while True:
pgres_next = libpq.PQgetResult(pgconn)
if not pgres_next:
break
if pgres:
libpq.PQclear(pgres)
pgres = pgres_next
return pgres
def quote_string(conn, value):
obj = QuotedString(value)
obj.prepare(conn)
return obj.getquoted()
def get_exception_for_sqlstate(code):
"""Translate the sqlstate to a relevant exception.
See for a list of possible errors:
http://www.postgresql.org/docs/current/static/errcodes-appendix.html
"""
if isinstance(code, six.binary_type):
code = bytes_to_ascii(code)
if code[0] == '0':
# Class 0A - Feature Not Supported
if code[1] == 'A':
return exceptions.NotSupportedError
elif code[0] == '2':
# Class 20 - Case Not Found
# Class 21 - Cardinality Violation
if code[1] in '01':
return exceptions.ProgrammingError
# Class 22 - Data Exception
if code[1] == '2':
return exceptions.DataError
# Class 23 - Integrity Constraint Violation
if code[1] == '3':
return exceptions.IntegrityError
# Class 24 - Invalid Cursor State
# Class 25 - Invalid Transaction State
if code[1] in '45':
return exceptions.InternalError
# Class 26 - Invalid SQL Statement Name
# Class 27 - Triggered Data Change Violation
# Class 28 - Invalid Authorization Specification
if code[1] in '678':
return exceptions.OperationalError
# Class 2B - Dependent Privilege Descriptors Still Exist
# Class 2D - Invalid Transaction Termination
# Class 2F - SQL Routine Exception
if code[1] in 'BDF':
return exceptions.InternalError
elif code[0] == '3':
# Class 34 - Invalid Cursor Name
if code[1] == '4':
return exceptions.OperationalError
# Class 38 - External Routine Exception
# Class 39 - External Routine Invocation Exception
# Class 3B - Savepoint Exception
if code[1] in '89B':
return exceptions.InternalError
# Class 3D - Invalid Catalog Name
# Class 3F - Invalid Schema Name
if code[1] in 'DF':
return exceptions.ProgrammingError
elif code[0] == '4':
# Class 40 - Transaction Rollback
if code[1] == '0':
return exceptions.TransactionRollbackError
# Class 42 - Syntax Error or Access Rule Violation
# Class 44 - WITH CHECK OPTION Violation
if code[1] in '24':
return exceptions.ProgrammingError
elif code[0] == '5':
if code == '57014':
return exceptions.QueryCanceledError
# Class 53 - Insufficient Resources
# Class 54 - Program Limit Exceeded
# Class 55 - Object Not In Prerequisite State
# Class 57 - Operator Intervention
# Class 58 - System Error (errors external to PostgreSQL itself)
return exceptions.OperationalError
elif code[0] == 'F':
# Class F0 - Configuration File Error
return exceptions.InternalError
elif code[0] == 'H':
# Class HV - Foreign Data Wrapper Error (SQL/MED)
return exceptions.OperationalError
elif code[0] == 'P':
# Class P0 - PL/pgSQL Error
return exceptions.InternalError
elif code[0] == 'X':
# Class XX - Internal Error
return exceptions.InternalError
# Fallback exception
return exceptions.DatabaseError
|