a
    còfY¶  ã                   @   s¬  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„ deƒZdd„ Zd7dd„Zd8dd„Zd9dd„Zd:dd„ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd „ d eƒZG d!d"„ d"eƒZG d#d$„ d$eƒZ G d%d&„ d&eƒZ!G d'd(„ d(eƒZ"G d)d*„ d*e"ƒZ#G d+d,„ d,e"ƒZ$G d-d.„ d.eƒZ%G d/d0„ d0eƒZ&G d1d2„ d2eƒZ'G d3d4„ d4eƒZ(G d5d6„ d6eƒZ)dS );zImplementation of Statements.é    Né   )ÚProgrammingErrorÚNotSupportedError)Ú
ExprParser)ÚLockContention)ÚDbDoc)Ú
deprecated)ÚResult)Úmysqlxpb_enumz&The given index name "{}" is not validc                   @   s   e Zd ZdZdd„ ZdS )ÚExprzExpression wrapper.c                 C   s
   || _ d S ©N)Úexpr)Úselfr   © r   úM/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/mysqlx/statement.pyÚ__init__0   s    zExpr.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   .   s   r   c                  G   s*   t | ƒdkr&t| d ttfƒr&| d S | S )zParse flexible parameters.r   r   )ÚlenÚ
isinstanceÚlistÚtuple)Úvaluesr   r   r   Úflexible_params4   s    r   Ú c                 C   sP   d|v r8| d dkr | d dkp6| d dko6| d dkS | d dkoN| d dkS )zòCheck 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_moder   r   r   Úis_quoted_identifier;   s
    
ÿr$   c                 C   s<   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!   r   r   r   Úquote_identifierK   s
    r'   c                    s   d  ‡ fd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                    s   g | ]}t |ˆ ƒ‘qS r   )r'   )Ú.0r"   ©r#   r   r   Ú
<listcomp>g   s   ÿz.quote_multipart_identifier.<locals>.<listcomp>)Újoin)Zidentifiersr#   r   r*   r   Úquote_multipart_identifier]   s    
ÿr-   c                 C   s^   d|v rdnd}||v r"d  |¡nd}| |d¡}t|ƒdkrB| n|d  |¡|d  |¡fS )	zÞParse 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}r(   r   r   r   )r%   Úsplitr   Ústrip)Zdefault_schemaZ
table_namer#   ÚquoteÚ	delimiterÚtempr   r   r   Úparse_table_namek   s    ÿr3   c                   @   sÒ   e Zd ZdZd"dd„Zedd„ ƒZedd„ ƒZed	d
„ ƒZej	dd
„ ƒZedd„ ƒZ
edd„ ƒZej	dd„ ƒZedd„ ƒZej	dd„ ƒZedd„ ƒZedd„ ƒZej	dd„ ƒZdd„ Zdd„ Zdd„ Zd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                 C   s@   || _ || _|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_basedr   r   r   r   …   s    zStatement.__init__c                 C   s   | j S )z#object: The database object target.)r5   ©r   r   r   r   r?   �   s    zStatement.targetc                 C   s   | j jS )z*:class:`mysqlx.Schema`: The Schema object.)r5   ÚschemarA   r   r   r   rB   ”   s    zStatement.schemac                 C   s   | j S )zXReturns this statement ID.

        Returns:
            int: The statement ID.
        ©r9   rA   r   r   r   Ústmt_id™   s    zStatement.stmt_idc                 C   s
   || _ d S r   rC   ©r   Úvaluer   r   r   rD   ¢   s    c                 C   s   | j S )z5int: The number of times this statement was executed.©r:   rA   r   r   r   Úexec_counter¦   s    zStatement.exec_counterc                 C   s   | j S )z+bool: `True` if this statement has changes.©r;   rA   r   r   r   Úchanged«   s    zStatement.changedc                 C   s
   || _ d S r   rI   rE   r   r   r   rJ   °   s    c                 C   s   | j S )z1bool: `True` if this statement has been prepared.©r<   rA   r   r   r   Úprepared´   s    zStatement.preparedc                 C   s
   || _ d S r   rK   rE   r   r   r   rL   ¹   s    c                 C   s
   | j dkS )zDbool: `True` if this statement was executed more than once.
        r   rG   rA   r   r   r   Úrepeated½   s    zStatement.repeatedc                 C   s   | j S )zBbool: `True` to deallocate + prepare + execute statement.
        ©r=   rA   r   r   r   Údeallocate_prepare_executeÃ   s    z$Statement.deallocate_prepare_executec                 C   s
   || _ d S r   rN   rE   r   r   r   rO   É   s    c                 C   s   | j S )zkCheck if it is document based.

        Returns:
            bool: `True` if it is document based.
        )r6   rA   r   r   r   Úis_doc_basedÍ   s    zStatement.is_doc_basedc                 C   s   |  j d7  _ dS )z@Increments the number of times this statement has been executed.r   NrG   rA   r   r   r   Úincrement_exec_counterÕ   s    z Statement.increment_exec_counterc                 C   s
   d| _ dS )z<Resets the number of times this statement has been executed.r   NrG   rA   r   r   r   Úreset_exec_counterÙ   s    zStatement.reset_exec_counterc                 C   s   t ‚dS ©zqExecute the statement.

        Raises:
           NotImplementedError: This method must be implemented.
        N©ÚNotImplementedErrorrA   r   r   r   ÚexecuteÝ   s    zStatement.executeN)T)r   r   r   r   r   Úpropertyr?   rB   rD   ÚsetterrH   rJ   rL   rM   rO   rP   rQ   rR   rV   r   r   r   r   r4   }   s<   













