connection_models
saConnectionBase
dataclass
Bases: DictMixin
Base class for SQLAlchemy connection models.
Each model will inherit the connection_string propery, which outputs a URL conection object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drivername |
str
|
The SQLAlchemy drivername string |
field(default=None)
|
host |
str
|
The database host name/address |
field(default=None)
|
port |
int
|
The connection port for the database |
field(default=None)
|
username |
str
|
The database username to authenticate as |
field(default=None)
|
password |
str
|
The |
field(default=None, repr=False)
|
database |
str
|
The name of the database to connect to |
field(default=None)
|
Source code in src/red_utils/ext/sqlalchemy_utils/connection_models.py
connection_string: sa.engine.url.URL
property
Return a formatted SQLAlchemy Engine connection URI string.
__post_init__()
Dataclasses does not have inbuilt validation for class variables.
Define validator functions in __post_init__(). Classes that inherit from this
base class will pass their values through these validators as well.
Note
SlingAcademy: How to validate data in dataclass
Source code in src/red_utils/ext/sqlalchemy_utils/connection_models.py
saConnectionGeneric
Bases: saConnectionBase
Generic SQLAlchemy connection class.
Inherits settings & properties from the base class, and can be extended if none of the other saConnection* models meet needs.
Source code in src/red_utils/ext/sqlalchemy_utils/connection_models.py
saMSSQLConnection
dataclass
Bases: saConnectionBase
Default Microsoft SQL Server connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drivername |
str
|
The SQLAlchemy drivername string |
field(default='mssql+pyodbc')
|
host |
str
|
The PostgreSQL database server address/hostname |
field(default='127.0.0.1')
|
port |
int
|
The PostgreSQL database connection port |
field(default=1433)
|
username |
str
|
The PostgreSQL user to authenticate as |
field(default='SA')
|
password |
str
|
The PostgreSQL password associated with |
field(default='1Secure*Password1', repr=False)
|
database |
str
|
The name of the database to connect to |
field(default='master')
|
Source code in src/red_utils/ext/sqlalchemy_utils/connection_models.py
connection_string: sa.engine.url.URL
property
Return a formatted SQLAlchemy Engine connection URI string.
saPGConnection
dataclass
Bases: saConnectionBase
Default Postgres connection. Useful for local testing.
For Postgres databases, the database you specify must exist before creating the initial connection/engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drivername |
str
|
The SQLAlchemy drivername string |
field(default='postgresql+psycopg2')
|
host |
str
|
The PostgreSQL database server address/hostname |
field(default='127.0.0.1')
|
port |
int
|
The PostgreSQL database connection port |
field(default=5432)
|
username |
str
|
The PostgreSQL user to authenticate as |
field(default='postgres')
|
password |
str
|
The PostgreSQL password associated with |
field(default='postgres', repr=False)
|
database |
str
|
The name of the database to connect to |
field(default='postgres')
|
Source code in src/red_utils/ext/sqlalchemy_utils/connection_models.py
saSQLiteConnection
dataclass
Bases: saConnectionBase
Default SQLite connection. Useful for local testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drivername |
str
|
The SQLAlchemy driver string for the database |
field(default='sqlite+pysqlite')
|
database |
str
|
The name/path to the SQLite database. It is recommended to use .sqlite for the file extension, although .db or any other should work fine as well. |
field(default='.db/default_unnamed.sqlite')
|
Pass a value for database to change the name of the SQLite database file.
If you use a path (i.e. db/test.sqlite), you need to create the Path
manually.
Source code in src/red_utils/ext/sqlalchemy_utils/connection_models.py
ensure_path()
Ensure path to self.database exists.
Use Path() to split the directory path
from the filename, and ensure directores in path
exist.