0

Background

Currently I'm using C++ MySQL connector to communicate with a database, and sometimes I need to send hardcoded commands through connection. As did many times, I make errors/typos and whatnot. This is for a very basic Database Systems class, so no industry involved.

Question

Is is possible to implement constexpr compiler for another language that would just run lexical, syntax, and semantic analyzis and report errors if it found them? Or maybe some additional compilation step?

Example

Lets suppose I wanted to send this command:

SELECT * FROM Persons

but instead, I forgot to type M:

SELECT * FRO Persons

I'd discover the problem at runtime, which could be caught by the compiler if it knew the language. My only idea to solve this is preprocessor madness.

From C++, I would call it like this:

auto statement = sql_parse("...");

and hopefully it should cause compilation error if something is wrong.

Incomputable
  • 2,188
  • 1
  • 20
  • 40
  • It is best to check quires in their natural environment which has the best error checking. – jdweng Sep 09 '17 at 16:20
  • _"which could be caught by the compiler if it knew the language"_ How do you expect the c++ compiler about knowing specifics of the SQL language?? – user0042 Sep 09 '17 at 16:20
  • Do you mean you have SQL queries in strings in your c++ code and want the c++ compiler to find SQL syntax errors inside them? – Yunnosch Sep 09 '17 at 16:20
  • I think you can't, according to this: https://stackoverflow.com/questions/7508475/is-c-preprocessor-metaprogramming-turing-complete – lilezek Sep 09 '17 at 16:20
  • @user0042, it would know it through `constexpr` compiler that I would write myself, if it would be possible. – Incomputable Sep 09 '17 at 16:21
  • @Yunnosch, yes. But I'd supply constexpr compiler myself. – Incomputable Sep 09 '17 at 16:22
  • 2
    Embedding SQL is normally done via a preprocessor of some sort (not usually the C preprocessor) - see https://en.wikipedia.org/wiki/Embedded_SQL –  Sep 09 '17 at 16:22
  • @lilezek, thanks for useful link – Incomputable Sep 09 '17 at 16:40
  • @NeilButterworth, thanks. I'll try to find out more about it, but right now I'm more inclined towards adding additional compilation step through CMake. – Incomputable Sep 09 '17 at 16:40
  • if it does not have to be exact SQL syntax, maybe something along [SQLpp11](https://github.com/rbock/sqlpp11) could work for you – sp2danny Sep 09 '17 at 17:49

1 Answers1

3

Is is possible to implement constexpr compiler for another language that would just run lexical, syntax, and semantic analyzis and report errors if it found them? Or maybe some additional compilation step?

No that's not possible at compile time using only the C-preprocessor/C++ compiler.

You'll need to implement a different tool to parse and validate the SQL code and generate the necessary C++ code for the SQL bindings.

auto statement = sql_parse("...");

will need a parser that inspects the SQL statement at runtime.


There are C++ template tools like boost::spirit, that allow you to integrate DSLs (like SQL syntax) at compile time though. That's way beyond what the C-preprocessor does.


For an easy and practical solution I'd recommend you test your SQL statements separately with an appropriate tool before adopting it into the C++ code.

user0042
  • 7,917
  • 3
  • 24
  • 39
  • What about things like `""_sql`, so it would create `constexpr` object, which the function could consume and be `constexpr` itself? – Incomputable Sep 09 '17 at 16:25
  • I can't think about a way without an additional _preprocessor_ which is specialized about catching inlined SQL syntax. I roughly remember about embedded INFORMIX statements with c/c++ code, which required extra stuff. – user0042 Sep 09 '17 at 16:28
  • What about adding additional compilation step through something like CMake? It would copy the specifically marked string, run compiler, and then crash with some meaningful error? – Incomputable Sep 09 '17 at 16:30
  • @Incomputable _"What about adding additional compilation step"_ That's exactly what I'm talking about. You'll need some extra parsing and code generation regarding the SQL binding stuff. Nothing the c-preprocessor or c++ compiler will do. – user0042 Sep 09 '17 at 16:34
  • I've edited my question, could you please slightly adjust it so I would just upvote and accept it? – Incomputable Sep 09 '17 at 16:37
  • 2
    I’m pretty sure the answer is wrong. C++ templates are Turing complete. This implies that you could almost certainly *theoretically* implement this. Just probably not in a practical way. – Konrad Rudolph Sep 09 '17 at 16:45
  • @KonradRudolph I could think of an implementation using `boost::spirit` maybe, something general that supports DSLs. Should I add that? (BTW I always appreciate discussing with other _"greybeards"_ like you ;-) ) – user0042 Sep 09 '17 at 16:48
  • @Konrad /OT Did you participate in the `comp.lang.c++.moderated` user group these ages ago? I have a feeling I know you well. – user0042 Sep 09 '17 at 17:34
  • @user0042 Probably not, I’ve never been a big contributor on usenet. I used to be somewhat active on a German C++ forum ages ago. Other than that, mostly here. – Konrad Rudolph Sep 09 '17 at 18:32