o
    *gw                     @   s   d dl Z d dlZd dlZd dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ e
 ZG dd	 d	Ze Zed
krQdd Ze e  dS dS )    N)deleteand_update)select)declarative_base)IntegrityError)text)MySQLConnectorc                   @   s   e Zd ZdZdd Zejdd Zdd Zdd	 Z	d
d Z
						d%dedededededefddZdd Zd&dedefddZd'defddZdedefdd Zd!d" Zd&d#d$ZdS )(MySQLDBz8Class to manage MySQL database connection and operationsc                 C   s   t  | _d S N)r	   	connectorself r   */var/www/html/XCapMarket/utils/mysql_db.py__init__   s   zMySQLDB.__init__c              	   C  sL   | j  4 I dH }|V  W d  I dH  dS 1 I dH sw   Y  dS )zReturn a session properlyN)r   get_session)r   sessionr   r   r   r      s   .zMySQLDB.get_sessionc              	      s\   | j j 4 I dH }|tjjI dH  W d  I dH  dS 1 I dH s'w   Y  dS )z)Initialize the database and create tablesN)r   enginebeginrun_syncBasemetadata
create_all)r   connr   r   r   init_db    s   .zMySQLDB.init_dbc                    s   | j  I dH  dS )zClose the database connectionN)r   closer   r   r   r   r   %   s   zMySQLDB.closec                    s   |   I dH  dS )z?Ensure the database connection is closed on object destruction.N)r   r   r   r   r   
__aexit___-   s   zMySQLDB.__aexit___NFfiltersorder_byoffsetgroup_bylimitfirstc              	      s   |   4 I dH \}t|}	|r|D ]}
|	|
}	q|r&|D ]}|	|}	q|r-|	|}	|r4|	|}	|r;|	|}	||	I dH }|rU| 	 W  d  I dH  S | 
 W  d  I dH  S 1 I dH skw   Y  dS )aV  
        Execute a query with dynamic filters, ordering, and limit.

        :param model: The SQLAlchemy model to query.
        :param filters: A list of filter conditions.
        :param order_by: A list of order_by conditions.
        :param limit: An optional limit for the number of results.
        :return: The query results.
        N)r   r   wherer   r"   r    r!   executescalarsr#   all)r   modelr   r   r    r!   r"   r#   r   query	conditionorderresultr   r   r   execute_query2   s*   




0zMySQLDB.execute_queryc                    s   |   4 I dH F}z!|| | I dH  ||I dH  |W W  d  I dH  S  ty> } zt|d|jd}~w tyN } zt|dd}~ww 1 I dH sUw   Y  dS )z{
        Insert a new record into the database.

        :param instance: The SQLAlchemy model instance to insert.
        NDuplicate entry detected.z%Failed to insert buyer intake record.)r   addcommitrefreshr   code
ValueError)r   instancer   er   r   r   execute_insert`   s    

zMySQLDB.execute_insert	record_idc              	      s   |   4 I dH 4}|rt| }t||}n
t||j|k}||I dH  | I dH  |W  d  I dH  S 1 I dH sCw   Y  dS )z
        Delete a record from the database.

        :param model: The SQLAlchemy model class to delete from.
        :param record_id: The ID of the record to delete.
        N)r   r   r   r$   idr%   r0   )r   r(   r7   r   r   combined_filtersr)   r   r   r   delete_recordq   s   0zMySQLDB.delete_recordr8   c              	      sv   |   4 I dH %}t|t|||k}||I dH }|  W  d  I dH  S 1 I dH s4w   Y  dS )z1
        Get a record from the database.
        N)r   r   r$   getattrr%   r&   r#   )r   r(   r7   primary_key_columnr   r)   r,   r   r   r   
get_record   s   
0zMySQLDB.get_recordvaluesc           	   	      s   |   4 I dH H}t|}|rt| }||}|jdi |}||I dH  | I dH  t|t| }||I dH }| 	 W  d  I dH  S 1 I dH sWw   Y  dS )a  
        Update a record in the database.

        :param model: The SQLAlchemy model class to update.
        :param filters: A list of filter conditions to identify the records to update.
        :param values: A dictionary of column-value pairs to set in the update.
        Nr   )
r   r   r   r$   r>   r%   r0   r   r&   r'   )	r   r(   r   r>   r   r)   r9   select_queryr,   r   r   r   update_record   s   

0zMySQLDB.update_recordc                    s   |   4 I dH \}z|| | I dH  W nA ty8 } zddiW  Y d}~W  d  I dH  S d}~w tyY } zdt|iW  Y d}~W  d  I dH  S d}~ww W d  I dH  dS 1 I dH skw   Y  dS )z
        Insert multiple records into the database in bulk.

        :param instances: A list of SQLAlchemy model instances to insert.
        Nerrorr.   )r   add_allr0   r   r3   str)r   	instancesr   r5   r   r   r   execute_bulk_insert   s    
.zMySQLDB.execute_bulk_insertc              	      sn   |   4 I dH !}|t||I dH }| I dH  | W  d  I dH  S 1 I dH s0w   Y  dS )z
        Execute a raw SQL query with optional values.

        :param query: The raw SQL query to execute.
        :param values: Optional values to use with the query.
        :return: The result of the query execution.
        N)r   r%   r   r0   fetchall)r   r)   r>   r   r,   r   r   r   r%      s   0zMySQLDB.execute)NNNNNFr   )r8   )__name__
__module____qualname____doc__r   
contextlibasynccontextmanagerr   r   r   r   listintboolr-   r6   r:   r=   dictr@   rE   r%   r   r   r   r   r
      sB    

.	r
   __main__c                     s(   t  } |  I d H  |  I d H  d S r   )r
   r   r   )dbr   r   r   main   s   rS   )asynciorK   os
sqlalchemyr   r   r   sqlalchemy.futurer   sqlalchemy.ormr   sqlalchemy.excr   sqlalchemy.sqlr   utils.mysql_connectorr	   r   r
   mysql_dbrG   rS   runr   r   r   r   <module>   s"     3