o
    i#                     @   s   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ dd	 Zd
d Zdd ZdddZdd Zdd ZdS )    N)copy)Dialectcreate_engine)make_url)OperationalErrorProgrammingError)object_session)UnmappedInstanceErrorc                 C   sN   t | dr	| j}nzt| j}W n ty   | }Y nw t |ds%td|S )a'  
    Return the bind for given SQLAlchemy Engine / Connection / declarative
    model object.
    :param obj: SQLAlchemy Engine / Connection / declarative model object
    ::
        from sqlalchemy_utils import get_bind
        get_bind(session)  # Connection object
        get_bind(user)
    bindexecutezSThis method accepts only Session, Engine, Connection and declarative model objects.)hasattrr
   r   r	   	TypeError)objconn r   [/var/www/html/LicenciasMIG/venv/lib/python3.10/site-packages/spyne/store/relational/util.pyget_bind.   s   


r   c                 C   s*   t | tr| }nt| j}|||S )a  
    Conditionally quote an identifier.
    ::
        from sqlalchemy_utils import quote
        engine = create_engine('sqlite:///:memory:')
        quote(engine, 'order')
        # '"order"'
        quote(engine, 'some_other_identifier')
        # 'some_other_identifier'
    :param mixed: SQLAlchemy Session / Connection / Engine / Dialect object.
    :param ident: identifier to conditionally quote
    )
isinstancer   r   dialectpreparerquote)mixedidentr   r   r   r   r   H   s   

r   c              	   C   s   t t| } | j}| jdrd| _n	| jdsd| _t| }|jjdkr3d| }t|	|
 S |jjdkrFd| }t|	|
 S |jjdkrV|dkpUtj|S d	}z|| _t| }|	| W d
S  ttfys   Y dS w )a  Check if a database exists.

    :param url: A SQLAlchemy engine URL.

    Performs backend-specific testing to quickly determine if a database
    exists on the server. ::

        database_exists('postgresql://postgres@localhost/name')  #=> False
        create_database('postgresql://postgres@localhost/name')
        database_exists('postgresql://postgres@localhost/name')  #=> True

    Supports checking against a constructed URL as well. ::

        engine = create_engine('postgresql://postgres@localhost/name')
        database_exists(engine.url)  #=> False
        create_database(engine.url)
        database_exists(engine.url)  #=> True

    postgressqliteN
postgresqlz,SELECT 1 FROM pg_database WHERE datname='%s'mysqlzLSELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '%s':memory:zSELECT 1TF)r   r   database
drivername
startswithr   r   nameboolr   scalarospathexistsr   r   )urlr   enginetextr   r   r   database_exists\   s4   
r*   utf8	template1c                 C   s  t t| } | j}| jdrd| _n	| jdsd| _t| }|jjdkrL|jdkr8ddl	m
} | | dt|||t||}|| dS |jjd	krbd
t|||}|| dS |jjdkru|dkrut|d  dS dt||}|| dS )a  Issue the appropriate CREATE DATABASE statement.

    :param url: A SQLAlchemy engine URL.
    :param encoding: The encoding to create the database as.
    :param psql_template:
        The name of the template from which to create the new database,
        only supported by PostgreSQL driver. As per Postgresql docs, defaults to
        "template1".

    To create a database, you can pass a simple URL that would have
    been passed to ``create_engine``. ::

        create_database('postgresql://postgres@localhost/name')

    You may also pass the url from an existing engine. ::

        create_database(engine.url)

    Has full support for mysql, postgres, and sqlite. In theory,
    other database engines should be supported.
    r   r   Nr   psycopg2r   ISOLATION_LEVEL_AUTOCOMMITz/CREATE DATABASE {0} ENCODING '{1}' TEMPLATE {2}r   z)CREATE DATABASE {0} CHARACTER SET = '{1}'r   wzCREATE DATABASE {0})r   r   r   r   r    r   r   r!   driverpsycopg2.extensionsr/   raw_connectionset_isolation_levelformatr   r   openclose)r'   encodingpsql_templater   r(   r/   r)   r   r   r   create_database   s:   
r:   c                 C   s,  t t| } | j}| jdrd| _n	| jdsd| _t| }|jjdkr3| jdkr3t	| j dS |jjdkr|j
dkrddlm} | | ttt|d	 d d
}|d dkrj|d dkrjdnd}d||d }|| dt||}|| dS dt||}|| dS )a8  Issue the appropriate DROP DATABASE statement.

    :param url: A SQLAlchemy engine URL.

    Works similar to the :ref:`create_database` method in that both url text
    and a constructed url are accepted. ::

        drop_database('postgresql://postgres@localhost/name')
        drop_database(engine.url)

    r   r,   r   Nr   r-   r   r.   zSHOW server_version.	         pidprocpidz
        SELECT pg_terminate_backend(pg_stat_activity.%(pid_column)s)
        FROM pg_stat_activity
        WHERE pg_stat_activity.datname = '%(database)s'
          AND %(pid_column)s <> pg_backend_pid();
        )
pid_columnr   zDROP DATABASE {0})r   r   r   r   r    r   r   r!   r$   remover1   r2   r/   r3   r4   listmapintr   firstsplitr5   r   )r'   r   r(   r/   versionrA   r)   r   r   r   drop_database   s8   
rI   c                    s    fdd}|S )Nc               	      sH   t  }z|   | i |}|  |W |jdd S |jdd w )Ntime)sort)cProfileProfileenabledisableprint_stats)argskwargsprofileresultfuncr   r   profiled_func  s   z"do_cprofile.<locals>.profiled_funcr   )rV   rW   r   rU   r   do_cprofile  s   
rX   )r+   r,   )r$   rL   r   sqlalchemy.enginer   r   sqlalchemy.engine.urlr   sqlalchemy.excr   r   sqlalchemy.ormr   sqlalchemy.orm.excr	   r   r   r*   r:   rI   rX   r   r   r   r   <module>   s   !
6?;