U
    ÌZj‰!  ã                   @   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 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 G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ de
ƒZG dd„ deƒZG d d!„ d!e	ƒZG d"d#„ d#eƒZ e Z!dS )$a3  
.. dialect:: postgresql+pygresql
    :name: pygresql
    :dbapi: pgdb
    :connectstring: postgresql+pygresql://user:password@host:port/dbname[?key=value&key=value...]
    :url: https://www.pygresql.org/

.. note::

    The pygresql dialect is **not tested as part of SQLAlchemy's continuous
    integration** and may have unresolved issues.  The recommended PostgreSQL
    dialect is psycopg2.

.. deprecated:: 1.4 The pygresql DBAPI is deprecated and will be removed
   in a future version. Please use one of the supported DBAPIs to
   connect to PostgreSQL.

é    Né   )Ú_DECIMAL_TYPES)Ú_FLOAT_TYPES)Ú
_INT_TYPES)Ú
PGCompiler)Ú	PGDialect)ÚPGIdentifierPreparer)ÚUUID)ÚHSTORE)ÚJSON)ÚJSONBé   )Úexc)Ú
processors)Úutil)ÚNull)ÚNumericc                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú
_PGNumericc                 C   s   d S ©N© )ÚselfÚdialectr   r   új/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/pygresql.pyÚbind_processor0   s    z_PGNumeric.bind_processorc                 C   s†   t |tƒs|j}| jrR|tkr.t tj| j	¡S |t
ks>|tkrBd S t d| ¡‚n0|tkr^d S |t
ksn|tkrttjS t d| ¡‚d S )NzUnknown PG numeric type: %d)Ú
isinstanceÚintÚoidZ	asdecimalr   r   Zto_decimal_processor_factoryÚdecimalÚDecimalZ_effective_decimal_return_scaler   r   r   ZInvalidRequestErrorZto_float©r   r   Úcoltyper   r   r   Úresult_processor3   s(    
 ÿÿÿz_PGNumeric.result_processorN)Ú__name__Ú
