B
    Ñš[O¼  ã               @   s  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ dd	lmZ dd
lmZmZmZ G dd„ deƒZdd„ ZdDdd„ZdEdd„ZdFdd„ZdGd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.d/„ d/eƒZ'G d0d1„ d1eƒZ(G d2d3„ d3eƒZ)G d4d5„ d5eƒZ*G d6d7„ d7eƒZ+G d8d9„ d9e+ƒZ,G d:d;„ d;eƒZ-G d<d=„ d=eƒZ.G d>d?„ d?e.ƒZ/G d@dA„ dAe/ƒZ0G dBdC„ dCeƒZ1dS )HzImplementation of Statements.é    Né   )ÚProgrammingError)Ú
ExprParser)ÚSTRING_TYPES)Ú
AlgorithmsÚ
Securities)ÚDbDoc)Úmysqlx_crud_pb2)Ú	SqlResultÚResultÚ
ColumnTypec               @   s   e Zd Zdd„ ZdS )ÚExprc             C   s
   || _ d S )N)Úexpr)Úselfr   © r   ú”C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-ioykiiwb\mysql-connector-python-rf\mysql_connector_python_rf-2.2.2.data\purelib\mysqlx\statement.pyÚ__init__&   s    zExpr.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   %   s   r   c              G   s*   t | ƒdkr&t| d ttfƒr&| d S | S )Nr   r   )ÚlenÚ
isinstanceÚlistÚtuple)Úvaluesr   r   r   Úflexible_params*   s    r   Ú c             C   sT   d|k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 d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   ú`éÿÿÿÿú"Nr   )Ú
identifierÚsql_moder   r   r   Úis_quoted_identifier0   s    
r#   c             C   s>   t | |ƒr| S d|kr(d |  dd¡¡S d |  dd¡¡S dS )	aŠ  Quote the given identifier with backticks, converting backticks (`) in
    the identifier name with the correct escape sequence (``) unless the
    identifier is quoted (") as in sql_mode set to ANSI_QUOTES.

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

    Returns:
        A string with the identifier quoted with backticks.
    r   z"{0}"r    z""z`{0}`r   z``N)r#   ÚformatÚreplace)r!   r"   r   r   r   Úquote_identifierA   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>_   s   z.quote_multipart_identifier.<locals>.<listcomp>)Újoin)Zidentifiersr"   r   )r"   r   Úquote_multipart_identifierU   s    
r+   c             C   s^   d|krdnd}||kr"d  |¡nd}| |d¡}t|ƒdkrB| n|d  |¡|d  |¡fS )	Nr   r    r   z.{0}r'   r   r   r   )r$   Úsplitr   Ústrip)Zdefault_schemaÚ
table_namer"   ÚquoteÚ	delimiterÚtempr   r   r   Úparse_table_namec   s
    r2   c               @   s:   e Zd ZdZddd„Zedd„ ƒZe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|jnd | _d S )N)Ú_targetÚ
_doc_basedÚ_connection)r   ÚtargetÚ	doc_basedr   r   r   r   s   s    zStatement.__init__c             C   s   | j S )z,object: The database object target.
        )r4   )r   r   r   r   r7   x   s    zStatement.targetc             C   s   | j jS )z3:class:`mysqlx.Schema`: The Schema object.
        )r4   Úschema)r   r   r   r   r9   ~   s    zStatement.schemac             C   s   t ‚dS )zqExecute the statement.

        Raises:
           NotImplementedError: This method must be implemented.
        N)ÚNotImplementedError)r   r   r   r   Úexecute„   s    zStatement.executeN)T)	r   r   r   Ú__doc__r   Úpropertyr7   r9   r;   r   r   r   r   r3   k   s
   