r4   c                       sÜ   e Zd ZdZd1‡ fdd„	Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zed$ƒd%d&„ ƒZed$ƒd'd(„ ƒZd2d)d*„Zd+d,„ Zd-d.„ Zd/d0„ Z‡  ZS )3Ú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                    sž   t t| ƒj||d� i | _i | _d | _d| _d | _d| _d | _	d| _
d | _d| _d | _d| _d | _d| _d| _d| _d| _d| _d| _d| _|rš|  |¡ d S )N©r?   r@   r   r   F)ÚsuperrY   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   r?   r@   Ú	condition©Ú	__class__r   r   r   ñ   s2    ÿzFilterableStatement.__init__c                 C   s¦   t |tƒr|  t|ƒ ¡ ¡ n„t |tƒr8|  | ¡ ¡ njt |tƒršzt |¡}t |tƒs\t‚W n tyx   t	dƒ‚Y n0 | 
¡ D ]}|  ||| ¡ q‚nt	dƒ‚dS )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   Zas_strÚstrÚjsonÚloadsÚ
ValueErrorr   Úkeys)r   ÚobjÚresÚkeyr   r   r   Ú_bind_single  s    





z FilterableStatement._bind_singlec                 G   s6   d| _ d t|Ž ¡| _t| j| j ƒ ¡ | _d| _| S )zÑSets the sorting criteria.

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

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.
        Tú,)	rn   r,   r   re   r   r6   Zparse_order_specrf   r;   ©r   Zclausesr   r   r   Ú_sort%  s    	ÿ
