o
    ŠågK2  ã                   @  s–  d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ dd	l	m
Z
 dd
l	m	Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ er¤ddlmZ ddl m!Z! ddl"m#Z# ddl"m$Z$ ddl"m%Z% dZ&G dd „ d ej'j(ƒZ(G d!d"„ d"e(ƒZ)G d#d$„ d$ej'ƒZ'G d%d&„ d&e'ƒZ*d'S )(é    )Úannotations)ÚAny)ÚCallable)ÚList)ÚOptional)ÚTYPE_CHECKING)ÚUnioné   )ÚARRAY)Úarray)ÚASTEXT)ÚCONTAINED_BY)ÚCONTAINS)ÚDELETE_PATH)ÚHAS_ALL)ÚHAS_ANY)ÚHAS_KEY)ÚJSONPATH_ASTEXT)ÚPATH_EXISTS)Ú
PATH_MATCHé   )Útypes)Úcast)Ú_T)ÚDialect)ÚColumnElement)Ú_BindProcessorType)Ú_LiteralProcessorType)Ú
TypeEngine)ÚJSONÚJSONBc                   @  s*   e Zd Zddd„Zdd
d„Zddd„ZdS )ÚJSONPathTypeÚdialectr   Ú
super_procúOptional[Callable[[Any], Any]]ÚreturnúCallable[[Any], Any]c                   s   d‡ fdd„}|S )NÚvaluer   r%   c                   s<   t | tƒr| S | rdd tt| ƒ¡ } nd} ˆ rˆ | ƒ} | S )Nz{%s}z, z{})Ú
isinstanceÚstrÚjoinÚmap)r'   ©r#   © ú_/var/www/html/flaskapp/venv/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/json.pyÚprocess/   s   
z(JSONPathType._processor.<locals>.process)r'   r   r%   r   r-   )Úselfr"   r#   r/   r-   r,   r.   Ú
_processor,   s   zJSONPathType._processorú_BindProcessorType[Any]c                 C  ó   |   ||  |¡¡S ©N)r1   Ústring_bind_processor©r0   r"   r-   r-   r.   Úbind_processor@   s   zJSONPathType.bind_processorú_LiteralProcessorType[Any]c                 C  r3   r4   )r1   Ústring_literal_processorr6   r-   r-   r.   Úliteral_processorC   s   zJSONPathType.literal_processorN)r"   r   r#   r$   r%   r&   )r"   r   r%   r2   )r"   r   r%   r8   )Ú__name__Ú
__module__Ú__qualname__r1   r7   r:   r-   r-   r-   r.   r!   +   s    

r!   c                   @  s   e Zd ZdZd ZdS )ÚJSONPATHad  JSON Path Type.

    This is usually required to cast literal values to json path when using
    json search like function, such as ``jsonb_path_query_array`` or
    ``jsonb_path_exists``::

        stmt = sa.select(
            sa.func.jsonb_path_query_array(
                table.c.jsonb_col, cast("$.address.id", JSONPATH)
            )
        )

    N)r;   r<   r=   Ú__doc__Ú__visit_name__r-   r-   r-   r.   r>   I   s    r>   c                      sZ   e Zd ZU dZdZe ¡ Zded< 		dd‡ fd
