o
    uvXjn+  ã                
   @   s„  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ zddlZddlZddlZW n eyZ Z zede ƒ‚dZ[ww dd„ Zeƒ Zedk rmedej ƒ‚ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z* ej+ ,eej+j-¡ ej. /¡  dZ0ej+ 1e0fdej+j2¡Z3ej+ 4e3¡ G dd„ de
ƒZ5dS )za
PostgreSQL database backend for Django.

Requires psycopg 2: http://initd.org/projects/psycopg2
é    N)Úsettings)ÚImproperlyConfigured)Úconnections)ÚBaseDatabaseWrapper)ÚDatabaseError)Úcached_property)ÚSafeText)Úget_version_tuplez!Error loading psycopg2 module: %sc                  C   s   t j dd¡d } t| ƒS )Nú é   r   )Úpsycopg2Ú__version__Úsplitr	   )Úversion© r   ú_/var/www/html/myproject/venv/lib/python3.10/site-packages/django/db/backends/postgresql/base.pyÚpsycopg2_version   s   r   )é   é   é   z8psycopg2_version 2.5.4 or newer is required; you have %sr   )ÚDatabaseClient)ÚDatabaseCreation)ÚDatabaseFeatures)ÚDatabaseIntrospection)ÚDatabaseOperations)ÚDatabaseSchemaEditor)Úutc_tzinfo_factoryi  Ú	INETARRAYc                       sb  e Zd ZdZdZi dd“dd“dd“d	d
“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“d d
“ddd!dd!d"d#d$d%œ¥Zd&d&d'œZd(d)d*d+d,d-d.d/d0d1d*d*d+d+d2œZd3Zd4d5d6d7d8d9d:œZ	e
Z
eZeZeZeZeZeZd;Zd<d=„ Zd>d?„ Zd@dA„ ZdBdC„ ZdSdEdF„ZdGdH„ ZdIdJ„ ZdSdKdL„ZdMdN„ Z e!‡ fdOdP„ƒZ"e#dQdR„ ƒZ$‡  Z%S )TÚDatabaseWrapperÚ
postgresqlÚ
PostgreSQLÚ	AutoFieldÚserialÚBigAutoFieldÚ	bigserialÚBinaryFieldÚbyteaÚBooleanFieldÚbooleanÚ	CharFieldzvarchar(%(max_length)s)Ú	DateFieldÚdateÚDateTimeFieldztimestamp with time zoneÚDecimalFieldz+numeric(%(max_digits)s, %(decimal_places)s)ÚDurationFieldÚintervalÚ	FileFieldÚFilePathFieldÚ
FloatFieldzdouble precisionÚIntegerFieldÚintegerÚBigIntegerFieldÚbigintÚIPAddressFieldÚinetÚGenericIPAddressFieldÚNullBooleanFieldÚsmallintÚtextÚtimeÚuuid)ÚOneToOneFieldÚPositiveIntegerFieldÚPositiveSmallIntegerFieldÚ	SlugFieldÚSmallIntegerFieldÚ	TextFieldÚ	TimeFieldÚ	UUIDFieldz"%(column)s" >= 0)r@   rA   z= %sz= UPPER(%s)zLIKE %szLIKE UPPER(%s)z~ %sz~* %sz> %sz>= %sz< %sz<= %s)ÚexactÚiexactÚcontainsÚ	icontainsÚregexÚiregexÚgtÚgteÚltÚlteÚ
startswithÚendswithÚistartswithÚ	iendswithzKREPLACE(REPLACE(REPLACE({}, E'\\', E'\\\\'), E'%%', E'\\%%'), E'_', E'\\_')zLIKE '%%' || {} || '%%'zLIKE '%%' || UPPER({}) || '%%'zLIKE {} || '%%'zLIKE UPPER({}) || '%%'zLIKE '%%' || {}zLIKE '%%' || UPPER({}))rI   rJ   rQ   rS   rR   rT   r   c                 C   sÐ   | j }|d dkrtdƒ‚t|d pdƒ| j ¡ kr,td|d t|d ƒ| j ¡ f ƒ‚d|d p2di|d ¥}| dd ¡ |d	 rH|d	 |d
< |d rR|d |d< |d r\|d |d< |d rf|d |d< |S )NÚNAMEÚ zJsettings.DATABASES is improperly configured. Please supply the NAME value.z‡The database name '%s' (%d characters) is longer than PostgreSQL's limit of %d characters. Supply a shorter NAME in settings.DATABASES.ÚdatabaseÚpostgresÚOPTIONSÚisolation_levelÚUSERÚuserÚPASSWORDÚpasswordÚHOSTÚhostÚPORTÚport)Úsettings_dictr   ÚlenÚopsÚmax_name_lengthÚpop)Úselfrc   Úconn_paramsr   r   r   Úget_connection_params‘   s8   ÿ
ýþÿ
ÿþz%DatabaseWrapper.get_connection_paramsc                 C   sd   t jdi |¤Ž}| jd }z|d | _W n ty"   |j| _Y |S w | j|jkr0|j| jd� |S )NrY   rZ   )rZ   r   )ÚDatabaseÚconnectrc   rZ   ÚKeyErrorÚset_session)rh   ri   Ú
connectionÚoptionsr   r   r   Úget_new_connection±   s   

