o 3Ûøa­`ã@sæddlZddlZddlZddlZddlZddlmZddlmZddl Z ddl m Z ddl m Z ddlmZmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlm Z dZ!Gdd„dƒZ"dS)éN)Údeque)Úcontextmanager)Úsettings)ÚImproperlyConfigured)ÚDEFAULT_DB_ALIASÚ DatabaseError)Úutils)ÚBaseDatabaseValidation)Úconnection_created)ÚTransactionManagementError©ÚDatabaseErrorWrapper)Útimezone)Ú async_unsafe)Úcached_propertyÚ __no_db__c@s~eZdZdZiZiZiZdZdZdZ dZ dZ dZ dZ dZdZeZdZefdd„Zdd„Zed d „ƒZed d „ƒZed d„ƒZedd„ƒZdd„Zdd„Zdd„Zd|dd„Ze dd„ƒZ!dd„Z"e dd„ƒZ#dd „Z$d|d!d"„Z%d#d$„Z&d%d&„Z'd'd(„Z(e d)d*„ƒZ)e d+d,„ƒZ*e d-d.„ƒZ+e d/d0„ƒZ,d1d2„Z-d3d4„Z.d5d6„Z/d7d8„Z0e d9d:„ƒZ1e d;d<„ƒZ2e d=d>„ƒZ3e d?d@„ƒZ4dAdB„Z5dCdD„Z6d}dFdG„Z7dHdI„Z8dJdK„Z9dLdM„Z:dNdO„Z;edTdU„Z?d|dVdW„Z@dXdY„ZAdZd[„ZBed\d]„ƒZCd^d_„ZDd`da„ZEdbdc„ZFddde„ZGedfdg„ƒZHdhdi„ZIdjdk„ZJdldm„ZKe/usr/lib/python3/dist-packages/django/db/backends/base/base.pyÚ__init__0s4       zBaseDatabaseWrapper.__init__cCódS)z Ensure the connection's timezone is set to `self.timezone_name` and return whether it changed or not. Fr;©r:r;r;r<Úensure_timezonenóz#BaseDatabaseWrapper.ensure_timezonecCs.tjsdS|jddurtjSt |jd¡S)a Return a tzinfo of the database connection time zone. This is only used when time zone support is enabled. When a datetime is read from the database, it is always returned in this time zone. When the database backend supports time zones, it doesn't matter which time zone Django uses, as long as aware datetimes are used everywhere. Other users connecting to the database can choose their own time zone. When the database backend doesn't support time zones, the time zone Django uses may be constrained by the requirements of other users of the database. NÚ TIME_ZONE)rÚUSE_TZrrÚutcÚpytzr?r;r;r<rus zBaseDatabaseWrapper.timezonecCs(tjstjS|jddurdS|jdS)zC Name of the time zone of the database connection. rBNÚUTC)rrCrBrr?r;r;r<Ú timezone_nameŒs  z!BaseDatabaseWrapper.timezone_namecCs |jptjS©N)rrÚDEBUGr?r;r;r<Úqueries_logged˜ó z"BaseDatabaseWrapper.queries_loggedcCs0t|jƒ|jjkrt d |jj¡¡t|jƒS)NzLLimit for query logging exceeded, only the last {} queries will be returned.)ÚlenrrÚwarningsÚwarnÚformatÚlistr?r;r;r<Úqueriesœs  þ zBaseDatabaseWrapper.queriescCótdƒ‚)zConnect to the database. Assume that the connection is closed.FÚ CONN_MAX_AGENÚ AUTOCOMMIT)Úsenderr)Úcheck_settingsrrr rÚtimeÚ monotonicr!r"r#rUrXrÚset_autocommitrYr ÚsendÚ __class__r+)r:Úmax_agerWr;r;r<Úconnect¸s   zBaseDatabaseWrapper.connectcCs*|jddurtjstd|jƒ‚dSdS)NrBz=Connection '%s' cannot set TIME_ZONE because USE_TZ is False.)rrrCrrr?r;r;r<r`Ïsÿÿÿz"BaseDatabaseWrapper.check_settingscCsB|jdur|j | ¡WdƒdS1swYdSdS)z;Guarantee that a connection to the database is established.N)rÚwrap_database_errorsrgr?r;r;r<Úensure_connectionÖs  "ÿÿz%BaseDatabaseWrapper.ensure_connectioncCs*| ¡|jr| |¡}|S| |¡}|S)zY Validate the connection is usable and perform database cursor wrapping. )Úvalidate_thread_sharingrJÚmake_debug_cursorÚ make_cursor)r:ÚcursorÚwrapped_cursorr;r;r<Ú_prepare_cursorßs   ÿz#BaseDatabaseWrapper._prepare_cursorcCsB| ¡|j| | |¡¡WdƒS1swYdSrH)rirhror\rZr;r;r<Ú_cursorês$ÿzBaseDatabaseWrapper._cursorcCóB|jdur|j |j ¡WdƒS1swYdSdSrH)rrhÚcommitr?r;r;r<Ú_commitïó $ÿÿzBaseDatabaseWrapper._commitcCrqrH)rrhÚrollbackr?r;r;r<Ú _rollbackôrtzBaseDatabaseWrapper._rollbackcCrqrH)rrhÚcloser?r;r;r<Ú_closeùrtzBaseDatabaseWrapper._closecCó| ¡S)z3Create a cursor, opening a connection if necessary.)rpr?r;r;r<rmszBaseDatabaseWrapper.cursorcCs(| ¡| ¡| ¡d|_d|_dS)z.Commit a transaction and reset the dirty flag.FTN)rjÚvalidate_no_atomic_blockrsr#r,r?r;r;r<rrs  zBaseDatabaseWrapper.commitcCs.| ¡| ¡| ¡d|_d|_g|_dS)z1Roll back a transaction and reset the dirty flag.FN)rjrzrvr#r r+r?r;r;r<rus  zBaseDatabaseWrapper.rollbackcCsj| ¡g|_|js|jdurdSz| ¡W|jr"d|_d|_dSd|_dS|jr1d|_d|_wd|_w)z%Close the connection to the database.NT)rjr+r"rrxrr r?r;r;r<rws   ü zBaseDatabaseWrapper.closecCó@| ¡}| |j |¡¡WdƒdS1swYdSrH)rmÚexecuter7Úsavepoint_create_sql©r:Úsidrmr;r;r<Ú _savepoint0ó "ÿzBaseDatabaseWrapper._savepointcCr{rH)rmr|r7Úsavepoint_rollback_sqlr~r;r;r<Ú_savepoint_rollback4rz'BaseDatabaseWrapper._savepoint_rollbackcCr{rH)rmr|r7Úsavepoint_commit_sqlr~r;r;r<Ú_savepoint_commit8rz%BaseDatabaseWrapper._savepoint_commitcCs|jjo| ¡ SrH)r3Úuses_savepointsÚget_autocommitr?r;r;r<Ú_savepoint_allowed<sz&BaseDatabaseWrapper._savepoint_allowedcCsV| ¡sdSt ¡}t|ƒ dd¡}|jd7_d||jf}| ¡| |¡|S)zà Create a savepoint inside the current transaction. Return an identifier for the savepoint that will be used for the subsequent rollback or commit. Do nothing if savepoints are not supported. Nú-Úézs%s_x%d)rˆr(r)ÚstrÚreplacerrjr€)r:Ú thread_identÚtidrr;r;r<Ú savepointBs zBaseDatabaseWrapper.savepointcs8| ¡sdS| ¡| ˆ¡‡fdd„|jDƒ|_dS)zW Roll back to a savepoint. Do nothing if savepoints are not supported. Ncs g|] \}}ˆ|vr||f‘qSr;r;)Ú.0ÚsidsÚfunc©rr;r<Ú csz:BaseDatabaseWrapper.savepoint_rollback..)rˆrjrƒr+©r:rr;r”r<Úsavepoint_rollbackWs   ÿz&BaseDatabaseWrapper.savepoint_rollbackcCs"| ¡sdS| ¡| |¡dS)zR Release a savepoint. Do nothing if savepoints are not supported. N)rˆrjr…r–r;r;r<Úsavepoint_commitgsz$BaseDatabaseWrapper.savepoint_commitcCs d|_dS)zY Reset the counter used to generate unique savepoint ids in this thread. rN)rr?r;r;r<Úclean_savepointsrs z$BaseDatabaseWrapper.clean_savepointscCrR)zR Backend-specific implementation to enable or disable autocommit. zHsubclasses of BaseDatabaseWrapper may require a _set_autocommit() methodrS)r:rr;r;r<Ú_set_autocommit{sz#BaseDatabaseWrapper._set_autocommitcCs| ¡|jS)zGet the autocommit state.)rirr?r;r;r<r‡ƒsz"BaseDatabaseWrapper.get_autocommitFcCsf| ¡| ¡|o| ot|dƒ}|r| ¡n| |¡||_|r/|jr1| ¡d|_dSdSdS)aé Enable or disable autocommit. The usual way to start a transaction is to turn autocommit off. SQLite does not properly start a transaction when disabling autocommit. To avoid this buggy behavior and to actually enter a new transaction, an explicit BEGIN is required. Using force_begin_transaction_with_broken_autocommit=True will issue an explicit BEGIN with SQLite. This option will be ignored for other backends. Ú#_start_transaction_under_autocommitFN)rzriÚhasattrr›ršrr,Úrun_and_clear_commit_hooks)r:rÚ.force_begin_transaction_with_broken_autocommitÚ"start_transaction_under_autocommitr;r;r<rcˆs  þ    þz"BaseDatabaseWrapper.set_autocommitcCs|jstdƒ‚|jS)z9Get the "needs rollback" flag -- for *advanced use* only.ú)zÍ Backends can implement as needed to temporarily disable foreign key constraint checking. Should return True if the constraints were disabled and will need to be reenabled. Fr;r?r;r;r<r¥Ñóz/BaseDatabaseWrapper.disable_constraint_checkingcCr>)zh Backends can implement as needed to re-enable foreign key constraint checking. Nr;r?r;r;r<r¦ÙrAz.BaseDatabaseWrapper.enable_constraint_checkingcCr>)zë Backends can override this method if they can apply constraint checking (e.g. via "SET CONSTRAINTS ALL IMMEDIATE"). Should raise an IntegrityError if any invalid foreign key references are encountered. Nr;)r:Ú table_namesr;r;r<Úcheck_constraintsàr©z%BaseDatabaseWrapper.check_constraintscCrR)a Test if the database connection is usable. This method may assume that self.connection is not None. Actual implementations should take care not to raise exceptions as that may prevent Django from recycling unusable connections. zCsubclasses of BaseDatabaseWrapper may require an is_usable() methodrSr?r;r;r<Ú is_usableês ÿzBaseDatabaseWrapper.is_usablecCsz|jdur7| ¡|jdkr| ¡dS|jr%| ¡rd|_n| ¡dS|jdur9t ¡|jkr;| ¡dSdSdSdS)z Close the current connection if unrecoverable errors have occurred or if it outlived its maximum age. Nr^F) rr‡rrwr#r¬r!rarbr?r;r;r<Úclose_if_unusable_or_obsoleteös îz1BaseDatabaseWrapper.close_if_unusable_or_obsoletecCs4|j |jdkWdƒS1swYdS©Nr©r&r'r?r;r;r<Úallow_thread_sharings$ÿz(BaseDatabaseWrapper.allow_thread_sharingcCs:|j|jd7_WdƒdS1swYdS)Nr‹r¯r?r;r;r<Úinc_thread_sharings"ÿz&BaseDatabaseWrapper.inc_thread_sharingcCsL|j|jdkr tdƒ‚|jd8_WdƒdS1swYdS)Nrz5Cannot decrement the thread sharing count below zero.r‹)r&r'Ú RuntimeErrorr?r;r;r<Údec_thread_sharings  "ýz&BaseDatabaseWrapper.dec_thread_sharingcCs6|js|jt ¡kstd|j|jt ¡fƒ‚dSdS)a3 Validate that the connection isn't accessed by another thread than the one which originally created it, unless the connection was explicitly authorized to be shared between threads (via the `inc_thread_sharing()` method). Raise an exception if the validation fails. z¢DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias '%s' was created in thread id %s and this is thread id %s.N)r°r*r(r)rrr?r;r;r<rj süÿÿz+BaseDatabaseWrapper.validate_thread_sharingcCr>)z~ Hook to do any database check or preparation, generally called before migrating a project or an app. Nr;r?r;r;r<Úprepare_database2rAz$BaseDatabaseWrapper.prepare_databasecCst|ƒS)z‹ Context manager and decorator that re-throws backend-specific database exceptions using Django's common wrappers. r r?r;r;r<rh9sz(BaseDatabaseWrapper.wrap_database_errorscCry)z˜ Return a cursor that tries to avoid caching in the database (if supported by the database), otherwise return a regular cursor. )rmr?r;r;r<Úchunked_cursorAsz"BaseDatabaseWrapper.chunked_cursorcCó t ||¡S)z:Create a cursor that logs all queries in self.queries_log.)rÚCursorDebugWrapper©r:rmr;r;r<rkHrKz%BaseDatabaseWrapper.make_debug_cursorcCr¶)z&Create a cursor without debug logging.)rÚ CursorWrapperr¸r;r;r<rlLrKzBaseDatabaseWrapper.make_cursorccsb|jdu}z"| ¡ }|VWdƒn1swYW|r'| ¡dSdS|r0| ¡ww)a8 Context manager that ensures that a connection is established, and if it opened one, closes it to avoid leaving a dangling connection. This is useful for operations outside of the request-response cycle. Provide a cursor: with self.temporary_connection() as cursor: ... N)rrmrw)r:Ú must_closermr;r;r<Útemporary_connectionPs€  ÿ€ ÿ ÿz(BaseDatabaseWrapper.temporary_connectionccst|ji|j¥ddi¥td}z%| ¡ }|VWdƒn1s"wYW| ¡dSW| ¡dS| ¡w)aq Return a cursor from an alternative connection to be used when there is no need to access the main database, specifically for test db creation/deletion. This also prevents the production database from being exposed to potential child threads while (or after) the test database is destroyed. Refs #10868, #17786, #16969. ÚNAMEN)r)rerÚ NO_DB_ALIASrmrw)r:Úconnrmr;r;r<Ú _nodb_cursoras€  ÿ ýz BaseDatabaseWrapper._nodb_cursorcOs*|jdur tdƒ‚|j|g|¢Ri|¤ŽS)zG Return a new instance of this backend's SchemaEditor. NzFThe SchemaEditorClass attribute of this database wrapper is still None)ÚSchemaEditorClassrT)r:ÚargsÚkwargsr;r;r<Ú schema_editorqs ÿz!BaseDatabaseWrapper.schema_editorcCsJt|ƒstdƒ‚|jr|j t|jƒ|f¡dS| ¡s tdƒ‚|ƒdS)Nz*on_commit()'s callback must be a callable.z;on_commit() cannot be used in manual transaction management) ÚcallableÚ TypeErrorrr+ÚappendÚsetrr‡r )r:r“r;r;r<Ú on_commitzs zBaseDatabaseWrapper.on_commitcCs8| ¡|j}g|_|r| d¡\}}|ƒ|s dSdSr®)rzr+Úpop)r:Úcurrent_run_on_commitr’r“r;r;r<r‡s þz.BaseDatabaseWrapper.run_and_clear_commit_hooksccs2|j |¡z dVW|j ¡dS|j ¡w)z| Return a context manager under which the wrapper is applied to suitable database query executions. N)r-rÆrÉ)r:Úwrapperr;r;r<Úexecute_wrappers € z#BaseDatabaseWrapper.execute_wrappercCs(t |j¡}|dur |j}t|ƒ||ƒS)zy Return a copy of this connection. For tests that require two connections to the same database. N)ÚcopyÚdeepcopyrrÚtype)r:rrr;r;r<rÍ›s zBaseDatabaseWrapper.copyrH)F)SÚ__name__Ú __module__Ú __qualname__Ú__doc__Ú data_typesÚdata_types_suffixÚdata_type_check_constraintsr7ÚvendorÚ display_namerÀr.r0r2r4r6r r8rrr=r@rrrGÚpropertyrJrQrUrXrYr\rrgr`rirorprsrvrxrmrrrurwr€rƒr…rˆrr—r˜r™ršr‡rcr¢r£rzr¤rr¨r¥r¦r«r¬r­r°r±r³rjr´rhrµrkrlr»r¿rÃrÈrrÌrÍr;r;r;r<rs¾ >                                r)#r(rÍr$rarMÚ collectionsrÚ contextlibrrEÚ django.confrÚdjango.core.exceptionsrÚ django.dbrrÚdjango.db.backendsrÚ"django.db.backends.base.validationr Údjango.db.backends.signalsr Údjango.db.transactionr Údjango.db.utilsr Ú django.utilsrÚdjango.utils.asynciorÚdjango.utils.functionalrr½rr;r;r;r<Ús*