Skip to content

Use raw string literals for SQL queries

Clang-format removes any indentation we have for the SQL queries, because it will align all the multi-line strings. There is no easy way to disable this without also disabling a whole bunch of other cases.

A good solution would be to replace these strings by raw string literals as follows.

old:

  const char *const sql =
    "SELECT "
      "CTA_CATALOGUE.SCHEMA_VERSION_MAJOR AS SCHEMA_VERSION_MAJOR,"
      "CTA_CATALOGUE.SCHEMA_VERSION_MINOR AS SCHEMA_VERSION_MINOR "
    "FROM "
      "CTA_CATALOGUE";

updated:

  const char *const sql = R"SQL(
    SELECT
      CTA_CATALOGUE.SCHEMA_VERSION_MAJOR AS SCHEMA_VERSION_MAJOR,
      CTA_CATALOGUE.SCHEMA_VERSION_MINOR AS SCHEMA_VERSION_MINOR 
    FROM 
      CTA_CATALOGUE
  )SQL";

This has several advantages:

  • It fixes the clang-format issues
  • It is more readable, because it does not require quotes on every line
  • It is less prone to errors (i.e. forgetting a whitespace before the quote at the end of a line)
  • IDEs should support custom highlighting of the strings based on the delimiter (SQL in the above case)

This should be done before disallowing failures in the clang-format pipeline. Blocks #757 (closed)