Skip to content

feat(python): add Pythonic API to SqlmathDb#8

Merged
kaizhu256 merged 3 commits into
sqlmath:betafrom
ababber:feature/pythonic-api
May 1, 2026
Merged

feat(python): add Pythonic API to SqlmathDb#8
kaizhu256 merged 3 commits into
sqlmath:betafrom
ababber:feature/pythonic-api

Conversation

@ababber
Copy link
Copy Markdown
Contributor

@ababber ababber commented Apr 24, 2026

Summary

Adds context manager and method-style API to SqlmathDb, matching sqlite3 stdlib conventions.

Usage

from sqlmath import db_open

with db_open(':memory:') as db:
    db.execute('CREATE TABLE t(x)')
    db.execute('INSERT INTO t VALUES(?)', bind_list=[42])
    result = db.execute('SELECT * FROM t')
# db automatically closed

Methods added

Method Description
__enter__ Context manager entry (returns self)
__exit__ Context manager exit (calls db_close)
execute(sql, bind_list=None, response_type=None) Alias for db_exec(db=self, ...)
close() Alias for db_close(self)

Rationale

Follows sqlite3.Connection stdlib conventions:

# sqlite3 stdlib
with sqlite3.connect(':memory:') as conn:
    conn.execute('SELECT 1')

# sqlmath (with this PR)
with db_open(':memory:') as db:
    db.execute('SELECT 1')

Module functions db_exec() and db_close() remain available. No breaking changes.

Made with Cursor

Adds context manager and method-style API, matching sqlite3 stdlib:

    with db_open(':memory:') as db:
        db.execute('CREATE TABLE t(x)')
        db.execute('INSERT INTO t VALUES(?)', bind_list=[42])
        result = db.execute('SELECT * FROM t')
    # db automatically closed

Methods added:
- __enter__ / __exit__ (context manager)
- execute(sql, bind_list=None, response_type=None)
- close()

Module functions db_exec() and db_close() remain available.
No breaking changes.

Made-with: Cursor
@kaizhu256
Copy link
Copy Markdown
Contributor

@kaizhu256 kaizhu256 merged commit d3368b8 into sqlmath:beta May 1, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants