Skip to content

Database Documentation

create_engine_db(db_path='sqlite:///DB.db')

Creates an SQLAlchemy Engine instance which is the starting point for any SQLAlchemy application.

Parameters:

Name Type Description Default
db_path str

The database URL to connect to. Defaults to an SQLite database named 'DB.db'.

'sqlite:///DB.db'

Returns:

Type Description
Engine

sqlalchemy.engine.base.Engine: A database engine that can be used to interact with the database.

Source code in CRR/db/db.py
63
64
65
66
67
68
69
70
71
72
73
74
75
def create_engine_db(db_path: str = 'sqlite:///DB.db') -> sqlalchemy.engine.base.Engine:
    """
    Creates an SQLAlchemy Engine instance which is the starting point for any SQLAlchemy application.

    Parameters:
        db_path (str): The database URL to connect to. Defaults to an SQLite database named 'DB.db'.

    Returns:
        sqlalchemy.engine.base.Engine: A database engine that can be used to interact with the database.
    """

    engine = create_engine(db_path)
    return engine

get_session(engine)

Creates and returns a new SQLAlchemy session bound to the given engine.

Parameters:

Name Type Description Default
engine Engine

The Engine instance connected to the database.

required

Returns:

Type Description
Session

sqlalchemy.orm.session.Session: A session object that acts as a handle to the database.

Source code in CRR/db/db.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def get_session(engine: sqlalchemy.engine.base.Engine) -> sqlalchemy.orm.session.Session:
    """
    Creates and returns a new SQLAlchemy session bound to the given engine.

    Parameters:
        engine (sqlalchemy.engine.base.Engine): The Engine instance connected to the database.

    Returns:
        sqlalchemy.orm.session.Session: A session object that acts as a handle to the database.
    """
    DBSession = sessionmaker(bind=engine)
    return DBSession()

initialize_database(engine)

Creates all tables in the database defined in the Base metadata class using the provided engine.

Parameters:

Name Type Description Default
engine Engine

The Engine instance connected to the database.

required

Returns:

Type Description
None

None

Source code in CRR/db/db.py
77
78
79
80
81
82
83
84
85
86
87
def initialize_database(engine: sqlalchemy.engine.base.Engine) -> None:
    """
    Creates all tables in the database defined in the Base metadata class using the provided engine.

    Parameters:
        engine (sqlalchemy.engine.base.Engine): The Engine instance connected to the database.

    Returns:
        None
    """
    Base.metadata.create_all(engine)

push_data_to_db(session, df, table_class)

Inserts data from a DataFrame into the specified table in the database, matching DataFrame columns to table columns.

Parameters:

Name Type Description Default
session Session

The SQLAlchemy session to be used for database transactions.

required
df DataFrame

The DataFrame containing the data to be inserted into the database.

required
table_class type

The SQLAlchemy table class (model) that defines the schema of the table where data will be inserted.

required

Returns:

Type Description
None

None

Description

The function filters the DataFrame to include only valid columns that exist in the database table. It handles missing values, converting them to None, and ensures that data types are correctly cast to match the column definitions in the database schema.

Source code in CRR/db/db.py
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
def push_data_to_db(session: Session, df: pd.DataFrame, table_class: type) -> None:
    """
    Inserts data from a DataFrame into the specified table in the database, matching DataFrame columns to table columns.

    Parameters:
        session (Session): The SQLAlchemy session to be used for database transactions.
        df (pd.DataFrame): The DataFrame containing the data to be inserted into the database.
        table_class (type): The SQLAlchemy table class (model) that defines the schema of the table where data will be inserted.

    Returns:
        None

    Description:
        The function filters the DataFrame to include only valid columns that exist in the database table. It handles missing values,
        converting them to None, and ensures that data types are correctly cast to match the column definitions in the database schema.
    """
    valid_columns = {c.name for c in table_class.__table__.columns}
    df = df.loc[:, df.columns.intersection(valid_columns)]

    for index, row in df.iterrows():
        row_dict = row.to_dict()
        for key, value in row_dict.items():
            if pd.isna(value):
                row_dict[key] = None
            else:
                col_type = type(getattr(table_class, key).type).__name__
                if col_type == 'Integer':
                    row_dict[key] = int(value)
                elif col_type == 'Float':
                    row_dict[key] = float(value)
                elif col_type == 'String':
                    row_dict[key] = str(value)

        table_instance = table_class(**row_dict)
        session.add(table_instance)
    session.commit()

view_table(session, table_class)

Retrieves and prints all records from a specified table in the database.

Parameters:

Name Type Description Default
session Session

The SQLAlchemy session used for querying the database.

required
table_class type

The SQLAlchemy table class (model) from which records will be retrieved.

required

Returns:

Type Description
None

None

Description

This function queries all instances of the specified table class and prints their attributes. This is useful for debugging or for simple inspection tasks where direct database access is impractical.

Source code in CRR/db/db.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def view_table(session: Session, table_class: type) -> None:
    """
    Retrieves and prints all records from a specified table in the database.

    Parameters:
        session (Session): The SQLAlchemy session used for querying the database.
        table_class (type): The SQLAlchemy table class (model) from which records will be retrieved.

    Returns:
        None

    Description:
        This function queries all instances of the specified table class and prints their attributes. This is useful for debugging
        or for simple inspection tasks where direct database access is impractical.
    """
    for instance in session.query(table_class).all():
        print(instance.__dict__)