Skip to content

Base

tibiawikisql.models.base

Module with base classes used by models.

Classes:

Name Description
WithStatus

Adds the status field to a model.

WithVersion

Adds the version field to a model.

WithImage

Adds the image field to a model.

RowModel

A mixin class to indicate that this model comes from a SQL table.

WithStatus pydantic-model

Bases: BaseModel

Adds the status field to a model.

Show JSON schema:
{
  "description": "Adds the status field to a model.",
  "properties": {
    "status": {
      "title": "Status",
      "type": "string"
    }
  },
  "required": [
    "status"
  ],
  "title": "WithStatus",
  "type": "object"
}

Fields:

status pydantic-field
status: str

The in-game status for this element

WithVersion pydantic-model

Bases: BaseModel

Adds the version field to a model.

Show JSON schema:
{
  "description": "Adds the version field to a model.",
  "properties": {
    "version": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "title": "Version"
    }
  },
  "required": [
    "version"
  ],
  "title": "WithVersion",
  "type": "object"
}

Fields:

version pydantic-field
version: str | None

The client version when this was implemented in the game, if known.

WithImage pydantic-model

Bases: BaseModel

Adds the image field to a model.

Show JSON schema:
{
  "description": "Adds the image field to a model.",
  "properties": {
    "image": {
      "anyOf": [
        {
          "format": "binary",
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Image"
    }
  },
  "title": "WithImage",
  "type": "object"
}

Fields:

image pydantic-field
image: bytes | None = None

An image representing this article.

RowModel pydantic-model

Bases: BaseModel

A mixin class to indicate that this model comes from a SQL table.

Show JSON schema:
{
  "description": "A mixin class to indicate that this model comes from a SQL table.",
  "properties": {},
  "title": "RowModel",
  "type": "object"
}
table class-attribute

The SQL table where this model is stored.

insert
insert(conn: Connection | Cursor) -> None

Insert the model into its respective database table.

Parameters:

Name Type Description Default
conn Connection | Cursor

A cursor or connection to the database.

required
Source code in tibiawikisql/models/base.py
def insert(self, conn: Connection | Cursor) -> None:
    """Insert the model into its respective database table.

    Args:
        conn: A cursor or connection to the database.
    """
    rows = {}
    for column in self.table.columns:
        try:
            value = getattr(self, column.name)
            if value == column.default:
                continue
            rows[column.name] = value
        except AttributeError:
            continue
    self.table.insert(conn, **rows)
from_row classmethod
from_row(row: Row | dict[str, Any]) -> Self

Return an instance of the model from a row or dictionary.

Parameters:

Name Type Description Default
row Row | dict[str, Any]

A dict representing a row or a Row object.

required

Returns:

Type Description
Self

An instance of the class, based on the row.

Source code in tibiawikisql/models/base.py
@classmethod
def from_row(cls, row: Row | dict[str, Any]) -> Self:
    """Return an instance of the model from a row or dictionary.

    Args:
        row: A dict representing a row or a Row object.

    Returns:
        An instance of the class, based on the row.

    """
    if isinstance(row, Row):
        row = dict(row)
    return cls.model_validate(row)
get_one_by_field classmethod
get_one_by_field(
    conn: Connection | Cursor,
    field: str,
    value: Any,
    use_like: bool = False,
) -> Self | None

Get a single element matching the field's value.

Parameters:

Name Type Description Default
conn Connection | Cursor

A connection or cursor of the database.

required
field str

The field to filter with.

required
value Any

The value to look for.

required
use_like bool

Whether to use LIKE as a comparator instead of =.

False

Returns:

Type Description
Self | None

The object found, or None if no entries match.

Raises:

Type Description
ValueError

The specified field doesn't exist in the table.

Source code in tibiawikisql/models/base.py
@classmethod
def get_one_by_field(cls, conn: Connection | Cursor, field: str, value: Any, use_like: bool = False) -> Self | None:
    """Get a single element matching the field's value.

    Args:
        conn: A connection or cursor of the database.
        field: The field to filter with.
        value: The value to look for.
        use_like: Whether to use ``LIKE`` as a comparator instead of ``=``.

    Returns:
        The object found, or ``None`` if no entries match.

    Raises:
        ValueError: The specified field doesn't exist in the table.

    """
    row = cls.table.get_one_by_field(conn, field, value, use_like)
    return cls.from_row(row) if row else None
get_list_by_field classmethod
get_list_by_field(
    conn: Connection | Cursor,
    field: str,
    value: Any | None = None,
    use_like: bool = False,
    sort_by: str | None = None,
    ascending: bool = True,
) -> list[Self]

Get a list of elements 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 connection or cursor of the database.

required
field str

The name of the field to filter by.

required
value Any | None

The value to filter by.

None
use_like bool

Whether to use LIKE as a comparator instead of =.

False
sort_by str | None

The name of the field to sort by.

None
ascending bool

Whether to sort ascending or descending.

True

Returns:

Type Description
list[Self]

A list containing all matching objects.

Raises:

Type Description
ValueError

The specified field doesn't exist in the table.

Source code in tibiawikisql/models/base.py
@classmethod
def get_list_by_field(
        cls,
        conn: Connection | Cursor,
        field: str,
        value: Any | None = None,
        use_like: bool = False,
        sort_by: str | None = None,
        ascending: bool = True,
) -> list[Self]:
    """Get a list of elements matching the specified field's value.

    Note that this won't get values found in child tables.

    Args:
        conn:  A connection or cursor of the database.
        field: The name of the field to filter by.
        value: The value to filter by.
        use_like: Whether to use ``LIKE`` as a comparator instead of ``=``.
        sort_by: The name of the field to sort by.
        ascending: Whether to sort ascending or descending.

    Returns:
        A list containing all matching objects.

    Raises:
        ValueError: The specified field doesn't exist in the table.

    """
    rows = cls.table.get_list_by_field(conn, field, value, use_like, sort_by, ascending)
    return [cls.from_row(r) for r in rows]
get_by_id classmethod
get_by_id(
    conn: Connection | Cursor, article_id: int
) -> Self | None

Get an entry by its article ID.

Parameters:

Name Type Description Default
conn Connection | Cursor

A connection to the database.

required
article_id int

The article ID to search for.

required

Returns:

Type Description
Self | None

The model matching the article ID if found.

Source code in tibiawikisql/models/base.py
@classmethod
def get_by_id(cls, conn: Connection | Cursor, article_id: int) -> Self | None:
    """Get an entry by its article ID.

    Args:
        conn: A connection to the database.
        article_id: The article ID to search for.

    Returns:
        The model matching the article ID if found.

    """
    return cls.get_one_by_field(conn, "article_id", article_id)
get_by_title classmethod
get_by_title(
    conn: Connection | Cursor, title: str
) -> Self | None

Get an entry by its title.

Parameters:

Name Type Description Default
conn Connection | Cursor

A connection to the database.

required
title str

The title of the article.

required

Returns:

Type Description
Self | None

The model matching the article title if found.

Source code in tibiawikisql/models/base.py
@classmethod
def get_by_title(cls, conn: Connection | Cursor, title: str) -> Self | None:
    """Get an entry by its title.

    Args:
        conn: A connection to the database.
        title: The title of the article.

    Returns:
        The model matching the article title if found.

    """
    return cls.get_one_by_field(conn, "title", title)