Skip to main content

Um módulo genérico para gerenciar conexões com banco de dados

Project description

Connection DB

Uma biblioteca Python flexível e fácil de usar para gerenciar conexões com diferentes tipos de bancos de dados usando SQLAlchemy.

Características

  • Suporte para múltiplos bancos de dados:
    • SQL Server
    • MySQL
    • PostgreSQL
    • MongoDB (em breve)
  • Gerenciamento automático de conexões usando context manager
  • Integração com SQLAlchemy ORM
  • Tratamento de erros robusto
  • Interface consistente para diferentes bancos de dados

Instalação

pip install connection-db

Requisitos

  • Python >= 3.7
  • SQLAlchemy >= 1.4.0
  • Drivers específicos para cada banco de dados:
    • SQL Server: pymssql >= 2.2.0
    • PostgreSQL: psycopg2-binary >= 2.9.0
    • MySQL: pymysql >= 1.0.0

Uso Básico

Conexão Simples

from connection_db import DBConnection

# Usando com SQL Server
with DBConnection(
    sql_server=True,
    connection_string="mssql+pymssql://user:password@server/database"
) as db:
    db.session.execute(text("SELECT * FROM users"))
    db.session.commit()

Uso com ORM

from connection_db import DBConnection
from sqlalchemy import select
from your_models import User

with DBConnection(
    sql_server=True,
    connection_string="mssql+pymssql://user:password@server/database",
    orm_mapped=User
) as db:
    query = select(db.table).where(db.table.name == "John")
    result = db.session.execute(query)
    db.session.commit()

Padrão Repository

from connection_db import DBConnection

class BaseRepository:
    def __init__(self):
        self.connection_string = "mssql+pymssql://user:password@server/database"
        self.db_type = "sql_server"

    def get_connection(self, orm_model=None):
        return DBConnection(
            connection_string=self.connection_string,
            sql_server=(self.db_type == "sql_server"),
            orm_mapped=orm_model
        )

Strings de Conexão

SQL Server

mssql+pymssql://username:password@hostname:port/database_name
mssql+pyodbc://username:password@hostname:port/database_name?driver=ODBC+Driver+17+for+SQL+Server

MySQL

mysql+pymysql://username:password@hostname:port/database_name
mysql://username:password@hostname:port/database_name

PostgreSQL

postgresql://username:password@hostname:port/database_name
postgresql+psycopg2://username:password@hostname:port/database_name

Tratamento de Erros

A biblioteca inclui classes de exceção personalizadas para melhor tratamento de erros:

  • DatabaseConnectionError: Erros de conexão com o banco
  • SessionError: Problemas com a sessão do banco

Contribuindo

Contribuições são bem-vindas! Por favor, sinta-se à vontade para submeter pull requests.

Licença

Este projeto está licenciado sob a Licença MIT - veja o arquivo LICENSE para detalhes.

Autor

Miguel Tenorio

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

connection_db-0.1.1.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

connection_db-0.1.1-py3-none-any.whl (2.4 kB view details)

Uploaded Python 3

File details

Details for the file connection_db-0.1.1.tar.gz.

File metadata

  • Download URL: connection_db-0.1.1.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for connection_db-0.1.1.tar.gz
Algorithm Hash digest
SHA256 78f72763001e411b02a188647993c5ca4b8c28420b91f8eaefa1d8ff13021806
MD5 8ecb1395a4708b4e4cee8baec5ecd8f9
BLAKE2b-256 2c4fd037d32935254695f62c9e322386c6a90bd20a195ab2af493bee77ccab7a

See more details on using hashes here.

File details

Details for the file connection_db-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: connection_db-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 2.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for connection_db-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3c79207299b10cbd8fd61f59c28b429560c24bc303f5d88f3147f0f0ac858c72
MD5 74f1d29e66c5145bc970ccd6518d7ee5
BLAKE2b-256 227fa21b1426b41447461d8fc6189027053bf71a5faf337544b222dd5f1cda18

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