zFilterableStatement._sortc                 C   sV   d| _ || _zt|| j ƒ}| ¡ | _W n tyB   tdƒ‚Y n0 |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)ro   rg   r   r6   r   rh   rz   r   Zplaceholder_name_to_positionr\   r;   )r   rr   r   r   r   r   rp   5  s    
zFilterableStatement._set_wherec                 G   s:   t |Ž }d| _d |¡| _t| j| j ƒ ¡ | _d| _dS )zJSet group by.

        Args:
            *fields: List of fields.
        Tr€   N)	r   rk   r,   r_   r   r6   Zparse_expr_listr`   r;   ©r   Úfieldsr   r   r   Ú_set_group_byJ  s    ÿ
z!FilterableStatement._set_group_byc                 C   s$   d| _ t|| j ƒ ¡ | _d| _dS )zOSet having.

        Args:
            condition (str): The condition.
        TN)rl   r   r6   r   r^   r;   ©r   rr   r   r   r   Ú_set_havingW  s    zFilterableStatement._set_havingc                 G   s:   t |Ž }d| _d |¡| _t| j| j ƒ ¡ | _d| _| S )z¡Set the projection.

        Args:
            *fields: List of fields.

        Returns:
            :class:`mysqlx.FilterableStatement`: Returns self.
        Tr€   )	r   rm   r,   rc   r   r6   Zparse_table_select_projectionrd   r;   rƒ   r   r   r   Ú_set_projectiona  s    	þ
z#FilterableStatement._set_projectionc                 C   s   | j S ©zlReturns the binding map dictionary.

        Returns:
            dict: The binding map dictionary.
        ©r\   rA   r   r   r   Úget_binding_maps  s    z#FilterableStatement.get_binding_mapc                 C   s   | j S ©z\Returns the bindings list.

        Returns:
            `list`: The bindings list.
        ©r]   rA   r   r   r   Úget_bindings{  s    z FilterableStatement.get_bindingsc                 C   s   | j S )zrReturns the grouping expression list.

        Returns:
            `list`: The grouping expression list.
        )r`   rA   r   r   r   Úget_groupingƒ  s    z FilterableStatement.get_groupingc                 C   s   | j S )zdReturns the having expression.

        Returns:
            object: The having expression.
        )r^   rA   r   r   r   Ú
get_having‹  s    zFilterableStatement.get_havingc                 C   s   | j S )z]Returns the limit row count.

        Returns:
            int: The limit row count.
        )rb   rA   r   r   r   Úget_limit_row_count“  s    z'FilterableStatement.get_limit_row_countc                 C   s   | j S )zWReturns the limit offset.

        Returns:
            int: The limit offset.
        )ra   rA   r   r   r   Úget_limit_offset›  s    z$FilterableStatement.get_limit_offsetc                 C   s   | j S )zbReturns the where expression.

        Returns:
            object: The where expression.
        )rh   rA   r   r   r   Úget_where_expr£  s    z"FilterableStatement.get_where_exprc                 C   s   | j S )zlReturns the projection expression.

        Returns:
            object: The projection expression.
        )rd   rA   r   r   r   Úget_projection_expr«  s    z'FilterableStatement.get_projection_exprc                 C   s   | j S )z`Returns the sort expression.

        Returns:
            object: The sort expression.
        )rf   rA   r   r   r   Úget_sort_expr³  s    z!FilterableStatement.get_sort_exprú8.0.12c                 C   s
   |   |¡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
        ©rp   r†   r   r   r   Úwhere»  s    zFilterableStatement.wherec                 G   s
   | j |Ž S )zñSets the sorting criteria.

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

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.

        .. deprecated:: 8.0.12
        ©r‚   r�   r   r   r   ÚsortÊ  s    zFilterableStatement.sortc                 C   sn   t |tƒr|dk rtdƒ‚| jsBt| jdkƒ| _t| jdk ƒ| _|| _d| _|rj|  	|¡ t
jdtd� | 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   Úintrz   rj   Úboolr:   r;   r=   rb   ÚoffsetÚwarningsÚwarnÚDeprecationWarning)r   Ú	row_countrž   r   r   r   ÚlimitØ  s    
þzFilterableStatement.limitc                 C   s$   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œ   rz   ra   )r   rž   r   r   r   rž   ö  s    zFilterableStatement.offsetc                 G   sN   d| _ t|ƒ}|dkr&|  |d ¡ n$|dkrB|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)ri   r   r   r]   r   )r   ÚargsÚcountr   r   r   rv   	  s    zFilterableStatement.bindc                 C   s   t ‚dS rS   rT   rA   r   r   r   rV   !  s    zFilterableStatement.execute)TN)N)r   r   r   r   r   r   r‚   rp   r…   r‡   rˆ   r‹   rŽ   r�   r�   r‘   r’   r“   r”   r•   r   r˜   rš   r£   rž   rv   rV   Ú__classcell__r   r   rs   r   rY   æ   s2   




