Database¶
SQLAlchemy-based CRUD helpers for persisting bus types, branch types and
entire networks to a SQLite database. The ORM mirror classes live next to the
Pydantic models in groundinsight.models.database_models and expose
from_pydantic / to_pydantic converters.
CRUD functions¶
crud ¶
CRUD Operations Module.
This module provides functions for creating, reading, updating, and deleting (CRUD) entities in the GroundInsight database using SQLAlchemy sessions. It facilitates the management of core electrical network components such as BusTypes, BranchTypes, Networks, Buses, Branches, Faults, Sources, and Paths. The functions convert between Pydantic models and SQLAlchemy database models to ensure seamless data manipulation and persistence.
load_branchtypes ¶
Load all BranchTypes from the database.
This function retrieves all BranchType entries from the database and converts them into a dictionary mapping BranchType names to their corresponding Pydantic models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The SQLAlchemy session used for database operations. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, BranchType]
|
Dict[str, BranchType]: A dictionary where keys are BranchType names and values are BranchType instances. |
Source code in src/groundinsight/database/crud.py
load_bustypes ¶
Load all BusTypes from the database.
This function retrieves all BusType entries from the database and converts them into a dictionary mapping BusType names to their corresponding Pydantic models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The SQLAlchemy session used for database operations. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, BusType]
|
Dict[str, BusType]: A dictionary where keys are BusType names and values are BusType instances. |
Source code in src/groundinsight/database/crud.py
load_network ¶
Load a Network from the database.
This function retrieves a Network instance by its name from the database and converts it
into a Pydantic Network model. It ensures that all related entities such as Buses, Branches,
Faults, Sources, and Paths are properly associated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the network to load. |
required |
session
|
Session
|
The SQLAlchemy session used for database operations. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Network |
Network
|
The loaded |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the specified network does not exist in the database. |
Source code in src/groundinsight/database/crud.py
save_branchtype ¶
Save a BranchType to the database.
This function converts a Pydantic BranchType model to its corresponding SQLAlchemy
BranchTypeDB model and saves it to the database. If a BranchType with the same name
already exists, it will be updated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
branch_type
|
BranchType
|
The BranchType instance to be saved. |
required |
session
|
Session
|
The SQLAlchemy session used for database operations. |
required |
Raises:
| Type | Description |
|---|---|
Exception
|
If there is an error during the database commit. |
Source code in src/groundinsight/database/crud.py
save_bustype ¶
Save a BusType to the database.
This function converts a Pydantic BusType model to its corresponding SQLAlchemy
BusTypeDB model and saves it to the database. If a BusType with the same name
already exists, it will be updated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bus_type
|
BusType
|
The BusType instance to be saved. |
required |
session
|
Session
|
The SQLAlchemy session used for database operations. |
required |
Raises:
| Type | Description |
|---|---|
Exception
|
If there is an error during the database commit. |
Source code in src/groundinsight/database/crud.py
save_network ¶
Save a Network to the database.
This function saves a comprehensive Network instance to the database, including all
associated BusTypes, BranchTypes, Buses, Branches, Faults, Sources, and Paths. It handles
the creation or updating of related entities and ensures referential integrity. If overwrite
is set to True, an existing network with the same name will be deleted and replaced.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
The Network instance to be saved. |
required |
session
|
Session
|
The SQLAlchemy session used for database operations. |
required |
overwrite
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the network already exists and |
Exception
|
If there is an error during the database commit. |
Source code in src/groundinsight/database/crud.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
ORM models¶
database_models ¶
Database Models Module.
This module defines the SQLAlchemy ORM (Object-Relational Mapping) models corresponding to the core electrical network components in the GroundInsight package. Each database model facilitates the storage, retrieval, and manipulation of data related to BusTypes, BranchTypes, Buses, Branches, Faults, Sources, Paths, and Networks. The models include methods to convert between Pydantic models and SQLAlchemy database models, ensuring seamless data integration and persistence.
BranchDB ¶
Bases: Base
BranchDB Model.
Represents a Branch in the database, including its properties, type, connected buses, and associated impedances.
BranchTypeDB ¶
Bases: Base
BranchTypeDB Model.
Represents a BranchType in the database, including its properties and associated branches.
BusDB ¶
Bases: Base
BusDB Model.
Represents a Bus in the database, including its properties, type, and associated faults and sources.
BusTypeDB ¶
Bases: Base
BusTypeDB Model.
Represents a BusType in the database, including its properties and associated buses.
ComplexNumberDB ¶
Bases: Base
ComplexNumberDB Model.
Represents a complex number with real and imaginary parts for storage in the database.
FaultDB ¶
Bases: Base
FaultDB Model.
Represents a Fault in the database, including its properties and associated bus.
NetworkDB ¶
Bases: Base
NetworkDB Model.
Represents a Network in the database, including its properties and associated components such as buses, branches, faults, sources, and paths. It also tracks the active fault within the network.
PathDB ¶
Bases: Base
PathDB Model.
Represents a Path in the database, including its properties, associated source and fault, and connected branches (segments).
SourceDB ¶
Bases: Base
SourceDB Model.
Represents a Source in the database, including its properties and associated bus.