pgkiln 0.45.0 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/db/migrations/106_form_designer.sql +102 -0
  2. package/db/migrations/107_app_version.sql +9 -0
  3. package/db/migrations/108_sql_history.sql +25 -0
  4. package/docs/apex-feature-parity.md +33 -4
  5. package/docs/guide/03-builder.md +123 -28
  6. package/docs/guide/04-pages-and-regions.md +74 -2
  7. package/docs/guide/05-items.md +14 -2
  8. package/docs/guide/08-security.md +1 -1
  9. package/docs/guide/09-reference.md +1 -1
  10. package/docs/guide/10-tutorial.md +1 -1
  11. package/docs/guide/11-from-apex.md +2 -1
  12. package/docs/guide/12-development.md +26 -3
  13. package/docs/guide/16-files.md +2 -2
  14. package/examples/hr/hr_61_form_designer.sql +80 -0
  15. package/package.json +1 -1
  16. package/public/app.css +62 -2
  17. package/public/app.js +244 -35
  18. package/public/builder.css +21 -1
  19. package/public/builder.js +67 -0
  20. package/public/sw.js +2 -1
  21. package/public/worksheet.css +244 -0
  22. package/public/worksheet.js +665 -0
  23. package/src/app.ts +9 -1
  24. package/src/builder/ai-builder.ts +3 -3
  25. package/src/builder/appwizard.ts +22 -6
  26. package/src/builder/arrange.ts +33 -12
  27. package/src/builder/code-editor.ts +1 -1
  28. package/src/builder/components.ts +1 -1
  29. package/src/builder/designer.ts +44 -17
  30. package/src/builder/errors.ts +65 -0
  31. package/src/builder/form-designer.ts +229 -0
  32. package/src/builder/locks.ts +6 -0
  33. package/src/builder/newapp.ts +2 -1
  34. package/src/builder/object-actions.ts +166 -0
  35. package/src/builder/object-browser.ts +111 -33
  36. package/src/builder/querybuilder.ts +1 -1
  37. package/src/builder/region-help.ts +2 -0
  38. package/src/builder/region-settings.ts +2 -0
  39. package/src/builder/reporter.ts +1 -1
  40. package/src/builder/routes.ts +7 -5
  41. package/src/builder/sampledata.ts +2 -2
  42. package/src/builder/scripts.ts +3 -3
  43. package/src/builder/search.ts +3 -3
  44. package/src/builder/shared.ts +2 -2
  45. package/src/builder/sql.ts +15 -73
  46. package/src/builder/table-editor.ts +219 -0
  47. package/src/builder/ui.ts +5 -3
  48. package/src/builder/unload.ts +8 -4
  49. package/src/builder/worksheet.ts +569 -0
  50. package/src/builder/workspaces.ts +1 -1
  51. package/src/cli/replace.ts +10 -0
  52. package/src/i18n/ar.ts +62 -0
  53. package/src/i18n/cs.ts +62 -0
  54. package/src/i18n/da.ts +62 -0
  55. package/src/i18n/de.ts +62 -0
  56. package/src/i18n/el.ts +62 -0
  57. package/src/i18n/es.ts +62 -0
  58. package/src/i18n/fi.ts +62 -0
  59. package/src/i18n/fr.ts +62 -0
  60. package/src/i18n/he.ts +62 -0
  61. package/src/i18n/it.ts +62 -0
  62. package/src/i18n/ja.ts +62 -0
  63. package/src/i18n/ko.ts +62 -0
  64. package/src/i18n/nb.ts +62 -0
  65. package/src/i18n/pl.ts +62 -0
  66. package/src/i18n/pt.ts +62 -0
  67. package/src/i18n/ru.ts +62 -0
  68. package/src/i18n/sv.ts +62 -0
  69. package/src/i18n/tr.ts +62 -0
  70. package/src/i18n/uk.ts +62 -0
  71. package/src/i18n/zh.ts +62 -0
  72. package/src/i18n.ts +124 -0
  73. package/src/metadata.ts +4 -2
  74. package/src/runtime/devbar.ts +1 -1
  75. package/src/runtime/facet-state.ts +10 -5
  76. package/src/runtime/facets.ts +29 -5
  77. package/src/runtime/form-designer.ts +676 -0
  78. package/src/runtime/grid.ts +15 -8
  79. package/src/runtime/items.ts +33 -21
  80. package/src/runtime/lists.ts +1 -1
  81. package/src/runtime/pdf.ts +2 -1
  82. package/src/runtime/regions.ts +4 -0
  83. package/src/runtime/render.ts +7 -3
  84. package/src/runtime/report.ts +2 -2
  85. package/src/runtime/routes.ts +3 -1
  86. package/src/runtime/smart-filters.ts +2 -1
  87. package/src/runtime/tasks.ts +6 -3
  88. package/src/sqlformat.ts +208 -0
  89. package/src/sqlplan.ts +133 -0
  90. package/src/sqlscript.ts +6 -2
  91. package/src/sqlworksheet.ts +278 -0
  92. package/src/tabledef.ts +226 -0
  93. package/src/unload.ts +14 -3
@@ -7,12 +7,15 @@ import { owner } from '../db.ts';
7
7
  import { clientIp, logActivity, type Session } from '../session.ts';
8
8
  import { clearCompletions } from './code-editor.ts';
9
9
  import { scriptRoutes } from './scripts.ts';
10
+ import { worksheetRoutes } from './worksheet.ts';
10
11
  import { quickSqlRoutes } from './quicksql.ts';
11
12
  import { queryBuilderRoutes } from './querybuilder.ts';
12
13
  import { unloadRoutes } from './unload.ts';
13
14
  import { ddlToolRoutes } from './ddl-tools.ts';
14
15
  import { sqlUtilityRoutes } from './sql-utilities.ts';
15
16
  import { createTemplate, objectBrowserRoutes } from './object-browser.ts';
