
    ؄_%                        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 n# e$ r dZY nw xY w G d dej                  Z G d de          Z G d de          Z G d de          Z G d de	          Z G d de          Z G d d e
          Z  G d! d"e          Z!e!Z"dS )#a  
.. dialect:: postgresql+pg8000
    :name: pg8000
    :dbapi: pg8000
    :connectstring: postgresql+pg8000://user:password@host:port/dbname[?key=value&key=value...]
    :url: https://pythonhosted.org/pg8000/

.. note::

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

.. _pg8000_unicode:

Unicode
-------

pg8000 will encode / decode string values between it and the server using the
PostgreSQL ``client_encoding`` parameter; by default this is the value in
the ``postgresql.conf`` file, which often defaults to ``SQL_ASCII``.
Typically, this can be changed to ``utf-8``, as a more useful default::

    #client_encoding = sql_ascii # actually, defaults to database
                                 # encoding
    client_encoding = utf8

The ``client_encoding`` can be overridden for a session by executing the SQL:

SET CLIENT_ENCODING TO 'utf8';

SQLAlchemy will execute this SQL on all new connections based on the value
passed to :func:`_sa.create_engine` using the ``client_encoding`` parameter::

    engine = create_engine(
        "postgresql+pg8000://user:pass@host/dbname", client_encoding='utf8')


.. _pg8000_isolation_level:

pg8000 Transaction Isolation Level
-------------------------------------

The pg8000 dialect offers the same isolation level settings as that
of the :ref:`psycopg2 <psycopg2_isolation_level>` dialect:

* ``READ COMMITTED``
* ``READ UNCOMMITTED``
* ``REPEATABLE READ``
* ``SERIALIZABLE``
* ``AUTOCOMMIT``

.. versionadded:: 0.9.5 support for AUTOCOMMIT isolation level when using
   pg8000.

.. seealso::

    :ref:`postgresql_isolation_level`

    :ref:`psycopg2_isolation_level`


    N   )_DECIMAL_TYPES)_FLOAT_TYPES)
