The C Pre processor (cpp) has historically been linked to C (hence the name), but it is really a generic word processor that can be used (or abused) for something else.
Consider this file named location.src (more on this later).
// C++ style comments works here
and the debug.src file, which is included:
#ifdef HAVE_DEBUG #define ASSERT(C, T) \ begin \ if (not (C)) then \ begin \ declare my_msg varchar(1000); \ set my_msg = concat("Assert failed, file:", __FILE__, \ ", line: ", __LINE__, \ ", condition ", #C, \ ", text: ", T); \ signal sqlstate "HY000" set message_text = my_msg; \ end; \ end if; \ end #else #define ASSERT(C, T) begin end #endif
When compiling with:
cpp -E location.src -o location.sql
you will get the code you are looking for with cpp extending #define values.
When compiling with:
cpp -E -DHAVE_DEBUG location.src -o location.sql
you get the same, plus the ASSERT macro (posted as a bonus to show what you can do).
Assuming that the assembly with HAVE_DEBUG is deployed in a test environment (version 5.5 or later since using SIGNAL), the result is as follows:
mysql> call AddLocation("Here"); +-----------+ | Hi there. | +-----------+ | Hi there. | +-----------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> call AddLocation(""); ERROR 1644 (HY000): Assert failed, file:location.src, line: 24, condition length(location) > 0, text: lost or something ?
Note how the file name, line number, and conditions are displayed right in the source code location in location.src where the statement is executed, thanks again to processor C.
Now about the .src file extension:
- you can use anything.
- Having a different file extension helps with make files, etc. and prevents confusion.
EDIT: Originally published as .xql, renamed to .src for clarity. Nothing related to xml requests here.
As with any tool, using cpp can lead to good things, and the use case for supporting LOCATION_LEN in portable mode seems very reasonable. It can also lead to bad things, with too many #include, #ifdef nested admins, macros, etc. that obfuscate the code at the end, so your mileage may vary.
With this answer you will get everything ( #define
, #include
, #ifdef
, __FILE__
, __LINE__
, #C
, command line options for assembly), so I hope he owes it all.