o
    Af                     @   sl   d Z ddlZddlZddlZedZde_G dd deZG dd de	Z
G d	d
 d
eZejZejZdS )z
	A lightweight wrapper around psycopg2.

	Originally part of the Tornado framework.  The tornado.database module
	is slated for removal in Tornado 3.0, and it is now available separately
	as torndb.
    Nzlocation.database   c                   @   s   e Zd ZdZd#ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" ZdS )$
Connectiona5  
		A lightweight wrapper around MySQLdb DB-API connections.

		The main value we provide is wrapping rows in a dict/object so that
		columns can be accessed by name. Typical usage::

			db = torndb.Connection("localhost", "mydatabase")
			for article in db.query("SELECT * FROM articles"):
				print article.title

		Cursors are hidden by the implementation, but other than that, the methods
		are very similar to the DB-API.

		We explicitly set the timezone to UTC and the character encoding to
		UTF-8 on all connections to avoid time zone and encoding errors.
	Nc                 C   sZ   || _ || _d | _||||dd| _z|   W d S  ty,   tjd| j dd Y d S w )NZrequire)hostdatabaseuserpasswordZsslmodez Cannot connect to database on %sT)exc_info)r   r   _db_db_args	reconnect	Exceptionlogerror)selfr   r   r   r    r   6/usr/lib/python3.10/site-packages/location/database.py__init__!   s   zConnection.__init__c                 C   s   |    d S N)closer   r   r   r   __del__3   s   zConnection.__del__c                 C   s(   t | dddur| j  d| _dS dS )z'
			Closes this database connection.
		r	   N)getattrr	   r   r   r   r   r   r   6   s   

zConnection.closec                 C   s2   |    tjdi | j| _d| j_| d dS )z?
			Closes the existing database connection and re-opens it.
		TzSET TIMEZONE TO 'UTC'Nr   )r   psycopg2Zconnectr
   r	   Z
autocommitexecuter   r   r   r   r   >   s   zConnection.reconnectc                    sP   |   }z| |||| dd |jD   fdd|D W |  S |  w )z=
			Returns a row list for the given query and parameters.
		c                 S   s   g | ]}|d  qS )r   r   ).0dr   r   r   
<listcomp>Q   s    z$Connection.query.<locals>.<listcomp>c                    s   g | ]	}t t |qS r   )Rowzip)r   rowZcolumn_namesr   r   r   R   s    )_cursor_executedescriptionr   r   query
parameterskwparameterscursorr   r    r   r%   J   s   zConnection.queryc                 O   s<   | j |g|R i |}|sdS t|dkrtd|d S )z:
			Returns the first row returned for the given query.
		Nr   z/Multiple rows returned for Database.get() queryr   )r%   lenr   )r   r%   r&   r'   Zrowsr   r   r   getV   s   zConnection.getc                 O   s   | j |g|R i |S zH
			Executes the given query, returning the lastrowid from the query.
		)execute_lastrowid)r   r%   r&   r'   r   r   r   r   b   s   zConnection.executec                 O   4   |   }z| |||| |jW |  S |  w r+   )r!   r"   	lastrowidr   r$   r   r   r   r,   h   
   zConnection.execute_lastrowidc                 O   r-   )zG
			Executes the given query, returning the rowcount from the query.
		)r!   r"   rowcountr   r$   r   r   r   execute_rowcounts   r/   zConnection.execute_rowcountc                 C   s   |  ||S zr
			Executes the given query against all the given param sequences.

			We return the lastrowid from the query.
		)executemany_lastrowid)r   r%   r&   r   r   r   executemany~   s   zConnection.executemanyc                 C   0   |   }z||| |jW |  S |  w r2   )r!   r4   r.   r   r   r%   r&   r(   r   r   r   r3      s
   z Connection.executemany_lastrowidc                 C   r5   )zq
			Executes the given query against all the given param sequences.

			We return the rowcount from the query.
		)r!   r4   r0   r   r6   r   r   r   executemany_rowcount   s
   zConnection.executemany_rowcountc                 C   s$   | j d u rtd |   d S d S )NzDatabase connection was lost...)r	   r   Zwarningr   r   r   r   r   _ensure_connected   s   

zConnection._ensure_connectedc                 C   s   |    | j S r   )r8   r	   r(   r   r   r   r   r!      s   
zConnection._cursorc                 C   s   t d|||p	|   t }z-z|||p|W W t | }t d|d   S  ty@   t d| j	 | 
   w t | }t d|d   w )NzExecuting query: %szQuery took %.2fmsg     @@z"Error connecting to database on %s)r   debugZmogrifydecodetime	monotonicr   OperationalErrorr   r   r   )r   r(   r%   r&   r'   tr   r   r   r   r"      s"   zConnection._executec                 C   s   t | S r   )Transactionr   r   r   r   transaction   s   zConnection.transaction)NN)__name__
__module____qualname____doc__r   r   r   r   r%   r*   r   r,   r1   r4   r3   r7   r8   r!   r"   r@   r   r   r   r   r      s$    
r   c                   @   s   e Zd ZdZdd ZdS )r   z:A dict that allows for object-like property access syntax.c                 C   s"   z| | W S  t y   t|w r   )KeyErrorAttributeError)r   namer   r   r   __getattr__   s
   
zRow.__getattr__N)rA   rB   rC   rD   rH   r   r   r   r   r      s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r?   c                 C   s   || _ | j d d S )NzSTART TRANSACTIONdbr   )r   rJ   r   r   r   r      s   zTransaction.__init__c                 C   s   | S r   r   r   r   r   r   	__enter__   s   zTransaction.__enter__c                 C   s(   |d ur| j d d S | j d d S )NZROLLBACKZCOMMITrI   )r   exctypeZexcvalue	tracebackr   r   r   __exit__   s   zTransaction.__exit__N)rA   rB   rC   r   rK   rN   r   r   r   r   r?      s    r?   )rD   Zloggingr   r;   Z	getLoggerr   Z	propagateobjectr   dictr   r?   ZIntegrityErrorr=   r   r   r   r   <module>   s    
 9	
