
    "iY                     d   d Z ddlZddlZddlZddlmZ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Z G d de          Zd Zd2dZd2dZd2dZd2dZ G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z  G d  d!e          Z! G d" d#e          Z" G d$ d%e"          Z# G d& d'e"          Z$ G d( d)e          Z% G d* d+e          Z& G d, d-e          Z' G d. d/e          Z( G d0 d1e          Z)dS )3zImplementation of Statements.    N   )ProgrammingErrorNotSupportedError)
ExprParser)LockContention)DbDoc)
deprecated)Result)mysqlxpb_enumz&The given index name "{}" is not validc                       e Zd ZdZd ZdS )ExprzExpression wrapper.c                     || _         d S N)expr)selfr   s     H/srv/django_bis/venv311/lib/python3.11/site-packages/mysqlx/statement.py__init__zExpr.__init__0   s    			    N)__name__
__module____qualname____doc__r    r   r   r   r   .   s)            r   r   c                      t          |           dk    r*t          | d         t          t          f          r| d         S | S )zParse flexible parameters.r   r   )len
isinstancelisttuple)valuess    r   flexible_paramsr    4   s9    
6{{aJvay4.AAayMr    c                     d|v r0| d         dk    r| d         dk    p| d         dk    o| d         dk    S | d         dk    o| d         dk    S )zCheck if the given identifier is quoted.

    Args:
        identifier (string): Identifier to check.
        sql_mode (Optional[string]): SQL mode.

    Returns:
        `True` if the identifier has backtick quotes, and False otherwise.
    ANSI_QUOTESr   `"r   
identifiersql_modes     r   is_quoted_identifierr*   ;   sl       A#%?*R.C*? AA#%?*R.C*?	Ba=C9JrNc$99r   c                     t          |           dk    rdS d|v r)d                    |                     dd                    S d                    |                     dd                    S )	a@  Quote the given identifier with backticks, converting backticks (`) in
    the identifier name with the correct escape sequence (``).

    Args:
        identifier (string): Identifier to quote.
        sql_mode (Optional[string]): SQL mode.

    Returns:
        A string with the identifier quoted with backticks.
    r   z``r#   z"{0}"r&   z""z`{0}`r$   )r   formatreplacer'   s     r   quote_identifierr.   K   si     :!t  ~~j00d;;<<<>>*,,S$77888r   c                 F    d                     fd| D                       S )a  Quote the given multi-part identifier with backticks.

    Args:
        identifiers (iterable): List of identifiers to quote.
        sql_mode (Optional[string]): SQL mode.

    Returns:
        A string with the multi-part identifier quoted with backticks.
    .c                 0    g | ]}t          |          S r   )r.   ).0r(   r)   s     r   
<listcomp>z.quote_multipart_identifier.<locals>.<listcomp>g   s3     4 4 4# &j(;; 4 4 4r   )join)identifiersr)   s    `r   quote_multipart_identifierr6   ]   sA     88 4 4 4 4'24 4 4 5 5 5r   c                 
   d|v rdnd}||v rd                     |          nd}|                    |d          }t          |          dk    r| n|d                             |          |d                             |          fS )	zParse table name.

    Args:
        default_schema (str): The default schema.
        table_name (str): The table name.
        sql_mode(Optional[str]): The SQL mode.

    Returns:
        str: The parsed table name.
    r#   r&   r$   z.{0}r0   r   r   r%   )r,   splitr   strip)default_schema
table_namer)   quote	delimitertemps         r   parse_table_namer?   k   s     !H,,CC#E(-(;(;e$$$IIq))D!$ii1nnNN$q'--2F2FHNN5!!$ $r   c                   b   e Zd ZdZddZed             Zed             Zed             Zej	        d             Zed             Z
ed	             Zej	        d
             Zed             Zej	        d             Zed             Zed             Zej	        d             Zd Zd Zd Zd ZdS )	Statementa	  Provides base functionality for statement objects.

    Args:
        target (object): The target database object, it can be
                         :class:`mysqlx.Collection` or :class:`mysqlx.Table`.
        doc_based (bool): `True` if it is document based.
    Tc                     || _         || _        |r|                                nd | _        d | _        d| _        d| _        d| _        d| _        d S )Nr   TF)	_target
_doc_basedget_connection_connection_stmt_id_exec_counter_changed	_prepared_deallocate_prepare_execute)r   target	doc_baseds      r   r   zStatement.__init__   sY    #6<F600222$+0(((r   c                     | j         S )z#object: The database object target.)rC   r   s    r   rL   zStatement.target   s     |r   c                     | j         j        S )z*:class:`mysqlx.Schema`: The Schema object.)rC   schemarO   s    r   rQ   zStatement.schema   s     |""r   c                     | j         S )zXReturns this statement ID.

        Returns:
            int: The statement ID.
        rG   rO   s    r   stmt_idzStatement.stmt_id   s     }r   c                     || _         d S r   rS   r   values     r   rT   zStatement.stmt_id       r   c                     | j         S )z5int: The number of times this statement was executed.rH   rO   s    r   exec_counterzStatement.exec_counter   s     !!r   c                     | j         S )z+bool: `True` if this statement has changes.rI   rO   s    r   changedzStatement.changed   s     }r   c                     || _         d S r   r]   rV   s     r   r^   zStatement.changed   rX   r   c                     | j         S )z1bool: `True` if this statement has been prepared.rJ   rO   s    r   preparedzStatement.prepared   s     ~r   c                     || _         d S r   ra   rV   s     r   rb   zStatement.prepared   s    r   c                     | j         dk    S )zDbool: `True` if this statement was executed more than once.
        r   rZ   rO   s    r   repeatedzStatement.repeated   s     !A%%r   c                     | j         S )zBbool: `True` to deallocate + prepare + execute statement.
        rK   rO   s    r   deallocate_prepare_executez$Statement.deallocate_prepare_execute   s     //r   c                     || _         d S r   rg   rV   s     r   rh   z$Statement.deallocate_prepare_execute   s    +0(((r   c                     | j         S )zkCheck if it is document based.

        Returns:
            bool: `True` if it is document based.
        )rD   rO   s    r   is_doc_basedzStatement.is_doc_based        r   c                 &    | xj         dz  c_         dS )z@Increments the number of times this statement has been executed.r   NrZ   rO   s    r   increment_exec_counterz Statement.increment_exec_counter   s    ar   c                     d| _         dS )z<Resets the number of times this statement has been executed.r   NrZ   rO   s    r   reset_exec_counterzStatement.reset_exec_counter   s    r   c                     t           zqExecute the statement.

        Raises:
           NotImplementedError: This method must be implemented.
        NotImplementedErrorrO   s    r   executezStatement.execute   
     "!r   NT)r   r   r   r   r   propertyrL   rQ   rT   setterr[   r^   rb   re   rh   rk   rn   rp   ru   r   r   r   rA   rA   }   s        1 1 1 1   X # # X#   X ^  ^ " " X"   X ^  ^   X _  _ & & X&
 0 0 X0
  &1 1 '&1         " " " " "r   rA   c                        e Zd ZdZd fd	Zd Zd Zd Zd Zd	 Z	d
 Z
d Zd Zd Zd Zd Zd Zd Zd Zd Z ed          d             Z ed          d             ZddZd Zd Zd Z xZS )FilterableStatementa  A statement to be used with filterable statements.

    Args:
        target (object): The target database object, it can be
                         :class:`mysqlx.Collection` or :class:`mysqlx.Table`.
        doc_based (Optional[bool]): `True` if it is document based
                                    (default: `True`).
        condition (Optional[str]): Sets the search condition to filter
                                   documents or records.
    TNc                    t          t          |                               ||           i | _        i | _        d | _        d| _        d | _        d| _        d | _	        d| _
        d | _        d| _        d | _        d| _        d | _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        |r|                     |           d S d S )NrL   rM   r!   r   F)superr{   r   _binding_map	_bindings_having_grouping_str	_grouping_limit_offset_limit_row_count_projection_str_projection_expr	_sort_str
_sort_expr
_where_str_where_exprhas_bindings	has_limithas_group_by
has_havinghas_projectionhas_sort	has_where
_set_wherer   rL   rM   	condition	__class__s       r   r   zFilterableStatement.__init__   s    !4((11<E 	2 	G 	G 	G $! $!!# 	'OOI&&&&&	' 	'r   c                 d   t          |t                    r6|                     t          |                                                     dS t          |t                    r)|                     |                                           dS t          |t
                    r	 t          j        |          }t          |t                    st          n# t          $ r t          d          w xY w|
                                D ]}|                     |||                    dS t          d          )a  Bind single object.

        Args:
            obj (:class:`mysqlx.DbDoc` or str): DbDoc or JSON string object.

        Raises:
            :class:`mysqlx.ProgrammingError`: If invalid JSON string to bind.
            ValueError: If JSON loaded is not a dictionary.
        zInvalid JSON string to bindz%Invalid JSON string or object to bindN)r   dictbindr   as_strstrjsonloads
ValueErrorr   keys)r   objreskeys       r   _bind_singlez FilterableStatement._bind_single  s2    c4   	LIIeCjj''))*****U## 	LIIcjjll#####S!! 
	LFjoo!#t,, %$$% F F F&'DEEEFxxzz ) )		#s3x(((() ) ##JKKKs    0C C+c                     d| _         d                    t          |           | _        t	          | j        | j                                                   | _        d| _        | S )zSets the sorting criteria.

        Args:
            *clauses: The expression strings defining the sort criteria.

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.
        T,)	r   r4   r    r   r   rD   parse_order_specr   rI   r   clausess     r   _sortzFilterableStatement._sort%  s[     /7";<<$T^)-%8: ::J:J:L:L 	r   c                     d| _         || _        	 t          || j                   }|                                | _        n# t          $ r t          d          w xY w|j        | _	        d| _
        | S )a  Sets the search condition to filter.

        Args:
            condition (str): Sets the search condition to filter documents or
                             records.

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.
        TzInvalid condition)r   r   r   rD   r   r   r   r   placeholder_name_to_positionr   rI   )r   r   r   s      r   r   zFilterableStatement._set_where5  s     #	8iT_)<==D#yy{{D 	8 	8 	8"#6777	8 =s   /A   Ac                     t          | }d| _        d                    |          | _        t	          | j        | j                                                   | _        d| _        dS )zJSet group by.

        Args:
            *fields: List of fields.
        Tr   N)	r    r   r4   r   r   rD   parse_expr_listr   rI   r   fieldss     r   _set_group_byz!FilterableStatement._set_group_byJ  sa     !&)  XXf--#D$6(,$79 99H9J9J 	r   c                 |    d| _         t          || j                                                   | _        d| _        dS )zOSet having.

        Args:
            condition (str): The condition.
        TN)r   r   rD   r   r   rI   r   r   s     r   _set_havingzFilterableStatement._set_havingW  s8     !)-@AAFFHHr   c                     t          | }d| _        d                    |          | _        t	          | j        | j                                                   | _        d| _        | S )zSet the projection.

        Args:
            *fields: List of fields.

        Returns:
            :class:`mysqlx.FilterableStatement`: Returns self.
        Tr   )	r    r   r4   r   r   rD   parse_table_select_projectionr   rI   r   s     r   _set_projectionz#FilterableStatement._set_projectiona  sg     !&)""xx// * !! !!!>!>!@!@ 	 r   c                     | j         S zlReturns the binding map dictionary.

        Returns:
            dict: The binding map dictionary.
        r   rO   s    r   get_binding_mapz#FilterableStatement.get_binding_maps         r   c                     | j         S z\Returns the bindings list.

        Returns:
            `list`: The bindings list.
        r   rO   s    r   get_bindingsz FilterableStatement.get_bindings{       ~r   c                     | j         S )zrReturns the grouping expression list.

        Returns:
            `list`: The grouping expression list.
        )r   rO   s    r   get_groupingz FilterableStatement.get_grouping  r   r   c                     | j         S )zdReturns the having expression.

        Returns:
            object: The having expression.
        )r   rO   s    r   
