o
    ªZhx  ã                   @   s„   d dl mZ d dlmZ d dlmZmZ d dlmZ zd dl	m
Z
 W n ey-   edƒ‚w er<d dlmZ d dlmZ d	d
„ ZdS )é    )Úabsolute_import)ÚTYPE_CHECKING)Úcache_statementÚshould_run_explain_plan)ÚDidNotEnable)ÚtextzSQLAlchemy not installed.)ÚAny)ÚSpanc           	      C   s~   |  ¡  ¡  d¡sdS t||ƒsdS | dd¡rdnd}d| | | }| t|ƒ¡}dd	„ |D ƒ}|  d
|¡ t||ƒ dS )aB  
    Run EXPLAIN or EXPLAIN ANALYZE on the given statement and attach the explain plan to the span data.

    Usage:
    ```
    sentry_sdk.init(
        dsn="...",
        _experiments={
            "attach_explain_plans": {
                "explain_cache_size": 1000,  # Run explain plan for the 1000 most run queries
                "explain_cache_timeout_seconds": 60 * 60 * 24,  # Run the explain plan for each statement only every 24 hours
                "use_explain_analyze": True,  # Run "explain analyze" instead of only "explain"
            }
        }
    ```
    ZSELECTNZuse_explain_analyzeFZANALYZEÚ zEXPLAIN %s c                 S   s   g | ]}|‘qS © r   )Ú.0Úrowr   r   ú\/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/db/explain_plan/sqlalchemy.pyÚ
<listcomp>.   s    z/attach_explain_plan_to_span.<locals>.<listcomp>zdb.explain_plan)	ÚstripÚupperÚ
startswithr   ÚgetÚexecuter   Úset_datar   )	ÚspanÚ
connectionZ	statementÚ
parametersÚoptionsZanalyzeZexplain_statementÚresultZexplain_planr   r   r   Úattach_explain_plan_to_span   s   
r   N)Ú
__future__r   Zsentry_sdk.constsr   Zsentry_sdk.db.explain_planr   r   Zsentry_sdk.integrationsr   Zsqlalchemy.sqlr   ÚImportErrorÚtypingr   Zsentry_sdk.tracingr	   r   r   r   r   r   Ú<module>   s    ÿ