Skip to main content

A simple, lightweight DB connector that allows queries and interactions through raw SQL

Project description

SQLCore

SQLCore is a lightweight Python package for managing both synchronous and asynchronous database connections, executing raw SQL queries, stored procedures, and table-valued functions, with optional connection pooling.

Features

  • Synchronous and Asynchronous Support: Provides separate classes for handling sync (DatabaseConnector) and async (AsyncDatabaseConnector) database interactions.
  • Connection Pooling: Supports optional connection pooling with user-defined limits.
  • Raw SQL Execution: Execute raw SQL queries, stored procedures, and table-valued functions (TVF) easily.
  • Lightweight: Designed to be minimal and focus on direct interactions with the database.

Installation

You can install the package from source or add it to your project:

pip install sqlcore

Usage

Synchronous Database Connection (DatabaseConnector) The DatabaseConnector class handles synchronous database operations. It supports connection pooling, executing raw SQL queries, stored procedures, and fetching results from table-valued functions.

from sqlcore.connector import DatabaseConnector

# Initialize the DatabaseConnector
db = DatabaseConnector(conn_string="DRIVER={SQL Server};SERVER=your_server;DATABASE=your_db;", pool_limit=5)

# Synchronously execute a query
result = db.execute_query("SELECT * FROM users WHERE age > ?", (30,))
print(result)

# Execute a stored procedure
db.execute_stored_procedure("MyStoredProcedure", "param1", 123)

# Execute a stored procedure and return the result
sp_result = db.execute_and_return_stored_procedure("GetUserInfo", 123)
print(sp_result)

# Execute a table-valued function and fetch results
tvf_result = db.execute_tvf_and_fetch_results("MyTVF", "param1")
print(tvf_result)

# Close the connection pool when finished
db.close()

Asynchronous Database Connection (AsyncDatabaseConnector)

The AsyncDatabaseConnector class handles asynchronous database operations, designed for use in asynchronous applications (e.g., web apps, APIs). Like the sync class, it supports connection pooling, raw SQL execution, and stored procedures.

import asyncio
from sqlcore.async_connector import AsyncDatabaseConnector

async def run_async_queries():
    # Initialize the AsyncDatabaseConnector
    db = AsyncDatabaseConnector(conn_string="DRIVER={SQL Server};SERVER=your_server;DATABASE=your_db;", pool_limit=5)

    # Asynchronously execute a query
    result = await db.async_execute_query("SELECT * FROM users WHERE age > ?", (30,))
    print(result)

    # Asynchronously execute a stored procedure
    await db.async_execute_stored_procedure("MyStoredProcedure", "param1", 123)

    # Asynchronously execute a stored procedure and return the result
    sp_result = await db.async_execute_and_return_stored_procedure("GetUserInfo", 123)
    print(sp_result)

    # Asynchronously execute a table-valued function and fetch results
    tvf_result = await db.async_execute_tvf_and_fetch_results("MyTVF", "param1")
    print(tvf_result)

    # Close the database connection pool when finished
    await db.close()

# Run the async operations
asyncio.run(run_async_queries())

Connection Pooling

You can control the number of database connections used by setting the pool_limit parameter:

Limited Pool: Use a fixed number of connections, e.g., pool_limit=5. Unlimited Pool: To allow an unlimited number of connections (i.e., one connection per query), set pool_limit=None.

# Create a connection pool with a limit of 5 connections
db = DatabaseConnector(conn_string="...", pool_limit=5)

# Create an unlimited pool (one connection per query)
db_unlimited = DatabaseConnector(conn_string="...", pool_limit=None)

Error Handling

Both DatabaseConnector and AsyncDatabaseConnector include basic error handling for SQL execution. Errors encountered during query execution, stored procedures, or TVF executions are caught and printed, allowing for easier debugging.

Testing

Unit tests for both synchronous and asynchronous operations can be added under the tests/ directory. Here's an example of how you might write a test for the synchronous connector:

import unittest
from sqlcore.connector import DatabaseConnector

class TestDatabaseConnector(unittest.TestCase):

    def test_execute_query(self):
        db = DatabaseConnector(conn_string="DRIVER={SQL Server};SERVER=test_server;DATABASE=test_db;")
        result = db.execute_query("SELECT 1")
        self.assertEqual(result, [{'1': 1}])

if __name__ == '__main__':
    unittest.main()

To run the tests:

python -m unittest discover tests

Contributing

Contributions to SQLCore are welcome! Please feel free to submit a pull request or open an issue if you encounter a bug or have a feature request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

sqlcore-0.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

sqlcore-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlcore-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for sqlcore-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ac08e754395b7580de7daf871aff285e9152038a8ce567decde20fd04c567c4c
MD5 b2deead354ae930c2db46f539244e723
BLAKE2b-256 ea1cadfc0a18612f7be3f24950f126c35ac7bc04f49469ed4bd8b2240ccf6007

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlcore-0.1.0.tar.gz:

Publisher: publish.yml on aldo-g/SQLCore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: sqlcore-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for sqlcore-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6815c8ab6491182ebce47bd248d56ad9fbf9ceb273fd23e6e901762969e3b63
MD5 b055d51caf3d43a2872f32e594363b9a
BLAKE2b-256 9460b172bb4b6ce69de8e544cb33c51c81319c149091c83cb977a913e0d19bdd

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlcore-0.1.0-py3-none-any.whl:

Publisher: publish.yml on aldo-g/SQLCore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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