r3   c                   sl   e Zd ZdZd‡ fdd„	Zdd„ Zdd	„ Zddd„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Z‡  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                s`   t t| ƒj||d� d| _d| _d| _d| _d| _d| _d| _	i | _
g | _|d k	r\|  |¡ d S )N)r7   r8   F)Úsuperr>   r   Ú_has_projectionÚ
_has_whereÚ
_has_limitÚ	_has_sortÚ_has_group_byÚ_has_havingÚ_has_bindingsÚ_binding_mapÚ	_bindingsÚwhere)r   r7   r8   Ú	condition)Ú	__class__r   r   r   ˜   s    zFilterableStatement.__init__c             C   s0   d| _ || _t|| j ƒ}| ¡ | _|j| _| 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.
        T)rA   Ú_wherer   r5   r   Z_where_exprZplaceholder_name_to_positionrG   )r   rJ   r   r   r   r   rI   §   s    

zFilterableStatement.wherec             G   s4   t |Ž }d| _d |¡| _t| j| j ƒ ¡ | _| S )NTú,)r   r@   r*   Ú_projection_strr   r5   Zparse_table_select_projectionZ_projection_expr)r   Úfieldsr   r   r   Ú_projection¸   s    zFilterableStatement._projectionr   c             C   s   d| _ || _|| _| S )aE  Sets the maximum number of records or documents to be returned.

        Args:
            row_count (int): The maximum number of records or documents.
            offset (Optional[int]) The number of records or documents to skip.

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.
        T)rB   Ú_limit_offsetÚ_limit_row_count)r   Z	row_countÚoffsetr   r   r   ÚlimitÀ   s    
zFilterableStatement.limitc             G   s4   t |Ž }d| _d |¡| _t| j| j ƒ ¡ | _| S )zÖSets the sorting criteria.

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

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.
        TrM   )r   rC   r*   Ú	_sort_strr   r5   Zparse_order_specZ
_sort_expr)r   Zsort_clausesr   r   r   ÚsortÏ   s    	zFilterableStatement.sortc             G   s4   t |Ž }d| _d |¡| _t| j| j ƒ ¡ | _d S )NTrM   )r   rD   r*   Ú_grouping_strr   r5   Zparse_expr_listZ	_grouping)r   rO   r   r   r   Ú	_group_byß   s
    zFilterableStatement._group_byc             C   s   d| _ t|| j ƒ ¡ | _d S )NT)rE   r   r5   r   Ú_having)r   rJ   r   r   r   rY   æ   s    zFilterableStatement._havingc             G   sV   d| _ t|ƒ}|dkr&|  |d ¡ n,|dkr8tdƒ‚n| j |d |d dœ¡ | S )a¤  Binds a value to a specific placeholder.

        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   é   z#Invalid number of arguments to bind)ÚnameÚvalue)rF   r   Ú_bind_singler   rH   Úappend)r   ÚargsÚcountr   r   r   Úbindê   s    
zFilterableStatement.bindc             C   sT   t |tƒr|  t|ƒ¡ n6t |tƒrPt |¡}x | ¡ D ]}|  ||| ¡ q8W d S )N)r   r   ra   Ústrr   ÚjsonÚloadsÚkeys)r   ÚobjectÚdictÚkeyr   r   r   r]     s    


z FilterableStatement._bind_singlec             C   s   t ‚dS )zqExecute the statement.

        Raises:
           NotImplementedError: This method must be implemented.
        N)r:   )r   r   r   r   r;   
  s    zFilterableStatement.execute)TN)r   )r   r   r   r<   r   rI   rP   rT   rV   rX   rY   ra   r]   r;   Ú__classcell__r   r   )rK   r   r>   �   s   

r>   c                   s(   e Zd ZdZ‡ f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 S )NF)r7   r8   )r?   rj   r   r6   Ú_sql)r   Ú
connectionÚsql)rK   r   r   r     s    zSqlStatement.__init__c             C   s   | j  | j¡ t| j ƒS )zaExecute the statement.

        Returns:
            mysqlx.SqlResult: SqlResult object.
        )r6   Zsend_sqlrk   r
   )r   r   r   r   r;     s    zSqlStatement.execute)r   r   r   r<   r   r;   ri   r   r   )rK   r   rj     s   rj   c                   s0   e Zd ZdZ‡ f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| ƒj|d� g | _g | _d S )N)r7   )r?   rn   r   Ú_valuesÚ_ids)r   Ú
collection)rK   r   r   r   /  s    zAddStatement.__init__c             G   s>   x8t |Ž D ],}t|tƒr&| j |¡ q
| j t|ƒ¡ q
W | 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   ro   r^   )r   r   Úvalr   r   r   Úadd4  s
    	
zAddStatement.addc             C   s@   t | jƒdkrtƒ S x| jD ]}| j | ¡ ¡ qW | j | ¡S )z[Execute the statement.

        Returns:
            mysqlx.Result: Result object.
        r   )r   ro   r   rp   r^   Z	ensure_idr6   Úsend_insert)r   Údocr   r   r   r;   D  s
    zAddStatement.execute)r   r   r   r<   r   rs   r;   ri   r   r   )rK   r   rn   )  s   rn   c               @   s   e Zd Zddd„Zdd„ ZdS )Ú
UpdateSpecNc             C   sj   |t jjkr|  ||¡ nL|| _|| _t|ƒdkrL|d dkrL|dd … | _t| jdƒ ¡ j	| _|| _
d S )Nr   ú$r   F)Ú
MySQLxCrudÚUpdateOperationÚSETÚ
_table_setÚupdate_typeÚsourcer   r   Zdocument_fieldr!   r\   )r   r|   r}   r\   r   r   r   r   T  s    zUpdateSpec.__init__c             C   s$   t jj| _t|dƒ ¡ | _|| _d S )NT)rx   ry   rz   r|   r   Zparse_table_update_fieldr}   r\   )r   r}   r\   r   r   r   r{   `  s    
zUpdateSpec._table_set)N)r   r   r   r   r{   r   r   r   r   rv   S  s   
rv   c                   sR   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
‡  ZS )ÚModifyStatementa  A statement for document update operations on a Collection.

    Args:
        collection (mysqlx.Collection): The Collection object.
        condition (Optional[str]): Sets the search condition to identify the
                                   documents to be updated.
    Nc                s   t t| ƒj||d� g | _d S )N)r7   rJ   )r?   r~   r   Ú_update_ops)r   rq   rJ   )rK   r   r   r   n  s    zModifyStatement.__init__c             C   s   | j  ttjj||ƒ¡ | 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.
        )r   r^   rv   rx   ry   ZITEM_SET)r   Údoc_pathr\   r   r   r   Úsets  s    
zModifyStatement.setc             C   s   | j  ttjj||ƒ¡ | S )aj  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.
        )r   r^   rv   rx   ry   ZITEM_REPLACE)r   r€   r\   r   r   r   Úchange�  s    
zModifyStatement.changec             G   s   | j  dd„ t|Ž D ƒ¡ | S )a  Removes attributes from documents in a collection.

        Args:
            doc_path (string): The document path of the attribute to be
                               removed.

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.
        c             S   s   g | ]}t tjj|ƒ‘qS r   )rv   rx   ry   ZITEM_REMOVE)r(   Úxr   r   r   r)   œ  s   z)ModifyStatement.unset.<locals>.<listcomp>)r   Úextendr   )r   Z	doc_pathsr   r   r   Úunset‘  s    
zModifyStatement.unsetc             C   s   | j  ttjj||ƒ¡ | 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.
        )r   r^   rv   rx   ry   ZARRAY_INSERT)r   Úfieldr\   r   r   r   Úarray_insert   s    zModifyStatement.array_insertc             C   s   | j  ttjj||ƒ¡ | 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.
        )r   r^   rv   rx   ry   ZARRAY_APPEND)r   r€   r\   r   r   r   Úarray_append°  s    
zModifyStatement.array_appendc             C   s   | j  | ¡S )z[Execute the statement.

        Returns:
            mysqlx.Result: Result object.
        )r6   Úupdate)r   r   r   r   r;   Â  s    zModifyStatement.execute)N)r   r   r   r<   r   r�   r‚   r…   r‡   rˆ   r;   ri   r   r   )rK   r   r~   f  s   r~   c                   sB   e Zd ZdZd‡ fdd„	Zdd„ Z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   rq   rJ   )rK   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.
        )rP   )r   rO   r   r   r   rO   Ø  s    
zFindStatement.fieldsc             G   s   | j |Ž  | S )zÜSets a grouping criteria for the resultset.

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

        Returns:
            mysqlx.FindStatement: FindStatement object.
        )rX   )r   rO   r   r   r   Úgroup_byä  s    	
zFindStatement.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.FindStatement: FindStatement object.
        )rY   )r   rJ   r   r   r   Úhavingð  s    
zFindStatement.havingc             C   s   | j  | ¡S )zaExecute the statement.

        Returns:
            mysqlx.DocResult: DocResult object.
        )r6   Úfind)r   r   r   r   r;   þ  s    zFindStatement.execute)N)
r   r   r   r<   r   rO   r‹   rŒ   r;   ri   r   r   )rK   r   rŠ   Ë  s   	rŠ   c                   s@   e Zd ZdZ‡ fdd„Z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 )NF)r?   rŽ   r   rP   )r   ÚtablerO   )rK   r   r   r     s    zSelectStatement.__init__c             G   s   | j |Ž  | S )zÔSets a grouping criteria for the resultset.

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

        Returns:
            mysqlx.SelectStatement: SelectStatement object.
        )rX   )r   rO   r   r   r   r‹     s    	
zSelectStatement.group_byc             C   s   |   |¡ | S )a;  Sets a condition for records to be considered in agregate function
        operations.

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

        Returns:
            mysqlx.SelectStatement: SelectStatement object.
        )rY   )r   rJ   r   r   r   rŒ     s    
zSelectStatement.havingc             C   s   | j  | ¡S )zaExecute the statement.

        Returns:
            mysqlx.RowResult: RowResult object.
        )r6   r�   )r   r   r   r   r;   ,  s    zSelectStatement.executec          
   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t| dd	ƒ| jj| jj|||||d
�}|S )Nz
 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}rN   Ú*)Úselectr9   r�   rT   rI   ÚgrouprŒ   Úorder)rA   r$   rL   rD   rW   rE   rY   rC   rU   rB   rR   rQ   Úgetattrr9   r[   r7   )r   rI   r‹   rŒ   Zorder_byrT   Ústmtr   r   r   Úget_sql4  s    
zSelectStatement.get_sql)
r   r   r   r<   r   r‹   rŒ   r;   r–   ri   r   r   )rK   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| ƒj|dd� t|Ž | _g | _d S )NF)r7   r8   )r?   r—   r   r   Ú_fieldsro   )r   r�   rO   )rK   r   r   r   M  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.
        )ro   r^   r   r   )r   r   r   r   r   r   R  s    	zInsertStatement.valuesc             C   s   | j  | ¡S )z[Execute the statement.

        Returns:
            mysqlx.Result: Result object.
        )r6   rt   )r   r   r   r   r;   ^  s    zInsertStatement.execute)r   r   r   r<   r   r   r;   ri   r   r   )rK   r   r—   F  s   r—   c                   s0   e Zd ZdZ‡ f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.
        *fields: The fields to be updated.
    c                s   t t| ƒj|dd� g | _d S )NF)r7   r8   )r?   r™   r   r   )r   r�   rO   )rK   r   r   r   n  s    zUpdateStatement.__init__c             C   s   | j  ttjj||ƒ¡ | 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   r^   rv   rx   ry   rz   )r   r†   r\   r   r   r   r�   r  s    
zUpdateStatement.setc             C   s   | j  | ¡S )zZExecute the statement.

        Returns:
            mysqlx.Result: Result object
        )r6   r‰   )r   r   r   r   r;   €  s    zUpdateStatement.execute)r   r   r   r<   r   r�   r;   ri   r   r   )rK   r   r™   g  s   r™   c                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚRemoveStatementz‚A statement for document removal from a collection.

    Args:
        collection (mysqlx.Collection): The Collection object.
    c                s   t t| ƒj|d� d S )N)r7   )r?   rš   r   )r   rq   )rK   r   r   r   �  s    zRemoveStatement.__init__c             C   s   | j  | ¡S )z[Execute the statement.

        Returns:
            mysqlx.Result: Result object.
        )r6   Údelete)r   r   r   r   r;   ’  s    zRemoveStatement.execute)r   r   r   r<   r   r;   ri   r   r   )rK   r   rš   ‰  s   rš   c                   s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )ÚDeleteStatementzæA statement that drops a table.

    Args:
        table (mysqlx.Table): The Table object.
        condition (Optional[str]): The string with the filter expression of
                                   the rows to be deleted.
    Nc                s   t t| ƒj||dd� d S )NF)r7   rJ   r8   )r?   rœ   r   )r   r�   rJ   )rK   r   r   r   £  s    zDeleteStatement.__init__c             C   s   | j  | ¡S )z[Execute the statement.

        Returns:
            mysqlx.Result: Result object.
        )r6   r›   )r   r   r   r   r;   ¨  s    zDeleteStatement.execute)N)r   r   r   r<   r   r;   ri   r   r   )rK   r   rœ   ›  s   rœ   c                   s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚCreateCollectionIndexStatementzØA statement that creates an index on a collection.

    Args:
        collection (mysqlx.Collection): Collection.
        index_name (string): Index name.
        is_unique (bool): `True` if the index is unique.
    c                s(   t t| ƒj|d� || _|| _g | _d S )N)r7   )r?   r�   r   Ú_index_nameÚ
