Querying data

Sqlite bedrock packs package provide functions to build queries more easily.

class Left(value)[source]

A helper class to indicate that a table should be joined using LEFT join in the query of EasyQuery class.

Parameters

value (str) –

value: str

The name of the table

build_easy_query(root, *tables, blacklist=('BehaviorPack', 'ResourcePack'), accept_non_pk=True, distinct=True, where=None, group_by=None, having=None, order_by=None)[source]

Returns a string with a SQL query that can be used to query the sqlite_bedrock_packs database. The query is built using the class names of the wrapper classes for the tables or their names wrapped in the Left object in case of LEFT join. The results of the query are the primary keys of the selected tables.

Example:

>>> query = build_easy_query(
...     Entity, Left(Geometry), RpAnimation,
...     accept_non_pk=True,
...     where=[
...         "EntityFile.EntityFile_pk == 1"
...     ]
... ).sql_code
>>> print(query)
SELECT DISTINCT
        Entity_pk AS Entity,
        Geometry_pk AS Geometry,
        RpAnimation_pk AS RpAnimation
FROM Entity
JOIN ClientEntity
        ON Entity.identifier = ClientEntity.identifier
JOIN ClientEntityGeometryField
        ON ClientEntity.ClientEntity_pk = ClientEntityGeometryField.ClientEntity_fk
LEFT JOIN Geometry
        ON ClientEntityGeometryField.identifier = Geometry.identifier
JOIN ClientEntityAnimationField
        ON ClientEntity.ClientEntity_pk = ClientEntityAnimationField.ClientEntity_fk
JOIN RpAnimation
        ON ClientEntityAnimationField.identifier = RpAnimation.identifier
WHERE
        EntityFile.EntityFile_pk == 1
Parameters
  • root (_T2) – The root table to start the query from.

  • tables (Any) – A list of tables to join. Use Left to indicate that a table should be joined using LEFT join. The tables don’t need to have a direct connection between each other, the connections will be found automatically if necessary relations exist (this means that the query genrated can include tables that are not in the list).

  • blacklist (Iterable[str]) – A list of tables to ignore while searching for connections. By default it’s BehaviorPack and ResourcePack because otherwise in many cases the query would look for objects from the same packs instead of using different more useful connections.

  • accept_non_pk (bool) – Whether to accept non-primary key relations. By default it’s True. The primary key connections only cover the situations where it’s guaranteed that the relation is valid (like a relation between EntityFile and Entity). Most of the connections that might be useful to query are non-primary key connections (like a relation between a ClientEntity and RenderController).

  • distinct (bool) – Whether to use DISTINCT in the query. By default it’s True. It’s recommended to use it when querying for multiple tables because otherwise the query might return the same row multiple times.

  • where (Optional[list[str]]) – A list of constraints to add to the query. This is a list of strings with raw SQL code which is inserted into the WHERE part of the query. The constraints are joined using AND.

  • group_by (Optional[list[str]]) – A list of columns to group the results by. This is a list of strings with raw SQL code which is inserted into the GROUP BY part of the query.

  • having (Optional[list[str]]) – A list of constraints to add to the query. This is a list of strings with raw SQL code which is inserted into the HAVING part of the query. The constraints are joined using AND.

  • order_by (Optional[list[str]]) – A list of columns to order the results by. This is a list of strings with raw SQL code which is inserted into the ORDER BY part of the query.

Return type

str

