U eB@sdZddlmZddlmZmZmZmZmZm Z m Z m Z m Z m Z mZmZmZdZdZdZdZdZd ZGd d d eZGd d d eZGdddeZGdddeZGdddeZdS)z,Implementation of the CRUD database objects.)ProgrammingError) FindStatement AddStatementRemoveStatementModifyStatementSelectStatementInsertStatementDeleteStatementUpdateStatementCreateCollectionIndexStatementDropCollectionIndexStatementCreateViewStatementAlterViewStatementCreateTableStatementz_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}`zDROP VIEW IF EXISTS `{0}`.`{1}`c@sXeZdZdZddZeddZeddZdd Zd d Z d d Z ddZ ddZ dS)DatabaseObjectzProvides base functionality for database objects. Args: schema (mysqlx.Schema): The Schema object. name (str): The database object name. cCs||_||_|jj|_dSN)_schema_name get_session _connectionselfschemanamer`/opt/hc_python/lib64/python3.8/site-packages/../../../lib/python3.8/site-packages/mysqlx/crud.py__init__5szDatabaseObject.__init__cCs|jS)z3:class:`mysqlx.Schema`: The Schema object. rrrrrr:szDatabaseObject.schemacCs|jS)z/str: The name of this database object. rrrrrr@szDatabaseObject.namecCs|jS)z{Returns the Schema object of this database object. Returns: mysqlx.Schema: The Schema object. rrrrr get_schemaFszDatabaseObject.get_schemacCs|jS)zwReturns the name of this database object. Returns: str: The name of this database object. rrrrrget_nameNszDatabaseObject.get_namecCstdS)zVerifies if this object exists in the database. Returns: bool: `True` if object exists in database. Raises: NotImplementedError: This method must be implemented. N)NotImplementedErrorrrrrexists_in_databaseVs z!DatabaseObject.exists_in_databasecCs|Sr)r#rrrr am_i_realaszDatabaseObject.am_i_realcCs|Sr)r!rrrrwho_am_idszDatabaseObject.who_am_iN) __name__ __module__ __qualname____doc__rpropertyrrr r!r#r$r%rrrrr.s   rcseZdZdZfddZddZddZdd Zd#d d Zd dZ d$ddZ d%ddZ d&ddZ ddZ ddZddZd'ddZd(ddZdd Zd)d!d"ZZS)*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. cs||_tt|||dSr)_sessionsuperr+r)rsessionr __class__rrrpszSchema.__init__cCst|j}|j|dkSzVerifies if this object exists in the database. Returns: bool: `True` if object exists in database. r)_COUNT_SCHEMAS_QUERYformatrrexecute_sql_scalarrsqlrrrr#ts zSchema.exists_in_databasecCs|jS)zuReturns the session of this Schema object. Returns: mysqlx.Session: The Session object. )r,rrrrr}szSchema.get_sessionc Cs~|jd|j}|g}|D]X}|ddkr4q zt||d}Wn$tk rlt||d}YnX||q |S)zwReturns a list of collections for this schema. Returns: list: List of Collection objects. list_objectstypeZ COLLECTION TABLE_NAMEr)rget_row_resultr fetch_all get_string Collection ValueErrorappend)rrows collectionsrow collectionrrrget_collectionss zSchema.get_collectionsFcCs |||S)zuReturns a a table object for the given collection Returns: mysqlx.Table: Table object. ) get_table)rrcheck_existencerrrget_collection_as_tableszSchema.get_collection_as_tablec Cs|jd|j}|g}d}|D]V}|d|kr$zt||d}Wn$tk rnt||d}YnX||q$|S)zmReturns a list of tables for this schema. Returns: list: List of Table objects. r7)ZTABLEZVIEWr8r9r)rr:rr;r<Tabler>r?)rr@ZtablesZ object_typesrBtablerrr get_tabless zSchema.get_tablescCs"t||}|r|std|S)zwReturns the table of the given name for this schema. Returns: mysqlx.Table: Table object. zTable does not exist)rHr#r)rrrFrIrrrrEs  zSchema.get_tablecCs"t||}|r|std|S)ztReturns the view of the given name for this schema. Returns: mysqlx.View: View object. zView does not exist)Viewr#r)rrrFviewrrrget_views  zSchema.get_viewcCs"t||}|r|std|S)zReturns the collection of the given name for this schema. Returns: mysqlx.Collection: Collection object. zCollection does not exist)r=r#r)rrrFrCrrrget_collections  zSchema.get_collectioncCs|jdt|j|ddS)zmDrops a collection. Args: name (str): The name of the collection to be dropped. r6FN)rexecute_nonquery_DROP_TABLE_QUERYr3rrrrrrdrop_collections  zSchema.drop_collectioncCs<t||}|r||n|jdt|j|ddS)zcDrops a table. Args: name (str): The name of the table to be dropped. r6FN)rHis_view drop_viewrrOrPr3r)rrrIrrr drop_tables   zSchema.drop_tablecCs|jdt|j|ddS)zaDrops a view. Args: name (str): The name of the view to be dropped. r6FN)rrO_DROP_VIEW_QUERYr3rrQrrrrTs  zSchema.drop_viewcCsF|s tdt||}|s6|jddd|j|n |sBtd|S)aCreates 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. zCollection name is invalidZxplugincreate_collectionTzCollection already exists)rr=r#rrOr)rrreuserCrrrrWs  zSchema.create_collectioncCst||}||S)aACreates 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. )rKget_create_statement)rrreplacerLrrr create_views zSchema.create_viewcCst||}|S)zAlters 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. )rKget_alter_statement)rrrLrrr alter_views zSchema.alter_viewcCs8|s tdt||}|s(t||S|s4td|S)NzTable name is invalidzTable already exists)rrHr#r)rrrXrIrrr create_table*s  zSchema.create_table)F)F)F)F)F)F)F)r&r'r(r)rr#rrDrGrJrErMrNrRrUrTrWr[r]r^ __classcell__rrr/rr+hs"         r+csneZdZdZfddZddZdddZd d Zd d Zdd dZ dddZ ddZ ddZ ddZ ZS)r=zRepresents a collection of documents on a schema. Args: schema (mysqlx.Schema): The Schema object. name (str): The collection name. cstt|||dSr)r-r=rrr/rrr<szCollection.__init__cCs$t|j|j}|j|dkSr1)_COUNT_TABLES_QUERYr3rr!rrr4r5rrrr#?szCollection.exists_in_databaseNcCs t||S)zRetrieves documents from a collection. Args: condition (Optional[str]): The string with the filter expression of the documents to be retrieved. )rr conditionrrrfindHszCollection.findcGst|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. )radd)rvaluesrrrrdQs zCollection.addcCs|d|S)zRemoves document by ID. Args: id (str): The document ID. Returns: mysqlx.RemoveStatement: RemoveStatement object. z _id = '{0}')remover3)ridrrr remove_one\s zCollection.remove_onecCst|}|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. )rwhere)rrbrsrrrrfgs  zCollection.removecCs 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. )rrarrrmodifyvs zCollection.modifycCst|jj|j}|j|S)z}Counts the documents in the collection. Returns: int: The total of documents in the collection.  _COUNT_QUERYr3rrrrr4r5rrrcountszCollection.countcCs t|||S)zCreates a collection index. Args: index_name (str): Index name. is_unique (bool): `True` if the index is unique. )r )r index_nameZ is_uniquerrr create_indexszCollection.create_indexcCs t||S)z[Drops a collection index. Args: index_name (str): Index name. )r )rrorrr drop_indexszCollection.drop_index)N)N)N)r&r'r(r)rr#rcrdrhrfrkrnrprqr_rrr/rr=5s       r=csZeZdZdZfddZddZddZdd Zd d Zdd dZ ddZ ddZ Z S)rHzRepresents 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. cstt|||dSr)r-rHrrr/rrrszTable.__init__cCs"t|jj|j}|j|dkSr1)r`r3rrrrr4r5rrrr#szTable.exists_in_databasecGst|f|S)zCreates a new :class:`mysqlx.SelectStatement` object. Args: *fields: The fields to be retrieved. Returns: mysqlx.SelectStatement: SelectStatement object )rrfieldsrrrselects z Table.selectcGst|f|S)zCreates a new :class:`mysqlx.InsertStatement` object. Args: *fields: The fields to be inserted. Returns: mysqlx.InsertStatement: InsertStatement object )rrrrrrinserts z Table.insertcCst|S)zCreates a new :class:`mysqlx.UpdateStatement` object. Args: *fields: The fields to update. Returns: mysqlx.UpdateStatement: UpdateStatement object )r rrrrupdates z Table.updateNcCs 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 rarrrdeletes z Table.deletecCst|jj|j}|j|S)ziCounts the rows in the table. Returns: int: The total of rows in the table. rlr5rrrrnsz Table.countcCs$t|j|j}|j|dkS)zDetermine if the underlying object is a view or not. Returns: bool: `True` if the underlying object is a view. r)_COUNT_VIEWS_QUERYr3rr!rrr4r5rrrrSsz Table.is_view)N) r&r'r(r)rr#rtrurvrwrnrSr_rrr/rrHs      rHcs:eZdZdZfddZddZd ddZd d ZZS) rKzRepresents 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. cstt|||dSr)r-rKrrr/rrrsz View.__init__cCs"t|jj|j}|j|dkSr1)rxr3rrrrr4r5rrrr#szView.exists_in_databaseFcCs t||S)zCreates a new :class:`mysqlx.CreateViewStatement` object. Args: replace (Optional[bool]): `True` to add replace. Returns: mysqlx.CreateViewStatement: CreateViewStatement object. )r )rrZrrrrY s zView.get_create_statementcCst|S)zCreates a new :class:`mysqlx.AlterViewStatement` object. Returns: mysqlx.AlterViewStatement: AlterViewStatement object. )rrrrrr\szView.get_alter_statement)F) r&r'r(r)rr#rYr\r_rrr/rrKs   rKN)r)errorsrZ statementrrrrrrr r r r r rrrxr`r2rmrPrVobjectrr+r=rHrKrrrrs <:NhV