d„Z	G dd„ dej
je ƒZeZ‡  ZS )r   a

  Represent the PostgreSQL JSON type.

    :class:`_postgresql.JSON` is used automatically whenever the base
    :class:`_types.JSON` datatype is used against a PostgreSQL backend,
    however base :class:`_types.JSON` datatype does not provide Python
    accessors for PostgreSQL-specific comparison methods such as
    :meth:`_postgresql.JSON.Comparator.astext`; additionally, to use
    PostgreSQL ``JSONB``, the :class:`_postgresql.JSONB` datatype should
    be used explicitly.

    .. seealso::

        :class:`_types.JSON` - main documentation for the generic
        cross-platform JSON datatype.

    The operators provided by the PostgreSQL version of :class:`_types.JSON`
    include:

    * Index operations (the ``->`` operator)::

        data_table.c.data["some key"]

        data_table.c.data[5]

    * Index operations returning text
      (the ``->>`` operator)::

        data_table.c.data["some key"].astext == "some value"

      Note that equivalent functionality is available via the
      :attr:`.JSON.Comparator.as_string` accessor.

    * Index operations with CAST
      (equivalent to ``CAST(col ->> ['some key'] AS <type>)``)::

        data_table.c.data["some key"].astext.cast(Integer) == 5

      Note that equivalent functionality is available via the
      :attr:`.JSON.Comparator.as_integer` and similar accessors.

    * Path index operations (the ``#>`` operator)::

        data_table.c.data[("key_1", "key_2", 5, ..., "key_n")]

    * Path index operations returning text (the ``#>>`` operator)::

        data_table.c.data[
            ("key_1", "key_2", 5, ..., "key_n")
        ].astext == "some value"

    Index operations return an expression object whose type defaults to
    :class:`_types.JSON` by default,
    so that further JSON-oriented instructions
    may be called upon the result type.

    Custom serializers and deserializers are specified at the dialect level,
    that is using :func:`_sa.create_engine`.  The reason for this is that when
    using psycopg2, the DBAPI only allows serializers at the per-cursor
    or per-connection level.   E.g.::

        engine = create_engine(
            "postgresql+psycopg2://scott:tiger@localhost/test",
            json_serializer=my_serialize_fn,
            json_deserializer=my_deserialize_fn,
        )

    When using the psycopg2 dialect, the json_deserializer is registered
    against the database using ``psycopg2.extras.register_default_json``.

    .. seealso::

        :class:`_types.JSON` - Core level JSON type

        :class:`_postgresql.JSONB`

    TzTypeEngine[str]Úastext_typeFNÚnone_as_nullÚboolúOptional[TypeEngine[str]]c                   s$   t ƒ j|d� |dur|| _dS dS )a‚  Construct a :class:`_types.JSON` type.

        :param none_as_null: if True, persist the value ``None`` as a
         SQL NULL value, not the JSON encoding of ``null``.   Note that
         when this flag is False, the :func:`.null` construct can still
         be used to persist a NULL value::

             from sqlalchemy import null

             conn.execute(table.insert(), {"data": null()})

         .. seealso::

              :attr:`_types.JSON.NULL`

        :param astext_type: the type to use for the
         :attr:`.JSON.Comparator.astext`
         accessor on indexed attributes.  Defaults to :class:`_types.Text`.

        )rB   N)ÚsuperÚ__init__rA   )r0   rB   rA   ©Ú	__class__r-   r.   rF   ¬   s   
ÿzJSON.__init__c                   @  s(   e Zd ZU dZded< ed	dd„ƒZdS )
zJSON.Comparatorú6Define comparison operations for :class:`_types.JSON`.r   Útyper%   úColumnElement[str]c                 C  sL   t | jjjtjjƒr| jjjt	| jj| jj
d�S | jjjt| jj| jj
d�S )a  On an indexed expression, use the "astext" (e.g. "->>")
            conversion when rendered in SQL.

            E.g.::

                select(data_table.c.data["some key"].astext)

            .. seealso::

                :meth:`_expression.ColumnElement.cast`

            ©Úresult_type)r(   ÚexprÚrightrJ   Úsqltypesr   r!   ÚleftÚoperater   rA   r   )r0   r-   r-   r.   ÚastextÎ   s   ýÿzJSON.Comparator.astextN)r%   rK   )r;   r<   r=   r?   Ú__annotations__ÚpropertyrS   r-   r-   r-   r.   Ú
ComparatorÉ   s
   
 rV   )FN)rB   rC   rA   rD   )r;   r<   r=   r?   Úrender_bind_castrP   ÚTextrA   rT   rF   r   rV   r   Úcomparator_factoryÚ__classcell__r-   r-   rG   r.   r   [   s   
 Mýr   c                   @  s.   e Zd ZdZd ZG dd„ deje ƒZeZdS )r    a  Represent the PostgreSQL JSONB type.

    The :class:`_postgresql.JSONB` type stores arbitrary JSONB format data,
    e.g.::

        data_table = Table(
            "data_table",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", JSONB),
        )

        with engine.connect() as conn:
            conn.execute(
                data_table.insert(), data={"key1": "value1", "key2": "value2"}
            )

    The :class:`_postgresql.JSONB` type includes all operations provided by
    :class:`_types.JSON`, including the same behaviors for indexing
    operations.
    It also adds additional operators specific to JSONB, including
    :meth:`.JSONB.Comparator.has_key`, :meth:`.JSONB.Comparator.has_all`,
    :meth:`.JSONB.Comparator.has_any`, :meth:`.JSONB.Comparator.contains`,
    :meth:`.JSONB.Comparator.contained_by`,
    :meth:`.JSONB.Comparator.delete_path`,
    :meth:`.JSONB.Comparator.path_exists` and
    :meth:`.JSONB.Comparator.path_match`.

    Like the :class:`_types.JSON` type, the :class:`_postgresql.JSONB`
    type does not detect
    in-place changes when used with the ORM, unless the
    :mod:`sqlalchemy.ext.mutable` extension is used.

    Custom serializers and deserializers
    are shared with the :class:`_types.JSON` class,
    using the ``json_serializer``
    and ``json_deserializer`` keyword arguments.  These must be specified
    at the dialect level using :func:`_sa.create_engine`.  When using
    psycopg2, the serializers are associated with the jsonb type using
    ``psycopg2.extras.register_default_jsonb`` on a per-connection basis,
    in the same way that ``psycopg2.extras.register_default_json`` is used
    to register these handlers with the json type.

    .. seealso::

        :class:`_types.JSON`

    c                   @  sj   e Zd ZU dZded< ddd	„Zdd
