Skip to main content

Backend-agnostic stored-procedure / SQL executor with a pluggable base class

Project description

sql-executor (v1, 0.1.0)

Backend-agnostic stored-procedure / SQL executor. BaseSqlExecutor owns connection lifecycle, commit/rollback, fetch-to-dict, chunked streaming, and error wrapping. A new backend only implements two methods.

Usage (SQL Server)

from sql_executor import SqlServerExecutor, DatabaseError

db = SqlServerExecutor(connstring="DRIVER={ODBC Driver 17 for SQL Server};...")

# Stored procedure
rows = db.call_procedure("dbo.GetIncidents", params=("2026-06-01",), fetch=True)

# Raw SQL
db.execute("UPDATE dbo.Logs SET status = ? WHERE id = ?", params=("done", 42))

# Stream large result sets in chunks instead of loading everything into memory
for row in db.stream_procedure("dbo.GetBigReport", chunk_size=1000):
    process(row)

try:
    db.execute("DELETE FROM dbo.Logs WHERE id = ?", params=(42,))
except DatabaseError:
    # Always raised on failure — never swallowed as None/False.
    raise

Adding a new backend (e.g. Postgres, Oracle)

Subclass BaseSqlExecutor and implement two methods:

import psycopg2
from sql_executor import BaseSqlExecutor

class PostgresExecutor(BaseSqlExecutor):
    driver_error = psycopg2.Error

    def __init__(self, dsn: str):
        self.dsn = dsn

    def _connect(self):
        return psycopg2.connect(self.dsn)

    def _call_procedure_sql(self, sp_name, params):
        placeholders = ",".join("%s" for _ in params)
        return f"CALL {sp_name}({placeholders})"

call_procedure, execute, stream, and stream_procedure all come for free from the base class.

Testing

FakeExecutor is a drop-in for SqlServerExecutor with no real DB:

from sql_executor import FakeExecutor

fake = FakeExecutor()
fake.register("dbo.GetIncidents", lambda since: [{"id": 1, "since": since}])
fake.call_procedure("dbo.GetIncidents", params=("2026-06-01",), fetch=True)

Offline install (corporate VM, no internet)

Build the wheel on a machine with internet access, then copy the .whl to the VM:

pip install --no-index --find-links=. sql_executor-0.1.0-py3-none-any.whl
pip install pyodbc  # only needed if you use SqlServerExecutor, also offline-installed

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysql_executor-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysql_executor-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file pysql_executor-0.1.0.tar.gz.

File metadata

  • Download URL: pysql_executor-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pysql_executor-0.1.0.tar.gz
Algorithm Hash digest
SHA256 34e0c5799b4ef433df5396f9b62d558836ad66dea646dad45c70d7cea8607582
MD5 35aefe3ae7af48eddac1567ab3f1c233
BLAKE2b-256 c5a2f76ccbdab9fd55334f52a34614c886e4e54eaedea61d2ac1c1a458ca134f

See more details on using hashes here.

File details

Details for the file pysql_executor-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pysql_executor-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pysql_executor-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 18f2327bc0274c9f43d0ee4edf890d513bbbf2a64a2443b91f1164fab91aafd0
MD5 87556276215b0adc959ca13ad50796c7
BLAKE2b-256 bd235dd8e20972f06292b341cb9143e68cbd6e8359d4280929f93a63e40a97b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page