3
S^L                 @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	m
Z
mZ ddlmZmZmZmZmZmZmZmZmZ dZdZd	Zd
Z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 dS )z,Implementation of the CRUD database objects.   )DbDoc)ER_NO_SUCH_TABLE)OperationalErrorProgrammingError)
deprecatedescapequote_identifier)	FindStatementAddStatementRemoveStatementModifyStatementSelectStatementInsertStatementDeleteStatementUpdateStatementCreateCollectionIndexStatementz_SELECT COUNT(*) FROM information_schema.views WHERE table_schema = '{0}' AND table_name = '{1}'z`SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '{0}' AND table_name = '{1}'zJSELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = '{0}'zSELECT COUNT(*) FROM {0}.{1}zDROP TABLE IF EXISTS {0}.{1}c               @   s   e Zd ZdZdd Zedd Zedd Zedd	 Zd
d Z	dd Z
dd Zdd Zdd Zedddd Zedddd ZdS )DatabaseObjectzProvides base functionality for database objects.

    Args:
        schema (mysqlx.Schema): The Schema object.
        name (str): The database object name.
    c             C   s(   || _ || _| j j | _| jj | _d S )N)_schema_nameget_session_sessionget_connection_connection)selfschemaname r   =/tmp/pip-build-8app2_gc/mysql-connector-python/mysqlx/crud.py__init__:   s    zDatabaseObject.__init__c             C   s   | j S )z5:class:`mysqlx.Session`: The Session object.
        )r   )r   r   r   r   session@   s    zDatabaseObject.sessionc             C   s   | j S )z3:class:`mysqlx.Schema`: The Schema object.
        )r   )r   r   r   r   r   F   s    zDatabaseObject.schemac             C   s   | j S )z/str: The name of this database object.
        )r   )r   r   r   r   r   L   s    zDatabaseObject.namec             C   s   | j S )z~Returns the underlying connection.

        Returns:
            mysqlx.connection.Connection: The connection object.
        )r   )r   r   r   r   r   R   s    zDatabaseObject.get_connectionc             C   s   | j S )zwReturns the session of this database object.

        Returns:
            mysqlx.Session: The Session object.
        )r   )r   r   r   r   r   Z   s    zDatabaseObject.get_sessionc             C   s   | j S )z{Returns the Schema object of this database object.

        Returns:
            mysqlx.Schema: The Schema object.
        )r   )r   r   r   r   
get_schemab   s    zDatabaseObject.get_schemac             C   s   | j S )zwReturns the name of this database object.

        Returns:
            str: The name of this database object.
        )r   )r   r   r   r   get_namej   s    zDatabaseObject.get_namec             C   s   t dS )zVerifies if this object exists in the database.

        Returns:
            bool: `True` if object exists in database.

        Raises:
           NotImplementedError: This method must be implemented.
        N)NotImplementedError)r   r   r   r   exists_in_databaser   s    	z!DatabaseObject.exists_in_databasez8.0.12z)Use 'exists_in_database()' method insteadc             C   s   | j  S )a+  Verifies if this object exists in the database.

        Returns:
            bool: `True` if object exists in database.

        Raises:
           NotImplementedError: This method must be implemented.

        .. deprecated:: 8.0.12
           Use ``exists_in_database()`` method instead.
        )r#   )r   r   r   r   	am_i_real}   s    zDatabaseObject.am_i_realzUse 'get_name()' method insteadc             C   s   | j  S )zReturns the name of this database object.

        Returns:
            str: The name of this database object.

        .. deprecated:: 8.0.12
           Use ``get_name()`` method instead.
        )r!   )r   r   r   r   who_am_i   s    
zDatabaseObject.who_am_iN)__name__
__module____qualname____doc__r   propertyr   r   r   r   r   r    r!   r#   r   r$   r%   r   r   r   r   r   3   s   r   c                   sr   e Zd ZdZ fddZdd Zdd Zdd	d
Zdd ZdddZ	dddZ
dddZdd ZdddZ  ZS )SchemazA client-side representation of a database schema. Provides access to
    the schema contents.

    Args:
        session (mysqlx.XSession): Session object.
        name (str): The Schema name.
    c                s   || _ tt| j| | d S )N)r   superr+   r   )r   r   r   )	__class__r   r   r      s    zSchema.__init__c             C   s    t jt| j}| jj|dkS )zVerifies if this object exists in the database.

        Returns:
            bool: `True` if object exists in database.
        r   )_COUNT_SCHEMAS_QUERYformatr   r   r   execute_sql_scalar)r   sqlr   r   r   r#      s    zSchema.exists_in_databasec             C   s|   | j jd| j}|j  g }xZ|D ]R}|d dkr4q"yt| |d }W n" tk
rh   t| |d }Y nX |j| q"W |S )zyReturns a list of collections for this schema.

        Returns:
            `list`: List of Collection objects.
        list_objectstypeZ
COLLECTION
TABLE_NAMEr   )r   get_row_resultr   	fetch_all
Collection
ValueErrorappend)r   rowscollectionsrow
collectionr   r   r   get_collections   s    
zSchema.get_collectionsFc             C   s   | j ||S )zuReturns a a table object for the given collection

        Returns:
            mysqlx.Table: Table object.

        )	get_table)r   r   check_existencer   r   r   get_collection_as_table   s    zSchema.get_collection_as_tablec             C   s~   | j jd| j}|j  g }d}xX|D ]P}|d |kr&yt| |d }W n" tk
rj   t| |d }Y nX |j| q&W |S )zoReturns a list of tables for this schema.

        Returns:
            `list`: List of Table objects.
        r2   TABLEVIEWr3   r4   r   )rB   rC   )r   r5   r   r6   Tabler8   r9   )r   r:   ZtablesZobject_typesr<   tabler   r   r   
get_tables   s    
zSchema.get_tablesc             C   s"   t | |}|r|j std|S )zwReturns the table of the given name for this schema.

        Returns:
            mysqlx.Table: Table object.
        zTable does not exist)rD   r#   r   )r   r   r@   rE   r   r   r   r?      s
    
zSchema.get_tablec             C   s"   t | |}|r|j std|S )ztReturns the view of the given name for this schema.

        Returns:
            mysqlx.View: View object.
        zView does not exist)Viewr#   r   )r   r   r@   viewr   r   r   get_view   s
    
zSchema.get_viewc             C   s"   t | |}|r|j std|S )zReturns the collection of the given name for this schema.

        Returns:
            mysqlx.Collection: Collection object.
        zCollection does not exist)r7   r#   r   )r   r   r@   r=   r   r   r   get_collection   s
    
zSchema.get_collectionc             C   s&   | j jdtjt| jt|d dS )zmDrops a collection.

        Args:
            name (str): The name of the collection to be dropped.
        r1   FN)r   execute_nonquery_DROP_TABLE_QUERYr/   r   r   )r   r   r   r   r   drop_collection  s    zSchema.drop_collectionc             C   sF   |st dt| |}|j s6| jjddd| j| n|sBt d|S )a  Creates in the current schema a new collection with the specified
        name and retrieves an object representing the new collection created.

        Args:
            name (str): The name of the collection.
            reuse (bool): `True` to reuse an existing collection.

        Returns:
            mysqlx.Collection: Collection object.

        Raises:
            :class:`mysqlx.ProgrammingError`: If ``reuse`` is False and
                                              collection exists.
        zCollection name is invalidxplugincreate_collectionTzCollection already exists)r   r7   r#   r   rK   r   )r   r   Zreuser=   r   r   r   rO     s    

zSchema.create_collection)F)F)F)F)F)r&   r'   r(   r)   r   r#   r>   rA   rF   r?   rI   rJ   rM   rO   __classcell__r   r   )r-   r   r+      s   	
	



r+   c               @   sr   e Zd Z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dd Zdd Zdd Zdd ZdS )r7   zRepresents a collection of documents on a schema.

    Args:
        schema (mysqlx.Schema): The Schema object.
        name (str): The collection name.
    c             C   s*   t jt| jjt| j}| jj|dkS )zVerifies if this object exists in the database.

        Returns:
            bool: `True` if object exists in database.
        r   )_COUNT_TABLES_QUERYr/   r   r   r   r   r   r0   )r   r1   r   r   r   r#   -  s    zCollection.exists_in_databaseNc             C   s   t | |}| jj |_|S )zRetrieves documents from a collection.

        Args:
            condition (Optional[str]): The string with the filter expression of
                                       the documents to be retrieved.
        )r	   r   get_next_statement_idstmt_id)r   	conditionstmtr   r   r   find7  s    
zCollection.findc             G   s   t | j| S )zAdds a list of documents to a collection.

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

        Returns:
            mysqlx.AddStatement: AddStatement object.
        )r
   add)r   valuesr   r   r   rW   B  s    	zCollection.addc             C   s   t | |}| jj |_|S )ap  Removes documents based on the ``condition``.

        Args:
            condition (str): The string with the filter expression of the
                             documents to be removed.

        Returns:
            mysqlx.RemoveStatement: RemoveStatement object.

        .. versionchanged:: 8.0.12
           The ``condition`` parameter is now mandatory.
        )r   r   rR   rS   )r   rT   rU   r   r   r   removeN  s    
zCollection.removec             C   s   t | |}| jj |_|S )ar  Modifies documents based on the ``condition``.

        Args:
            condition (str): The string with the filter expression of the
                             documents to be modified.

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.

        .. versionchanged:: 8.0.12
           The ``condition`` parameter is now mandatory.
        )r   r   rR   rS   )r   rT   rU   r   r   r   modify_  s    
zCollection.modifyc             C   st   t jt| jjt| j}y| jj|}W nD tk
rn } z(|j	t
kr\tdj| j| jj W Y dd}~X nX |S )z}Counts the documents in the collection.

        Returns:
            int: The total of documents in the collection.
        z-Collection '{}' does not exist in schema '{}'N)_COUNT_QUERYr/   r   r   r   r   r   r0   r   errnor   )r   r1   reserrr   r   r   countp  s    
zCollection.countc             C   s   t | ||S )ab  Creates a collection index.

        Args:
            index_name (str): Index name.
            fields_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,
                                                "array": array,
                                                "collation": collation,
                                                "options": options,
                                                "srid": srid},
                                                # {... more members,
                                                #      repeated as many times
                                                #      as needed}
                                                ],
                                    "type": type}
        )r   )r   
index_nameZfields_descr   r   r   create_index  s    zCollection.create_indexc             C   s    | j jddd| jj| j| dS )z[Drops a collection index.

        Args:
            index_name (str): Index name.
        rN   Zdrop_collection_indexFN)r   rK   r   r   r   )r   r`   r   r   r   
drop_index  s    
zCollection.drop_indexc             C   s   | j djd|jd|j S )zReplaces the Document matching the document ID with a new document
        provided.

        Args:
            doc_id (str): Document ID
            doc (:class:`mysqlx.DbDoc` or `dict`): New Document
        z	_id = :id$id)rZ   setbindexecute)r   doc_iddocr   r   r   replace_one  s    zCollection.replace_onec             C   s,   t |tst|}| j|j|jdj S )zUpserts the Document matching the document ID with a new document
        provided.

        Args:
            doc_id (str): Document ID
            doc (:class:`mysqlx.DbDoc` or dict): New Document
        T)
isinstancer   rW   copyZupsertrg   )r   rh   ri   r   r   r   add_or_replace_one  s    
zCollection.add_or_replace_onec             C   s,   | j djd|j }|j }| jj  |S )zReturns a Document matching the Document ID.

        Args:
            doc_id (str): Document ID

        Returns:
            mysqlx.DbDoc: The Document matching the Document ID.
        z	_id = :idrd   )rV   rf   rg   Z	fetch_oner   Zfetch_active_result)r   rh   resultri   r   r   r   get_one  s    	
zCollection.get_onec             C   s   | j djd|j S )zRemoves a Document matching the Document ID.

        Args:
            doc_id (str): Document ID

        Returns:
            mysqlx.Result: Result object.
        z	_id = :idrd   )rY   rf   rg   )r   rh   r   r   r   