_is_uniquer˜   )r   rq   Ú
index_nameZ	is_unique)rK   r   r   r   ¹  s    z'CreateCollectionIndexStatement.__init__c             C   s   | j  |||f¡ | S )a�  Add the field specification to this index creation statement.

        Args:
            document_path (string): The document path.
            column_type (string): The column type.
            is_required (bool): `True` if the field is required.

        Returns:
            mysqlx.CreateCollectionIndexStatement:                                    CreateCollectionIndexStatement object.
        )r˜   r^   )r   Zdocument_pathZcolumn_typeZis_requiredr   r   r   r†   ¿  s    z$CreateCollectionIndexStatement.fieldc             C   s<   dd„ | j D ƒ}| jjddd| jjj| jj| j| jf|žŽ S )z[Execute the statement.

        Returns:
            mysqlx.Result: Result object.
        c             S   s   g | ]}|D ]}|‘qqS r   r   )r(   ZsublistÚitemr   r   r   r)   Ô  s    z:CreateCollectionIndexStatement.execute.<locals>.<listcomp>ÚxpluginZcreate_collection_indexT)r˜   r6   Úexecute_nonqueryr4   r9   r[   rž   rŸ   )r   rO   r   r   r   r;   Î  s
    z&CreateCollectionIndexStatement.execute)r   r   r   r<   r   r†   r;   ri   r   r   )rK   r   r�   ±  s   r�   c                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚDropCollectionIndexStatementz¬A statement that drops an index on a collection.

    Args:
        collection (mysqlx.Collection): The Collection object.
        index_name (string): The index name.
    c                s   t t| ƒj|d� || _d S )N)r7   )r?   r¤   r   rž   )r   rq   r    )rK   r   r   r   â  s    z%DropCollectionIndexStatement.__init__c             C   s"   | j  ddd| jjj| jj| j¡S )z[Execute the statement.

        Returns:
            mysqlx.Result: Result object.
        r¢   Zdrop_collection_indexT)r6   r£   r4   r9   r[   rž   )r   r   r   r   r;   æ  s    z$DropCollectionIndexStatement.execute)r   r   r   r<   r   r;   ri   r   r   )rK   r   r¤   Û  s   r¤   c               @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )Ú
TableIndexr   rZ   c             C   s   || _ || _|| _d S )N)Ú_nameÚ_index_typeÚ_columns)r   r[   Z
index_typeÚcolumnsr   r   r   r   ô  s    zTableIndex.__init__c             C   s6   d}| j tjkr|d7 }|d7 }| | jd | j¡¡S )Nr   zUNIQUE zINDEX {0} ({1})rM   )r§   r¥   ÚUNIQUE_INDEXr$   r¦   r*   r¨   )r   r•   r   r   r   r–   ù  s
    zTableIndex.get_sqlN)r   r   r   rª   ÚINDEXr   r–   r   r   r   r   r¥   ñ  s   r¥   c                   sZ   e Zd ZdZd‡ 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‡  ZS )ÚCreateViewStatementz•A statement for creating views.

    Args:
        view (mysqlx.View): The View object.
        replace (Optional[bool]): `True` to add replace.
    Fc                s\   t t| ƒj|dd� || _|j| _|j| _|| _g | _	t
j| _tj| _d | _d | _d | _d S )NF)r7   r8   )r?   r¬   r   Ú_viewr9   Ú_schemar[   r¦   Ú_replacer¨   r   Ú	UNDEFINEDÚ
_algorithmr   ZDEFINERÚ	_securityÚ_definerÚ_defined_asÚ_check_option)r   Úviewr%   )rK   r   r   r     s    zCreateViewStatement.__init__c             C   s   dd„ |D ƒ| _ | S )zºSets the column names.

        Args:
            columns (list): The list of column names.

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        c             S   s   g | ]}t |ƒ‘qS r   )r&   )r(   Úcolr   r   r   r)     s    z/CreateViewStatement.columns.<locals>.<listcomp>)r¨   )r   r©   r   r   r   r©     s    	zCreateViewStatement.columnsc             C   s
   || _ | S )z¹Sets the algorithm.

        Args:
            mysqlx.constants.ALGORITHMS: The algorithm.

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        )r±   )r   Ú	algorithmr   r   r   r¸   !  s    	zCreateViewStatement.algorithmc             C   s
   || _ | S )zÉSets the SQL security mode.

        Args:
            mysqlx.constants.SECURITIES: The SQL security mode.

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        )r²   )r   Úsecurityr   r   r   r¹   -  s    	zCreateViewStatement.securityc             C   s
   || _ | S )zªSets the definer.

        Args:
            definer (string): The definer.

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        )r³   )r   Údefinerr   r   r   rº   9  s    	zCreateViewStatement.definerc             C   s
   || _ | S )zåSets the SelectStatement statement for describing the view.

        Args:
            mysqlx.SelectStatement: SelectStatement object.

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        )r´   )r   Z	statementr   r   r   Ú
defined_asE  s    	zCreateViewStatement.defined_asc             C   s
   || _ | S )zÂSets the check option.

        Args:
            mysqlx.constants.CHECK_OPTIONS: The check option.

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        )rµ   )r   Úcheck_optionr   r   r   Úwith_check_optionQ  s    	z%CreateViewStatement.with_check_optionc          
   C   sœ   | j r