get_havingzFilterableStatement.get_having       |r   c                     | j         S )z]Returns the limit row count.

        Returns:
            int: The limit row count.
        )r   rO   s    r   get_limit_row_countz'FilterableStatement.get_limit_row_count       $$r   c                     | j         S )zWReturns the limit offset.

        Returns:
            int: The limit offset.
        )r   rO   s    r   get_limit_offsetz$FilterableStatement.get_limit_offset  s     !!r   c                     | j         S )zbReturns the where expression.

        Returns:
            object: The where expression.
        )r   rO   s    r   get_where_exprz"FilterableStatement.get_where_expr       r   c                     | j         S )zlReturns the projection expression.

        Returns:
            object: The projection expression.
        )r   rO   s    r   get_projection_exprz'FilterableStatement.get_projection_expr  r   r   c                     | j         S )z`Returns the sort expression.

        Returns:
            object: The sort expression.
        )r   rO   s    r   get_sort_exprz!FilterableStatement.get_sort_expr  rl   r   8.0.12c                 ,    |                      |          S )a&  Sets the search condition to filter.

        Args:
            condition (str): Sets the search condition to filter documents or
                             records.

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.

        .. deprecated:: 8.0.12
        r   r   s     r   wherezFilterableStatement.where  s     y)))r   c                      | j         | S )zSets the sorting criteria.

        Args:
            *clauses: The expression strings defining the sort criteria.

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.

        .. deprecated:: 8.0.12
        r   r   s     r   sortzFilterableStatement.sort  s     tz7##r   c                 ^   t          |t                    r|dk     rt          d          | j        s;t	          | j        dk              | _        t	          | j        dk               | _        || _        d| _        |r0| 	                    |           t          j        dt                     | S )a  Sets the maximum number of items to be returned.

        Args:
            row_count (int): The maximum number of items.

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.

        Raises:
            ValueError: If ``row_count`` is not a positive integer.

        .. versionchanged:: 8.0.12
           The usage of ``offset`` was deprecated.
        r   z0The 'row_count' value must be a positive integerTzh'limit(row_count, offset)' is deprecated, please use 'offset(offset)' to set the number of items to skip)category)r   intr   r   boolrH   rI   rK   r   offsetwarningswarnDeprecationWarning)r   	row_countr   s      r   limitzFilterableStatement.limit  s     )S)) 	QY]]OPPP~ 	Q !3q!899DM/38Ja8O4O/P/PD, ) 	?KKM !+=? ? ? ? r   c                 h    t          |t                    r|dk     rt          d          || _        | S )a:  Sets the number of items to skip.

        Args:
            offset (int): The number of items to skip.

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.

        Raises:
            ValueError: If ``offset`` is not a positive integer.

        .. versionadded:: 8.0.12
        r   z-The 'offset' value must be a positive integer)r   r   r   r   )r   r   s     r   r   zFilterableStatement.offset  s;     &#&& 	N&1**LMMM#r   c                     d| _         t          |          }|dk    r|                     |d                    n,|dk    r|d         | j        |d         <   nt	          d          | S )a  Binds value(s) to a specific placeholder(s).

        Args:
            *args: The name of the placeholder and the value to bind.
                   A :class:`mysqlx.DbDoc` object or a JSON string
                   representation can be used.

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.

        Raises:
            ProgrammingError: If the number of arguments is invalid.
        Tr   r      #Invalid number of arguments to bind)r   r   r   r   r   )r   argscounts      r   r   zFilterableStatement.bind	  so     !D		A::d1g&&&&aZZ&*1gDN47##"#HIIIr   c                     t           rr   rs   rO   s    r   ru   zFilterableStatement.execute!  rv   r   TNr   )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   r   ru   __classcell__r   s   @r   r{   r{      s       	 	' ' ' ' ' '4L L L4     *      $! ! !      % % %" " "     % % %   Z* * * Z$ $ $   <  &  0" " " " " " "r   r{   c                   P     e Zd ZdZ fdZed             Zd Zd Zd Z	d Z
 xZS )SqlStatementzA statement for SQL execution.

    Args:
        connection (mysqlx.connection.Connection): Connection object.
        sql (string): The sql statement to be executed.
    c                     t          t          |                               d d           || _        || _        d | _        g | _        d| _        d| _        d S NFr}   )	r~   r   r   rF   _sqlr   r   r   r   )r   
