o
    g                     @   sR   d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
 G dd dZdS )	z/Function to standardize the connection to MySQL    N)asynccontextmanager)
quote_plus)AsyncSessioncreate_async_engine)sessionmakerc                   @   s.   e Zd ZdZd	ddZedd Zdd ZdS )
MySQLConnectorz,Class to standardize the connection to MySQLNc                 C   s|   t jd}t jd}t jd}|pt jd}t|}d| d| d| d| }t|d	d
| _t| jtdd| _dS )z1Constructor to initialize the connection to MySQLrds_usernamerds_password	rds_routerds_databasezmysql+asyncmy://:@/T)echoF)bindclass_expire_on_commitN)	osenvirongetr   r   enginer   r   async_session)selfdbnameuserpasswordhostencoded_passworddatabase_url r   1/var/www/html/XCapMarket/utils/mysql_connector.py__init__   s   zMySQLConnector.__init__c                 C  s6   |   }z|V  W | I dH  dS | I dH  w )z Return an async session properlyN)r   close)r   sessionr   r   r    get_session   s
   "zMySQLConnector.get_sessionc                    s   | j  I dH  dS )zMethod to close the engineN)r   dispose)r   r   r   r    r"   '   s   zMySQLConnector.close)N)__name__
__module____qualname____doc__r!   r   r$   r"   r   r   r   r    r      s    

r   )r)   r   
contextlibr   urllib.parser   sqlalchemy.ext.asyncior   r   sqlalchemy.ormr   r   r   r   r   r    <module>   s    