o
    Zh=                     @   sP   d dl mZ d dlmZmZ er"d dlmZ d dlmZ d dlm	Z	 dd Z
dS )	    )TYPE_CHECKING)cache_statementshould_run_explain_plan)Any)Callable)Spanc           
      C   s   |   dsdS t||sdS |ddrdnd}d| |||d }| "}|| d	d
 | D }	| 	d|	 t
|| W d   dS 1 sQw   Y  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 zutf-8c                 S   s   g | ]}|qS  r	   ).0rowr	   r	   X/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/db/explain_plan/django.py
<listcomp>,   s    z/attach_explain_plan_to_span.<locals>.<listcomp>zdb.explain_plan)stripupper
startswithr   getdecodecursorexecuteZfetchallset_datar   )
span
connectionZ	statement
parametersZmogrifyoptionsZanalyzeZexplain_statementr   Zexplain_planr	   r	   r   attach_explain_plan_to_span   s    


"r   N)Zsentry_sdk.constsr   Zsentry_sdk.db.explain_planr   r   typingr   r   Zsentry_sdk.tracingr   r   r	   r	   r	   r   <module>   s    