rY   c                       sL   e Zd ZdZ‡ fdd„Zedd„ ƒZdd„ Zdd	„ Zd
d„ Z	dd„ Z
‡  ZS )ÚSqlStatementz¬A statement for SQL execution.

    Args:
        connection (mysqlx.connection.Connection): Connection object.
        sql (string): The sql statement to be executed.
    c                    s<   t t| ƒjd dd� || _|| _d | _g | _d| _d| _d S ©NFrZ   )	r[   r©   r   r8   Ú_sqlr\   r]   ri   rj   )r   Ú
connectionÚsqlrs   r   r   r   1  s    zSqlStatement.__init__c                 C   s   | j S )zstring: The SQL text statement.)r«   rA   r   r   r   r­   :  s    zSqlStatement.sqlc                 C   s   | j S r‰   rŠ   rA   r   r   r   r‹   ?  s    zSqlStatement.get_binding_mapc                 C   s   | j S rŒ   r�   rA   r   r   r   rŽ   G  s    zSqlStatement.get_bindingsc                 G   sH   t |ƒdkrtdƒ‚d| _t|Ž }t|ttfƒr8|| _n| j |¡ | S )zµBinds value(s) to a specific placeholder(s).

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

        Returns:
            mysqlx.SqlStatement: SqlStatement object.
        r   r¥   T)	r   r   ri   r   r   r   r   r]   Úappend)r   r¦   Zbindingsr   r   r   rv   O  s    	zSqlStatement.bindc                 C   s   | j  | ¡S )zaExecute the statement.

        Returns:
            mysqlx.SqlResult: SqlResult object.
        )r8   Zsend_sqlrA   r   r   r   rV   b  s    zSqlStatement.execute)r   r   r   r   r   rW   r­   r‹   rŽ   rv   rV   r¨   r   r   rs   r   r©   *  s   	
r©   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚWriteStatementz/Provide common write operation attributes.
    c                    s   t t| ƒ ||¡ g | _d S r   )r[   r¯   r   Ú_valuesr>   rs   r   r   r   n  s    zWriteStatement.__init__c                 C   s   | j S )z^Returns the list of values.

        Returns:
            `list`: The list of values.
        )r°   rA   r   r   r   Ú
get_valuesr  s    zWriteStatement.get_valuesc                 C   s   t ‚dS rS   rT   rA   r   r   r   rV   z  s    zWriteStatement.execute)r   r   r   r   r   r±   rV   r¨   r   r   rs   r   r¯   k  s   r¯   c                       sB   e Zd ZdZ‡ fdd„Zdd„ Zddd„Zd	d
„ Zdd„ Z‡  Z	S )ÚAddStatementz�A statement for document addition on a collection.

    Args:
        collection (mysqlx.Collection): The Collection object.
    c                    s"   t t| ƒ |d¡ d| _g | _d S )NTF)r[   r²   r   Ú_upsertÚids)r   Ú
collectionrs   r   r   r   ‰  s    zAddStatement.__init__c                 C   s   | j S )zhReturns `True` if it's an upsert.

        Returns:
            bool: `True` if it's an upsert.
        ©r³   rA   r   r   r   Ú	is_upsertŽ  s    zAddStatement.is_upsertTc                 C   s
   || _ | S )zÿSets 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¶   rE   r   r   r   Úupsert–  s    zAddStatement.upsertc                 G   s:   t |Ž D ],}t|tƒr$| j |¡ q| j t|ƒ¡ q| S )zÏAdds 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   Úvalr   r   r   Úadd¡  s
    	
