Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
 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)
Esempio n. 6
0
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)
Esempio n. 7
0
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)
Esempio n. 8
0
def q_all_books(**kwargs):
    dq = DQ("(b.id, b.name) Book b")
    a = []
    for rec in dq.tuples():
        a.append(rec)
Esempio n. 9
0
def q_avg_price(**kwargs):
    dq = DQ("(avg(b.price)) Book b")
    a = []
    for rec in dq.tuples():
        a.append(rec)
Esempio n. 10
0
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)
Esempio n. 11
0
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)