
    j                      V   d dl mZ d dlZd dlZd dlmZmZ d dlmZ 	 d dl	Z	d dl	m
Z
 d dlmZ n# e$ r	 dZ	dZ
dZY nw xY w ej        dd          Z e ej        d	d
                    Z ej        d ej        dd                    Z ej        dd          Z ej        dd          Zda ej                    Zdeeef         fdZd ZddZedee         fd            Zddedeez  ez  dz  defdZ ddedeez  ez  dz  dee         fdZ!ddedeez  ez  dz  defdZ"dS )    )contextmanagerN)AnyIterator)logger)pool)RealDictCursorKINGBASE_HOST KINGBASE_PORT54321KINGBASE_DBKINGBASE_DBNAMEKINGBASE_USERKINGBASE_PASSWORDreturnc                  D    t           t          t          t          t          dS )N)hostportdatabaseuserpassword)r	   r   r   r   r        4/home/longshao/multi-rider-rag/shared/db/kingbase.py_connection_kwargsr      s     %  r   c                  >   t           t           S t          t          t          d          t          5  t           Ft          j        ddddt                      a t          j        dt          t          t                     ddd           n# 1 swxY w Y   t           S )z6Initialize the shared KingBase connection pool lazily.Nzpsycopg2 driver not available   
   )minconnmaxconnz.KingBase connection pool initialized: %s:%s/%sr   )_kingbase_poolpsycopg2r   RuntimeError
_pool_lockThreadedConnectionPoolr   r   infor	   r   r   r   r   r   	init_poolr'   (   s     !4<:;;;	  !!8   %&& N
 K@	                 s   ABBBc                      t           5  t          /t                                           dat          j        d           ddd           dS # 1 swxY w Y   dS )z2Close all connections in the shared KingBase pool.NzKingBase connection pool closed)r$   r!   closeallr   r&   r   r   r   
close_poolr*   A   s     
 ; ;%##%%%!NK9:::	; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;s   7AAAc               #   P  K   d} t                      }	 |                                } t          | dd          r+|                    | d           |                                } | V  |                                  nr# t
          $ re | at          t          | dd                    }|s(	 |                                  n# t
          $ r d}Y nw xY w|                    | |           d}  w xY w	 | |                    |            dS dS # | |                    |            w w xY w)z3Acquire a KingBase connection from the shared pool.Nclosedr   T)close)r'   getconngetattrputconncommit	Exceptionboolrollback)conndb_pool	is_closeds      r   get_connectionr8   K   sc      DkkG"  41%% 	%OODO---??$$D


 
 
 
WT8Q7788I %%MMOOOO  % % % $III%OOD	O222D
 	 OOD!!!!! 4OOD!!!! sB   A(A= <D =,C,*B?>C,?CC,CC,,D D%sqlparamsc                 N   t                      5 }|                    t                    5 }|                    | |           |                                }|rt          |          ni cd d d            cd d d            S # 1 swxY w Y   	 d d d            d S # 1 swxY w Y   d S )Ncursor_factory)r8   cursorr   executefetchonedict)r9   r:   r5   r>   rows        r   	query_onerC   h   s7   			 ,T[[[77 	,6NN3'''//##C #+4999	, 	, 	, 	, 	, 	, 	,, , , , , , , ,	, 	, 	, 	, 	, 	, 	, 	, 	,, , , , , , , , , , , , , , , , , ,s4   B=B(BB	BB		BB!Bc                 <   t                      5 }|                    t                    5 }|                    | |           d |                                D             cd d d            cd d d            S # 1 swxY w Y   	 d d d            d S # 1 swxY w Y   d S )Nr<   c                 ,    g | ]}t          |          S r   )rA   ).0rB   s     r   
<listcomp>zquery_all.<locals>.<listcomp>t   s    ;;;#DII;;;r   )r8   r>   r   r?   fetchallr9   r:   r5   r>   s       r   	query_allrJ   p   s1   			 <T[[[77 	<6NN3''';;):):;;;	< 	< 	< 	< 	< 	< 	<< < < < < < < <	< 	< 	< 	< 	< 	< 	< 	< 	<< < < < < < < < < < < < < < < < < <s4   B4A8B8A<	<B?A<	 BBBc                     t                      5 }|                                5 }|                    | |           |j        cd d d            cd d d            S # 1 swxY w Y   	 d d d            d S # 1 swxY w Y   d S N)r8   r>   r?   rowcountrI   s       r   r?   r?   w   s   			 #T[[]] 	#fNN3'''?	# 	# 	# 	# 	# 	# 	## # # # # # # #	# 	# 	# 	# 	# 	# 	# 	# 	## # # # # # # # # # # # # # # # # #s4   A3AA3A	A3!A	"A33A7:A7)r   NrL   )#
contextlibr   os	threadingtypingr   r   shared.config.configr   r"   r   psycopg2.extrasr   r2   getenvr	   intr   r   r   r   r!   Lockr$   rA   strr   r'   r*   r8   tuplelistrC   rJ   r?   r   r   r   <module>rZ      s~   % % % % % % 				                     ' ' ' ' ' 'OOO.......   HDNNN 	/2..IBIow7788biyry1BB'G'GHH	/2..BI1266  Y^
DcN      2; ; ; ; " " " " "8, ,3 ,ut 3d : ,d , , , ,< <3 <ut 3d : <d4j < < < <# # #dUlT1D8 #C # # # # # #s   / ==