zAddStatement.addc                 C   s    t | jƒdkrtƒ S | j | ¡S )ú[Execute the statement.

        Returns:
            mysqlx.Result: Result object.
        r   )r   r°   r	   r8   Úsend_insertrA   r   r   r   rV   ±  s    zAddStatement.execute)T)
r   r   r   r   r   r·   r¸   rº   rV   r¨   r   r   rs   r   r²   ƒ  s   
r²   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )Ú
UpdateSpecz²Update specification class implementation.

    Args:
        update_type (int): The update type.
        source (str): The source.
        value (Optional[str]): The value.
    Nc                 C   sj   |t dƒkr|  ||¡ nL|| _|| _t|ƒdkrL|d dkrL|dd … | _t| jdƒ ¡ j| _|| _d S )Nú*Mysqlx.Crud.UpdateOperation.UpdateType.SETr   ú$r   F)	r
   Ú
_table_setÚupdate_typeÚsourcer   r   Zdocument_fieldr"   rF   )r   rÁ   rÂ   rF   r   r   r   r   Å  s    ÿÿzUpdateSpec.__init__c                 C   s$   t dƒ| _t|dƒ ¡ | _|| _dS )zlTable set.

        Args:
            source (str): The source.
            value (str): The value.
        r¾   TN)r
   rÁ   r   Zparse_table_update_fieldrÂ   rF   )r   rÂ   rF   r   r   r   rÀ   Ò  s
    ÿzUpdateSpec._table_set)N)r   r   r   r   r   rÀ   r   r   r   r   r½   ½  s   
r½   c                       sp   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zed
ƒdd„ ƒZ	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Z‡  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                    s   t t| ƒj||d� i | _d S ©N)r?   rr   )r[   rÃ   r   Ú_update_ops©r   rµ   rr   rs   r   r   r   ê  s    ÿzModifyStatement.__init__c                 G   s
   | j |Ž S )zÉSets the sorting criteria.

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

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.
        r™   r�   r   r   r   rš   ï  s    	zModifyStatement.sortc                 C   s   | j S ©ztReturns the list of update operations.

        Returns:
            `list`: The list of update operations.
        ©rÅ   rA   r   r   r   Úget_update_opsú  s    zModifyStatement.get_update_opsc                 C   s    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Å   r;   ©r   Zdoc_pathrF   r   r   r   Úset  s    
ÿþ
zModifyStatement.setr–   c                 C   s    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Ë   r   r   r   Úchange  s    ÿþ
zModifyStatement.changec                 G   s,   t |Ž D ]}ttdƒ|ƒ| j|< q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Å   r;   )r   Z	doc_pathsÚitemr   r   r   Úunset&  s    
ÿÿzModifyStatement.unsetc                 C   s    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   ÚfieldrF   r   r   r   Úarray_insert6  s    ÿþ
zModifyStatement.array_insertc                 C   s    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Ë   r   r   r   Úarray_appendH  s    ÿþ
zModifyStatement.array_appendc                 C   sX   |du rd}t |ttttfƒs&tdƒ‚ttdƒdt |tƒrB| ¡ 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   ru   r   rw   r   r½   r
   r   rÅ   r;   )r   Údocr   r   r   rÔ   [  s    ÿþ
zModifyStatement.patchc                 C   s   | 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©ro   r   r8   Zsend_updaterA   r   r   r   rV   s  s    	zModifyStatement.execute)r   r   r   r   r   rš   rÉ   rÌ   r   rÍ   rÏ   rÒ   rÓ   rÔ   rV   r¨   r   r   rs   r   rÃ   ß  s   

