
    :?hg                    n   S r SSKJr  SSKrSSKrSSKJrJrJrJ	r	J
r
Jr  SSKJr  SSKJrJrJrJr  SSKJrJrJr  SS	KJrJrJr  SS
KJrJrJrJrJ r J!r!J"r"J#r#J$r$  SSK%J&r&J'r'J(r(J)r)  \(       a  SSK*J+r+  Sr,Sr-Sr.Sr/Sr0 " S S5      r1 " S S\15      r2 " S S\15      r3 " S S\15      r4 " S S\45      r5g)z,Implementation of the CRUD database objects.    )annotationsN)TYPE_CHECKINGAnyDictListOptionalUnion   )DbDoc)ER_NO_SUCH_TABLEER_TABLE_EXISTS_ERRORER_X_CMD_NUM_ARGUMENTSER_X_INVALID_ADMIN_COMMAND)NotSupportedErrorOperationalErrorProgrammingError)
deprecatedescapequote_identifier)	AddStatementCreateCollectionIndexStatementDeleteStatementFindStatementInsertStatementModifyStatementRemoveStatementSelectStatementUpdateStatement)ConnectionType
SchemaTypeSessionType
StrOrBytes)Resultz_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                      \ rS rSrSrSS jr\SS j5       r\SS j5       r\SS j5       r	SS jr
SS jrSS	 jrSS
 jrSS jr\" SS5      SS j5       r\" SS5      SS j5       rSrg)DatabaseObjectN   zProvides base functionality for database objects.

Args:
    schema (mysqlx.Schema): The Schema object.
    name (str): The database object name.
c                    Xl         [        U[        5      (       a  UR                  5       OUU l        U R                   R                  5       U l        U R                  R                  5       U l        g N)	_schema
isinstancebytesdecode_nameget_session_sessionget_connection_connection)selfschemanames      S/var/www/ev-aguas/ev-aguas-proceso/venv/lib/python3.13/site-packages/mysqlx/crud.py__init__DatabaseObject.__init__V   sK    #)+5dE+B+B$++-
%)\\%=%=%?+/==+G+G+I    c                    U R                   $ )z,:class:`mysqlx.Session`: The Session object.r/   r2   s    r5   sessionDatabaseObject.session\   s     }}r8   c                    U R                   $ )z*:class:`mysqlx.Schema`: The Schema object.r)   r;   s    r5   r3   DatabaseObject.schemaa   s     ||r8   c                    U R                   $ )z&str: The name of this database object.r-   r;   s    r5   r4   DatabaseObject.namef   s     zzr8   c                    U R                   $ )zfReturns the underlying connection.

Returns:
    mysqlx.connection.Connection: The connection object.
)r1   r;   s    r5   r0   DatabaseObject.get_connectionk   s     r8   c                    U R                   $ )z_Returns the session of this database object.

Returns:
    mysqlx.Session: The Session object.
r:   r;   s    r5   r.   DatabaseObject.get_sessions   s     }}r8   c                    U R                   $ )zcReturns the Schema object of this database object.

Returns:
    mysqlx.Schema: The Schema object.
r?   r;   s    r5   
get_schemaDatabaseObject.get_schema{   s     ||r8   c                    U R                   $ )z_Returns the name of this database object.

Returns:
    str: The name of this database object.
rB   r;   s    r5   get_nameDatabaseObject.get_name   s     zzr8   c                    [         e)zVerifies if this object exists in the database.

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

