Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 418555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:41:25+00:00 2026-05-12T18:41:25+00:00

We need to produce a fairly complex dynamic query builder for retrieving reports on

  • 0

We need to produce a fairly complex dynamic query builder for retrieving reports on the fly. We’re scratching our heads a little on what sort of data structure would be best.

It’s really nothing more than holding a list of selectParts, a list of fromParts, a list of where criteria, order by, group by, that sort of thing, for persistence. When we start thinking about joins, especially outer joins, having clauses, and aggregate functions, things start getting a little fuzzy.

We’re building it up interfaces first for now and trying to think as far ahead as we can, but definitely will go through a series of refactorings when we discover limitations with our structures.

I’m posting this question here in the hopes that someone has already come up with something that we can base it on. Or know of some library or some such. It would be nice to get some tips or heads-up on potential issues before we dive into implementations next week.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:41:25+00:00Added an answer on May 12, 2026 at 6:41 pm

    I’ve done something similar couple of times in the past. A couple of the bigger things spring to mind..

    • The where clause is the hardest to get right. If you divide things up into what I would call “expressions” and “predicates” it makes it easier.
    • Expressions – column references, parameters, literals, functions, aggregates (count/sum)
    • Predicates – comparisons, like, between, in, is null (predicates have expression as children, e.g. expr1 = expr2. Then you also having composites such as and/or/not.
    • The whole where clause, as you can imagine, is a tree with a predicate at the root, with maybe sub-predicates underneath eventually terminating with expressions at the leaves.
    • To construct the HQL you walk the model (depth first usually). I used a visitor as I need to walk my models for other reasons, but if you don’t have multiple purposes you can build the rendering code right into the model.

    e.g. If you had

    "where upper(column1) = :param1 AND ( column2 is null OR column3 between :param2 and param3)"
    

    Then the tree is

    Root
    - AND
      - Equal
        - Function(upper)
          - ColumnReference(column1)
        - Parameter(param1)
      - OR
        - IsNull
          - ColumnReference(column2)
        - Between
          - ColumnReference(column3)
          - Parameter(param2)
          - Parameter(param3)
    

    Then you walk the tree depth first and merge rendered bits of HQL on the way back up. The upper function for example would expect one piece of child HQL to be rendered and it would then generate

    "upper( " + childHql + " )"

    and pass that up to it’s parent. Something like Between expects three child HQL pieces.

    • You can then re-use the expression model in the select/group by/order by clauses

    • You can skip storing the group by if you wish by just storing the select and before query construction scan for aggregate. If there is one or more then just copy all the non-aggregate select expressions into the group by.

    • From clause is just a list of table reference + zero or more join clauses. Each join clause has a type (inner/left/right) and a table reference. Table reference is a table name + optional alias.

    Plus, if you ever get into wanting to parse a query language (or anything really) then I can highly recommend ANTLR. Learning curve is quite steep but there are plenty of example grammars to look at.

    HTH.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 289k
  • Answers 289k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don;t use GUIDs as PKs but can't you set… May 13, 2026 at 5:39 pm
  • Editorial Team
    Editorial Team added an answer var array = new[] { true, false, false }; var… May 13, 2026 at 5:39 pm
  • Editorial Team
    Editorial Team added an answer Here's a writeup of some reasons: http://phrogz.net/css/WhyTablesAreBadForLayout.html I personally think… May 13, 2026 at 5:39 pm

Related Questions

I am in the processes of replacing the framework for a fairly complex business
We are building a new application in .net 3.5 with SQL server database. The
I'm a big fan of keeping application logic in the servlet, and keeping the
Does anyone have, or know of, a binary patch generation algorithm implementation in C#?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.