__module__Ú__qualname__r   r!   r   r   r   r   r   /   s   r   c                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )Ú	_PGHStorec                    s.   |j stt| ƒ |¡S |jj‰ ‡ fdd„}|S )Nc                    s   t | tƒrˆ | ƒS | S r   )r   Údict©Úvalue©Úhstorer   r   ÚprocessT   s    
z)_PGHStore.bind_processor.<locals>.process)Úhas_native_hstoreÚsuperr%   r   ÚdbapiZHstore©r   r   r+   ©Ú	__class__r)   r   r   O   s
    z_PGHStore.bind_processorc                    s   |j stt| ƒ ||¡S d S r   )r,   r-   r%   r!   r   r0   r   r   r!   [   s    z_PGHStore.result_processor©r"   r#   r$   r   r!   Ú__classcell__r   r   r0   r   r%   N   s   r%   c                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )Ú_PGJSONc                    s0   |j sttˆƒ |¡S |jj‰ ‡ ‡fdd„}|S )Nc                    sN   | ˆj krd } nt| tƒs(| d kr,ˆjr,d S | d ksBt| ttfƒrJˆ | ƒS | S r   ©ZNULLr   r   Znone_as_nullr&   Úlistr'   ©Újsonr   r   r   r+   f   s    

ÿÿz'_PGJSON.bind_processor.<locals>.process)Úhas_native_jsonr-   r4   r   r.   ÚJsonr/   r0   r7   r   r   a   s
    z_PGJSON.bind_processorc                    s   |j stt| ƒ ||¡S d S r   )r9   r-   r4   r!   r   r0   r   r   r!   s   s    z_PGJSON.result_processorr2   r   r   r0   r   r4   `   s   r4   c                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )Ú_PGJSONBc                    s0   |j sttˆƒ |¡S |jj‰ ‡ ‡fdd„}|S )Nc                    sN   | ˆj krd } nt| tƒs(| d kr,ˆjr,d S | d ksBt| ttfƒrJˆ | ƒS | S r   r5   r'   r7   r   r   r+   ~   s    

ÿÿz(_PGJSONB.bind_processor.<locals>.process)r9   r-   r;   r   r.   r:   r/   r0   r7   r   r   y   s
    z_PGJSONB.bind_processorc                    s   |j stt| ƒ ||¡S d S r   )r9   r-   r;   r!   r   r0   r   r   r!   ‹   s    z_PGJSONB.result_processorr2   r   r   r0   r   r;   x   s   r;   c                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )Ú_PGUUIDc                    s.   |j stt| ƒ |¡S |jj‰ ‡ fdd„}|S )Nc                    sP   | d krd S t | ttfƒr8t| ƒdkr0ˆ | d�S ˆ | ƒS t | tƒrLˆ | d�S | S )Né   )Úbytes)r   )r   Ústrr>   Úlenr   r'   ©Úuuidr   r   r+   –   s    


z'_PGUUID.bind_processor.<locals>.process)Úhas_native_uuidr-   r<   r   r.   ZUuidr/   r0   rA   r   r   ‘   s
    z_PGUUID.bind_processorc                    s.   |j stt| ƒ ||¡S | js*dd„ }|S d S )Nc                 S   s   | d k	rt | ƒS d S r   )r?   r'   r   r   r   r+   ¨   s    z)_PGUUID.result_processor.<locals>.process)rC   r-   r<   r!   Zas_uuid)r   r   r    r+   r0   r   r   r!   £   s
    z_PGUUID.result_processorr2   r   r   r0   r   r<   �   s   r<   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú_PGCompilerc                 K   s$   | j |jf|Žd | j |jf|Ž S )Nz %% )r+   ÚleftÚright)r   ÚbinaryÚoperatorÚkwr   r   r   Úvisit_mod_binary°   s    ÿþÿz_PGCompiler.visit_mod_binaryc                 C   s   |  dd¡S ©Nú%z%%)Úreplace)r   Útextr   r   r   Úpost_process_text·   s    z_PGCompiler.post_process_textN)r"   r#   r$   rJ   rO   r   r   r   r   rD   ¯   s   rD   c                   @   s   e Zd Zdd„ ZdS )Ú_PGIdentifierPreparerc                 C   s   |  | j| j¡}|  dd¡S rK   )rM   Zescape_quoteZescape_to_quote)r   r(   r   r   r   Ú_escape_identifier¼   s    z(_PGIdentifierPreparer._escape_identifierN)r"   r#   r$   rQ   r   r   r   r   rP   »   s   rP   c                       sn   e Zd ZdZdZeZeZe	dd„ ƒZ
e ejeeeeeeeeeeeei¡Z‡ fdd„Zdd„ Zd	d
„ Z‡  ZS )ÚPGDialect_pygresqlZpygresqlTc                 C   s   dd l }tjddd� |S )Nr   zŠThe pygresql DBAPI is deprecated and will be removed in a future version. Please use one of the supported DBAPIs to connect to PostgreSQL.z1.4)Úversion)Úpgdbr   Zwarn_deprecated)ÚclsrT   r   r   r   r.   É   s    üzPGDialect_pygresql.dbapic              
      sÂ   t t| ƒjf |Ž z4| jj}t d|¡}t| d¡ƒt| d¡ƒf}W n t	t
tfk
rd   d}Y nX || _|dk r”d } }}|dkr¬t d¡ nd| _d| _d } }}|| _|| _|| _d S )	Nz(\d+)\.(\d+)r   é   )r   r   )é   r   FzAPyGreSQL is only fully supported by SQLAlchemy since version 5.0.T)r-   rR   Ú__init__r.   rS   ÚreÚmatchr   ÚgroupÚAttributeErrorÚ
ValueErrorÚ	TypeErrorZdbapi_versionr   ÚwarnZsupports_unicode_statementsZsupports_unicode_bindsr,   r9   rC   )r   ÚkwargsrS   Úmr,   r9   rC   r0   r   r   rX   â   s(     
ÿzPGDialect_pygresql.__init__c                 C   sR   |j dd�}d|kr>d| dd¡ dd¡d	 | d¡f |d< | |j¡ g |fS )
NÚuser)ÚusernameÚportz%s:%sÚhostÚ ú:r   r   )Ztranslate_connect_argsÚgetÚrsplitÚpopÚupdateÚquery)r   ÚurlÚoptsr   r   r   Úcreate_connect_argsú   s    þ
z&PGDialect_pygresql.create_connect_argsc                 C   sj   t || jjƒrf|sdS z
|j}W n tk
r4   Y n
X |s>dS z|jW S  tk
rd   |jd k Y S X dS )NF)r   r.   ÚErrorÚ
connectionr\   ÚclosedZ_cnx)r   Úerq   Úcursorr   r   r   Úis_disconnect  s    
z PGDialect_pygresql.is_disconnect)r"   r#   r$   ZdriverZsupports_statement_cacherD   Zstatement_compilerrP   ÚpreparerÚclassmethodr.   r   Zupdate_copyr   Zcolspecsr   r   r
   r%   r:   r4   r   r   r;   r	   r<   rX   ro   ru   r3   r   r   r0   r   rR   Á   s2   
      úþ
rR   )"Ú__doc__r   rY   Úbaser   r   r   r   r   r   r	   r*   r
   r8   r   r   rf   r   r   r   Zsql.elementsr   Útypesr:   r   r   r%   r4   r;   r<   rD   rP   rR   r   r   r   r   r   Ú<module>   s6   U