Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- none

# v2026.4.1-beta
- sqlmath-python - Add context manager and Pythonic API (execute, close) to SqlmathDb matching sqlite3 conventions.
- sqlmath-python - Add __repr__ and __bool__ methods to SqlmathDb for better debugging experience.
- sqlmath - Removue under-used custom-files sqlmath_custom.c, sqlmath_custom.mjs.
- github-ci - Pin various github-runner-os to stable/lts version.
Expand Down
28 changes: 25 additions & 3 deletions sqlmath/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,36 @@ class SqlmathDb:
filename = ""
ptr = 0

def __bool__(self):
"""Return True if database is open."""
return not self.closed

def __enter__(self):
"""Enter context manager."""
return self

def __exit__(self, exc_type, exc_val, exc_tb):
"""Exit context manager and close database."""
db_close(self)
return False

def close(self):
"""Close database connection. Alias for db_close(self)."""
db_close(self)

def __repr__(self):
"""Return string representation for debugging."""
state = "closed" if self.closed else "open"
return f"SqlmathDb({self.filename!r}, {state})"

def __bool__(self):
"""Return True if database is open."""
return not self.closed
def execute(self, sql, bind_list=None, response_type=None):
"""Execute SQL statement. Alias for db_exec(db=self, ...)."""
return db_exec(
db=self,
sql=sql,
bind_list=bind_list,
response_type=response_type,
)


class SqlmathError(Exception):
Expand Down