o
    Rc"                     @   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                   @      e Zd Zdd Zdd ZdS )
_PGNumericc                 C   s   d S N )selfdialectr   r   ID:\Flask\env\Lib\site-packages\sqlalchemy/dialects/postgresql/pygresql.pybind_processor0   s   z_PGNumeric.bind_processorc                 C   s   t |ts|j}| jr(|tv rttj| j	S |t
v s|tv r!d S td| |tv r.d S |t
v s6|tv r9tjS td| )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                       (   e Zd Z fddZ fddZ  ZS )	_PGHStorec                    .   |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                       |j stt| ||S d S r   )r/   r0   r'   r"   r    r3   r   r   r"   [      z_PGHStore.result_processorr#   r$   r%   r   r"   __classcell__r   r   r3   r   r'   N   s    r'   c                       r&   )_PGJSONc                    0   |j stt|S |jj  fdd}|S )Nc                    N   | j u rd } nt| ts| d u rjrd S | d u s!t| ttfr% | S | S r   NULLr   r   Znone_as_nullr)   listr*   jsonr   r   r   r.   f      

z'_PGJSON.bind_processor.<locals>.process)has_native_jsonr0   r9   r   r1   Jsonr2   r3   r?   r   r   a   
   z_PGJSON.bind_processorc                    r5   r   )rB   r0   r9   r"   r    r3   r   r   r"   s   r6   z_PGJSON.result_processorr7   r   r   r3   r   r9   `       r9   c                       r&   )_PGJSONBc                    r:   )Nc                    r;   r   r<   r*   r?   r   r   r.   ~   rA   z(_PGJSONB.bind_processor.<locals>.process)rB   r0   rF   r   r1   rC   r2   r3   r?   r   r   y   rD   z_PGJSONB.bind_processorc                    r5   r   )rB   r0   rF   r"   r    r3   r   r   r"      r6   z_PGJSONB.result_processorr7   r   r   r3   r   rF   x   rE   rF   c                       r&   )_PGUUIDc                    r(   )Nc                    sP   | d u rd S t | ttfrt| dkr | dS  | S t | tr& | dS | S )N   )bytes)r   )r   strrI   lenr   r*   uuidr   r   r.      s   


z'_PGUUID.bind_processor.<locals>.process)has_native_uuidr0   rG   r   r1   ZUuidr2   r3   rL   r   r      s
   z_PGUUID.bind_processorc                    s.   |j stt| ||S | jsdd }|S d S )Nc                 S   s   | d urt | S d S r   )rJ   r*   r   r   r   r.      s   z)_PGUUID.result_processor.<locals>.process)rN   r0   rG   r"   Zas_uuid)r   r   r!   r.   r3   r   r   r"      s   z_PGUUID.result_processorr7   r   r   r3   r   rG      rE   rG   c                   @   r   )_PGCompilerc                 K   s,   | j |jfi |d | j |jfi | 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%   rU   rZ   r   r   r   r   rO      s    rO   c                   @   s   e Zd Zdd ZdS )_PGIdentifierPreparerc                 C   s   | | j| j}| ddS rV   )rX   Zescape_quoteZescape_to_quote)r   r+   r   r   r   _escape_identifier   s   z(_PGIdentifierPreparer._escape_identifierN)r#   r$   r%   r\   r   r   r   r   r[      s    r[   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   zThe 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)clsr_   r   r   r   r1      s   zPGDialect_pygresql.dbapic              
      s   t t| jd	i | z| jj}td|}t|dt|df}W n t	t
tfy3   d}Y nw || _|dk rKd } }}|dkrJt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.Tr   )r0   r]   __init__r1   r^   rematchr   groupAttributeError
ValueError	TypeErrorZdbapi_versionr   warnZsupports_unicode_statementsZsupports_unicode_bindsr/   rB   rN   )r   kwargsr^   mr/   rB   rN   r3   r   r   rc      s,    
zPGDialect_pygresql.__init__c                 C   sR   |j dd}d|v 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   sf   t || jjr1|sdS z|j}W n	 ty   Y nw |sdS z|jW S  ty0   |jd u  Y S w dS )NF)r   r1   Error
connectionrg   closedZ_cnx)r   er|   cursorr   r   r   is_disconnect  s    
z PGDialect_pygresql.is_disconnect)r#   r$   r%   ZdriverZsupports_statement_cacherO   Zstatement_compilerr[   preparerclassmethodr1   r   Zupdate_copyr   Zcolspecsr   r   r
   r'   rC   r9   r   r   rF   r	   rG   rc   rz   r   r8   r   r   r3   r   r]      s(    

r]   )"__doc__r   rd   baser   r   r   r   r   r   r	   r-   r
   r@   r   r   rq   r   r   r   Zsql.elementsr   typesrC   r   r   r'   r9   rF   rG   rO   r[   r]   r   r   r   r   r   <module>   s8   U