
    'j                         d Z ddlZddlmZmZ ddZddZdej	        fdZde
dedz  fd	Zd
e
dedz  fdZd
ede
de
de
fdZdS )u  Authentication database — delegates to the shared asyncpg pool.

Auth and conversation share the same PostgreSQL database (DB_* env vars).
Rather than maintaining a second pool, this module reuses the pool owned
by ``backend.chanakya.conversation.database``.
    N)ensure_pool
close_poolreturnc                  2   K   t                       d{V  dS )z&Warm up the shared DB pool at startup.N_ensure_pool     C/var/www/html/ai-enterprise-brain/backend/chanakya/auth/database.pyinit_auth_dbr      s(      
..r
   c                  2   K   t                       d{V  dS )z%Close the shared DB pool at shutdown.N)_close_poolr	   r
   r   close_auth_dbr      s(      
--r
   c                  .   K   t                       d {V S Nr   r	   r
   r   r   r      s"      r
   usernamec                 :  K   t                       d {V }|                                4 d {V }|                    d|            d {V }d d d           d {V  n# 1 d {V swxY w Y   |d S |d         |d         |d         |d         |d         d u dS )Nz
            SELECT id, username, hashed_password, deleted_at
            FROM eb_users
            WHERE username = $1 AND deleted_at IS NULL
            idr   hashed_password
deleted_at)r   r   r   r   	is_active)r   acquirefetchrow)r   poolconnrows       r   get_user_by_usernamer   !   s]     D||~~ 
 
 
 
 
 
 
MM
 
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 {t$i
O01,'&$.  s   A  
A*-A*user_idc                 0   K   t          |            d {V S r   )r   )r   s    r   get_user_by_idr    7   s&      %g.........r
   r   c                    K   t                       d {V }|                                4 d {V }|                    d| ||           d {V }d d d           d {V  n# 1 d {V swxY w Y   |rdndS )Na>  
            INSERT INTO eb_users (id, username, hashed_password)
            VALUES ($1, $2, $3)
            ON CONFLICT (id) DO UPDATE
                SET username        = EXCLUDED.username,
                    hashed_password = EXCLUDED.hashed_password
            RETURNING (xmax = 0) AS was_inserted
            insertedupdated)r   r   fetchval)r   r   r   r   r   results         r   upsert_userr&   ;   s-     D||~~ 
 
 
 
 
 
 
}} 
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  .::Y.s   A""
A,/A,)r   N)__doc__asyncpg&backend.chanakya.conversation.databaser   r   r   r   r   r   Poolstrdictr   r    intr&   r	   r
   r   <module>r.      s              
   
 7<             ,/# /$+ / / / //s /c /C /C / / / / / /r
   