
    Wh^                     <    d Z ddlZddlZddlmZ ddlmZ d Zd Zy)a  
db.py

Purpose:
  Encapsulate SQLite connection lifecycle, providing `get_db()` for obtaining a
  request-scoped connection and `register_teardown(app)` to close it after the
  request. This mirrors Flask's application context pattern and keeps DB logic
  out of `app.py`.

Exports:
  - get_db(): returns a sqlite3.Row-based connection stored on flask.g
  - register_teardown(app): registers a teardown handler to close the DB
    N)g   )ROOT_DIRc                     t        t        dd       } | t        j                  d      xs$ t        j                  j                  t        d      }t        j                  |      } t        j                  | _
        	 | j                  d       | j                  d       | j                  d       | t        _        | S # t        $ r Y w xY w)N	_databaseDATABASE_PATHztheialogin.dbzPRAGMA journal_mode=WALzPRAGMA synchronous=NORMALzPRAGMA foreign_keys=ON)getattrr   osgetenvpathjoinr   sqlite3connectRowrow_factoryexecute	Exceptionr   )dbdb_paths     Ac:\Users\algun\Documents\ceba web\Ceba - Github\app_modules\db.pyget_dbr      s    	K	&B	z))O,WX0W__W% 	JJ01JJ23JJ/0 I  		s   :3B: :	CCc                 (    | j                   d        }y )Nc                 L    t        t        dd       }||j                          y y )Nr   )r	   r   close)	exceptionr   s     r   close_connectionz+register_teardown.<locals>.close_connection(   s#    QT*>HHJ     )teardown_appcontext)appr   s     r   register_teardownr    '   s     r   )	__doc__r   r
   flaskr   pathsr   r   r     r   r   <module>r%      s"     	  $r   