connectionsqlr   s      r   r   zSqlStatement.__init__1  sW    lD!!**$%*HHH%	 !r   c                     | j         S )zstring: The SQL text statement.)r   rO   s    r   r   zSqlStatement.sql:  s     yr   c                     | j         S r   r   rO   s    r   r   zSqlStatement.get_binding_map?  r   r   c                     | j         S r   r   rO   s    r   r   zSqlStatement.get_bindingsG  r   r   c                     t          |          dk    rt          d          d| _        t          | }t	          |t
          t          f          r|| _        n| j                            |           | S )zBinds value(s) to a specific placeholder(s).

        Args:
            *args: The value(s) to bind.

        Returns:
            mysqlx.SqlStatement: SqlStatement object.
        r   r   T)	r   r   r   r    r   r   r   r   append)r   r   bindingss      r   r   zSqlStatement.bindO  sn     t99>>"#HIII "D)hu.. 	,%DNNN!!(+++r   c                 6    | j                             |           S )zaExecute the statement.

        Returns:
            mysqlx.SqlResult: SqlResult object.
        )rF   send_sqlrO   s    r   ru   zSqlStatement.executeb  s     ((...r   )r   r   r   r   r   rx   r   r   r   r   ru   r   r   s   @r   r   r   *  s                X! ! !    &/ / / / / / /r   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )WriteStatementz/Provide common write operation attributes.
    c                 h    t          t          |                               ||           g | _        d S r   )r~   r   r   _values)r   rL   rM   r   s      r   r   zWriteStatement.__init__n  s.    nd##,,VY???r   c                     | j         S )z^Returns the list of values.

        Returns:
            `list`: The list of values.
        )r   rO   s    r   
get_valueszWriteStatement.get_valuesr  r   r   c                     t           rr   rs   rO   s    r   ru   zWriteStatement.executez  rv   r   )r   r   r   r   r   r  ru   r   r   s   @r   r   r   k  s`               " " " " " " "r   r   c                   <     e Zd ZdZ fdZd ZddZd Zd Z xZ	S )	AddStatementzA statement for document addition on a collection.

    Args:
        collection (mysqlx.Collection): The Collection object.
    c                 v    t          t          |                               |d           d| _        g | _        d S )NTF)r~   r  r   _upsertids)r   
collectionr   s     r   r   zAddStatement.__init__  s5    lD!!**:t<<<r   c                     | j         S )zhReturns `True` if it's an upsert.

        Returns:
            bool: `True` if it's an upsert.
        r  rO   s    r   	is_upsertzAddStatement.is_upsert  r   r   Tc                     || _         | S )zSets the upset flag to the boolean of the value provided.
        Setting of this flag allows updating of the matched rows/documents
        with the provided value.

        Args:
            value (optional[bool]): Set or unset the upsert flag.
        r
  rV   s     r   upsertzAddStatement.upsert  s     r   c                     t          | D ]Y}t          |t                    r| j                            |           2| j                            t          |                     Z| S )zAdds a list of documents into a collection.

        Args:
            *values: The documents to be added into the collection.

        Returns:
            mysqlx.AddStatement: AddStatement object.
        )r    r   r   r   r   )r   r   vals      r   addzAddStatement.add  sg     #F+ 	0 	0C#u%% 0##C((((##E#JJ////r   c                     t          | j                  dk    rt                      S | j                            |           S )[Execute the statement.

        Returns:
            mysqlx.Result: Result object.
        r   )r   r   r
   rF   send_insertrO   s    r   ru   zAddStatement.execute  s9     t|!!88O++D111r   rw   )
r   r   r   r   r   r  r  r  ru   r   r   s   @r   r  r    s         
    
  	 	 	 	   	2 	2 	2 	2 	2 	2 	2r   r  c                        e Zd ZdZddZd ZdS )
UpdateSpeczUpdate specification class implementation.

    Args:
        update_type (int): The update type.
        source (str): The source.
        value (Optional[str]): The value.
    Nc                 D   |t          d          k    r|                     ||           d S || _        || _        t	          |          dk    r|d         dk    r|dd          | _        t          | j        d                                          j        | _        || _        d S )N*Mysqlx.Crud.UpdateOperation.UpdateType.SETr   $r   F)	r   
_table_setupdate_typesourcer   r   document_fieldr(   rW   )r   r  r  rW   s       r   r   zUpdateSpec.__init__  s    -<> > > >OOFE******D DK6{{Q6!9#3#3$QRRj$T[%*, ,,:N,<,<Z KDJJJr   c                     t          d          | _        t          |d                                          | _        || _        dS )zlTable set.

        Args:
            source (str): The source.
            value (str): The value.
        r  TN)r   r  r   parse_table_update_fieldr  rW   )r   r  rW   s      r   r  zUpdateSpec._table_set  sA     )8: : ..GGII


r   r   )r   r   r   r   r   r  r   r   r   r  r    sA            
 
 
 
 
r   r  c                   z     e Zd ZdZ fdZd Zd Zd Z ed          d             Z	d Z
d	 Zd
 Zd Zd Z xZS )ModifyStatementaU  A statement for document update operations on a Collection.

    Args:
        collection (mysqlx.Collection): The Collection object.
        condition (str): Sets the search condition to identify the documents
                         to be modified.

    .. versionchanged:: 8.0.12
       The ``condition`` parameter is now mandatory.
    c                 j    t          t          |                               ||           i | _        d S N)rL   r   )r~   r   r   _update_opsr   r  r   r   s      r   r   zModifyStatement.__init__  s@    ot$$--Z8A 	. 	C 	C 	Cr   c                      | j         | S )zSets the sorting criteria.

        Args:
            *clauses: The expression strings defining the sort criteria.

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.
        r   r   s     r   r   zModifyStatement.sort       tz7##r   c                     | j         S ztReturns the list of update operations.

        Returns:
            `list`: The list of update operations.
        r#  rO   s    r   get_update_opszModifyStatement.get_update_ops  r   r   c                 `    t          t          d          ||          | j        |<   d| _        | S )a2  Sets or updates attributes on documents in a collection.

        Args:
            doc_path (string): The document path of the item to be set.
            value (string): The value to be set on the specified attribute.

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.
        z/Mysqlx.Crud.UpdateOperation.UpdateType.ITEM_SETTr  r   r#  rI   r   doc_pathrW   s      r   setzModifyStatement.set  s?     &0=1? 1?08%&A &A" r   r   c                 `    t          t          d          ||          | j        |<   d| _        | S )a  Add an update to the statement setting the field, if it exists at
        the document path, to the given value.

        Args:
            doc_path (string): The document path of the item to be set.
            value (object): The value to be set on the specified attribute.

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.

        .. deprecated:: 8.0.12
        z3Mysqlx.Crud.UpdateOperation.UpdateType.ITEM_REPLACETr,  r-  s      r   changezModifyStatement.change  sA     &0A1C 1C08%&A &A" r   c                 v    t          | D ]'}t          t          d          |          | j        |<   (d| _        | S )a  Removes attributes from documents in a collection.

        Args:
            doc_paths (list): The list of document paths of the attributes to be
                              removed.

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.
        z2Mysqlx.Crud.UpdateOperation.UpdateType.ITEM_REMOVET)r    r  r   r#  rI   )r   	doc_pathsitems      r   unsetzModifyStatement.unset&  sV     $Y/ 	M 	MD%/D1F 1FGK&M &MDT""r   c                 `    t          t          d          ||          | j        |<   d| _        | S )a  Insert a value into the specified array in documents of a
        collection.

        Args:
            field (string): A document path that identifies the array attribute
                            and position where the value will be inserted.
            value (object): The value to be inserted.

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.
        z3Mysqlx.Crud.UpdateOperation.UpdateType.ARRAY_INSERTTr,  r   fieldrW   s      r   array_insertzModifyStatement.array_insert6  s?     #-]A.C .C-2E#; #; r   c                 `    t          t          d          ||          | j        |<   d| _        | S )a  Inserts a value into a specific position in an array attribute in
        documents of a collection.

        Args:
            doc_path (string): A document path that identifies the array
                               attribute and position where the value will be
                               inserted.
            value (object): The value to be inserted.

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.
        z3Mysqlx.Crud.UpdateOperation.UpdateType.ARRAY_APPENDTr,  r-  s      r   array_appendzModifyStatement.array_appendH  sA     &0A1C 1C08%&A &A" r   c                 (   |d}t          |t          t          t          t          f          st          d          t          t          d          dt          |t                    r|                                n|          | j	        d<   d| _
        | S )a  Takes a :class:`mysqlx.DbDoc`, string JSON format or a dict with the
        changes and applies it on all matching documents.

        Args:
            doc (object): A generic document (DbDoc), string in JSON format or
                          dict, with the changes to apply to the matching
                          documents.

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.
        Nr!   z>Invalid data for update operation on document collection tablez2Mysqlx.Crud.UpdateOperation.UpdateType.MERGE_PATCHpatchT)r   r   r   r   r   r   r  r   r   r#  rI   )r   docs     r   r=  zModifyStatement.patch[  s     ;C#
