Querying¶
-
sir.querying.
iter_bounds
(db_session, column, batch_size, importlimit)[source]¶ Return a list of (lower bound, upper bound) tuples which contain row ids to iterate through a table in batches of
batch_size
. Ifimportlimit
is greater than zero, return only enough tuples to containimportlimit
rows. The second element of the last tuple in the returned list may beNone
. This happens if the last batch will contain less thanbatch_size
rows.Parameters: - db_session (sqlalchemy.orm.session.Session) –
- column (sqlalchemy.Column) –
- batch_size (int) –
- importlimit (int) –
Return type:
-
sir.querying.
iterate_path_values
(path, obj)[source]¶ Return an iterator over all values for path on obj, an instance of a declarative class by first splitting the path into its elements by splitting it on the dots, resulting in a list of path elements. Then, for each element, a call to
getattr()
is made - the arguments will be the current model (which initially is the model assigned to theSearchEntity
) and the current path element. After doing this, there are two cases:- The path element is not the last one in the path. In this case, the
getattr()
call returns one or more objects of another model which will replace the current one. - The path element is the last one in the path. In this case, the value
returned by the
getattr()
call will be returned and added to the list of values for this field.
Warning
Hybrid attributes like @hybrid_property are currently not supported.
To give an example, lets presume the object we’re starting with is an instance of
Artist
and the path is “begin_area.name”. The firstgetattr()
call will be:getattr(obj, "begin_area")
which returns an
Area
object, on which the call:getattr(obj, "name")
will return the final value:
>>> from mbdata.models import Artist, Area >>> artist = Artist(name="Johan de Meij") >>> area = Area(name="Netherlands") >>> artist.begin_area = area >>> list(iterate_path_values("begin_area.name", artist)) ['Netherlands']
One-to-many relationships will of course be handled as well:
>>> from mbdata.models import Recording, ISRC >>> recording = Recording(name="Fortuna Imperatrix Mundi: O Fortuna") >>> recording.isrcs.append(ISRC(isrc="DEF056730100")) >>> recording.isrcs.append(ISRC(isrc="DEF056730101")) >>> list(iterate_path_values("isrcs.isrc", recording)) ['DEF056730100', 'DEF056730101']
- The path element is not the last one in the path. In this case, the