Uname: Linux web3.us.cloudlogin.co 5.10.226-xeon-hst #2 SMP Fri Sep 13 12:28:44 UTC 2024 x86_64
Software: Apache
PHP version: 8.1.31 [ PHP INFO ] PHP os: Linux
Server Ip: 162.210.96.117
Your Ip: 3.145.62.14
User: edustar (269686) | Group: tty (888)
Safe Mode: OFF
Disable Function:
NONE

name : statement.cpython-310.pyc
o

�\O��@sdZddlZddlZddlmZddlmZddlmZddl	m
Z
mZddlm
Z
dd	lmZdd
lmZmZmZGdd�de�Zd
d�ZdDdd�ZdDdd�ZdDdd�ZdDdd�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGd d!�d!e�Z Gd"d#�d#e�Z!Gd$d%�d%e�Z"Gd&d'�d'e�Z#Gd(d)�d)e�Z$Gd*d+�d+e�Z%Gd,d-�d-e�Z&Gd.d/�d/e�Z'Gd0d1�d1e�Z(Gd2d3�d3e�Z)Gd4d5�d5e�Z*Gd6d7�d7e�Z+Gd8d9�d9e+�Z,Gd:d;�d;e�Z-Gd<d=�d=e�Z.Gd>d?�d?e.�Z/Gd@dA�dAe/�Z0GdBdC�dCe�Z1dS)EzImplementation of Statements.�N�)�ProgrammingError)�
ExprParser)�STRING_TYPES)�
Algorithms�
Securities)�DbDoc)�mysqlx_crud_pb2)�	SqlResult�Result�
ColumnTypec@seZdZdd�ZdS)�ExprcC�
||_dS�N)�expr)�selfr�r�G/usr/local/python-3.10/lib/python3.10/site-packages/mysqlx/statement.py�__init__&�
z
Expr.__init__N)�__name__�
__module__�__qualname__rrrrrr
%sr
cGs*t|�dkrt|dttf�r|dS|S)Nrr)�len�
isinstance�list�tuple)�valuesrrr�flexible_params*sr�cCsPd|vr|ddkr|ddkp|ddko|ddkS|ddko'|ddkS)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_moderrr�is_quoted_identifier0s

�r'cCs:t||�r|Sd|vrd�|�dd��Sd�|�dd��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``)r'�format�replacer$rrr�quote_identifierAs

r*csd��fdd�|D��S)aQuote 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.
    �.csg|]}t|���qSr�r*)�.0r%�r&rr�
<listcomp>_s�z.quote_multipart_identifier.<locals>.<listcomp>)�join)Zidentifiersr&rr.r�quote_multipart_identifierUs
�r1cCs^d|vrdnd}||vrd�|�nd}|�|d�}t|�dur!|n|d�|�|d�|�fS)	Nr r#r!z.{0}r+rrr")r(�splitr�strip)Zdefault_schema�
table_namer&�quote�	delimiter�temprrr�parse_table_namecs�r8c@s:eZdZdZddd�Zedd��Zedd��Zd	d
�ZdS)
�	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.
    TcCs&||_||_|r|j|_dSd|_dSr)�_target�
_doc_based�_connection)r�target�	doc_basedrrrrsszStatement.__init__cCs|jS)z,object: The database object target.
        )r:�rrrrr=xszStatement.targetcCs|jjS)z3:class:`mysqlx.Schema`: The Schema object.
        )r:�schemar?rrrr@~szStatement.schemacC�t��zqExecute the statement.

        Raises:
           NotImplementedError: This method must be implemented.
        ��NotImplementedErrorr?rrr�execute��zStatement.executeN)T)	rrr�__doc__r�propertyr=r@rErrrrr9ks


r9csleZdZdZd�fdd�	Zdd�Zdd	�Zddd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
�ZS)�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.
    TNcsdtt|�j||d�d|_d|_d|_d|_d|_d|_d|_	i|_
g|_|dur0|�|�dSdS)N�r=r>F)
�superrIr�_has_projection�
_has_where�
_has_limit�	_has_sort�
_has_group_by�_has_having�
_has_bindings�_binding_map�	_bindings�where)rr=r>�	condition��	__class__rrr�s��zFilterableStatement.__init__cCs0d|_||_t||j�}|��|_|j|_|S)aSets the search condition to filter.

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

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.
        T)rM�_whererr;rZ_where_exprZplaceholder_name_to_positionrS)rrVrrrrrU�s

