Hacked By AnonymousFox

Current Path : /opt/cloudlinux/venv/lib64/python3.11/site-packages/sqlalchemy/dialects/postgresql/__pycache__/
Upload File :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/sqlalchemy/dialects/postgresql/__pycache__/base.cpython-311.pyc

�

�܋f��
��dZddlmZddlZddlZddlmZddlm	Z
ddlmZddlm
Zd	d
lmZd	dlmZd	dlmZd	d
lmZd	dlmZd	dlmZd	dlmZd	dlmZd	dlmZd	dlmZd	d
lmZd	dlmZd	dlmZd	dlmZd	dlm Z d	dlm!Z!d	dlm"Z"d	dlm#Z#d	dlm$Z$d	dlm%Z%d	dlm&Z&d	dlm'Z'd	dlm(Z(	dd l)m*Z+n
#e,$rdZ+YnwxYwej-d!ej.��Z/ej-d"ej.ej0z��Z1e2gd#���Z3d$Z4d%Z5d&Z6Gd'�d(ej7��Z8Gd)�d*ej9��Z:Gd+�d,ej;��Z<e<Z=Gd-�d.ej;��Z>e>Z?Gd/�d0ej;��Z@e@ZAGd1�d2ej;��ZBGd3�d4ej;��ZCGd5�d6ej;��ZDGd7�d8ejE��ZEGd9�d:ejF��ZFGd;�d<ejGejH��ZIeIZJGd=�d>ej;��ZKeKZLGd?�d@ej;��Z*e*ZMGdA�dBej;��ZNGdC�dDejGejO��ZPejQejQejReIejOePejSjTejTejSejSiZUidEejQ�dFe
jV�dGejS�dHejW�dIejX�dJejY�dKejZ�dLej[�dMej\�dNej]�dOe#�dPe�dQe&�dRe(�dSe �dTej^�dUej^�idVe'�dWe$�dXe"�dYe%�dZe<�d[e>�d\e*�d]eK�d^eK�d_e@�d`eB�daeC�dbeD�dce:�ddeE�deeE�dfeE��eFeFe!eFe8eeIeNdg��Z_Gdh�diej`��ZaGdj�dkejb��ZcGdl�dmejd��ZeGdn�doejf��ZgGdp�dqejh��ZiGdr�dsejj��ZkGdt�duejj��ZlGdv�dwejm��ZnGdx�dyejo��ZpdS)za��
.. dialect:: postgresql
    :name: PostgreSQL

.. _postgresql_sequences:

Sequences/SERIAL/IDENTITY
-------------------------

PostgreSQL supports sequences, and SQLAlchemy uses these as the default means
of creating new primary key values for integer-based primary key columns. When
creating tables, SQLAlchemy will issue the ``SERIAL`` datatype for
integer-based primary key columns, which generates a sequence and server side
default corresponding to the column.

To specify a specific named sequence to be used for primary key generation,
use the :func:`~sqlalchemy.schema.Sequence` construct::

    Table('sometable', metadata,
            Column('id', Integer, Sequence('some_id_seq'), primary_key=True)
        )

When SQLAlchemy issues a single INSERT statement, to fulfill the contract of
having the "last insert identifier" available, a RETURNING clause is added to
the INSERT statement which specifies the primary key columns should be
returned after the statement completes. The RETURNING functionality only takes
place if PostgreSQL 8.2 or later is in use. As a fallback approach, the
sequence, whether specified explicitly or implicitly via ``SERIAL``, is
executed independently beforehand, the returned value to be used in the
subsequent insert. Note that when an
:func:`~sqlalchemy.sql.expression.insert()` construct is executed using
"executemany" semantics, the "last inserted identifier" functionality does not
apply; no RETURNING clause is emitted nor is the sequence pre-executed in this
case.

To force the usage of RETURNING by default off, specify the flag
``implicit_returning=False`` to :func:`_sa.create_engine`.

PostgreSQL 10 IDENTITY columns
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

PostgreSQL 10 has a new IDENTITY feature that supersedes the use of SERIAL.
Built-in support for rendering of IDENTITY is not available yet, however the
following compilation hook may be used to replace occurrences of SERIAL with
IDENTITY::

    from sqlalchemy.schema import CreateColumn
    from sqlalchemy.ext.compiler import compiles


    @compiles(CreateColumn, 'postgresql')
    def use_identity(element, compiler, **kw):
        text = compiler.visit_create_column(element, **kw)
        text = text.replace("SERIAL", "INT GENERATED BY DEFAULT AS IDENTITY")
        return text

Using the above, a table such as::

    t = Table(
        't', m,
        Column('id', Integer, primary_key=True),
        Column('data', String)
    )

Will generate on the backing database as::

    CREATE TABLE t (
        id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
        data VARCHAR,
        PRIMARY KEY (id)
    )

.. _postgresql_isolation_level:

Transaction Isolation Level
---------------------------

Most SQLAlchemy dialects support setting of transaction isolation level
using the :paramref:`_sa.create_engine.execution_options` parameter
at the :func:`_sa.create_engine` level, and at the :class:`_engine.Connection`
level via the :paramref:`.Connection.execution_options.isolation_level`
parameter.

For PostgreSQL dialects, this feature works either by making use of the
DBAPI-specific features, such as psycopg2's isolation level flags which will
embed the isolation level setting inline with the ``"BEGIN"`` statement, or for
DBAPIs with no direct support by emitting ``SET SESSION CHARACTERISTICS AS
TRANSACTION ISOLATION LEVEL <level>`` ahead of the ``"BEGIN"`` statement
emitted by the DBAPI.   For the special AUTOCOMMIT isolation level,
DBAPI-specific techniques are used which is typically an ``.autocommit``
flag on the DBAPI connection object.

To set isolation level using :func:`_sa.create_engine`::

    engine = create_engine(
        "postgresql+pg8000://scott:tiger@localhost/test",
        execution_options={
            "isolation_level": "REPEATABLE READ"
        }
    )

To set using per-connection execution options::

    with engine.connect() as conn:
        conn = conn.execution_options(
            isolation_level="REPEATABLE READ"
        )
        with conn.begin():
            # ... work with transaction

Valid values for ``isolation_level`` on most PostgreSQL dialects include:

* ``READ COMMITTED``
* ``READ UNCOMMITTED``
* ``REPEATABLE READ``
* ``SERIALIZABLE``
* ``AUTOCOMMIT``

.. seealso::

    :ref:`dbapi_autocommit`

    :ref:`psycopg2_isolation_level`

    :ref:`pg8000_isolation_level`

.. _postgresql_schema_reflection:

Remote-Schema Table Introspection and PostgreSQL search_path
------------------------------------------------------------

**TL;DR;**: keep the ``search_path`` variable set to its default of ``public``,
name schemas **other** than ``public`` explicitly within ``Table`` definitions.

The PostgreSQL dialect can reflect tables from any schema.  The
:paramref:`_schema.Table.schema` argument, or alternatively the
:paramref:`.MetaData.reflect.schema` argument determines which schema will
be searched for the table or tables.   The reflected :class:`_schema.Table`
objects
will in all cases retain this ``.schema`` attribute as was specified.
However, with regards to tables which these :class:`_schema.Table`
objects refer to
via foreign key constraint, a decision must be made as to how the ``.schema``
is represented in those remote tables, in the case where that remote
schema name is also a member of the current
`PostgreSQL search path
<http://www.postgresql.org/docs/current/static/ddl-schemas.html#DDL-SCHEMAS-PATH>`_.

