A Python package for Oracle database connections with environment variable support
Project description
niorclconn
A Python package for Oracle database connections with environment variable support and convenient connection management.
Features
- Environment Variable Support: Automatically reads Oracle credentials from environment variables
- Flexible Configuration: Support for both direct parameters and environment variables
- Context Manager Support: Use with
withstatements for automatic connection cleanup - Convenience Functions: Simple functions for quick connection operations
- Type Hints: Full type annotation support for better IDE experience
- Comprehensive Testing: Includes both unit tests and integration test examples
Installation
pip install niorclconn
Or using Poetry:
poetry add niorclconn
Quick Start
Using Environment Variables
Set the following environment variables:
export ORACLE_USER=your_username
export ORACLE_PASSWORD=your_password
export ORACLE_DSN=your_dsn # e.g., "localhost:1521/XE" or TNS name
Then use the package:
from niorclconn import ConnectionManager
# Automatically uses environment variables
manager = ConnectionManager()
connection = manager.open_connection()
# Use the connection
cursor = connection.cursor()
cursor.execute("SELECT 1 FROM DUAL")
result = cursor.fetchone()
print(result)
# Clean up
manager.close_connection()
Using Direct Parameters
from niorclconn import ConnectionManager
manager = ConnectionManager(
user="your_username",
password="your_password",
dsn="localhost:1521/XE"
)
connection = manager.open_connection()
# ... use connection ...
manager.close_connection()
Using Context Manager (Recommended)
from niorclconn import ConnectionManager
with ConnectionManager("user", "password", "dsn") as connection:
cursor = connection.cursor()
cursor.execute("SELECT SYSDATE FROM DUAL")
result = cursor.fetchone()
print(f"Current time: {result[0]}")
# Connection automatically closed
Using Convenience Functions
from niorclconn import open_connection, close_connection
# Quick connection
connection = open_connection("user", "password", "dsn")
# Use connection
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM USER_TABLES")
table_count = cursor.fetchone()[0]
print(f"Number of tables: {table_count}")
# Close connection
close_connection(connection)
Environment Variables
The package supports the following environment variables:
| Variable | Alternative | Description |
|---|---|---|
ORACLE_USER |
ORACLE_USERNAME |
Oracle database username |
ORACLE_PASSWORD |
- | Oracle database password |
ORACLE_DSN |
TNS_ADMIN |
Data Source Name (DSN) or TNS name |
Note: Direct parameters take precedence over environment variables.
Configuration Examples
Easy Connect String
manager = ConnectionManager(dsn="localhost:1521/XE")
TNS Name
manager = ConnectionManager(dsn="ORCL") # Assuming ORCL is in your tnsnames.ora
Full TNS Descriptor
dsn = """(DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))
(CONNECT_DATA=(SID=XE))
)"""
manager = ConnectionManager(dsn=dsn)
Error Handling
The package raises appropriate exceptions for better error handling:
from niorclconn import ConnectionManager
import oracledb
try:
manager = ConnectionManager() # May raise ValueError if credentials missing
connection = manager.open_connection() # May raise oracledb.Error
# Your database operations here
except ValueError as e:
print(f"Configuration error: {e}")
except oracledb.Error as e:
print(f"Database error: {e}")
finally:
if 'manager' in locals():
manager.close_connection()
Testing
Run the test suite:
# Install development dependencies
poetry install
# Run tests
poetry run pytest
# Run tests with coverage
poetry run pytest --cov=niorclconn
Integration Testing
For integration tests with a real Oracle database, update the test credentials in tests/test_connector.py and remove the @pytest.mark.skip decorator.
API Reference
ConnectionManager
class ConnectionManager:
def __init__(self, user=None, password=None, dsn=None):
"""Initialize connection manager with credentials."""
def open_connection(self) -> oracledb.Connection:
"""Open database connection."""
def close_connection(self) -> None:
"""Close database connection."""
@property
def is_connected(self) -> bool:
"""Check if connection is active."""
Convenience Functions
def open_connection(user=None, password=None, dsn=None) -> oracledb.Connection:
"""Open an Oracle database connection."""
def close_connection(connection: oracledb.Connection) -> None:
"""Close an Oracle database connection."""
Requirements
- Python 3.9+
- oracledb >= 2.0.0
License
MIT License - see LICENSE file for details.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
Changelog
0.1.0
- Initial release
- Environment variable support
- Context manager support
- Comprehensive test suite
- Type hints and documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file niorclconn-0.1.0.tar.gz.
File metadata
- Download URL: niorclconn-0.1.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74eaaa513fbf13c25d00eaa32c93ee20dc5fd27992e8f6199e12554cfcfaa246
|
|
| MD5 |
a61802db3d03c1f3548c6b896ea455d7
|
|
| BLAKE2b-256 |
29543608020f6c738b459b5883e8b5e1cc31a18828145e69f527c753428b2df5
|
File details
Details for the file niorclconn-0.1.0-py3-none-any.whl.
File metadata
- Download URL: niorclconn-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5911d5e1ad75c5ae1a6b62b6d43482c2d530a6a6201ab68700948c9102f7eea4
|
|
| MD5 |
4d2d4ec16e444ce7308ed9dc7f93f569
|
|
| BLAKE2b-256 |
c8dc3a32da045b00532ca313699009cfc18961dbbfaf2b2c61d4366f0fded2d6
|