Database¶
kwai_core.db
¶
Package for all database related modules.
ORM (Object-relational mapping) software (like SQLAlchemy) is not used. Instead, the repository pattern is used. A repository is responsible for saving, creating, querying and deleting entities. sql-smith is used to build queries.
The [DatabaseQuery][kwai.kwai_core.db.database_query.DatabaseQuery] class can be used to implement the Query interface.
kwai_core.db.database
¶
Module for database classes/functions.
Database
¶
Class for communicating with a database.
Attributes:
| Name | Type | Description |
|---|---|---|
_connection |
Connection | None
|
A connection |
_settings |
DatabaseSettings
|
The settings for this database connection. |
settings
property
¶
Return the database settings.
This property is immutable: the returned value is a copy of the current settings.
begin()
async
¶
Start a transaction.
check_connection()
async
¶
Check if the connection is set, if not it will try to connect.
close()
async
¶
Close the connection.
commit()
async
¶
Commit all changes.
create_query_factory()
classmethod
¶
Return a query factory for the current database engine.
The query factory is used to start creating a SELECT, INSERT, UPDATE or DELETE query.
Returns:
| Type | Description |
|---|---|
QueryFactory
|
The query factory from sql-smith. Currently, it returns a query factory for the mysql engine. In the future it can provide other engines. |
delete(id_, table_name, id_column='id')
async
¶
Delete a row from the table using the id field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_
|
Any
|
The id of the row to delete. |
required |
table_name
|
str
|
The name of the table. |
required |
id_column
|
str
|
The name of the id column (default is 'id') |
'id'
|
Raises:
| Type | Description |
|---|---|
QueryException
|
Raised when the query results in an error. |
execute(query)
async
¶
Execute a query.
The last rowid from the cursor is returned when the query executed successfully. On insert, this can be used to determine the new id of a row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
AbstractQuery
|
The query to execute. |
required |
Returns:
| Type | Description |
|---|---|
int
|
When the query is an insert query, it will return the last rowid. |
None
|
When there is no last rowid. |
Raises:
| Type | Description |
|---|---|
QueryException
|
Raised when the query contains an error. |
fetch(query)
async
¶
Execute a query and yields each row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
SelectQuery
|
The query to execute. |
required |
Yields:
| Type | Description |
|---|---|
Record
|
A row is a dictionary using the column names as key and the column values as value. |
Raises:
| Type | Description |
|---|---|
QueryException
|
Raised when the query contains an error. |
fetch_one(query)
async
¶
Execute a query and return the first row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
SelectQuery
|
The query to execute. |
required |
Returns:
| Type | Description |
|---|---|
Record
|
A row is a dictionary using the column names as key and the column values as value. |
None
|
The query resulted in no rows found. |
Raises:
| Type | Description |
|---|---|
QueryException
|
Raised when the query contains an error. |
insert(table_name, *table_data, id_column='id')
async
¶
Insert one or more instances of a dataclass into the given table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
The name of the table |
required |
table_data
|
Any
|
One or more instances of a dataclass containing the values |
()
|
id_column
|
str
|
The name of the id column (default is 'id') |
'id'
|
Returns:
| Type | Description |
|---|---|
int
|
The last inserted id. When multiple inserts are performed, this will be the id of the last executed insert. |
Raises:
| Type | Description |
|---|---|
QueryException
|
Raised when the query contains an error. |
log_affected_rows(rowcount)
¶
Log the number of affected rows of the last executed query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rowcount
|
int
|
The number of affected rows. |
required |
log_query(query)
¶
Log a query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The query to log. |
required |
rollback()
async
¶
Rollback a transaction.
setup()
async
¶
Set up the connection pool.
update(id_, table_name, table_data, id_column='id')
async
¶
Update a dataclass in the given table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_
|
Any
|
The id of the data to update. |
required |
table_name
|
str
|
The name of the table. |
required |
table_data
|
Any
|
The dataclass containing the data. |
required |
id_column
|
str
|
The name of the id column (default is 'id'). |
'id'
|
Raises:
| Type | Description |
|---|---|
QueryException
|
Raised when the query contains an error. |
Returns:
| Type | Description |
|---|---|
int
|
The number of rows affected. |
kwai_core.db.database_query
¶
Module that implements a query for a database.
DatabaseQuery
¶
Bases: Query
Creates a query using a database.
columns
abstractmethod
property
¶
Returns the columns used in the query.
count_column
property
¶
The column used to count records.
count()
async
¶
Execute the query and counts the number of records.
The count_column is used as column for a distinct count.
fetch(limit=None, offset=None)
¶
Fetch all records from this query.
fetch_one()
async
¶
Fetch only one record from this query.
init()
abstractmethod
¶
Override this method to create the base query.
kwai_core.db.exceptions
¶
Module that defines all database related exceptions.
DatabaseException
¶
Bases: Exception
Raised when a database error occurred.
QueryException
¶
Bases: DatabaseException
Raised when the execution of a query failed.
sql
property
¶
Return the sql statement of the query.
kwai_core.db.table_row
¶
Module that defines some dataclasses that can be used as data transfer objects.
JoinedTableRow
dataclass
¶
A data transfer object for data from multiple tables.
Each field of the dataclass will represent a table. The name of the field will be used as prefix for creating an alias for each column of the associated table.
The derived class must be a dataclass.
Note
The derived class is also the ideal place to act as builder for an entity.
TableRow
dataclass
¶
A data transfer object for a row of one table.
The derived class must be a dataclass.
Note
The derived class is also the ideal place to act as builder for an entity.
column(column_name)
classmethod
¶
Return the column prefixed with the table name.
field(column_name)
classmethod
¶
Call sql-smith field with the given column.
short-cut for: field(table.table_name + '.' + column_name)
get_aliases(prefix=None)
classmethod
¶
Return aliases for all the fields of the dataclass.
get_column_alias(name, prefix=None)
classmethod
¶
Return the alias for a column.
map(row, prefix=None)
classmethod
¶
Map the data of a row to the dataclass.
A ValueError will be raised when a field contains data with the wrong type.
search(column_name)
classmethod
¶
Call sql-smith search with the given column.
short-cut for: search(table.table_name + '.' + column_name)
unwrap(val)
¶
Assert when the value is None.