|
| 1 | +CREATE TABLE reference_category ( |
| 2 | + id TEXT PRIMARY KEY, |
| 3 | + title TEXT NOT NULL, |
| 4 | + sort_order INTEGER NOT NULL |
| 5 | +); |
| 6 | + |
| 7 | +CREATE TABLE config_option ( |
| 8 | + name TEXT PRIMARY KEY, |
| 9 | + type TEXT NOT NULL, |
| 10 | + default_md TEXT, |
| 11 | + description_md TEXT NOT NULL |
| 12 | +); |
| 13 | + |
| 14 | +CREATE TABLE component ( |
| 15 | + name TEXT PRIMARY KEY, |
| 16 | + description TEXT NOT NULL, |
| 17 | + icon TEXT, |
| 18 | + introduced_in_version TEXT, |
| 19 | + catalog_category TEXT REFERENCES reference_category(id), |
| 20 | + catalog_summary TEXT |
| 21 | +); |
| 22 | + |
| 23 | +CREATE TABLE parameter_type (name TEXT PRIMARY KEY); |
| 24 | +INSERT INTO parameter_type VALUES |
| 25 | + ('BOOLEAN'), ('COLOR'), ('HTML'), ('ICON'), ('INTEGER'), ('JSON'), ('REAL'), ('TEXT'), ('TIMESTAMP'), ('URL'); |
| 26 | + |
| 27 | +CREATE TABLE parameter ( |
| 28 | + component TEXT NOT NULL REFERENCES component(name), |
| 29 | + top_level BOOLEAN NOT NULL, |
| 30 | + name TEXT NOT NULL, |
| 31 | + type TEXT NOT NULL REFERENCES parameter_type(name), |
| 32 | + description_md TEXT NOT NULL, |
| 33 | + optional BOOLEAN NOT NULL, |
| 34 | + PRIMARY KEY (component, top_level, name) |
| 35 | +); |
| 36 | + |
| 37 | +CREATE TABLE example ( |
| 38 | + component TEXT NOT NULL REFERENCES component(name), |
| 39 | + description TEXT NOT NULL, |
| 40 | + properties JSON |
| 41 | +); |
| 42 | + |
| 43 | +CREATE TABLE sqlpage_functions ( |
| 44 | + name TEXT PRIMARY KEY, |
| 45 | + icon TEXT, |
| 46 | + description_md TEXT NOT NULL, |
| 47 | + return_type TEXT, |
| 48 | + introduced_in_version TEXT, |
| 49 | + catalog_category TEXT REFERENCES reference_category(id), |
| 50 | + catalog_summary TEXT |
| 51 | +); |
| 52 | + |
| 53 | +CREATE TABLE sqlpage_function_parameters ( |
| 54 | + "function" TEXT NOT NULL REFERENCES sqlpage_functions(name), |
| 55 | + "index" INTEGER NOT NULL, |
| 56 | + name TEXT NOT NULL, |
| 57 | + description_md TEXT NOT NULL, |
| 58 | + type TEXT NOT NULL, |
| 59 | + optional BOOLEAN NOT NULL, |
| 60 | + PRIMARY KEY ("function", "index"), |
| 61 | + UNIQUE ("function", name) |
| 62 | +); |
| 63 | + |
| 64 | +CREATE TABLE blog_posts ( |
| 65 | + title TEXT PRIMARY KEY, |
| 66 | + description TEXT NOT NULL, |
| 67 | + icon TEXT NOT NULL, |
| 68 | + external_url TEXT, |
| 69 | + content TEXT, |
| 70 | + created_at TEXT NOT NULL |
| 71 | +); |
| 72 | + |
| 73 | +CREATE TABLE guide ( |
| 74 | + sort_order INTEGER PRIMARY KEY, |
| 75 | + title TEXT NOT NULL, |
| 76 | + link TEXT NOT NULL UNIQUE, |
| 77 | + description TEXT NOT NULL |
| 78 | +); |
| 79 | + |
| 80 | +CREATE TABLE example_cards ( |
| 81 | + title TEXT NOT NULL, |
| 82 | + folder TEXT NOT NULL, |
| 83 | + db_engine TEXT, |
| 84 | + description TEXT NOT NULL |
| 85 | +); |
| 86 | + |
| 87 | +-- The reference shows the SQL and renders the same stored component rows. |
| 88 | +-- Arrays with several items become repeated column aliases, as in handwritten SQLPage files. |
| 89 | +CREATE VIEW example_with_source AS |
| 90 | +SELECT example.rowid AS example_id, |
| 91 | + row_number() OVER (PARTITION BY component ORDER BY example.rowid) AS example_number, |
| 92 | + example.*, |
| 93 | + (SELECT group_concat('select' || char(10) || ( |
| 94 | + WITH fields AS ( |
| 95 | + SELECT field.key AS name, coalesce(item.type, field.type) AS type, |
| 96 | + CASE WHEN item.key IS NULL THEN field.value ELSE item.value END AS value |
| 97 | + FROM json_each(statement.value) AS field |
| 98 | + LEFT JOIN json_each( |
| 99 | + CASE WHEN field.type = 'array' AND json_array_length(field.value) > 1 THEN field.value END |
| 100 | + ) AS item ON true |
| 101 | + ) |
| 102 | + SELECT group_concat(' ' || CASE type |
| 103 | + WHEN 'true' THEN 'TRUE' |
| 104 | + WHEN 'false' THEN 'FALSE' |
| 105 | + WHEN 'array' THEN 'JSON(' || quote(value) || ')' |
| 106 | + WHEN 'object' THEN 'JSON(' || quote(value) || ')' |
| 107 | + ELSE quote(value) |
| 108 | + END || format(' as "%w"', name), ',' || char(10)) |
| 109 | + FROM fields |
| 110 | + ) || ';', char(10) || char(10)) FROM json_each(example.properties) AS statement) AS source |
| 111 | +FROM example; |
| 112 | + |
| 113 | +-- The catalog, topic navigation, and reference pages use the same entries and links. |
| 114 | +CREATE VIEW reference_entry AS |
| 115 | +WITH entries AS ( |
| 116 | + SELECT 'component' AS kind, name, icon, catalog_category, catalog_summary, |
| 117 | + description AS description_md, introduced_in_version, NULL AS return_type, |
| 118 | + '/component.sql?component=' || name AS link, |
| 119 | + (SELECT count(*) FROM example WHERE example.component = component.name) AS example_count, |
| 120 | + NULL AS arguments |
| 121 | + FROM component |
| 122 | + UNION ALL |
| 123 | + SELECT 'function', name, icon, catalog_category, catalog_summary, |
| 124 | + description_md, introduced_in_version, return_type, '/functions.sql?function=' || name, NULL, |
| 125 | + coalesce((SELECT group_concat(CASE WHEN optional THEN '[' || name || ']' ELSE name END, ', ' ORDER BY "index") |
| 126 | + FROM sqlpage_function_parameters WHERE "function" = f.name), '') |
| 127 | + FROM sqlpage_functions AS f |
| 128 | +) |
| 129 | +SELECT kind, name, icon, link, description_md, introduced_in_version, return_type, example_count, arguments, |
| 130 | + coalesce(category.id, 'other') AS category_id, |
| 131 | + coalesce(category.title, 'Other') AS category_title, |
| 132 | + coalesce(category.sort_order, 99) AS category_order, |
| 133 | + coalesce(catalog_summary, substr(trim(description_md, char(10) || ' #'), 1, |
| 134 | + instr(trim(description_md, char(10) || ' #') || char(10), char(10)) - 1)) AS summary |
| 135 | +FROM entries |
| 136 | +LEFT JOIN reference_category AS category ON category.id = entries.catalog_category; |
| 137 | + |
| 138 | +-- The section is part of the anchor: a component can accept the same name at both levels. |
| 139 | +CREATE VIEW reference_parameter AS |
| 140 | +WITH fields AS ( |
| 141 | + SELECT 'component' AS kind, component AS entry, |
| 142 | + CASE WHEN top_level THEN 'top-level-parameters' ELSE 'row-level-parameters' END AS section, |
| 143 | + name, type, optional, description_md, optional AS position |
| 144 | + FROM parameter |
| 145 | + UNION ALL |
| 146 | + SELECT 'function', "function", 'parameters', name, type, optional, description_md, "index" |
| 147 | + FROM sqlpage_function_parameters |
| 148 | +), sections(section, title, introduction, section_order) AS (VALUES |
| 149 | + ('top-level-parameters', 'Top-level parameters', 'Set these in the SELECT that names the component.', 1), |
| 150 | + ('row-level-parameters', 'Row-level parameters', 'Return these columns in the rows that follow the component.', 2), |
| 151 | + ('parameters', 'Parameters', NULL, 1) |
| 152 | +) |
| 153 | +SELECT fields.*, title, introduction, section_order, section || '-' || name AS anchor |
| 154 | +FROM fields JOIN sections USING (section); |
0 commit comments