
    tĀi	                        U d Z ddlZddlmZ ddlmZmZmZ ddlZ	ddlm
Z ddlmZ ddlmZ  ej                   e      Zdaee   ed<   d	efd
Zed	ee	j,                  j.                  ddf   fd       Ze	 ddeded	ee	j,                  j4                  j6                  ddf   fd       Z	 ddedee   ded	eee eef         fdZ!dedee   d	e"fdZ#d	efdZ$y)z/
MariaDB connection pool for content pipeline.
    N)contextmanager)Any	GeneratorOptional)Error)MySQLConnectionPool)get_settings_poolreturnc                      t         8t               } t        ddddd| j                  a t        j                  d       t         S )zGet or create connection pool.content_pipeline   T)	pool_name	pool_sizepool_reset_sessionz Database connection pool created )r
   r	   r   	db_configloggerinfo)settingss    5/var/www/html/content-pipeline/database/connection.pyget_poolr      sL     }># 
(#
   	
 	67L    c               #      K   t               } | j                         }	 | |j                          y# |j                          w xY ww)zGet a connection from the pool.N)r   get_connectionclose)pool
connections     r   r   r   #   s@      :D$$&J
s   A3 AAA
dictionarybufferedc              #   \  K   t               5 }|j                  | |      }	 | |j                          	 |j                          	 ddd       y# t        $ r.}|j	                          t
        j                  d|         d}~ww xY w# |j                          w xY w# 1 sw Y   yxY ww)z2Get a cursor with automatic connection management.)r   r    zDatabase error: N)r   cursorcommit
MySQLErrorrollbackr   errorr   )r   r    r   r"   es        r   
get_cursorr(   .   s     
 
	 
Z""j8"L	L LLN
 

  	!LL+A3/0	
 LLN
 
sJ   B,B AB 	B,	B)BBBBB  B)%B,queryparamsfetchc                     t               5 }|j                  | |       |r|j                         cddd       S 	 ddd       y# 1 sw Y   yxY w)z-Execute a query and optionally fetch results.N)r(   executefetchall)r)   r*   r+   r"   s       r   execute_queryr/   @   sJ     
 uf%??$  	  s   $AAAparams_listc                 ~    t               5 }|j                  | |       |j                  cddd       S # 1 sw Y   yxY w)z-Execute a query with multiple parameter sets.N)r(   executemanyrowcount)r)   r0   r"   s      r   execute_manyr4   K   s5    	 5+.  s   3<c                      	 t        d      } | duxr t        |       dkD  S # t        $ r"}t        j	                  d|        Y d}~yd}~ww xY w)zTest database connectivity.zSELECT 1 as testNr   zConnection test failed: F)r/   lenr$   r   r&   )resultr'   s     r   test_connectionr8   R   sQ    12T!5c&kAo5 /s34s   ! 	AAA)TT)NT)%__doc__logging
contextlibr   typingr   r   r   mysql.connectormysqlr   r$   mysql.connector.poolingr   config.settingsr	   	getLogger__name__r   r
   __annotations__r   	connectorMySQLConnectionr   boolr"   MySQLCursorr(   strtuplelistdictr/   intr4   r8   r   r   r   <module>rM      s:    % + +  / 7 (			8	$'+x#$ +%  	%//"A"A4"MN   .2'+u%%114=> $ ?C 7;d4S>"# $u+ #  r   