TransWikia.com

SOQL Parser implementation in Apex

Salesforce Asked by Robert Sösemann on October 4, 2021

In my app, I get passed in user-generated SOQL query strings and need to validate and modify them. For that, I would need a parser that can extract me chunks They contain many SOQL features like:

  • Query Parent Object fields
  • Query Child records
  • Conditions in own and child relationships
  • Aggregate functions

I tried to:

  1. "hack my own parser" and FAILED
  2. tried to port an open-source parser from Java to Apex and FAILED

Now I need your help to eighter point me to an existing solution that I didn’t find yet or help me understand how to make 1) or 2) work.

Please also don’t close-vote this question as too broad. Nobody is harmed when we leave it to hang around. Eventually, someone will be motivated by it to write such a parser.

One Answer

I would really advise you not to do this.

Apex in its current form is really not suited for parsing and - more importantly - user-generated SOQL strings is a big security red flag. I know it's probably not the answer you wanted, but my advice would be to have a user-generated object-representation of the query (for example, an object representation of the fields, the objects, the filters, etc) and generate the SOQL downstream. This would avoid the parsing problem entirely and let you handle security robustly.

Plus, it's a fun object-oriented design exercise.

Correct answer by Lorenzo Frattini on October 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP