mqlalchemy Package

mqlalchemy Package

mqlalchemy.__init__

Query SQLAlchemy objects using MongoDB style syntax.

copyright:(c) 2016 by Nicholas Repole and contributors. See AUTHORS for more details.
license:MIT - See LICENSE for more details.
exception mqlalchemy.__init__.InvalidMQLException[source]

Bases: Exception

Generic exception class for invalid queries.

mqlalchemy.__init__.apply_mql_filters(query_session, model_class, filters=None, whitelist=None, stack_size_limit=None, convert_key_names_func=None, gettext=None)[source]

Applies filters to a query and returns it.

Supported operators include:

  • $and
  • $or
  • $not
  • $nor
  • $in
  • $nin
  • $gt
  • $gte
  • $lt
  • $lte
  • $ne
  • $mod

Custom operators added for convenience:

  • $eq - Explicit equality check.
  • $like - Search a text field for the given value.

Considering adding:

  • $regex
  • $size
  • Array index queries - e.g. Album.tracks.0 to get the first track.

Won’t be implemented:

  • $all
  • $exists
  • $text
  • $type
  • $where
  • Exact matches for arrays/relationships

This function is massive, but breaking it up seemed to make things even harder to follow. Should find a better way of breaking things up, but for now, accept my apologies.

Parameters:
  • query_session – A db session or query object. Filters are applied to this object.
  • model_class – The sqlalchemy model class you want to query.
  • filters – A dictionary of MongoDB style query filters.
  • whitelist – May either be a function, a list, or None. If a function is provided, it should take in a dot separated field name and return True if it is acceptable to query that field, or False if not. If a list of fieldnames is provided, field names will be checked against that list to determine whether or not it is an allowed field to be queried. If None is provided, all fields and relationships of a model will be queryable. Also note that the field name being checked will already have been converted by convert_key_names_func if provided.
  • convert_key_names_func – Optional function used to convert a provided attribute name into a field name for a model. Should take one parameter, which is a dot separated name, and should return a converted string in the same dot separated format. For example, say you want to be able to query your model, which contains field names with underscores, using lowerCamelCase instead. The provided function should take a string such as “tracks.unitPrice” and convert it to “tracks.unit_price”. For the sake of raising more useful exceptions, the function should return None if an invalid field name is provided, however this is not necessary.
  • stack_size_limit – Optional parameter used to limit the allowable complexity of the provided filters. Can be useful in preventing malicious query attempts.
  • gettext – Supply a translation function to convert error messages to the desired language. Note that no translations are included by default, you must generate your own.
mqlalchemy.__init__.convert_to_alchemy_type(value, alchemy_type)[source]

Convert a given value to a sqlalchemy friendly type.

_compat Module

mqlalchemy._compat

Python2/Python3 support helper library.

copyright:(c) 2016 by Nicholas Repole and contributors. See AUTHORS for more details.
license:MIT - See LICENSE for more details.