3
๖aฮ  ใ               @   s^   d dl mZ G dd dejZG dd deZG dd deejjZG dd	 d	eejjZd
S )้   )ฺtypesc               @   s   e Zd ZdZdS )ฺJSONaล	  MSSQL JSON type.

    MSSQL supports JSON-formatted data as of SQL Server 2016.

    The :class:`_mssql.JSON` datatype at the DDL level will represent the
    datatype as ``NVARCHAR(max)``, but provides for JSON-level comparison
    functions as well as Python coercion behavior.

    :class:`_mssql.JSON` is used automatically whenever the base
    :class:`_types.JSON` datatype is used against a SQL Server backend.

    .. seealso::

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

    The :class:`_mssql.JSON` type supports persistence of JSON values
    as well as the core index operations provided by :class:`_types.JSON`
    datatype, by adapting the operations to render the ``JSON_VALUE``
    or ``JSON_QUERY`` functions at the database level.

    The SQL Server :class:`_mssql.JSON` type necessarily makes use of the
    ``JSON_QUERY`` and ``JSON_VALUE`` functions when querying for elements
    of a JSON object.   These two functions have a major restriction in that
    they are **mutually exclusive** based on the type of object to be returned.
    The ``JSON_QUERY`` function **only** returns a JSON dictionary or list,
    but not an individual string, numeric, or boolean element; the
    ``JSON_VALUE`` function **only** returns an individual string, numeric,
    or boolean element.   **both functions either return NULL or raise
    an error if they are not used against the correct expected value**.

    To handle this awkward requirement, indexed access rules are as follows:

    1. When extracting a sub element from a JSON that is itself a JSON
       dictionary or list, the :meth:`_types.JSON.Comparator.as_json` accessor
       should be used::

            stmt = select(
                data_table.c.data["some key"].as_json()
            ).where(
                data_table.c.data["some key"].as_json() == {"sub": "structure"}
            )

    2. When extracting a sub element from a JSON that is a plain boolean,
       string, integer, or float, use the appropriate method among
       :meth:`_types.JSON.Comparator.as_boolean`,
       :meth:`_types.JSON.Comparator.as_string`,
       :meth:`_types.JSON.Comparator.as_integer`,
       :meth:`_types.JSON.Comparator.as_float`::

            stmt = select(
                data_table.c.data["some key"].as_string()
            ).where(
                data_table.c.data["some key"].as_string() == "some string"
            )

    .. versionadded:: 1.4


    N)ฺ__name__ฺ
__module__ฺ__qualname__ฺ__doc__ฉ r   r   ๚D/tmp/pip-build-6_cqtusv/SQLAlchemy/sqlalchemy/dialects/mssql/json.pyr      s   <r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )ฺ_FormatTypeMixinc             C   s
   t  d S )N)ฺNotImplementedError)ฺselfฺvaluer   r   r	   ฺ_format_valueQ   s    z_FormatTypeMixin._format_valuec                s    j | fdd}|S )Nc                s    j | } r| } | S )N)r   )r   )r   ฺ
super_procr   r	   ฺprocessW   s    
z0_FormatTypeMixin.bind_processor.<locals>.process)Zstring_bind_processor)r   ฺdialectr   r   )r   r   r	   ฺbind_processorT   s    
z_FormatTypeMixin.bind_processorc                s    j | fdd}|S )Nc                s    j | } r| } | S )N)r   )r   )r   r   r   r	   r   b   s    
z3_FormatTypeMixin.literal_processor.<locals>.process)Zstring_literal_processor)r   r   r   r   )r   r   r	   ฺliteral_processor_   s    
z"_FormatTypeMixin.literal_processorN)r   r   r   r   r   r   r   r   r   r	   r
   P   s   r
   c               @   s   e Zd Zdd ZdS )ฺJSONIndexTypec             C   s    t |trd| }nd| }|S )Nz$[%s]z$."%s")ฺ
isinstanceฺint)r   r   r   r   r	   r   l   s    

zJSONIndexType._format_valueN)r   r   r   r   r   r   r   r	   r   k   s   r   c               @   s   e Zd Zdd ZdS )ฺJSONPathTypec             C   s   ddj dd |D  S )Nz$%sฺ c             S   s&   g | ]}t |trd | nd| qS )z[%s]z."%s")r   r   )ฺ.0ฺelemr   r   r	   ๚
<listcomp>y   s   z.JSONPathType._format_value.<locals>.<listcomp>)ฺjoin)r   r   r   r   r	   r   u   s    zJSONPathType._format_valueN)r   r   r   r   r   r   r   r	   r   t   s   r   N)r   r   Zsqltypesr   ฺobjectr
   r   r   r   r   r   r	   ฺ<module>   s   
E	