Session#

class ormstorm.Session(path: str, tables: list[Union[ormstorm.orm.table.DynamicTable, Type[ormstorm.orm.table.Table]]] | None = None, **kwargs)#

Creates a new session to work with the database.

Parameters:
  • path – Path to the database

  • tables – List of tables to be created during session initialization

  • kwargs – Other options for opening a database [ More details in sqlite3.connect(…) ]

clear(table: DynamicTable | Type[Table]) None#

Clears the selected table.

Parameters:

table – Table or dynamic table

Returns:

Nothing

close() None#

Will close the session.

Returns:

Nothing

count(data: MagicFilter | DynamicTable | Table | Type[Table]) int#

Counts the number of rows satisfying given conditions.

Parameters:

data – Any type of table or magic filter

Returns:

Integer

create(table: DynamicTable | Type[Table]) None#

Creates a new table in the database.

Parameters:

table – Table or dynamic table

Returns:

Nothing

delete(data: MagicFilter | DynamicTable | Table | Type[Table]) None#

Removes all rows that match the specified conditions

Parameters:

data – Any type of table or magic filter

Returns:

Nothing

drop(table: DynamicTable | Type[Table]) None#

Completely removes the table from the database.

Parameters:

table – Table or dynamic table

Returns:

Nothing

execute(sql: str, parameters: tuple | object = Ellipsis) Cursor#

Execute sql query. [ More details in sqlite3.connect(…) ]

Parameters:
  • sql – Sql query

  • parameters – Query parameters

Returns:

SQLite database cursor

exists(data: MagicFilter | DynamicTable | Table | Type[Table]) bool#

Checks for the existence of rows satisfying given conditions.

Parameters:

data – Any type of table or magic filter

Returns:

Boolean

insert(table: Table, replace: bool = False) None#

Adds a new row to the table.

Parameters:
  • table – Initialized table object

  • replace – Will replace an existing row

Returns:

Nothing

select(data: MagicFilter | DynamicTable | Table | Type[Table], items: list[Union[ormstorm.orm.column.Column, ormstorm.orm.column.ColumnType]] | None = None) list[dict[str, object]]#

Selects certain data from a table that satisfies given conditions.

Parameters:
  • data – Any type of table or magic filter

  • items – Elements to select

Returns:

List of tuples

update(data: MagicFilter | DynamicTable | Table | Type[Table], table: Table) None#

Updates the selected rows in the table.

Parameters:
  • data – Initialized table object

  • table – Any type of table or magic filter

Returns:

Nothing

ormstorm.create_session(path: str, tables: list[Union[ormstorm.orm.table.DynamicTable, Type[ormstorm.orm.table.Table]]], **kwargs) Callable[[], Session]#

Creates all tables in the selected database.

Parameters:
  • path – Path to the database

  • tables – List of tables to be created during session initialization

  • kwargs – Other options for opening a database [ More details in sqlite3.connect(…) ]

Returns:

Local session with given parameters

Usage#

Standard session creation.

session = Session(
    path="path/to/database",
    tables=[YourTable, ...]
)

You can also use the with construct to automatically close the session.

with Session(
    path="path/to/database",
    tables=[YourTable, ...]
) as session:
    ...

The best solution would be to create a local session. This will allow you not to every time create a new session yourself, specifying the path to the database, table to initialize, etc.

IMPORTANT! The local session does not take any arguments and it is impossible to change any parameters for it!

LocalSession = create_session(
    path="path/to/database",
    tables=[YourTable, ...]
)

with LocalSession() as session:
    ...