17
+ import { objectActionRoutes } from './object-actions.ts';
18
+ import { tableEditorRoutes } from './table-editor.ts';
16
19
  import { currentWorkspace, sessionSchema, usableSchemas, workshopDb, workshopRunsAs } from './workspaces.ts';
17
20
 
18
21
  // SQL Workshop: SQL commands and the object browser (owner connection; an isolated
@@ -53,29 +56,29 @@ async function workshopHome(req: Req, reply: FastifyReply) {
53
56
  ]);
54
57
  const tile = (href: string, ic: string, title: string, text: string) =>
55
58
  html`<a class="ab-tile sw-tile" href="${BASE}${href}"><span class="ab-tile-icon sw-tile-icon">${icon(ic)}</span><strong>${title}</strong><span>${text}</span></a>`;
56
- const list = (title: string, rows: Raw[], empty: string) =>
59
+ const list = (title: string | Raw, rows: Raw[], empty: string) =>
57
60
  html`<section class="sw-recent"><h2>${title}</h2>${rows.length ? html`<ul>${rows}</ul>` : html`<p class="muted">${empty}</p>`}</section>`;
58
61
  const main = html`<div class="ab-home">
59
62
  <div class="ab-main">
60
63
  <h1 class="sr-only">SQL Workshop</h1>
61
64
  ${workshopTabs('home')}
62
65
  <div class="ab-tiles sw-tiles">
66
+ ${tile('/sql/worksheet', 'code', 'SQL Worksheet', 'Run statements and scripts, with a navigator, plans and history.')}
63
67
  ${tile('/sql/objects', 'search', 'Object Browser', 'Browse tables, views and functions, their policies, grants and data.')}
64
- ${tile('/sql/commands', 'code', 'SQL Commands', 'Run SQL statements against your schemas.')}
65
68
  ${tile('/sql/scripts', 'file', 'SQL Scripts', 'Create, upload and run stored SQL scripts.')}
66
69
  ${tile('/sql/utilities', 'wrench', 'Utilities', 'Load and unload data, sample data, DDL, schema comparison, object reports and the database.')}
67
70
  ${tile('/sql/quick', 'bolt', 'Quick SQL', 'Write a data model in shorthand and get the DDL.')}
68
71
  </div>
69
72
  <div class="sw-recents">
70
73
  ${list('Recently created tables', tables.rows.map((t) => html`<li><a href="${BASE}/sql/objects?o=${encodeURIComponent(t.qname)}">${t.qname}</a><span class="muted">~${Number(t.rows).toLocaleString('en')} rows</span></li>`), 'No tables you can read.')}
71
- ${list('Recent SQL commands', commands.rows.map((c) => html`<li><a href="${BASE}/sql/commands?recent=${c.id}" title="${c.detail.slice(0, 500)}">${c.detail.replace(/\s+/g, ' ').slice(0, 60)}</a><span class="muted">${ago(c.at)}</span></li>`), 'You haven\'t run SQL commands yet.')}
74
+ ${list(html`<a href="${BASE}/sql/worksheet?tab=history" title="Open SQL History">Recent SQL commands</a>`, commands.rows.map((c) => html`<li><a href="${BASE}/sql/worksheet?recent=${c.id}" title="${c.detail.slice(0, 500)}">${c.detail.replace(/\s+/g, ' ').slice(0, 60)}</a><span class="muted">${ago(c.at)}</span></li>`), 'You haven\'t run SQL commands yet.')}
72
75
  ${list('Recent SQL scripts', scripts.rows.map((x) => html`<li><a href="${BASE}/sql/scripts/${x.id}">${x.name}</a><span class="muted">${ago(x.updated_at)}</span></li>`), 'No scripts in this workspace yet.')}
73
76
  </div>
74
77
  </div>
75
78
  <aside class="ab-side" aria-label="About, schema and tasks">
76
79
  <section><h2>About</h2><p>The SQL Workshop is a collection of tools to manage database objects. It runs as ${workshopRunsAs(db)}.</p>
77
80
  <a href="https://github.com/pgkiln/pgkiln/blob/main/docs/guide/03-builder.md#sql-workshop" target="_blank" rel="noopener">Learn more …</a></section>
78
- <section><h2>Schema</h2><p>The default schema of this session: first in the search path of SQL Commands and SQL Scripts.</p>
81
+ <section><h2>Schema</h2><p>The default schema of this session: first in the search path of the SQL Worksheet and SQL Scripts.</p>
79
82
  <form method="post" action="${BASE}/sql/schema" class="sw-schema">${csrf(s)}
80
83
  <label class="sr-only" for="f_schema">Schema</label>
81
84
  <select id="f_schema" name="schema" data-autosubmit><option value="">(the default search path)</option>${schemas.map((n) => html`<option value="${n}"${n === schema ? raw(' selected') : ''}>${n}</option>`)}</select>
@@ -102,76 +105,11 @@ export async function sqlRoutes(app: FastifyInstance) {
102
105
  // Generate DDL and Schema Comparison (sprint 56)
103
106
  await app.register(ddlToolRoutes);
104
107
  await app.register(sqlUtilityRoutes);
105
- // ---------------------------------------------------------------- SQL workshop
106
- const resultTable = (res: pg.QueryResult<any[]>, limit = 500) => {
107
- const rows = (res.rows ?? []).slice(0, limit);
108
- return html`<div class="table-wrap"><table class="report"><thead><tr>${res.fields.map((f) => html`<th>${f.name}</th>`)}</tr></thead>
109
- <tbody>${rows.map((r) => html`<tr>${r.map((v) => html`<td>${v === null ? html`<span class="null">null</span>` : typeof v === 'object' ? JSON.stringify(v) : String(v)}</td>`)}</tr>`)}</tbody></table></div>`;
110
- };
111
-
112
- const sqlWorkshop = async (req: Req, reply: FastifyReply) => {
113
- const s = await developer(req, reply);
114
- if (!s) return;
115
- const sql = req.body?.sql ?? '';
116
- let result: Raw | '' = '';
117
- const db = await workshopDb(s);
118
- // (sprint 61) a command from the home page's Recent SQL Commands (the developer's own)
119
- let text = sql;
120
- if (req.method === 'GET' && /^\d{1,18}$/.test(String(req.query?.recent ?? ''))) {
121
- const r = await owner.one<{ detail: string }>(`select detail from meta.activity_log where id = $1 and username = $2 and event = 'sql_command'`, [req.query!.recent, s.username]);
122
- if (r) text = r.detail;
123
- }
124
- const schema = await sessionSchema(s, db);
125
- if (req.method === 'GET') {
126
- // (sprint 75) a statement to review: from the Object Browser's column actions, or a CREATE template
127
- if (typeof s.state.__SQLDRAFT === 'string') {
128
- text = s.state.__SQLDRAFT;
129
- delete s.state.__SQLDRAFT;
130
- } else if (typeof req.query?.template === 'string') {
131
- const into = typeof req.query.schema === 'string' && (await usableSchemas(db)).includes(req.query.schema) ? req.query.schema : (schema ?? 'public');
132
- text = createTemplate(req.query.template, into) ?? text;
133
- }
134
- }
135
- if (req.method === 'POST' && sql.trim()) {
136
- const started = performance.now();
137
- try {
138
- // a connection of its own, closed afterwards: the SQL may SET ROLE or change settings,
139
- // which must not leak into the builder's pooled owner connections
140
- const c = await db.pool.connect();
141
- let out;
142
- try {
143
- if (schema) await c.query(`select set_config('search_path', $1, false)`, [`${pg.escapeIdentifier(schema)}, public`]);
144
- out = await c.query({ text: sql, rowMode: 'array' });
145
- } finally {
146
- c.release(true);
147
- }
148
- const res = Array.isArray(out) ? out[out.length - 1] : out;
149
- const ms = (performance.now() - started).toFixed(0);
150
- result = res.fields?.length
151
- ? html`<p class="muted">${res.rowCount} row(s) in ${ms} ms${res.rows.length > 500 ? ' (showing 500)' : ''}</p>${resultTable(res)}`
152
- : html`<div class="alert alert-success">${res.command ?? 'Statement'} completed${res.rowCount !== null ? `, ${res.rowCount} row(s)` : ''} in ${ms} ms.</div>`;
153
- } catch (e) {
154
- result = html`<div class="alert alert-error"><strong>Error:</strong> ${(e as Error).message}</div>`;
155
- }
156
- clearCompletions(); // the script may have changed tables or grants
157
- await logActivity({ username: s.username, event: 'sql_command', ip: clientIp(req), elapsedMs: Math.round(performance.now() - started), detail: sql.length > 2000 ? `${sql.slice(0, 2000)}…` : sql });
158
- }
159
- const main = html`<h1 class="u-mb1">SQL Workshop</h1>${workshopTabs('sql')}
160
- <p class="muted">Runs as ${workshopRunsAs(db)} (not as an application role)${schema ? html`, schema <code>${schema}</code> first in the search path (<a href="${BASE}/sql">change</a>)` : ''}. Multiple statements are allowed; results of the last one are shown.</p>
161
- <form method="post" action="${BASE}/sql/commands">${csrf(s)}
162
- <textarea name="sql" class="code sql-editor" rows="12" spellcheck="false" aria-label="SQL" data-code="plpgsql">${text || 'select current_user, version();'}</textarea>
163
- <div class="buttons"><button class="btn btn-hot">${icon('play')} Run (Ctrl+Enter)</button></div>
164
- </form>
165
- <div class="u-mt1">${result}</div>`;
166
- return send(reply, s, shell(s, 'SQL Workshop', [['SQL Workshop']], main, 'sql'));
167
- };
168
- app.get(`${BASE}/sql/commands`, sqlWorkshop);
169
- app.post(`${BASE}/sql/commands`, sqlWorkshop);
170
- // (before sprint 61 SQL Commands was /builder/sql: a post there still runs)
171
- app.post(`${BASE}/sql`, sqlWorkshop);
108
+ // (sprint 91) the SQL Worksheet (was SQL Commands; /sql/commands and a post to /sql are the same page)
109
+ await app.register(worksheetRoutes);
172
110
  app.get(`${BASE}/sql`, workshopHome);
173
111
 
174
- // (sprint 61) the session's default schema: first in the search path of SQL Commands and SQL Scripts
112
+ // (sprint 61) the session's default schema: first in the search path of the SQL Worksheet and SQL Scripts
175
113
  app.post(`${BASE}/sql/schema`, async (req: Req, reply) => {
176
114
  const s = await developer(req, reply);
177
115
  if (!s) return;
@@ -180,10 +118,14 @@ export async function sqlRoutes(app: FastifyInstance) {
180
118
  if (wanted && !usable.includes(wanted)) return reply.code(404).send('Not found');
181
119
  if (wanted) s.state.__SQLSCHEMA = wanted;
182
120
  else delete s.state.__SQLSCHEMA;
183
- flash(s, wanted ? `Schema ${wanted} first in the search path of SQL Commands and SQL Scripts.` : 'The default search path again.');
121
+ flash(s, wanted ? `Schema ${wanted} first in the search path of the SQL Worksheet and SQL Scripts.` : 'The default search path again.');
184
122
  return back(reply, s, `${BASE}/sql`);
185
123
  });
186
124
 
187
125
  // (sprint 75) the Object Browser: object-browser.ts
188
126
  await app.register(objectBrowserRoutes);
127
+ // (sprint 94) an object's menu: Quick DDL, Drop, Add index / trigger / policy, Open in a panel
128
+ await app.register(objectActionRoutes);
129
+ // (sprint 95) Table Properties: create, edit or copy a table in a form
130
+ await app.register(tableEditorRoutes);
189
131
  }
@@ -0,0 +1,219 @@
1
+ import type { FastifyInstance, FastifyReply } from 'fastify';
2
+ import { esc, html, raw, type Raw } from '../html.ts';
3
+ import { icon } from '../icons.ts';
4
+ import { clientIp, logActivity, type Session } from '../session.ts';
5
+ import { back, BASE, csrf, developer, flash, send, shell, workshopTabs, type Body, type Req } from './ui.ts';
6
+ import { SYSTEM_SCHEMAS } from './object-browser.ts';
7
+ import { clearCompletions } from './code-editor.ts';
8
+ import { usableSchemas, workshopDb } from './workspaces.ts';
9
+ import { checkTable, emptyTable, ON_DELETE, readTable, tableSql, type ColumnDef, type TableDef } from '../tabledef.ts';
10
+
11
+ // (sprint 95, Database Actions DA-8: Table Properties) Create a table, edit one, or start from one as a template, in
12
+ // a form: columns (name, type, not null, default, identity, comment, primary key), unique keys, foreign keys,
13
+ // checks, indexes, the comment, row level security and, for a new table, partitioning. Preview DDL shows the exact
14
+ // CREATE or ALTER statements; Apply runs them in one transaction with the workshop's connection (nothing is
15
+ // changed when one fails); Open in the worksheet puts them there to review. The form posts the whole definition;
16
+ // readTable() gives the table as it is now, so the statements are always made against the catalog, not against
17
+ // what the page showed. Without JavaScript the sections stack and spare empty rows add columns and keys.
18
+
19
+ const SPARE = 3; // empty rows for new columns, keys, checks and indexes (the page adds more with script)
20
+ const TYPES = ['bigint', 'integer', 'smallint', 'numeric', 'numeric(12,2)', 'real', 'double precision', 'text', 'varchar(200)', 'char(1)', 'boolean', 'date',
21
+ 'timestamp', 'timestamptz', 'time', 'interval', 'uuid', 'jsonb', 'json', 'bytea', 'inet', 'text[]', 'integer[]'];
22
+
23
+ const rows = (b: Body, prefix: string) => {
24
+ const ix = [...new Set(Object.keys(b).map((k) => new RegExp(`^${prefix}\\.(\\d{1,3})\\.`).exec(k)?.[1]).filter((x): x is string => !!x))].map(Number).sort((x, y) => x - y);
25
+ return ix.map((i) => (f: string) => String(b[`${prefix}.${i}.${f}`] ?? '').trim());
26
+ };
27
+
28
+ /** The definition the form posted; generated expressions and indexes the form can't edit come from the table as it is. */
29
+ function fromForm(b: Body, schema: string, old: TableDef | null): TableDef {
30
+ const def = emptyTable(schema);
31
+ def.name = String(b.name ?? '').trim();
32
+ def.comment = String(b.comment ?? '').trim();
33
+ def.pkName = String(b.pk_name ?? '').trim();
34
+ def.rls = b.rls === 'on';
35
+ def.forceRls = b.force_rls === 'on';
36
+ if (!old) {
37
+ def.partitionBy = (['range', 'list', 'hash'] as const).find((x) => x === b.part_by) ?? '';
38
+ def.partitionKey = def.partitionBy ? String(b.part_key ?? '').trim() : '';
39
+ }
40
+ for (const r of rows(b, 'col')) {
41
+ if (r('drop') === 'on' || (!r('name') && !r('type'))) continue;
42
+ const orig = old?.columns.find((c) => c.orig === r('orig')) ? r('orig') : '';
43
+ const was = old?.columns.find((c) => c.orig === orig);
44
+ const identity = r('identity') === 'always' || r('identity') === 'by default' ? (r('identity') as ColumnDef['identity']) : '';
45
+ def.columns.push({ name: r('name'), orig, type: r('type'), notNull: r('nn') === 'on', default: r('def'), identity, generated: was?.generated ?? '', comment: r('comment'), pk: r('pk') === 'on' });
46
+ }
47
+ for (const r of rows(b, 'uk')) if (r('drop') !== 'on' && (r('name') || r('cols'))) def.uniques.push({ name: r('name'), columns: r('cols') });
48
+ for (const r of rows(b, 'fk')) if (r('drop') !== 'on' && (r('name') || r('cols'))) def.fks.push({ name: r('name'), columns: r('cols'), refTable: r('ref'), refColumns: r('refcols'), onDelete: r('del') || 'no action' });
49
+ for (const r of rows(b, 'ck')) if (r('drop') !== 'on' && (r('name') || r('expr'))) def.checks.push({ name: r('name'), expr: r('expr') });
50
+ for (const r of rows(b, 'ix')) if (r('drop') !== 'on' && (r('name') || r('cols'))) def.indexes.push({ name: r('name'), columns: r('cols'), unique: r('unique') === 'on' });
51
+ // indexes the form can't express stay as they are
52
+ for (const i of old?.indexes ?? []) if (i.fixed) def.indexes.push(i);
53
+ return def;
54
+ }
55
+
56
+ /** A copy of a table under a new name, for "Use as template": new names for its constraints and indexes too. */
57
+ function asTemplate(t: TableDef): TableDef {
58
+ const name = `${t.name}_copy`.slice(0, 63);
59
+ const rename = (n: string) => (n.includes(t.name) ? n.replace(t.name, name) : `${n}_copy`).slice(0, 63);
60
+ return {
61
+ ...structuredClone(t), name, pkName: t.pkName ? rename(t.pkName) : '',
62
+ columns: t.columns.map((c) => ({ ...c, orig: '' })),
63
+ uniques: t.uniques.map((k) => ({ ...k, name: rename(k.name) })),
64
+ fks: t.fks.map((k) => ({ ...k, name: rename(k.name) })),
65
+ checks: t.checks.map((k) => ({ ...k, name: rename(k.name) })),
66
+ indexes: t.indexes.filter((i) => !i.fixed).map((i) => ({ ...i, name: rename(i.name) })),
67
+ };
68
+ }
69
+
70
+ async function tables(db: Awaited<ReturnType<typeof workshopDb>>) {
71
+ return (await db.query<{ q: string }>(
72
+ `select c.oid::regclass::text as q from pg_class c join pg_namespace n on n.oid = c.relnamespace
73
+ where c.relkind in ('r', 'p') and not c.relispartition and n.nspname !~ '^pg_' and n.nspname <> 'information_schema' and not (n.nspname = any ($1))
74
+ and has_schema_privilege(n.oid, 'USAGE') order by 1 limit 2000`, [SYSTEM_SCHEMAS])).rows.map((r) => r.q);
75
+ }
76
+
77
+ function form(s: Session, def: TableDef, opts: { editing: TableDef | null; schemas: string[]; refTables: string[]; ddl: string[]; errors: string[]; output: Raw | ''; tab: string; ref: string }): Raw {
78
+ const { editing } = opts;
79
+ const chk = (on: boolean) => (on ? raw(' checked') : '');
80
+ const sel = (v: string, cur: string) => (v === cur ? raw(' selected') : '');
81
+ const typeList = html`<datalist id="te-types">${TYPES.map((t) => html`<option value="${t}">`)}</datalist>`;
82
+ const colRow = (c: ColumnDef | null, i: number) => html`<tr class="te-row${c ? '' : ' te-spare'}">
83
+ <td class="te-move"><button type="button" class="btn btn-sm btn-icon" data-te-up title="Move up" hidden>↑</button><button type="button" class="btn btn-sm btn-icon" data-te-down title="Move down" hidden>↓</button></td>
84
+ <td><input type="hidden" name="col.${i}.orig" value="${c?.orig ?? ''}"><input id="te-c${i}-name" aria-label="Name" name="col.${i}.name" value="${c?.name ?? ''}" autocomplete="off" spellcheck="false"></td>
85
+ <td><input id="te-c${i}-type" aria-label="Type" name="col.${i}.type" value="${c?.type ?? ''}" list="te-types" autocomplete="off" spellcheck="false"></td>
86
+ <td class="te-c"><input type="checkbox" aria-label="Primary key" id="te-c${i}-pk" name="col.${i}.pk"${chk(!!c?.pk)}></td>
87
+ <td class="te-c"><input type="checkbox" aria-label="Not null" id="te-c${i}-nn" name="col.${i}.nn"${chk(!!c?.notNull)}></td>
88
+ <td>${c?.generated
89
+ ? html`<code class="te-generated" title="Generated (stored)">= ${c.generated}</code><input type="hidden" name="col.${i}.def" value="">`
90
+ : html`<input id="te-c${i}-def" aria-label="Default" name="col.${i}.def" value="${c?.default ?? ''}" class="code" spellcheck="false">`}</td>
91
+ <td><select id="te-c${i}-id" aria-label="Identity" name="col.${i}.identity"><option value="">-</option><option value="by default"${sel('by default', c?.identity ?? '')}>by default</option><option value="always"${sel('always', c?.identity ?? '')}>always</option></select></td>
92
+ <td><input id="te-c${i}-cm" aria-label="Comment" name="col.${i}.comment" value="${c?.comment ?? ''}"></td>
93
+ <td class="te-c"><label class="te-drop"><input type="checkbox" name="col.${i}.drop"><span>Remove</span></label></td></tr>`;
94
+ const simpleRows = <T,>(list: T[], prefix: string, cells: (x: T | null, i: number) => Raw) =>
95
+ [...list, ...Array<null>(SPARE).fill(null)].map((x, i) => html`<tr class="te-row${x ? '' : ' te-spare'}">${cells(x, i)}<td class="te-c"><label class="te-drop"><input type="checkbox" name="${prefix}.${i}.drop"><span>Remove</span></label></td></tr>`);
96
+ // extra: attributes written by this file (constants, or values escaped with esc())
97
+ const input = (name: string, value: string, label: string, extra = '') => html`<input id="te-${name}" aria-label="${label}" name="${name}" value="${value}" autocomplete="off" spellcheck="false"${extra ? raw(` ${extra}`) : ''}>`;
98
+ const sections: [string, string, Raw][] = [
99
+ ['columns', 'Columns', html`<div class="table-wrap"><table class="report te-table" data-te-grid="col"><thead><tr><th></th><th>Name</th><th>Type</th><th>Key</th><th>Not null</th><th>Default</th><th>Identity</th><th>Comment</th><th></th></tr></thead>
100
+ <tbody>${[...def.columns, ...Array<null>(SPARE).fill(null)].map((c, i) => colRow(c, i))}</tbody></table></div>
101
+ <div class="buttons"><button type="button" class="btn btn-sm" data-te-add="col" hidden>${icon('plus')} Add column</button></div>
102
+ <div class="field u-mt1"><label class="label" for="te-pk_name">Primary key name</label>${input('pk_name', def.pkName, 'Primary key name', `placeholder="${esc(def.name ? `${def.name}_pkey` : 'table_pkey')}"`)}<small class="help">Tick Key on the columns of the primary key.</small></div>`],
103
+ ['keys', 'Unique keys', html`<div class="table-wrap"><table class="report te-table" data-te-grid="uk"><thead><tr><th>Name</th><th>Columns (comma-separated)</th><th></th></tr></thead><tbody>
104
+ ${simpleRows(def.uniques, 'uk', (k, i) => html`<td>${input(`uk.${i}.name`, k?.name ?? '', 'Name')}</td><td>${input(`uk.${i}.cols`, k?.columns ?? '', 'Columns')}</td>`)}</tbody></table></div>`],
105
+ ['fks', 'Foreign keys', html`<div class="table-wrap"><table class="report te-table" data-te-grid="fk"><thead><tr><th>Name</th><th>Columns</th><th>References</th><th>Their columns</th><th>On delete</th><th></th></tr></thead><tbody>
106
+ ${simpleRows(def.fks, 'fk', (f, i) => html`<td>${input(`fk.${i}.name`, f?.name ?? '', 'Name')}</td><td>${input(`fk.${i}.cols`, f?.columns ?? '', 'Columns')}</td>
107
+ <td><select id="te-fk${i}-ref" aria-label="References" name="fk.${i}.ref"><option value="">-</option>${opts.refTables.map((t) => html`<option value="${t}"${sel(t, f?.refTable ?? '')}>${t}</option>`)}</select></td>
108
+ <td>${input(`fk.${i}.refcols`, f?.refColumns ?? '', 'Their columns', 'placeholder="its primary key"')}</td>
109
+ <td><select id="te-fk${i}-del" aria-label="On delete" name="fk.${i}.del">${ON_DELETE.map((d) => html`<option value="${d}"${sel(d, f?.onDelete ?? 'no action')}>${d}</option>`)}</select></td>`)}</tbody></table></div>`],
110
+ ['checks', 'Checks', html`<div class="table-wrap"><table class="report te-table" data-te-grid="ck"><thead><tr><th>Name</th><th>Condition</th><th></th></tr></thead><tbody>
111
+ ${simpleRows(def.checks, 'ck', (k, i) => html`<td>${input(`ck.${i}.name`, k?.name ?? '', 'Name')}</td><td>${input(`ck.${i}.expr`, k?.expr ?? '', 'Condition', 'class="code" placeholder="salary &gt;= 0"')}</td>`)}</tbody></table></div>`],
112
+ ['indexes', 'Indexes', html`<div class="table-wrap"><table class="report te-table" data-te-grid="ix"><thead><tr><th>Name</th><th>Columns or expressions</th><th>Unique</th><th></th></tr></thead><tbody>
113
+ ${simpleRows(def.indexes.filter((i) => !i.fixed), 'ix', (x, i) => html`<td>${input(`ix.${i}.name`, x?.name ?? '', 'Name')}</td><td>${input(`ix.${i}.cols`, x?.columns ?? '', 'Columns', 'class="code" placeholder="lower(name), created_at desc"')}</td>
114
+ <td class="te-c"><input type="checkbox" aria-label="Unique" id="te-ix${i}-u" name="ix.${i}.unique"${chk(!!x?.unique)}></td>`)}</tbody></table></div>
115
+ ${def.indexes.some((i) => i.fixed) ? html`<p class="muted small">Kept as they are (another method or a condition): ${def.indexes.filter((i) => i.fixed).map((i) => html`<code>${i.fixed}</code> `)}</p>` : ''}`],
116
+ ['more', 'Comment and security', html`<div class="field"><label class="label" for="te-comment">Comment</label><textarea id="te-comment" name="comment" rows="2">${def.comment}</textarea></div>
117
+ <label class="check"><input type="checkbox" name="rls"${chk(def.rls)}> Row level security: rows only through policies</label>
118
+ <label class="check"><input type="checkbox" name="force_rls"${chk(def.forceRls)}> Force it for the table's owner too</label>
119
+ ${editing ? '' : html`<fieldset class="prop-group u-mt1"><legend>Partitioning</legend><div class="form-grid">
120
+ <div class="field"><label class="label" for="te-part_by">Partition by</label><select id="te-part_by" name="part_by"><option value="">Not partitioned</option>${(['range', 'list', 'hash'] as const).map((p) => html`<option value="${p}"${sel(p, def.partitionBy)}>${p}</option>`)}</select></div>
121
+ <div class="field"><label class="label" for="te-part_key">Key</label>${input('part_key', def.partitionKey, 'Key', 'class="code" placeholder="created_at"')}</div></div>
122
+ <small class="help">The partitions themselves are tables made with CREATE TABLE … PARTITION OF in the worksheet.</small></fieldset>`}`],
123
+ ['ddl', 'DDL', opts.ddl.length ? html`<pre class="source te-ddl">${opts.ddl.join('\n\n')}</pre>` : html`<p class="muted">${editing ? 'No changes yet: the table is as the form shows it.' : 'Fill in the table and press Preview DDL.'}</p>`],
124
+ ['output', 'Output', opts.output || html`<p class="muted">Apply shows what happened here.</p>`],
125
+ ];
126
+ const tab = sections.some(([k]) => k === opts.tab) ? opts.tab : opts.errors.length ? 'columns' : 'columns';
127
+ return html`<form method="post" action="${BASE}/sql/objects/table" class="te" data-te-tab="${tab}">${csrf(s)}
128
+ <input type="hidden" name="ref" value="${opts.ref}">${typeList}
129
+ ${opts.errors.length ? html`<div class="alert alert-error" role="alert"><ul>${opts.errors.map((e) => html`<li>${e}</li>`)}</ul></div>` : ''}
130
+ <div class="form-grid te-head">
131
+ <div class="field"><label class="label" for="te-schema">Schema</label>${editing
132
+ ? html`<input id="te-schema" value="${def.schema}" readonly><input type="hidden" name="schema" value="${def.schema}">`
133
+ : html`<select id="te-schema" name="schema">${opts.schemas.map((n) => html`<option value="${n}"${sel(n, def.schema)}>${n}</option>`)}</select>`}</div>
134
+ <div class="field"><label class="label" for="te-name">Table name</label><input id="te-name" name="name" value="${def.name}" required autocomplete="off" spellcheck="false"></div>
135
+ </div>
136
+ <nav class="te-tabs" role="tablist" aria-label="Table properties" hidden>${sections.map(([k, label]) => html`<button type="button" role="tab" aria-selected="${k === tab ? 'true' : 'false'}" data-te-show="${k}">${label}</button>`)}</nav>
137
+ ${sections.map(([k, label, body]) => html`<section class="te-section" data-te-section="${k}"><h2>${label}</h2>${body}</section>`)}
138
+ <div class="buttons te-buttons">
139
+ <button class="btn" name="mode" value="preview">${icon('eye')} Preview DDL</button>
140
+ <button class="btn btn-hot" name="mode" value="apply">${icon('check')} Apply</button>
141
+ <button class="btn" name="mode" value="worksheet">${icon('code')} Open in the worksheet</button>
142
+ <a class="btn" href="${editing ? `${BASE}/sql/objects?${new URLSearchParams({ schema: def.schema, k: 'table', o: opts.ref })}` : `${BASE}/sql/objects?${new URLSearchParams({ schema: def.schema })}`}">Cancel</a>
143
+ </div></form>`;
144
+ }
145
+
146
+ async function page(req: Req, reply: FastifyReply) {
147
+ const s = await developer(req, reply);
148
+ if (!s) return;
149
+ const db = await workshopDb(s);
150
+ const isPost = req.method === 'POST';
151
+ const b: Body = isPost ? (req.body ?? {}) : (req.query ?? {});
152
+ const schemas = (await usableSchemas(db)).filter((n) => !SYSTEM_SCHEMAS.includes(n));
153
+ const refTables = await tables(db);
154
+ // the table being edited, read again from the catalog on every request
155
+ const ref = String((isPost ? b.ref : b.o) ?? '');
156
+ const editing = ref ? await readTable(db, ref) : null;
157
+ if (ref && (!editing || SYSTEM_SCHEMAS.includes(editing.schema) || !schemas.includes(editing.schema))) return reply.code(404).send('Not found');
158
+ let def: TableDef;
159
+ let errors: string[] = [];
160
+ let ddl: string[] = [];
161
+ let output: Raw | '' = '';
162
+ let tab = 'columns';
163
+ if (!isPost) {
164
+ const template = typeof b.template === 'string' && b.template ? await readTable(db, b.template) : null;
165
+ if (b.template && (!template || !schemas.includes(template.schema))) return reply.code(404).send('Not found');
166
+ def = editing ?? (template ? asTemplate(template) : emptyTable(schemas.includes(String(b.schema)) ? String(b.schema) : (schemas[0] ?? 'public')));
167
+ } else {
168
+ const schema = editing ? editing.schema : String(b.schema ?? '');
169
+ if (!schemas.includes(schema)) return reply.code(404).send('Not found');
170
+ def = fromForm(b, schema, editing);
171
+ errors = checkTable(def, editing);
172
+ if (!errors.length) ddl = tableSql(def, editing);
173
+ tab = 'ddl';
174
+ if (!errors.length && b.mode === 'worksheet') {
175
+ s.state.__SQLDRAFT = ddl.join('\n\n') || '-- no changes';
176
+ flash(s, 'Review the statements and run them: nothing has been changed yet.');
177
+ return back(reply, s, `${BASE}/sql/worksheet`);
178
+ }
179
+ if (!errors.length && b.mode === 'apply' && ddl.length) {
180
+ const c = await db.pool.connect();
181
+ let failed = '';
182
+ let at = '';
183
+ try {
184
+ await c.query('begin');
185
+ for (const x of ddl) {
186
+ at = x;
187
+ await c.query(x);
188
+ }
189
+ await c.query('commit');
190
+ } catch (e) {
191
+ failed = (e as Error).message;
192
+ await c.query('rollback').catch(() => {});
193
+ } finally {
194
+ c.release(true);
195
+ }
196
+ await logActivity({ username: s.username, event: 'sql_command', ip: clientIp(req), detail: ddl.join(';\n').slice(0, 2000) });
197
+ clearCompletions();
198
+ if (!failed) {
199
+ flash(s, `${editing ? 'Changed' : 'Created'} ${def.schema}.${def.name}: ${ddl.length} statement${ddl.length === 1 ? '' : 's'}.`);
200
+ const now = await readTable(db, `${pgIdent(def.schema)}.${pgIdent(def.name)}`);
201
+ return back(reply, s, `${BASE}/sql/objects/table?${new URLSearchParams({ o: now ? `${pgIdent(now.schema)}.${pgIdent(now.name)}` : '' })}`);
202
+ }
203
+ output = html`<div class="alert alert-error" role="alert"><p><strong>Nothing was changed</strong>: the statements ran in one transaction, which was rolled back.</p><p>${failed}</p><pre class="source">${at}</pre></div>`;
204
+ tab = 'output';
205
+ }
206
+ if (errors.length) tab = 'columns';
207
+ }
208
+ const title = editing ? `Table ${editing.schema}.${editing.name}` : 'Create table';
209
+ const main = html`<h1 class="u-mb1">${title}</h1>${workshopTabs('objects')}
210
+ ${form(s, def, { editing, schemas, refTables, ddl, errors, output, tab, ref: editing ? ref : '' })}`;
211
+ return send(reply, s, shell(s, title, [['SQL Workshop', `${BASE}/sql`], ['Object Browser', `${BASE}/sql/objects`], [title]], main, 'sql'));
212
+ }
213
+
214
+ const pgIdent = (n: string) => (/^[a-z_][a-z0-9_$]*$/.test(n) ? n : `"${n.replace(/"/g, '""')}"`);
215
+
216
+ export async function tableEditorRoutes(app: FastifyInstance) {
217
+ app.get(`${BASE}/sql/objects/table`, page);
218
+ app.post(`${BASE}/sql/objects/table`, page);
219
+ }
package/src/builder/ui.ts CHANGED
@@ -12,6 +12,8 @@ import { ensureDevbar } from '../runtime/devbar.ts';
12
12
  // Shared building blocks of the builder UI.
