U
    ڲgV                     @  s   d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
mZ ddlmZ ddlmZ dd	lmZ ertdd
lmZ ejZedZG dd dejeZG dd dejeZG dd deZG dd deZdS )z)
Entry point into the adaptation system.
    )annotations)ABCabstractmethod)AnyTYPE_CHECKING   )pqabc)PyFormat)Transformer)AdaptersMap)BaseConnection\c                   @  s   e Zd ZU dZdZded< ejjZ	ded< d dd	d
ddZ
ddddZedddddZdddddZddddddZddd dddZdS )!DumperzP
    Convert Python object of the type `!cls` to PostgreSQL representation.
    r   intoid	pq.FormatformatNtypeabc.AdaptContext | Noneclscontextc                 C  s   || _ |  |r|jnd | _d S N)r   
connectionselfr   r    r   1/tmp/pip-unpacked-wheel-b_ea6rx_/psycopg/adapt.py__init__&   s    zDumper.__init__str)returnc              	   C  s2   dt | j dt | j d| j dt| dd	S )N<.z (oid=z) at 0xx>)r   
__module____qualname__r   id)r   r   r   r   __repr__+   s    0zDumper.__repr__r   zBuffer | None)objr!   c                 C  s   d S r   r   )r   r*   r   r   r   dump1   s    zDumper.dumpBufferc                 C  s   |  |}|dkrdS | jr4t| jj}||S t }||}t|krZd| d S d| d }|ddkr|dd}|S )z
        By default return the `dump()` value quoted and sanitised, so
        that the result can be used to build a SQL string. This works well
        for most types and you won't likely have to implement this method in a
        subclass.
        Ns   NULL   's    E'   \s   \\)	r+   r   r   ZEscapingZpgconnZescape_literalZescape_stringORD_BSreplace)r   r*   valueescoutrvr   r   r   quote4   s    


zDumper.quoter
   zabc.DumperKey)r*   r   r!   c                 C  s   | j S )a  
        Implementation of the `~psycopg.abc.Dumper.get_key()` member of the
        `~psycopg.abc.Dumper` protocol. Look at its definition for details.

        This implementation returns the `!cls` passed in the constructor.
        Subclasses needing to specialise the PostgreSQL type according to the
        *value* of the object dumped (not only according to to its type)
        should override this class.

        )r   r   r*   r   r   r   r   get_keya   s    zDumper.get_keyc                 C  s   | S )a/  
        Implementation of the `~psycopg.abc.Dumper.upgrade()` member of the
        `~psycopg.abc.Dumper` protocol. Look at its definition for details.

        This implementation just returns `!self`. If a subclass implements
        `get_key()` it should probably override `!upgrade()` too.
        r   r6   r   r   r   upgraden   s    zDumper.upgrade)N)__name__r&   r'   __doc__r   __annotations__r   FormatTEXTr   r   r)   r   r+   r5   r7   r8   r   r   r   r   r      s   
-r   c                   @  sH   e Zd ZU dZejjZded< dddddd	Z	e
d
ddddZdS )LoaderzK
    Convert PostgreSQL values with type OID `!oid` to Python objects.
    r   r   Nr   r   r   r   c                 C  s   || _ |  |r|jnd | _d S r   )r   r   r   r   r   r   r   r   r      s    zLoader.__init__r,   r   )datar!   c                 C  s   dS )z.Convert a PostgreSQL value to a Python object.Nr   )r   rA   r   r   r   load   s    zLoader.load)N)r9   r&   r'   r:   r   r<   r=   r   r;   r   r   rB   r   r   r   r   r>   y   s
   
r>   c                      s*   e Zd ZdZdddd fddZ  ZS )	RecursiveDumperz:Dumper with a transformer to help dumping recursive types.Nr   r   r   c                   s   t  || t|| _d S r   superr   r   Zfrom_contextZ_txr   	__class__r   r   r      s    zRecursiveDumper.__init__)Nr9   r&   r'   r:   r   __classcell__r   r   rF   r   rC      s   rC   c                      s*   e Zd ZdZdddd fddZ  ZS )	RecursiveLoaderz:Loader with a transformer to help loading recursive types.Nr   r   r?   c                   s   t  || t|| _d S r   rD   r@   rF   r   r   r      s    zRecursiveLoader.__init__)NrH   r   r   rF   r   rJ      s   rJ   N)r:   
__future__r   r	   r   r   typingr   r    r   Z_enumsr
   Z_transformerr   Z_adapters_mapr   Z_connection_baser   r,   ordr/   r   r>   rC   rJ   r   r   r   r   <module>   s   ^