zFilterableStatement.wherecG�4t|�}d|_d�|�|_t|j|j���|_|S�NT�,)rrLr0�_projection_strrr;Zparse_table_select_projectionZ_projection_expr�r�fieldsrrr�_projection�s��zFilterableStatement._projectionrcCsd|_||_||_|S)aESets 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)rN�
_limit_offset�_limit_row_count)rZ	row_count�offsetrrr�limit�s
zFilterableStatement.limitcGrZ)z�Sets the sorting criteria.

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

        Returns:
            mysqlx.FilterableStatement: FilterableStatement object.
        Tr\)rrOr0�	_sort_strrr;Zparse_order_specZ
_sort_expr)rZsort_clausesrrr�sort�s	��zFilterableStatement.sortcGs4t|�}d|_d�|�|_t|j|j���|_dSr[)rrPr0�
_grouping_strrr;Zparse_expr_listZ	_groupingr^rrr�	_group_by�s��zFilterableStatement._group_bycCsd|_t||j���|_dS�NT)rQrr;r�_having�rrVrrrrj�szFilterableStatement._havingcGsVd|_t|�}|dkr|�|d�|S|dkrtd��|j�|d|dd��|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.
        Trr�z#Invalid number of arguments to bind)�name�value)rRr�_bind_singlerrT�append)r�args�countrrr�bind�s�zFilterableStatement.bindcCsVt|t�r|�t|��dSt|t�r't�|�}|��D]}|�|||�qdSdSr)rrrs�strr�json�loads�keys)r�object�dict�keyrrrros


�z FilterableStatement._bind_singlecCrArBrCr?rrrrE
rFzFilterableStatement.execute)TN)r)rrrrGrrUr`rdrfrhrjrsrorE�
__classcell__rrrWrrI�s

rIc�(eZdZdZ�fdd�Zdd�Z�ZS)�SqlStatementz�A statement for SQL execution.

    Args:
        connection (mysqlx.connection.Connection): Connection object.
        sql (string): The sql statement to be executed.
    cs$tt|�jddd�||_||_dS�NFrJ)rKr}rr<�_sql)rZ
connection�sqlrWrrrs
zSqlStatement.__init__cCs|j�|j�t|j�S)zaExecute the statement.

        Returns:
            mysqlx.SqlResult: SqlResult object.
        )r<Zsend_sqlrr
r?rrrrEs
zSqlStatement.execute�rrrrGrrEr{rrrWrr}sr}c�0eZdZdZ�fdd�Zdd�Zdd�Z�ZS)�AddStatementz�A statement for document addition on a collection.

    Args:
        collection (mysqlx.Collection): The Collection object.
    cs"tt|�j|d�g|_g|_dS�N)r=)rKr�r�_values�_ids�r�
collectionrWrrr/s
zAddStatement.__init__cGs: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.
        )rrrr�rp)rr�valrrr�add4s
	
zAddStatement.addcCs<t|j�dkr
t�S|jD]
}|j�|���q
|j�|�S)�[Execute the statement.

        Returns:
            mysqlx.Result: Result object.
        r)rr�rr�rpZ	ensure_idr<�send_insert)r�docrrrrEDs

zAddStatement.execute)rrrrGrr�rEr{rrrWrr�)s
r�c@seZdZddd�Zdd�ZdS)�
UpdateSpecNcCsl|tjjkr|�||�dS||_||_t|�dkr'|ddkr'|dd�|_t|jd���j	|_||_
dS)Nr�$rF)�
MySQLxCrud�UpdateOperation�SET�
_table_set�update_type�sourcerrZdocument_fieldr%rn)rr�r�rnrrrrTs��
zUpdateSpec.__init__cCs$tjj|_t|d���|_||_dSri)r�r�r�r�rZparse_table_update_fieldr�rn)rr�rnrrrr�`s

zUpdateSpec._table_setr)rrrrr�rrrrr�Ss
r�csReZdZdZd�fdd�	Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
�ZS)�ModifyStatementaA 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.
    Ncstt|�j||d�g|_dS)N)r=rV)rKr�r�_update_ops�rr�rVrWrrrns�
zModifyStatement.__init__cC�|j�ttjj||��|S)a2Sets 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�rpr�r�r�ZITEM_SET�rZdoc_pathrnrrr�sets�
�zModifyStatement.setcCr�)ajAdd 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�rpr�r�r�ZITEM_REPLACEr�rrr�change�s
��zModifyStatement.changecGs|j�dd�t|�D��|S)aRemoves attributes from documents in a collection.

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

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.
        cSsg|]	}ttjj|��qSr)r�r�r�ZITEM_REMOVE)r-�xrrrr/�s��z)ModifyStatement.unset.<locals>.<listcomp>)r��extendr)rZ	doc_pathsrrr�unset�s
�zModifyStatement.unsetcCr�)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�rpr�r�r�ZARRAY_INSERT�r�fieldrnrrr�array_insert�s�zModifyStatement.array_insertcCr�)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�rpr�r�r�ZARRAY_APPENDr�rrr�array_append�s
��zModifyStatement.array_appendcC�|j�|�S�r��r<�updater?rrrrE��zModifyStatement.executer)rrrrGrr�r�r�r�r�rEr{rrrWrr�fsr�csBeZdZdZd
�fdd�	Zdd�Zdd�Zd	d
�Zdd�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.
    Ncstt|��|d|�dSri)rKr�rr�rWrrr�szFindStatement.__init__cGs
|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^rrrr_�s

zFindStatement.fieldscG�|j|�|S)z�Sets a grouping criteria for the resultset.

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

        Returns:
            mysqlx.FindStatement: FindStatement object.
        �rhr^rrr�group_by��
	zFindStatement.group_bycC�|�|�|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.
        �rjrkrrr�having��
zFindStatement.havingcCr�)zaExecute the statement.

        Returns:
            mysqlx.DocResult: DocResult object.
        �r<�findr?rrrrE�r�zFindStatement.executer)