By default, the PostgreSQL dialect mimics the behavior encouraged by
PostgreSQL's own ``pg_get_constraintdef()`` builtin procedure.  This function
returns a sample definition for a particular foreign key constraint,
omitting the referenced schema name from that definition when the name is
also in the PostgreSQL schema search path.  The interaction below
illustrates this behavior::

    test=> CREATE TABLE test_schema.referred(id INTEGER PRIMARY KEY);
    CREATE TABLE
    test=> CREATE TABLE referring(
    test(>         id INTEGER PRIMARY KEY,
    test(>         referred_id INTEGER REFERENCES test_schema.referred(id));
    CREATE TABLE
    test=> SET search_path TO public, test_schema;
    test=> SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM
    test-> pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n
    test-> ON n.oid = c.relnamespace
    test-> JOIN pg_catalog.pg_constraint r  ON c.oid = r.conrelid
    test-> WHERE c.relname='referring' AND r.contype = 'f'
    test-> ;
                   pg_get_constraintdef
    ---------------------------------------------------
     FOREIGN KEY (referred_id) REFERENCES referred(id)
    (1 row)

Above, we created a table ``referred`` as a member of the remote schema
``test_schema``, however when we added ``test_schema`` to the
PG ``search_path`` and then asked ``pg_get_constraintdef()`` for the
``FOREIGN KEY`` syntax, ``test_schema`` was not included in the output of
the function.

On the other hand, if we set the search path back to the typical default
of ``public``::

    test=> SET search_path TO public;
    SET

The same query against ``pg_get_constraintdef()`` now returns the fully
schema-qualified name for us::

    test=> SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM
    test-> pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n
    test-> ON n.oid = c.relnamespace
    test-> JOIN pg_catalog.pg_constraint r  ON c.oid = r.conrelid
    test-> WHERE c.relname='referring' AND r.contype = 'f';
                         pg_get_constraintdef
    ---------------------------------------------------------------
     FOREIGN KEY (referred_id) REFERENCES test_schema.referred(id)
    (1 row)

SQLAlchemy will by default use the return value of ``pg_get_constraintdef()``
in order to determine the remote schema name.  That is, if our ``search_path``
were set to include ``test_schema``, and we invoked a table
reflection process as follows::

    >>> from sqlalchemy import Table, MetaData, create_engine
    >>> engine = create_engine("postgresql://scott:tiger@localhost/test")
    >>> with engine.connect() as conn:
    ...     conn.execute("SET search_path TO test_schema, public")
    ...     meta = MetaData()
    ...     referring = Table('referring', meta,
    ...                       autoload=True, autoload_with=conn)
    ...
    <sqlalchemy.engine.result.ResultProxy object at 0x101612ed0>

The above process would deliver to the :attr:`_schema.MetaData.tables`
collection
``referred`` table named **without** the schema::

    >>> meta.tables['referred'].schema is None
    True

To alter the behavior of reflection such that the referred schema is
maintained regardless of the ``search_path`` setting, use the
``postgresql_ignore_search_path`` option, which can be specified as a
dialect-specific argument to both :class:`_schema.Table` as well as
:meth:`_schema.MetaData.reflect`::

    >>> with engine.connect() as conn:
    ...     conn.execute("SET search_path TO test_schema, public")
    ...     meta = MetaData()
    ...     referring = Table('referring', meta, autoload=True,
    ...                       autoload_with=conn,
    ...                       postgresql_ignore_search_path=True)
    ...
    <sqlalchemy.engine.result.ResultProxy object at 0x1016126d0>

We will now have ``test_schema.referred`` stored as schema-qualified::

    >>> meta.tables['test_schema.referred'].schema
    'test_schema'

.. sidebar:: Best Practices for PostgreSQL Schema reflection

    The description of PostgreSQL schema reflection behavior is complex, and
    is the product of many years of dealing with widely varied use cases and
    user preferences. But in fact, there's no need to understand any of it if
    you just stick to the simplest use pattern: leave the ``search_path`` set
    to its default of ``public`` only, never refer to the name ``public`` as
    an explicit schema name otherwise, and refer to all other schema names
    explicitly when building up a :class:`_schema.Table` object.  The options
    described here are only for those users who can't, or prefer not to, stay
    within these guidelines.

Note that **in all cases**, the "default" schema is always reflected as
``None``. The "default" schema on PostgreSQL is that which is returned by the
PostgreSQL ``current_schema()`` function.  On a typical PostgreSQL
installation, this is the name ``public``.  So a table that refers to another
which is in the ``public`` (i.e. default) schema will always have the
``.schema`` attribute set to ``None``.

.. versionadded:: 0.9.2 Added the ``postgresql_ignore_search_path``
   dialect-level option accepted by :class:`_schema.Table` and
   :meth:`_schema.MetaData.reflect`.


.. seealso::

    `The Schema Search Path
    <http://www.postgresql.org/docs/9.0/static/ddl-schemas.html#DDL-SCHEMAS-PATH>`_
    - on the PostgreSQL website.

INSERT/UPDATE...RETURNING
-------------------------

The dialect supports PG 8.2's ``INSERT..RETURNING``, ``UPDATE..RETURNING`` and
``DELETE..RETURNING`` syntaxes.   ``INSERT..RETURNING`` is used by default
for single-row INSERT statements in order to fetch newly generated
primary key identifiers.   To specify an explicit ``RETURNING`` clause,
use the :meth:`._UpdateBase.returning` method on a per-statement basis::

    # INSERT..RETURNING
    result = table.insert().returning(table.c.col1, table.c.col2).\
        values(name='foo')
    print(result.fetchall())

    # UPDATE..RETURNING
    result = table.update().returning(table.c.col1, table.c.col2).\
        where(table.c.name=='foo').values(name='bar')
    print(result.fetchall())

    # DELETE..RETURNING
    result = table.delete().returning(table.c.col1, table.c.col2).\
        where(table.c.name=='foo')
    print(result.fetchall())

.. _postgresql_insert_on_conflict:

INSERT...ON CONFLICT (Upsert)
------------------------------

Starting with version 9.5, PostgreSQL allows "upserts" (update or insert) of
rows into a table via the ``ON CONFLICT`` clause of the ``INSERT`` statement. A
candidate row will only be inserted if that row does not violate any unique
constraints.  In the case of a unique constraint violation, a secondary action
can occur which can be either "DO UPDATE", indicating that the data in the
target row should be updated, or "DO NOTHING", which indicates to silently skip
this row.

Conflicts are determined using existing unique constraints and indexes.  These
constraints may be identified either using their name as stated in DDL,
or they may be *inferred* by stating the columns and conditions that comprise
the indexes.

SQLAlchemy provides ``ON CONFLICT`` support via the PostgreSQL-specific
:func:`_postgresql.insert()` function, which provides
the generative methods :meth:`~.postgresql.Insert.on_conflict_do_update`
and :meth:`~.postgresql.Insert.on_conflict_do_nothing`::

    from sqlalchemy.dialects.postgresql import insert

    insert_stmt = insert(my_table).values(
        id='some_existing_id',
        data='inserted value')

    do_nothing_stmt = insert_stmt.on_conflict_do_nothing(
        index_elements=['id']
    )

    conn.execute(do_nothing_stmt)

    do_update_stmt = insert_stmt.on_conflict_do_update(
        constraint='pk_my_table',
        set_=dict(data='updated value')
    )

    conn.execute(do_update_stmt)

Both methods supply the "target" of the conflict using either the
named constraint or by column inference:

* The :paramref:`.Insert.on_conflict_do_update.index_elements` argument
  specifies a sequence containing string column names, :class:`_schema.Column`
  objects, and/or SQL expression elements, which would identify a unique
  index::

    do_update_stmt = insert_stmt.on_conflict_do_update(
        index_elements=['id'],
        set_=dict(data='updated value')
    )

    do_update_stmt = insert_stmt.on_conflict_do_update(
        index_elements=[my_table.c.id],
        set_=dict(data='updated value')
    )

* When using :paramref:`.Insert.on_conflict_do_update.index_elements` to
  infer an index, a partial index can be inferred by also specifying the
  use the :paramref:`.Insert.on_conflict_do_update.index_where` parameter::

    from sqlalchemy.dialects.postgresql import insert

    stmt = insert(my_table).values(user_email='a@b.com', data='inserted data')
    stmt = stmt.on_conflict_do_update(
        index_elements=[my_table.c.user_email],
        index_where=my_table.c.user_email.like('%@gmail.com'),
        set_=dict(data=stmt.excluded.data)
        )
    conn.execute(stmt)

* The :paramref:`.Insert.on_conflict_do_update.constraint` argument is
  used to specify an index directly rather than inferring it.  This can be
  the name of a UNIQUE constraint, a PRIMARY KEY constraint, or an INDEX::

    do_update_stmt = insert_stmt.on_conflict_do_update(
        constraint='my_table_idx_1',
        set_=dict(data='updated value')
    )

    do_update_stmt = insert_stmt.on_conflict_do_update(
        constraint='my_table_pk',
        set_=dict(data='updated value')
    )

* The :paramref:`.Insert.on_conflict_do_update.constraint` argument may
  also refer to a SQLAlchemy construct representing a constraint,
  e.g. :class:`.UniqueConstraint`, :class:`.PrimaryKeyConstraint`,
  :class:`.Index`, or :class:`.ExcludeConstraint`.   In this use,
  if the constraint has a name, it is used directly.  Otherwise, if the
  constraint is unnamed, then inference will be used, where the expressions
  and optional WHERE clause of the constraint will be spelled out in the
  construct.  This use is especially convenient
  to refer to the named or unnamed primary key of a :class:`_schema.Table`
  using the
  :attr:`_schema.Table.primary_key` attribute::

    do_update_stmt = insert_stmt.on_conflict_do_update(
        constraint=my_table.primary_key,
        set_=dict(data='updated value')
    )

``ON CONFLICT...DO UPDATE`` is used to perform an update of the already
existing row, using any combination of new values as well as values
from the proposed insertion.   These values are specified using the
:paramref:`.Insert.on_conflict_do_update.set_` parameter.  This
parameter accepts a dictionary which consists of direct values
for UPDATE::

    from sqlalchemy.dialects.postgresql import insert

    stmt = insert(my_table).values(id='some_id', data='inserted value')
    do_update_stmt = stmt.on_conflict_do_update(
        index_elements=['id'],
        set_=dict(data='updated value')
        )
    conn.execute(do_update_stmt)

.. warning::

    The :meth:`_expression.Insert.on_conflict_do_update`
    method does **not** take into
    account Python-side default UPDATE values or generation functions, e.g.
    those specified using :paramref:`_schema.Column.onupdate`.
    These values will not be exercised for an ON CONFLICT style of UPDATE,
    unless they are manually specified in the
    :paramref:`.Insert.on_conflict_do_update.set_` dictionary.

In order to refer to the proposed insertion row, the special alias
:attr:`~.postgresql.Insert.excluded` is available as an attribute on
the :class:`_postgresql.Insert` object; this object is a
:class:`_expression.ColumnCollection`
which alias contains all columns of the target
table::

    from sqlalchemy.dialects.postgresql import insert

    stmt = insert(my_table).values(
        id='some_id',
        data='inserted value',
        author='jlh')
    do_update_stmt = stmt.on_conflict_do_update(
        index_elements=['id'],
        set_=dict(data='updated value', author=stmt.excluded.author)
        )
    conn.execute(do_update_stmt)

The :meth:`_expression.Insert.on_conflict_do_update` method also accepts
a WHERE clause using the :paramref:`.Insert.on_conflict_do_update.where`
parameter, which will limit those rows which receive an UPDATE::

    from sqlalchemy.dialects.postgresql import insert

    stmt = insert(my_table).values(
        id='some_id',
        data='inserted value',
        author='jlh')
    on_update_stmt = stmt.on_conflict_do_update(
        index_elements=['id'],
        set_=dict(data='updated value', author=stmt.excluded.author)
        where=(my_table.c.status == 2)
        )
    conn.execute(on_update_stmt)

``ON CONFLICT`` may also be used to skip inserting a row entirely
if any conflict with a unique or exclusion constraint occurs; below
this is illustrated using the
:meth:`~.postgresql.Insert.on_conflict_do_nothing` method::

    from sqlalchemy.dialects.postgresql import insert

    stmt = insert(my_table).values(id='some_id', data='inserted value')
    stmt = stmt.on_conflict_do_nothing(index_elements=['id'])
    conn.execute(stmt)

If ``DO NOTHING`` is used without specifying any columns or constraint,
it has the effect of skipping the INSERT for any unique or exclusion
constraint violation which occurs::

    from sqlalchemy.dialects.postgresql import insert

    stmt = insert(my_table).values(id='some_id', data='inserted value')
    stmt = stmt.on_conflict_do_nothing()
    conn.execute(stmt)

.. versionadded:: 1.1 Added support for PostgreSQL ON CONFLICT clauses

.. seealso::

    `INSERT .. ON CONFLICT
    <http://www.postgresql.org/docs/current/static/sql-insert.html#SQL-ON-CONFLICT>`_
    - in the PostgreSQL documentation.

.. _postgresql_match:

Full Text Search
----------------

SQLAlchemy makes available the PostgreSQL ``@@`` operator via the
:meth:`_expression.ColumnElement.match`
method on any textual column expression.
On a PostgreSQL dialect, an expression like the following::

    select([sometable.c.text.match("search string")])

will emit to the database::

    SELECT text @@ to_tsquery('search string') FROM table

The PostgreSQL text search functions such as ``to_tsquery()``
and ``to_tsvector()`` are available
explicitly using the standard :data:`.func` construct.  For example::

    select([
        func.to_tsvector('fat cats ate rats').match('cat & rat')
    ])

Emits the equivalent of::

    SELECT to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat')

The :class:`_postgresql.TSVECTOR` type can provide for explicit CAST::

    from sqlalchemy.dialects.postgresql import TSVECTOR
    from sqlalchemy import select, cast
    select([cast("some text", TSVECTOR)])

produces a statement equivalent to::

    SELECT CAST('some text' AS TSVECTOR) AS anon_1

Full Text Searches in PostgreSQL are influenced by a combination of: the
PostgreSQL setting of ``default_text_search_config``, the ``regconfig`` used
to build the GIN/GiST indexes, and the ``regconfig`` optionally passed in
during a query.

When performing a Full Text Search against a column that has a GIN or
GiST index that is already pre-computed (which is common on full text
searches) one may need to explicitly pass in a particular PostgreSQL
``regconfig`` value to ensure the query-planner utilizes the index and does
not re-compute the column on demand.

In order to provide for this explicit query planning, or to use different
search strategies, the ``match`` method accepts a ``postgresql_regconfig``
keyword argument::

    select([mytable.c.id]).where(
        mytable.c.title.match('somestring', postgresql_regconfig='english')
    )

Emits the equivalent of::

    SELECT mytable.id FROM mytable
    WHERE mytable.title @@ to_tsquery('english', 'somestring')

One can also specifically pass in a `'regconfig'` value to the
``to_tsvector()`` command as the initial argument::

    select([mytable.c.id]).where(
            func.to_tsvector('english', mytable.c.title )\
            .match('somestring', postgresql_regconfig='english')
        )

produces a statement equivalent to::

    SELECT mytable.id FROM mytable
    WHERE to_tsvector('english', mytable.title) @@
        to_tsquery('english', 'somestring')

It is recommended that you use the ``EXPLAIN ANALYZE...`` tool from
PostgreSQL to ensure that you are generating queries with SQLAlchemy that
take full advantage of any indexes you may have created for full text search.

FROM ONLY ...
-------------

The dialect supports PostgreSQL's ONLY keyword for targeting only a particular
table in an inheritance hierarchy. This can be used to produce the
``SELECT ... FROM ONLY``, ``UPDATE ONLY ...``, and ``DELETE FROM ONLY ...``
syntaxes. It uses SQLAlchemy's hints mechanism::

    # SELECT ... FROM ONLY ...
    result = table.select().with_hint(table, 'ONLY', 'postgresql')
    print(result.fetchall())

    # UPDATE ONLY ...
    table.update(values=dict(foo='bar')).with_hint('ONLY',
                                                   dialect_name='postgresql')

    # DELETE FROM ONLY ...
    table.delete().with_hint('ONLY', dialect_name='postgresql')


.. _postgresql_indexes:

PostgreSQL-Specific Index Options
---------------------------------

Several extensions to the :class:`.Index` construct are available, specific
to the PostgreSQL dialect.

.. _postgresql_partial_indexes:

Partial Indexes
^^^^^^^^^^^^^^^

Partial indexes add criterion to the index definition so that the index is
applied to a subset of rows.   These can be specified on :class:`.Index`
using the ``postgresql_where`` keyword argument::

  Index('my_index', my_table.c.id, postgresql_where=my_table.c.value > 10)

.. _postgresql_operator_classes:

Operator Classes
^^^^^^^^^^^^^^^^

PostgreSQL allows the specification of an *operator class* for each column of
an index (see
http://www.postgresql.org/docs/8.3/interactive/indexes-opclass.html).
The :class:`.Index` construct allows these to be specified via the
``postgresql_ops`` keyword argument::

    Index(
        'my_index', my_table.c.id, my_table.c.data,
        postgresql_ops={
            'data': 'text_pattern_ops',
            'id': 'int4_ops'
        })

Note that the keys in the ``postgresql_ops`` dictionaries are the
"key" name of the :class:`_schema.Column`, i.e. the name used to access it from
the ``.c`` collection of :class:`_schema.Table`, which can be configured to be
different than the actual name of the column as expressed in the database.

If ``postgresql_ops`` is to be used against a complex SQL expression such
as a function call, then to apply to the column it must be given a label
that is identified in the dictionary by name, e.g.::

    Index(
        'my_index', my_table.c.id,
        func.lower(my_table.c.data).label('data_lower'),
        postgresql_ops={
            'data_lower': 'text_pattern_ops',
            'id': 'int4_ops'
        })

Operator classes are also supported by the
:class:`_postgresql.ExcludeConstraint` construct using the
:paramref:`_postgresql.ExcludeConstraint.ops` parameter. See that parameter for
details.

.. versionadded:: 1.3.21 added support for operator classes with
   :class:`_postgresql.ExcludeConstraint`.


Index Types
^^^^^^^^^^^

PostgreSQL provides several index types: B-Tree, Hash, GiST, and GIN, as well
as the ability for users to create their own (see
http://www.postgresql.org/docs/8.3/static/indexes-types.html). These can be
specified on :class:`.Index` using the ``postgresql_using`` keyword argument::

    Index('my_index', my_table.c.data, postgresql_using='gin')

The value passed to the keyword argument will be simply passed through to the
underlying CREATE INDEX command, so it *must* be a valid index type for your
version of PostgreSQL.

.. _postgresql_index_storage:

Index Storage Parameters
^^^^^^^^^^^^^^^^^^^^^^^^

PostgreSQL allows storage parameters to be set on indexes. The storage
parameters available depend on the index method used by the index. Storage
parameters can be specified on :class:`.Index` using the ``postgresql_with``
keyword argument::

    Index('my_index', my_table.c.data, postgresql_with={"fillfactor": 50})

.. versionadded:: 1.0.6

PostgreSQL allows to define the tablespace in which to create the index.
The tablespace can be specified on :class:`.Index` using the
``postgresql_tablespace`` keyword argument::

    Index('my_index', my_table.c.data, postgresql_tablespace='my_tablespace')

.. versionadded:: 1.1

Note that the same option is available on :class:`_schema.Table` as well.

.. _postgresql_index_concurrently:

Indexes with CONCURRENTLY
^^^^^^^^^^^^^^^^^^^^^^^^^

The PostgreSQL index option CONCURRENTLY is supported by passing the
flag ``postgresql_concurrently`` to the :class:`.Index` construct::

    tbl = Table('testtbl', m, Column('data', Integer))

    idx1 = Index('test_idx1', tbl.c.data, postgresql_concurrently=True)

The above index construct will render DDL for CREATE INDEX, assuming
PostgreSQL 8.2 or higher is detected or for a connection-less dialect, as::

    CREATE INDEX CONCURRENTLY test_idx1 ON testtbl (data)

For DROP INDEX, assuming PostgreSQL 9.2 or higher is detected or for
a connection-less dialect, it will emit::

    DROP INDEX CONCURRENTLY test_idx1

.. versionadded:: 1.1 support for CONCURRENTLY on DROP INDEX.  The
   CONCURRENTLY keyword is now only emitted if a high enough version
   of PostgreSQL is detected on the connection (or for a connection-less
   dialect).

When using CONCURRENTLY, the PostgreSQL database requires that the statement
be invoked outside of a transaction block.   The Python DBAPI enforces that
even for a single statement, a transaction is present, so to use this
construct, the DBAPI's "autocommit" mode must be used::

    metadata = MetaData()
    table = Table(
        "foo", metadata,
        Column("id", String))
    index = Index(
        "foo_idx", table.c.id, postgresql_concurrently=True)

    with engine.connect() as conn:
        with conn.execution_options(isolation_level='AUTOCOMMIT'):
            table.create(conn)

.. seealso::

    :ref:`postgresql_isolation_level`

.. _postgresql_index_reflection:

PostgreSQL Index Reflection
---------------------------

The PostgreSQL database creates a UNIQUE INDEX implicitly whenever the
UNIQUE CONSTRAINT construct is used.   When inspecting a table using
:class:`_reflection.Inspector`, the :meth:`_reflection.Inspector.get_indexes`
and the :meth:`_reflection.Inspector.get_unique_constraints`
will report on these
two constructs distinctly; in the case of the index, the key
``duplicates_constraint`` will be present in the index entry if it is
detected as mirroring a constraint.   When performing reflection using
``Table(..., autoload=True)``, the UNIQUE INDEX is **not** returned
in :attr:`_schema.Table.indexes` when it is detected as mirroring a
:class:`.UniqueConstraint` in the :attr:`_schema.Table.constraints` collection
.

.. versionchanged:: 1.0.0 - :class:`_schema.Table` reflection now includes
   :class:`.UniqueConstraint` objects present in the
   :attr:`_schema.Table.constraints`
   collection; the PostgreSQL backend will no longer include a "mirrored"
   :class:`.Index` construct in :attr:`_schema.Table.indexes`
   if it is detected
   as corresponding to a unique constraint.

Special Reflection Options
--------------------------

The :class:`_reflection.Inspector`
used for the PostgreSQL backend is an instance
of :class:`.PGInspector`, which offers additional methods::

    from sqlalchemy import create_engine, inspect

    engine = create_engine("postgresql+psycopg2://localhost/test")
    insp = inspect(engine)  # will be a PGInspector

    print(insp.get_enums())

.. autoclass:: PGInspector
    :members:

.. _postgresql_table_options:

PostgreSQL Table Options
------------------------

Several options for CREATE TABLE are supported directly by the PostgreSQL
dialect in conjunction with the :class:`_schema.Table` construct:

* ``TABLESPACE``::

    Table("some_table", metadata, ..., postgresql_tablespace='some_tablespace')

  The above option is also available on the :class:`.Index` construct.

* ``ON COMMIT``::

    Table("some_table", metadata, ..., postgresql_on_commit='PRESERVE ROWS')

* ``WITH OIDS``::

    Table("some_table", metadata, ..., postgresql_with_oids=True)

* ``WITHOUT OIDS``::

    Table("some_table", metadata, ..., postgresql_with_oids=False)

* ``INHERITS``::

    Table("some_table", metadata, ..., postgresql_inherits="some_supertable")

    Table("some_table", metadata, ..., postgresql_inherits=("t1", "t2", ...))

    .. versionadded:: 1.0.0

* ``PARTITION BY``::

    Table("some_table", metadata, ...,
          postgresql_partition_by='LIST (part_column)')

    .. versionadded:: 1.2.6

.. seealso::

    `PostgreSQL CREATE TABLE options
    <http://www.postgresql.org/docs/current/static/sql-createtable.html>`_

Table values, Row and Tuple objects
-----------------------------------

Row Types
^^^^^^^^^

Built-in support for rendering a ``ROW`` is not available yet, however the
:func:`_expression.tuple_` may be used in its place. Another alternative is
to use the :attr:`_sa.func` generator with ``func.ROW`` ::

    table.select().where(
        tuple_(table.c.id, table.c.fk) > (1,2)
    ).where(func.ROW(table.c.id, table.c.fk) < func.ROW(3, 7))

Will generate the row-wise comparison::

    SELECT *
    FROM table
    WHERE (id, fk) > (1, 2)
    AND ROW(id, fk) < ROW(3, 7)

.. seealso::

    `PostgreSQL Row Constructors
    <https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS>`_

    `PostgreSQL Row Constructor Comparison
    <https://www.postgresql.org/docs/current/functions-comparisons.html#ROW-WISE-COMPARISON>`_

Table Types
^^^^^^^^^^^

PostgreSQL also supports passing a table as an argument to a function. This
is not available yet in sqlalchemy, however the
:func:`_expression.literal_column` function with the name of the table may be
used in its place::

    select(['*']).select_from(func.my_function(literal_column('my_table')))

Will generate the SQL::

    SELECT *
    FROM my_function(my_table)

ARRAY Types
-----------

The PostgreSQL dialect supports arrays, both as multidimensional column types
as well as array literals:

* :class:`_postgresql.ARRAY` - ARRAY datatype

* :class:`_postgresql.array` - array literal

* :func:`_postgresql.array_agg` - ARRAY_AGG SQL function

* :class:`_postgresql.aggregate_order_by` - helper for PG's ORDER BY aggregate
  function syntax.

JSON Types
----------

The PostgreSQL dialect supports both JSON and JSONB datatypes, including
psycopg2's native support and support for all of PostgreSQL's special
operators:

* :class:`_postgresql.JSON`

* :class:`_postgresql.JSONB`

HSTORE Type
-----------

The PostgreSQL HSTORE type as well as hstore literals are supported:

* :class:`_postgresql.HSTORE` - HSTORE datatype

* :class:`_postgresql.hstore` - hstore literal

ENUM Types
----------

PostgreSQL has an independently creatable TYPE structure which is used
to implement an enumerated type.   This approach introduces significant
complexity on the SQLAlchemy side in terms of when this type should be
CREATED and DROPPED.   The type object is also an independently reflectable
entity.   The following sections should be consulted:

* :class:`_postgresql.ENUM` - DDL and typing support for ENUM.

* :meth:`.PGInspector.get_enums` - retrieve a listing of current ENUM types

* :meth:`.postgresql.ENUM.create` , :meth:`.postgresql.ENUM.drop` - individual
  CREATE and DROP commands for ENUM.

.. _postgresql_array_of_enum:

Using ENUM with ARRAY
^^^^^^^^^^^^^^^^^^^^^

The combination of ENUM and ARRAY is not directly supported by backend
DBAPIs at this time.   Prior to SQLAlchemy 1.3.17, a special workaround
was needed in order to allow this combination to work, described below.

.. versionchanged:: 1.3.17 The combination of ENUM and ARRAY is now directly
   handled by SQLAlchemy's implementation without any workarounds needed.

.. sourcecode:: python

    from sqlalchemy import TypeDecorator
    from sqlalchemy.dialects.postgresql import ARRAY

    class ArrayOfEnum(TypeDecorator):
        impl = ARRAY

        def bind_expression(self, bindvalue):
            return sa.cast(bindvalue, self)

        def result_processor(self, dialect, coltype):
            super_rp = super(ArrayOfEnum, self).result_processor(
                dialect, coltype)

            def handle_raw_string(value):
                inner = re.match(r"^{(.*)}$", value).group(1)
                return inner.split(",") if inner else []

            def process(value):
                if value is None:
                    return None
                return super_rp(handle_raw_string(value))
            return process

E.g.::

    Table(
        'mydata', metadata,
        Column('id', Integer, primary_key=True),
        Column('data', ArrayOfEnum(ENUM('a', 'b, 'c', name='myenum')))

    )

This type is not included as a built-in type as it would be incompatible
with a DBAPI that suddenly decides to support ARRAY of ENUM directly in
a new version.

.. _postgresql_array_of_json:

Using JSON/JSONB with ARRAY
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Similar to using ENUM, prior to SQLAlchemy 1.3.17, for an ARRAY of JSON/JSONB
we need to render the appropriate CAST.   Current psycopg2 drivers accomodate
the result set correctly without any special steps.

.. versionchanged:: 1.3.17 The combination of JSON/JSONB and ARRAY is now
   directly handled by SQLAlchemy's implementation without any workarounds
   needed.

.. sourcecode:: python

    class CastingArray(ARRAY):
        def bind_expression(self, bindvalue):
            return sa.cast(bindvalue, self)

E.g.::

    Table(
        'mydata', metadata,
        Column('id', Integer, primary_key=True),
        Column('data', CastingArray(JSONB))
    )


�)�defaultdictN�)�array)�hstore)�json)�ranges�)�exc��schema)�sql)�util)�default)�
reflection)�compiler)�elements)�
expression)�sqltypes)�DDLBase)�BIGINT)�BOOLEAN)�CHAR)�DATE)�FLOAT)�INTEGER)�NUMERIC)�REAL)�SMALLINT)�TEXT)�VARCHAR)�UUIDz ^(?:btree|hash|gist|gin|[\w_]+)$zs\s*(?:UPDATE|INSERT|CREATE|DELETE|DROP|ALTER|GRANT|REVOKE|IMPORT FOREIGN SCHEMA|REFRESH MATERIALIZED VIEW|TRUNCATE))f�all�analyse�analyze�and�anyr�as�asc�
asymmetric�both�case�cast�check�collate�column�
constraint�create�current_catalog�current_date�current_role�current_time�current_timestamp�current_userr�
deferrable�desc�distinct�do�else�end�except�false�fetch�for�foreign�from�grant�group�having�in�	initially�	intersect�into�leading�limit�	localtime�localtimestamp�new�not�null�of�off�offset�old�on�only�or�order�placing�primary�
references�	returning�select�session_user�some�	symmetric�table�then�to�trailing�true�union�unique�user�using�variadic�when�where�window�with�
authorization�between�binary�cross�current_schema�freeze�full�ilike�inner�is�isnull�join�left�like�natural�notnull�outer�over�overlaps�right�similar�verbose)i�i�)i�i�i�i�)����i�i�i�c��eZdZdZdS)�BYTEAN��__name__�
__module__�__qualname__�__visit_name__���Z/opt/cloudlinux/venv/lib64/python3.11/site-packages/sqlalchemy/dialects/postgresql/base.pyr�r��s�������N�N�Nr�r�c��eZdZdZdS)�DOUBLE_PRECISIONNr�r�r�r�r�r���������'�N�N�Nr�r�c��eZdZdZdS)�INETNr�r�r�r�r�r����������N�N�Nr�r�c��eZdZdZdS)�CIDRNr�r�r�r�r�r��r�r�r�c��eZdZdZdS)�MACADDRNr�r�r�r�r�r��s�������N�N�Nr�r�c��eZdZdZdZdS)�MONEYa�Provide the PostgreSQL MONEY type.

    Depending on driver, result rows using this type may return a
    string value which includes currency symbols.

    For this reason, it may be preferable to provide conversion to a
    numerically-based currency datatype using :class:`_types.TypeDecorator`::

        import re
        import decimal
        from sqlalchemy import TypeDecorator

        class NumericMoney(TypeDecorator):
            impl = MONEY

            def process_result_value(self, value: Any, dialect: Any) -> None:
                if value is not None:
                    # adjust this for the currency and numeric
                    m = re.match(r"\$([\d.]+)", value)
                    if m:
                        value = decimal.Decimal(m.group(1))
                return value

    Alternatively, the conversion may be applied as a CAST using
    the :meth:`_types.TypeDecorator.column_expression` method as follows::

        import decimal
        from sqlalchemy import cast
        from sqlalchemy import TypeDecorator

        class NumericMoney(TypeDecorator):
            impl = MONEY

            def column_expression(self, column: Any):
                return cast(column, Numeric())

    .. versionadded:: 1.2

    N�r�r�r��__doc__r�r�r�r�r�r��s ������&�&�P�N�N�Nr�r�c��eZdZdZdZdS)�OIDzCProvide the PostgreSQL OID type.

    .. versionadded:: 0.9.5

    Nr�r�r�r�r�r��s���������N�N�Nr�r�c��eZdZdZdZdS)�REGCLASSzHProvide the PostgreSQL REGCLASS type.

    .. versionadded:: 1.2.7

    Nr�r�r�r�r�r��s�������� �N�N�Nr�r�c� ��eZdZd�fd�	Z�xZS)�	TIMESTAMPFNc�h��tt|���|���||_dS�N)�timezone)�superr��__init__�	precision��selfr�r��	__class__s   �r�r�zTIMESTAMP.__init__�s/���
