B
    Ñš[ãB  ã               @   s¸   d Z ddlmZ ddlmZmZmZmZ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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.é   )ÚProgrammingError)ÚFindStatementÚAddStatementÚRemoveStatementÚModifyStatementÚSelectStatementÚInsertStatementÚDeleteStatementÚUpdateStatementÚCreateCollectionIndexStatementÚDropCollectionIndexStatementÚCreateViewStatementÚAlterViewStatementÚCreateTableStatementz_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}'zMSELECT COUNT(*) FROM information_schema.schemata WHERE schema_name like '{0}'z SELECT COUNT(*) FROM `{0}`.`{1}`z DROP TABLE IF EXISTS `{0}`.`{1}`zDROP VIEW IF EXISTS `{0}`.`{1}`c               @   sX   e Zd ZdZ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dS )ÚDatabaseObjectz¢Provides base functionality for database objects.

    Args:
        schema (mysqlx.Schema): The Schema object.
        name (str): The database object name.
    c             C   s   || _ || _| j  ¡ j| _d S )N)Ú_schemaÚ_nameÚget_sessionÚ_connection)ÚselfÚschemaÚname© 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\crud.pyÚ__init__5   s    zDatabaseObject.__init__c             C   s   | j S )z3:class:`mysqlx.Schema`: The Schema object.
        )r   )r   r   r   r   r   :   s    zDatabaseObject.schemac             C   s   | j S )z/str: The name of this database object.
        )r   )r   r   r   r   r   @   s    zDatabaseObject.namec             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_schemaF   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_nameN   s    zDatabaseObject.get_namec             C   s   t ‚dS )zÓVerifies 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_databaseV   s    	z!DatabaseObject.exists_in_databasec             C   s   |   ¡ S )N)r   )r   r   r   r   Ú	am_i_reala   s    zDatabaseObject.am_i_realc             C   s   |   ¡ S )N)r   )r   r   r   r   Úwho_am_id   s    zDatabaseObject.who_am_iN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r   r   r   r    r   r   r   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d„Zdd„ Z	d$dd„Z
d%dd„Zd&dd„Zdd„ Zdd„ Zdd„ Zd'dd„Zd(dd„Zdd „ Zd)d!d"„Z‡  ZS )*ÚSchemazÆA 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| ƒ | |¡ d S )N)Ú_sessionÚsuperr&   r   )r   Úsessionr   )Ú	__class__r   r   r   p   s    zSchema.__init__c             C   s   t  | j¡}| j |¡dkS )z�Verifies if this object exists in the database.

        Returns:
            bool: `True` if object exists in database.
        r   )Ú_COUNT_SCHEMAS_QUERYÚformatr   r   Úexecute_sql_scalar)r   Úsqlr   r   r   r   t   s    zSchema.exists_in_databasec             C   s   | j S )zuReturns the session of this Schema object.

        Returns:
            mysqlx.Session: The Session object.
        )r'   )r   r   r   r   r   }   s    zSchema.get_sessionc          	   C   s‚   | j  d| j¡}| ¡  g }x`|D ]X}| d¡dkr6q"yt| | d¡ƒ}W n$ tk
rn   t| | d¡ƒ}Y nX | |¡ q"W |S )zwReturns 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Ú
get_stringÚ
CollectionÚ
ValueErrorÚappend)r   ÚrowsÚcollectionsÚrowÚ
collectionr   r   r   Úget_collections…   s    
zSchema.get_collectionsFc             C   s   |   ||¡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  d| j¡}| ¡  g }d}x^|D ]V}| d¡|kr&yt| | d¡ƒ}W n$ tk
rp   t| | d¡ƒ}Y nX | |¡ q&W |S )zmReturns a list of tables for this schema.

        Returns:
            list: List of Table objects.
        r/   )ZTABLEZVIEWr0   r1   r   )r   r2   r   r3   r4   ÚTabler6   r7   )r   r8   ÚtablesZobject_typesr:   Útabler   r   r   Ú
get_tables¡   s    
zSchema.get_tablesc             C   s"   t | |ƒ}|r| ¡ stdƒ‚|S )zwReturns the table of the given name for this schema.

        Returns:
            mysqlx.Table: Table object.
        zTable does not exist)r@   r   r   )r   r   r>   rB   r   r   r   r=   ´   s
    
zSchema.get_tablec             C   s"   t | |ƒ}|r| ¡ 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| ¡ stdƒ‚|S )z†Returns the collection of the given name for this schema.

        Returns:
            mysqlx.Collection: Collection object.
        zCollection does not exist)r5   r   r   )r   r   r>   r;   r   r   r   Úget_collectionÌ   s
    