d„Zddd„Zddd„Zddd„Z	ddd„Z
ddd„Zddd„ZdS ) zJSONB.ComparatorrI   r    rJ   Úotherr   r%   úColumnElement[bool]c                 C  ó   | j t|tjd�S )z™Boolean expression.  Test for presence of a key (equivalent of
            the ``?`` operator).  Note that the key may be a SQLA expression.
            rL   )rR   r   rP   ÚBoolean©r0   r[   r-   r-   r.   Úhas_key#  ó   zJSONB.Comparator.has_keyc                 C  r]   )zxBoolean expression.  Test for presence of all keys in jsonb
            (equivalent of the ``?&`` operator)
            rL   )rR   r   rP   r^   r_   r-   r-   r.   Úhas_all)  ra   zJSONB.Comparator.has_allc                 C  r]   )zwBoolean expression.  Test for presence of any key in jsonb
            (equivalent of the ``?|`` operator)
            rL   )rR   r   rP   r^   r_   r-   r-   r.   Úhas_any/  ra   zJSONB.Comparator.has_anyÚkwargsc                 K  r]   )a"  Boolean expression.  Test if keys (or array) are a superset
            of/contained the keys of the argument jsonb expression
            (equivalent of the ``@>`` operator).

            kwargs may be ignored by this operator but are required for API
            conformance.
            rL   )rR   r   rP   r^   )r0   r[   rd   r-   r-   r.   Úcontains5  s   zJSONB.Comparator.containsc                 C  r]   )z¬Boolean expression.  Test if keys are a proper subset of the
            keys of the argument jsonb expression
            (equivalent of the ``<@`` operator).
            rL   )rR   r   rP   r^   r_   r-   r-   r.   Úcontained_by?  s   ÿzJSONB.Comparator.contained_byr   ú Union[List[str], _pg_array[str]]úColumnElement[JSONB]c                 C  s2   t |tƒs	t|ƒ}t|ttjƒƒ}| jt|td�S )a=  JSONB expression. Deletes field or array element specified in
            the argument array (equivalent of the ``#-`` operator).

            The input may be a list of strings that will be coerced to an
            ``ARRAY`` or an instance of :meth:`_postgres.array`.

            .. versionadded:: 2.0
            rL   )	r(   Ú	_pg_arrayr   r
   rP   ÚTEXTrR   r   r    )r0   r   Ú
right_sider-   r-   r.   Údelete_pathH  s   
zJSONB.Comparator.delete_pathc                 C  r]   )z¸Boolean expression. Test for presence of item given by the
            argument JSONPath expression (equivalent of the ``@?`` operator).

            .. versionadded:: 2.0
            rL   )rR   r   rP   r^   r_   r-   r-   r.   Úpath_existsX  s   ÿzJSONB.Comparator.path_existsc                 C  r]   )a  Boolean expression. Test if JSONPath predicate given by the
            argument JSONPath expression matches
            (equivalent of the ``@@`` operator).

            Only the first item of the result is taken into account.

            .. versionadded:: 2.0
            rL   )rR   r   rP   r^   r_   r-   r-   r.   Ú
path_matchb  s   	ÿzJSONB.Comparator.path_matchN)r[   r   r%   r\   )r[   r   rd   r   r%   r\   )r   rg   r%   rh   )r;   r<   r=   r?   rT   r`   rb   rc   re   rf   rl   rm   rn   r-   r-   r-   r.   rV     s   
 






	

rV   N)	r;   r<   r=   r?   r@   r   rV   r   rY   r-   r-   r-   r.   r    ê   s
    1Qr    N)+Ú
__future__r   Útypingr   r   r   r   r   r   r   r
   ri   Ú	operatorsr   r   r   r   r   r   r   r   r   r   Ú r   rP   Úsqlr   Úsql._typingr   Úengine.interfacesr   Úsql.elementsr   Úsql.type_apir   r   r   Ú__all__r   r!   r>   r    r-   r-   r-   r.   Ú<module>   sD    