D%=>> 	"   %/NOOjj99B


s%D %D! r   c                 b    | j         st          d          | j                            |           S )Execute the statement.

        Returns:
            mysqlx.Result: Result object.

        Raises:
            ProgrammingError: If condition was not set.
        z!No condition was found for modifyr   r   rF   send_updaterO   s    r   ru   zModifyStatement.executes  5     ~ 	H"#FGGG++D111r   )r   r   r   r   r   r   r*  r/  r	   r1  r5  r9  r;  r=  ru   r   r   s   @r   r   r     s        	 	    
	$ 	$ 	$         Z  &     $  &  02 2 2 2 2 2 2r   r   c                        e Zd ZdZd fd	Zed             Zd Zd Zd Z	e
j        fd	Ze
j        fd
Zd Zd Zd Z xZS )ReadStatementa  Provide base functionality for Read operations

    Args:
        target (object): The target database object, it can be
                         :class:`mysqlx.Collection` or :class:`mysqlx.Table`.
        doc_based (Optional[bool]): `True` if it is document based
                                    (default: `True`).
        condition (Optional[str]): Sets the search condition to filter
                                   documents or records.
    TNc                     t          t          |                               |||           d| _        d| _        t
          j        | _        d S NF)r~   rE  r   _lock_exclusive_lock_sharedr   DEFAULT_lock_contentionr   s       r   r   zReadStatement.__init__  sF    mT""++FIyIII$! . 6r   c                     | j         S )z::class:`mysqlx.LockContention`: The lock contention value.)rK  rO   s    r   lock_contentionzReadStatement.lock_contention  s     $$r   c                 z    	 t          j        |          }n# t          $ r t          d          w xY w|| _        dS )zSet the lock contention.

        Args:
            lock_contention (:class:`mysqlx.LockContention`): Lock contention.

        Raises:
            ProgrammingError: If is an invalid lock contention value.
        z;Invalid lock contention mode. Use 'NOWAIT' or 'SKIP_LOCKED'N)r   indexr   r   rK  )r   rM  _s      r   _set_lock_contentionz"ReadStatement._set_lock_contention  s[    	7$_55AA 	7 	7 	7" $6 7 7 7	7 !0s    1c                     | j         S )zrReturns `True` if is `EXCLUSIVE LOCK`.

        Returns:
            bool: `True` if is `EXCLUSIVE LOCK`.
        )rH  rO   s    r   is_lock_exclusivezReadStatement.is_lock_exclusive  s     ##r   c                     | j         S )zlReturns `True` if is `SHARED LOCK`.

        Returns:
            bool: `True` if is `SHARED LOCK`.
        )rI  rO   s    r   is_lock_sharedzReadStatement.is_lock_shared  r   r   c                 L    d| _         d| _        |                     |           | S )zExecute a read operation with `SHARED LOCK`. Only one lock can be
           active at a time.

        Args:
            lock_contention (:class:`mysqlx.LockContention`): Lock contention.
        FTrH  rI  rQ  r   rM  s     r   lock_sharedzReadStatement.lock_shared  s-      % !!/222r   c                 L    d| _         d| _        |                     |           | S )zExecute a read operation with `EXCLUSIVE LOCK`. Only one lock can be
           active at a time.

        Args:
            lock_contention (:class:`mysqlx.LockContention`): Lock contention.
        TFrW  rX  s     r   lock_exclusivezReadStatement.lock_exclusive  s-      $!!!/222r   c                      | j         |  | S )zSets a grouping criteria for the resultset.

        Args:
            *fields: The string expressions identifying the grouping criteria.

        Returns:
            mysqlx.ReadStatement: ReadStatement object.
        )r   r   s     r   group_byzReadStatement.group_by  s     	F##r   c                 0    |                      |           | S )a=  Sets a condition for records to be considered in agregate function
        operations.

        Args:
            condition (string): A condition on the agregate functions used on
                                the grouping criteria.

        Returns:
            mysqlx.ReadStatement: ReadStatement object.
        )r   r   s     r   havingzReadStatement.having  s     	###r   c                 6    | j                             |           S r  )rF   	send_findrO   s    r   ru   zReadStatement.execute  s     ))$///r   r   )r   r   r   r   r   rx   rM  rQ  rS  rU  r   rJ  rY  r[  r]  r_  ru   r   r   s   @r   rE  rE    s        	 	7 7 7 7 7 7 % % X%0 0 0"$ $ $! ! ! +9*@ 
 
 
 
 .<-C 
 
 
 

 
 
  0 0 0 0 0 0 0r   rE  c                   0     e Zd ZdZd fd	Zd Zd Z xZS )FindStatementa  A statement document selection on a Collection.

    Args:
        collection (mysqlx.Collection): The Collection object.
        condition (Optional[str]): An optional expression to identify the
                                   documents to be retrieved. If not specified
                                   all the documents will be included on the
                                   result unless a limit is set.
    Nc                 \    t          t          |                               |d|           d S )NT)r~   rd  r   r$  s      r   r   zFindStatement.__init__  s+    mT""++JiHHHHHr   c                      | j         | S )zSets a document field filter.

        Args:
            *fields: The string expressions identifying the fields to be
                     extracted.

        Returns:
            mysqlx.FindStatement: FindStatement object.
        )r   r   s     r   r   zFindStatement.fields   s     $t#V,,r   c                      | j         | S zSets the sorting criteria.

        Args:
            *clauses: The expression strings defining the sort criteria.

        Returns:
            mysqlx.FindStatement: FindStatement object.
        r   r   s     r   r   zFindStatement.sort  r&  r   r   )r   r   r   r   r   r   r   r   r   s   @r   rd  rd    sk         I I I I I I