zSchema.get_collectionc             C   s   | j  dt | j|¡d¡ dS )zmDrops a collection.

        Args:
            name (str): The name of the collection to be dropped.
        r.   FN)r   Úexecute_nonqueryÚ_DROP_TABLE_QUERYr,   r   )r   r   r   r   r   Údrop_collectionØ   s    zSchema.drop_collectionc             C   s<   t | |ƒ}| ¡ r|  |¡ n| j dt | j|¡d¡ dS )zcDrops a table.

        Args:
            name (str): The name of the table to be dropped.
        r.   FN)r@   Úis_viewÚ	drop_viewr   rH   rI   r,   r   )r   r   rB   r   r   r   Ú
drop_tableá   s
    
zSchema.drop_tablec             C   s   | j  dt | j|¡d¡ dS )zaDrops a view.

        Args:
            name (str): The name of the view to be dropped.
        r.   FN)r   rH   Ú_DROP_VIEW_QUERYr,   r   )r   r   r   r   r   rL   î   s    zSchema.drop_viewc             C   sF   |st dƒ‚t| |ƒ}| ¡ s6| 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:
            ProgrammingError: If ``reuse`` is False and collection exists.
        zCollection name is invalidZxpluginÚcreate_collectionTzCollection already exists)r   r5   r   r   rH   r   )r   r   Úreuser;   r   r   r   rO   ÷   s    

zSchema.create_collectionc             C   s   t | |ƒ}| |¡S )aA  Creates in the current schema a new view with the specified name
        and retrieves an object representing the new view created.

        Args:
            name (string): The name of the view.
            replace (Optional[bool]): `True` to add replace.

        Returns:
            mysqlx.View: View object.
        )rD   Úget_create_statement)r   r   ÚreplacerE   r   r   r   Úcreate_view  s    
zSchema.create_viewc             C   s   t | |ƒ}| ¡ S )zóAlters a view in the current schema with the specified name and
        retrieves an object representing the view.

        Args:
            name (string): The name of the view.

        Returns:
            mysqlx.View: View object.
        )rD   Úget_alter_statement)r   r   rE   r   r   r   Ú
alter_view  s    

zSchema.alter_viewc             C   s8   |st dƒ‚t| |ƒ}| ¡ s(t| |ƒS |s4t dƒ‚|S )NzTable name is invalidzTable already exists)r   r@   r   r   )r   r   rP   rB   r   r   r   Úcreate_table*  s    

zSchema.create_table)F)F)F)F)F)F)F)r!   r"   r#   r$   r   r   r   r<   r?   rC   r=   rF   rG   rJ   rM   rL   rO   rS   rU   rV   Ú__classcell__r   r   )r*   r   r&   h   s"   	
	


		

r&   c                   sn   e Zd ZdZ‡ fdd„Zdd„ Zddd„Zd	d
„ Zdd„ Zddd„Z	ddd„Z
dd„ Zdd„ Zdd„ Z‡  ZS )r5   z�Represents a collection of documents on a schema.

    Args:
        schema (mysqlx.Schema): The Schema object.
        name (str): The collection name.
    c                s   t t| ƒ ||¡ d S )N)r(   r5   r   )r   r   r   )r*   r   r   r   <  s    zCollection.__init__c             C   s$   t  | j ¡ | j¡}| j |¡dkS )z�Verifies if this object exists in the database.

        Returns:
            bool: `True` if object exists in database.
        r   )Ú_COUNT_TABLES_QUERYr,   r   r   r   r   r-   )r   r.   r   r   r   r   ?  s    zCollection.exists_in_databaseNc             C   s
   t | |ƒS )zÔRetrieves documents from a collection.

        Args:
            condition (Optional[str]): The string with the filter expression of
                                       the documents to be retrieved.
        )r   )r   Ú	conditionr   r   r   ÚfindH  s    zCollection.findc             G   s   t | ƒj|Ž S )zÑAdds 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   r[   Q  s    	zCollection.addc             C   s   |   d |¡¡S )z¤Removes document by ID.

        Args:
            id (str): The document ID.

        Returns:
            mysqlx.RemoveStatement: RemoveStatement object.
        z_id = '{0}')Úremover,   )r   Úidr   r   r   Ú
remove_one\  s    	zCollection.remove_onec             C   s   t | ƒ}|r| |¡ |S )a'  Removes documents based on the ``condition``.

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

        Returns:
            mysqlx.RemoveStatement: RemoveStatement object.
        )r   Úwhere)r   rY   Úrsr   r   r   r]   g  s    

zCollection.removec             C   s
   t | |ƒS )a)  Modifies documents based on the ``condition``.

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

        Returns:
            mysqlx.ModifyStatement: ModifyStatement object.
        )r   )r   rY   r   r   r   Úmodifyv  s    