dnd}| jr d | j¡nd}| jr<d d | j¡¡nd}t| jj| jfƒ}| j	rdd | j	¡nd}dj|| j
|| j||| j|d�}| j d	|¡ | jS )
zhExecute the statement to create a view.

        Returns:
            mysqlx.View: View object.
        z OR REPLACEr   z DEFINER = {0}z ({0})z, z WITH {0} CHECK OPTIONz€CREATE{replace} ALGORITHM = {algorithm}{definer} SQL SECURITY {security} VIEW {view_name}{columns} AS {defined_as}{check_option})r%   r¸   rº   r¹   Ú	view_namer©   r»   r¼   rm   )r¯   r³   r$   r¨   r*   r+   r®   r[   r¦   rµ   r±   r²   r´   r6   r£   r­   )r   r%   rº   r©   r¾   r¼   rm   r   r   r   r;   ]  s    zCreateViewStatement.execute)F)r   r   r   r<   r   r©   r¸   r¹   rº   r»   r½   r;   ri   r   r   )rK   r   r¬     s   r¬   c                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚAlterViewStatementzYA statement for alter views.

    Args:
        view (mysqlx.View): The View object.
    c                s   t t| ƒ |¡ d S )N)r?   r¿   r   )r   r¶   )rK   r   r   r   ~  s    zAlterViewStatement.__init__c          	   C   sŒ   | j rd | j ¡nd}| jr.d d | j¡¡nd}t| jj| jfƒ}| jrVd | j¡nd}dj| j	|| j
||| j|d�}| j d|¡ | jS )	zgExecute the statement to alter a view.

        Returns:
            mysqlx.View: View object.
        z DEFINER = {0}r   z ({0})z, z WITH {0} CHECK OPTIONzvALTER ALGORITHM = {algorithm}{definer} SQL SECURITY {security} VIEW {view_name}{columns} AS {defined_as}{check_option})r¸   rº   r¹   r¾   r©   r»   r¼   rm   )r³   r$   r¨   r*   r+   r®   r[   r¦   rµ   r±   r²   r´   r6   r£   r­   )r   rº   r©   r¾   r¼   rm   r   r   r   r;   �  s    zAlterViewStatement.execute)r   r   r   r<   r   r;   ri   r   r   )rK   r   r¿   x  s   r¿   c                   s²   e Zd ZdZe dej¡Z‡ fdd„Ze	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‡  ZS )%ÚCreateTableStatementzÁA statement that creates a new table if it doesn't exist already.

    Args:
        collection (mysqlx.Schema): The Schema object.
        table_name (string): The name for the new table.
    z$(from\s+)([`\"].+[`\"]|[^\.]+)(\s|$)c                st   t t| ƒ |¡ d | _d | _d | _d | _d | _d| _g | _	g | _
g | _g | _g | _d| _|| _d | j ¡ ¡| _d S )NFr   z
\1{0}.\2\3)r?   rÀ   r   Ú_charsetÚ
_collationÚ_commentÚ_asÚ_likeÚ_tempr¨   Ú_f_keysÚ_indicesÚ_p_keysÚ
_u_indicesÚ	_auto_incr¦   r$   r9   Úget_nameÚ	_tbl_repl)r   r9   r.   )rK   r   r   r   ¡  s    zCreateTableStatement.__init__c             C   s   t t| jj| jƒƒS )z7string: The fully qualified name of the Table.
        )r+   r2   r9   r[   r¦   )r   r   r   r   r.   ³  s    zCreateTableStatement.table_namec             C   sb   g }|  d¡ | jr|  d¡ | jr.|  d¡ | jr>|  d¡ d |¡}|j| j| j| j| jd�S )NzAUTO_INCREMENT = {inc}z!DEFAULT CHARACTER SET = {charset}zDEFAULT COLLATE = {collation}zCOMMENT = '{comment}'rM   )ÚincÚcharsetÚ	collationÚcomment)r^   rÁ   rÂ   rÃ   r*   r$   rË   )r   ÚoptionsÚ
table_optsr   r   r   Ú_get_table_optsº  s    




z$CreateTableStatement._get_table_optsc             C   s¤   g }| j r"| d d | j ¡¡¡ x| jD ]}| | ¡ ¡ q*W x| jD ]}| | ¡ ¡ qHW x| jD ]}| | ¡ ¡ qfW x| jD ]}| | ¡ ¡ q„W d |¡S )NzPRIMARY KEY ({0})rM   )	rÉ   r^   r$   r*   r¨   r–   rÇ   rÈ   rÊ   )r   Zdefsr·   rh   Úindexr   r   r   Ú_get_create_defÈ  s    z$CreateTableStatement._get_create_defc             C   s   t t| jj|ƒƒ| _| S )zæCreate table with the definition of another existing Table.

        Args:
            table_name (string): Name of the source table.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )r+   r2   r9   r[   rÅ   )r   r.   r   r   r   Úlike×  s    	zCreateTableStatement.likec             C   s6   t |tƒrtj | j|¡| _nt |tƒr2| ¡ | _| S )a.  Create the Table and fill it with values from a Select Statement.

        Args:
            select (object): Select Statement. Can be a string or an instance of
            :class`mysqlx.SelectStatement`.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )	r   r   rÀ   Útbl_frmtÚsubrÍ   rÄ   rŽ   r–   )r   r‘   r   r   r   Ú	as_selectä  s
    



