summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/psycopg_any.py
blob: e9bb84f31396540eee5254cec9df0d38cc041ee0 (plain)
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
from enum import IntEnum

from psycopg2 import errors, extensions, sql  # NOQA
from psycopg2.extras import DateRange, DateTimeRange, DateTimeTZRange, Inet  # NOQA
from psycopg2.extras import Json as Jsonb  # NOQA
from psycopg2.extras import NumericRange, Range  # NOQA

RANGE_TYPES = (DateRange, DateTimeRange, DateTimeTZRange, NumericRange)


class IsolationLevel(IntEnum):
    READ_UNCOMMITTED = extensions.ISOLATION_LEVEL_READ_UNCOMMITTED
    READ_COMMITTED = extensions.ISOLATION_LEVEL_READ_COMMITTED
    REPEATABLE_READ = extensions.ISOLATION_LEVEL_REPEATABLE_READ
    SERIALIZABLE = extensions.ISOLATION_LEVEL_SERIALIZABLE


def _quote(value, connection=None):
    adapted = extensions.adapt(value)
    if hasattr(adapted, "encoding"):
        adapted.encoding = "utf8"
    # getquoted() returns a quoted bytestring of the adapted value.
    return adapted.getquoted().decode()


sql.quote = _quote


def mogrify(sql, params, connection):
    with connection.cursor() as cursor:
        return cursor.mogrify(sql, params).decode()