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.
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
Parameters:source (Source) – The source for which we set the alias.
- 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. TheState
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 thescope
. - 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.
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.
Return whether the current state is wrapped in parentheses.
Return whether the current state is the child of another query.
The default scope. Sources are referred to by alias, columns bydotted-path from the source.
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.
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.
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).
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.
- 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.
Returns:The updated Context object.
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.
Returns:a 2-tuple consisting of (sql, parameters) for the context.