U
    9a!                     @   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   K/tmp/pip-unpacked-wheel-7pet0iyd/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   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)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 rd } }}|dkrt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