_INT_TYPES)
PGCompiler)	PGDialect)PGExecutionContext)PGIdentifierPreparer)UUID)JSON   )exc)
processors)types)util)quoted_namec                       e Zd Zd ZdS )
_PGNumericc                 @   | j         rX|t          v r$t          j        t          j        | j                  S |t          v s	|t          v rd S t          j
        d|z            |t          v rd S |t          v s	|t          v rt          j        S t          j
        d|z            )NzUnknown PG numeric type: %d)	asdecimalr   r   to_decimal_processor_factorydecimalDecimal_effective_decimal_return_scaler   r   r   InvalidRequestErrorto_float)selfdialectcoltypes      j/var/www/book.euthymeo.com/html/venv/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/pg8000.pyresult_processorz_PGNumeric.result_processor`   s    > 	,&&!>OT%I   N**g.C.Ct-1G;   ,&&tN**g.C.C!**-1G;      N)__name__
__module____qualname__r!    r"   r    r   r   _   s#            r"   r   c                       e Zd Zd ZdS )_PGNumericNoBindc                     d S Nr&   )r   r   s     r    bind_processorz_PGNumericNoBind.bind_processorz   s    tr"   N)r#   r$   r%   r+   r&   r"   r    r(   r(   y   s#            r"   r(   c                        e Zd Z fdZ xZS )_PGJSONc                 p    |j         dk    rd S t          t          |                               ||          S )N)r   
   r   )_dbapi_versionsuperr-   r!   )r   r   r   	__class__s      r    r!   z_PGJSON.result_processor   s6    !J..4$''88'JJJr"   )r#   r$   r%   r!   __classcell__r2   s   @r    r-   r-   ~   sA        K K K K K K K K Kr"   r-   c                       e Zd Zd Zd ZdS )_PGUUIDc                     | j         sd }|S d S )Nc                 (    | t          |           } | S r*   )_python_UUIDvalues    r    processz'_PGUUID.bind_processor.<locals>.process   s    $(//Er"   as_uuid)r   r   r<   s      r    r+   z_PGUUID.bind_processor   ,    | 	  
 N	 	r"   c                     | j         sd }|S d S )Nc                 (    | t          |           } | S r*   strr:   s    r    r<   z)_PGUUID.result_processor.<locals>.process   s    $JJEr"   r=   )r   r   r   r<   s       r    r!   z_PGUUID.result_processor   r?   r"   N)r#   r$   r%   r+   r!   r&   r"   r    r6   r6      s2              r"   r6   c                       e Zd ZdS )PGExecutionContext_pg8000N)r#   r$   r%   r&   r"   r    rE   rE      s        Dr"   rE   c                       e Zd Zd Zd ZdS )PGCompiler_pg8000c                 V     | j         |j        fi |dz    | j         |j        fi |z   S )Nz %% )r<   leftright)r   binaryoperatorkws       r    visit_mod_binaryz"PGCompiler_pg8000.visit_mod_binary   sF    DL++++dl6<..2../	
r"   c                 ^    d|v rt          j        d           |                    dd          S )N%%z^The SQLAlchemy postgresql dialect now automatically escapes '%' in text() expressions to '%%'.%)r   warnreplace)r   texts     r    post_process_textz#PGCompiler_pg8000.post_process_text   s:    4<<I'  
 ||C&&&r"   N)r#   r$   r%   rN   rU   r&   r"   r    rG   rG      s2        
 
 
' ' ' ' 'r"   rG   c                       e Zd Zd ZdS )PGIdentifierPreparer_pg8000c                 n    |                     | j        | j                  }|                     dd          S )NrQ   rP   )rS   escape_quoteescape_to_quote)r   r;   s     r    _escape_identifierz.PGIdentifierPreparer_pg8000._escape_identifier   s/    d/1EFF}}S$'''r"   N)r#   r$   r%   r[   r&   r"   r    rW   rW      s#        ( ( ( ( (r"   rW   c                   "    e Zd ZdZdZdZdZdZeZ	e
ZeZdZ ej        ej        ej        eej        eeeej        eeei          ZddZ fdZej        d             Ze d	             Z!d
 Z"d Z#d Z$d Z%d Z&d Z'	 ddZ(	 ddZ)d Z*d Z+ xZ,S )PGDialect_pg8000pg8000Tformatuse_encodingNc                 8    t          j        | fi | || _        d S r*   )r   __init__client_encoding)r   rc   kwargss      r    rb   zPGDialect_pg8000.__init__   s)    4**6***.r"   c                 x    | j         dk    | _        t          t          |                               |           d S )N)r   	      )r0   supports_sane_multi_rowcountr1   r]   
initialize)r   
connectionr2   s     r    ri   zPGDialect_pg8000.initialize   s8    ,0,?:,M)%%00<<<<<r"   c                     | j         rKt          | j         d          r6t          d t          j        d| j         j                  D                       S dS )N__version__c                 ,    g | ]}t          |          S r&   )int).0xs     r    
<listcomp>z3PGDialect_pg8000._dbapi_version.<locals>.<listcomp>   s.        FF  r"   z(\d+)(?:[-\.]?|$))c   rr   rr   )dbapihasattrtuplerefindallrl   )r   s    r    r0   zPGDialect_pg8000._dbapi_version   sj    : 
	 '$*m<< 
	  Z,dj.D       <r"   c                      t          d          S )Nr^   )
__import__)clss    r    rs   zPGDialect_pg8000.dbapi   s    (###r"   c                     |                     d          }d|v rt          |d                   |d<   |                    |j                   g |fS )Nuser)usernameport)translate_connect_argsrn   updatequery)r   urloptss      r    create_connect_argsz$PGDialect_pg8000.create_connect_args   sT    ))6)::T>>tF|,,DLCIDzr"   c                 $    dt          |          v S )Nzconnection is closedrB   )r   erj   cursors       r    is_disconnectzPGDialect_pg8000.is_disconnect   s    %Q//r"   c           
         |                     dd          }t          |d          r|j        }|dk    r	d|_        d S || j        v r^d|_        |                                }|                    d|z             |                    d           |                                 d S t          j	        d	|d
| j
        dd                    | j                  d          )N_ rj   
AUTOCOMMITTFz=SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL %sCOMMITzInvalid value 'z2' for isolation_level. Valid isolation levels for z are z, z or AUTOCOMMIT)rS   rt   rj   
autocommit_isolation_lookupr   executecloser   ArgumentErrornamejoin)r   rj   levelr   s       r    set_isolation_levelz$PGDialect_pg8000.set_isolation_level   s    c3'' :|,, 	/#.JL  $(J!!!d,,,$)J!&&((FNN%',-   NN8$$$LLNNNNN## 55$)))TYYt/E%F%F%F%FH  r"   c                     t          |d          r|j        }|                                }|                    d|z   dz              |                    d           |                                 d S )Nrj   zSET CLIENT_ENCODING TO ''r   )rt   rj   r   r   r   )r   rj   rc   r   s       r    set_client_encodingz$PGDialect_pg8000.set_client_encoding  sm    :|,, 	/#.J""$$1OCcIJJJx   r"   c                 @    |j                             d|df           d S Nr    )rj   	tpc_beginr   rj   xids      r    do_begin_twophasez"PGDialect_pg8000.do_begin_twophase  s%    ''C55555r"   c                 8    |j                                          d S r*   )rj   tpc_preparer   s      r    do_prepare_twophasez$PGDialect_pg8000.do_prepare_twophase  s    ))+++++r"   Fc                 @    |j                             d|df           d S r   )rj   tpc_rollbackr   rj   r   is_preparedrecovers        r    do_rollback_twophasez%PGDialect_pg8000.do_rollback_twophase  s'     	**AsB<88888r"   c                 @    |j                             d|df           d S r   )rj   
tpc_commitr   s        r    do_commit_twophasez#PGDialect_pg8000.do_commit_twophase#  s'     	((!S"66666r"   c                 H    d |j                                         D             S )Nc                     g | ]
}|d          S )r   r&   )ro   rows     r    rq   z8PGDialect_pg8000.do_recover_twophase.<locals>.<listcomp>)  s    FFF3AFFFr"   )rj   tpc_recover)r   rj   s     r    do_recover_twophasez$PGDialect_pg8000.do_recover_twophase(  s%    FF*"7"C"C"E"EFFFFr"   c                      g d }                     |            j         fd}                     |            j         fd}                     |           t                    dk    rfd}|S d S )Nc                 N    | j         t          j                 | j         t          <   d S r*   )py_typesr   	text_typer   )conns    r    
on_connectz/PGDialect_pg8000.on_connect.<locals>.on_connect.  s    )-t~)FDM+&&&r"   c                 >                         | j                   d S r*   )r   rc   r   r   s    r    r   z/PGDialect_pg8000.on_connect.<locals>.on_connect5  "    ((t/CDDDDDr"   c                 >                         | j                   d S r*   )r   isolation_levelr   s    r    r   z/PGDialect_pg8000.on_connect.<locals>.on_connect<  r   r"   r   c                 (    D ]} ||            d S r*   r&   )r   fnfnss     r    r   z/PGDialect_pg8000.on_connect.<locals>.on_connectC  s+      BBtHHHH r"   )appendrc   r   len)r   r   r   s   ` @r    r   zPGDialect_pg8000.on_connect+  s    	G 	G 	G 	

