v1a0 / sqllex

The most pythonic ORM (for SQLite and PostgreSQL). Seriously, try it out!
https://v1a0.github.io/sqllex
GNU General Public License v3.0
92 stars 8 forks source link

FEATURE | "With-as" statement #55

Open v1a0 opened 2 years ago

v1a0 commented 2 years ago

Add __enter__ and __exit__ methods for db, table and column classes to make possible nice temporary connection.

from sqllex import SQLite3x

with SQLite3x('/path/database.db') as db:
    # creting connection
    db.insert(...)
    ...
    # commit and disconnection

with SQLite3x('/path/database.db').connection as conn:
    # creting connection
    # doing things (idk)
    ...
    # commit and disconnection

with SQLite3x('/path/database.db')['table'] as table:
    # creting connection
    table.insert(...)
    table.select(...)
    # commit and disconnection

with SQLite3x('/path/database.db')['table']['column'] as column:
    # creting connection
    column.find(...)
    # commit and disconnection (if necessary)

P.S.: For developers, stackoverflow "Implementing use of 'with object as o' in custom class in python"

v1a0 commented 2 years ago

After a few tries I'm just not sure is this really so necessary and what is the bes way to code it 😅.