1

I am in the process of implementing a program that applies some transformations to SQL code. When it comes to parsing said code, I thought about two approaches.

  1. Implementing a "standard" parser using ordinary functions
  2. Implementing a Reader macro that does such interpretation at read-time.

I'd like to know if implementing a reader macro is valid for this case or I'm better off writing it with usual functions and avoid killing a insect with a bazooka.

2
  • 4
    If your program is taking the SQL code as an input (rather than there being hard-coded SQL literals in the source), reader macros are not applicable. In any case, reader macros would just be a layer on top of the parser implemented with functions, so start with that and worry about macros (of any kind) after you have the actual logic ready. Commented Apr 18, 2019 at 17:41
  • 3
    If you are thinking about a reader macro because you want sql in your source code, why not skip the parsing step and write a more structured program (e.g. (select (foo bar (named average-baz (/ (sum baz) (count baz)))) :from (join things bits :on bit-id :kind :left) :group-by wazoo :sort-by (:ascending bing) :where (> quality 7)), or even some representation that is easier for you. If you want to read sql then you should write a proper parser. You say “killing an insect with a bazooker” but in fact reader macros are often painful and weak parsers best suited to simple tasks. Commented Apr 18, 2019 at 20:56

1 Answer 1

2

You can use a reader macro as a way to inline SQL code if you want, like

#[SQL code here]

having that call a function like (sql "SQL code here"), but you won't be able to do something as complex as writing a full SQL interpreter with reader macros. Besides, how could you access the database at runtime (when you will probably need it) if you did all that at read time?

Another approach is to create a lispy SQL DSL, where you could use regular functions and macros.

Sign up to request clarification or add additional context in comments.

3 Comments

Maybe you could start using the existing cl-sql or postmodern libraries.
That is probably the best thing to do.
Oh I realize I posted the comment under your answer, that was expected to be for OP sorry.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.