13
13
 
14
14
  export const BASE = '/builder';
15
+ /** (sprint 86) The page designer's address: the application's id and the page's number (/builder/pages/:id redirects here). */
16
+ export const designerUrl = (appId: number, pageNo: number, query = '') => `${BASE}/apps/${appId}/pages/${pageNo}${query ? `?${query}` : ''}`;
15
17
 
16
18
  /** pgkiln's version and the database name, for the builder's status bar. */
17
19
  const VERSION = (JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8')) as { version: string }).version;
@@ -49,8 +51,8 @@ export type Section = 'apps' | 'users' | 'sql' | 'developers' | 'gallery';
49
51
  // Builder-only stylesheets and scripts, loaded on every builder page after
50
52
  // app.css / app.js (the runtime never loads them). Add new builder assets
51
53
  // here, and only here.
52
- export const BUILDER_STYLES = ['/static/builder.css', '/static/code-editor.css'];
53
- export const BUILDER_SCRIPTS = ['/static/builder.js', '/static/code-editor.js'];
54
+ export const BUILDER_STYLES = ['/static/builder.css', '/static/code-editor.css', '/static/worksheet.css'];
55
+ export const BUILDER_SCRIPTS = ['/static/builder.js', '/static/code-editor.js', '/static/worksheet.js'];
54
56
  export const builderHead = () =>
55
57
  html`${BUILDER_STYLES.map((href) => html`<link rel="stylesheet" href="${href}">\n`)}${BUILDER_SCRIPTS.map((src) => html`<script src="${src}" defer></script>\n`)}`;
56
58
 
@@ -257,7 +259,7 @@ export const workshopTabs = (active: WorkshopTab) => {
257
259
  ];
258
260
  const open = more.find(([k]) => k === active);
259
261
  return html`<nav class="ide-tabs sw-tabs u-mb1" aria-label="SQL Workshop">
260
- ${tab('home', '/sql', 'home', 'Home')}${tab('objects', '/sql/objects', 'database', 'Object Browser')}${tab('sql', '/sql/commands', 'code', 'SQL Commands')}${tab('scripts', '/sql/scripts', 'file', 'SQL Scripts')}${tab('utilities', '/sql/utilities', 'wrench', 'Utilities')}
262
+ ${tab('home', '/sql', 'home', 'Home')}${tab('sql', '/sql/worksheet', 'code', 'SQL Worksheet')}${tab('objects', '/sql/objects', 'database', 'Object Browser')}${tab('scripts', '/sql/scripts', 'file', 'SQL Scripts')}${tab('utilities', '/sql/utilities', 'wrench', 'Utilities')}
261
263
  ${open ? tab(...open) : ''}
262
264
  <details class="menu sw-more"><summary class="ide-tab">${icon('chevron-down')}<span>More</span></summary>
263
265
  <div class="menu-panel align-right"><div class="menu-section menu-links">${more.map(([k, href, ic, label]) => html`<a href="${BASE}${href}"${k === active ? raw(' aria-current="page"') : ''}>${icon(ic)} ${label}</a>`)}</div></div></details></nav>`;
@@ -21,12 +21,12 @@ import {
21
21
  type UnloadOptions,
22
22
  } from '../unload.ts';
23
23
  import { BASE, csrf, developer, region, send, shell, workshopTabs, type Body, type Req } from './ui.ts';
24
- import { workshopDb } from './workspaces.ts';
24
+ import { sessionSchema, workshopDb } from './workspaces.ts';
25
25
 
26
26
  // SQL Workshop → Unload Data (APEX: Data Workshop → Unload Data): a table or
27
27
  // view (chosen columns, an optional WHERE and ORDER BY) or a query, to CSV,
28
28
  // JSON, Excel or XML, streamed from a cursor (src/unload.ts). Runs on a
29
- // connection of its own as the owner, like SQL Commands, but in a READ ONLY
29
+ // connection of its own as the owner, like the SQL Worksheet, but in a READ ONLY
30
30
  // transaction with a statement timeout; the connection is closed afterwards
31
31
  // so nothing the query sets can leak into the pool. The table step is a GET
32
32
  // form and the download a POST form (CSRF), so it works without JavaScript.
@@ -100,7 +100,7 @@ export async function unloadRoutes(app: FastifyInstance) {
100
100
  return html`
101
101
  <fieldset class="prop-group u-mt125"><legend>Format</legend>
102
102
  <div class="unload-formats" role="radiogroup" aria-label="Format">${UNLOAD_FORMATS.map(
103
- (f) => html`<label class="check"><input type="radio" name="format" value="${f}"${fmt === f ? raw(' checked') : ''}> ${{ csv: 'CSV', json: 'JSON', xlsx: 'Excel (.xlsx)', xml: 'XML' }[f]}</label>`,
103
+ (f) => html`<label class="check"><input type="radio" name="format" value="${f}"${fmt === f ? raw(' checked') : ''}> ${{ csv: 'CSV', json: 'JSON', xlsx: 'Excel (.xlsx)', xml: 'XML', sql: 'SQL inserts' }[f]}</label>`,
104
104
  )}</div>
105
105
  </fieldset>
106
106
  <fieldset class="prop-group"><legend>CSV</legend><div class="form-grid">
@@ -213,11 +213,15 @@ export async function unloadRoutes(app: FastifyInstance) {
213
213
  if (typeof opts === 'string') return refuse(opts);
214
214
 
215
215
  const started = performance.now();
216
- const c = await (await workshopDb(s)).pool.connect();
216
+ const db = await workshopDb(s);
217
+ const schema = await sessionSchema(s, db);
218
+ const c = await db.pool.connect();
217
219
  let opened;
218
220
  try {
219
221
  await c.query('begin transaction read only');
220
222
  await c.query(`select set_config('statement_timeout', $1, true)`, [timeout()]);
223
+ // (sprint 92) the session's schema first in the search path, like the SQL Worksheet (its downloads come here)
224
+ if (schema) await c.query(`select set_config('search_path', $1, true)`, [`${pg.escapeIdentifier(schema)}, public`]);
221
225
  opened = await openUnload(c, sql, opts);
222
226
  } catch (e) {
223
227
  await c.query('rollback').catch(() => {});