3
a!                 @   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   M/tmp/pip-build-6_cqtusv/SQLAlchemy/sqlalchemy/dialects/postgresql/pygresql.pybind_processor0   s    z_PGNumeric.bind_processorc             C   s   t |ts|j}| jrR|tkr.tjtj| j	S |t
ks>|tkrBd S tjd| n0|tkr^d S |t
ksn|tkrttjS tj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| j|S |jj  fdd}|S )Nc                s   t | tr | S | S )N)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| j||S d S )N)r(   r)   r#   r   )r   r   r   )r+   r   r   r   [   s    z_PGHStore.result_processor)r    r!   r"   r   r   __classcell__r   r   )r+   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j|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 )N)NULLr   r   none_as_nullr$   list)r%   )jsonr   r   r   r'   f   s    

z'_PGJSON.bind_processor.<locals>.process)has_native_jsonr)   r-   r   r*   Json)r   r   r'   )r+   )r1   r   r   r   a   s
    z_PGJSON.bind_processorc                s   |j stt| j||S d S )N)r2   r)   r-   r   )r   r   r   )r+   r   r   r   s   s    z_PGJSON.result_processor)r    r!   r"   r   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 )_PGJSONBc                s0   |j sttj|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 )N)r.   r   r   r/   r$   r0   )r%   )r1   r   r   r   r'   ~   s    

z(_PGJSONB.bind_processor.<locals>.process)r2   r)   r4   r   r*   r3   )r   r   r'   )r+   )r1   r   r   r   y   s
    z_PGJSONB.bind_processorc                s   |j stt| j||S d S )N)r2   r)   r4   r   )r   r   r   )r+   r   r   r      s    z_PGJSONB.result_processor)r    r!   r"   r   r   r,   r   r   )r+   r   r4   x   s   r4   c                   s(   e Zd Z fddZ fddZ  ZS )_PGUUIDc                s.   |j stt| j|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   strr7   lenr   )r%   )uuidr   r   r'      s    


z'_PGUUID.bind_processor.<locals>.process)has_native_uuidr)   r5   r   r*   ZUuid)r   r   r'   )r+   )r:   r   r      s
    z_PGUUID.bind_processorc                s.   |j stt| j||S | js*dd }|S d S )Nc             S   s   | d k	rt | S d S )N)r8   )r%   r   r   r   r'      s    z)_PGUUID.result_processor.<locals>.process)r;   r)   r5   r   Zas_uuid)r   r   r   r'   )r+   r   r   r      s
    z_PGUUID.result_processor)r    r!   r"   r   r   r,   r   r   )r+   r   r5      s   r5   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   |j ddS )N%z%%)replace)r   textr   r   r   post_process_text   s    z_PGCompiler.post_process_textN)r    r!   r"   rB   rF   r   r   r   r   r<      s   r<   c               @   s   e Zd Zdd ZdS )_PGIdentifierPreparerc             C   s   |j | j| j}|j ddS )NrC   z%%)rD   Zescape_quoteZescape_to_quote)r   r%   r   r   r   _escape_identifier   s    z(_PGIdentifierPreparer._escape_identifierN)r    r!   r"   rH   r   r   r   r   rG      s   rG   c                   sn   e Zd ZdZdZeZeZe	dd Z
ej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)clsrK   r   r   r   r*      s
    zPGDialect_pygresql.dbapic                s   t t| jf | y4| jj}tjd|}t|jdt|jdf}W n t	t
tfk
rd   d	}Y nX || _|d
k rd } }}|dkrtjd nd| _d| _d } }}|| _|| _|| _d S )Nz(\d+)\.(\d+)r      r      FzAPyGreSQL is only fully supported by SQLAlchemy since version 5.0.T)r   r   )rN   r   )r   r   )r)   rI   __init__r*   rJ   rematchr   groupAttributeError
ValueError	TypeErrorZdbapi_versionr   warnZsupports_unicode_statementsZsupports_unicode_bindsr(   r2   r;   )r   kwargsrJ   mr(   r2   r;   )r+   r   r   rO      s&     
zPGDialect_pygresql.__init__c             C   sR   |j dd}d|kr>d|jddjddd	 |jdf |d< |j|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   sd   t || jjr`|sdS y
|j}W n tk
r4   Y n
X |s>dS y|jS  tk
r^   |jd kS X dS )NF)r   r*   Error
connectionrS   closedZ_cnx)r   erh   cursorr   r   r   is_disconnect  s    
z PGDialect_pygresql.is_disconnect)r    r!   r"   ZdriverZsupports_statement_cacher<   Zstatement_compilerrG   Zpreparerclassmethodr*   r   Zupdate_copyr   Zcolspecsr   r   r
   r#   r3   r-   r   r   r4   r	   r5   rO   rf   rl   r,   r   r   )r+   r   rI      s    

rI   )"__doc__r   rP   baser   r   r   r   r   r   r	   r&   r
   r1   r   r   r]   r   r   r   Zsql.elementsr   typesr3   r   r   r#   r-   r4   r5   r<   rG   rI   r   r   r   r   r   <module>   s6   U