remove_one  s    	zCollection.remove_one)N)r&   r'   r(   r)   r#   rV   rW   rY   rZ   r_   ra   rb   rj   rm   ro   rp   r   r   r   r   r7   %  s   


r7   c               @   sH   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d Z
dS )rD   zRepresents a database table on a schema.

    Provides access to the table through standard INSERT/SELECT/UPDATE/DELETE
    statements.

    Args:
        schema (mysqlx.Schema): The Schema object.
        name (str): The table name.
    c             C   s*   t jt| jjt| j}| jj|dkS )zVerifies if this object exists in the database.

        Returns:
            bool: `True` if object exists in database.
        r   )rQ   r/   r   r   r   r   r   r0   )r   r1   r   r   r   r#     s    zTable.exists_in_databasec             G   s   t | f| }| jj |_|S )zCreates a new :class:`mysqlx.SelectStatement` object.

        Args:
            *fields: The fields to be retrieved.

        Returns:
            mysqlx.SelectStatement: SelectStatement object
        )r   r   rR   rS   )r   fieldsrU   r   r   r   select  s    	zTable.selectc             G   s   t | f| }| jj |_|S )zCreates a new :class:`mysqlx.InsertStatement` object.

        Args:
            *fields: The fields to be inserted.

        Returns:
            mysqlx.InsertStatement: InsertStatement object
        )r   r   rR   rS   )r   rq   rU   r   r   r   insert  s    	zTable.insertc             C   s   t | }| jj |_|S )zCreates a new :class:`mysqlx.UpdateStatement` object.

        Returns:
            mysqlx.UpdateStatement: UpdateStatement object
        )r   r   rR   rS   )r   rU   r   r   r   update  s    zTable.updatec             C   s   t | }| jj |_|S )zCreates a new :class:`mysqlx.DeleteStatement` object.

        Returns:
            mysqlx.DeleteStatement: DeleteStatement object

        .. versionchanged:: 8.0.12
           The ``condition`` parameter was removed.
        )r   r   rR   rS   )r   rU   r   r   r   delete  s    	zTable.deletec             C   st   t jt| jjt| j}y| jj|}W nD tk
