Mozhi
Mozhi

Reputation: 827

How to express SQL queries in yaml format?

I am experimenting with some random idea of end-user expressing a query in yaml format. This yaml file would be fed into some intermediate transformer to convert yaml to SQL statement.

Following is sample yaml snippet, but when I think of having conditions, expressions, subqueries, joins. And the yaml becomes more complex as queries tends to be more complex.

Question: how to define a SQL query in yaml format and yet cover all the scenarios? (Are there any existing libraries that can do this kind of magic)

table:
    schema: test
    name: test_table
  select:
    columns:
      - column: * # retrieve all columns     
  where:
    columns:
      - column: user_id
      - column: customer_id
  limit:
    value: 10
  # groupBy
  # orderBy

Upvotes: 5

Views: 15978

Answers (1)

flyx
flyx

Reputation: 39708

The answer to your question is: You serialize the syntax tree of the SQL statement to YAML.

Have a quick look at pg_query to get an impression of how complex this tree looks even for simple queries (this tool uses Postgres' SQL parser to generate a syntax tree of an SQL statement). This complexity is required for covering all the scenarios.

Now you can of course try and simplify this structure in a way that it retains all information necessary to reconstruct the original syntax tree. And this is exactly what the SQL query language does! No user wants to write the explicit structure down when they can use a well-defined and well-supported syntax for expressing the query.

You would basically create an entirely new, YAML-based language somewhere between the syntax tree and the original SQL language. Using it will require the user to know both YAML and the semantics of your structure. So if your goal is to enhance user-friendliness, this is very unlikely to be a way of achieving it.

Upvotes: 6

Related Questions