yield_from_easy_query(db: Union[Connection, Database], t1: Type[_T], /, blacklist: Iterable[str] = ('BehaviorPack', 'ResourcePack'), accept_non_pk: bool = True, distinct: bool = True, where: Optional[list[str]] = None, group_by: Optional[list[str]] = None, having: Optional[list[str]] = None, order_by: Optional[list[str]] = None) Iterator[tuple[_T]][source]
yield_from_easy_query(db: Union[Connection, Database], t1: Type[_T], t2: Type[_T2], /, blacklist: Iterable[str] = ('BehaviorPack', 'ResourcePack'), accept_non_pk: bool = True, distinct: bool = True, where: Optional[list[str]] = None, group_by: Optional[list[str]] = None, having: Optional[list[str]] = None, order_by: Optional[list[str]] = None) Iterator[tuple[_T, _T2]]
yield_from_easy_query(db: Union[Connection, Database], t1: Type[_T], t2: Type[_T2], t3: Type[_T3], /, blacklist: Iterable[str] = ('BehaviorPack', 'ResourcePack'), accept_non_pk: bool = True, distinct: bool = True, where: Optional[list[str]] = None, group_by: Optional[list[str]] = None, having: Optional[list[str]] = None, order_by: Optional[list[str]] = None) Iterator[tuple[_T, _T2, _T3]]
yield_from_easy_query(db: Union[Connection, Database], t1: Type[_T], t2: Type[_T2], t3: Type[_T3], t4: Type[_T4], /, blacklist: Iterable[str] = ('BehaviorPack', 'ResourcePack'), accept_non_pk: bool = True, distinct: bool = True, where: Optional[list[str]] = None, group_by: Optional[list[str]] = None, having: Optional[list[str]] = None, order_by: Optional[list[str]] = None) Iterator[tuple[_T, _T2, _T3, _T4]]
yield_from_easy_query(db: Union[Connection, Database], t1: Type[_T], t2: Type[_T2], t3: Type[_T3], t4: Type[_T4], t5: Type[_T5], /, blacklist: Iterable[str] = ('BehaviorPack', 'ResourcePack'), accept_non_pk: bool = True, distinct: bool = True, where: Optional[list[str]] = None, group_by: Optional[list[str]] = None, having: Optional[list[str]] = None, order_by: Optional[list[str]] = None) Iterator[tuple[_T, _T2, _T3, _T4, _T5]]
yield_from_easy_query(db: Union[Connection, Database], t1: Type[_T], t2: Type[_T2], t3: Type[_T3], t4: Type[_T4], t5: Type[_T5], t6: Type[_T6], /, blacklist: Iterable[str] = ('BehaviorPack', 'ResourcePack'), accept_non_pk: bool = True, distinct: bool = True, where: Optional[list[str]] = None, group_by: Optional[list[str]] = None, having: Optional[list[str]] = None, order_by: Optional[list[str]] = None) Iterator[tuple[_T, _T2, _T3, _T4, _T5, _T6]]
yield_from_easy_query(db: Union[Connection, Database], t1: Type[_T], t2: Type[_T2], t3: Type[_T3], t4: Type[_T4], t5: Type[_T5], t6: Type[_T6], t7: Type[_T7], /, blacklist: Iterable[str] = ('BehaviorPack', 'ResourcePack'), accept_non_pk: bool = True, distinct: bool = True, where: Optional[list[str]] = None, group_by: Optional[list[str]] = None, having: Optional[list[str]] = None, order_by: Optional[list[str]] = None) Iterator[tuple[_T, _T2, _T3, _T4, _T5, _T6, _T7]]
yield_from_easy_query(db: Union[Connection, Database], t1: Type[_T], t2: Type[_T2], t3: Type[_T3], t4: Type[_T4], t5: Type[_T5], t6: Type[_T6], t7: Type[_T7], t8: Type[_T8], /, blacklist: Iterable[str] = ('BehaviorPack', 'ResourcePack'), accept_non_pk: bool = True, distinct: bool = True, where: Optional[list[str]] = None, group_by: Optional[list[str]] = None, having: Optional[list[str]] = None, order_by: Optional[list[str]] = None) Iterator[tuple[_T, _T2, _T3, _T4, _T5, _T6, _T7, _T8]]
yield_from_easy_query(db: Union[Connection, Database], t1: Type[_T], t2: Type[_T2], t3: Type[_T3], t4: Type[_T4], t5: Type[_T5], t6: Type[_T6], t7: Type[_T7], t8: Type[_T8], t9: Type[_T9], /, blacklist: Iterable[str] = ('BehaviorPack', 'ResourcePack'), accept_non_pk: bool = True, distinct: bool = True, where: Optional[list[str]] = None, group_by: Optional[list[str]] = None, having: Optional[list[str]] = None, order_by: Optional[list[str]] = None) Iterator[tuple[_T, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9]]

Returns an iterator that yields the wrapper classes from the query results. The arguments are the same as for build_easy_query(). The first argument is the Database object or the sqlite3.Connection.

yield_from_any_query(db, sql_query)[source]

Yields the results from any query generated by build_easy_query() function and wraps them in the wrapper classes. This function in combination with build_easy_query() is equivalent to the yield_from_easy_query() function.

Parameters
  • db (Union[Connection, Database]) –

  • sql_query (str) –

Return type

Iterator[tuple]