�i����'�'��'�:�:�:�"����r��FN�r�r�r�r��
__classcell__�r�s@r�r�r���=�������#�#�#�#�#�#�#�#�#�#r�r�c� ��eZdZd�fd�	Z�xZS)�TIMEFNc�h��tt|���|���||_dSr�)r�r�r�r�r�s   �r�r�z
TIME.__init__�s/���
�d�D���"�"�H�"�5�5�5�"����r�r�r�r�s@r�r�r��r�r�r�c�d�eZdZdZdZdZdd�Zed���Ze	d���Z
e	d���ZdS)	�INTERVALz�PostgreSQL INTERVAL type.

    The INTERVAL type may not be supported on all DBAPIs.
    It is known to work on psycopg2 and not pg8000 or zxjdbc.

    TNc�"�||_||_dS)a Construct an INTERVAL.

        :param precision: optional integer precision value
        :param fields: string fields specifier.  allows storage of fields
         to be limited, such as ``"YEAR"``, ``"MONTH"``, ``"DAY TO HOUR"``,
         etc.

         .. versionadded:: 1.2

        N)r��fields)r�r�r�s   r�r�zINTERVAL.__init__s��#�������r�c�,�t|j���S)N�r�)r��second_precision)�cls�interval�kws   r��adapt_emulated_to_nativez!INTERVAL.adapt_emulated_to_natives���(�";�<�<�<�<r�c��tjS�N)r�Interval�r�s r��_type_affinityzINTERVAL._type_affinitys��� � r�c��tjSr�)�dt�	timedeltar�s r��python_typezINTERVAL.python_types
���|�r�)NN)r�r�r�r�r��nativer��classmethodr��propertyr�r�r�r�r�r�r��s��������� �N�
�F������=�=��[�=��!�!��X�!�����X���r�r�c��eZdZdZdd�ZdS)�BITNFc�:�|s
|pd|_n||_||_dS)Nr)�length�varying)r�r�r�s   r�r�zBIT.__init__)s+���	!� �+�A�D�K�K�!�D�K�����r��NF)r�r�r�r�r�r�r�r�r�r�&s-�������N������r�r�c�*�eZdZdZdZdd�Zd�Zd�ZdS)r!a
PostgreSQL UUID type.

    Represents the UUID column type, interpreting
    data either as natively returned by the DBAPI
    or as Python uuid objects.

    The UUID type may not be supported on all DBAPIs.
    It is known to work on psycopg2 and not pg8000.

    Fc�D�|rt�td���||_dS)z�Construct a UUID type.


        :param as_uuid=False: if True, values will be interpreted
         as Python uuid objects, converting to/from string via the
         DBAPI.

        Nz=This version of Python does not support the native UUID type.)�_python_UUID�NotImplementedError�as_uuid)r�r�s  r�r�z
UUID.__init__Es5���	�|�+�%�(���
�����r�c��|jrd�}|SdS)Nc�2�|�tj|��}|Sr�)r�	text_type��values r��processz$UUID.bind_processor.<locals>.processXs���$� �N�5�1�1�E��r��r�)r��dialectr�s   r��bind_processorzUUID.bind_processorU�'���<�		�
�
�
�
�N��4r�c��|jrd�}|SdS)Nc�(�|�t|��}|Sr�)r�r�s r�r�z&UUID.result_processor.<locals>.processds���$�(��/�/�E��r�r�)r�r��coltyper�s    r��result_processorzUUID.result_processorar�r�N�F)r�r�r�r�r�r�r�r�r�r�r�r!r!6sW������	�	��N����� 
�
�
�
�
�
�
�
r�r!c��eZdZdZdZdS)�TSVECTORaThe :class:`_postgresql.TSVECTOR` type implements the PostgreSQL
    text search type TSVECTOR.

    It can be used to do full text queries on natural language
    documents.

    .. versionadded:: 0.9.0

    .. seealso::

        :ref:`postgresql_match`

    Nr�r�r�r�r�r�qs�������� �N�N�Nr�r�c���eZdZdZdZ�fd�Zed���Zdd�Zdd�Z	Gd�d	e
��ZGd
�de
��Zd�Z
dd�Zdd�Zdd�Zdd�Z�xZS)�ENUMa�PostgreSQL ENUM type.

    This is a subclass of :class:`_types.Enum` which includes
    support for PG's ``CREATE TYPE`` and ``DROP TYPE``.

    When the builtin type :class:`_types.Enum` is used and the
    :paramref:`.Enum.native_enum` flag is left at its default of
    True, the PostgreSQL backend will use a :class:`_postgresql.ENUM`
    type as the implementation, so the special create/drop rules
    will be used.

    The create/drop behavior of ENUM is necessarily intricate, due to the
    awkward relationship the ENUM type has in relationship to the
    parent table, in that it may be "owned" by just a single table, or
    may be shared among many tables.

    When using :class:`_types.Enum` or :class:`_postgresql.ENUM`
    in an "inline" fashion, the ``CREATE TYPE`` and ``DROP TYPE`` is emitted
    corresponding to when the :meth:`_schema.Table.create` and
    :meth:`_schema.Table.drop`
    methods are called::

        table = Table('sometable', metadata,
            Column('some_enum', ENUM('a', 'b', 'c', name='myenum'))
        )

        table.create(engine)  # will emit CREATE ENUM and CREATE TABLE
        table.drop(engine)  # will emit DROP TABLE and DROP ENUM

    To use a common enumerated type between multiple tables, the best
    practice is to declare the :class:`_types.Enum` or
    :class:`_postgresql.ENUM` independently, and associate it with the
    :class:`_schema.MetaData` object itself::

        my_enum = ENUM('a', 'b', 'c', name='myenum', metadata=metadata)

        t1 = Table('sometable_one', metadata,
            Column('some_enum', myenum)
        )

        t2 = Table('sometable_two', metadata,
            Column('some_enum', myenum)
        )

    When this pattern is used, care must still be taken at the level
    of individual table creates.  Emitting CREATE TABLE without also
    specifying ``checkfirst=True`` will still cause issues::

        t1.create(engine) # will fail: no such type 'myenum'

    If we specify ``checkfirst=True``, the individual table-level create
    operation will check for the ``ENUM`` and create if not exists::

        # will check if enum exists, and emit CREATE TYPE if not
        t1.create(engine, checkfirst=True)

    When using a metadata-level ENUM type, the type will always be created
    and dropped if either the metadata-wide create/drop is called::

        metadata.create_all(engine)  # will emit CREATE TYPE
        metadata.drop_all(engine)  # will emit DROP TYPE

    The type can also be created and dropped directly::

        my_enum.create(engine)
        my_enum.drop(engine)

    .. versionchanged:: 1.0.0 The PostgreSQL :class:`_postgresql.ENUM` type
       now behaves more strictly with regards to CREATE/DROP.  A metadata-level
       ENUM type will only be created and dropped at the metadata level,
       not the table level, with the exception of
       ``table.create(checkfirst=True)``.
       The ``table.drop()`` call will now emit a DROP TYPE for a table-level
       enumerated type.

    Tc�~��|�dd��|_tt|��j|i|��dS)aConstruct an :class:`_postgresql.ENUM`.

        Arguments are the same as that of
        :class:`_types.Enum`, but also including
        the following parameters.

        :param create_type: Defaults to True.
         Indicates that ``CREATE TYPE`` should be
         emitted, after optionally checking for the
         presence of the type, when the parent
         table is being created; and additionally
         that ``DROP TYPE`` is called when the table
         is dropped.    When ``False``, no check
         will be performed and no ``CREATE TYPE``
         or ``DROP TYPE`` is emitted, unless
         :meth:`~.postgresql.ENUM.create`
         or :meth:`~.postgresql.ENUM.drop`
         are called directly.
         Setting to ``False`` is helpful
         when invoking a creation scheme to a SQL file
         without access to the actual database -
         the :meth:`~.postgresql.ENUM.create` and
         :meth:`~.postgresql.ENUM.drop` methods can
         be used to emit SQL to a target bind.

        �create_typeTN)�popr�r�r�r�)r��enumsr�r�s   �r�r�z
ENUM.__init__�sC���6�6�6�-��6�6���"��d�D���"�E�0�R�0�0�0�0�0r�c��|�d|j��|�d|j��|�d|j��|�d|j��|�d|j��|�dd��|�d|j��|d	i|��S)
zbProduce a PostgreSQL native :class:`_postgresql.ENUM` from plain
        :class:`.Enum`.

        �validate_strings�namer�inherit_schema�metadata�_create_eventsF�values_callabler�)�
setdefaultr�r�rr�r�r�)r��implr�s   r�r�zENUM.adapt_emulated_to_native�s���	�
�
�(�$�*?�@�@�@�
�
�
�f�d�i�(�(�(�
�
�
�h���,�,�,�
�
�
�&��(;�<�<�<�
�
�
�j�$�-�0�0�0�
�
�
�&��.�.�.�
�
�
�'��)=�>�>�>��s�y�y�R�y�y�r�Nc�\�|jjsdS|�|j||���dS)a�Emit ``CREATE TYPE`` for this
        :class:`_postgresql.ENUM`.

        If the underlying dialect does not support
        PostgreSQL CREATE TYPE, no action is taken.

        :param bind: a connectable :class:`_engine.Engine`,
         :class:`_engine.Connection`, or similar object to emit
         SQL.
        :param checkfirst: if ``True``, a query against
         the PG catalog will be first performed to see
         if the type does not exist already before
         creating.

        N��
checkfirst)r��supports_native_enum�_run_visitor�
EnumGenerator�r��bindr�s   r�r1zENUM.creates:�� �|�0�	��F����$�,�d�z��J�J�J�J�Jr�c�\�|jjsdS|�|j||���dS)a�Emit ``DROP TYPE`` for this
        :class:`_postgresql.ENUM`.

        If the underlying dialect does not support
        PostgreSQL DROP TYPE, no action is taken.

        :param bind: a connectable :class:`_engine.Engine`,
         :class:`_engine.Connection`, or similar object to emit
         SQL.
        :param checkfirst: if ``True``, a query against
         the PG catalog will be first performed to see
         if the type actually exists before dropping.

        Nr�)r�r�r��EnumDropperrs   r��dropz	ENUM.drops:���|�0�	��F����$�*�D�Z��H�H�H�H�Hr�c�,��eZdZd�fd�	Zd�Zd�Z�xZS)�ENUM.EnumGeneratorFc�b��ttj|��j|fi|��||_dSr�)r�r�rr�r��r�r��
connectionr��kwargsr�s     �r�r�zENUM.EnumGenerator.__init__,s6���4�E�$�$�d�+�+�4�Z�J�J�6�J�J�J�(�D�O�O�Or�c��|jsdS|j�|��}|jj�|j|j|���S�NTr�r�r
�schema_for_objectr��has_typer��r��enum�effective_schemas   r��_can_create_enumz#ENUM.EnumGenerator._can_create_enum0sZ���?�
��t�#��@�@��F�F����.�7�7�����3C�8����
r�c��|�|��sdS|j�t|����dSr�)rr
�execute�CreateEnumType�r�rs  r��
visit_enumzENUM.EnumGenerator.visit_enum:sA���(�(��.�.�
����O�#�#�N�4�$8�$8�9�9�9�9�9r�r�)r�r�r�r�rrr�r�s@r�rr+s[�������	)�	)�	)�	)�	)�	)�	�	�	�	:�	:�	:�	:�	:�	:�	:r�rc�,��eZdZd�fd�	Zd�Zd�Z�xZS)�ENUM.EnumDropperFc�b��ttj|��j|fi|��||_dSr�)r�r�rr�r�r	s     �r�r�zENUM.EnumDropper.__init__As6���2�E�$�"�D�)�)�2�:�H�H��H�H�H�(�D�O�O�Or�c��|jsdS|j�|��}|jj�|j|j|���Sr
rrs   r��_can_drop_enumzENUM.EnumDropper._can_drop_enumEsU���?�
��t�#��@�@��F�F���?�*�3�3�����3C�4���
r�c��|�|��sdS|j�t|����dSr�)rr
r�DropEnumTypers  r�rzENUM.EnumDropper.visit_enumOsA���&�&�t�,�,�
����O�#�#�L��$6�$6�7�7�7�7�7r�r�)r�r�r�r�rrr�r�s@r�rr@s[�������	)�	)�	)�	)�	)�	)�	�	�	�	8�	8�	8�	8�	8�	8�	8r�rc���|jsdSd|vrj|d}d|jvr|jd}nt��x}|jd<|j|jf|v}|�|j|jf��|SdS)aLook in the 'ddl runner' for 'memos', then
        note our name in that collection.

        This to ensure a particular named enum is operated
        upon only once within any kind of create/drop
        sequence without relying upon "checkfirst".

        T�_ddl_runner�	_pg_enumsF)r��memo�setrr��add)r�r�r��
ddl_runner�pg_enums�presents      r��_check_for_name_in_memoszENUM._check_for_name_in_memosUs�����	��4��B����M�*�J��j�o�-�-�%�?�;�7���:=�%�%�?��:�?�;�7��{�D�I�.�(�:�G��L�L�$�+�t�y�1�2�2�2��N��5r�Fc��|s|jsG|�dd��s3|�||��s|�||���dSdSdSdS�N�_is_metadata_operationF�rr�)r��getr*r1�r��targetrr�r�s     r��_on_table_createzENUM._on_table_createls���
�	:��M�	:����7��?�?�		:��/�/�
�B�?�?�
	:�
�K�K�T�j�K�9�9�9�9�9�	:�	:�	:�	:�	:�	:r�c��|jsE|�dd��s1|�||��s|�||���dSdSdSdSr,)r�r/r*rr0s     r��_on_table_dropzENUM._on_table_dropwsy���
�	8��F�F�3�U�;�;�	8��1�1�*�b�A�A�	8�

�I�I�4�J�I�7�7�7�7�7�	8�	8�	8�	8�	8�	8r�c�d�|�||��s|�||���dSdS�Nr.)r*r1r0s     r��_on_metadata_createzENUM._on_metadata_creates@���,�,�Z��<�<�	:��K�K�T�j�K�9�9�9�9�9�	:�	:r�c�d�|�||��s|�||���dSdSr6)r*rr0s     r��_on_metadata_dropzENUM._on_metadata_drop�s@���,�,�Z��<�<�	8��I�I�4�J�I�7�7�7�7�7�	8�	8r�)NTr�)r�r�r�r��native_enumr�r�r�r1rrrrr*r2r4r7r9r�r�s@r�r�r��sJ�������K�K�Z�K�1�1�1�1�1�<����[��K�K�K�K�*I�I�I�I�(:�:�:�:�:��:�:�:�*8�8�8�8�8�g�8�8�8�*���.	:�	:�	:�	:�8�8�8�8�:�:�:�:�8�8�8�8�8�8�8�8r�r��_arrayrr�jsonb�	int4range�	int8range�numrange�	daterange�tsrange�	tstzrange�integer�bigint�smallintzcharacter varying�	characterz"char"r��text�numeric�float�real�inet�cidr�uuid�bit�bit varying�macaddr�money�oid�regclass�double precision�	timestamp�timestamp with time zone�timestamp without time zone)�time with time zone�time without time zone�date�time�bytea�booleanr��tsvectorc���eZdZd�Zd�Z	dd�Z	dd�Zd�Zd�Zd�Z	d	�Z
d
�Zd�Z�fd�Z
d
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z�xZS)�
PGCompilerc�$�d|j|fi|��zS)Nz	ARRAY[%s])�visit_clauselist�r��elementr�s   r��visit_arrayzPGCompiler.visit_array�s#���2�T�2�7�A�A�b�A�A�A�Ar�c�T�|j|jfi|���d|j|jfi|����S)N�:)r��start�stoprcs   r��visit_slicezPGCompiler.visit_slice�sG���D�L���-�-�"�-�-�-�-��D�L���,�,��,�,�,�
�	
r�Fc��|sC|jjtjur+d|d<|jtj||j��fi|��Sd|d<|j||sdndfi|��S)NT�
_cast_applied�eager_groupingz -> z ->> ��typer�r�JSONr�r
r,�_generate_generic_binary�r�rr�operatorrlr�s     r��visit_json_getitem_op_binaryz'PGCompiler.visit_json_getitem_op_binary�s����	E���*�(�-�?�?�"&�B����4�<������ =� =�D�D��D�D�D�#����,�t�,��-�<�F�F�W�
�
�@B�
�
�	
r�c��|sC|jjtjur+d|d<|jtj||j��fi|��Sd|d<|j||sdndfi|��S)NTrlrmz #> z #>> rnrrs     r��!visit_json_path_getitem_op_binaryz,PGCompiler.visit_json_path_getitem_op_binary�s����	E���*�(�-�?�?�"&�B����4�<������ =� =�D�D��D�D�D�#����,�t�,��-�<�F�F�W�
�
�@B�
�
�	
r�c�V�|j|jfi|���d|j|jfi|���d�S)N�[�])r�r|r�)r�rrrsr�s    r��visit_getitem_binaryzPGCompiler.visit_getitem_binary�sJ���D�L���+�+��+�+�+�+��D�L���,�,��,�,�,�,�
�	
r�c�T�|j|jfi|���d|j|jfi|����S)Nz
 ORDER BY )r�r1�order_byrcs   r��visit_aggregate_order_byz#PGCompiler.visit_aggregate_order_by�sH���D�L���.�.�2�.�.�.�.��D�L��)�0�0�R�0�0�0�
�	
r�c	��d|jvrZ|�|jdtj��}|r-|j|jfi|���d|�d|j|jfi|���d�S|j|jfi|���d|j|jfi|���d�S)N�postgresql_regconfigz @@ to_tsquery(�, �))�	modifiers�render_literal_valuer�
STRINGTYPEr�r|r�)r�rrrsr��	regconfigs     r��visit_match_op_binaryz PGCompiler.visit_match_op_binary�s���!�V�%5�5�5��1�1�� �!7�8�(�:M���I��
� �D�L���3�3��3�3�3�3��I�I� �D�L���4�4��4�4�4�4���
�D�L���+�+��+�+�+�+��D�L���,�,��,�,�,�,�
�	
r�c���|j�dd��}|j|jfi|���d|j|jfi|����|r#d|�|tj��zndzS)N�escapez ILIKE � ESCAPE ��r�r/r�r|r�r�rr��r�rrrsr�r�s     r��visit_ilike_op_binaryz PGCompiler.visit_ilike_op_binarys����!�%�%�h��5�5��
�D�L���+�+��+�+�+�+��D�L���,�,��,�,�,�
�
�
�J��2�2�6�8�;N�O�O�O�O��

�	
r�c���|j�dd��}|j|jfi|���d|j|jfi|����|r#d|�|tj��zndzS)Nr�z NOT ILIKE r�r�r�r�s     r��visit_notilike_op_binaryz#PGCompiler.visit_notilike_op_binarys����!�%�%�h��5�5���D�L���+�+��+�+�+�+��D�L���,�,��,�,�,�
�
�
�J��2�2�6�8�;N�O�O�O�O��

�	
r�c�l��dd��fd�|pt��gD�����d�S)NzSELECT r�c3��K�|];}d�jj�|jrt	��n|��zV��<dS)zCAST(NULL AS %s)N)r��
type_compilerr��_isnullr)�.0�type_r�s  �r��	<genexpr>z2PGCompiler.visit_empty_set_expr.<locals>.<genexpr>sh�������
�	#��,�,�4�4�!&��9�G�I�I�I�E���������r�z WHERE 1!=1)r{r)r��
element_typess` r��visit_empty_set_exprzPGCompiler.visit_empty_set_exprs_����

�I�I�����
+�9�w�y�y�k����
�
�
�
�
�	
r�c���tt|���||��}|jjr|�dd��}|S)N�\z\\)r�r`r�r��_backslash_escapes�replace)r�r�r�r�s   �r�r�zPGCompiler.render_literal_value(sG����j�$�'�'�<�<�U�E�J�J���<�*�	0��M�M�$��/�/�E��r�c�<�d|j�|��zS)Nz
nextval('%s'))�preparer�format_sequence)r��seqr�s   r��visit_sequencezPGCompiler.visit_sequence/s�����!>�!>�s�!C�!C�C�Cr�c��d}|j�|d|j|jfi|��zz
}|j�%|j�|dz
}|d|j|jfi|��zz
}|S)Nr�z	 
 LIMIT z 
 LIMIT ALLz OFFSET )�
_limit_clauser��_offset_clause)r�r^r�rGs    r��limit_clausezPGCompiler.limit_clause2s}������+��L�<�4�<��0D�#K�#K��#K�#K�K�K�D�� �,��#�+���'���J����f�.C�!J�!J�r�!J�!J�J�J�D��r�c�j�|���dkrtjd|z���d|zS)N�ONLYzUnrecognized hint: %rzONLY )�upperr
�CompileError)r��sqltextrb�hint�iscruds     r��format_from_hint_textz PGCompiler.format_from_hint_text<s7���:�:�<�<�6�!�!��"�#:�T�#A�B�B�B��� � r�c����|jdurr|jdurdSt|jttf��r-dd���fd�|jD����zdzSd�j|jfi���zdzSdS)	NFTz	DISTINCT z
DISTINCT ON (r�c�,��g|]}�j|fi�����Sr��r�)r��colr�r�s  ��r��
<listcomp>z4PGCompiler.get_select_precolumns.<locals>.<listcomp>Is-���M�M�M�S����c�0�0�R�0�0�M�M�Mr�z) r�)�	_distinct�
isinstance�list�tupler{r�)r�r^r�s` `r��get_select_precolumnsz PGCompiler.get_select_precolumnsAs�������5�(�(���4�'�'�"�{��F�,�t�U�m�<�<�

�#��i�i�M�M�M�M�M�F�<L�M�M�M�����	��$�"�d�l�6�#3�:�:�r�:�:�;�����2r�c����|jjr|jjrd}nd}n|jjrd}nd}|jjrqt	j��}|jjD])}|�tj|�����*|dd�	��fd�|D����zz
}|jj
r|dz
}|jjr|d	z
}|S)
Nz FOR KEY SHAREz
 FOR SHAREz FOR NO KEY UPDATEz FOR UPDATEz OF r�c3�:�K�|]}�j|fddd����V��dS)TF)�ashint�
use_schemaNr�)r�rbr�r�s  ��r�r�z/PGCompiler.for_update_clause.<locals>.<genexpr>hsR�����&�&�����U�H�4�E�H�H�R�H�H�&�&�&�&�&�&r�z NOWAITz SKIP LOCKED)�_for_update_arg�read�	key_sharerRr�
OrderedSet�update�sql_util�surface_selectables_onlyr{�nowait�skip_locked)r�r^r��tmp�tables�cs` `   r��for_update_clausezPGCompiler.for_update_clauseVs�����!�&�	 ��%�/�
#�&���"���
�
#�
-�	 �&�C�C��C��!�$�		��_�&�&�F��+�.�
D�
D���
�
�h�?��B�B�C�C�C�C��6�D�I�I�&�&�&�&�&�#�&�&�&����
�C�
�!�(�	��9��C��!�-�	"��>�!�C��
r�c�t���fd�tj|��D��}dd�|��zS)Nc	�B��g|]}��d|ddi����S)NTF)�_label_select_column�r�r�r�s  �r�r�z/PGCompiler.returning_clause.<locals>.<listcomp>vs?���
�
�
��
�%�%�d�A�t�U�B�?�?�
�
�
r�z
RETURNING r�)r�_select_iterablesr{)r��stmt�returning_cols�columnss`   r��returning_clausezPGCompiler.returning_clausetsO���
�
�
�
��1�.�A�A�
�
�
��
�d�i�i��0�0�0�0r�c��|j|jjdfi|��}|j|jjdfi|��}t|jj��dkr*|j|jjdfi|��}d|�d|�d|�d�Sd|�d|�d�S)Nrr�z
SUBSTRING(z FROM z FOR r�)r��clauses�len)r��funcr��srhr�s      r��visit_substring_funczPGCompiler.visit_substring_func}s����D�L���-�a�0�7�7�B�7�7�����T�\�1�!�4�;�;��;�;���t�|�#�$�$�q�(�(�!�T�\�$�,�"6�q�"9�@�@�R�@�@�F�F�56�Q�Q����v�v�v�F�F�)�/0�a�a����7�7r�c����|j�d|jz}n]|j�Tdd��fd�|jD����z}|j�#|d��|jdd���zz
}nd}|S)	NzON CONSTRAINT %s�(%s)r�c3��K�|]P}t|tj��r�j�|��n��|dd���V��QdS)F��
include_tabler�N)r�r�string_typesr��quoter�r�s  �r�r�z1PGCompiler._on_conflict_target.<locals>.<genexpr>�sv�����-�-��"�!�T�%6�7�7�P�D�M�'�'��*�*�*����a�u���O�O�	-�-�-�-�-�-r��	 WHERE %sFr�r�)�constraint_target�inferred_target_elementsr{�inferred_target_whereclauser�)r��clauser��target_texts`   r��_on_conflict_targetzPGCompiler._on_conflict_target�s�����#�/�,�v�/G�G�K�K�
�
,�
8� �4�9�9�-�-�-�-� �8�
-�-�-�$�$��K��1�=��{�T�\�\��6�"'�$�.:�.�.� �����K��r�c�0�|j|fi|��}|rd|zSdS)NzON CONFLICT %s DO NOTHINGzON CONFLICT DO NOTHING)r�)r��on_conflictr�r�s    r��visit_on_conflict_do_nothingz'PGCompiler.visit_on_conflict_do_nothing�s5��.�d�.�{�A�A�b�A�A���	,�.��<�<�+�+r�c	���|}|j|fi|��}g}t|j��}|jdd}|jj}|D]�}	|	j}
|
|vr�|�|
��}tj	|��rtj
d||	j���}nFt|tj
��r,|jj
r |���}|	j|_|�|���d���}|j�|
��}
|�|
�d|������|r�t)jd|jjj�dd	�d
�|D��������|���D]�\}}t|t(j��r|j�|��n|�|d���}
|�tj|��d���}|�|
�d|������d	�|��}|j�#|d|�|jdd�
��zz
}d|�d|��S)N����
selectable�r�F)r�z = z?Additional column names not matching any column keys in table 'z': r�c3� K�|]	}d|zV��
dS�z'%s'Nr�)r�r�s  r�r�z9PGCompiler.visit_on_conflict_do_update.<locals>.<genexpr>�s&����B�B�a�v��z�B�B�B�B�B�Br�r�Tr�zON CONFLICT z DO UPDATE SET )r��dict�update_values_to_set�stackrbr��keyr�r�_is_literal�
BindParameterror�r��_cloner��
self_groupr�r��appendr�warn�current_executabler�r{�itemsr��_literal_as_binds�update_whereclause)r�r�r�r�r��action_set_ops�set_parameters�insert_statement�colsr��col_keyr��
value_text�key_text�k�v�action_texts                 r��visit_on_conflict_do_updatez&PGCompiler.visit_on_conflict_do_update�s�����.�d�.�{�A�A�b�A�A�����f�9�:�:�� �:�b�>�,�7���%�'���	J�	J�A��e�G��.�(�(�&�*�*�7�3�3���'��.�.�	,�$�2�4��a�f�M�M�M�E�E�#�5�(�*@�A�A�,�!�J�.�,�!&������%&�V��
�!�\�\�%�*:�*:�*<�*<��\�O�O�
��=�.�.�w�7�7���%�%�8�8�8�Z�Z�&H�I�I�I���	J��I�I��+�1�6�6�6��Y�Y�B�B�>�B�B�B�B�B�B�	�
�
�
�'�,�,�.�.�	
J�	
J���1�"�!�T�%6�7�7�;�D�M�'�'��*�*�*����a�E��:�:��
"�\�\��.�q�1�1�e�*���
��%�%�8�8�8�Z�Z�&H�I�I�I�I��i�i��/�/���$�0��;�����)��%�*6�*�*��
�K��5@�K�K���M�Mr�c�T����dd����fd�|D����zS)NzFROM r�c3�:�K�|]}|j�fd�d����V��dS�T)�asfrom�	fromhintsN��_compiler_dispatch�r��t�
from_hintsr�r�s  ���r�r�z0PGCompiler.update_from_clause.<locals>.<genexpr>�sS�����#
�#
��
!�A� ��O�d�j�O�O�B�O�O�#
�#
�#
�#
�#
�#
r��r{)r��update_stmt�
from_table�extra_fromsrr�s`   ``r��update_from_clausezPGCompiler.update_from_clause�sT���������#
�#
�#
�#
�#
�#
� �#
�#
�#
�
�
�
�	
r�c�T����dd����fd�|D����zS)z9Render the DELETE .. USING clause specific to PostgreSQL.zUSING r�c3�:�K�|]}|j�fd�d����V��dSrrrs  ���r�r�z6PGCompiler.delete_extra_from_clause.<locals>.<genexpr>�sS�����$
�$
��
!�A� ��O�d�j�O�O�B�O�O�$
�$
�$
�$
�$
�$
r�r)r��delete_stmtrrrr�s`   ``r��delete_extra_from_clausez#PGCompiler.delete_extra_from_clause�sT������$�)�)�$
�$
�$
�$
�$
�$
� �$
�$
�$
�
�
�
�	
r�r�)r�r�r�rerjrtrvrzr}r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr�r�s@r�r`r`�s��������B�B�B�
�
�
�/4�
�
�
�
�"/4�

�

�

�

�
�
�
�
�
�
�
�
�
� 

�

�

�	
�	
�	
�
�
�
������D�D�D����!�!�!�
���*���<1�1�1�8�8�8����0,�,�,�;N�;N�;N�z
�
�
�
�
�
�
�
�
�
r�r`c�T��eZdZd�Z�fd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Z�xZ
S)�
PGDDLCompilerc�(�|j�|��}|j�|j��}t|tj��r|j}|j	r�||j
jur�|jjst|tj
��sx|j�+t|jtj��rR|jjrFt|tj��r|dz
}nwt|tj
��r|dz
}nW|dz
}nQ|d|jj�|j||j���zz
}|�|��}|�|d|zz
}|j� |d|�|j��zz
}|js|dz
}|S)Nz
 BIGSERIALz SMALLSERIALz SERIAL� )�type_expression�identifier_preparerz	 DEFAULT z	 NOT NULL)r��
format_columnro�dialect_implr�r�r�
TypeDecoratorr��primary_keyrb�_autoincrement_column�supports_smallserial�SmallIntegerrr�Sequence�optional�
BigIntegerr�r��get_column_default_string�computed�nullable)r�r/r�colspec�	impl_typers      r��get_column_specificationz&PGDDLCompiler.get_column_specification�s����-�-�-�f�5�5���K�,�,�T�\�:�:�	��i��!7�8�8�	'�!��I�
��	1��&�,�<�<�<���1�=�"�)�X�-B�C�C�=���&��v�~�v��?�?�'���/�'��)�X�%8�9�9�
%��<�'����I�x�'<�=�=�
%��>�)����9�$����s�T�\�7�?�?��� &�$(�M�@����
�G�
�4�4�V�<�<�G��"��;��0�0���?�&��s�T�\�\�&�/�:�:�:�:�G���	#��{�"�G��r�c�R��|jrxt|j��dj}t	|t
j��r?t	|jt
j��r |jj	stjd���tt|���|��S)Nrz�PostgreSQL dialect cannot produce the CHECK constraint for ARRAY of non-native ENUM; please specify create_constraint=False on this Enum datatype.)�_type_boundr�r�ror�r�ARRAY�	item_type�Enumr:r
r�r�r�visit_check_constraint)r�r0�typr�s   �r�r7z$PGDDLCompiler.visit_check_constraint$s�����!�	��z�)�*�*�1�-�2�C��3���/�/�	
��s�}�h�m�<�<�	
��
�1�	
�
�&�E�����]�D�)�)�@�@��L�L�Lr�c�F�d|j�|j��zS)NzCOMMENT ON TABLE %s IS NULL)r��format_tablerd)r�rs  r��visit_drop_table_commentz&PGDDLCompiler.visit_drop_table_comment4s(��,�t�}�/I�/I��L�0
�0
�
�	
r�c���|j}d�j�|���dd��fd�|jD�����d�S)NzCREATE TYPE z
 AS ENUM (r�c3�t�K�|]2}�j�tj|��d���V��3dS)T��
literal_bindsN)�sql_compilerr�r
�literal)r��er�s  �r�r�z7PGDDLCompiler.visit_create_enum_type.<locals>.<genexpr>>sU���������!�)�)�#�+�a�.�.��)�M�M������r�r�)rdr��format_typer{r�)r�r1r�s`  r��visit_create_enum_typez$PGDDLCompiler.visit_create_enum_type9st�������
�M�%�%�e�,�,�,�,��I�I����������
�
�
�
�
�	
r�c�J�|j}d|j�|��zS)NzDROP TYPE %s)rdr�rC)r�rr�s   r��visit_drop_enum_typez"PGDDLCompiler.visit_drop_enum_typeDs$��������!:�!:�5�!A�!A�B�Br�c�����j}|j}��|��d}|jr|dz
}|dz
}�jjr|jdd}|r|dz
}|��|d����d	|�|j	���d
�z
}|jdd}|r8|d�j�
|t�����zz
}|jdd
�|dd�
��fd�|jD����zz
}|jdd}|r7|dd�
d�|���D����zz
}|jdd}|r|d|�|��zz
}|jdd}	|	�%�j�|	dd���}
|d|
zz
}|S)NzCREATE zUNIQUE zINDEX �
postgresql�concurrently�
CONCURRENTLY F��include_schemaz ON rrjz	USING %s �opsr�r�c���g|]y}�j�t|tj��s|���n|dd���t
|d��r|j�vrd�|jzndz��zS)FT�r�r?r�rr�)r@r�r�r�ColumnClauser��hasattrr�)r��exprrMr�s  ��r�r�z4PGDDLCompiler.visit_create_index.<locals>.<listcomp>fs���������%�-�-�)�$�
�0G�H�H�"����)�)�)�!�&+�&*�.���#�4��/�/� �48�H��O�O��s�4�8�}�,�,�����r�roz
 WITH (%s)c��g|]}d|z��S)z%s = %sr�)r��storage_parameters  r�r�z4PGDDLCompiler.visit_create_index.<locals>.<listcomp>}s.�����-�"�$5�5���r��
tablespacez TABLESPACE %srmTrOz WHERE )r�rd�_verify_index_tablerhr��#_supports_create_index_concurrently�dialect_options�_prepared_index_namer:rb�validate_sql_phrase�	IDX_USING�lowerr{�expressionsr�r�r@r�)r�r1r��indexrGrIrj�
withclause�tablespace_name�whereclause�where_compiledrMs`          @r��visit_create_indexz PGDDLCompiler.visit_create_indexIse�����=������ � ��'�'�'����<�	��I��D������<�;�	(� �0��>�~�N�L��
(���'����%�%�e�E�%�B�B�B�B��!�!�%�+�.�.�.�.�
�	
��
�%�l�3�G�<���	����-�3�3�E�9�E�E�K�K�M�M�N�
�D�
�#�L�1�%�8�����I�I������!&� 1����
�
�
�	
��(�*�<�8��@�
��	��L��	�	���1;�1A�1A�1C�1C�������
�D� �/��=�l�K���	G��$�x�~�~�o�'F�'F�F�F�D��+�L�9�'�B���"�!�.�6�6��5��7���N�
�I��.�.�D��r�c��|j}d}|jjr|jdd}|r|dz
}||�|d���z
}|S)Nz
DROP INDEX rHrIrJTrK)rdr��!_supports_drop_index_concurrentlyrXrY)r�rr^rGrIs     r��visit_drop_indexzPGDDLCompiler.visit_drop_index�sa��������<�9�	(� �0��>�~�N�L��
(���'����)�)�%��)�E�E�E���r�c�r�d}|j� |d|j�|��zz
}g}|jD]i\}}}d|d<|jj|fi|��t
|d��r#|j|jvrd|j|jzndz}|j	|�d|�����j|d|j�
|jt���
���d	d
�|���d�z
}|j�'|d|j�|jd
���zz
}||�|��z
}|S)Nr�zCONSTRAINT %s Fr�r�rz WITH zEXCLUDE USING z (r�r�z WHERE (%s)Tr>)r�r��format_constraint�
_render_exprsr@r�rQr�rMr�rZrjr[r\r{rm�define_constraint_deferrability)	r�r0r�rGrrRr��op�exclude_elements	         r��visit_exclude_constraintz&PGDDLCompiler.visit_exclude_constraint�s������?�&��$�t�}�'F�'F��(�(��
�D���(�6�	B�	B�N�D�$��"'�B���7�d�/�7��C�C��C�C��4��'�'��,0�H�
��,F�,F��z�~�d�h�/�/�/���O�
�H�O�O�O�O�R�R�@�A�A�A�A����M�-�-�� �)�
�
��e�g�g�
�
�
�I�I�h�����	
�	
����'��M�D�$5�$=�$=�� ��%>�%�%��
�D�	
��4�4�Z�@�@�@���r�c���g}|jd}|�d��}|�Yt|ttf��s|f}|�dd��fd�|D����zdz��|dr|�d|dz��|d	d
ur|�d��n|d	dur|�d
��|drF|d�dd�����}|�d|z��|dr8|d}|�d�j	�
|��z��d�|��S)NrH�inheritsz
 INHERITS ( r�c3�L�K�|]}�j�|��V��dSr�)r�r�)r�r�r�s  �r�r�z2PGDDLCompiler.post_create_table.<locals>.<genexpr>�s3�����K�K�$�D�M�/�/��5�5�K�K�K�K�K�Kr�z )�partition_byz
 PARTITION BY %s�	with_oidsTz
 WITH OIDSFz
 WITHOUT OIDS�	on_commit�_rz
 ON COMMIT %srUz
 TABLESPACE %sr�)rXr/r�r�r�r�r{r�r�r�r�)r�rb�
table_opts�pg_optsro�on_commit_optionsr`s`      r��post_create_tablezPGDDLCompiler.post_create_table�s�����
��'��5���;�;�z�*�*�����h��u�
�6�6�
'�$�;����� ��)�)�K�K�K�K�(�K�K�K�K�K�L���
�
�
��>�"�	N����2�W�^�5L�L�M�M�M��;��4�'�'����n�-�-�-�-�
�[�
!�U�
*�
*����/�0�0�0��;��	E� '�� 4� <� <�S�#� F� F� L� L� N� N�����/�2C�C�D�D�D��<� �	�%�l�3�O����"�T�]�%8�%8��%I�%I�I�
�
�
��w�w�z�"�"�"r�c��|jdurtjd���d|j�|jdd���zS)NFz�PostrgreSQL computed columns do not support 'virtual' persistence; set the 'persisted' flag to None or True for PostgreSQL support.zGENERATED ALWAYS AS (%s) STOREDTrO)�	persistedr
r�r@r�r�)r��	generateds  r��visit_computed_columnz#PGDDLCompiler.visit_computed_column�s^����%�'�'��"�&���
�1�4�3D�3L�3L���U�$�4M�4
�4
�
�	
r�)r�r�r�r1r7r;rDrFrcrfrmrxr|r�r�s@r�rr�s��������+�+�+�ZM�M�M�M�M� 
�
�
�
	
�	
�	
�C�C�C�
G�G�G�R������: #� #� #�D

�

�

�

�

�

�

r�rc����eZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z�fd�Zd d�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z �xZ!S)!�PGTypeCompilerc��dS)Nr�r��r�r�r�s   r��visit_TSVECTORzPGTypeCompiler.visit_TSVECTOR�����zr�c��dS)Nr�r�r�s   r��
visit_INETzPGTypeCompiler.visit_INET�����vr�c��dS)Nr�r�r�s   r��
visit_CIDRzPGTypeCompiler.visit_CIDR�r�r�c��dS)Nr�r�r�s   r��
visit_MACADDRzPGTypeCompiler.visit_MACADDR�����yr�c��dS)Nr�r�r�s   r��visit_MONEYzPGTypeCompiler.visit_MONEY�����wr�c��dS)Nr�r�r�s   r��	visit_OIDzPGTypeCompiler.visit_OID�s���ur�c��dS)Nr�r�r�s   r��visit_REGCLASSzPGTypeCompiler.visit_REGCLASS�r�r�c�,�|jsdSdd|jizS)NrzFLOAT(%(precision)s)r�r�r�s   r��visit_FLOATzPGTypeCompiler.visit_FLOAT	s$����	K��7�)�[�%�/�,J�J�Jr�c��dS)NzDOUBLE PRECISIONr�r�s   r��visit_DOUBLE_PRECISIONz%PGTypeCompiler.visit_DOUBLE_PRECISION	s��!�!r�c��dS)Nrr�r�s   r��visit_BIGINTzPGTypeCompiler.visit_BIGINT
	����xr�c��dS)N�HSTOREr�r�s   r��visit_HSTOREzPGTypeCompiler.visit_HSTORE
	r�r�c��dS)Nrpr�r�s   r��
visit_JSONzPGTypeCompiler.visit_JSON	r�r�c��dS)N�JSONBr�r�s   r��visit_JSONBzPGTypeCompiler.visit_JSONB	r�r�c��dS)N�	INT4RANGEr�r�s   r��visit_INT4RANGEzPGTypeCompiler.visit_INT4RANGE	����{r�c��dS)N�	INT8RANGEr�r�s   r��visit_INT8RANGEzPGTypeCompiler.visit_INT8RANGE	r�r�c��dS)N�NUMRANGEr�r�s   r��visit_NUMRANGEzPGTypeCompiler.visit_NUMRANGE	r�r�c��dS)N�	DATERANGEr�r�s   r��visit_DATERANGEzPGTypeCompiler.visit_DATERANGE	r�r�c��dS)N�TSRANGEr�r�s   r��
visit_TSRANGEzPGTypeCompiler.visit_TSRANGE"	r�r�c��dS)N�	TSTZRANGEr�r�s   r��visit_TSTZRANGEzPGTypeCompiler.visit_TSTZRANGE%	r�r�c��|j|fi|��Sr�)�visit_TIMESTAMPr�s   r��visit_datetimezPGTypeCompiler.visit_datetime(	s��#�t�#�E�0�0�R�0�0�0r�c���|jr|jjs!tt|��j|fi|��S|j|fi|��Sr�)r:r�r�r�r~r�
visit_ENUM)r�r�r�r�s   �r�rzPGTypeCompiler.visit_enum+	sY���� �	0���(I�	0�9�5���.�.�9�%�F�F�2�F�F�F�"�4�?�5�/�/�B�/�/�/r�Nc�H�|�|jj}|�|��Sr�)r�r!rC)r�r�r!r�s    r�r�zPGTypeCompiler.visit_ENUM1	s'���&�"&�,�"B��"�.�.�u�5�5�5r�c�`�dt|dd���
d|jznd�d|jrdpddz��S)	Nr�r��(%d)r�r�WITH�WITHOUT�
 TIME ZONE��getattrr�r�r�s   r�r�zPGTypeCompiler.visit_TIMESTAMP7	�W����u�k�4�0�0�<�
�U�_�$�$��
�
��^�
&��
3�)�|�C�C�	
�	
r�c�`�dt|dd���
d|jznd�d|jrdpddz��S)	Nr�r�r�r�rr�r�r�r�r�s   r��
visit_TIMEzPGTypeCompiler.visit_TIME?	r�r�c�Z�d}|j�
|d|jzz
}|j�
|d|jzz
}|S)Nr�rz (%d))r�r�)r�r�r�rGs    r��visit_INTERVALzPGTypeCompiler.visit_INTERVALG	s>�����<�#��C�%�,�&�&�D��?�&��G�e�o�-�-�D��r�c�V�|jrd}|j�
|d|jzz
}n
d|jz}|S)NzBIT VARYINGr�zBIT(%d))r�r�)r�r�r��compileds    r��	visit_BITzPGTypeCompiler.visit_BITO	s<���=�	0�$�H��|�'��F�U�\�1�1��� �5�<�/�H��r�c��dS)Nr!r�r�s   r��
visit_UUIDzPGTypeCompiler.visit_UUIDX	r�r�c��|j|fi|��Sr�)�visit_BYTEAr�s   r��visit_large_binaryz!PGTypeCompiler.visit_large_binary[	s���t���,�,��,�,�,r�c��dS)Nr�r�r�s   r�r�zPGTypeCompiler.visit_BYTEA^	r�r�c��|�|j��}tjddd|j�|jndzz|d���S)Nz((?: COLLATE.*)?)$z%s\1�[]r)�count)r�r5�re�sub�
dimensions)r�r�r�rxs    r��visit_ARRAYzPGTypeCompiler.visit_ARRAYa	s_�����U�_�-�-���v�!���+0�+;�+G�u�'�'�Q�P��
��
�
�
�	
r�r�)"r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�r�r�r�r�r�r�r�r�r�r�s@r�r~r~�s�����������������������������K�K�K�"�"�"�������������������������������1�1�1�0�0�0�0�0�6�6�6�6�
�
�
�
�
�
����������-�-�-����
�
�
�
�
�
�
r�r~c� �eZdZeZd�Zdd�ZdS)�PGIdentifierPreparerc�x�|d|jkr(|dd��|j|j��}|S)Nrrr�)�
initial_quoter��escape_to_quote�escape_quote)r�r�s  r��_unquote_identifierz(PGIdentifierPreparer._unquote_identifierw	sC����8�t�)�)�)��!�B�$�K�'�'��$�d�&7���E��r�Tc���|jstjd���|�|j��}|�|��}|js|r|�|�|��dz|z}|S)Nz%PostgreSQL ENUM type requires a name.�.)r�r
r�r�r�omit_schema�quote_schema)r�r�r�r�rs     r�rCz PGIdentifierPreparer.format_type~	s����z�	L��"�#J�K�K�K��z�z�%�*�%�%���1�1�%�8�8��� �	D��	D�!�,��$�$�%5�6�6��<�t�C�D��r�N)T)r�r�r��RESERVED_WORDS�reserved_wordsr�rCr�r�r�r�r�s	s<������#�N����
�
�
�
�
�
r�r�c�4�eZdZd�Zdd�Zdd�Zdd�Zd	d�ZdS)
�PGInspectorc�F�tj�||��dSr�)r�	Inspectorr�)r��conns  r�r�zPGInspector.__init__�	s!����%�%�d�D�1�1�1�1�1r�Nc�R�|j�|j|||j���S)z(Return the OID for the given table name.��
info_cache)r��
get_table_oidrr�)r��
table_namers   r�r�zPGInspector.get_table_oid�	s0���|�)�)��I�z�6�d�o�*�
�
�	
r�c�T�|p|j}|j�|j|��S)aKReturn a list of ENUM objects.

        Each member is a dictionary containing these fields:

            * name - name of the enum
            * schema - the schema name for the enum.
            * visible - boolean, whether or not this enum is visible
              in the default search path.
            * labels - a list of string labels that apply to the enum.

        :param schema: schema name.  If None, the default schema
         (typically 'public') is used.  May also be set to '*' to
         indicate load enums for all schemas.

        .. versionadded:: 1.0.0

        )�default_schema_namer��_load_enumsr�r�rs  r��	get_enumszPGInspector.get_enums�	s+��$�3�4�3���|�'�'��	�6�:�:�:r�c�T�|p|j}|j�|j|��S)aReturn a list of FOREIGN TABLE names.

        Behavior is similar to that of
        :meth:`_reflection.Inspector.get_table_names`,
        except that the list is limited to those tables that report a
        ``relkind`` value of ``f``.

        .. versionadded:: 1.0.0

        )r�r��_get_foreign_table_namesrr�s  r��get_foreign_table_namesz#PGInspector.get_foreign_table_names�	s+���3�4�3���|�4�4�T�Y��G�G�Gr���plain�materializedc�R�|j�|j||j|���S)a�Return all view names in `schema`.

        :param schema: Optional, retrieve names from a non-default schema.
         For special quoting, use :class:`.quoted_name`.

        :param include: specify which types of views to return.  Passed
         as a string value (for a single type) or a tuple (for any number
         of types).  Defaults to ``('plain', 'materialized')``.

         .. versionadded:: 1.1

        )r��include)r��get_view_namesrr�)r�rr�s   r�r�zPGInspector.get_view_names�	s0���|�*�*��I�v�$�/�7�+�
�
�	
r�r��Nr�)r�r�r�r�r�r�r�r�r�r�r�r�r��	sw������2�2�2�
�
�
�
�;�;�;�;�*H�H�H�H�
�
�
�
�
�
r�r�c��eZdZdZdS)r�create_enum_typeNr�r�r�r�rr�	r�r�rc��eZdZdZdS)r �drop_enum_typeNr�r�r�r�r r �	s������%�N�N�Nr�r c�*��eZdZd�Z�fd�Zd�Z�xZS)�PGExecutionContextc�d�|�d|j�|��z|��S)Nzselect nextval('%s'))�_execute_scalarr!r�)r�r�r�s   r��
fire_sequencez PGExecutionContext.fire_sequence�	s<���#�#�&��*�:�:�3�?�?�@�
�
�
�	
r�c���|j�rR||jju�rC|jr4|jjr(|�d|jjz|j��S|j�|jj	r�|jj
r�	|j}n�#t$r}|jj
}|j
}|ddtddt|��z
��z�}|ddtddt|��z
��z�}|�d|�d�}|x|_}YnwxYw|j� |j�|j��}nd}|�
d|�d|�d�}nd|�d�}|�||j��St%t&|���|��S)	Nz	select %sr�rt�_seqzselect nextval('"z"."z"'))r%rbr&�server_default�has_argumentr�argror�is_sequencer*�_postgresql_seq_name�AttributeErrorr��maxr�r
rr�r�get_insert_default)	r�r/�seq_name�tabr�r�rr
r�s	        �r�rz%PGExecutionContext.get_insert_default�	s������)	>�&�F�L�,N�"N�"N��$�(
>��)>�)K�(
>��+�+��&�"7�";�;�V�[������'���*�(�/5�~�/F�(�B�%�:�H�H��%�B�B�B� �,�+�C� �+�C��a�"�s�1�r�C��H�H�}�'>�'>�">�>�?�C��a�"�s�1�r�C��H�H�}�'>�'>�">�>�?�C�*-�#�#�s�s�s�3�D�=A�A�F�/�(�(�(�
B�����<�+�'+��'H�'H���(�(�$�$�(,�$�#�/�/�(�(�(� ����C�C�0�9A���B�C��+�+�C���=�=�=��'��.�.�A�A�&�I�I�Is�4A<�<BD�Dc�6�t�|��Sr�)�AUTOCOMMIT_REGEXP�match)r��	statements  r��should_autocommit_textz)PGExecutionContext.should_autocommit_text
s�� �&�&�y�1�1�1r�)r�r�r�rrrr�r�s@r�rr�	s\�������
�
�
�,J�,J�,J�,J�,J�\2�2�2�2�2�2�2r�rc	�4��eZdZdZdZdZdZdZdZdZ	dZ
dZdZdZ
dZdZdZdZdZeZeZeZeZeZeZeZeZ dZ!e"j#ddididd�fe"j$ddddddd�fgZ%d	Z&dZ'dZ(dZ)			d-d
�Z*�fd�Z+d�Z,e-gd
���Z.d�Z/d�Z0d�Z1d�Z2	d.d�Z3	d.d�Z4d�Z5d�Z6d�Z7d/d�Z8d/d�Z9d/d�Z:d�Z;e<j=d/d���Z>e<j=d���Z?e<j=d/d���Z@e<j=d/d���ZAe<j=	d0d ���ZBe<j=d/d!���ZCe<j=d/d"���ZDd#�ZEe<j=d/d$���ZFe<j=		d1d%���ZGd&�ZHe<j=d'���ZIe<j=	d/d(���ZJe<j=d/d)���ZKe<j=d/d*���ZLd/d+�ZMd,�ZN�xZOS)2�	PGDialectrHT�?F�pyformatN)rjrmrMrIrorU)�ignore_search_pathrUrqrrrsro)�postgresql_ignore_search_pathc�^�tjj|fi|��||_||_||_dSr�)r�DefaultDialectr��isolation_level�_json_deserializer�_json_serializer)r�r�json_serializer�json_deserializerrs     r�r�zPGDialect.__init__Q
s@��	��'��7�7��7�7�7�
 /���"3��� /����r�c�T��tt|���|��|jdko|j�dd��|_|jdk|_|jsc|j�	��|_|j�
tjd��|j�
td��|jdk|_|jdkp|�d��dk|_|jdk|_|jdk|_dS)N��r��implicit_returningT�r&r	)�	r�z show standard_conforming_stringsrS)r�r�
initialize�server_version_info�__dict__r/r'r��colspecs�copyr�rr6r�r'�scalarr�rWre)r�r
r�s  �r�r*zPGDialect.initializea
s1���
�i����)�)�*�5�5�5��$��
�
>�
�
�!�!�"6��=�=�
	
��%)�$<��$F��!��(�	*� �M�.�.�0�0�D�M��M���h�m�T�2�2�2��M���d�D�)�)�)�%)�$<��$F��!�
�$�v�-�
N�� � �!C�D�D��M�	
��
�$��.�	
�0�26�1I�N
�2
��.�.�.r�c�$���j��fd�}|SdS)Nc�>����|�j��dSr�)�set_isolation_levelr)r�r�s �r��connectz%PGDialect.on_connect.<locals>.connect�
s"����(�(��t�/C�D�D�D�D�Dr�)r)r�r3s` r��
on_connectzPGDialect.on_connect�
s6�����+�
E�
E�
E�
E�
E��N��4r�)�SERIALIZABLEzREAD UNCOMMITTEDzREAD COMMITTEDzREPEATABLE READc
�b�|�dd��}||jvr:tjd|�d|j�dd�|j�������|���}|�d|z��|�d��|���dS)	NrtrzInvalid value 'z2' for isolation_level. Valid isolation levels for z are r�z=SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL %s�COMMIT)	r��_isolation_lookupr
�
ArgumentErrorr�r{�cursorr�close)r�r
�levelr:s    r�r2zPGDialect.set_isolation_level�
s����
�
�c�3�'�'����.�.�.��#�#��5�5�$�)�)�)�T�Y�Y�t�/E�%F�%F�%F�H���
�
�"�"�$�$�����
!�#(�
)�	
�	
�	
�	���x� � � ��������r�c���|���}|�d��|���d}|���|���S)Nz show transaction isolation levelr)r:r�fetchoner;r�)r�r
r:�vals    r��get_isolation_levelzPGDialect.get_isolation_level�
sU���"�"�$�$�����9�:�:�:��o�o����"���������y�y�{�{�r�c�:�|�|j��dSr�)�do_beginr
�r�r
�xids   r��do_begin_twophasezPGDialect.do_begin_twophase�
s���
�
�j�+�,�,�,�,�,r�c�6�|�d|z��dS)NzPREPARE TRANSACTION '%s')rrCs   r��do_prepare_twophasezPGDialect.do_prepare_twophase�
s"�����5��;�<�<�<�<�<r�c��|r`|r|�d��|�d|z��|�d��|�|j��dS|�|j��dS)N�ROLLBACKzROLLBACK PREPARED '%s'�BEGIN)r�do_rollbackr
�r�r
rD�is_prepared�recovers     r��do_rollback_twophasezPGDialect.do_rollback_twophase�
s����	4��
/�
�"�"�:�.�.�.����7�#�=�>�>�>����w�'�'�'����Z�2�3�3�3�3�3����Z�2�3�3�3�3�3r�c��|r`|r|�d��|�d|z��|�d��|�|j��dS|�|j��dS)NrIzCOMMIT PREPARED '%s'rJ)rrKr
�	do_commitrLs     r��do_commit_twophasezPGDialect.do_commit_twophase�
s����	2��
/��"�"�:�.�.�.����5��;�<�<�<����w�'�'�'����Z�2�3�3�3�3�3��N�N�:�0�1�1�1�1�1r�c�h�|�tjd����}d�|D��S)Nz!SELECT gid FROM pg_prepared_xactsc��g|]
}|d��S�rr�)r��rows  r�r�z1PGDialect.do_recover_twophase.<locals>.<listcomp>�
s��,�,�,�3��A��,�,�,r�)rr
rG)r�r
�	resultsets   r��do_recover_twophasezPGDialect.do_recover_twophase�
s;���&�&��H�8�9�9�
�
�	�-�,�)�,�,�,�,r�c�,�|�d��S)Nzselect current_schema())r/)r�r
s  r��_get_default_schema_namez"PGDialect._get_default_schema_name�
s��� � �!:�;�;�;r�c�8�d}|�tj|���tjdtj|j����tj	�������}t|�����S)Nz=select nspname from pg_namespace where lower(nspname)=:schemarr�)rr
rG�
bindparams�	bindparamrr�r\r�Unicode�bool�first)r�r
r�queryr:s     r��
has_schemazPGDialect.has_schema�
s���N�	��#�#��H�U�O�O�&�&��
���N�<�6�<�>�>�2�2�"�*����
�
�
�
���F�L�L�N�N�#�#�#r�c�Z�|�l|�tjd���tjdtj|��tj�������}n�|�tjd���tjdtj|��tj���tjdtj|��tj�������}t|�
����S)Nz�select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where pg_catalog.pg_table_is_visible(c.oid) and relname=:namer�r�ztselect relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname=:schema and relname=:namer�rr
rGr\r]rr�rr^r_r`)r�r
r�rr:s     r��	has_tablezPGDialect.has_table�
s���>��'�'���(���
�*��M����z�2�2�&�.������

�
�F�F� �'�'���$����*��M����z�2�2�&�.����
�M� ���v�.�.�&�.����
����F�$�F�L�L�N�N�#�#�#r�c�Z�|�l|�tjd���tjdtj|��tj�������}n�|�tjd���tjdtj|��tj���tjdtj|��tj�������}t|�
����S)Nz�SELECT relname FROM pg_class c join pg_namespace n on n.oid=c.relnamespace where relkind='S' and n.nspname=current_schema() and relname=:namer�r�z�SELECT relname FROM pg_class c join pg_namespace n on n.oid=c.relnamespace where relkind='S' and n.nspname=:schema and relname=:namerrd)r�r
�
sequence_namerr:s     r��has_sequencezPGDialect.has_sequences���>��'�'���(���
�*��M����}�5�5�&�.������

�
�F�F� �'�'���:����*��M����}�5�5�&�.����
�M� ���v�.�.�&�.����
����F�&�F�L�L�N�N�#�#�#r�c���|�d}tj|��}nd}tj|��}|�tjdt	j|��tj�����}|�F|�tjdt	j|��tj�����}|�|��}t|�
����S)Na
            SELECT EXISTS (
                SELECT * FROM pg_catalog.pg_type t, pg_catalog.pg_namespace n
                WHERE t.typnamespace = n.oid
                AND t.typname = :typname
                AND n.nspname = :nspname
                )
                z�
            SELECT EXISTS (
                SELECT * FROM pg_catalog.pg_type t
                WHERE t.typname = :typname
                AND pg_type_is_visible(t.oid)
                )
                �typnamer��nspname)r
rGr\r]rr�rr^rr_r/)r�r
�	type_namerrar:s      r�rzPGDialect.has_type4s������E��H�U�O�O�E�E��E��H�U�O�O�E�� � ��M��4�>�)�4�4�H�<L�
�
�
�
�
��
���$�$��
��t�~�f�5�5�X�=M������E�
�#�#�E�*�*���F�M�M�O�O�$�$�$r�c��|�d�����}tjd|��}|st	d|z���td�|�ddd��D����S)Nzselect version()zQ.*(?:PostgreSQL|EnterpriseDB) (\d+)\.?(\d+)?(?:\.(\d+))?(?:\.\d+)?(?:devel|beta)?z,Could not determine version from string '%s'c�0�g|]}|�t|����Sr�)�int)r��xs  r�r�z6PGDialect._get_server_version_info.<locals>.<listcomp>as��H�H�H��!�-�c�!�f�f�-�-�-r�rr�r	)rr/r�r�AssertionErrorr�rE)r�r
r�ms    r��_get_server_version_infoz"PGDialect._get_server_version_infoVs������1�2�2�9�9�;�;���H�
C�
�
�
��
�	� �>��B���
��H�H�a�g�g�a��A�&6�&6�H�H�H�I�I�Ir�c��d}|�d}nd}d|z}tj|��}|�tj|��}tj|���t
j���}|�t
j���}|r3|�tj	dt
j�����}|�
|||�	��}	|	���}|�tj
|���|S)
z�Fetch the oid for schema.table_name.

        Several reflection methods require the table oid.  The idea for using
        this method is that it can be fetched one time and cached for
        subsequent calls.

        Nzn.nspname = :schemaz%pg_catalog.pg_table_is_visible(c.oid)a	
            SELECT c.oid
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE (%s)
            AND c.relname = :table_name AND c.relkind in
            ('r', 'v', 'm', 'f', 'p')
        )r�)rRrr�)r�r)rr�r
rGr\rr^r��Integerr]rr/r
�NoSuchTableError)
r�r
r�rr��	table_oid�schema_where_clauserar�r�s
          r�r�zPGDialect.get_table_oidcs����	���"7���"I��
�"�
"�	��^�J�/�/�
����^�F�+�+�F��H�U�O�O�&�&�(�2B�&�C�C��
�I�I�(�*�I�+�+���	N����S�]�8�8�;K�L�L�L�M�M�A����q�Z���G�G���H�H�J�J�	����&�z�2�2�2��r�c��|�tjd���tj�����}d�|D��S)NzOSELECT nspname FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' ORDER BY nspname)rkc��g|]\}|��Sr�r��r�r�s  r�r�z.PGDialect.get_schema_names.<locals>.<listcomp>����)�)�)����)�)�)r�)rr
rGr�rr^)r�r
r��results    r��get_schema_nameszPGDialect.get_schema_names�sU���#�#��H�#�
�
��g�h�.�g�/�/�
�
��*�)�&�)�)�)�)r�c��|�tjd���tj���|�|n|j���}d�|D��S)Nz�SELECT c.relname FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = :schema AND c.relkind in ('r', 'p')��relnamerc��g|]\}|��Sr�r�r{s  r�r�z-PGDialect.get_table_names.<locals>.<listcomp>�r|r��rr
rGr�rr^r��r�r
rr�r}s     r��get_table_nameszPGDialect.get_table_names�sj���#�#��H�H�
�
��g�h�.�g�/�/�#�/�6�6�T�5M�
$�
�
��*�)�&�)�)�)�)r�c��|�tjd���tj���|�|n|j���}d�|D��S)Nz|SELECT c.relname FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = :schema AND c.relkind = 'f'r�rc��g|]\}|��Sr�r�r{s  r�r�z6PGDialect._get_foreign_table_names.<locals>.<listcomp>�r|r�r�r�s     r�r�z"PGDialect._get_foreign_table_names�sj���#�#��H�@�
�
��g�h�.�g�/�/�#�/�6�6�T�5M�
$�
�
��*�)�&�)�)�)�)r�r�c	���ddd��	�fd�tj|��D��}n!#t$rtd|�d����wxYw|std���|�tjdd	�d
�|D����z���tj
���|�|n|j���}d
�|D��S)Nrrrr�c� ��g|]
}�|��Sr�r�)r��i�include_kinds  �r�r�z,PGDialect.get_view_names.<locals>.<listcomp>�s���D�D�D��\�!�_�D�D�Dr�zinclude zU unknown, needs to be a sequence containing one or both of 'plain' and 'materialized'zZempty include, needs to be a sequence containing one or both of 'plain' and 'materialized'z~SELECT c.relname FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = :schema AND c.relkind IN (%s)r�c3� K�|]	}d|zV��
dSr�r�)r��elems  r�r�z+PGDialect.get_view_names.<locals>.<genexpr>�s&����=�=�t�V�d�]�=�=�=�=�=�=r�r�rc��g|]\}|��Sr�r�r{s  r�r�z,PGDialect.get_view_names.<locals>.<listcomp>�r|r�)r�to_list�KeyError�
ValueErrorrr
rGr{r�rr^r�)r�r
rr�r��kindsr}r�s       @r�r�zPGDialect.get_view_names�s(���
"%�c�:�:��	�D�D�D�D�d�l�7�.C�.C�D�D�D�E�E���	�	�	��*�?F�w�w�I���
�	����
�	��<���
�
�#�#��H�B��9�9�=�=�u�=�=�=�=�=�?�
�
�
�g�h�.�g�/�/�#�/�6�6�T�5M�$�
�
��*�)�&�)�)�)�)s	� )�Ac��|�tjd���tj���|�|n|j|���}|S)Nz�SELECT pg_get_viewdef(c.oid) view_def FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = :schema AND c.relname = :view_name AND c.relkind IN ('v', 'm'))�view_def)r�	view_name)r/r
rGr�rr^r�)r�r
r�rr�r�s      r��get_view_definitionzPGDialect.get_view_definition�s]���$�$��H�.�
�
�
�g�x�/�g�0�0�#�/�6�6�T�5M��%�	
�	
���r�c���|�||||�d�����}|jdkrdnd}d|z}tj|���tjdtj������	tj
tj
�	��}|�||�
��}	|	���}
|�
|��}td�|�|d�
��D����}g}
|
D]=\}}}}}}}}|�|||||||||�	�	}|
�|���>|
S)Nr�r�)�za.attgenerated as generatedzNULL as generateda�
            SELECT a.attname,
              pg_catalog.format_type(a.atttypid, a.atttypmod),
              (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid)
                FROM pg_catalog.pg_attrdef d
               WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum
               AND a.atthasdef)
              AS DEFAULT,
              a.attnotnull, a.attnum, a.attrelid as table_oid,
              pgd.description as comment,
              %s
            FROM pg_catalog.pg_attribute a
            LEFT JOIN pg_catalog.pg_description pgd ON (
                pgd.objoid = a.attrelid AND pgd.objsubid = a.attnum)
            WHERE a.attrelid = :table_oid
            AND a.attnum > 0 AND NOT a.attisdropped
            ORDER BY a.attnum
        rwr�)�attnamer�rwc3�`K�|])}|dr|df|fn|d|df|fV��*dS)�visibler�rNr�)r��recs  r�r�z(PGDialect.get_columns.<locals>.<genexpr>	sf����
�
���9�~�
5�c�&�k�^�S�!�!��x�=�#�f�+�.��4�
�
�
�
�
�
r��*r)r�r/r+r
rGr\r]rrur�r^r�fetchall�
_load_domainsr�r��_get_column_infor�)r�r
r�rr�rwr{�SQL_COLSr�r��rows�domainsr�r�r�rC�default_r�attnum�comment�column_infos                     r��get_columnszPGDialect.get_columns�s����&�&��
�F�r�v�v�l�7K�7K�'�
�
�	��'�5�0�0�
*�)�$�	�
�$�%
�	�,
�H�X���
�Z��
�k��9I�J�J�J�
K�
K�
�W�X�-�x�7G�W�
H�
H�	
�

���q�I��6�6���z�z�|�|���$�$�Z�0�0���
�
��'�'�
�3�'�?�?�	
�
�
�
�
�����	(�	(�	
����������/�/����������
�
�K�
�N�N�;�'�'�'�'��r�c
���d�}
tjdd|��}|
|��\}}ttj|����}
|}tjd|��}|r|�d��}tjd|��}|rK|�d��r6ttjd|�d������}nd}i}|d	kr<|r7|�d
��\}}t|��t|��f}n�d}n�|dkrd}n�|d
krd}n�|dvrd|d<|rt|��|d<d}n�|dvrd|d<|rt|��|d<d}n�|dkrd|d<|rt|��f}n}d}nz|�	d��rStj
d|tj��}|rt|��|d<|r|�d��|d<d}d}n|rt|��f}	||jvr|j|}n�|
|vrC||
}t}|d|d<|ds|d|d<t|d��}nc|
|vr\||
}|d}|
|��\}}ttj|����}
|o|d}|dr
|s|d}��d}	|r!||i|��}|r|jd |��}n'tjd!|�d"|�d#���tj}|	rt#||	d$k�%��}d}nd}d}|��tjd&|��}|��t%|jtj��rd}|}d'|�d(��vrL|�J|�d��d)|zzd'z|�d(��z|�d*��z}t#||||||�+��}|�||d,<|S)-Nc�X�tjdd|��|�d��fS)Nz\[\]$r�r�)r�r��endswith)�attypes r��_handle_array_typez6PGDialect._get_column_info.<locals>._handle_array_type7s.����x��V�,�,�����%�%��
r�z\(.*\)r�z\(([\d,]+)\)rz\((.*)\)�\s*,\s*r�rH�,rT)�5rC)rVrXTr�r�)rWrYr[FrOr�r�z
interval (.+)r�r�r�r�labelsr�r.rr;zDid not recognize type 'z
' of column '�'r�)r�rzz(nextval\(')([^']+)('.*$)r�r�z"%s"r	)r�ror.r�
autoincrementr�r-)r�r�r�r�quoted_token_parser�searchrE�splitro�
startswithr�I�
ischema_namesr�r�r�NULLTYPEr��
issubclassr�ru)r�r�rCrrr�r�rr�r{r�r��is_array�enum_or_domain_keyr.�charlen�argsr�prec�scale�field_matchr�r�domainr-r�r�schr�s                             r�r�zPGDialect._get_column_info+s��	�	�	���	�2�{�3�3��.�-�f�5�5����#�4�#;�F�#C�#C�D�D���;���)�O�[�9�9���	'��m�m�A�&�&�G��y��k�2�2���	�D�J�J�q�M�M�	����*�d�j�j��m�m�<�<�=�=�D�D��D����Y����
�%�m�m�C�0�0���e��D�	�	�3�u�:�:�.������
�)�
)�
)��D�D�
�y�
 �
 ��D�D�
�J�
J�
J�!%�F�:���
3�&)�'�l�l��{�#��D�D�
�
�
�
�
"'�F�:���
3�&)�'�l�l��{�#��D�D�
�}�
$�
$� $�F�9���
��G���������
�
�
�z�
*�
*�		#��(�#3�V�R�T�B�B�K��
3�&)�'�l�l��{�#��
8�#.�#4�#4�Q�#7�#7��x� ��F��D�D�
�	#���L�L�?�D�	���+�+�+��,�V�4���#�u�,�,��/�0����!%�f���v���I��6�'+�H�~�F�8�$��T�(�^�,�,���#�w�.�.� �!3�4����)��#5�#5�f�#=�#=� ���%*�4�+C�F�+K�+K�%L�%L�"�$�:��z�(:���)�$�0�W�0�%�Y�/�G������	(��g�t�.�v�.�.�G��
@�6�$�,�X�6�w�?�?����I�I�@F������M�
�
�
��'�G��	��G�y�C�7G�H�H�H�H��G�G��H��
����I�>��H�H�E�� ��g�4�h�6F�G�G�)�$(�M����e�k�k�!�n�n�,�,���
���A���!�C�<�)��� �+�+�a�.�.�)� �+�+�a�.�.�	)�������'��

�
�
����&.�K�
�#��r�c�>�|�||||�d�����}|jdkrd|�dd��z}nd}t	j|���tj���}|�	||�	��}d
�|�
��D��}	d}
t	j|
���tj���}|�	||�	��}|���}|	|d
�S)Nr�r�)r&�aq
                SELECT a.attname
                FROM
                    pg_class t
                    join pg_index ix on t.oid = ix.indrelid
                    join pg_attribute a
                        on t.oid=a.attrelid AND %s
                 WHERE
                  t.oid = :table_oid and ix.indisprimary = 't'
                ORDER BY a.attnum
            �a.attnum�	ix.indkeya�
                SELECT a.attname
                FROM pg_attribute a JOIN (
                    SELECT unnest(ix.indkey) attnum,
                           generate_subscripts(ix.indkey, 1) ord
                    FROM pg_index ix
                    WHERE ix.indrelid = :table_oid AND ix.indisprimary
                    ) k ON a.attnum=k.attnum
                WHERE a.attrelid = :table_oid
                ORDER BY k.ord
            �r�r�c��g|]
}|d��SrUr�)r��rs  r�r�z/PGDialect.get_pk_constraint.<locals>.<listcomp>�s��+�+�+���!��+�+�+r�z�
        SELECT conname
           FROM  pg_catalog.pg_constraint r
           WHERE r.conrelid = :table_oid AND r.contype = 'p'
           ORDER BY 1
        )�conname)�constrained_columnsr�)r�r/r+�
_pg_index_anyr
rGr�rr^rr�r/)r�r
r�rr�rw�PK_SQLrr�r�PK_CONS_SQLr�s            r��get_pk_constraintzPGDialect.get_pk_constraint�s'���&�&��
�F�r�v�v�l�7K�7K�'�
�
�	��#�f�,�,�
��$�$��K����F�F�"
�F�
�H�V���$�$�X�-=�$�>�>�����q�I��6�6��+�+�a�j�j�l�l�+�+�+����
�H�[�!�!�)�)�(�2B�)�C�C�����q�I��6�6���x�x�z�z��'+�T�:�:�:r�c�b��|j�|�||||�d�����}d}tjd��}tj|���tj	tj	���}	|�
|	|���}
g}|
���D]�\}}
}tj||
���
��}|\
}}}}}}}}}}}}}|�
|dkrdnd	}�fd
�tjd|��D��}|r||jkr|}n%|}n"|r��|��}n
|�||kr|}��|��}�fd�tjd
|��D��}||||||||||d�d�}|�|����|S)Nr�r�a�
          SELECT r.conname,
                pg_catalog.pg_get_constraintdef(r.oid, true) as condef,
                n.nspname as conschema
          FROM  pg_catalog.pg_constraint r,
                pg_namespace n,
                pg_class c

          WHERE r.conrelid = :table AND
                r.contype = 'f' AND
                c.oid = confrelid AND
                n.oid = c.relnamespace
          ORDER BY 1
        a/FOREIGN KEY \((.*?)\) REFERENCES (?:(.*?)\.)?(.*?)\((.*?)\)[\s]?(MATCH (FULL|PARTIAL|SIMPLE)+)?[\s]?(ON UPDATE (CASCADE|RESTRICT|NO ACTION|SET NULL|SET DEFAULT)+)?[\s]?(ON DELETE (CASCADE|RESTRICT|NO ACTION|SET NULL|SET DEFAULT)+)?[\s]?(DEFERRABLE|NOT DEFERRABLE)?[\s]?(INITIALLY (DEFERRED|IMMEDIATE)+)?)r��condef)rb�
DEFERRABLETFc�:��g|]}��|����Sr��r��r�rpr�s  �r�r�z.PGDialect.get_foreign_keys.<locals>.<listcomp>D
s7���#�#�#���,�,�Q�/�/�#�#�#r�r�c�:��g|]}��|����Sr�r�r�s  �r�r�z.PGDialect.get_foreign_keys.<locals>.<listcomp>[
s7��� � � ���,�,�Q�/�/� � � r�z\s*,\s)�onupdate�ondeleter8rHr)r�r��referred_schema�referred_table�referred_columns�options)r!r�r/r��compiler
rGr�rr^rr�r��groupsr�r�r�r�)r�r
r�rrr�rw�FK_SQL�FK_REGEXrr��fkeysr�r��	conschemarrr�r�r�r�rtrr�r�r8rH�fkey_dr�s                           @r��get_foreign_keyszPGDialect.get_foreign_keys
sa����+���&�&��
�F�r�v�v�l�7K�7K�'�
�
�	�
���:�
7�	
�	
��
�H�V���$�$��$�X�-=�
%�
�
��
���q�	��2�2����*+�*�*�,�,�>	!�>	!�&�G�V�Y��	�(�F�+�+�2�2�4�4�A� �
�#��� �����������%�%/�<�%?�%?�T�T�U�
�#�#�#�#���*�.A�B�B�#�#�#��
-�
)��� 8�8�8�&/�O�O�&,�O�O� �
)�#+�">�">��"O�"O����#��)�(;�(;�#)��%�9�9�.�I�I�N� � � � ���)�-=�>�>� � � ��
 �':�#2�"0�$4� (� (�",�!*�"���

�
�F�
�L�L�� � � � ��r�c����|jdkr3dd���fd�tdd��D����zS��d��d�S)	N)r&rr�z OR c3�(�K�|]}d�|�fzV��
dS)z%s[%d] = %sNr�)r��indr��
compare_tos  ��r�r�z*PGDialect._pg_index_any.<locals>.<genexpr>x
s@�����(�(�;>�
��S�#� 6�6�(�(�(�(�(�(r�r�
z = ANY(r�)r+r{�range)r�r�r�s ``r�r�zPGDialect._pg_index_anyp
sy�����#�f�,�,��F�K�K�(�(�(�(�(�BG��2�,�,�(�(�(����
�&)�S�S�*�*�*�5�5r�c
���"�|�||||�d�����}|jdkrKd|jdkrdnd�d|jdkrd	nd
�d|jdkrd
nd
�d|�dd���d�	}nd|jdkrdnd
�d�}t	j|���tjtj���}|�	||���}td���}	d}
|���D�]t}|\}}
}}}}}}}}}}|r ||
krtj
d|z��|}
�4|r||
kstj
d|z��|}
||	v}|	|}|�||d|<|�s|���}|r,||d�r"tj
d|�d���|d|�}d�|D��|d<i}t|pd�����D]N\}}t!|�����}d }|d!zr|d"z
}|d#zs|d$z
}n
|d#zr|d%z
}|r|||<�O|r||d&<|
|d'<|�||d(<|rt%d)�|D����|d*<|r|d+kr||d,<��vg}|	���D]�\} �"| �"d'�"fd-��"dD��d.�}!d(�"vr�"d(|!d(<d&�"vr6t%�"fd/��"d&���D����|!d0<d*�"vr�"d*|!�d1i��d2<d,�"vr�"d,|!�d1i��d3<|�|!����|S)4Nr�r�)r&�z�
              SELECT
                  i.relname as relname,
                  ix.indisunique, ix.indexprs, ix.indpred,
                  a.attname, a.attnum, NULL, ix.indkeyr(z	::varcharr�z,
                  zix.indoption::varchar�NULLr�r%zi.reloptionsam, am.amname,
                  NULL as indnkeyatts
              FROM
                  pg_class t
                        join pg_index ix on t.oid = ix.indrelid
                        join pg_class i on i.oid = ix.indexrelid
                        left outer join
                            pg_attribute a
                            on t.oid = a.attrelid and r�r�aw
                        left outer join
                            pg_am am
                            on i.relam = am.oid
              WHERE
                  t.relkind IN ('r', 'v', 'f', 'm')
                  and t.oid = :table_oid
                  and ix.indisprimary = 'f'
              ORDER BY
                  t.relname,
                  i.relname
            a
              SELECT
                  i.relname as relname,
                  ix.indisunique, ix.indexprs, ix.indpred,
                  a.attname, a.attnum, c.conrelid, ix.indkey::varchar,
                  ix.indoption::varchar, i.reloptions, am.amname,
                  )�rzix.indnkeyattsa� as indnkeyatts
              FROM
                  pg_class t
                        join pg_index ix on t.oid = ix.indrelid
                        join pg_class i on i.oid = ix.indexrelid
                        left outer join
                            pg_attribute a
                            on t.oid = a.attrelid and a.attnum = ANY(ix.indkey)
                        left outer join
                            pg_constraint c
                            on (ix.indrelid = c.conrelid and
                                ix.indexrelid = c.conindid and
                                c.contype in ('p', 'u', 'x'))
                        left outer join
                            pg_am am
                            on i.relam = am.oid
              WHERE
                  t.relkind IN ('r', 'v', 'f', 'm', 'p')
                  and t.oid = :table_oid
                  and ix.indisprimary = 'f'
              ORDER BY
                  t.relname,
                  i.relname
            )r�r�r�c�*�tt��Sr��rr�r�r�r��<lambda>z'PGDialect.get_indexes.<locals>.<lambda>�
���k�$�&7�&7�r�z;Skipped unsupported reflection of expression-based index %sz7Predicate of partial index %s ignored during reflectionrz#INCLUDE columns for covering index z ignored during reflectionc�P�g|]#}t|�������$Sr�)ro�strip)r�rs  r�r�z)PGDialect.get_indexes.<locals>.<listcomp>	s&��A�A�A�1��A�G�G�I�I���A�A�Ar�r�r�r)r9r�)�	nullslast)�
nullsfirst�sortingrh�duplicates_constraintc�8�g|]}|�d����S)�=)r�)r��options  r�r�z)PGDialect.get_indexes.<locals>.<listcomp>&s$��A�A�A�v����c�*�*�A�A�Ar�r��btree�amnamec�,��g|]}�d|��S�rr�)r�r��idxs  �r�r�z)PGDialect.get_indexes.<locals>.<listcomp>5s!��� D� D� D�A��V��Q�� D� D� Dr�)r�rh�column_namesc3�V�K�|]#\}}�d�d||fV��$dS)rr�Nr�)r�r�r�rs   �r�r�z(PGDialect.get_indexes.<locals>.<genexpr>:sQ�����/�/� ��5���[��U��A��/��7�/�/�/�/�/�/r��column_sortingrX�postgresql_with�postgresql_using)r�r/r+r�r
rGr�rr^rrr�rr�r��	enumerateror�r�r�r�r�)#r�r
r�rr�rw�IDX_SQLrr��indexes�sv_idx_namerV�idx_namerhrR�prdr��col_num�conrelid�idx_key�
idx_optionr�r�indnkeyatts�has_idxr^�idx_keysr��col_idx�	col_flags�col_sortingr}r��entryrs#                                  @r��get_indexeszPGDialect.get_indexes~
s.����&�&��
�F�r�v�v�l�7K�7K�'�
�
�	��#�f�,�,�,�: $�7�6�A�A���r�I�I��+�v�5�5�(�'�����+�v�5�5�������"�"�:�{�;�;�;�;�G$�G�G�-�L�+�w�6�6�!� ����=!�G�F
�H�W���%�%��$�h�.>�
&�
�
��
���q�I��6�6���7�7�8�8�����:�:�<�<�U	-�U	-�C��

��������������
��{�*�*��I�4�6>�?����'����
'�8�{�2�2��	�M������'���'�)�G��H�%�E���),��f�
�g�&��1
-�"�=�=�?�?���6�8�K�L�L�#9�6��I�I�7?�x�x�B���� (����5�H�A�A��A�A�A��e��
��*3��%�2�,�,�.�.�+�+�7�7�&�G�Y�!$�I�O�O�$5�$5� 6� 6�I�"$�K� �4�'�;�#�y�0�� )�D� 0�:�'�>�9�K��$�t�+�;�'�?�:�K�"�7�+6���(���/�'.�E�)�$�"(��h���'�5=�E�1�2���'+�A�A��A�A�A�(�(�E�)�$��-�f��/�/�&,�E�(�O���� �����	!�	!�I�D�#���h�-� D� D� D� D��U�� D� D� D���E�
'�#�-�-�14�5L�1M��-�.��C���*.�/�/�/�/�$'�	�N�$8�$8�$:�$:�/�/�/�+�+��&�'��C����	�N�� � �!2�B�7�7�%���3�����M�� � �!2�B�7�7�&��
�M�M�%� � � � ��
r�c���|�||||�d�����}d}tj|���t
j���}|�||���}td���}	|�	��D].}
|	|
j
}|
j|d<|
j|d|
j
<�/d	�|	���D��S)
Nr�r�a�
            SELECT
                cons.conname as name,
                cons.conkey as key,
                a.attnum as col_num,
                a.attname as col_name
            FROM
                pg_catalog.pg_constraint cons
                join pg_attribute a
                  on cons.conrelid = a.attrelid AND
                    a.attnum = ANY(cons.conkey)
            WHERE
                cons.conrelid = :table_oid AND
                cons.contype = 'u'
        )�col_namer�c�*�tt��Sr�r�r�r�r�r�z2PGDialect.get_unique_constraints.<locals>.<lambda>dr�r�r�rc�D��g|]\}�|�fd��dD��d���S)c�,��g|]}�d|��Srr�)r�r��ucs  �r�r�z?PGDialect.get_unique_constraints.<locals>.<listcomp>.<listcomp>ks!���+M�+M�+M�a�B�v�J�q�M�+M�+M�+Mr�r�)r�rr�)r�r�r s  @r�r�z4PGDialect.get_unique_constraints.<locals>.<listcomp>jsN���
�
�
���b��+M�+M�+M�+M�2�e�9�+M�+M�+M�N�N�
�
�
r�)r�r/r
rGr�rr^rrr�r�r�rrr�)r�r
r�rr�rw�
UNIQUE_SQLrr��uniquesrVr s            r��get_unique_constraintsz PGDialect.get_unique_constraintsIs����&�&��
�F�r�v�v�l�7K�7K�'�
�
�	��
� 
�H�Z� � �(�(�(�2B�(�C�C�����q�I��6�6���7�7�8�8���:�:�<�<�	3�	3�C����"�B���B�u�I�&)�l�B�v�J�s�{�#�#�
�
�#�M�M�O�O�
�
�
�	
r�c���|�||||�d�����}d}|�tj|��|���}d|���iS)Nr�r�z�
            SELECT
                pgd.description as table_comment
            FROM
                pg_catalog.pg_description pgd
            WHERE
                pgd.objsubid = 0 AND
                pgd.objoid = :table_oid
        r�rG)r�r/rr
rGr/)r�r
r�rr�rw�COMMENT_SQLr�s        r��get_table_commentzPGDialect.get_table_commentoso���&�&��
�F�r�v�v�l�7K�7K�'�
�
�	���
���s�x��4�4�	��J�J������
�
�#�#r�c�@�|�||||�d�����}d}|�tj|��|���}g}|D]�\}	}
tjd|
t
j���}|stj	d|
z��d}nGtj
d	t
j����d
|�d����}|	|d�}
|r|�d
��rddi|
d<|�
|
����|S)Nr�r�a
            SELECT
                cons.conname as name,
                pg_get_constraintdef(cons.oid) as src
            FROM
                pg_catalog.pg_constraint cons
            WHERE
                cons.conrelid = :table_oid AND
                cons.contype = 'c'
        r�z^CHECK *\((.+)\)( NOT VALID)?$)�flagsz)Could not parse CHECK constraint text: %rr�z^[\s\n]*\((.+)\)[\s\n]*$z\1r)r�r�r��	not_validTrX)r�r/rr
rGr�r�DOTALLrr�r�r�rEr�)r�r
r�rr�rw�	CHECK_SQLr��retr��srcrrr�rs              r��get_check_constraintszPGDialect.get_check_constraints�sG���&�&��
�F�r�v�v�l�7K�7K�'�
�
�	�	�	�
���s�x�	�2�2�i��H�H�����	�	�I�D�#���1�3�b�i����A��
)��	�E��K�L�L�L�����*�/�r�y�����#�e�Q�W�W�Q�Z�Z�(�(��"�g�6�6�E��
?�Q�W�W�Q�Z�Z�
?�,7��+>��'�(��J�J�u������
r�c��|p|j}|jsiSd}|dkr|dz
}|dz
}tj|���t
jt
j���}|dkr|�|���}|�|��}g}i}|�	��D]�}|d|df}	|	|vr(||	d	�
|d
���>|d|d|dgd�x||	<}
|d
�!|
d	�
|d
��|�
|
����|S)
Na�
            SELECT t.typname as "name",
               -- no enum defaults in 8.4 at least
               -- t.typdefault as "default",
               pg_catalog.pg_type_is_visible(t.oid) as "visible",
               n.nspname as "schema",
               e.enumlabel as "label"
            FROM pg_catalog.pg_type t
                 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
                 LEFT JOIN pg_catalog.pg_enum e ON t.oid = e.enumtypid
            WHERE t.typtype = 'e'
        r�zAND n.nspname = :schema z ORDER BY "schema", "name", e.oid)r��labelrrr�r�r0r�)r�rr�r�)r�r�r
rGr�rr^r\rr�r�)r�r
r�	SQL_ENUMSr�r�r��enum_by_namerr��enum_recs           r�r�zPGDialect._load_enums�s����3�4�3���(�	��I��	��S�=�=��3�3�I�	�7�7�	��H�Y���'�'��$�H�,<�
(�
�
���S�=�=����F��+�+�A����q�!�!�������J�J�L�L�
	'�
	'�D���>�4��<�0�C��l�"�"��S�!�(�+�2�2�4��=�A�A�A�A�!��L�"�8�n�#�I�� �	0�0���S�!�H���=�,��X�&�-�-�d�7�m�<�<�<����X�&�&�&�&��r�c��d}tj|���tj���}|�|��}i}|���D]g}tjd|d���	d��}|dr
|df}n|d|df}||d	|d
d�||<�h|S)Na�
            SELECT t.typname as "name",
               pg_catalog.format_type(t.typbasetype, t.typtypmod) as "attype",
               not t.typnotnull as "nullable",
               t.typdefault as "default",
               pg_catalog.pg_type_is_visible(t.oid) as "visible",
               n.nspname as "schema"
            FROM pg_catalog.pg_type t
               LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
            WHERE t.typtype = 'd'
        r�z([^\(]+)r�rr�r�rr.r)r�r.r)
r
rGr�rr^rr�r�r�rE)	r�r
�SQL_DOMAINSr�r�r�r�r�r�s	         r�r�zPGDialect._load_domains�s���
��
�H�[�!�!�)�)�(�2B�)�C�C�����q�!�!�����j�j�l�l�	�	�F��Y�{�F�8�,<�=�=�C�C�A�F�F�F�
�i� �
9��f�~�'����h�'����8��!�"�:�.�!�)�,���G�C�L�L��r�)NNN)TFr�r�r�)Pr�r�r�r��supports_alter�max_identifier_length�supports_sane_rowcountr��supports_native_booleanr'�supports_sequences�sequences_optional�"preexecute_autoincrement_sequences�postfetch_lastrowid�supports_comments�supports_default_values�supports_empty_insert�supports_multivalues_insert�default_paramstyler�r-r`�statement_compilerr�ddl_compilerr~r�r�r�r�execution_ctx_clsr��	inspectorrr�Index�Table�construct_arguments�reflection_optionsr�rWrer�r*r4r%r8r2r@rErGrOrRrXrZrbrerhrrsr�cacher�r~r�r�r�r�r�r�r�r�r�rr#r&r.r�r�r�r�s@r�rr
s���������D��N���!����"��������)-�&�����"��!��"&��#��!�M��H�#�� �L�"�M�#�H�*���I��O�
�L���� %��"�

�
�
	
�
�L�&+�"� $�!�!� �

�
�
	
���2<����*.�'�(,�%����	0�0�0�0�  
� 
� 
� 
� 
�D�����	
�	
�	
������� ���-�-�-�=�=�=�:?�4�4�4�4�":?�
2�
2�
2�
2�-�-�-�<�<�<�$�$�$� $$�$$�$$�$$�L$$�$$�$$�$$�L %� %� %� %�DJ�J�J���%�%�%���%�N��*�*���*���	*�	*�	*���	*���	*�	*�	*���	*���/H�*�*�*���*�:����������N�N�N���N�`b�b�b�H��0;�0;�0;���0;�d��
�&+�k�k�k���k�Z6�6�6���H�H���H�T��-1�#
�#
�#
���#
�J��$�$�$���$�$��*�*�*���*�X2�2�2�2�h$�$�$�$�$�$�$r�r)qr��collectionsr�datetimer�r�r�rr;r�_hstorer�_jsonr�_rangesr
rr
r�enginerrrrrrr��sql.ddlr�typesrrrrrrrrrrr rMr!r��ImportErrorr�r�r[�UNICODErr%r��_DECIMAL_TYPES�_FLOAT_TYPES�
_INT_TYPES�LargeBinaryr��Floatr��
TypeEnginer��PGInetr��PGCidrr��	PGMacAddrr�r�r�r�r��NativeForEmulated�_AbstractIntervalr��
PGIntervalr��PGBit�PGUuidr�r6r�r4r�rp�JSONPathTyper-r�r�r�r�r�r�r�r��Stringr��SQLCompilerr`�DDLCompilerr�GenericTypeCompilerr~�IdentifierPreparerr�r�r��_CreateDropBaserr �DefaultExecutionContextrrrr�r�r��<module>rls�	��h�h�R$�#�#�#�#�#�����	�	�	�	������������������������������������������������������� � � � � � �������������������������#�#�#�#�#�#��������������������������������������������������������������������������)�)�)�)�)�)�)�������L�L�L�����
�B�J�:�B�D�A�A�	��B�J�@��D�2�:�������g�g�g�i�i��V��%��
/�
������H� ����(�(�(�(�(�x�~�(�(�(������8�����
�������8�����
�������h�!����
�	�*�*�*�*�*�H��*�*�*�Z�����(�
���� � � � � �x�"� � � �#�#�#�#�#��"�#�#�#�#�#�#�#�#�8�=�#�#�#�$�$�$�$�$�x�)�8�+E�$�$�$�N�
�
�
�
�
�
�(�
�
�
�
�	��5�5�5�5�5�8��5�5�5�p
�� � � � � �x�"� � � �&A8�A8�A8�A8�A8�8�%�x�}�A8�A8�A8�J
�N�F�L���x��M�4��M��� 2��M�5�:���+��f�l�+��g�n�+��E�J�+��U�[�	+�
��"�+���"�
+��� �+���"�+��w��+���"�+��w�+�
�f�+���+���+���+� 
�h�o�!+�"�H�O�#+�+�$�D�%+�&�w�'+�(�U�)+�*�D�++�,�D�-+�.�D�/+�0�D�1+�2
�3�3+�4�3�5+�6�w�7+�8�U�9+�:
�3�;+�<��=+�>�(�?+�@��A+�B�	�C+�D"�9�E+�+�F �"���
����U+�+�+�
�\u
�u
�u
�u
�u
��%�u
�u
�u
�p	r
�r
�r
�r
�r
�H�(�r
�r
�r
�jE
�E
�E
�E
�E
�X�1�E
�E
�E
�P�����8�6����6>
�>
�>
�>
�>
�*�&�>
�>
�>
�B(�(�(�(�(�V�+�(�(�(�&�&�&�&�&�6�)�&�&�&�92�92�92�92�92��8�92�92�92�xt�t�t�t�t��&�t�t�t�t�ts�4B;�;C�C

Hacked By AnonymousFox1.0, Coded By AnonymousFox