def q_sub_queryset(**kwargs): qs = Book.objects.filter(name__startswith="B").only("id") dq = DQ("(b.name, b.price) Book{id in '@qs_sub'} b", names={"qs_sub": qs}) a = [] for rec in dq.tuples(): a.append(rec)
def q_diff_avg_price(**kwargs): a = [] dq = DQ(""" (Publisher.name, max(Book.price) - avg(Book.price) as price_diff) Book b """) for rec in dq.tuples(): a.append(rec)
def q_rewind(**kwargs): dq = DQ("(b.name) Book b") a = [] for rec in dq.tuples(): a.append(rec) a = [] for rec in dq.rewind().tuples(): a.append(rec)
def q_subquery(**kwargs): pubs = DQ("(p.id) Publisher p", name="pubs") # noqa: F841 books = DQ("(b.name) Book{publisher in '@pubs'} b") if kwargs.get("sql"): print(books.query()) a = [] for rec in books.tuples(): a.append(rec)
def handle(self, *args, **options): q = DQ( options.get("src"), limit=options.get("limit"), offset=options.get("offset"), verbosity=options.get("verbosity"), ) if options.get("format") == "dicts": print(json.dumps(list(q.dicts()), cls=DjangoJSONEncoder, indent=4)) else: for rec in getattr(q, options.get("format"))(): print(rec)
def q_grouping(**kwargs): dq = DQ( "(b.id, b.name) Book{(b.id == 1 or b.id == 2) and b.id == 3} b ", verbosity=kwargs["verbose"], ) qs = Book.objects.filter(name__startswith="B").only("id") [rec.id for rec in qs][:3] a = [] for rec in dq.limit(10).tuples(): a.append(rec)
def q_sub_list(**kwargs): qs = Book.objects.filter(name__startswith="B").only("id") ids = [rec.id for rec in qs] dq = DQ( "(b.name, b.price) Book{id in '@qs_sub'} b", names={"qs_sub": ids}, verbosity=kwargs.get("verbose"), ) a = [] for rec in dq.tuples(): a.append(rec)
def q_all_books(**kwargs): dq = DQ("(b.id, b.name) Book b") a = [] for rec in dq.tuples(): a.append(rec)
def q_avg_price(**kwargs): dq = DQ("(avg(b.price)) Book b") a = [] for rec in dq.tuples(): a.append(rec)
def q_books_avg_min_max(**kwargs): a = [] dq = DQ("(avg(b.price), max(b.price), min(b.price)) Book b") for rec in dq.tuples(): a.append(rec)
def q_books_per_publisher(**kwargs): a = [] dq = DQ("(Publisher.name, count(Book.id) as num_books) Book b") for rec in dq.tuples(): a.append(rec)