Raises:
   NotImplementedError: This method must be implemented.
)NotImplementedErrorr;   s    r5   exists_in_database!DatabaseObject.exists_in_database   s
     "!r8   z8.0.12z)Use 'exists_in_database()' method insteadc                "    U R                  5       $ )zVerifies 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.
)rP   r;   s    r5   	am_i_realDatabaseObject.am_i_real   s     &&((r8   zUse 'get_name()' method insteadc                "    U R                  5       $ )zReturns the name of this database object.

Returns:
    str: The name of this database object.

.. deprecated:: 8.0.12
   Use ``get_name()`` method instead.
)rL   r;   s    r5   who_am_iDatabaseObject.who_am_i   s     }}r8   )r1   r-   r)   r/   N)r3   r    r4   r"   returnNone)rX   r!   )rX   r    )rX   str)rX   r   )rX   r   )__name__
__module____qualname____firstlineno____doc__r6   propertyr<   r3   r4   r0   r.   rI   rL   rP   r   rS   rV   __static_attributes__ r8   r5   r%   r%   N   s    J       	" EF) G) ;<	 =	r8   r%   c                     ^  \ rS rSrSrSU 4S jjrSS jrSS jr S     SS jjrSS jr	SSS jjr
SSS	 jjrSSS
 jjrSS jr  S         SS jjr S     SS jjrSrU =r$ )Schema   zA 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                0   > Xl         [        TU ]	  X5        g r(   )r/   superr6   )r2   r<   r4   	__class__s      r5   r6   Schema.__init__   s    %,$r8   c                    [         R                  [        U R                  5      5      nU R                  R                  U5      S:H  $ ziVerifies if this object exists in the database.

Returns:
    bool: `True` if object exists in database.
r
   )_COUNT_SCHEMAS_QUERYformatr   r-   r1   execute_sql_scalarr2   sqls     r5   rP   Schema.exists_in_database   s9     #))&*<=22371<<r8   c                   U R                   R                  SSU R                  05      nUR                  5         / nU H.  nUS   S:w  a  M   [	        XS   5      nUR                  U5        M0     U$ ! [
         a    [	        XS   5      n N1f = f)zaReturns a list of collections for this schema.

Returns:
    `list`: List of Collection objects.
list_objectsr3   type
COLLECTION
TABLE_NAMEr4   )r1   get_row_resultr-   	fetch_all
Collection
ValueErrorappend)r2   rowscollectionsrow
collections        r5   get_collectionsSchema.get_collections   s     ..~$**?UVC6{l*;',.?@
 z*    ;'&k:
;s   A11BBc                $    U R                  X5      $ )z]Returns a a table object for the given collection

Returns:
    mysqlx.Table: Table object.

)	get_table)r2   r4   check_existences      r5   get_collection_as_tableSchema.get_collection_as_table   s     ~~d44r8   c                "   U R                   R                  SSU R                  05      nUR                  5         / nSnU H.  nUS   U;   d  M   [	        XS   5      nUR                  U5        M0     U$ ! [
         a    [	        XS   5      n N1f = f)zWReturns a list of tables for this schema.

Returns:
    `list`: List of Table objects.
rs   r3   )TABLEVIEWrt   rv   r4   )r1   rw   r-   rx   Tablerz   r{   )r2   r|   tablesobject_typesr~   tables         r5   
get_tablesSchema.get_tables   s     ..~$**?UV
 C6{l*5!$L(9:E e$   " 5!$F4E5s   A33BBc                j    [        X5      nU(       a   UR                  5       (       d  [        S5      eU$ )z_Returns the table of the given name for this schema.

Returns:
    mysqlx.Table: Table object.
zTable does not exist)r   rP   r   )r2   r4   r   r   s       r5   r   Schema.get_table   s1     d!++--&'=>>r8   c                j    [        X5      nU(       a   UR                  5       (       d  [        S5      eU$ )z\Returns the view of the given name for this schema.

Returns:
    mysqlx.View: View object.
zView does not exist)ViewrP   r   )r2   r4   r   views       r5   get_viewSchema.get_view  s1     D**,,&'<==r8   c                j    [        X5      nU(       a   UR                  5       (       d  [        S5      eU$ )znReturns the collection of the given name for this schema.

Returns:
    mysqlx.Collection: Collection object.
zCollection does not exist)ry   rP   r   )r2   r4   r   r   s       r5   get_collectionSchema.get_collection  s2      +
0022&'BCCr8   c           	         U R                   R                  S[        R                  [	        U R
                  5      [	        U5      5      S5        g)zUDrops a collection.

Args:
    name (str): The name of the collection to be dropped.