ùz"DatabaseWrapper.get_new_connectionc                 C   st   | j d u rdS | j  d¡}| j}|r8||kr8| j  ¡ �}| | j ¡ |g¡ W d   ƒ dS 1 s1w   Y  dS dS )NFÚTimeZoneT)ro   Úget_parameter_statusÚtimezone_nameÚcursorÚexecutere   Úset_time_zone_sql)rh   Úconn_timezone_namert   ru   r   r   r   Úensure_timezoneÅ   s   

ÿþzDatabaseWrapper.ensure_timezonec                 C   s6   | j  d¡ |  ¡ }|r|  ¡ s| j  ¡  d S d S d S )NÚUTF8)ro   Úset_client_encodingry   Úget_autocommitÚcommit)rh   Útimezone_changedr   r   r   Úinit_connection_stateÐ   s   ýz%DatabaseWrapper.init_connection_stateNc                 C   s@   |r| j j|d| j jd�}n| j  ¡ }tjrt|_|S d |_|S )NF)Ú
scrollableÚwithhold)ro   ru   Ú
autocommitr   ÚUSE_TZr   Útzinfo_factory)rh   Únameru   r   r   r   Úcreate_cursorÙ   s   
ÿzDatabaseWrapper.create_cursorc                 C   s*   |  j d7  _ | jdt ¡ j| j f d�S )Nr   z_django_curs_%d_%d)r…   )Ú_named_cursor_idxÚ_cursorÚ	threadingÚcurrent_threadÚident©rh   r   r   r   Úchunked_cursorã   s   ýÿzDatabaseWrapper.chunked_cursorc                 C   s4   | j � || j_W d   ƒ d S 1 sw   Y  d S ©N)Úwrap_database_errorsro   r‚   )rh   r‚   r   r   r   Ú_set_autocommití   s   
"ÿzDatabaseWrapper._set_autocommitc                 C   s    |   ¡  d¡ |   ¡  d¡ dS )zl
        Check constraints by setting them to immediate. Return them to deferred
        afterward.
        zSET CONSTRAINTS ALL IMMEDIATEzSET CONSTRAINTS ALL DEFERREDN)ru   rv   )rh   Útable_namesr   r   r   Úcheck_constraintsñ   s   z!DatabaseWrapper.check_constraintsc                 C   s.   z| j  ¡  d¡ W dS  tjy   Y dS w )NzSELECT 1FT)ro   ru   rv   rk   ÚErrorrŒ   r   r   r   Ú	is_usableù   s   ýÿzDatabaseWrapper.is_usablec              	      s�   t ƒ j}z| ¡  W |S  tjtfyG   t dt¡ t	 
¡ D ]$}|jdkrC|jd dkrC| ji | j¥d|jd i¥| jd�   Y S qY |S w )Na8  Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.r   rU   rX   )Úalias)ÚsuperÚ_nodb_connectionÚensure_connectionrk   r   ÚWrappedDatabaseErrorÚwarningsÚwarnÚRuntimeWarningr   ÚallÚvendorrc   Ú	__class__r•   )rh   Únodb_connectionro   ©rŸ   r   r   r—     s&   
ñúþ€þñz DatabaseWrapper._nodb_connectionc                 C   s4   |   ¡ � | jjW  d   ƒ S 1 sw   Y  d S rŽ   )Útemporary_connectionro   Úserver_versionrŒ   r   r   r   Ú
pg_version  s   
$ÿzDatabaseWrapper.pg_versionrŽ   )&Ú__name__Ú
__module__Ú__qualname__rž   Údisplay_nameÚ
data_typesÚdata_type_check_constraintsÚ	operatorsÚpattern_escÚpattern_opsrk   r   ÚSchemaEditorClassr   Úclient_classr   Úcreation_classr   Úfeatures_classr   Úintrospection_classr   Ú	ops_classr‡   rj   rq   ry   r   r†   r�   r�   r’   r”   Úpropertyr—   r   r¤   Ú__classcell__r   r   r¡   r   r   =   s¼    ÿþýüûúùø	÷
öõôóòñðïçþòú	 
	


	r   )6Ú__doc__r‰   rš   Údjango.confr   Údjango.core.exceptionsr   Ú	django.dbr   Údjango.db.backends.base.baser   Údjango.db.utilsr   r™   Údjango.utils.functionalr   Údjango.utils.safestringr   Údjango.utils.versionr	   r   rk   Úpsycopg2.extensionsÚpsycopg2.extrasÚImportErrorÚer   ÚPSYCOPG2_VERSIONr   Úclientr   Úcreationr   Úfeaturesr   Úintrospectionr   Ú
operationsr   Úschemar   Úutilsr   Ú
extensionsÚregister_adapterÚQuotedStringÚextrasÚregister_uuidÚINETARRAY_OIDÚnew_array_typeÚUNICODEr   Úregister_typer   r   r   r   r   Ú<module>   sP    €ÿ
ý