Query-builder Internals

  • class AliasManager
  • Manages the aliases assigned to Source objects in SELECTqueries, so as to avoid ambiguous references when multiple sources areused in a single query.

    • add(source)
    • Add a source to the AliasManager’s internal registry at the currentscope. The alias will be automatically generated using the followingscheme (where each level of indentation refers to a new scope):

Parameters:source (Source) – Make the manager aware of a new source. If thesource has already been added, the call is a no-op.

  • get(source[, any_depth=False])
  • Return the alias for the source in the current scope. If the sourcedoes not have an alias, it will be given the next available alias.

Parameters:source (Source) – The source whose alias should be retrieved.Returns:The alias already assigned to the source, or the nextavailable alias.Return type:str

  • setitem(source, alias)
  • Manually set the alias for the source at the current scope.

Parameters:source (Source) – The source for which we set the alias.

  • push()
  • Push a new scope onto the stack.

  • pop()

  • Pop scope from the stack.
  • class State(scope[, parentheses=False[, subquery=False[, **kwargs]]])
  • Lightweight object for representing the state at a given scope. During SQLgeneration, each object visited by the Context can inspect thestate. The State class allows Peewee to do things like:

    • Use a common interface for field types or SQL expressions, but usevendor-specific data-types or operators.
    • Compile a Column instance into a fully-qualified attribute,as a named alias, etc, depending on the value of the scope.
    • Ensure parentheses are used appropriately.

Parameters:

  • scope (int) – The scope rules to be applied while the state is active.
  • parentheses (bool) – Wrap the contained SQL in parentheses.
  • subquery (bool) – Whether the current state is a child of an outerquery.
  • kwargs (dict) – Arbitrary settings which should be applied in thecurrent state.
  • class Context(**settings)
  • Converts Peewee structures into parameterized SQL queries.

Peewee structures should all implement a sql method, which will becalled by the Context class during SQL generation. The sql methodaccepts a single parameter, the Context instance, which allows forrecursive descent and introspection of scope and state.

  • scope
  • Return the currently-active scope rules.

  • parentheses

  • Return whether the current state is wrapped in parentheses.

  • subquery

  • Return whether the current state is the child of another query.

  • scopenormal([**kwargs_])

  • The default scope. Sources are referred to by alias, columns bydotted-path from the source.

  • scopesource([**kwargs_])

  • Scope used when defining sources, e.g. in the column list and FROMclause of a SELECT query. This scope is used for defining thefully-qualified name of the source and assigning an alias.

  • scopevalues([**kwargs_])

  • Scope used for UPDATE, INSERT or DELETE queries, where instead ofreferencing a source by an alias, we refer to it directly. Similarly,since there is a single table, columns do not need to be referencedby dotted-path.

  • scopecte([**kwargs_])

  • Scope used when generating the contents of a common-table-expression.Used after a WITH statement, when generating the definition for a CTE(as opposed to merely a reference to one).

  • scopecolumn([**kwargs_])

  • Scope used when generating SQL for a column. Ensures that the column isrendered with it’s correct alias. Was needed because when referencingthe inner projection of a sub-select, Peewee would render the fullSELECT query as the “source” of the column (instead of the query’salias + . + column). This scope allows us to avoid rendering the fullquery when we only need the alias.

  • sql(obj)

  • Append a composable Node object, sub-context, or other object to thequery AST. Python values, such as integers, strings, floats, etc. aretreated as parameterized values.

Returns:The updated Context object.

  • literal(keyword)
  • Append a string-literal to the current query AST.

Returns:The updated Context object.

  • parse(node)

Parameters:node (Node) – Instance of a Node subclass.Returns:a 2-tuple consisting of (sql, parameters).

Convert the given node to a SQL AST and return a 2-tuple consistingof the SQL query and the parameters.

  • query()

Returns:a 2-tuple consisting of (sql, parameters) for the context.