zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
U
¤ý°d„gã@sFdZddlmZddlZddlZddlmZmZmZm    Z    m
Z
m Z ddl m Z ddlmZmZmZmZddlmZmZmZdd    lmZmZmZdd
lmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$dd l%m&Z&m'Z'm(Z(m)Z)eràdd l*m+Z+d Z,dZ-dZ.dZ/dZ0Gdd„dƒZ1Gdd„de1ƒZ2Gdd„de1ƒZ3Gdd„de1ƒZ4Gdd„de4ƒZ5dS)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}'zSELECT COUNT(*) FROM {0}.{1}zDROP TABLE IF EXISTS {0}.{1}c@sÎeZdZdZddddœdd„Zedd    œd
d „ƒZedd    œd d „ƒZedd    œdd„ƒZdd    œdd„Z    dd    œdd„Z
dd    œdd„Z dd    œdd„Z dd    œdd„Z eddƒdd    œdd „ƒZedd!ƒdd    œd"d#„ƒZd$S)%ÚDatabaseObjectz¢Provides base functionality for database objects.
 
    Args:
        schema (mysqlx.Schema): The Schema object.
        name (str): The database object name.
    rr!ÚNone)ÚschemaÚnameÚreturncCs:||_t|tƒr| ¡n||_|j ¡|_|j ¡|_dS©N)    Ú_schemaÚ
isinstanceÚbytesÚdecodeÚ_nameÚ get_sessionÚ_sessionÚget_connectionÚ _connection)Úselfr%r&©r3úBd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\mysqlx/crud.pyÚ__init__Vs zDatabaseObject.__init__r ©r'cCs|jS)z,:class:`mysqlx.Session`: The Session object.©r/©r2r3r3r4Úsession\szDatabaseObject.sessioncCs|jS)z*:class:`mysqlx.Schema`: The Schema object.©r)r8r3r3r4r%aszDatabaseObject.schemaÚstrcCs|jS)z&str: The name of this database object.©r-r8r3r3r4r&fszDatabaseObject.namercCs|jS)z~Returns the underlying connection.
 
        Returns:
            mysqlx.connection.Connection: The connection object.
        )r1r8r3r3r4r0kszDatabaseObject.get_connectioncCs|jS)zwReturns the session of this database object.
 
        Returns:
            mysqlx.Session: The Session object.
        r7r8r3r3r4r.sszDatabaseObject.get_sessioncCs|jS)z{Returns the Schema object of this database object.
 
        Returns:
            mysqlx.Schema: The Schema object.
        r:r8r3r3r4Ú
get_schema{szDatabaseObject.get_schemacCs|jS)zwReturns the name of this database object.
 
        Returns:
            str: The name of this database object.
        r<r8r3r3r4Úget_nameƒszDatabaseObject.get_namercCst‚dS)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)ÚNotImplementedErrorr8r3r3r4Úexists_in_database‹s    z!DatabaseObject.exists_in_databasez8.0.12z)Use 'exists_in_database()' method insteadcCs| ¡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@r8r3r3r4Ú    am_i_real–s zDatabaseObject.am_i_realzUse 'get_name()' method insteadcCs| ¡S)zÅReturns the name of this database object.
 
        Returns:
            str: The name of this database object.
 
        .. deprecated:: 8.0.12
           Use ``get_name()`` method instead.
        )r>r8r3r3r4Úwho_am_i¥s
zDatabaseObject.who_am_iN)Ú__name__Ú
__module__Ú __qualname__Ú__doc__r5Úpropertyr9r%r&r0r.r=r>r@rrArBr3r3r3r4r#Ns" r#csàeZdZdZddddœ‡fdd„ Zdd    œd
d „Zd d    œd d„Zd+ddddœdd„Zdd    œdd„Zd,ddddœdd„Z    d-ddddœdd„Z
d.ddddœdd„Z dddœd d!„Z d/ddd#d$dd%œd&d'„Z d0dd#dd(œd)d*„Z‡ZS)1Ú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.
    r r;r$)r9r&r'cs||_tƒ ||¡dSr()r/Úsuperr5)r2r9r&©Ú    __class__r3r4r5»szSchema.__init__Úboolr6cCs t t|jƒ¡}|j |¡dkS©zVerifies if this object exists in the database.
 
        Returns:
            bool: `True` if object exists in database.
        r    )Ú_COUNT_SCHEMAS_QUERYÚformatrr-r1Úexecute_sql_scalar©r2Úsqlr3r3r4r@¿szSchema.exists_in_databasezList[Collection]c    Cs||j dd|ji¡}| ¡g}|D]R}|ddkr6q$zt||dƒ}Wn"tk
rjt||dƒ}YnX| |¡q$|S)zyReturns a list of collections for this schema.
 
        Returns:
            `list`: List of Collection objects.
        Ú list_objectsr%ÚtypeZ
COLLECTIONÚ
TABLE_NAMEr&)r1Úget_row_resultr-Ú    fetch_allÚ
CollectionÚ
ValueErrorÚappend)r2ÚrowsÚ collectionsÚrowÚ
collectionr3r3r4Úget_collectionsÈs  zSchema.get_collectionsFÚTable)r&Úcheck_existencer'cCs | ||¡S)zuReturns a a table object for the given collection
 
        Returns:
            mysqlx.Table: Table object.
 
        )Ú    get_table)r2r&rar3r3r4Úget_collection_as_tableÛs    zSchema.get_collection_as_tablez List[Table]c    Cs~|j dd|ji¡}| ¡g}d}|D]P}|d|kr(zt||dƒ}Wn"tk
rlt||dƒ}YnX| |¡q(|S)zoReturns a list of tables for this schema.
 
        Returns:
            `list`: List of Table objects.
        rSr%)ZTABLEZVIEWrTrUr&)r1rVr-rWr`rYrZ)r2r[ZtablesZ object_typesr]Útabler3r3r4Ú
get_tablesæs  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)r`r@r)r2r&rardr3r3r4rbüs
 
zSchema.get_tableÚViewcCs"t||ƒ}|r| ¡stdƒ‚|S)ztReturns the view of the given name for this schema.
 
        Returns:
            mysqlx.View: View object.
        zView does not exist)rfr@r)r2r&raÚviewr3r3r4Úget_views
 
zSchema.get_viewrXcCs"t||ƒ}|r| ¡stdƒ‚|S)z†Returns the collection of the given name for this schema.
 
        Returns:
            mysqlx.Collection: Collection object.
        zCollection does not exist)rXr@r)r2r&rar^r3r3r4Úget_collections
 
zSchema.get_collection)r&r'cCs&|j dt t|jƒt|ƒ¡d¡dS)zmDrops a collection.
 
        Args:
            name (str): The name of the collection to be dropped.
        rRFN)r1Úexecute_nonqueryÚ_DROP_TABLE_QUERYrOrr-)r2r&r3r3r4Údrop_collection sÿûzSchema.drop_collectionNz%Optional[Dict[str, Union[str, Dict]]]r)r&Úreuse_existingÚ
validationÚkwargsr'c
Ks–|s tdƒ‚d|kr(t dt¡|d}t||ƒ}|j|dœ}|dk    r
t|tƒrV|s^tdƒ‚d}|D]}||krftd|›ƒ‚qfg}    d    |kr¶|d    }
t|
tƒs¨td
ƒ‚|         d    |
f¡d |krþ|d } t| ttfƒsÜtd ƒ‚|         d t| tƒröt
  | ¡n| f¡d |    f|d<z|j   ddd|¡Wnptk
r} zP| jtkrJtdƒ| ‚| jtkrp|s€td|›dƒ| ‚nt| j| jƒ| ‚W5d} ~ XYnX|S)aJCreates 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 invalidZreusezG'reuse' is deprecated since 8.0.21. Please use 'reuse_existing' instead)r%r&NúInvalid value for 'validation'©Úlevelr%ú Invalid option in 'validation': rsúInvalid value for 'level'r%úInvalid value for 'schema'rnÚoptionsÚmysqlxÚcreate_collectionTúlYour 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ÚDeprecationWarningrXr-r*Údictr;rZÚjsonÚdumpsr1rjrÚerrnor rr Úmsg) r2r&rmrnror^ÚfieldsÚ valid_optionsÚoptionrwrsr%Úerrr3r3r4ry.spý
 
 
þÿ ÿ ÿü 
ÿþ"zSchema.create_collection)r&rnr'c
 