zCreateTableStatement.as_selectc             C   s    |  | j ¡ ¡ | j |¡ | S )zÏAdd a Column to the Table.

        Args:
            column_def (MySQLx.ColumnDef): Column Definition object.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )Ú
set_schemar9   rÌ   r¨   r^   )r   Z
column_defr   r   r   Ú
add_columnô  s    	zCreateTableStatement.add_columnc             G   s   t |Ž }| j |¡ | S )zÍAdd multiple Primary Keys to the Table.

        Args:
            *keys: Fields to be used as Primary Keys.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )r   rÉ   r„   )r   re   r   r   r   Úadd_primary_key  s    	z$CreateTableStatement.add_primary_keyc             G   s   | j  t|tjt|Ž ƒ¡ | S )zñAdds an Index to the Table.

        Args:
            index_name (string): Name of the Index.
            *cols: Fields to be used as an Index.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )rÈ   r^   r¥   r«   r   )r   r    Úcolsr   r   r   Ú	add_index  s    
zCreateTableStatement.add_indexc             G   s   | j  t|tjt|Ž ƒ¡ | S )a  Adds a Unique Index to the Table.

        Args:
            index_name (string): Name of the Unique Index.
            *cols: Fields to be used as a Unique Index.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )rÊ   r^   r¥   rª   r   )r   r    rÞ   r   r   r   Úadd_unique_index  s    
z%CreateTableStatement.add_unique_indexc             C   s*   |  | j ¡ ¡ | |¡ | j |¡ | S )zÛAdds a Foreign Key to the Table.

        Args:
            key (MySQLx.ForeignKeyDef): The Foreign Key Definition object.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )rÛ   r9   rÌ   Úset_namerÇ   r^   )r   r[   rh   r   r   r   Úadd_foreign_key*  s    	
z$CreateTableStatement.add_foreign_keyc             C   s
   || _ | S )zêSet the initial Auto Increment value for the table.

        Args:
            inc (int): The initial AUTO_INCREMENT value for the table.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )rË   )r   rÎ   r   r   r   Úset_initial_auto_increment8  s    	z/CreateTableStatement.set_initial_auto_incrementc             C   s
   || _ | S )zÈSets the default Charset type for the Table.

        Args:
            charset (string): Charset type.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )rÁ   )r   rÏ   r   r   r   Úset_default_charsetD  s    	z(CreateTableStatement.set_default_charsetc             C   s
   || _ | S )zÎSets the default Collation type for the Table.

        Args:
            collation (string): Collation type.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )rÂ   )r   rÐ   r   r   r   Úset_default_collationP  s    	z*CreateTableStatement.set_default_collationc             C   s
   || _ | S )zËAdd a comment to the Table.

        Args:
            comment (string): Comment to be added to the Table.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )rÃ   )r   rÑ   r   r   r   Úset_comment\  s    	z CreateTableStatement.set_commentc             C   s
   d| _ | S )zSet the Table to be Temporary.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        T)rÆ   )r   r   r   r   Ú	temporaryh  s    zCreateTableStatement.temporaryc             C   sn   dj | j| jrdndd�}| jr&d}nd}|j || jp>| jp>d|  ¡ |  ¡ d�}| j d	|d
¡ | j	 
| j¡S )zYExecute the statement.

        Returns:
            mysqlx.Table: Table object.
        zCREATE {table_type} {name}zTEMPORARY TABLEZTABLE)r[   Z
table_typez{create} LIKE {query}z,{create} ({create_def}) {table_opts} {query}r   )ÚcreateÚqueryZ
create_defrÓ   rm   F)r$   r.   rÆ   rÅ   rÄ   rÖ   rÔ   r6   r£   r9   Z	get_tabler¦   )r   rè   r•   r   r   r   r;   q  s    zCreateTableStatement.execute)r   r   r   r<   ÚreÚcompileÚ
IGNORECASErØ   r   r=   r.   rÔ   rÖ   r×   rÚ   rÜ   rÝ   rß   rà   râ   rã   rä   rå   ræ   rç   r;   ri   r   r   )rK   r   rÀ   ™  s&   	rÀ   c               @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚColumnDefBasezàA Base class defining the basic parameters required to define a column.

    Args:
        name (string): Name of the column.
        type (MySQLx.ColumnType): Type of the column.
        size (int): Size of the column.
    c             C   s4   d | _ d| _d| _d| _|| _|| _d| _|| _d S )NFr   )Ú_default_schemaÚ	_not_nullÚ_p_keyÚ_u_indexr¦   Ú_sizerÃ   Ú_type)r   r[   ÚtypeÚsizer   r   r   r   �  s    zColumnDefBase.__init__c             C   s
   d| _ | S )zwDisable NULL values for this column.

        Returns:
            mysqlx.ColumnDefBase: ColumnDefBase object.
        T)rï   )r   r   r   r   Únot_nullš  s    zColumnDefBase.not_nullc             C   s
   d| _ | S )zwSet current column as a Unique Index.

        Returns:
            mysqlx.ColumnDefBase: ColumnDefBase object.
       T)rñ   )r   r   r   r   Úunique_index£  s    zColumnDefBase.unique_indexc             C   s
   || _ | S )z¿Add a comment to the column.

        Args:
            comment (string): Comment to be added to the column.

        Returns:
            mysqlx.ColumnDefBase: ColumnDefBase object.
        )rÃ   )r   rÑ   r   r   r   rÑ   ¬  s    	zColumnDefBase.commentc             C   s
   d| _ | S )ztSets the Column as a Primary Key.

        Returns:
            mysqlx.ColumnDefBase: ColumnDefBase object.
        T)rð   )r   r   r   r   Úprimary¸  s    zColumnDefBase.primaryc             C   s
   || _ d S )N)rî   )r   r9   r   r   r   rÛ   Á  s    zColumnDefBase.set_schemaN)