rrrrGrr_r�r�rEr{rrrWrr��s	r�cs@eZdZdZ�fdd�Zdd�Zdd�Zdd	�Zd
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.
    cs tt|��|d�|j|�dS�NF)rKr�rr`�r�tabler_rWrrrszSelectStatement.__init__cGr�)z�Sets a grouping criteria for the resultset.

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

        Returns:
            mysqlx.SelectStatement: SelectStatement object.
        r�r^rrrr�r�zSelectStatement.group_bycCr�)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.
        r�rkrrrr�r�zSelectStatement.havingcCr�)zaExecute the statement.

        Returns:
            mysqlx.RowResult: RowResult object.
        r�r?rrrrE,r�zSelectStatement.executec
Cs�|jr	d�|j�nd}|jrd�|j�nd}|jrd�|j�nd}|jr*d�|j�nd}|j	r7d�|j
|j�nd}djt|dd	�|j
j|jj|||||d
�}|S)Nz
 WHERE {0}rz
 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}r]�*)�selectr@r�rdrU�groupr��order)rMr(rYrPrgrQrjrOrerNrbra�getattrr@rmr=)rrUr�r�Zorder_byrd�stmtrrr�get_sql4s,���
���
�zSelectStatement.get_sql)
rrrrGrr�r�rEr�r{rrrWrr�sr�cr�)�InsertStatementz�A statement for insert operations on Table.

    Args:
        table (mysqlx.Table): The Table object.
        *fields: The fields to be inserted.
    cs(tt|�j|dd�t|�|_g|_dSr~)rKr�rr�_fieldsr�r�rWrrrMs

zInsertStatement.__init__cGs|j�tt|���|S)z�Set the values to be inserted.

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

        Returns:
            mysqlx.InsertStatement: InsertStatement object.
        )r�rprr�rrrrrrRs	zInsertStatement.valuescCr�r�)r<r�r?rrrrE^r�zInsertStatement.execute)rrrrGrrrEr{rrrWrr�Fs
r�cr�)�UpdateStatementz�A statement for record update operations on a Table.

    Args:
        table (mysqlx.Table): The Table object.
        *fields: The fields to be updated.
    cstt|�j|dd�g|_dSr~)rKr�rr�r�rWrrrns
zUpdateStatement.__init__cCr�)aUpdates 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�rpr�r�r�r�r�rrrr�rr�zUpdateStatement.setcCr�)zZExecute the statement.

        Returns:
            mysqlx.Result: Result object
        r�r?rrrrE�r�zUpdateStatement.execute)rrrrGrr�rEr{rrrWrr�gs
r�cr|)�RemoveStatementz�A statement for document removal from a collection.

    Args:
        collection (mysqlx.Collection): The Collection object.
    cstt|�j|d�dSr�)rKr�rr�rWrrr�szRemoveStatement.__init__cCr�r��r<�deleter?rrrrE�r�zRemoveStatement.executer�rrrWrr���r�cs*eZdZdZd�fdd�	Zdd�Z�ZS)�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.
    Ncstt|�j||dd�dS)NF)r=rVr>)rKr�r)rr�rVrWrrr�s
�zDeleteStatement.__init__cCr�r�r�r?rrrrE�r�zDeleteStatement.executerr�rrrWrr��sr�cr�)�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.
    cs(tt|�j|d�||_||_g|_dSr�)rKr�r�_index_name�
_is_uniquer�)rr��
index_nameZ	is_uniquerWrrr�s
z'CreateCollectionIndexStatement.__init__cCs|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�rp)rZ
document_pathZcolumn_typeZis_requiredrrrr��sz$CreateCollectionIndexStatement.fieldcCs>dd�|jD�}|jjddd|jjj|jj|j|jg|�R�S)r�cSsg|]	}|D]}|�qqSrr)r-Zsublist�itemrrrr/�sz:CreateCollectionIndexStatement.execute.<locals>.<listcomp>�xpluginZcreate_collection_indexT)r�r<�execute_nonqueryr:r@rmr�r�r^rrrrE�s��z&CreateCollectionIndexStatement.execute)rrrrGrr�rEr{rrrWrr��s
r�cr|)�DropCollectionIndexStatementz�A statement that drops an index on a collection.

    Args:
        collection (mysqlx.Collection): The Collection object.
        index_name (string): The index name.
    cstt|�j|d�||_dSr�)rKr�rr�)rr�r�rWrrr�s
z%DropCollectionIndexStatement.__init__cCs"|j�ddd|jjj|jj|j�S)r�r�Zdrop_collection_indexT)r<r�r:r@rmr�r?rrrrE�s�z$DropCollectionIndexStatement.executer�rrrWrr��sr�c@s$eZdZdZdZdd�Zdd�ZdS)�
TableIndexrrlcCs||_||_||_dSr)�_name�_index_type�_columns)rrmZ
index_type�columnsrrrr�s
zTableIndex.__init__cCs6d}|jtjur|d7}|d7}|�|jd�|j��S)NrzUNIQUE zINDEX {0} ({1})r\)r�r��UNIQUE_INDEXr(r�r0r�)rr�rrrr��s
zTableIndex.get_sqlN)rrrr��INDEXrr�rrrrr��s
r�csZeZdZdZd�fdd�	Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Z�ZS)�CreateViewStatementz�A statement for creating views.

    Args:
        view (mysqlx.View): The View object.
        replace (Optional[bool]): `True` to add replace.
    Fcs\tt|�j|dd�||_|j|_|j|_||_g|_	t
j|_t
j|_d|_d|_d|_dSr~)rKr�r�_viewr@�_schemarmr��_replacer�rZ	UNDEFINED�
_algorithmrZDEFINER�	_security�_definer�_defined_as�
_check_option)r�viewr)rWrrrs
zCreateViewStatement.__init__cCsdd�|D�|_|S)z�Sets the column names.

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

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        cSsg|]}t|��qSrr,)r-�colrrrr/sz/CreateViewStatement.columns.<locals>.<listcomp>)r�)rr�rrrr��	zCreateViewStatement.columnscC�
||_|S)z�Sets the algorithm.

        Args:
            mysqlx.constants.ALGORITHMS: The algorithm.

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        )r�)r�	algorithmrrrr�!�	zCreateViewStatement.algorithmcCr�)z�Sets the SQL security mode.

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

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        )r�)r�securityrrrr�-r�zCreateViewStatement.securitycCr�)z�Sets the definer.

        Args:
            definer (string): The definer.

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        )r�)r�definerrrrr�9r�zCreateViewStatement.definercCr�)z�Sets the SelectStatement statement for describing the view.

        Args:
            mysqlx.SelectStatement: SelectStatement object.

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        )r�)r�	statementrrr�
defined_asEr�zCreateViewStatement.defined_ascCr�)z�Sets the check option.

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

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        )r�)r�check_optionrrr�with_check_optionQr�z%CreateViewStatement.with_check_optionc
Cs�|jrdnd}|jrd�|j�nd}|jrd�d�|j��nd}t|jj|jf�}|j	r2d�|j	�nd}dj||j
||j|||j|d�}|j
�d	|�|jS)
zhExecute the statement to create a view.

        Returns:
            mysqlx.View: View object.
        z OR REPLACEr� DEFINER = {0}� ({0})�, � 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�r�)r�r�r(r�r0r1r�rmr�r�r�r�r�r<r�r�)rr)r�r�r�r�r�rrrrE]s.�������	zCreateViewStatement.execute)F)
rrrrGrr�r�r�r�r�r�rEr{rrrWrr�s
r�cr|)�AlterViewStatementzYA statement for alter views.

    Args:
        view (mysqlx.View): The View object.
    cstt|��|�dSr)rKr�r)rr�rWrrr~szAlterViewStatement.__init__c	Cs�|jr	d�|j�nd}|jrd�d�|j��nd}t|jj|jf�}|jr+d�|j�nd}dj|j	||j
|||j|d�}|j�
d|�|jS)	zgExecute the statement to alter a view.

        Returns:
            mysqlx.View: View object.
        r�rr�r�r�zvALTER ALGORITHM = {algorithm}{definer} SQL SECURITY {security} VIEW {view_name}{columns} AS {defined_as}{check_option})r�r�r�r�r�r�r�r�)r�r(r�r0r1r�rmr�r�r�r�r�r<r�r�)rr�r�r�r�r�rrrrE�s*�������zAlterViewStatement.executer�rrrWrr�xr�r�cs�eZdZdZe�dej�Z�fdd�Ze	dd��Z
dd�Zd	d
�Zdd�Z
d
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Z�ZS)%�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|$)csttt|��|�d|_d|_d|_d|_d|_d|_g|_	g|_
g|_g|_g|_
d|_||_d�|j���|_dS)NFrz
\1{0}.\2\3)rKr�r�_charset�
_collation�_comment�_as�_like�_tempr��_f_keys�_indices�_p_keys�
_u_indices�	_auto_incr�r(r@�get_name�	_tbl_repl)rr@r4rWrrr�szCreateTableStatement.__init__cCstt|jj|j��S)z7string: The fully qualified name of the Table.
        )r1r8r@rmr�r?rrrr4�s
�zCreateTableStatement.table_namecCsbg}|�d�|jr|�d�|jr|�d�|jr|�d�d�|�}|j|j|j|j|jd�S)NzAUTO_INCREMENT = {inc}z!DEFAULT CHARACTER SET = {charset}zDEFAULT COLLATE = {collation}zCOMMENT = '{comment}'r\)�inc�charset�	collation�comment)rpr�r�r�r0r(r�)r�options�
table_optsrrr�_get_table_opts�s




�z$CreateTableStatement._get_table_optscCs�g}|jr|�d�d�|j���|jD]	}|�|���q|jD]	}|�|���q!|jD]	}|�|���q.|jD]	}|�|���q;d�|�S)NzPRIMARY KEY ({0})r\)	r�rpr(r0r�r�r�r�r�)rZdefsr�rz�indexrrr�_get_create_def�s




z$CreateTableStatement._get_create_defcCstt|jj|��|_|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.
        )r1r8r@rmr�)rr4rrr�like�s	�zCreateTableStatement.likecCs8t|t�rtj�|j|�|_|St|t�r|��|_|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.
        )	rrr��tbl_frmt�subrr�r�r�)rr�rrr�	as_select�s


�
zCreateTableStatement.as_selectcCs |�|j���|j�|�|S)z�Add a Column to the Table.

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

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )�
set_schemar@r�r�rp)rZ
column_defrrr�
add_column�s	zCreateTableStatement.add_columncGst|�}|j�|�|S)z�Add multiple Primary Keys to the Table.

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

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        )rr�r�)rrwrrr�add_primary_keys	z$CreateTableStatement.add_primary_keycG�|j�t|tjt|���|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�rpr�r�r�rr��colsrrr�	add_index�
�zCreateTableStatement.add_indexcGr)aAdds 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�rpr�r�rrrrr�add_unique_indexrz%CreateTableStatement.add_unique_indexcCs*|�|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.
        )rr@r��set_namer�rp)rrmrzrrr�add_foreign_key*s	
z$CreateTableStatement.add_foreign_keycCr�)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�)rrrrr�set_initial_auto_increment8r�z/CreateTableStatement.set_initial_auto_incrementcCr�)z�Sets the default Charset type for the Table.

        Args:
            charset (string): Charset type.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        �r��rrrrr�set_default_charsetDr�z(CreateTableStatement.set_default_charsetcCr�)z�Sets the default Collation type for the Table.

        Args:
            collation (string): Collation type.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        �r��rrrrr�set_default_collationPr�z*CreateTableStatement.set_default_collationcCr�)z�Add a comment to the Table.

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

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        �r��rrrrr�set_comment\r�z CreateTableStatement.set_commentcC�
d|_|S)zSet the Table to be Temporary.

        Returns:
            mysqlx.CreateTableStatement: CreateTableStatement object.
        T)r�r?rrr�	temporaryh�zCreateTableStatement.temporarycCsndj|j|jr	dndd�}|jrd}nd}|j||jp|jpd|��|��d�}|j�d	|d
�|j	�
|j�S)zYExecute the statement.

        Returns:
            mysqlx.Table: Table object.
        zCREATE {table_type} {name}zTEMPORARY TABLEZTABLE)rmZ
table_typez{create} LIKE {query}z,{create} ({create_def}) {table_opts} {query}r)�create�queryZ
create_defrr�F)r(r4r�r�r�r	rr<r�r@Z	get_tabler�)rr'r�rrrrEqs��zCreateTableStatement.execute)rrrrG�re�compile�
IGNORECASErrrHr4rr	r
r
rrrrrrrr r#r%rEr{rrrWrr��s*



	r�c@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�
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.
    cCs4d|_d|_d|_d|_||_||_d|_||_dS)NFr)�_default_schema�	_not_null�_p_key�_u_indexr��_sizer��_type�rrm�type�sizerrrr�s
zColumnDefBase.__init__cCr$)zwDisable NULL values for this column.

        Returns:
            mysqlx.ColumnDefBase: ColumnDefBase object.
        T)r.r?rrr�not_null�r&zColumnDefBase.not_nullcCr$)zwSet current column as a Unique Index.

        Returns:
            mysqlx.ColumnDefBase: ColumnDefBase object.
       T)r0r?rrr�unique_index�r&zColumnDefBase.unique_indexcCr�)z�Add a comment to the column.

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

        Returns:
            mysqlx.ColumnDefBase: ColumnDefBase object.
        r!r"rrrr�r�zColumnDefBase.commentcCr$)ztSets the Column as a Primary Key.

        Returns:
            mysqlx.ColumnDefBase: ColumnDefBase object.
        T)r/r?rrr�primary�r&zColumnDefBase.primarycCrr�r-�rr@rrrr�rzColumnDefBase.set_schemaN)
rrrrGrr6r7rr8rrrrrr,�s
			r,cs�eZdZdZd�fdd�	Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Z�ZS)�	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.
    NcsZtt|��|||�d|_d|_d|_d|_d|_d|_d|_	g|_
g|_d|_d|_
dSr�)rKr;rZ_ref�_default�	_decimals�
_ref_table�_binaryr��	_unsignedr��_ref_fieldsr�r�r3rWrrr�s
zColumnDef.__init__cCs�d}|jrt�|j�st�|j�st�|j�rd�|j�}n#t�|j�r2|jr2d�|j|jp/d�}nt�	|j�rAd�d�
|j��}|jrId�|�}|j
rQd�|�}|jr[d�||j�}|jred	�||j�}d
�t�|j�|�S)Nrz({0})z
({0}, {1})rr\z{0} UNSIGNEDz
{0} BINARYz{0} CHARACTER SET {1}z{0} COLLATE {1}�{0} {1})r1rZ
is_numericr2Zis_charZ	is_binaryr(Zis_decimalsr=Z
is_finite_setr0r�r@r?r�r�Z	to_string)rZtype_defrrr�
_data_type�s(
�
�

zColumnDef._data_typecCs�|jrdnd}|jrdnd}|jrdnd}|jrdnd}d�|��||||�}|jr0d�|�}n|jr8d	�|�}|jrR|j	rRt
t|j|j��}d
�||d�
|j	��}|j|j|jd�S)
Nz	 NOT NULLz NULLz AUTO_INCREMENTrz DEFAULT {default}z COMMENT '{comment}'z{0}{1}{2}{3}{4}z{0} PRIMARY KEYz{0} UNIQUE KEYz{0} REFERENCES {1} ({2})r\)�defaultr)r.r�r<r�r(rCr/r0r>rAr1r8r-r0)r�nullZauto_incrDr�defnZ	ref_tablerrr�_col_definition�s&�
�
�zColumnDef._col_definitioncCs6t|t�r|j|_|S|durd|_|St|�|_|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.
        NZNULL)rr
rr<�repr)rZdefault_valrrr�set_defaults

�
�zColumnDef.set_defaultcCr$)zlSet the Column to Auto Increment.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        Trr?rrr�auto_incrementr&zColumnDef.auto_incrementcG�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.
        )rrAr>�rrm�refsrrr�foreign_key$�

zColumnDef.foreign_keycCr$)zfSet the Column as unsigned.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        T)r@r?rrr�unsigned2r&zColumnDef.unsignedcCr�)z�Set the size of the decimal Column.

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

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        )r=)rr5rrr�decimals;r�zColumnDef.decimalscCr�)z�Set the Charset type of the Column.

        Args:
            charset (string): Charset type.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        rrrrrrGr�zColumnDef.charsetcCr�)z�Set the Collation type of the Column.

        Args:
            collation (string): Collation type.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        rrrrrrSr�zColumnDef.collationcCr$)zqSet the current column to binary type.

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        T)r?r?rrr�binary_r&zColumnDef.binarycGsttt|��|_|S)z�Set the Enum/Set values.

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

        Returns:
            mysqlx.ColumnDef: ColumnDef object.
        )�maprHrr�r�rrrrhr�zColumnDef.valuescCsd�|j|���S)NrB)r(r�rGr?rrrr�tszColumnDef.get_sqlr)rrrrGrrCrGrIrJrNrPrQrrrRrr�r{rrrWrr;�s			r;cs4eZdZdZ�fdd�Zdd�Z�fdd�Z�ZS)�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.
    cs2tt|��||�t|t�sJ�d|_|j|_dSr�)rKrTrrr
�_storedr�_expr)rrmZcol_typerrWrrr�szGeneratedColumnDef.__init__cCr$)z�Set the Generated Column to be stored.

        Returns:
            mysqlx.GeneratedColumnDef: GeneratedColumnDef object.
        T)rUr?rrr�stored�r&zGeneratedColumnDef.storedcs&d�tt|���|j|jrd�Sd�S)Nz {0} GENERATED ALWAYS AS ({1}){2}z STOREDr)r(rKrTr�rVrUr?rWrrr��s��zGeneratedColumnDef.get_sql)rrrrGrrWr�r{rrrWrrTxs
	rTc@sheZdZdZdZdZdZdZdd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)�
ForeignKeyDefzClass describing a Foreign Key.rrl��cCs>g|_g|_d|_d|_d|_|�tj�|_|�tj�|_	dSr)
r��	_f_fieldsr��_f_tabler-�_actionrX�	NO_ACTION�_update_action�_delete_actionr?rrrr�szForeignKeyDef.__init__cCs.|tjurdS|tjurdS|tjurdSdS)N�RESTRICT�CASCADEzSET NULLz	NO ACTION)rXrarb�SET_NULL�r�actionrrrr]�s


zForeignKeyDef._actioncCrr)r�)rrmrrrr�rzForeignKeyDef.set_namecCrrr9r:rrrr�rzForeignKeyDef.set_schemacGst|�|_|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.
        )rr�r^rrrr_�r�zForeignKeyDef.fieldscGrK)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.
        )rr[r\rLrrr�	refers_to�rOzForeignKeyDef.refers_tocC�|�|�|_|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_rdrrr�	on_update�szForeignKeyDef.on_updatecCrg)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`rdrrr�	on_delete�szForeignKeyDef.on_deletecCsVd�|j�}d�|j�}d�|jd�|j�tt|j|j	��d�|j
��}d�|||�S)Nz
ON UPDATE {0}z
ON DELETE {0}z)FOREIGN KEY {0}({1}) REFERENCES {2} ({3})r\z{0} {1} {2})r(r_r`r�r0r�r1r8r-r\r[)rr�r�rzrrrr��s�
�zForeignKeyDef.get_sqlN)rrrrGr^rarbrcrr]rrr_rfrhrir�rrrrrX�s		rX)r)2rGrur)�errorsrrr�compatr�	constantsrrZdbdocrZprotobufr	r��resultr
rrrxr
rr'r*r1r8r9rIr}r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r,r;rTrXrrrr�<module>sR



"*e<?!"*w!p=4
© 2025 GrazzMean