rÃ   c                       sz   e Zd ZdZd‡ fdd„	Zedd„ ƒZdd	„ Zd
d„ Zdd„ Z	e
jfdd„Ze
jfdd„Zdd„ Zdd„ Zdd„ Z‡  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                    s,   t t| ƒ |||¡ d| _d| _tj| _d S ©NF)r[   rØ   r   Ú_lock_exclusiveÚ_lock_sharedr   ÚDEFAULTÚ_lock_contentionrq   rs   r   r   r   Œ  s    zReadStatement.__init__c                 C   s   | j S )z::class:`mysqlx.LockContention`: The lock contention value.)rÝ   rA   r   r   r   Úlock_contention’  s    zReadStatement.lock_contentionc                 C   s4   zt  |¡}W n ty(   tdƒ‚Y n0 || _dS )zÖSet 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   Úindexrz   r   rÝ   )r   rÞ   Ú_r   r   r   Ú_set_lock_contention—  s
    	z"ReadStatement._set_lock_contentionc                 C   s   | j S )zrReturns `True` if is `EXCLUSIVE LOCK`.

        Returns:
            bool: `True` if is `EXCLUSIVE LOCK`.
        )rÚ   rA   r   r   r   Úis_lock_exclusive¨  s    zReadStatement.is_lock_exclusivec                 C   s   | j S )zlReturns `True` if is `SHARED LOCK`.

        Returns:
            bool: `True` if is `SHARED LOCK`.
        )rÛ   rA   r   r   r   Úis_lock_shared°  s    zReadStatement.is_lock_sharedc                 C   s   d| _ d| _|  |¡ | S )zÅExecute a read operation with `SHARED LOCK`. Only one lock can be
           active at a time.

        Args:
            lock_contention (:class:`mysqlx.LockContention`): Lock contention.
        FT©rÚ   rÛ   rá   ©r   rÞ   r   r   r   Úlock_shared¸  s    
zReadStatement.lock_sharedc                 C   s   d| _ d| _|  |¡ | S )zÈExecute a read operation with `EXCLUSIVE LOCK`. Only one lock can be
           active at a time.

        Args:
            lock_contention (:class:`mysqlx.LockContention`): Lock contention.
        TFrä   rå   r   r   r   Úlock_exclusiveÄ  s    
zReadStatement.lock_exclusivec                 G   s   | j |Ž  | S )zÜSets a grouping criteria for the resultset.

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

        Returns:
            mysqlx.ReadStatement: ReadStatement object.
        )r…   rƒ   r   r   r   Úgroup_byÐ  s    	
zReadStatement.group_byc                 C   s   |   |¡ | 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†   r   r   r   ÚhavingÜ  s    
zReadStatement.havingc                 C   s   | j  | ¡S ©r»   )r8   Z	send_findrA   r   r   r   rV   ê  s    zReadStatement.execute)TN)r   r   r   r   r   rW   rÞ   rá   râ   rã   r   rÜ   ræ   rç   rè   ré   rV   r¨   r   r   rs   r   rØ   �  s   

rØ   c                       s2   e Zd ZdZd	‡ fdd„	Zdd„ Zdd„ Z‡  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                    s   t t| ƒ |d|¡ d S )NT)r[   rë   r   rÆ   rs   r   r   r   ý  s    zFindStatement.__init__c                 G   s
   | j |Ž S )zèSets a document field filter.

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

        Returns:
            mysqlx.FindStatement: FindStatement object.
        )rˆ   rƒ   r   r   r   r„      s    
zFindStatement.fieldsc                 G   s
   | j |Ž S ©zÅSets the sorting criteria.

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

        Returns:
            mysqlx.FindStatement: FindStatement object.
        r™   r�   r   r   r   rš     s    	zFindStatement.sort)N)r   r   r   r   r   r„   rš   r¨   r   r   rs   r   rë   ó  s   	rë   c                       s8   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡  ZS )
ÚSelectStatementz¤A statement for record retrieval operations on a Table.

    Args:
        table (mysqlx.Table): The Table object.
        *fields: The fields to be retrieved.
    c                    s    t t| ƒ |d¡ | j|Ž  d S rÙ   )r[   rí   r   rˆ   ©r   Útabler„   rs   r   r   r     s    zSelectStatement.__init__c                 C   s
   |   |¡S )zÔSets the search condition to filter.

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

        Returns:
            mysqlx.SelectStatement: SelectStatement object.
        r—   r†   r   r   r   r˜   #  s    	zSelectStatement.wherec                 G   s
   | j |Ž S )zÎSets the order by criteria.

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

        Returns:
            mysqlx.SelectStatement: SelectStatement object.
        r™   r�   r   r   r   Úorder_by.  s    	zSelectStatement.order_byc              
   C   sž   | j rd | j¡nd}| jr(d | j¡nd}| jr>d | j¡nd}| jrTd | j¡nd}| j	rnd | j