rn } z(|j	t
kr\tdj| j| jj W Y dd}~X nX |S )ziCounts the rows in the table.

        Returns:
            int: The total of rows in the table.
        z(Table '{}' does not exist in schema '{}'N)r[   r/   r   r   r   r   r   r0   r   r\   r   )r   r1   r]   r^   r   r   r   r_     s    
zTable.countc             C   s*   t jt| jjt| j}| jj|dkS )zDetermine if the underlying object is a view or not.

        Returns:
            bool: `True` if the underlying object is a view.
        r   )_COUNT_VIEWS_QUERYr/   r   r   r   r   r   r0   )r   r1   r   r   r   is_view-  s    zTable.is_viewN)r&   r'   r(   r)   r#   rr   rs   rt   ru   r_   rw   r   r   r   r   rD     s   	

rD   c               @   s   e Zd ZdZdd ZdS )rG   zRepresents a database view on a schema.

    Provides a mechanism for creating, alter and drop views.

    Args:
        schema (mysqlx.Schema): The Schema object.
        name (str): The table name.
    c             C   s*   t jt| jjt| j}| jj|dkS )zVerifies if this object exists in the database.

        Returns:
            bool: `True` if object exists in database.
        r   )rv   r/   r   r   r   r   r   r0   )r   r1   r   r   r   r#   B  s    zView.exists_in_databaseN)r&   r'   r(   r)   r#   r   r   r   r   rG   8  s   rG   N)!r)   Zdbdocr   	errorcoder   errorsr   r   helpersr   r   r   Z	statementr	   r
   r   r   r   r   r   r   r   rv   rQ   r.   r[   rL   objectr   r+   r7   rD   rG   r   r   r   r   <module>   s"   ,f  1c