rp   FN)r1   execute_nonquery_DROP_TABLE_QUERYrm   r   r-   )r2   r4   s     r5   drop_collectionSchema.drop_collection   sB     	))$$ ,.>t.D 	
r8   c                   U(       d  [        S5      eSU;   a   [        R                  " S[        5        US   n[	        X5      nU R
                  US.nUb  [        U[        5      (       a  U(       d  [        S5      eSnU H  nX;  d  M
  [        SU 35      e   / n	S	U;   a8  US	   n
[        U
[        5      (       d  [        S
5      eU	R                  S	U
45        SU;   ai  US   n[        U[        [        45      (       d  [        S5      eU	R                  S[        U[        5      (       a  [        R                  " U5      OU45        SU	4US'    U R                  R                  SSSU5        U$ ! [         aw  nUR                  [         :X  a  [#        S5      UeUR                  [$        :X  a  U(       d  [        SU S35      Ue SnAU$ [        UR&                  UR                  5      UeSnAff = f)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_existing (bool): `True` to reuse an existing collection.
    validation (Optional[dict]): A dict, containing the keys `level`
                                 with the validation level and `schema`
                                 with a dict or a string representation
                                 of a JSON schema specification.

Returns:
    mysqlx.Collection: Collection object.

Raises:
    :class:`mysqlx.ProgrammingError`: If ``reuse_existing`` is False
                                      and collection exists or the
                                      collection name is invalid.
    :class:`mysqlx.NotSupportedError`: If schema validation is not
                                       supported by the server.

.. versionchanged:: 8.0.21
Collection name is invalidreusezG'reuse' is deprecated since 8.0.21. Please use 'reuse_existing' instead)r3   r4   NInvalid value for 'validation'levelr3    Invalid option in 'validation': r   Invalid value for 'level'r3   Invalid value for 'schema'
validationoptionsmysqlxcreate_collectionTlYour MySQL server does not support the requested operation. Please update to MySQL 8.0.19 or a later versionCollection 'z' already exists)r   warningswarnDeprecationWarningry   r-   r*   dictrZ   r{   jsondumpsr1   r   r   errnor   r   r   msg)r2   r4   reuse_existingr   kwargsr   fieldsvalid_optionsoptionr   r   r3   errs                r5   r   Schema.create_collection.  s   < "#?@@fMM6"
 $G_N+
,0JJ!E!j$//z&'GHH/M$.*-MfX+VWW % G*$"7+!%--*+FGG/0:%#H-!&3+66*+GHH .8.F.F

6*F ".w 7F9	D---tV$    	Dyy22' 	
 yy11%*&tf,<= &  'sww		:C	Ds   E2 2
G3<AG.!G..G3c                   U(       d  [        S5      e[        U[        5      (       a  U(       d  [        S5      eSnU H  nXC;  d  M
  [        SU 35      e   / nSU;   a8  US   n[        U[        5      (       d  [        S5      eUR	                  SU45        SU;   ai  US   n[        U[        [        45      (       d  [        S5      eUR	                  S[        U[        5      (       a  [
        R                  " U5      OU45        U R                  US	U4S
.n U R                  R                  SSSU5        g! [         aF  n	U	R                  [        :X  a  [        S5      U	e[        U	R                  U	R                  5      U	eSn	A	ff = f)a  Modifies a collection using a JSON schema validation.

Args:
    name (str): The name of the collection.
    validation (Optional[dict]): A dict, containing the keys `level`
                                 with the validation level and `schema`
                                 with a dict or a string representation
                                 of a JSON schema specification.

Raises:
    :class:`mysqlx.ProgrammingError`: If the collection name or
                                      validation is invalid.
    :class:`mysqlx.NotSupportedError`: If schema validation is not
                                       supported by the server.

.. versionadded:: 8.0.21
r   r   r   r   r   r   r3   r   r   )r3   r4   r   r   modify_collection_optionsTr   N)r   r*   r   rZ   r{   r   r   r-   r1   r   r   r   r   r   r   )
r2   r4   r   r   r   r   r   r3   r   r   s
             r5   modify_collectionSchema.modify_collection  s   ( "#?@@*d++:"#CDD+ F*&)I&'RSS ! j w'EeS))&'BCCNNGU+,z!)FfsDk22&'CDDNN*4VT*B*BDJJv& jj$g.
	@--5tV   	@yy66' 	
 #377CII6C?	@s   D9 9
F	AFF	r:   )r<   r!   r4   rZ   rX   rY   rX   bool)rX   zList[Collection])F)r4   rZ   r   r   rX   r   )rX   zList[Table])r4   rZ   r   r   rX   r   )r4   rZ   r   r   rX   ry   )r4   rZ   rX   rY   )FN)
r4   rZ   r   r   r   %Optional[Dict[str, Union[str, Dict]]]r   r   rX   ry   r(   )r4   rZ   r   r   rX   rY   )r[   r\   r]   r^   r_   r6   rP   r   r   r   r   r   r   r   r   r   ra   __classcell__)rh   s   @r5   rd   rd      s    %=( 27	5	5*.	5		5,



"  %<@	]] ] :	]
 ] 
]@ NRB@B@%JB@	B@ B@r8   rd   c                      \ rS rSrSrSS jrSSS jjrSS jrSS jrSS jr	SS	 jr
      SS
 jrSS jrSS jrSS jrSS jrSS jrSrg)ry   i  zRepresents a collection of documents on a schema.

Args:
    schema (mysqlx.Schema): The Schema object.
    name (str): The collection name.
c                    [         R                  [        U R                  R                  5      [        U R
                  5      5      nU R                  R                  U5      S:H  $ rk   _COUNT_TABLES_QUERYrm   r   r)   r4   r-   r1   rn   ro   s     r5   rP   Collection.exists_in_database  J     "((0A0A)BF4::DVW22371<<r8   Nc                Z    [        X5      nU R                  R                  5       Ul        U$ )zRetrieves documents from a collection.

Args:
    condition (Optional[str]): The string with the filter expression of
                               the documents to be retrieved.
)r   r1   get_next_statement_idstmt_idr2   	conditionstmts      r5   findCollection.find  s)     T-''==?r8   c                2    [        U 5      R                  " U6 $ )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)r2   valuess     r5   r   Collection.add  s     D!%%v..r8   c                Z    [        X5      nU R                  R                  5       Ul        U$ )a0  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   r1   r   r   r   s      r5   removeCollection.remove  )     t/''==?r8   c                Z    [        X5      nU R                  R                  5       Ul        U$ )a2  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   r1   r   r   r   s      r5   modifyCollection.modify
  r   r8   c                z   [         R                  [        U R                  R                  5      [        U R
                  5      5      n U R                  R                  U5      nU$ ! [         aK  nUR                  [        :X  a1  [        SU R
                   SU R                  R                   S35      Uee SnAff = f)zeCounts the documents in the collection.

Returns:
    int: The total of documents in the collection.
r   ' does not exist in schema ''N_COUNT_QUERYrm   r   r)   r4   r-   r1   rn   r   r   r   r2   rp   resr   s       r5   countCollection.count  s     !!T\\../1A$**1M
	""55c:C 
   	yy,,&"4::, /))*!-  	   A% %
B:/AB55B:c                    [        XU5      $ )a  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   )r2   
index_namefields_descs      r5   create_indexCollection.create_index/  s    0 .dLLr8   c           	         U R                   R                  SSSU R                  R                  U R                  US.5        g)zCDrops a collection index.

Args:
    index_name (str): Index name.
r   drop_collection_indexF)r3   r   r4   N)r1   r   r)   r4   r-   )r2   r   s     r5   
drop_indexCollection.drop_indexI  s=     	))#,,++"jj"			
r8   c                    SU;   a  US   U:w  a  [        S5      eU R                  S5      R                  SU5      R                  SU5      R	                  5       $ )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
_idKReplacement document has an _id that is different than the matched document	_id = :id$id)r   r   setbindexecuter2   doc_iddocs      r5   replace_oneCollection.replace_oneZ  s\     C<CJ&0"#  {{;'++C5::4HPPRRr8   c                    SU;   a  US   U:w  a  [        S5      e[        U[        5      (       d  [        U5      nU R                  UR	                  U5      5      R                  S5      R                  5       $ )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
r   r   T)r   r*   r   r   copyupsertr   r   s      r5   add_or_replace_oneCollection.add_or_replace_onei  sl     C<CJ&0"#  #u%%*Cxx()006>>@@r8   c                    U R                  S5      R                  SU5      R                  5       nUR                  5       nU R                  R                  5         U$ )zReturns a Document matching the Document ID.

Args:
    doc_id (str): Document ID

Returns:
    mysqlx.DbDoc: The Document matching the Document ID.
r   r   )r   r   r   	fetch_oner1   fetch_active_result)r2   r   resultr   s       r5   get_oneCollection.get_onez  sM     ;',,T6:BBD ,,.
r8   c                `    U R                  S5      R                  SU5      R                  5       $ )z~Removes a Document matching the Document ID.

Args:
    doc_id (str): Document ID

Returns:
    mysqlx.Result: Result object.
r   r   )r   r   r   )r2   r   s     r5   
remove_oneCollection.remove_one  s)     {{;',,T6:BBDDr8   rb   r   r(   )r   zOptional[str]rX   r   )r   r   rX   r   )r   rZ   rX   r   )r   rZ   rX   r   rX   int)r   rZ   r   zDict[str, Any]rX   r   )r   rZ   rX   rY   )r   rZ   r   zUnion[Dict, DbDoc]rX   'Result')r   rZ   rX   r   )r   rZ   rX   r  )r[   r\   r]   r^   r_   rP   r   r   r   r   r   r   r   r   r  r  r  ra   rb   r8   r5   ry   ry     sb    =		/""(MM,:M	'M4
"SA"	Er8   ry   c                  ^    \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrS
rg)r   i  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                    [         R                  [        U R                  R                  5      [        U R
                  5      5      nU R                  R                  U5      S:H  $ rk   r   ro   s     r5   rP   Table.exists_in_database  r   r8   c                \    [        U /UQ76 nU R                  R                  5       Ul        U$ )zCreates a new :class:`mysqlx.SelectStatement` object.

Args:
    *fields: The fields to be retrieved.

Returns:
    mysqlx.SelectStatement: SelectStatement object
)r   r1   r   r   r2   r   r   s      r5   selectTable.select  .     t-f-''==?r8   c                \    [        U /UQ76 nU R                  R                  5       Ul        U$ )zCreates a new :class:`mysqlx.InsertStatement` object.

Args:
    *fields: The fields to be inserted.

Returns:
    mysqlx.InsertStatement: InsertStatement object
)r   r1   r   r   r  s      r5   insertTable.insert  r  r8   c                Z    [        U 5      nU R                  R                  5       Ul        U$ )zsCreates a new :class:`mysqlx.UpdateStatement` object.

Returns:
    mysqlx.UpdateStatement: UpdateStatement object
)r   r1   r   r   r2   r   s     r5   updateTable.update  s)     t$''==?r8   c                Z    [        U 5      nU R                  R                  5       Ul        U$ )zCreates a new :class:`mysqlx.DeleteStatement` object.

Returns:
    mysqlx.DeleteStatement: DeleteStatement object

.. versionchanged:: 8.0.12
   The ``condition`` parameter was removed.
)r   r1   r   r   r  s     r5   deleteTable.delete  s)     t$''==?r8   c                z   [         R                  [        U R                  R                  5      [        U R
                  5      5      n U R                  R                  U5      nU$ ! [         aK  nUR                  [        :X  a1  [        SU R
                   SU R                  R                   S35      Uee SnAff = f)zQCounts the rows in the table.

Returns:
    int: The total of rows in the table.
zTable 'r   r   Nr   r   s       r5   r   Table.count  s     !!T\\../1A$**1M
	""55c:C 
   	yy,,&djj\ *))*!-  	r   c                    [         R                  [        U R                  R                  5      [        U R
                  5      5      nU R                  R                  U5      S:H  $ )ztDetermine if the underlying object is a view or not.

Returns:
    bool: `True` if the underlying object is a view.
r
   _COUNT_VIEWS_QUERYrm   r   r)   r4   r-   r1   rn   ro   s     r5   is_viewTable.is_view  J     !''t||/@/@(A6$**CUV22371<<r8   rb   Nr   )r   rZ   rX   r   )r   r   rX   r   )rX   r   )rX   r   r  )r[   r\   r]   r^   r_   rP   r  r  r  r   r   r'  ra   rb   r8   r5   r   r     s*    =(=r8   r   c                  "    \ rS rSrSrSS jrSrg)r   i  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                    [         R                  [        U R                  R                  5      [        U R
                  5      5      nU R                  R                  U5      S:H  $ rk   r%  ro   s     r5   rP   View.exists_in_database  r)  r8   rb   Nr   )r[   r\   r]   r^   r_   rP   ra   rb   r8   r5   r   r     s    =r8   r   )6r_   
__future__r   r   r   typingr   r   r   r   r   r	   dbdocr   	errorcoder   r   r   r   errorsr   r   r   helpersr   r   r   	statementr   r   r   r   r   r   r   r   r   typesr   r    r!   r"   r  r#   r&  r   rl   r   r   r%   rd   ry   r   r   rb   r8   r5   <module>r5     s   : 3 "   B B   J I 9 9
 
 
 G F8 
8 
 Q  .2 a aH]@^ ]@@	E ED`=N `=F=5 =r8   