Skip to content

Edk2 db

edk2_db

A class for interacting with a database implemented using json.

Base

The base class for creating database table models.

This class should be the subclass for any table model that will be used with Edk2DB.

Edk2DB

A SQLite3 database manager for a EDKII workspace.

This class provides the ability to register parsers that will create / update tables in the database. This will create a SQLite datbase file that can be queried using any SQLite3 client. VSCode provides multiple extensions for viewing and interacting with the database. Queries can also be created and run in python using the sqlite3 module that comes with python.

Edk2DB can, and should, be used as a context manager to ensure that the database is closed properly. If not using as a context manager, the db.connection.commit() and db.connection.close() must be used to cleanly close the database.

Attributes:

Name Type Description
connection Connection

The connection to the database

Note

Edk2DB provides a table called junction that can be used to make associations between tables. It has the following schema: env_id, table1, key1, table2, key2.

Example
from edk2toollib.database.parsers import *
table = "..."
with Edk2DB(Path("path/to/db.db"), edk2path) as db:
    db.register(Parser1(), Parser2(), Parser3())
    db.parse()
    db.connection.execute("SELECT * FROM ?", table)

__init__

__init__(
    db_path: str,
    pathobj: Edk2Path = None,
    **kwargs: dict[str, Any]
) -> Edk2DB

Initializes the database.

Parameters:

Name Type Description Default
db_path str

Path to create or load the database from

required
pathobj Edk2Path

Edk2Path object for the workspace

None
**kwargs dict[str, Any]

None

{}

session

session() -> Session

Provides a context manager for a session with the database.

Handles commiting changes and rolling back if an exception is raised.

register

register(*parsers: TableGenerator) -> None

Registers a one or more table generators.

Parameters:

Name Type Description Default
*parsers TableGenerator

One or more instantiated TableGenerator object

()

clear_parsers

clear_parsers() -> None

Empties the list of registered table generators.

parse

parse(env: dict) -> None

Runs all registered table parsers against the database.

Note

To enable queries to differentiate between two parses, an environment table is always created if it does not exist, and a row is added for each call of this command.

TableGenerator

An interface for a parser that generates a sqlite3 table maintained by Edk2DB.

Allows you to parse a workspace, file, etc, and load the contents into the database as rows in a table.

Edk2Db provides a connection to a sqlite3 database and will commit any changes made during parse once the parser has finished executing and has returned. Review sqlite3 documentation for more information on how to interact with the database.

__init__

__init__(*args: Any, **kwargs: Any) -> TableGenerator

Initialize the query with the specific settings.

parse

parse(
    session: Session, pathobj: Edk2Path, id: str, env: dict
) -> None

Execute the parser and update the database.