From 340c14e0b2881288290b60a05ee7223281f65db8 Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Tue, 6 May 2014 14:51:48 +0300 Subject: [PATCH] Fix docs --- docs/data_types.rst | 2 +- docs/utility_classes.rst | 5 +- sqlalchemy_utils/query_chain.py | 134 ++++++++++++++++++-------------- 3 files changed, 81 insertions(+), 60 deletions(-) diff --git a/docs/data_types.rst b/docs/data_types.rst index 0bf9f92..a8bd143 100644 --- a/docs/data_types.rst +++ b/docs/data_types.rst @@ -59,7 +59,7 @@ LocaleType IPAddressType ^^^^^^^^^^^^^ -.. module:: sqlalchemy_utils.types.ip_addresss +.. module:: sqlalchemy_utils.types.ip_address .. autoclass:: IPAddressType diff --git a/docs/utility_classes.rst b/docs/utility_classes.rst index 3764973..504b7cb 100644 --- a/docs/utility_classes.rst +++ b/docs/utility_classes.rst @@ -4,7 +4,10 @@ Utility classes QueryChain ---------- -.. module:: sqlalchemy_utils.query_chain +.. automodule:: sqlalchemy_utils.query_chain + +API +^^^ .. autoclass:: QueryChain :members: diff --git a/sqlalchemy_utils/query_chain.py b/sqlalchemy_utils/query_chain.py index 20ecfbd..6b879bd 100644 --- a/sqlalchemy_utils/query_chain.py +++ b/sqlalchemy_utils/query_chain.py @@ -1,3 +1,79 @@ +""" +QueryChain is a wrapper for sequence of queries. + + +Features: + + * Easy iteration for sequence of queries + * Limit and offset which are applied to all queries in the chain + * Smart __getitem__ support + + +Initialization +^^^^^^^^^^^^^^ + +QueryChain takes iterable of queries as first argument. Additionally limit and +offset parameters can be given + +:: + + chain = QueryChain([session.query(User), session.query(Article)]) + + chain = QueryChain( + [session.query(User), session.query(Article)], + limit=4 + ) + + +Simple iteration +^^^^^^^^^^^^^^^^ +:: + + chain = QueryChain([session.query(User), session.query(Article)]) + + for obj in chain: + print obj + + +Limit and offset +^^^^^^^^^^^^^^^^ + +Lets say you have 5 blog posts, 5 articles and 5 news items in your +database. + +:: + + chain = QueryChain( + [ + session.query(BlogPost), + session.query(Article), + session.query(NewsItem) + ], + limit=5 + ) + + list(chain) # all blog posts but not articles and news items + + + chain.offset = 4 + list(chain) # last blog post, and first four articles + + +Chain slicing +^^^^^^^^^^^^^ + +:: + + chain = QueryChain( + [ + session.query(BlogPost), + session.query(Article), + session.query(NewsItem) + ] + ) + + chain[3:6] # New QueryChain with offset=3 and limit=6 +""" from copy import copy @@ -5,70 +81,12 @@ class QueryChain(object): """ QueryChain can be used as a wrapper for sequence of queries. - Features: - - * Easy iteration for sequence of queries - * Limit and offset which are smartly applied to all queries - * Smart __getitem__ support - :param queries: A sequence of SQLAlchemy Query objects :param limit: Similar to normal query limit this parameter can be used for limiting the number of results for the whole query chain. :param offset: Similar to normal query offset this parameter can be used for offsetting the query chain as a whole. - - Simple iteration - ^^^^^^^^^^^^^^^^ - :: - - chain = QueryChain([session.query(User), session.query(Article)]) - - for obj in chain: - print obj - - - Limit and offset - ^^^^^^^^^^^^^^^^ - - Lets say you have 5 blog posts, 5 articles and 5 news items in your - database. - - :: - - chain = QueryChain( - [ - session.query(BlogPost), - session.query(Article), - session.query(NewsItem) - ], - limit=5 - ) - - list(chain) # all blog posts but not articles and news items - - - chain.offset = 4 - list(chain) # last blog post, and first four articles - - - Chain slicing - ^^^^^^^^^^^^^ - - :: - - chain = QueryChain( - [ - session.query(BlogPost), - session.query(Article), - session.query(NewsItem) - ] - ) - - chain[3:6] # New QueryChain with offset=3 and limit=6 - - - .. versionadded: 0.26.0 """ def __init__(self, queries, limit=None, offset=None):