- 
- 
-	$ 	$ 	$ 	$ 	$ 	$ 	$r   rd  c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )SelectStatementzA statement for record retrieval operations on a Table.

    Args:
        table (mysqlx.Table): The Table object.
        *fields: The fields to be retrieved.
    c                 n    t          t          |                               |d            | j        |  d S rG  )r~   rj  r   r   r   tabler   r   s      r   r   zSelectStatement.__init__  s9    ot$$--eU;;;f%%%%r   c                 ,    |                      |          S )zSets the search condition to filter.

        Args:
            condition (str): Sets the search condition to filter records.

        Returns:
            mysqlx.SelectStatement: SelectStatement object.
        r   r   s     r   r   zSelectStatement.where#       y)))r   c                      | j         | S )zSets the order by criteria.

        Args:
            *clauses: The expression strings defining the order by criteria.

        Returns:
            mysqlx.SelectStatement: SelectStatement object.
        r   r   s     r   order_byzSelectStatement.order_by.  r&  r   c           
         | j         rd                    | j                  nd}| j        rd                    | j                  nd}| j        rd                    | j                  nd}| j        rd                    | j                  nd}| j	        r d                    | j
        | j                  nd}d                    | j        pd| j        j        | j        j        |||||	          }|S )
zYReturns the generated SQL.

        Returns:
            str: The generated SQL.
        z
 WHERE {0}r!   z GROUP BY {0}z HAVING {0}z ORDER BY {0}z LIMIT {0} OFFSET {1}zISELECT {select} FROM {schema}.{table}{where}{group}{having}{order}{limit}*)selectrQ   rm  r   r   groupr_  order)r   r,   r   r   r   r   r   r   r   r   r   r   r   rQ   namerL   )r   r   r]  r_  rq  r   stmts          r   get_sqlzSelectStatement.get_sql9  s    9=N##DO444B&?))$*<===#% 	7;N%%dl333B=A] ?))$.999 	 37.I'..t/D/3/AC C CFH 	!!'t/C/Js/3{/?.2k.>e.38/5X	 "( "G "G 	 r   )	r   r   r   r   r   r   rq  ry  r   r   s   @r   rj  rj    so         & & & & &	* 	* 	*	$ 	$ 	$      r   rj  c                   .     e Zd ZdZ fdZd Zd Z xZS )InsertStatementzA statement for insert operations on Table.

    Args:
        table (mysqlx.Table): The Table object.
        *fields: The fields to be inserted.
    c                 v    t          t          |                               |d           t          | | _        d S rG  )r~   r{  r   r    _fieldsrl  s      r   r   zInsertStatement.__init__X  s3    ot$$--eU;;;&/r   c                 b    | j                             t          t          |                      | S )zSet the values to be inserted.

        Args:
            *values: The values of the columns to be inserted.

        Returns:
            mysqlx.InsertStatement: InsertStatement object.
        )r   r   r   r    )r   r   s     r   r   zInsertStatement.values\  s,     	D&!9::;;;r   c                 6    | j                             |           S ra  )rF   r  rO   s    r   ru   zInsertStatement.executeh  s     ++D111r   )r   r   r   r   r   r   ru   r   r   s   @r   r{  r{  Q  s`         0 0 0 0 0
 
 
2 2 2 2 2 2 2r   r{  c                   @     e Zd ZdZ fdZd Zd Zd Zd Zd Z	 xZ
S )UpdateStatementzA statement for record update operations on a Table.

    Args:
        table (mysqlx.Table): The Table object.

    .. versionchanged:: 8.0.12
       The ``fields`` parameters were removed.
    c                 j    t          t          |                               |d           i | _        d S r   )r~   r  r   r#  r   rm  r   s     r   r   zUpdateStatement.__init__z  s4    ot$$--Ue-LLLr   c                 ,    |                      |          S )zSets the search condition to filter.

        Args:
            condition (str): Sets the search condition to filter records.

        Returns:
            mysqlx.UpdateStatement: UpdateStatement object.
        r   r   s     r   r   zUpdateStatement.where~  ro  r   c                      | j         | S )zSets the order by criteria.

        Args:
            *clauses: The expression strings defining the order by criteria.

        Returns:
            mysqlx.UpdateStatement: UpdateStatement object.
        r   r   s     r   rq  zUpdateStatement.order_by  r&  r   c                     | j         S r(  r)  rO   s    r   r*  zUpdateStatement.get_update_ops  r   r   c                 `    t          t          d          ||          | j        |<   d| _        | S )a  Updates the column value on records in a table.

        Args:
            field (string): The column name to be updated.
            value (object): The value to be set on the specified column.

        Returns:
            mysqlx.UpdateStatement: UpdateStatement object.
        r  Tr,  r7  s      r   r/  zUpdateStatement.set  s=     #-]8.: .:;@%#I #Ir   c                 b    | j         st          d          | j                            |           S )zExecute the statement.

        Returns:
            mysqlx.Result: Result object

        Raises:
            ProgrammingError: If condition was not set.
        z!No condition was found for updaterA  rO   s    r   ru   zUpdateStatement.execute  rC  r   )r   r   r   r   r   r   rq  r*  r/  ru   r   r   s   @r   r  r  q  s             	* 	* 	*	$ 	$ 	$       2 2 2 2 2 2 2r   r  c                   .     e Zd ZdZ fdZd Zd Z xZS )RemoveStatementaE  A statement for document removal from a collection.

    Args:
        collection (mysqlx.Collection): The Collection object.
        condition (str): Sets the search condition to identify the documents
                         to be removed.

    .. versionchanged:: 8.0.12
       The ``condition`` parameter was added.
    c                 \    t          t          |                               ||           d S r"  )r~   r  r   r$  s      r   r   zRemoveStatement.__init__  s>    ot$$--Z8A 	. 	C 	C 	C 	C 	Cr   c                      | j         | S rh  r   r   s     r   r   zRemoveStatement.sort  r&  r   c                 b    | j         st          d          | j                            |           S )r@  z!No condition was found for remover   r   rF   send_deleterO   s    r   ru   zRemoveStatement.execute  rC  r   )r   r   r   r   r   r   ru   r   r   s   @r   r  r    se        	 	C C C C C	$ 	$ 	$2 2 2 2 2 2 2r   r  c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )DeleteStatementzA statement that drops a table.

    Args:
        table (mysqlx.Table): The Table object.

    .. versionchanged:: 8.0.12
       The ``condition`` parameter was removed.
    c                 \    t          t          |                               |d           d S r   )r~   r  r   r  s     r   r   zDeleteStatement.__init__  s,    ot$$--Ue-LLLLLr   c                 ,    |                      |          S )zSets the search condition to filter.

        Args:
            condition (str): Sets the search condition to filter records.

        Returns:
            mysqlx.DeleteStatement: DeleteStatement object.
        r   r   s     r   r   zDeleteStatement.where  ro  r   c                      | j         | S )zSets the order by criteria.

        Args:
            *clauses: The expression strings defining the order by criteria.

        Returns:
            mysqlx.DeleteStatement: DeleteStatement object.
        r   r   s     r   rq  zDeleteStatement.order_by  r&  r   c                 b    | j         st          d          | j                            |           S )r@  z!No condition was found for deleter  rO   s    r   ru   zDeleteStatement.execute  rC  r   )	r   r   r   r   r   r   rq  ru   r   r   s   @r   r  r    st         M M M M M	* 	* 	*	$ 	$ 	$2 2 2 2 2 2 2r   r  c                   (     e Zd ZdZ fdZd Z xZS )CreateCollectionIndexStatementa)  A statement that creates an index on a collection.

    Args:
        collection (mysqlx.Collection): Collection.
        index_name (string): Index name.
        index_desc (dict): A dictionary containing the fields members that
                           constraints the index to be created. It must have
                           the form as shown in the following::

                               {"fields": [{"field": member_path,
                                            "type": member_type,
                                            "required": member_required,
                                            "collation": collation,
                                            "options": options,
                                            "srid": srid},
                                            # {... more members,
                                            #      repeated as many times
                                            #      as needed}
                                            ],
                                "type": type}
    c                     t          t          |                               |           t          j        |          | _        || _        | j                            dg           | _        d S )N)rL   r   )	r~   r  r   copydeepcopy_index_desc_index_namepop_fields_desc)r   r  
