o
    y¬´\Ò&  ã                   @   s6  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	Ze
je
j Ze
jd
eddd�Ze
jdeddd�Ze
jdeddd�Ze
jdeddd�Ze
jdeddd�Ze
jdeddd�Ze
jdeddd�ZG dd„ dejjƒZeeejj< G dd„ dejƒZeeej< eed< G dd„ deƒZeed< dS )é    )Úabsolute_importé   )Úcolspecs)Úischema_namesé   )Útypes)Úutil)Ú	operators)ÚJSONÚJSONBz->>T)Ú
precedenceÚnatural_self_precedentÚeager_groupingz#>>ú?z?&z?|z@>z<@c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚJSONPathTypec                    ó   |   |¡‰ ‡ fdd„}|S )Nc                    ó>   t | tjjƒs	J ‚dd„ | D ƒ}dd |¡ } ˆ rˆ | ƒ} | S )Nc                 S   ó   g | ]}t  |¡‘qS © ©r   Ú	text_type©Ú.0Úelemr   r   úU/var/www/html/env/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/json.pyÚ
<listcomp>M   ó    z@JSONPathType.bind_processor.<locals>.process.<locals>.<listcomp>ú{%s}ú, ©Ú
isinstancer   Úcollections_abcÚSequenceÚjoin©ÚvalueÚtokens©Ú
super_procr   r   ÚprocessK   ó   z,JSONPathType.bind_processor.<locals>.process)Ústring_bind_processor©ÚselfÚdialectr)   r   r'   r   Úbind_processorH   ó   
zJSONPathType.bind_processorc                    r   )Nc                    r   )Nc                 S   r   r   r   r   r   r   r   r   Z   r   zCJSONPathType.literal_processor.<locals>.process.<locals>.<listcomp>r   r   r   r$   r'   r   r   r)   X   r*   z/JSONPathType.literal_processor.<locals>.process)Ústring_literal_processorr,   r   r'   r   Úliteral_processorU   r0   zJSONPathType.literal_processorN)Ú__name__Ú
__module__Ú__qualname__r/   r2   r   r   r   r   r   G   s    r   c                       sB   e Zd ZdZe ¡ Zd‡ fdd„	ZG dd„ dejj	ƒZ	e	Z
‡  ZS )	r
   a 	  Represent the PostgreSQL JSON type.

    This type is a specialization of the Core-level :class:`.types.JSON`
    type.   Be sure to read the documentation for :class:`.types.JSON` for
    important tips regarding treatment of NULL values and ORM use.

    .. versionchanged:: 1.1 :class:`.postgresql.JSON` is now a PostgreSQL-
       specific specialization of the new :class:`.types.JSON` type.

    The operators provided by the PostgreSQL version of :class:`.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'

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

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

    * 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'

    .. versionchanged:: 1.1  The :meth:`.ColumnElement.cast` operator on
       JSON objects now requires that the :attr:`.JSON.Comparator.astext`
       modifier be called explicitly, if the cast works only from a textual
       string.

    Index operations return an expression object whose type defaults to
    :class:`.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:`.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://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:`.JSONB`

    FNc                    s(   t t| ƒj|d� |dur|| _dS dS )a   Construct a :class:`.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())

         .. versionchanged:: 0.9.8 - Added ``none_as_null``, and :func:`.null`
            is now supported in order to persist a NULL value.

         .. seealso::

              :attr:`.JSON.NULL`

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

         .. versionadded:: 1.1

         )Únone_as_nullN)Úsuperr
   Ú__init__Úastext_type)r-   r6   r9   ©Ú	__class__r   r   r8   «   s   
ÿzJSON.__init__c                   @   s   e Zd ZdZedd„ ƒZdS )zJSON.Comparatorú0Define comparison operations for :class:`.JSON`.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:`.ColumnElement.cast`

            ©Úresult_type)r    ÚexprÚrightÚtypeÚsqltypesr
   r   ÚleftÚoperateÚJSONPATH_ASTEXTr9   ÚASTEXT)r-   r   r   r   ÚastextË   s   ýÿzJSON.Comparator.astextN)r3   r4   r5   Ú__doc__ÚpropertyrG   r   r   r   r   Ú
ComparatorÈ   s    rJ   )FN)r3   r4   r5   rH   rB   ÚTextr9   r8   r
   rJ   Úcomparator_factoryÚ__classcell__r   r   r:   r   r
   f   s    Br
   Újsonc                   @   s*   e Zd ZdZd ZG dd„ dejƒZeZdS )r   a_  Represent the PostgreSQL JSONB type.

    The :class:`.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:`.JSONB` type includes all operations provided by
    :class:`.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`,
    and :meth:`.JSONB.Comparator.contained_by`.

    Like the :class:`.JSON` type, the :class:`.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:`.JSON` class, using the ``json_serializer``
    and ``json_deserializer`` keyword arguments.  These must be specified
    at the dialect level using :func:`.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.

    .. versionadded:: 0.9.7

    .. seealso::

        :class:`.JSON`

    c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )zJSONB.Comparatorr<   c                 C   ó   | j t|tjd�S )zvBoolean expression.  Test for presence of a key.  Note that the
            key may be a SQLA expression.
            r=   )rD   ÚHAS_KEYrB   ÚBoolean©r-   Úotherr   r   r   Úhas_key  ó   zJSONB.Comparator.has_keyc                 C   rO   )zHBoolean expression.  Test for presence of all keys in jsonb
            r=   )rD   ÚHAS_ALLrB   rQ   rR   r   r   r   Úhas_all#  ó   zJSONB.Comparator.has_allc                 C   rO   )zGBoolean expression.  Test for presence of any key in jsonb
            r=   )rD   ÚHAS_ANYrB   rQ   rR   r   r   r   Úhas_any(  rX   zJSONB.Comparator.has_anyc                 K   rO   )zŒBoolean expression.  Test if keys (or array) are a superset
            of/contained the keys of the argument jsonb expression.
            r=   )rD   ÚCONTAINSrB   rQ   )r-   rS   Úkwargsr   r   r   Úcontains-  rU   zJSONB.Comparator.containsc                 C   rO   )z|Boolean expression.  Test if keys are a proper subset of the
            keys of the argument jsonb expression.
            r=   )rD   ÚCONTAINED_BYrB   rQ   rR   r   r   r   Úcontained_by3  s   ÿzJSONB.Comparator.contained_byN)	r3   r4   r5   rH   rT   rW   rZ   r]   r_   r   r   r   r   rJ     s    rJ   N)r3   r4   r5   rH   Ú__visit_name__r
   rJ   rL   r   r   r   r   r   ì   s
    +!r   ÚjsonbN)Ú
__future__r   Úbaser   r   Ú r   rB   r   Úsqlr	   Ú__all__Ú_PRECEDENCEÚjson_getitem_opÚidx_precedenceÚ	custom_oprF   rE   rP   rV   rY   r[   r^   r
   r   r   r   r   r   r   Ú<module>   st   üüüüüüü 
R