
    ɧ-i                        U d dl Z d dlmZ d dlmZ  ed                              e          Z edd          Z	ee
d<   daee         e
d<    e j        d	 e j        d
d                    Z e j        d e j        dd                    Z e j        d e j        dd                    Z e j        d e j        dd                    Z e j        d e j        dd                    Z e e j        dd                    Z e e j        dd                    Z e e j        dd                    Z G d d          Zd  Zd! Zd" Zd# Zd$ ZdS )%    N)
ContextVar)Optionalloggingdb_connection)default_db_connection_connection_poolDB_NAMEMYSQL_DBeatance_website_builderDB_USER
MYSQL_USERrootDB_PASSWORDMYSQL_PASSWORD DB_HOST
MYSQL_HOST	localhostDB_PORT
MYSQL_PORT3306DB_POOL_MIN_SIZE5DB_POOL_MAX_SIZE20DB_POOL_RECYCLE3600c                   \    e Zd ZdZdefdZd ZddZd Zd Z	d	 Z
d
 Zd Zd Zd Zd ZdS )AsyncDBAdapterzAsync adapter that exposes execute(), executemany(), cursor(), commit(), and fetch helpers
    for underlying async connections from aiomysql.
    backendc                 0    || _         || _        d | _        d S N)_connr!   _last_cursor)selfconnr!   s      7/var/www/html/web-builder-api.evdpl.com/src/utils/db.py__init__zAsyncDBAdapter.__init__   s    
     c                    K   | j         dk    r| j                                         d {V S | j                                         d {V S )Nmysql)r!   r$   cursorr&   s    r(   r-   zAsyncDBAdapter.cursor#   s_      <7""**,,,,,,,,,Z&&(((((((((r*   Nc                   K   |                                   d {V }|}|}|5| j        dv r,	 d|v r|                    dd          }n# t          $ r Y nw xY w|%t	          |t
          t          t          f          s|f}	 ||                    |           d {V  n|                    ||           d {V  || _	        |S # t          $ r}t          |          }	 t                              d| j         d|            n# t          $ r Y nw xY w|pt	          |t
          t          f          rT	 t          d |D                       }|                    ||           d {V  || _	        |cY d }~S # t          $ r Y nw xY w	 t                              d| j         d| d	| d
|            n# t          $ r Y nw xY w d }~ww xY w)N)postgresr,   ?z%sz6DB execute failed, attempting tolerant retry: backend=z; error=c              3   <   K   | ]}|d nt          |          V  d S r#   )str).0xs     r(   	<genexpr>z)AsyncDBAdapter.execute.<locals>.<genexpr>I   s0      II!	s1vvIIIIIIr*   zDB execute error (backend=z): z; query=z	; params=)r-   r!   replace	Exception
isinstancelisttupledictexecuter%   r3   loggerdebugerror)	r&   queryparamscurqpemsgalts	            r(   r=   zAsyncDBAdapter.execute)   s{     KKMM!!!!!!=T\-BBB!88		#t,,A    =AeT/B!C!C=A	ykk!nn$$$$$$$$kk!Q''''''''' #DJ 	 	 	a&&CqVZVbqqloqqrrrr    }Ae}!=!=}IIqIIIIIC++a---------(+D%JJJJJJ    De$,ee1eeVWeebceeffff   +	s   A 
AA AC 
GG%DG
DGD!G2=E5/G5
F?GFG+F21G2
F?<G>F??GGc                    K   |                                   d {V }|                    ||           d {V }|| _        |S r#   )r-   executemanyr%   )r&   rA   seq_of_paramsrC   ress        r(   rJ   zAsyncDBAdapter.executemanyV   sW      KKMM!!!!!!OOE=99999999
r*   c                    K   | j         4|                                  d {V }|                                 d {V S | j                                          d {V S r#   )r%   r-   fetchoner&   rC   s     r(   rN   zAsyncDBAdapter.fetchone\   u      $%%%%%%C'''''''&//111111111r*   c                    K   | j         4|                                  d {V }|                                 d {V S | j                                          d {V S r#   )r%   r-   fetchallrO   s     r(   rR   zAsyncDBAdapter.fetchallb   rP   r*   c                 l   K   	 | j                                          d {V  d S # t          $ r Y d S w xY wr#   )r$   commitr8   r.   s    r(   rT   zAsyncDBAdapter.commith   sY      	*##%%%%%%%%%%% 	 	 	DD	   % 
33c                 l   K   	 | j                                          d {V  d S # t          $ r Y d S w xY wr#   )r$   closer8   r.   s    r(   rW   zAsyncDBAdapter.closen   sY      	*""$$$$$$$$$$$ 	 	 	DD	rU   c                 
   K   | S r#    r.   s    r(   
__aenter__zAsyncDBAdapter.__aenter__t   s      r*   c                    K   	 |3	 | j                                          d {V  d S # t          $ r Y d S w xY w	 | j                                          d {V  d S # t          $ r Y d S w xY w# t          $ r Y d S w xY wr#   )r$   rT   r8   rollback)r&   exc_typeexctbs       r(   	__aexit__zAsyncDBAdapter.__aexit__w   s      	*++-----------    DD*--///////////    DD 	 	 	DD	sD   A, ( 
6A, 6A, A 
A)%A, (A))A, ,
A:9A:c                 ,    t          | j        |          S r#   )getattrr$   )r&   names     r(   __getattr__zAsyncDBAdapter.__getattr__   s    tz4(((r*   r#   )__name__
__module____qualname____doc__r3   r)   r-   r=   rJ   rN   rR   rT   rW   rZ   r`   rd   rY   r*   r(   r    r       s         !c ! ! ! !
) ) )+ + + +Z  2 2 22 2 2         ) ) ) ) )r*   r    c                  d  K   t           t           S 	 ddl} n-# t          $ r }t                              d            d}~ww xY wt
          t          t          t          dt          t          t          dd	}t          r)	 t          t                    |d<   n# t          $ r Y nw xY w	  | j        di | d{V a t                              dt           d	t
           d
t           dt           dt           d           n0# t          $ r#}t                              d|             d}~ww xY wt           S )z7Create a global connection pool for better performance.Nr   ;aiomysql is required for mysql backend but is not installedutf8mb4F)	hostuserpassworddbcharsetminsizemaxsizepool_recycle
autocommitportzCreated MySQL connection pool: @:z (min=z, max=)z"Failed to create connection pool: rY   )r	   aiomysqlr8   r>   r@   r   r   r   r
   POOL_MIN_SIZEPOOL_MAX_SIZEPOOL_RECYCLEr   intcreate_poolinfo)ry   rF   	conn_infos      r(   _create_connection_poolr      s|      #   RSSS
   $
 
I  	 #GIf 	 	 	D	!5!5!B!B	!B!BBBBBBB~g~~~~'~~Yf~~n{~~~   =!==>>> s=    
A<A;B 
B B $AC; ;
D(D##D(c                  Z  K   	 ddl } n-# t          $ r }t                              d            d}~ww xY wt                                          }||S t                       d{V }|                                 d{V }t          |d          }t          	                    |           |S )zGet a connection from the pool.r   Nrj   r,   )
ry   r8   r>   r@   r   getr   acquirer    set)ry   rF   existing_connpoolr'   adapters         r(   _connect_mysqlr      s         RSSS
 #&&((M  )********D DT7++GwNs   	 
3.3c                  .   K   t                       d{V S )z+Return an async DB connection (MySQL only).N)r   rY   r*   r(   get_connectionr      s$      !!!!!!!!!r*   c                     K   	 t                                           } | Bt          t                              | j                   t                               d           dS dS # t          $ r Y dS w xY w)z$Release connection back to the pool.N)r   r   r	   releaser$   r   r8   )r'   s    r(   close_connectionr      s      !!##+ ((444t$$$$$	 
    s   AA# #
A10A1c                  0  K   	 t           Vt                                            t                                            d{V  da t                              d           dS dS # t
          $ r(} t                              d|             Y d} ~ dS d} ~ ww xY w)zKClose the entire connection pool. Should be called on application shutdown.NzConnection pool closedzError closing connection pool: )r	   rW   wait_closedr>   r   r8   r@   )rF   s    r(   
close_poolr      s      <'""$$$"..000000000#KK011111	 ('
  < < <:q::;;;;;;;;;<s   AA# #
B-BB)oscontextvarsr   typingr   
__import__	getLoggerre   r>   r   __annotations__r	   anygetenvr
   r   r   r   r   r}   rz   r{   r|   r    r   r   r   r   r   rY   r*   r(   <module>r      s   					 " " " " " "      	I			(	(	2	2 (ZFFF
 F F F #' (3- & & & ")Iyry5NOO
P
P
")Iyryv>>
?
?biyry1A2'F'FGG
")Iyry{CC
D
D
")Iyryv>>
?
? IBI0#6677IBI0$7788s929.7788o) o) o) o) o) o) o) o)d& & &P  0" " "
  
< 
< 
< 
< 
<r*   