r   r   r   r<   r   rö   r÷   rÑ   rø   rÛ   r   r   r   r   rí   ˆ  s   
			rí   c                   s‚   e Zd ZdZd‡ 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‡  ZS )Ú	ColumnDefzÏClass containing the complete definition of the Column.

    Args:
        name (string): Name of the column.
        type (MySQL.ColumnType): Type of the column.
        size (int): Size of the column.
    Nc                sZ   t t| ƒ |||¡ d | _d | _d | _d | _d| _d| _d| _	g | _
g | _d | _d | _d S )NF)r?   rù   r   Z_refÚ_defaultÚ	_decimalsÚ
_ref_tableÚ_binaryrË   Ú	_unsignedro   Ú_ref_fieldsrÁ   rÂ   )r   r[   rô   rõ   )rK   r   r   r   Í  s    zColumnDef.__init__c             C   sÞ   d}| j r<t | j¡s.t | j¡s.t | j¡r<d | j ¡}nFt | j¡rd| j rdd | j | jp^d¡}nt 	| j¡r‚d d 
| j¡¡}| jr’d |¡}| jr¢d |¡}| jr¶d || j¡}| jrÊd	 || j¡}d
 t | j¡|¡S )Nr   z({0})z
({0}, {1})r   rM   z{0} UNSIGNEDz
{0} BINARYz{0} CHARACTER SET {1}z{0} COLLATE {1}z{0} {1})rò   r   Z
is_numericró   Zis_charZ	is_binaryr$   Zis_decimalsrû   Zis_finite_setr*   ro   rþ   rý   rÁ   rÂ   Z	to_string)r   Ztype_defr   r   r   Ú
_data_typeÞ  s"    