Cs:|s tdƒ‚t|tƒr|s"tdƒ‚d}|D]}||kr*td|›ƒ‚q*g}d|krz|d}t|tƒsltdƒ‚| d|f¡d|krÂ|d}t|ttfƒs tdƒ‚| dt|tƒrºt |¡n|f¡|j|d    |fd
œ}z|j     d d d |¡WnJt
k
r4}    z*|    j t krt dƒ|    ‚t|    j|    j ƒ|    ‚W5d}    ~    XYnXdS)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
        rprqrrrtrsrur%rvrn)r%r&rwrxZmodify_collection_optionsTrzN)rr*rr;rZr€rr-r1rjrr‚rrrƒ)
r2r&rnr…r†rwrsr%r„r‡r3r3r4Úmodify_collectionsT
þÿýÿ ÿüzSchema.modify_collection)F)F)F)F)FN)N)rCrDrErFr5r@r_rcrerbrhrirlryrˆÚ __classcell__r3r3rJr4rH²s     ÿ    ü`ÿrHc@sÔeZdZdZddœdd„Zd0ddd    œd
d „Zd d dœdd„Zddd    œdd„Zddd    œdd„Zddœdd„Z    ddddœdd„Z
dd d!œd"d#„Z dd$d%d&œd'd(„Z dd$d%d&œd)d*„Z dd d+œd,d-„Zdd%d+œd.d/„ZdS)1rXzRepresents a collection of documents on a schema.
 
    Args:
        schema (mysqlx.Schema): The Schema object.
        name (str): The collection name.
    rLr6cCs*t t|jjƒt|jƒ¡}|j |¡dkSrM©Ú_COUNT_TABLES_QUERYrOrr)r&r-r1rPrQr3r3r4r@ÚszCollection.exists_in_databaseNz Optional[str]r)Ú    conditionr'cCst||ƒ}|j ¡|_|S)zÔRetrieves documents from a collection.
 
        Args:
            condition (Optional[str]): The string with the filter expression of
                                       the documents to be retrieved.
        )rr1Úget_next_statement_idÚstmt_id©r2rŒÚstmtr3r3r4Úfindãs
 zCollection.findr
r)Úvaluesr'cGst|ƒ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)r2r’r3r3r4r“îs    zCollection.addr;rcCst||ƒ}|j ¡|_|S)apRemoves 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.
        )rr1rrŽrr3r3r4Úremoveùs
 zCollection.removercCst||ƒ}|j ¡|_|S)arModifies 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.
        )rr1rrŽrr3r3r4Úmodify
s
 zCollection.modifyÚintc
Cs|t t|jjƒt|jƒ¡}z|j |¡}WnLtk
rv}z.|j    t
krdtd|j›d|jj›dƒ|‚‚W5d}~XYnX|S)z}Counts the documents in the collection.
 
        Returns:
            int: The total of documents in the collection.
        r{ú' does not exist in schema 'ú'N© Ú _COUNT_QUERYrOrr)r&r-r1rPrr‚r ©r2rRÚresr‡r3r3r4Úcounts
ÿ
ÿýzCollection.countzDict[str, Any]r)Ú
index_nameÚ fields_descr'cCs t|||ƒS)abCreates 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)r2ržrŸr3r3r4Ú create_index/szCollection.create_indexr$)ržr'c    Cs$|j ddd|jj|j|dœ¡dS)z[Drops a collection index.
 
        Args:
            index_name (str): Index name.
        rxZdrop_collection_indexF)r%r^r&N)r1rjr)r&r-)r2ržr3r3r4Ú
drop_indexIsýüzCollection.drop_indexzUnion[Dict, DbDoc]z'Result')Údoc_idÚdocr'cCs:d|kr|d|krtdƒ‚| d¡ d|¡ d|¡ ¡S)zÒReplaces 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
        Ú_idúKReplacement document has an _id that is different than the matched documentú    _id = :idú$Úid)rr•ÚsetÚbindÚexecute©r2r¢r£r3r3r4Ú replace_oneZs
ÿzCollection.replace_onecCsHd|kr|d|krtdƒ‚t|tƒs.t|ƒ}| | |¡¡ d¡ ¡S)zÏUpserts 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)rr*r
r“ÚcopyZupsertr«r¬r3r3r4Úadd_or_replace_oneisÿ
zCollection.add_or_replace_one)r¢r'cCs,| d¡ d|¡ ¡}| ¡}|j ¡|S)z½Returns 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«Z    fetch_oner1Zfetch_active_result)r2r¢Úresultr£r3r3r4Úget_onezs    
zCollection.get_onecCs| d¡ d|¡ ¡S)z¦Removes a Document matching the Document ID.
 
        Args:
            doc_id (str): Document ID
 
        Returns:
            mysqlx.Result: Result object.
        r¦r¨)r”rªr«)r2r¢r3r3r4Ú
remove_oneˆs    zCollection.remove_one)N)rCrDrErFr@r‘r“r”r•rr r¡r­r¯r±r²r3r3r3r4rXÒs      rXc@sveZdZdZddœdd„Zdddœd    d
„Zd d dœd d„Zddœdd„Zddœdd„Zddœdd„Z    ddœdd„Z
dS)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.
    rLr6cCs*t t|jjƒt|jƒ¡}|j |¡dkSrMrŠrQr3r3r4r@ŸszTable.exists_in_databaser;r)r„r'cGst|f|žŽ}|j ¡|_|S)zËCreates a new :class:`mysqlx.SelectStatement` object.
 
        Args:
            *fields: The fields to be retrieved.
 
        Returns:
            mysqlx.SelectStatement: SelectStatement object
        )rr1rrŽ©r2r„rr3r3r4Úselect¨s     z Table.selectrrcGst|f|žŽ}|j ¡|_|S)zÊCreates a new :class:`mysqlx.InsertStatement` object.
 
        Args:
            *fields: The fields to be inserted.
 
        Returns:
            mysqlx.InsertStatement: InsertStatement object
        )rr1rrŽr³r3r3r4Úinsertµs     z Table.insertrcCst|ƒ}|j ¡|_|S)z‹Creates a new :class:`mysqlx.UpdateStatement` object.
 
        Returns:
            mysqlx.UpdateStatement: UpdateStatement object
        )rr1rrŽ©r2rr3r3r4ÚupdateÂs z Table.updatercCst|ƒ}|j ¡|_|S)zãCreates a new :class:`mysqlx.DeleteStatement` object.
 
        Returns:
            mysqlx.DeleteStatement: DeleteStatement object
 
        .. versionchanged:: 8.0.12
           The ``condition`` parameter was removed.
        )rr1rrŽr¶r3r3r4ÚdeleteÌs     z Table.deleter–c
Cs|t t|jjƒt|jƒ¡}z|j |¡}WnLtk
rv}z.|j    t
krdtd|j›d|jj›dƒ|‚‚W5d}~XYnX|S)ziCounts the rows in the table.
 
        Returns:
            int: The total of rows in the table.
        zTable 'r—r˜Nr™r›r3r3r4rÙs
ÿ
ÿýz Table.countcCs*t t|jjƒt|jƒ¡}|j |¡dkS)zŒDetermine if the underlying object is a view or not.
 
        Returns:
            bool: `True` if the underlying object is a view.
        r    ©Ú_COUNT_VIEWS_QUERYrOrr)r&r-r1rPrQr3r3r4Úis_viewísz Table.is_viewN) rCrDrErFr@r´rµr·r¸rr»r3r3r3r4r`”s
      
 r`c@seZdZdZddœdd„ZdS)rfzÌ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.
    rLr6cCs*t t|jjƒt|jƒ¡}|j |¡dkSrMr¹rQr3r3r4r@szView.exists_in_databaseN)rCrDrErFr@r3r3r3r4rf÷s    rf)6rFÚ
__future__rr€r|ÚtypingrrrrrrZdbdocr
Ú    errorcoder r r rÚerrorsrrrZhelpersrrrZ    statementrrrrrrrrrÚtypesrrr r!r°r"rºr‹rNršrkr#rHrXr`rfr3r3r3r4Ú<module>s8   ,  ÿÿÿd"Cc