index_name
index_descr   s       r   r   z'CreateCollectionIndexStatement.__init__'  s`    ,d33<<J<OOO=44% ,002>>r   c                 
   | j         ,t          t                              | j                             	 t	          | j                                                                                   }t          |t                    rF|d         t          d          k    r,t          t                              | j                             nD|j
        t          d          k    r,t          t                              | j                             nA# t          t          f$ r- t          t                              | j                             w xY w| j        s't          d                    | j                            t          | j        t                    st          d          i }| j         |d<   | j        j        |d<   | j        j        j        |d<   d| j        v r| j                            d          |d<   nd	|d<   | j                            d
d          |d
<   |d
         rt)          d          g |d<   | j        r't          d                    | j                            	 | j        D ]Y}i }|                    d          |d<   |                    d          |d<   |                    dd          |d<   |                    dd          |d<   t          |d         t*                    st-          d          t          |d         t*                    st-          d          |d                                         dk    r|d         st          d          |d                                         d	k    r|d         dk    rt          d          d|v r|d                                                             d          s:t          d                    |d                                                             |                    d          |d<   d|v rE|d                                         dk    rt          d          |                    d          |d<   d|v rE|d                                         dk    rt          d          |                    d          |d<   |d                             |           [n5# t4          $ r(}t          d                     ||                    d}~ww xY w| j        D ]&}|r"t          d!                    |                    '| j                            d"d#d$|          S )%r  NtypezMysqlx.Expr.Expr.Type.IDENTzERequired member 'fields' not found in the given index description: {}z-Required member 'fields' must contain a list.rw  r  rQ   INDEXuniqueFz!Unique indexes are not supported.
constraintzUnidentified fields: {}r8  memberrequiredarrayz'Field member 'required' must be Booleanz$Field member 'array' must be BooleanSPATIALzQField member 'required' must be set to 'True' when index type is set to 'SPATIAL'GEOJSONzIIndex 'type' must be set to 'SPATIAL' when field type is set to 'GEOJSON'	collationTEXTzFThe 'collation' member can only be used when field type is set to '{}'optionszIThe 'options' member can only be used when index type is set to 'GEOJSON'sridzFThe 'srid' member can only be used when index type is set to 'GEOJSON'z4Required inner member {} not found in constraint: {}zUnidentified inner fields:{}mysqlxcreate_collection_indexT)r  r   ERR_INVALID_INDEX_NAMEr,   r   r   get_messager   r   r   r  r   AttributeErrorr  r  r   rC   rw  rQ   r  r   r   	TypeErrorupper
startswithr   KeyErrorrF   execute_nonquery)r   parsed_identr   
field_descr  errs         r   ru   z&CreateCollectionIndexStatement.execute-  s     #"&--d.>??A A A	A%d&677<<>>JJLLL ,-- 	I'=5,7 ,7 7 7*.55d6FGGI I I7
  $5)7 )7 7 7*.55d6FGGI I I N+ 	A 	A 	A"&--d.>??A A A	A
   	@" $&&,fT-=&>&>@ @ @ $+T22 	," $+ , , , 'V!\.\,1XT%%%+//77DLL"DL)--h>>X> 	I#$GHHH\ 	@" $&&,fT-=&>&>@ @ @-	M"/ )6 )6

'1~~g'>'>
8$%/^^F%;%;
6")3
E)J)J
:&&0nnWe&D&D
7#!*Z"8$?? O#$MNNN!*W"5t<< L#$JKKK<%%''944!*- 5*9: : : <%%''722f%22*34 4 4 *,,%f-3355@@HH C.%vj&8&>&>&@&@AAC C C /9nn[.I.IJ{+ 
**!&)//11Y>>.=> > > -7NN9,E,EJy)Z''!&)//11Y>>.78 8 8 *4)?)?Jv&\"))*5555S)6T  	M 	M 	M" $44:F3
4K4KM M M	M + 	> 	>J >& (**0&*<*<> > >> 00/t= = 	=s%   CD >E)I"S 
S>#S99S>)r   r   r   r   r   ru   r   r   s   @r   r  r    sX         *? ? ? ? ?n= n= n= n= n= n= n=r   r  )r!   )*r   r  r   r   errorsr   r   r   r   	constantsr   dbdocr   helpersr	   resultr
   protobufr   r  objectr   r    r*   r.   r6   r?   rA   r{   r   r   r  r  r   rE  rd  rj  r{  r  r  r  r  r   r   r   <module>r     s  : $ #    7 7 7 7 7 7 7 7       % % % % % %                   # # # # # #A     6     : : : : 9 9 9 9$5 5 5 5$ $ $ $$f" f" f" f" f" f" f" f"RA" A" A" A" A") A" A" A"H
>/ >/ >/ >/ >/9 >/ >/ >/B" " " " "Y " " "072 72 72 72 72> 72 72 72t       D_2 _2 _2 _2 _2) _2 _2 _2Do0 o0 o0 o0 o0' o0 o0 o0d"$ "$ "$ "$ "$M "$ "$ "$J6 6 6 6 6m 6 6 6r2 2 2 2 2n 2 2 2@E2 E2 E2 E2 E2) E2 E2 E2P%2 %2 %2 %2 %2) %2 %2 %2P-2 -2 -2 -2 -2) -2 -2 -2`J= J= J= J= J=Y J= J= J= J= J=r   