| j¡nd}dj| jp~d| jj| jj|||||d	�}|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}Ú*)ÚselectrB   rï   r£   r˜   Úgroupré   Úorder)ro   r%   rg   rk   r_   rl   r^   rn   re   rj   rb   ra   rc   rB   Únamer?   )r   r˜   rè   ré   rð   r£   Ústmtr   r   r   Úget_sql9  s.    ÿÿÿþ
ÿþûzSelectStatement.get_sql)	r   r   r   r   r   r˜   rð   r÷   r¨   r   r   rs   r   rí     s
   rí   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚInsertStatementz—A statement for insert operations on Table.

    Args:
        table (mysqlx.Table): The Table object.
        *fields: The fields to be inserted.
    c                    s    t t| ƒ |d¡ t|Ž | _d S rÙ   )r[   rø   r   r   Ú_fieldsrî   rs   r   r   r   X  s    zInsertStatement.__init__c                 G   s   | j  tt|Ž ƒ¡ | S )zÃSet 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   r   r   r   r   \  s    	zInsertStatement.valuesc                 C   s   | j  | ¡S rê   )r8   r¼   rA   r   r   r   rV   h  s    zInsertStatement.execute)r   r   r   r   r   r   rV   r¨   r   r   rs   r   rø   Q  s   rø   c                       sH   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	‡  Z
S )ÚUpdateStatementzÃA 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                    s   t t| ƒj|dd� i | _d S rª   )r[   rú   r   rÅ   ©r   rï   rs   r   r   r   z  s    zUpdateStatement.__init__c                 C   s
   |   |¡S )zÔSets the search condition to filter.

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

        Returns:
            mysqlx.UpdateStatement: UpdateStatement object.
        r—   r†   r   r   r   r˜   ~  s    	zUpdateStatement.wherec                 G   s
   | j |Ž S )zÎSets the order by criteria.

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

        Returns:
            mysqlx.UpdateStatement: UpdateStatement object.
        r™   r�   r   r   r   rð   ‰  s    	zUpdateStatement.order_byc                 C   s   | j S rÇ   rÈ   rA   r   r   r   rÉ   ”  s    zUpdateStatement.get_update_opsc                 C   s    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Ê   rÐ   r   r   r   rÌ   œ  s    
ÿÿ
zUpdateStatement.setc                 C   s   | j stdƒ‚| j | ¡S )z£Execute the statement.

        Returns:
            mysqlx.Result: Result object

        Raises:
            ProgrammingError: If condition was not set.
        z!No condition was found for updater×   rA   r   r   r   rV   «  s    	zUpdateStatement.execute)r   r   r   r   r   r˜   rð   rÉ   rÌ   rV   r¨   r   r   rs   r   rú   q  s   rú   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  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                    s   t t| ƒj||d� d S rÄ   )r[   rü   r   rÆ   rs   r   r   r   Ä  s    ÿzRemoveStatement.__init__c                 G   s
   | j |Ž S rì   r™   r�   r   r   r   rš   È  s    	zRemoveStatement.sortc                 C   s   | j stdƒ‚| j | ¡S )rÖ   z!No condition was found for remove©ro   r   r8   Zsend_deleterA   r   r   r   rV   Ó  s    	zRemoveStatement.execute)r   r   r   r   r   rš   rV   r¨   r   r   rs   r   rü   ¹  s   