zCollection.modifyc             C   s   t  | jj| j¡}| j |¡S )z}Counts the documents in the collection.

        Returns:
            int: The total of documents in the collection.
        )Ú_COUNT_QUERYr,   r   r   r   r   r-   )r   r.   r   r   r   Úcount‚  s    zCollection.countc             C   s   t | ||ƒS )zšCreates a collection index.

        Args:
            index_name (str): Index name.
            is_unique (bool): `True` if the index is unique.
        )r   )r   Ú
index_nameZ	is_uniquer   r   r   Úcreate_index‹  s    zCollection.create_indexc             C   s
   t | |ƒS )z[Drops a collection index.

        Args:
            index_name (str): Index name.
        )r   )r   re   r   r   r   Ú
drop_index”  s    zCollection.drop_index)N)N)N)r!   r"   r#   r$   r   r   rZ   r[   r_   r]   rb   rd   rf   rg   rW   r   r   )r*   r   r5   5  s   	
	

		r5   c                   sZ   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Z	dd„ Z
dd„ Z‡  ZS )r@   zîRepresents 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                s   t t| ƒ ||¡ d S )N)r(   r@   r   )r   r   r   )r*   r   r   r   §  s    zTable.__init__c             C   s"   t  | jj| j¡}| j |¡dkS )z�Verifies if this object exists in the database.

        Returns:
            bool: `True` if object exists in database.
        r   )rX   r,   r   r   r   r   r-   )r   r.   r   r   r   r   ª  s    zTable.exists_in_databasec             G   s   t | f|žŽ S )zËCreates a new :class:`mysqlx.SelectStatement` object.

        Args:
            *fields: The fields to be retrieved.

        Returns:
            mysqlx.SelectStatement: SelectStatement object
        )r   )r   Úfieldsr   r   r   Úselect³  s    	zTable.selectc             G   s   t | f|žŽ S )zÊCreates a new :class:`mysqlx.InsertStatement` object.

        Args:
            *fields: The fields to be inserted.

        Returns:
            mysqlx.InsertStatement: InsertStatement object
        )r   )r   rh   r   r   r   Úinsert¾  s    	zTable.insertc             C   s   t | ƒS )zÅCreates a new :class:`mysqlx.UpdateStatement` object.

        Args:
            *fields: The fields to update.

        Returns:
            mysqlx.UpdateStatement: UpdateStatement object
        )r
   )r   r   r   r   ÚupdateÉ  s    	zTable.updateNc             C   s
   t | |ƒS )a)  Creates a new :class:`mysqlx.DeleteStatement` object.

        Args:
            condition (Optional[str]): The string with the filter expression of
                                       the rows to be deleted.

        Returns:
            mysqlx.DeleteStatement: DeleteStatement object
        )r	   )r   rY   r   r   r   ÚdeleteÔ  s    
zTable.deletec             C   s   t  | jj| j¡}| j |¡S )ziCounts the rows in the table.

        Returns:
            int: The total of rows in the table.
        )rc   r,   r   r   r   r   r-   )r   r.   r   r   r   rd   à  s    zTable.countc             C   s$   t  | j ¡ | j¡}| j |¡dkS )zŒDetermine 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-   )r   r.   r   r   r   rK   é  s    zTable.is_view)N)r!   r"   r#   r$   r   r   ri   rj   rk   rl   rd   rK   rW   r   r   )r*   r   r@   �  s   		
	r@   c                   s:   e Zd ZdZ‡ fdd„Zdd„ Zddd„Zd	d
„ Z‡  ZS )rD   zÌRepresents 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                s   t t| ƒ ||¡ d S )N)r(   rD   r   )r   r   r   )r*   r   r   r   ý  s    zView.__init__c             C   s"   t  | jj| j¡}| j |¡dkS )z�Verifies if this object exists in the database.

        Returns:
            bool: `True` if object exists in database.
        r   )rm   r,   r   r   r   r   r-   )r   r.   r   r   r   r      s    zView.exists_in_databaseFc             C   s
   t | |ƒS )zäCreates a new :class:`mysqlx.CreateViewStatement` object.

        Args:
            replace (Optional[bool]): `True` to add replace.

        Returns:
            mysqlx.CreateViewStatement: CreateViewStatement object.
        )r   )r   rR   r   r   r   rQ   	  s    	zView.get_create_statementc             C   s   t | ƒS )z•Creates a new :class:`mysqlx.AlterViewStatement` object.

        Returns:
            mysqlx.AlterViewStatement: AlterViewStatement object.
        )r   )r   r   r   r   rT     s    zView.get_alter_statement)F)	r!   r"   r#   r$   r   r   rQ   rT   rW   r   r   )r*   r   rD   ó  s
   	
rD   N)r$   Úerrorsr   Z	statementr   r   r   r   r   r   r	   r
   r   r   r   r   r   rm   rX   r+   rc   rI   rN   Úobjectr   r&   r5   r@   rD   r   r   r   r   Ú<module>   s   <: NhV