zColumnDef._data_typec             C   s¶   | j r
dnd}| jrdnd}| jr&dnd}| jr4dnd}d |  ¡ ||||¡}| jr`d |¡}n| jrpd	 |¡}| jr¤| j	r¤t
t| j| jƒƒ}d
 ||d | j	¡¡}|j| j| jd�S )Nz	 NOT NULLz NULLz AUTO_INCREMENTr   z DEFAULT {default}z COMMENT '{comment}'z{0}{1}{2}{3}{4}z{0} PRIMARY KEYz{0} UNIQUE KEYz{0} REFERENCES {1} ({2})rM   )ÚdefaultrÑ   )rï   rË   rú   rÃ   r$   r   rð   rñ   rü   rÿ   r+   r2   rî   r*   )r   ÚnullZauto_incr  rÑ   ZdefnZ	ref_tabler   r   r   Ú_col_definitionó  s     
zColumnDef._col_definitionc             C   s2   t |tƒr|j| _n|dkr$d| _n
t|ƒ| _| S )zÿSets the default value of this Column.

        Args:
            default_val (object): The default value of the Column. Can be a
            string, number or :class`MySQLx.Expr`.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        NÚNULL)r   r   r   rú   Úrepr)r   Zdefault_valr   r   r   Úset_default  s    



zColumnDef.set_defaultc             C   s
   d| _ | S )zlSet the Column to Auto Increment.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        T)rË   )r   r   r   r   Úauto_increment  s    zColumnDef.auto_incrementc             G   s   t |Ž | _|| _| S )zæSets the Column as a Foreign Key.

        Args:
            name (string): Name of the referenced Table.
            *refs: Fields this Column references.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        )r   rÿ   rü   )r   r[   Úrefsr   r   r   Úforeign_key$  s    

zColumnDef.foreign_keyc             C   s
   d| _ | S )zfSet the Column as unsigned.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        T)rþ   )r   r   r   r   Úunsigned2  s    zColumnDef.unsignedc             C   s
   || _ | S )zªSet the size of the decimal Column.

        Args:
            size (int): Size of the decimal.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        )rû   )r   rõ   r   r   r   Údecimals;  s    	zColumnDef.decimalsc             C   s
   || _ | S )z©Set the Charset type of the Column.

        Args:
            charset (string): Charset type.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        )rÁ   )r   rÏ   r   r   r   rÏ   G  s    	zColumnDef.charsetc             C   s
   || _ | S )z¯Set the Collation type of the Column.

        Args:
            collation (string): Collation type.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        )rÂ   )r   rÐ   r   r   r   rÐ   S  s    	zColumnDef.collationc             C   s
   d| _ | S )zqSet the current column to binary type.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        T)rý   )r   r   r   r   Úbinary_  s    zColumnDef.binaryc             G   s   t tt|Ž ƒ| _| S )z¨Set the Enum/Set values.

        Args:
            *values: Values for Enum/Set type Column.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        )Úmapr  r   ro   )r   r   r   r   r   r   h  s    	zColumnDef.valuesc             C   s   d  | j|  ¡ ¡S )Nz{0} {1})r$   r¦   r  )r   r   r   r   r–   t  s    zColumnDef.get_sql)N)r   r   r   r<   r   r   r  r  r  r	  r
  r  rÏ   rÐ   r  r   r–   ri   r   r   )rK   r   rù   Å  s   			rù   c                   s4   e Zd ZdZ‡ fdd„Zdd„ Z‡ fdd„Z‡  ZS )ÚGeneratedColumnDefzÊClass used to describe a Generated Column.

    Args:
        name: Name of the column.
        col_type: Type of the column.
        expr: The Expression used to generate the value of this column.
    c                s2   t t| ƒ ||¡ t|tƒs t‚d| _|j| _d S )NF)	r?   r  r   r   r   ÚAssertionErrorÚ_storedr   Ú_expr)r   r[   Zcol_typer   )rK   r   r   r   €  s    zGeneratedColumnDef.__init__c             C   s
   d| _ | S )zƒSet the Generated Column to be stored.

        Returns:
            mysqlx.GeneratedColumnDef: GeneratedColumnDef object.
        T)r  )r   r   r   r   Ústored†  s    zGeneratedColumnDef.storedc                s$   d  tt| ƒ ¡ | j| jrdnd¡S )Nz {0} GENERATED ALWAYS AS ({1}){2}z STOREDr   )r$   r?   r  r–   r  r  )r   )rK   r   r   r–   �  s    zGeneratedColumnDef.get_sql)r   r   r   r<   r   r  r–   ri   r   r   )rK   r   r  x  s   	r  c               @   sh   e Zd ZdZdZdZdZ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S )ÚForeignKeyDefzClass describing a Foreign Key.r   rZ   é   é   c             C   s>   g | _ g | _d | _d | _d | _|  tj¡| _|  tj¡| _	d S )N)
r˜   Ú	_f_fieldsr¦   Ú_f_tablerî   Ú_actionr  Ú	NO_ACTIONÚ_update_actionÚ_delete_action)r   r   r   r   r   œ  s    zForeignKeyDef.__init__c             C   s.   |t jkrdS |t jkrdS |t jkr*dS dS )NÚRESTRICTÚCASCADEzSET NULLz	NO ACTION)r  r  r  ÚSET_NULL)r   Úactionr   r   r   r  ¥  s    


zForeignKeyDef._actionc             C   s
   || _ d S )N)r¦   )r   r[   r   r   r   rá   ®  s    zForeignKeyDef.set_namec             C   s
   || _ d S )N)rî   )r   r9   r   r   r   rÛ   ±  s    zForeignKeyDef.set_schemac             G   s   t |Ž | _| S )zÜAdd a list of fields in the parent table.

        Args:
            *fields: Fields in the given table which constitute the Foreign Key.

        Returns:
            mysqlx.ForeignKeyDef: ForeignKeyDef object.
        )r   r˜   )r   rO   r   r   r   rO   ´  s    	
zForeignKeyDef.fieldsc             G   s   t |Ž | _|| _| S )zýAdd the child table name and the fields.

        Args:
            name (string): Name of the referenced table.
            *refs: A list fields in the referenced table.

        Returns:
            mysqlx.ForeignKeyDef: ForeignKeyDef object.
        )r   r  r  )r   r[   r  r   r   r   Ú	refers_toÀ  s    

zForeignKeyDef.refers_toc             C   s   |   |¡| _| S )aè  Define the action on updating a Foreign Key.

        Args:
            action (int): Action to be performed on updating the reference.
                          Can be any of the following values:
                          1. ForeignKeyDef.NO_ACTION
                          2. ForeignKeyDef.RESTRICT
                          3. ForeignKeyDef.CASCADE
                          4. ForeignKeyDef.SET_NULL

        Returns:
            mysqlx.ForeignKeyDef: ForeignKeyDef object.
        )r  r  )r   r  r   r   r   Ú	on_updateÎ  s    zForeignKeyDef.on_updatec             C   s   |   |¡| _| S )aè  Define the action on deleting a Foreign Key.

        Args:
            action (int): Action to be performed on updating the reference.
                          Can be any of the following values:
                          1. ForeignKeyDef.NO_ACTION
                          2. ForeignKeyDef.RESTRICT
                          3. ForeignKeyDef.CASCADE
                          4. ForeignKeyDef.SET_NULL

        Returns:
            mysqlx.ForeignKeyDef: ForeignKeyDef object.
        )r  r  )r   r  r   r   r   Ú	on_deleteà  s    zForeignKeyDef.on_deletec             C   sV   d  | j¡}d  | j¡}d  | jd | j¡tt| j| j	ƒƒd | j
¡¡}d  |||¡S )NzON UPDATE {0}zON DELETE {0}z)FOREIGN KEY {0}({1}) REFERENCES {2} ({3})rM   z{0} {1} {2})r$   r  r  r¦   r*   r˜   r+   r2   rî   r  r  )r   r‰   r›   rh   r   r   r   r–   ñ  s    zForeignKeyDef.get_sqlN)r   r   r   r<   r  r  r  r  r   r  rá   rÛ   rO   r   r!  r"  r–   r   r   r   r   r  •  s   		r  )r   )r   )r   )r   )2r<   rc   rê   Úerrorsr   r   r   Úcompatr   Ú	constantsr   r   Zdbdocr   Zprotobufr	   rx   Úresultr
   r   r   rf   r   r   r#   r&   r+   r2   r3   r>   rj   rn   rv   r~   rŠ   rŽ   r—   r™   rš   rœ   r�   r¤   r¥   r¬   r¿   rÀ   rí   rù   r  r  r   r   r   r   Ú<module>   sP   



" *e<?!"*w! p= 4