Skip to content

Commit

Permalink
feat: introduce infoSchemaMarkdown SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
shah committed Oct 16, 2023
1 parent ee44f8a commit 7fec826
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions pattern/content-aide/notebook.sqla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,75 @@ export function library<EmitContext extends SQLa.SqlEmitContext>(libOptions: {
m.type = 'table';`),
};

const infoSchemaMarkdown = SQL()`
WITH TableInfo AS (
SELECT
m.tbl_name AS table_name,
CASE WHEN c.pk THEN '*' ELSE '' END AS is_primary_key,
c.name AS column_name,
c."type" AS column_type,
CASE WHEN c."notnull" THEN '*' ELSE '' END AS not_null,
COALESCE(c.dflt_value, '') AS default_value,
COALESCE((SELECT pfkl.'table' || '.' || pfkl.'to' FROM pragma_foreign_key_list(m.tbl_name) AS pfkl WHERE pfkl.'from' = c.name), '') as fk_refs,
ROW_NUMBER() OVER (PARTITION BY m.tbl_name ORDER BY c.cid) AS row_num
FROM sqlite_master m JOIN pragma_table_info(m.tbl_name) c ON 1=1
WHERE m.type = 'table'
ORDER BY table_name, row_num
),
Views AS (
SELECT '## Views ' AS markdown_output
UNION ALL
SELECT '| View | Column | Type |' AS markdown_output
UNION ALL
SELECT '| ---- | ------ |----- |' AS markdown_output
UNION ALL
SELECT '| ' || tbl_name || ' | ' || c.name || ' | ' || c."type" || ' | '
FROM
sqlite_master m,
pragma_table_info(m.tbl_name) c
WHERE
m.type = 'view'
),
Indexes AS (
SELECT '## Indexes' AS markdown_output
UNION ALL
SELECT '| Table | Index | Columns |' AS markdown_output
UNION ALL
SELECT '| ----- | ----- | ------- |' AS markdown_output
UNION ALL
SELECT '| ' || m.name || ' | ' || il.name || ' | ' || group_concat(ii.name, ', ') || ' |' AS markdown_output
FROM sqlite_master as m,
pragma_index_list(m.name) AS il,
pragma_index_info(il.name) AS ii
WHERE
m.type = 'table'
GROUP BY
m.name,
il.name
)
SELECT
markdown_output AS markdown_result
FROM
(
SELECT '## Tables' AS markdown_output
UNION ALL
SELECT
CASE WHEN ti.row_num = 1 THEN '
### \`' || ti.table_name || '\` Table
| PK | Column | Type | Req? | Default | References |
| -- | ------ | ---- | ---- | ------- | ---------- |
' ||
'| ' || is_primary_key || ' | ' || ti.column_name || ' | ' || ti.column_type || ' | ' || ti.not_null || ' | ' || ti.default_value || ' | ' || ti.fk_refs || ' |'
ELSE
'| ' || is_primary_key || ' | ' || ti.column_name || ' | ' || ti.column_type || ' | ' || ti.not_null || ' | ' || ti.default_value || ' | ' || ti.fk_refs || ' |'
END
FROM TableInfo ti
UNION ALL SELECT ''
UNION ALL SELECT * FROM Views
UNION ALL SELECT ''
UNION ALL SELECT * FROM Indexes
);`;

const mimeTypesSeedDML = () => {
const { loadExtnSQL: load } = libOptions;
return SQL()`
Expand Down Expand Up @@ -606,6 +675,7 @@ export function library<EmitContext extends SQLa.SqlEmitContext>(libOptions: {
optimalOpenDB,
optimalCloseDB,
inspect,
infoSchemaMarkdown,
deviceDML,
mimeTypesSeedDML,
insertContent,
Expand Down

0 comments on commit 7fec826

Please sign in to comment.