buggins / hibernated

HibernateD is ORM for D language (similar to Hibernate)
82 stars 31 forks source link

Proposal: Implement HQL Query Parsing via Parsing Expression Grammar (PEG) #92

Open vnayar opened 4 months ago

vnayar commented 4 months ago

Motivation: I was looking over the code for processing HQL queries in https://github.com/buggins/hibernated/blob/master/source/hibernated/query.d because I was looking to see how difficult it would be to add support for the DELETE statement, which is useful because it can have a major performance boost over deleting a single object at a time when there is a large number of objects. I realized that the code in query.d is very much built around SELECT statements, and changing it to support other kinds of statements would be difficult, error-prone, and hard to maintain.

Idea: Replace the existing HQL query parsing with one backed by a Parsing Expression Grammar.

Advantages include:

  1. Easy to expand and adopt to new types of statements with little effort.
  2. Cleanly split parsing and interpretation for easier code testing and maintenance.
  3. Automatically gain error detection pointing out the character position of any mistake.

Most of the initial work would be to make a clean separation between the parsing logic and the SQL query generation logic for the current "SELECT" statements. Once that work is done, then it will be much easier to expand the parsing to support new statement types like "UPDATE" or "DELETE", and then add a modular SQL query generator based on the parsing results.

I've used a D library in other projects to generate a PEG parser: https://code.dlang.org/packages/pegged

If I were to make a larger overhaul like this and submit a PR, would it be considered?

SingingBush commented 4 months ago

I think it would be good and to do. potentially separated either as a sub package or a feature so that end users can easily choose to include it or not. I like the separation of using a parent dub file with multiple sub projects but this is just my personal preference. I've not been using D recently so it would be good to get some input from other users or perhaps @buggins.