rü   c                       s8   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡  ZS )
ÚDeleteStatementz¯A statement that drops a table.

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

    .. versionchanged:: 8.0.12
       The ``condition`` parameter was removed.
    c                    s   t t| ƒj|dd� d S rª   )r[   rþ   r   rû   rs   r   r   r   ê  s    zDeleteStatement.__init__c                 C   s
   |   |¡S )zÔSets the search condition to filter.

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

        Returns:
            mysqlx.DeleteStatement: DeleteStatement object.
        r—   r†   r   r   r   r˜   í  s    	zDeleteStatement.wherec                 G   s
   | j |Ž S )zÎSets the order by criteria.

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

        Returns:
            mysqlx.DeleteStatement: DeleteStatement object.
        r™   r�   r   r   r   rð   ø  s    	zDeleteStatement.order_byc                 C   s   | j stdƒ‚| j | ¡S )rÖ   z!No condition was found for deleterý   rA   r   r   r   rV     s    	zDeleteStatement.execute)	r   r   r   r   r   r˜   rð   rV   r¨   r   r   rs   r   rþ   á  s
   rþ   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  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                    s8   t t| ƒj|d� t |¡| _|| _| j dg ¡| _d S )N)r?   r„   )	r[   rÿ   r   ÚcopyÚdeepcopyÚ_index_descÚ_index_nameÚpopÚ_fields_desc)r   rµ   Z
index_nameZ
index_descrs   r   r   r   '  s    z'CreateCollectionIndexStatement.__init__c              
   C   sP  | j du rtt | j ¡ƒ‚z`t| j ƒ ¡  ¡ }t|tƒrZ|d t	dƒkrxtt | j ¡ƒ‚n|j
t	dƒkrxtt | j ¡ƒ‚W n& ttfy    tt | j ¡ƒ‚Y n0 | 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¡ƒ‚�z„| jD �]v}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 �rl|d  ¡  d¡�s^td |d  ¡ ¡ƒ‚| d¡|d< d|v �rž|d  ¡ dk�r�tdƒ‚| d¡|d< d|v �rÐ|d  ¡ dk�rÂtdƒ‚| d¡|d< |d  |¡ �qhW n6 t�y } ztd  ||¡ƒ‚W Y d}~n
d}~0 0 | jD ]}|�r"td! |¡ƒ‚�q"| 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.rõ   rµ   rB   ZINDEXÚuniqueFz!Unique indexes are not supported.Ú
constraintzUnidentified fields: {}rÑ   ÚmemberÚrequiredÚarrayz'Field member 'required' must be Booleanz$Field member 'array' must be BooleanZSPATIALzQField member 'required' must be set to 'True' when index type is set to 'SPATIAL'ZGEOJSONzIIndex 'type' must be set to 'SPATIAL' when field type is set to 'GEOJSON'Z	collationZ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'Z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:{}ZmysqlxZcreate_collection_indexT)r  r   ÚERR_INVALID_INDEX_NAMEr%   r   r   Zget_messager   ru   r
   r  rz   ÚAttributeErrorr  r  r   r5   rõ   rB   r  r   r�   Ú	TypeErrorÚupperÚ
startswithr®   ÚKeyErrorr8   Zexecute_nonquery)r   Zparsed_identr¦   Z
field_descr  Úerrr   r   r   rV   -  sÈ    

ÿ
ÿ
ÿÿ
ÿ
ÿ
þ

ÿÿÿ
ÿÿ

þÿ
ÿ
ÿÿ
ÿ
ÿz&CreateCollectionIndexStatement.execute)r   r   r   r   r   rV   r¨   r   r   rs   r   rÿ     s   rÿ   )r   )r   )r   )r   )*r   r   rx   rŸ   Úerrorsr   r   r   r   Ú	constantsr   Zdbdocr   Úhelpersr   Úresultr	   Úprotobufr
   r  Úobjectr   r   r$   r'   r-   r3   r4   rY   r©   r¯   r²   r½   rÃ   rØ   rë   rí   rø   rú   rü   rþ   rÿ   r   r   r   r   Ú<module>   sF   



i  FA:" #r%9 H(0