WITH table_columns AS (
    SELECT m.tbl_name AS table_name,
           group_concat(c.name) AS column_names_for_select,
           json_group_array(c.name) AS column_names_for_atc_json
      FROM sqlite_master m,
           pragma_table_info(m.tbl_name) c
     WHERE m.type = 'table'
  GROUP BY m.tbl_name
),
target AS (
  -- set SQLite parameter :osquery_atc_path to assign a different path
  SELECT COALESCE(SELECT "value" FROM "session_state_ephemeral" WHERE "key" = 'infoSchemaOsQueryATCs_path';, 'No infoSchemaOsQueryATCs_path argument supplied in session_state_ephemeral') AS path
),
table_query AS (
    SELECT table_name,
           'SELECT ' || column_names_for_select || ' FROM ' || table_name AS query,
           column_names_for_atc_json
      FROM table_columns
)
SELECT json_object('auto_table_construction',
          json_group_object(
              table_name,
              json_object(
                  'query', query,
                  'columns', json(column_names_for_atc_json),
                  'path', path
              )
          )
       ) AS osquery_auto_table_construction
  FROM table_query, target;