Database
tibiawikisql.database
¶
Contains all the SQL related model definitions.
Classes:
| Name | Description |
|---|---|
Column |
Represents a column in a SQL table. |
TableMeta |
Metaclass for table classes. |
Table |
Represents a SQL table. |
SQLType |
An SQL type definition. |
Timestamp |
A timestamp, represented as a ISO 8601 string. |
Date |
A date, represented as a ISO 8601 string. |
Integer |
Integer type. |
Real |
Real type. |
Text |
Text type. |
Blob |
Blob type. |
Boolean |
Boolean type. |
ForeignKey |
Defines a foreign key. |
Column
¶
Column(
column_type: type[SQLType] | SQLType,
name: str | None = None,
*,
unique: bool = False,
primary_key: bool = False,
nullable: bool = True,
default: Any | None = None,
auto_increment: bool = False,
index: bool = False,
no_case: bool = False,
)
Represents a column in a SQL table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column_type
|
type[SQLType] | SQLType
|
The SQL type of the column. |
required |
name
|
str | None
|
The name of the column. If unset, it will get it from the attribute's name. |
None
|
unique
|
bool
|
Whether to create a unique index for the column or not. |
False
|
primary_key
|
bool
|
Whether the column is a primary key or not. |
False
|
nullable
|
bool
|
Whether the class can be null or not. |
True
|
default
|
Any | None
|
The default value of the column if undefined. |
None
|
auto_increment
|
bool
|
Whether the value should auto increment or not. |
False
|
index
|
bool
|
Whether the column is indexed or not. |
False
|
no_case
|
bool
|
Whether the column should be case-insensitive or not. |
False
|
Methods:
| Name | Description |
|---|---|
get_column_definition |
Get the SQL definition of this column, as used in a |
Source code in tibiawikisql/database.py
get_column_definition
¶
get_column_definition() -> str
Get the SQL definition of this column, as used in a CREATE TABLE statement.
Returns:
| Type | Description |
|---|---|
str
|
The statement that defines the column. |
Source code in tibiawikisql/database.py
TableMeta
¶
Table
¶
Represents a SQL table.
Methods:
| Name | Description |
|---|---|
get_create_table_statement |
Generate the |
all_tables |
Get a list of all defined tables. |
insert |
Insert a row into this table. |
get_drop_statement |
Get the SQL query to drop this table. |
get_one_by_field |
Get a row by a specific column's value. |
get_list_by_field |
Get a list of rows matching the specified field's value. |
get_create_table_statement
classmethod
¶
Generate the CREATE TABLE statement.
Returns:
| Type | Description |
|---|---|
str
|
A SQL statement to create the table. |
Source code in tibiawikisql/database.py
all_tables
classmethod
¶
insert
classmethod
¶
insert(conn: Connection | Cursor, **kwargs: Any) -> None
Insert a row into this table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection | Cursor
|
A connection to the database. |
required |
**kwargs
|
Any
|
The column values. |
{}
|
Source code in tibiawikisql/database.py
get_drop_statement
classmethod
¶
get_drop_statement() -> str
get_one_by_field
classmethod
¶
get_one_by_field(
conn: Connection | Cursor,
column: str,
value: Any,
use_like: bool = False,
) -> Row | None
Get a row by a specific column's value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection | Cursor
|
A SQL connection. |
required |
column
|
str
|
The name of the column. |
required |
value
|
Any
|
The value to match it against. |
required |
use_like
|
bool
|
Whether to use |
False
|
Returns:
| Type | Description |
|---|---|
Row | None
|
The matching row, or None. |
Raises:
| Type | Description |
|---|---|
ValueError
|
The specified column doesn't exist in the table. |
Source code in tibiawikisql/database.py
get_list_by_field
classmethod
¶
get_list_by_field(
conn: Connection | Cursor,
column: str,
value: Any,
use_like: bool = False,
sort_by: str | None = None,
ascending: bool = True,
limit: int | None = None,
*,
base_query: SQLLiteQuery | None = None,
) -> list[Row]
Get a list of rows matching the specified field's value.
Note that this won't get values found in child tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection | Cursor
|
A SQL connection. |
required |
column
|
str
|
The name of the column. |
required |
value
|
Any
|
The value to match it against. |
required |
use_like
|
bool
|
Whether to use |
False
|
sort_by
|
str | None
|
The name of the field to sort by. |
None
|
ascending
|
bool
|
Whether to sort ascending or descending. |
True
|
limit
|
int | None
|
Only return up to this many rows. |
None
|
Returns:
| Type | Description |
|---|---|
list[Row]
|
The matching row, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
Tg |
Source code in tibiawikisql/database.py
SQLType
¶
An SQL type definition.
Methods:
| Name | Description |
|---|---|
to_sql |
Get the name of the corresponding type in SQLite. |
to_sql_value |
Convert a value to its corresponding SQL value. |
Attributes:
| Name | Type | Description |
|---|---|---|
python |
type
|
The python class that represents this object. |
to_sql
¶
to_sql() -> str
Get the name of the corresponding type in SQLite.
Returns:
| Type | Description |
|---|---|
str
|
A string containing the type's definition. |
Timestamp
¶
Bases: SQLType
A timestamp, represented as a ISO 8601 string.
Methods:
| Name | Description |
|---|---|
to_sql |
Get the name of the corresponding type in SQLite. |
to_sql_value |
Convert a value to its corresponding SQL value. |
Attributes:
| Name | Type | Description |
|---|---|---|
python |
The python class that represents this object. |
Date
¶
Bases: SQLType
A date, represented as a ISO 8601 string.
Methods:
| Name | Description |
|---|---|
to_sql |
Get the name of the corresponding type in SQLite. |
to_sql_value |
Convert a value to its corresponding SQL value. |
Attributes:
| Name | Type | Description |
|---|---|---|
python |
The python class that represents this object. |
Integer
¶
Bases: SQLType
Integer type.
Methods:
| Name | Description |
|---|---|
to_sql |
Get the name of the corresponding type in SQLite. |
Attributes:
| Name | Type | Description |
|---|---|---|
python |
The python class that represents this object. |
Real
¶
Bases: SQLType
Real type.
Methods:
| Name | Description |
|---|---|
to_sql |
Get the name of the corresponding type in SQLite. |
Attributes:
| Name | Type | Description |
|---|---|---|
python |
The python class that represents this object. |
Text
¶
Blob
¶
Bases: SQLType
Blob type.
Methods:
| Name | Description |
|---|---|
to_sql |
Get the name of the corresponding type in SQLite. |
Attributes:
| Name | Type | Description |
|---|---|---|
python |
The python class that represents this object. |
Boolean
¶
Bases: SQLType
Boolean type.
Methods:
| Name | Description |
|---|---|
to_sql |
Get the name of the corresponding type in SQLite. |
Attributes:
| Name | Type | Description |
|---|---|---|
python |
The python class that represents this object. |
ForeignKey
¶
Bases: SQLType
Defines a foreign key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql_type
|
type[SQLType]
|
The SQL type of the column. |
required |
table
|
str
|
The name of the table that is referenced. |
required |
column
|
str
|
The name of the column from the reference table. |
required |
Methods:
| Name | Description |
|---|---|
to_sql |
Get the name of the corresponding type in SQLite. |
Attributes:
| Name | Type | Description |
|---|---|---|
python |
The python class that represents this object. |