:+E E E E E JJz"""+E E E E E JJz"""s88a<<     4r"   r*   )TF)-r#   r$   r%   driversupports_unicode_statementssupports_unicode_bindsdefault_paramstylerh   rE   execution_ctx_clsrG   statement_compilerrW   preparerdescription_encodingr   update_copyr   colspecssqltypesNumericr(   Floatr   r   r-   r   r6   rb   ri   memoized_propertyr0   classmethodrs   r   r   r   r   r   r   r   r   r   r   r3   r4   s   @r    r]   r]      s       F"&!!#' 1**H)t.NJ'M7'	
	 	H/ / / /= = = = = 
      $ $ [$  0 0 0  2  6 6 6, , , :?9 9 9 9 :?7 7 7 7
G G G      r"   r]   )#__doc__r   rv   baser   r   r   r   r   r	   r
   r   jsonr   r   r   r   r   r   r   sql.elementsr   uuidr9   ImportErrorr   r   r(   r-   r6   rE   rG   rW   r]   r   r&   r"   r    <module>r      s  > >~  				                                     $ $ $ $ $ $ & & & & & &                         ! ! ! ! ! !       ' ' ' ' ' ')))))))   LLL    !   4    z   
K K K K Kd K K K    d   ,	 	 	 	 	 2 	 	 	' ' ' ' '
 ' ' '$( ( ( ( ("6 ( ( (Q Q Q Q Qy Q Q Qh s    A' 'A10A1