Compare commits

...
Author SHA1 Message Date
james.maes 777eb3b95b Merge branch 'develop' into feature/developer-panel 2026-10-12 16:03:18 -05:00
james.maes 7f21e46a2f Merge pull request #15 from QRun-IO/feature/ga-only-release-tags
ci: publish only GA tags (vX.Y.Z), never RC tags
2026-10-12 16:01:09 -05:00
james.maesandClaude Opus 5.5 dbdb5eb113 ci: publish only GA tags (vX.Y.Z), never RC tags
An RC tag matched /v.*/ and the orb republished it as the next RC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 15:58:14 -05:00
james.maesandClaude Opus 5.5 c36a0e8c94 Merge develop into feature/developer-panel; open SQL in the Runner
Brings the snake_case rename (#7) and the app home and Runner (#9).
The developer panel's "open as a new cell" links now target the Runner
app (SqlWorkbenchRunnerAppMetaDataProducer.APP_NAME, prefixed), since
/app/sqlWorkbench is the home dashboard. Tests and README updated; the
changelog entry moves under 1.0.0-RC.3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 15:31:22 -05:00
james.maes 72647623fa Merge pull request #9 from QRun-IO/feature/app-home-runner-nav
feat: app home dashboard, Runner nav entry, main tables only (1.0.0-RC.3)
2026-10-12 15:14:21 -05:00
james.maes f7b5ffd569 Merge branch 'develop' into feature/app-home-runner-nav 2026-10-12 15:12:19 -05:00
james.maes f42f76c59b Merge pull request #7 from QRun-IO/feature/snake-case-table-names
feat!: snake_case QQQ names with the sql_workbench_ prefix (for 1.0.0-RC.3)
2026-10-12 15:12:05 -05:00
james.maes b3913fca7f Merge branch 'develop' into feature/snake-case-table-names 2026-10-12 15:10:14 -05:00
james.maesandClaude Opus 5.5 68f559dfc8 fix: home and notebook record polish from the screenshot review
- Recent, failed and write runs sort by start time (not insert order).
- Activity by User has four columns (last run moves under the user), the
  host readability rule's maximum.
- No widget-permission throw inside the renderer: the middleware checks
  widget permissions, and host guards render every widget.
- Notebook record view: plain section titles (Cells, Shared With, Pinned
  Results, Versions, Runs) instead of join names; Runs newest first,
  Versions newest first; the Open in Runner widget is not a nested card.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 15:05:46 -05:00
james.maes 5be0693255 Merge pull request #8 from QRun-IO/chore/qrun-repo-standard
chore: align with the QRun qbit repository standard
2026-10-12 14:54:53 -05:00
james.maesandClaude Opus 5.5 18651d6712 chore: align with the QRun qbit repository standard
- `.editorconfig` (Kingsrook 3-space layout)
- `.gitattributes` (LF, CRLF for `*.cmd`)
- `.github/dependabot.yml` (Maven + Actions, grouped minor/patch, at most 3 open PRs, target `develop`)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 14:48:42 -05:00
james.maesandClaude Opus 5.5 6f2626b04f feat: app home dashboard, Runner nav entry, main tables only in the nav
Owner spec: the SQL Workbench app home is an information dashboard like
the other app homes, the notebook UI is its own nav entry ("Runner"), and
the nav lists only the main tables; sub-tables are relations on a notebook.

- Home: summary tiles and links, runs per day, recent notebooks, recent
  runs, slowest and failed queries, most-used notebooks, shared notebooks,
  committed writes, and (admins only) activity by user. Read through core
  actions in the viewer's session, so permissions and record locks apply;
  honest empty and no-permission states. Full width, inline styles only.
- Runner: sqlWorkbenchRunner child app showing the SQL_WORKBENCH widget
  (/app/sqlWorkbenchRunner?notebook=<id>).
- Nav: Runner, sql_workbench_notebook, sql_workbench_execution. Cells,
  versions, pinned results and shares stay standard tables (URL, API) as
  child lists on the notebook; new Runs child list (notebook -> execution
  join, locked to the viewer's runs) and an Open in Runner widget.
- SqlWorkbenchIntegrations: opt-in ESB and Quick Search hooks for the main
  tables, no dependency on either module.
- Tests: home widgets (H2 and Postgres), nav structure, child lists,
  hooks. README, CHANGELOG, UPGRADE, CLAUDE/AGENTS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 14:45:38 -05:00
james.maesandClaude Opus 5.5 51c5a06859 feat!: snake_case QQQ names with the sql_workbench_ prefix (owner naming rule)
Owner rule: table names are lowercase snake_case, prefixed with the
qbit's name. Metadata only; the physical tables were already
sql_workbench_*, so no DDL.

- Tables, notebook PVS: sql_workbench_notebook, _cell, _notebook_version,
  _result_snapshot, _notebook_share, _execution (now equal to their
  backend table names)
- Enum PVS: sql_workbench_share_audience_type
- Permissions: sql_workbench.{hasAccess,write,share},
  sql_workbench_admin.hasAccess, <table>.read etc.
- Security key types: sql_workbench_user (all-access
  sql_workbench_all_access), sql_workbench_audience
- Joins: sql_workbench_notebook_join_sql_workbench_*
- tableNamePrefix / namespace normalized to snake_case, one "_"
- @Deprecated LEGACY_* constants with the pre-RC.3 names
- UPGRADE.md: permissions, security keys, stored references (audit_table,
  saved views/reports) with sample SQL; CHANGELOG 1.0.0-RC.3
- Test for the new names, legacy constants and prefix normalization

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 14:07:13 -05:00
james.maes c78737c359 Merge pull request #5 from QRun-IO/feature/3-standard-qqq-tables
feat(#3): workbench data as standard QQQ tables secured by record locks (1.0.0-RC.2)
2026-10-12 12:27:40 -05:00
james.maesandClaude Opus 5.5 ad8f9001a6 ci: rebuild now that QQQ 4.1.0-RC.4 is on Maven Central
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 12:25:22 -05:00
james.maesandClaude Opus 5.5 8346a5fe27 build: build against the released QQQ 4.1.0-RC.4
RC.4 lets partial edits of child rows pass joined record locks
(qqq#1073), which the table security matrix tests need.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 12:24:46 -05:00
james.maesandClaude Opus 5.5 e0d9312aff feat: SQL Workbench record developer panel
Add SqlWorkbenchDeveloperPanelProvider (id sqlWorkbench, order 450, icon
terminal), registered by SqlWorkbenchDeveloperPanelsMetaDataProducer via
DeveloperPanelsMetaData. For tables on an allowed RDBMS backend, and users
with the workbench access permission, it shows read-only SELECTs for the
record and its table in the backend's own table/column names (dialect
quoted identifiers, escaped primary-key literal), backend details, and
links that open each query in the workbench (?sql=&backend=, not run).

Build against QQQ 4.1.0-RC.4 (the developer panel contract).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 10:38:51 -05:00
james.maesandClaude Opus 5.5 663c209f4e fix(#3): gate record actions with a context-free permission checker
QQQ's UseTablePermissionCustomPermissionChecker needs a QContext instance
when available permissions are listed and threw a NullPointerException
there (seen in Website-Backend's permission-listing tests). The record
actions now use SqlWorkbenchPermissionChecker, which requires one named
session permission (share-table insert, notebook insert / edit). Test:
listing available permissions with no QContext.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 06:21:13 -05:00
james.maesandClaude Opus 5.5 f823c9a50b fix(#3): harden versioning, cascades and visibility sync; more security tests
From review of the standard-tables change:
- Versioning: the next number passes both currentVersion and the newest
  stored version, the currentVersion update is checked, and failures are
  logged (QQQ turns post-customizer exceptions into record warnings).
- Notebook delete cascade: a failed child delete aborts the delete.
- Share visibility sync checks its update.
- An admin ownership transfer resets ownerName to the new owner id.
- Shares are audited at record level, like the other tables.
- Tests: mixed insert/update batches, re-targeting rows into one's own
  notebook, planting ownership, a shared reader vs pinned results, and
  per-notebook children after denied deletes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 06:17:10 -05:00
james.maesandClaude Opus 5.5 ddd85ebe0d feat(#3): expose workbench data as standard QQQ tables secured by record locks
Notebooks, cells, versions, pinned results, shares and the execution
audit are now ordinary QQQ tables in the SQL Workbench app (nav children,
record views with child lists, edit, export, audits, developer mode)
instead of hidden admin-only tables.

- Record security locks (core-enforced, so every entry point): owner OR
  share audience for notebooks and their children (through joins), the
  audit by user; admins hold sqlWorkbenchAllAccess. Key types
  sqlWorkbenchUser / sqlWorkbenchAudience, applied to sessions by
  SqlWorkbenchSecurity.applyToSession (no keys without workbench access).
- New sqlWorkbenchNotebookShare (USER / GROUP / ALL); publishing writes an
  ALL share and visibility follows the shares.
- Per-table read/insert/edit/delete permissions, listed per role by the
  config (user / share / admin).
- Table customizers hold the notebook invariants for the notebook UI and
  the tables alike: owner from the session, cell validation and order,
  one version per change, share keys, append-only versions and audit
  (admins included), pinned results only from pinning, cascades.
- NotebookService and history run as the caller through the same actions;
  explicit owner/share checks stay as defense in depth.
- Record actions: Share Notebook, Duplicate Notebook, Restore Version.
- DDL: share table plus an idempotent backfill of library shares.
- Tests: TableSecurityMatrixTest (role x table x action over the v1 table
  API, core actions, export, PVS, record actions, notebook endpoints) and
  a PostgreSQL lock test. Docs: README, DESIGN, CHANGELOG.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 06:08:10 -05:00
james.maes 6c90ba12c0 Merge pull request #4 from QRun-IO/docs/adoption-checklist-link
docs(readme): link the QQQ adoption checklist
2026-10-12 06:04:07 -05:00
james.maesandClaude Opus 5.5 ea6e9ffec9 docs(readme): link the QQQ adoption checklist
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: James Maes <james@kof22.com>
2026-10-12 05:47:17 -05:00
james.maes 4f1e2305e1 Merge pull request #2 from QRun-IO/docs/adopting-in-another-app
docs: add Adopting in another app section and 1.0.0-RC.1 changelog entry
2026-10-12 05:28:47 -05:00
james.maesandClaude Opus 5.5 c2e754b146 docs: add Adopting in another app section and 1.0.0-RC.1 changelog entry
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 05:26:49 -05:00
james.maes 731225813d Merge pull request #1 from QRun-IO/feature/1-sql-workbench-1.0
feat: SQL Workbench qbit 1.0 backend
2026-10-11 13:52:05 -05:00
64 changed files with 8485 additions and 713 deletions

No files matched your search

+1 -1
View File
@@ -57,7 +57,7 @@ workflows:
branches:
ignore: /.*/
tags:
only: [/v.*/]
only: [/^v\d+\.\d+\.\d+$/]
publish_hotfix_release:
jobs:
+20
View File
@@ -0,0 +1,20 @@
# QRun-IO qbit .editorconfig: the Kingsrook layout (3-space indentation, never tabs).
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 3
tab_width = 3
[*.md]
trim_trailing_whitespace = false
[*.{yml,yaml}]
indent_size = 2
[*.{cmd,bat}]
end_of_line = crlf
+3
View File
@@ -0,0 +1,3 @@
* text=auto eol=lf
*.cmd text eol=crlf
mvnw text eol=lf
+25
View File
@@ -0,0 +1,25 @@
# Weekly, grouped dependency updates. Minor and patch updates arrive as one PR per ecosystem;
# major updates stay separate so each gets its own review. At most three version-update PRs per
# ecosystem are open at once. Security updates are enabled in the repository settings and are
# not limited by this schedule.
version: 2
updates:
- package-ecosystem: maven
directory: /
target-branch: develop
schedule:
interval: weekly
open-pull-requests-limit: 3
groups:
maven-minor-and-patch:
update-types: [minor, patch]
- package-ecosystem: github-actions
directory: /
target-branch: develop
schedule:
interval: weekly
open-pull-requests-limit: 3
groups:
github-actions:
patterns: ['*']
+12 -5
View File
@@ -4,7 +4,7 @@ This file provides guidance to Codex when working with code in this repository.
## Project Overview
**QBit SQL Workbench**: a QQQ QBit that provides a super-user SQL notebook. Sessions are read-only by default, write mode is gated, and everything is audited. It works over any RDBMS backend of the instance. The UI is the Next UI `SQL_WORKBENCH` widget. Design: `docs/DESIGN.md`.
**QBit SQL Workbench**: a QQQ QBit that provides a super-user SQL notebook. Sessions are read-only by default, write mode is gated, and everything is audited. It works over any RDBMS backend of the instance. The UI is the Next UI `SQL_WORKBENCH` widget on the Runner page; the app's home is a dashboard (`home/`). Design: `docs/DESIGN.md`.
## Build Commands
@@ -18,7 +18,7 @@ CI=true ./mvnw verify # Unit + Postgres integration tests (Docker) + JaCoCo ga
How tests are split:
- **Integration tests** (`*IntegrationTest`) run under failsafe and use `@ExtendWith(RequiresDockerCondition.class)`. Without Docker they are skipped locally; with `CI=true` the build fails instead.
- **Unit tests** extend `BaseSqlWorkbenchTest`. It sets up an H2 backend `h2` with the snake_case DDL from `src/test/resources/sql-workbench-h2.sql`, MOCK auth, and a session customizer. The session id prefix picks the permissions: `reader-*` gets access, `writer-*` gets access plus write plus share, anything else gets nothing.
- **Unit tests** extend `BaseSqlWorkbenchTest`. It sets up an H2 backend `h2` with the snake_case DDL from `src/test/resources/sql-workbench-h2.sql`, MOCK auth, and a session customizer. The session id prefix picks the permissions: `reader-*` gets the user permission list, `writer-*` adds write and share, `admin-*` gets the admin list, anything else gets nothing. `loginAs(user, groups...)` / `as(user)` also apply the security keys; notebook calls must run with the matching session. `TableSecurityMatrixTest` is the role x table x action matrix.
## Architecture
@@ -37,15 +37,22 @@ How tests are split:
- `ExecutionAuditor` writes audits through QQQ actions as `QSystemUserSession`.
- `explain/`: `PlanParser` (Postgres JSON, MySQL JSON/text, SQLite rows, text) and `PlanNode`.
- `schema/`: `SchemaIntrospector` (JDBC `DatabaseMetaData` plus the QQQ table and field to SQL name mapping), cached in the runtime.
- `notebook/`: `NotebookService` (CRUD, visibility, versions in one transaction, snapshots).
- `notebook/`: `NotebookService` (CRUD, sharing, versions in one transaction, snapshots), run as the caller's session through the standard table actions; `NotebookVersioning` (one version per change, as the system user).
- `api/`: `SqlWorkbenchApi` (transport-agnostic, permission checks), `SqlWorkbenchRouteProvider` (Javalin 7 routes under `/qqq/v1/sql-workbench`), `ResultExporter` (CSV/JSON/XLSX via fastexcel), and `SqlWorkbenchRoutes`.
- `metadata/`: the 5 tables, the notebook PVS, the `SQL_WORKBENCH` widget and its renderer, and the `sqlWorkbench` app.
- `metadata/`: the 6 tables (record locks, joins, child-list widgets), the security key types, the PVSs, the record actions, the `SQL_WORKBENCH` widget and its renderer, the home dashboard widgets, the Runner child app, and the `sqlWorkbench` app (nav: Runner, then the main tables only).
- `home/`: the dashboard data (`SqlWorkbenchHome`, read through core actions in the viewer's session so locks apply), its renderer, and inline-styled HTML helpers.
- `SqlWorkbenchIntegrations`: opt-in ESB and Quick Search hooks for the main tables.
- `customizers/`: table customizers holding the notebook invariants for every entry point; `SqlWorkbenchSecurity` puts the lock keys in sessions.
- `processes/`: the record-action steps (Share, Duplicate, Restore Version).
- `developer/`: `SqlWorkbenchDeveloperPanelProvider` (the "SQL Workbench" record developer panel, registered by `metadata/SqlWorkbenchDeveloperPanelsMetaDataProducer`) and `RecordSql` (read-only SELECTs in backend names, dialect quoting and literals).
**Invariants (do not break):**
- Never log SQL text, parameters, or results. Only the audit table holds them.
- Audit `start()` runs before any SQL. If it fails, the SQL does not run.
- Audit updates are deferred until the session's connection is released (`Deferred` in the engine), so the engine never needs two pooled connections at once.
- Engine-side session settings must be transaction-scoped (`SET LOCAL`) or reverted in `SessionGuard.close`.
- Workbench rows are read and written as the caller (record locks apply). Only the audit writes, version bookkeeping, cascades and share-visibility sync run as the system user, after QQQ authorized the change. Never re-hide the tables or bypass the locks to fix a permission problem.
- Table customizers hold the notebook invariants; the notebook UI and the table UI must behave the same.
- SQL errors are results (`status` FAILED, TIMED_OUT, or CANCELLED). Exceptions are only for 400, 401, 403, and 404.
## Code Style
@@ -58,7 +65,7 @@ How tests are split:
## Dependencies
- Java 21.
- Parent `com.kingsrook:qbit-build-parent:2.0.0` (qqq 4.0.0). The default-active `qqq-snapshot` profile imports `qqq-bom-pom:4.1.0-RC.2` ahead of it, as in qbit-quick-search (ADR-0007). Remove that activation when the parent moves to 4.1.
- Parent `com.kingsrook:qbit-build-parent:2.0.0` (qqq 4.0.0). The default-active `qqq-snapshot` profile imports `qqq-bom-pom:4.1.0-RC.4` ahead of it (RC.4 brings the record developer panel contract), as in qbit-quick-search (ADR-0007). Remove that activation when the parent moves to 4.1.
- `qqq-backend-core`, `qqq-backend-module-rdbms`, and `qqq-middleware-javalin` (optional).
- Tests: JUnit 5, AssertJ, Mockito, H2, `qqq-backend-module-sqlite`/`-postgres` (drivers), Testcontainers 1.21.4.
+47
View File
@@ -2,6 +2,53 @@
All notable changes to this project are documented here. The format follows Keep a Changelog; versions follow semantic versioning.
## [1.0.0-RC.3] - Unreleased
### Added
- **App home dashboard.** The `sqlWorkbench` app's home is now an information dashboard of real data, read in the viewer's session (permissions and record locks apply): summary tiles and links, runs per day, recent notebooks, recent runs, slowest and failed queries, most-used notebooks, shared notebooks, committed writes, and (workbench admins only) activity by user. Each widget has an honest empty state.
- **Runner.** The notebook editor (`SQL_WORKBENCH` widget) moves to its own nav entry: the `sqlWorkbenchRunner` child app, labeled Runner. Open a notebook with `/app/sqlWorkbenchRunner?notebook=<id>`.
- On a notebook's record view: an Open in Runner widget, and a Runs child list (its executions the viewer may see) through the new join `sql_workbench_notebook_join_sql_workbench_execution`.
- `SqlWorkbenchIntegrations`: opt-in ESB (`esbTableNames`) and Quick Search (`quickSearchTables`) hooks for the main tables, with no dependency on either module.
- **Record developer panel** "SQL Workbench" (`SqlWorkbenchDeveloperPanelProvider`, registered by `SqlWorkbenchDeveloperPanelsMetaDataProducer`): read-only `SELECT`s for the record and its table in the backend's own names, backend details, and links that open them in the Runner as a new cell (`/app/sqlWorkbenchRunner?sql=&backend=`, never run). Requires QQQ 4.1.0-RC.4 (the developer panel contract) and a Next UI with the Runner `?sql=` support.
### Changed
- **The nav lists only the main tables**: Notebooks and the execution audit, after the Runner. Cells, versions, pinned results and shares are child lists on a notebook (still standard tables, reachable by URL and API). The app's widget list is now the dashboard, not the `SQL_WORKBENCH` widget; links to `/app/sqlWorkbench` that expected the editor should point at `/app/sqlWorkbenchRunner`.
- The notebook record's header shows the description.
- **Names follow the owner rule: lowercase snake_case, prefixed with the qbit's name (metadata only, no DDL).** Tables `sql_workbench_notebook`, `sql_workbench_cell`, `sql_workbench_notebook_version`, `sql_workbench_result_snapshot`, `sql_workbench_notebook_share`, `sql_workbench_execution` (they now equal their backend table names); possible-value source `sql_workbench_share_audience_type`; permissions `sql_workbench.*`, `sql_workbench_admin.hasAccess` and `<table>.read` etc.; security key types `sql_workbench_user` (all-access `sql_workbench_all_access`) and `sql_workbench_audience`; joins `sql_workbench_notebook_join_sql_workbench_*`.
- `tableNamePrefix` (and the QBit namespace) is normalized to lowercase snake_case and joined with one `_`.
### Deprecated
- `LEGACY_TABLE_NAME` on each entity, `ShareAudienceType.LEGACY_NAME` and `SqlWorkbenchQBitConfig.LEGACY_*` hold the pre-RC.3 names, for migrating stored references only.
### Upgrade from RC.2
- Follow [UPGRADE.md](UPGRADE.md): grant the renamed permissions and rename stored table names (audit, saved views and reports). No DDL.
## [1.0.0-RC.2] - Unreleased
The workbench's data becomes standard QQQ tables, secured by QQQ's own mechanisms instead of being hidden ([#3](https://github.com/QRun-IO/qbit-sql-workbench/issues/3)). Requires QQQ `4.1.0-RC.4` or later ([qqq#1073](https://github.com/QRun-IO/qqq/issues/1073)).
### Added
- `sqlWorkbenchNotebookShare`: share a notebook, read-only, with a user, a group, or everyone (`ALL`, the shared library). Publishing in the notebook UI now writes an `ALL` share; a notebook's `visibility` follows its shares.
- Record security locks on every table, enforced by QQQ's core actions (table UI, `/qqq/v1` table API, export, PVS lookups, processes, the workbench endpoints): owners see and change their notebooks and children, share audiences read them, users read their own execution audit, admins (`sqlWorkbenchAdmin.hasAccess`) hold the all-access key. Security key types `sqlWorkbenchUser` and `sqlWorkbenchAudience`; `SqlWorkbenchSecurity.applyToSession(qInstance, session, groups)` puts the keys in a session.
- Per-table `read` / `insert` / `edit` / `delete` permissions, listed per role by `SqlWorkbenchQBitConfig.getUserPermissionNames()`, `getSharePermissionNames()` and `getAdminPermissionNames()`.
- All six tables are children of the `sqlWorkbench` app, with record-view sections, child-list widgets (cells, shares, pinned results, versions) and SQL / JSON code-editor adornments.
- Record actions (processes on tables, not in the nav): Share Notebook, Duplicate Notebook, Restore Version.
- Table customizers that keep the notebook invariants for every entry point: ownership from the session, cell validation and ordering, one version per change, share keys and visibility, append-only versions and audit (also for admins), pinned results only from pinning, cascading notebook deletes.
- Record-level QQQ audits for notebooks, cells, pinned results and shares.
### Changed
- `NotebookService` and the history endpoint run as the caller's session through the standard table actions (no system-user bypass); the explicit owner / share checks stay as defense in depth.
- The tables' permission rules are per-table permissions instead of `sqlWorkbenchAdmin.hasAccess`. Hosts must grant the new lists and call `applyToSession`.
- Built against QQQ `4.1.0-RC.4`.
### Upgrade from RC.1
- Run the share-table DDL from `docs/ddl/postgres.sql`: it creates `sql_workbench_notebook_share` and backfills an `all` share for each `SHARED` notebook (additive, idempotent; owners unchanged).
- Grant the permission lists and call `SqlWorkbenchSecurity.applyToSession` in the authentication customizer.
## [1.0.0-RC.1] - 2026-10-11
First release candidate of 1.0, published to Maven Central from `release/1.0`. It contains every change listed under 1.0.0 below. Requires QQQ 4.1 (built against 4.1.0-RC.2) and a Next UI release that renders the `SQL_WORKBENCH` widget (`v1.0.0-RC.13` or later).
## [1.0.0] - Unreleased
First release. Requires QQQ 4.1 (built against 4.1.0-RC.2).
+12 -5
View File
@@ -4,7 +4,7 @@ This file provides guidance to Claude Code when working with code in this reposi
## Project Overview
**QBit SQL Workbench**: a QQQ QBit that provides a super-user SQL notebook. Sessions are read-only by default, write mode is gated, and everything is audited. It works over any RDBMS backend of the instance. The UI is the Next UI `SQL_WORKBENCH` widget. Design: `docs/DESIGN.md`.
**QBit SQL Workbench**: a QQQ QBit that provides a super-user SQL notebook. Sessions are read-only by default, write mode is gated, and everything is audited. It works over any RDBMS backend of the instance. The UI is the Next UI `SQL_WORKBENCH` widget on the Runner page; the app's home is a dashboard (`home/`). Design: `docs/DESIGN.md`.
## Build Commands
@@ -18,7 +18,7 @@ CI=true ./mvnw verify # Unit + Postgres integration tests (Docker) + JaCoCo ga
How tests are split:
- **Integration tests** (`*IntegrationTest`) run under failsafe and use `@ExtendWith(RequiresDockerCondition.class)`. Without Docker they are skipped locally; with `CI=true` the build fails instead.
- **Unit tests** extend `BaseSqlWorkbenchTest`. It sets up an H2 backend `h2` with the snake_case DDL from `src/test/resources/sql-workbench-h2.sql`, MOCK auth, and a session customizer. The session id prefix picks the permissions: `reader-*` gets access, `writer-*` gets access plus write plus share, anything else gets nothing.
- **Unit tests** extend `BaseSqlWorkbenchTest`. It sets up an H2 backend `h2` with the snake_case DDL from `src/test/resources/sql-workbench-h2.sql`, MOCK auth, and a session customizer. The session id prefix picks the permissions: `reader-*` gets the user permission list, `writer-*` adds write and share, `admin-*` gets the admin list, anything else gets nothing. `loginAs(user, groups...)` / `as(user)` also apply the security keys; notebook calls must run with the matching session. `TableSecurityMatrixTest` is the role x table x action matrix.
## Architecture
@@ -37,15 +37,22 @@ How tests are split:
- `ExecutionAuditor` writes audits through QQQ actions as `QSystemUserSession`.
- `explain/`: `PlanParser` (Postgres JSON, MySQL JSON/text, SQLite rows, text) and `PlanNode`.
- `schema/`: `SchemaIntrospector` (JDBC `DatabaseMetaData` plus the QQQ table and field to SQL name mapping), cached in the runtime.
- `notebook/`: `NotebookService` (CRUD, visibility, versions in one transaction, snapshots).
- `notebook/`: `NotebookService` (CRUD, sharing, versions in one transaction, snapshots), run as the caller's session through the standard table actions; `NotebookVersioning` (one version per change, as the system user).
- `api/`: `SqlWorkbenchApi` (transport-agnostic, permission checks), `SqlWorkbenchRouteProvider` (Javalin 7 routes under `/qqq/v1/sql-workbench`), `ResultExporter` (CSV/JSON/XLSX via fastexcel), and `SqlWorkbenchRoutes`.
- `metadata/`: the 5 tables, the notebook PVS, the `SQL_WORKBENCH` widget and its renderer, and the `sqlWorkbench` app.
- `metadata/`: the 6 tables (record locks, joins, child-list widgets), the security key types, the PVSs, the record actions, the `SQL_WORKBENCH` widget and its renderer, the home dashboard widgets, the Runner child app, and the `sqlWorkbench` app (nav: Runner, then the main tables only).
- `home/`: the dashboard data (`SqlWorkbenchHome`, read through core actions in the viewer's session so locks apply), its renderer, and inline-styled HTML helpers.
- `SqlWorkbenchIntegrations`: opt-in ESB and Quick Search hooks for the main tables.
- `customizers/`: table customizers holding the notebook invariants for every entry point; `SqlWorkbenchSecurity` puts the lock keys in sessions.
- `processes/`: the record-action steps (Share, Duplicate, Restore Version).
- `developer/`: `SqlWorkbenchDeveloperPanelProvider` (the "SQL Workbench" record developer panel, registered by `metadata/SqlWorkbenchDeveloperPanelsMetaDataProducer`) and `RecordSql` (read-only SELECTs in backend names, dialect quoting and literals).
**Invariants (do not break):**
- Never log SQL text, parameters, or results. Only the audit table holds them.
- Audit `start()` runs before any SQL. If it fails, the SQL does not run.
- Audit updates are deferred until the session's connection is released (`Deferred` in the engine), so the engine never needs two pooled connections at once.
- Engine-side session settings must be transaction-scoped (`SET LOCAL`) or reverted in `SessionGuard.close`.
- Workbench rows are read and written as the caller (record locks apply). Only the audit writes, version bookkeeping, cascades and share-visibility sync run as the system user, after QQQ authorized the change. Never re-hide the tables or bypass the locks to fix a permission problem.
- Table customizers hold the notebook invariants; the notebook UI and the table UI must behave the same.
- SQL errors are results (`status` FAILED, TIMED_OUT, or CANCELLED). Exceptions are only for 400, 401, 403, and 404.
## Code Style
@@ -58,7 +65,7 @@ How tests are split:
## Dependencies
- Java 21.
- Parent `com.kingsrook:qbit-build-parent:2.0.0` (qqq 4.0.0). The default-active `qqq-snapshot` profile imports `qqq-bom-pom:4.1.0-RC.2` ahead of it, as in qbit-quick-search (ADR-0007). Remove that activation when the parent moves to 4.1.
- Parent `com.kingsrook:qbit-build-parent:2.0.0` (qqq 4.0.0). The default-active `qqq-snapshot` profile imports `qqq-bom-pom:4.1.0-RC.4` ahead of it (RC.4 brings the record developer panel contract), as in qbit-quick-search (ADR-0007). Remove that activation when the parent moves to 4.1.
- `qqq-backend-core`, `qqq-backend-module-rdbms`, and `qqq-middleware-javalin` (optional).
- Tests: JUnit 5, AssertJ, Mockito, H2, `qqq-backend-module-sqlite`/`-postgres` (drivers), Testcontainers 1.21.4.
+126 -22
View File
@@ -2,12 +2,13 @@
A super-user SQL notebook for any [QQQ](https://github.com/QRun-IO/qqq) application.
You write SQL and markdown in notebook cells, run them against any RDBMS backend of the instance (Postgres, MySQL, SQLite, H2), and see typed, paged, exportable results, quick charts, and visual EXPLAIN plans. The notebook UI lives in the [Next UI](https://github.com/QRun-IO/qqq-frontend-next) as a `SQL_WORKBENCH` widget on the qbit's app page.
You write SQL and markdown in notebook cells, run them against any RDBMS backend of the instance (Postgres, MySQL, SQLite, H2), and see typed, paged, exportable results, quick charts, and visual EXPLAIN plans. The notebook UI (the **Runner**) lives in the [Next UI](https://github.com/QRun-IO/qqq-frontend-next) as a `SQL_WORKBENCH` widget on its own page in the app's nav. The app's home page is an information dashboard over the notebooks and the execution audit.
- **Read-only by default.** A default session runs one statement inside a database `READ ONLY` transaction, and a statement classifier refuses anything that isn't a read.
- **Gated write mode.** Writes need a separate permission and two steps. First a dry run executes in a transaction that is rolled back, so you see the affected-row counts. You then confirm with a single-use token bound to that exact SQL. A row guard, a hard cap, a drift check, and statement timeouts apply.
- **Everything is audited.** The `sqlWorkbenchExecution` table records who ran what, when, on which backend, in which mode, how long it took, how many rows it returned or affected, and any error. SQL text and results never go to the logs.
- **Notebooks.** Notebooks are personal by default and can be published to a shared library. They have named `:parameters` (always bound, never spliced), version history, and pinned result snapshots.
- **Everything is audited.** The `sql_workbench_execution` table records who ran what, when, on which backend, in which mode, how long it took, how many rows it returned or affected, and any error. SQL text and results never go to the logs.
- **Notebooks.** Notebooks are personal by default and can be shared, read-only, with users, groups, or everyone (the shared library). They have named `:parameters` (always bound, never spliced), version history, and pinned result snapshots.
- **Standard QQQ tables.** Notebooks, cells, versions, pinned results, shares, and the execution audit are ordinary QQQ tables, with the usual query, record, edit, export, audit, and developer-mode screens. The nav lists the main tables (Notebooks and the execution audit); the others are child lists on a notebook's record view and stay reachable by URL and the table API. Record security locks decide who sees and changes which rows, for every entry point.
> **Raw SQL bypasses QQQ.** It ignores record security locks, table and field permissions, hidden fields, and record locks. It also skips QQQ audits, record-change listeners, automations, ESB events, and search indexing. That is why the workbench is for super-users only. Use QQQ processes for business changes.
@@ -15,7 +16,7 @@ The full design (architecture, data model, security model, limits, API contract,
## Requirements
- Java 21 and QQQ 4.1 (built against `4.1.0-RC.2`).
- Java 21 and QQQ 4.1 (built against `4.1.0-RC.4`, which is required: it lets partial edits of child rows pass joined record locks, [qqq#1073](https://github.com/QRun-IO/qqq/issues/1073)).
- `qqq-backend-module-rdbms` for the targets. `qqq-middleware-javalin` for the REST routes; it is optional, and without it the engine still works but no routes are registered.
- A Next UI release that renders the `SQL_WORKBENCH` widget.
@@ -25,13 +26,13 @@ The full design (architecture, data model, security model, limits, API contract,
<dependency>
<groupId>com.kingsrook.qbits</groupId>
<artifactId>qbit-sql-workbench</artifactId>
<version>1.0.0-RC.1</version>
<version>1.0.0-RC.2</version>
</dependency>
```
```java
SqlWorkbenchQBitConfig config = new SqlWorkbenchQBitConfig()
.withBackendName("postgres") // where the qbit's 5 tables live
.withBackendName("postgres") // where the qbit's 6 tables live
.withTableMetaDataCustomizer(new SnakeCaseCustomizer()) // optional: map names to snake_case
.withReadOnlyCredentials("postgres", new SqlWorkbenchCredentials()
.withUsername("${env.SQLWB_RO_USER}") // optional, recommended: a SELECT-only role
@@ -42,17 +43,49 @@ new SqlWorkbenchQBitProducer().withQBitConfig(config).produce(qInstance);
The producer adds the following to the instance:
- **Tables:** `sqlWorkbenchNotebook`, `sqlWorkbenchCell`, `sqlWorkbenchNotebookVersion`, `sqlWorkbenchResultSnapshot`, `sqlWorkbenchExecution`.
- **A possible value source** over the notebook table.
- **The `sqlWorkbenchWidget` widget** (type `SQL_WORKBENCH`).
- **The `sqlWorkbench` app**, which contains the widget plus the notebook and audit tables. It has no processes.
- **Tables:** `sql_workbench_notebook`, `sql_workbench_cell`, `sql_workbench_notebook_version`, `sql_workbench_result_snapshot`, `sql_workbench_notebook_share`, `sql_workbench_execution`, each with record security locks, per-table permissions, and table customizers ([Permissions](#permissions)).
- **Security key types** `sql_workbench_user` (all-access key `sql_workbench_all_access`) and `sql_workbench_audience`.
- **Joins** from a notebook to its cells, versions, pinned results, shares, and runs (execution audit rows), with child-list widgets on the notebook's record view, plus an Open in Runner widget there.
- **Possible value sources** over the notebook table and the share audience types.
- **Record actions** (processes on a table, never in the nav): Share Notebook, Duplicate Notebook, Restore Version.
- **The `sqlWorkbenchWidget` widget** (type `SQL_WORKBENCH`): the notebook editor.
- **The home dashboard widgets** ([App home](#app-home)).
- **The `sqlWorkbench` app.** Its home page is the dashboard. Its nav lists the **Runner** (`sqlWorkbenchRunner`, a child app whose page is the `SQL_WORKBENCH` widget), then the main tables: Notebooks (`sql_workbench_notebook`) and the execution audit (`sql_workbench_execution`). Cells, versions, pinned results and shares are not in the nav; they are child lists on a notebook.
- **The `/qqq/v1/sql-workbench/*` routes**, registered through `QJavalinMetaData`, as `qqq-esb` does.
To move the app under another app (for example an Admin app), re-parent `SqlWorkbenchQBitProducer.APP_NAME` after producing.
To move the app under another app (for example an Admin app), re-parent `SqlWorkbenchQBitProducer.APP_NAME` after producing; the Runner moves with it. Open a notebook in the Runner with `/app/sqlWorkbenchRunner?notebook=<id>`.
## App home
The app's home page is a dashboard of real data, read through QQQ's core actions in the viewer's session. Permissions and record locks therefore decide what each widget counts: a user sees their own runs and the notebooks they can open, and a workbench admin sees everyone's. A widget whose table the user may not read says so rather than showing zeros. Every widget is full width and has an empty state.
| Widget | Shows |
|---|---|
| SQL Workbench | Tiles: notebooks (yours, shared with you), runs today, runs and failures in 7 days, committed writes and rows affected in 30 days. Links to the Runner, Notebooks and the execution audit. |
| Runs per Day | Runs, failures and writes per day, last 30 days. |
| Recent Notebooks | Last changed notebooks, owner, sharing, with Open in Runner links. |
| Recent Runs | Latest runs: when, who, SQL (linked to the audit row), backend, notebook, status, duration, rows. |
| Slowest Queries, 30 Days | Longest runs, linked. |
| Failed Queries, 30 Days | Failed, timed-out, rejected and rolled-back runs with their error, linked. |
| Most-Used Notebooks, 30 Days | Notebooks by runs, with users and last run. |
| Shared Notebooks | Notebooks shared with users, groups or everyone, and since when. |
| Writes | Committed writes (gated write mode): who, what, when, rows affected, result, and whether write mode is on. |
| Activity by User, 30 Days | Runs, failures and writes per user. Workbench admins only. |
The home owns this data; a host's other dashboards should link to it rather than repeat it.
## ESB and Quick Search
`SqlWorkbenchIntegrations` is an opt-in hook: it names what to wire, and the host wires it with its own modules (the qbit depends on neither).
- **ESB:** `esbTableNames(config)` lists the main tables. Publish their insert, update and delete events the usual way, for example `EsbTableMetaData.ofOrWithNew(qInstance.getTable(name)).withPublication(new EsbTablePublication().withDestinationName(...).withEvents(...))`. Execution rows are inserted when a run starts and updated when it ends.
- **Quick Search:** `quickSearchTables(config)` lists the main tables with weighted fields, the basepull timestamp field and a hit label. Notebooks: title, description, owner, default backend. Executions: SQL text, execution UUID, user, error, backend. Map each to a `SearchableTableConfig` (with `new SearchableFieldConfig(name).withWeight(w)`). Hits are re-read through the tables' record locks, so a user finds only what they could open. The Quick Search record developer panel then covers these tables too.
What leaves the database: a notebook event or search document carries the notebook row. An execution event carries the audit row, which includes the SQL text and parameter values, and its search document includes the SQL text. Results are never stored, so they are never published or indexed. Decide whether that suits your broker and search cluster before you opt in.
## Storage
Create the five tables on `backendName`. [docs/ddl/postgres.sql](docs/ddl/postgres.sql) has idempotent PostgreSQL DDL that you can paste into a Liquibase formatted-SQL changeset.
Create the six tables on `backendName`. [docs/ddl/postgres.sql](docs/ddl/postgres.sql) has idempotent PostgreSQL DDL that you can paste into a Liquibase formatted-SQL changeset.
The DDL uses snake_case names: `sql_workbench_notebook`, `owner_user_id`, and so on. Match them with a table customizer:
@@ -68,25 +101,70 @@ public class SnakeCaseCustomizer implements MetaDataCustomizerInterface<QTableMe
}
```
## Storage upgrade from 1.0.0-RC.1
RC.2 adds `sql_workbench_notebook_share`. The DDL creates it and backfills an `all` share (the shared library) for every notebook whose `visibility` is `SHARED`; it is additive and idempotent, and owners (`owner_user_id`) are unchanged. Run it before deploying RC.2: without the share rows, published notebooks are visible only to their owners.
## Permissions
Everything is denied by default.
Everything is denied by default. Two standard QQQ layers apply:
| Permission | Grants |
|---|---|
| `sqlWorkbench.hasAccess` | The app, widget, and every workbench endpoint: read-only SQL, EXPLAIN, schema, your notebooks and the shared library, your history |
| `sqlWorkbench.write` | Write mode (preview and commit) and EXPLAIN ANALYZE |
| `sqlWorkbench.share` | Publishing your notebooks to the shared library |
| `sqlWorkbenchAdmin.hasAccess` | The qbit's five tables in the generic QQQ table UI / API (`/qqq/v1/table/...`): **every** user's notebooks (personal ones included), cells, versions, pinned result snapshots (real query data), and the full execution audit |
1. **Permissions** say which operations a user has on each table (QQQ's per-table `read`, `insert`, `edit`, `delete`) and on the endpoints.
2. **Record security locks** say which rows. QQQ's core actions enforce them, so they apply to the table UI, the `/qqq/v1` table API, exports, possible-value lookups, processes, and the workbench's own endpoints alike.
The names are configurable (`accessPermissionBaseName`, `writePermissionName`, `sharePermissionName`, `adminPermissionBaseName`).
| Role | Grant | What it allows |
|---|---|---|
| User | `config.getUserPermissionNames()`: `sql_workbench.hasAccess`, plus read/insert/edit/delete on notebooks and cells, read on versions, shares and the audit, and read/edit/delete on pinned results | The app, widget and endpoints (read-only SQL, EXPLAIN, schema). Their own notebooks, read-only access to notebooks shared with them or in the library, and their own execution history. |
| Writer | `config.getWritePermissionName()` (`sql_workbench.write`) | Write mode (preview and commit) and EXPLAIN ANALYZE. |
| Sharer | `config.getSharePermissionNames()`: `sql_workbench.share`, plus insert/edit/delete on shares | Sharing their own notebooks with users, groups, or everyone (publishing to the library). |
| Admin | `config.getAdminPermissionNames()`: the user permissions plus `sql_workbench_admin.hasAccess` | The all-access key: every user's notebooks, cells, versions, pinned results (real query data), shares, and the whole execution audit, and editing any notebook. |
Grant `sqlWorkbenchAdmin.hasAccess` only to administrators or auditors. Ordinary workbench users do not need it: the workbench endpoints check ownership, visibility, and the share permission themselves, then read and write the tables as the system user. Without the admin permission a user cannot reach another user's personal notebook or audit row through the generic table API. Cells, versions, snapshots, and the audit are read-only even for admins (insert, edit, and delete are disabled; only the qbit writes them).
The names are configurable (`accessPermissionBaseName`, `writePermissionName`, `sharePermissionName`, `adminPermissionBaseName`). Table permissions follow the table names, including any `tableNamePrefix`. QQQ 4.1 does not list `write` and `share` as available permissions, so grant them where you grant roles, for example in an authentication customizer.
`write` and `share` are plain session permissions. QQQ 4.1 does not list them as available permissions, so grant them where you grant roles, for example in an authentication customizer.
**Security keys.** Call `SqlWorkbenchSecurity.applyToSession(qInstance, session, groups)` from your authentication customizer, after the session's user and permissions are set. It puts these keys in the session:
| Key | Values | Used by |
|---|---|---|
| `sql_workbench_user` | the user's id (`QUser.idReference`) | owner locks: a notebook's `ownerUserId` (its children reach it through the notebook) and an execution's `userId` |
| `sql_workbench_audience` | `user:<id>`, `group:<name>` for each group passed, and `all` | share locks (read only): a share row's `audienceKey` |
| `sql_workbench_all_access` | `true` for holders of the admin permission | every row |
A session without the access permission gets no keys, so it finds no workbench rows at all, even through code paths that skip table permissions. If the host never calls `applyToSession`, the workbench endpoints add the user and `all` keys themselves, but group shares need the host to pass the groups.
**What the locks and customizers enforce:**
| Table | Read | Write |
|---|---|---|
| Notebook | the owner, a share audience of the session, or an admin | the owner or an admin. The owner and visibility are system-managed. |
| Cell, version, pinned result | as their notebook | the owner or an admin, through the notebook. Versions are append-only. |
| Share | the notebook's owner, the audience it names, or an admin | the owner or an admin, with the share permission |
| Execution (audit) | the user who ran it, or an admin | nobody: append-only, only the qbit writes it |
`writeModeEnabled=false` turns write mode off for everyone.
## Record developer panel
With QQQ 4.1.0-RC.4 or later, the qbit registers a **SQL Workbench** panel for a record's developer view (`/app/{table}/{id}/dev` in the Next UI). It shows for tables on an RDBMS backend the workbench targets (honoring `allowedBackendNames` / `excludedBackendNames`), and only to users with `sql_workbench.hasAccess`. It contains:
- A read-only `SELECT` for the record and one for the table (`LIMIT 100`), in the backend's own table and column names (aliased to the QQQ field names), with identifiers quoted and the primary key written as an escaped literal for the dialect.
- The backend, dialect, database table, and whether read-only sessions use the backend's read-only replica.
- Links that open each query in the workbench (`/app/sqlWorkbenchRunner?sql=...&backend=...`, the Runner, with the table name prefix applied). The Runner opens the SQL as a new cell and does not run it.
The panel only builds text. What runs is what the user executes, under their workbench permissions; the write permission does not change the panel.
## Administering through the tables
Everything the Runner does can be done and inspected through the tables. Notebooks and the execution audit are in the SQL Workbench app's nav; the others open from a notebook's record view (or by URL). The same customizers run either way.
- **Notebooks**: create, rename, describe, delete (which removes its cells, versions, pinned results, and shares). The record view has an Open in Runner link and lists the notebook's cells, shares, pinned results, versions, and runs. Record actions: Share Notebook and Duplicate Notebook.
- **Cells**: add (appended at the end, with a generated key), edit, reorder (`sortOrder`), delete. Each change saves a new notebook version, exactly like a save in the notebook UI.
- **Versions**: read-only history. Restore Version saves the chosen version as a new one.
- **Shares**: who can read a notebook. `USER` and `GROUP` shares name their audience, matched without regard to case (so user ids and group names must be case-insensitive in the host); `ALL` publishes the notebook to the shared library. The notebook's `visibility` follows its shares.
- **Pinned results**: relabel or delete. Only pinning a result in the notebook UI creates them.
- **Execution audit**: read, filter, and export. Nobody can edit or delete it through QQQ.
Notebooks, cells, pinned results, and shares get record-level QQQ audits, so SQL text, result data, and share audiences are not copied into the host's audit tables (whose readers the workbench locks do not reach).
## Safety model
> **Production: configure `readOnlyCredentials` for every target backend.** Use a dedicated database role with `SELECT` grants only: no write privileges, not a superuser, no `pg_signal_backend` (or MySQL `PROCESS` / `SUPER` / `CONNECTION_ADMIN`). Without it, read-only mode connects as the application's own role and relies on the classifier and the session's `READ ONLY` transaction alone. On H2 / generic backends there is no enforced read-only transaction, so the classifier is the only control. On Postgres a `READ ONLY` transaction still lets the application role call side-effecting functions such as `pg_terminate_backend()` or `pg_cancel_backend()`. `/capabilities` reports `readOnlyCredentials: false` / `readOnlyEnforced: false` per backend so you can check.
@@ -146,6 +224,32 @@ Base path: `/qqq/v1/sql-workbench`. The contract is in [docs/DESIGN.md §5](docs
POST and PUT bodies must be `application/json`.
## Adopting in another app
A checklist for adding the workbench to a QQQ application. Each row points to the section above that has the details.
| Item | What to do |
|---|---|
| Coordinates | `com.kingsrook.qbits:qbit-sql-workbench`. Current release candidate: `1.0.0-RC.2` (Maven Central, published from `release/1.0`). |
| QQQ version | QQQ 4.1, `4.1.0-RC.4` or later (qqq#1073: partial edits of child rows under joined locks). Java 21. |
| Dependencies | `qqq-backend-module-rdbms` comes in transitively. Add `qqq-middleware-javalin` yourself for the REST routes; it is optional in this qbit's POM, and without it no routes are registered. |
| Frontend | A Next UI (`qqq-frontend-next`) release that renders the `SQL_WORKBENCH` widget: `v1.0.0-RC.13` or later. Other frontends show the app and tables but not the notebook. |
| Registration | `new SqlWorkbenchQBitProducer().withQBitConfig(config).produce(qInstance)` after your backends are in the instance. The producer registers its own QBit metadata; don't add it by hand. To nest the app (for example under Admin), re-parent `SqlWorkbenchQBitProducer.APP_NAME` after producing. |
| Storage | Create the six tables on `backendName` before first use ([Storage](#storage)). From RC.1, run the share-table DDL and its backfill ([upgrade](#storage-upgrade-from-100-rc1)). PostgreSQL DDL ships in [docs/ddl/postgres.sql](docs/ddl/postgres.sql) and drops into a Liquibase formatted-SQL changeset; translate it for other databases. Match the snake_case names with a `tableMetaDataCustomizer`. |
| Targets | Every RDBMS backend in the instance is a target unless you narrow it with `allowedBackendNames` or `excludedBackendNames`. |
| Secrets | In production, configure `readOnlyCredentials` for each target backend with a SELECT-only database role, and optionally `writeCredentials`. Pass them as `${env.X}` references (the example uses `SQLWB_RO_USER` and `SQLWB_RO_PASSWORD`; the names are yours). The qbit reads no other environment variables. |
| Permissions | Grant `config.getUserPermissionNames()` to workbench users, plus `getWritePermissionName()` for write mode, `getSharePermissionNames()` for sharing, and `getAdminPermissionNames()` for administrators and auditors only. See [Permissions](#permissions). |
| Security keys | Call `SqlWorkbenchSecurity.applyToSession(qInstance, session, groups)` in your authentication customizer after setting permissions. Pass the user's groups if you want group shares. |
| Navigation | The app's home is the dashboard; its nav lists the Runner and the two main tables (Notebooks, the execution audit). The other four tables are child lists on a notebook; keep them out of explicit menus. The processes are record actions on the tables; keep them out of the nav. |
| ESB and search | Optional: wire `SqlWorkbenchIntegrations.esbTableNames` and `quickSearchTables` into your ESB and Quick Search config ([ESB and Quick Search](#esb-and-quick-search)). |
| Turning writes off | `withWriteModeEnabled(false)` disables write mode for everyone. |
| Known limitations | Raw SQL bypasses QQQ security, audits, listeners and automations ([Safety model](#safety-model)). H2 and generic backends have no enforced read-only transaction. Only PostgreSQL DDL ships. Shares are read-only (no shared editing). |
| Upgrading | From RC.2: the QQQ names became snake_case (`sql_workbench_*`; no DDL). See [UPGRADE.md](UPGRADE.md) for permissions and stored table names. From RC.1: also run the share-table DDL and backfill, grant the new permission lists (the tables no longer rely on the admin `hasAccess` permission alone), call `applyToSession`, and use QQQ `4.1.0-RC.4` or later. |
Nothing in the qbit is specific to one host application.
See also the [QQQ adoption checklist](https://github.com/QRun-IO/qqq/blob/develop/docs/migration/adoption-checklist.adoc) for the full per-feature list (QQQ upgrade, Next dashboard, QBits, migrations, permissions).
## Build
```bash
+58
View File
@@ -0,0 +1,58 @@
# Upgrading
## To 1.0.0-RC.3: snake_case names (metadata only)
The owner naming rule now applies to every QRun-IO qbit: table names are lowercase snake_case, prefixed with the qbit's name. In 1.0.0-RC.3 the SQL Workbench's QQQ names change to match. **The database does not change**: the physical tables were already `sql_workbench_*` (`docs/ddl/postgres.sql`), so no DDL runs. What changes is the names QQQ uses, and anything your app stored under the old names.
| Kind | Before (RC.1, RC.2) | From RC.3 |
|---|---|---|
| Tables (and the notebook possible-value source) | `sqlWorkbenchNotebook`, `sqlWorkbenchCell`, `sqlWorkbenchNotebookVersion`, `sqlWorkbenchResultSnapshot`, `sqlWorkbenchNotebookShare`, `sqlWorkbenchExecution` | `sql_workbench_notebook`, `sql_workbench_cell`, `sql_workbench_notebook_version`, `sql_workbench_result_snapshot`, `sql_workbench_notebook_share`, `sql_workbench_execution` |
| Enum possible-value source | `sqlWorkbenchShareAudienceType` | `sql_workbench_share_audience_type` |
| Permissions | `sqlWorkbench.hasAccess`, `sqlWorkbench.write`, `sqlWorkbench.share`, `sqlWorkbenchAdmin.hasAccess`, `sqlWorkbenchNotebook.read` etc. | `sql_workbench.hasAccess`, `sql_workbench.write`, `sql_workbench.share`, `sql_workbench_admin.hasAccess`, `sql_workbench_notebook.read` etc. |
| Security key types / all-access key | `sqlWorkbenchUser` (`sqlWorkbenchAllAccess`), `sqlWorkbenchAudience` | `sql_workbench_user` (`sql_workbench_all_access`), `sql_workbench_audience` |
| Joins | `sqlWorkbenchNotebookJoinSqlWorkbenchCell` etc. | `sql_workbench_notebook_join_sql_workbench_cell` etc. |
Unchanged: the `sqlWorkbench` app, the `sqlWorkbenchWidget` widget, the record-action process names, the REST routes, field names, and Java class names.
A `tableNamePrefix` (or QBit namespace) is now normalized to lowercase snake_case and joined with one `_`: `ns` and `ns_` give `ns_sql_workbench_notebook`, and `myNs` gives `my_ns_sql_workbench_notebook`. Prefixed tables follow the same rename.
The old names stay available as `@Deprecated` constants for migration scripts only: `SqlWorkbenchNotebook.LEGACY_TABLE_NAME` (and the same on each entity), `ShareAudienceType.LEGACY_NAME`, and `SqlWorkbenchQBitConfig.LEGACY_*`. They will be removed in 1.1.
### Steps
1. **Backend table names.** If your host mapped the old camelCase names to `sql_workbench_*` (for example with `inferBackendName`), nothing else is needed: the new names are already the backend names, and the mapping becomes a no-op. If your backend tables were really named after the camelCase QQQ names, rename them (`ALTER TABLE "sqlWorkbenchNotebook" RENAME TO sql_workbench_notebook;` and so on) or set the backend table names explicitly.
2. **Permissions.** Grant the new names. If you set the permission names through the config getters (`getUserPermissionNames()`, `getSharePermissionNames()`, `getAdminPermissionNames()`), this is automatic. If roles live in an identity provider or a permissions table, rename them there. If you pinned the old names in config (`withAccessPermissionBaseName("sqlWorkbench")`), you can keep them, but the table permissions still change.
3. **Security keys.** `SqlWorkbenchSecurity.applyToSession` sets the new key types. Custom code that put `sqlWorkbenchUser` or `sqlWorkbenchAudience` values in a session must use the new names (`config.getUserSecurityKeyTypeName()` and so on).
4. **Stored references.** Rename the table names your app stored. A Postgres example, with the usual QQQ table and column names (adjust to your app; run inside one transaction, after a backup):
```sql
BEGIN;
CREATE TEMP TABLE swb_rename (old_name TEXT, new_name TEXT) ON COMMIT DROP;
INSERT INTO swb_rename VALUES
('sqlWorkbenchNotebook', 'sql_workbench_notebook'),
('sqlWorkbenchCell', 'sql_workbench_cell'),
('sqlWorkbenchNotebookVersion', 'sql_workbench_notebook_version'),
('sqlWorkbenchResultSnapshot', 'sql_workbench_result_snapshot'),
('sqlWorkbenchNotebookShare', 'sql_workbench_notebook_share'),
('sqlWorkbenchExecution', 'sql_workbench_execution');
-- audit: QQQ's audit_table holds the QQQ table name
UPDATE audit_table a SET name = r.new_name FROM swb_rename r WHERE a.name = r.old_name;
-- saved views and saved reports
UPDATE saved_view v SET table_name = r.new_name FROM swb_rename r WHERE v.table_name = r.old_name;
UPDATE saved_report s SET table_name = r.new_name FROM swb_rename r WHERE s.table_name = r.old_name;
-- any app table that stores QQQ table names (for example a search index or a shared-record table)
-- UPDATE quick_search_index q SET table_name = r.new_name FROM swb_rename r WHERE q.table_name = r.old_name;
COMMIT;
```
Saved views and reports can also name columns of joined tables (`sqlWorkbenchCell.title`) inside their JSON filter or column lists; search those columns for `sqlWorkbench` and replace the table part. Rebuild any search index whose document ids embed table names.
5. **Frontends.** Links to `/app/sqlWorkbench/sqlWorkbenchNotebook/...` become `/app/sqlWorkbench/sql_workbench_notebook/...`.
6. **Navigation and the notebook UI.** The app's home (`/app/sqlWorkbench`) is now a dashboard; the notebook editor moved to the Runner (`/app/sqlWorkbenchRunner`, `?notebook=<id>` opens one). Update bookmarks and links that expected the editor on the app page. The nav lists only Notebooks and the execution audit; if your host builds its own menus or nav tests, drop the cell, version, pinned-result and share tables from them (they are child lists on a notebook). Nothing to grant: the Runner and the dashboard use the app's access permission, and Activity by User the admin permission.
7. **Optional: ESB and Quick Search.** Wire `SqlWorkbenchIntegrations.esbTableNames` and `quickSearchTables` into your configuration (README, "ESB and Quick Search"). Adding tables to Quick Search needs an index rebuild or a basepull for the existing rows.
## To 1.0.0-RC.2
See the "Upgrade from RC.1" section of [CHANGELOG.md](CHANGELOG.md).
+56 -28
View File
@@ -40,7 +40,7 @@ SQL Workbench is a super-user SQL notebook for any QQQ application. Users write
┌──────────────────────────────────┐ ┌────────────────────────────────────────────┐
│ /app/sqlWorkbench (AppHome) │ GET metaData │ QQQ middleware (Javalin 7) │
│ └ WidgetRenderer │◄───────────────►│ /qqq/v1/metaData (app + widget, filtered │
│ case 'SQL_WORKBENCH' │ │ by sqlWorkbench.hasAccess)│
│ case 'SQL_WORKBENCH' │ │ by sql_workbench.hasAccess)│
│ └ SqlWorkbenchWidget (lazy) │ │ │
│ ├ NotebookList │ /qqq/v1/ │ SqlWorkbenchRouteProvider │
│ ├ NotebookEditor │ sql-workbench/*│ └ SqlWorkbenchApi (session, perms, JSON) │
@@ -64,7 +64,8 @@ SQL Workbench is a super-user SQL notebook for any QQQ application. Users write
|---|---|
| `SqlWorkbenchQBitConfig` | Host-facing config (§7). Implements `QBitConfig` with list-based, null-safe `validate`, `getDefaultBackendNameForTables`, and `getTableMetaDataCustomizer`. |
| `SqlWorkbenchQBitProducer` | `QBitMetaDataProducer<SqlWorkbenchQBitConfig>`, the same pattern as Quick Search. The default `produce()` registers `QBitMetaData`, runs the component producers in `metadata/`, and stamps `sourceQBitName`. `postProduceActions` builds the `SqlWorkbenchRuntime` and registers the Javalin route provider (when Javalin is on the classpath). `produce(QInstance)` adds everything to the instance (Quick Search compat style). |
| `metadata/*` | The 5 tables, the `sqlWorkbenchWidget` widget (type `SQL_WORKBENCH`), and the `sqlWorkbench` app (widget plus notebook and audit tables). No processes (owner rule). |
| `metadata/*` | The 6 tables with their record locks, joins and child-list widgets, the two security key types, the record actions (Share Notebook, Duplicate Notebook, Restore Version: processes on tables, never in the nav), the `sqlWorkbenchWidget` widget (type `SQL_WORKBENCH`), and the `sqlWorkbench` app (widget plus all six tables). |
| `customizers/*` | Table customizers that keep the notebook invariants for every entry point (notebook UI, table UI, `/qqq/v1` API): ownership, cell order and limits, versions, share keys, library visibility, append-only versions and audit, cascades. |
| `SqlWorkbenchRuntime` | Instance-scoped live state, held transient on the config: result cache, running-statement registry, write-preview tokens, schema cache. |
| `sql/*` | `SqlScanner` (dialect-aware lexer), `SqlClassifier` (split plus classify), `NamedParameterSql`. Pure Java, with no JDBC dependency. |
| `engine/*` | `WorkbenchBackends` (target resolution and connection sourcing), `SqlExecutionEngine` (read-only, preview, commit), `SessionGuard` (per-vendor read-only, timeouts, cleanup), `ResultCollector` (typing and limits), `ResultCache`, `WritePreviewTokens`, `ExecutionAuditor`. |
@@ -77,7 +78,7 @@ SQL Workbench is a super-user SQL notebook for any QQQ application. Users write
### 2.2 Frontend (Next UI)
The feature is advertised through **app presence**, the same as ESB. `/qqq/v1/metaData` publishes only an allow-list of supplemental metadata, so a flag would not reach the UI. The qbit's app `sqlWorkbench` carries one widget of type `SQL_WORKBENCH`. Both are gated by `sqlWorkbench.hasAccess`, so users without access never see either one. The generic `/app/[slug]` route renders `AppHome`, then `ConnectedWidget`, then `WidgetRenderer`, which gains:
The feature is advertised through **app presence**, the same as ESB. `/qqq/v1/metaData` publishes only an allow-list of supplemental metadata, so a flag would not reach the UI. The qbit's app `sqlWorkbench` carries one widget of type `SQL_WORKBENCH`. Both are gated by `sql_workbench.hasAccess`, so users without access never see either one. The generic `/app/[slug]` route renders `AppHome`, then `ConnectedWidget`, then `WidgetRenderer`, which gains:
```tsx
case 'SQL_WORKBENCH': return <SqlWorkbenchWidget widgetMetaData={widgetMetaData} /> // React.lazy
@@ -104,9 +105,20 @@ Components (`src/components/sql-workbench/`):
## 3. Data model
Five tables on the host-chosen backend (`config.backendName`). Names can be prefixed (`tableNamePrefix`) or customized (`tableMetaDataCustomizer`, for example snake_case backend names). The Postgres DDL is in [`docs/ddl/postgres.sql`](ddl/postgres.sql). Liquibase users can drop it into a formatted-SQL changeset.
Six tables on the host-chosen backend (`config.backendName`). They are standard QQQ tables in the `sqlWorkbench` app, with the usual query, record, edit, export, audit, and developer-mode screens, and record security locks decide who sees which rows (section 6).
### `sqlWorkbenchNotebook`
```mermaid
erDiagram
sql_workbench_notebook ||--o{ sql_workbench_cell : "cells (ordered)"
sql_workbench_notebook ||--o{ sql_workbench_notebook_version : "versions (append-only)"
sql_workbench_notebook ||--o{ sql_workbench_result_snapshot : "pinned results"
sql_workbench_notebook ||--o{ sql_workbench_notebook_share : "shared with (read)"
sql_workbench_execution }o..o| sql_workbench_notebook : "ran from (no FK)"
```
Names can be prefixed (`tableNamePrefix`) or customized (`tableMetaDataCustomizer`, for example snake_case backend names). The Postgres DDL is in [`docs/ddl/postgres.sql`](ddl/postgres.sql). Liquibase users can drop it into a formatted-SQL changeset.
### `sql_workbench_notebook`
| field | type | notes |
|---|---|---|
| id | INTEGER PK | |
@@ -114,34 +126,48 @@ Five tables on the host-chosen backend (`config.backendName`). Names can be pref
| description | TEXT | |
| ownerUserId | STRING(255) | `QSession.user.idReference`, required |
| ownerName | STRING(255) | display only |
| visibility | STRING(16) | `PERSONAL` (default) or `SHARED` |
| visibility | STRING(16) | `PERSONAL` or `SHARED`; derived from the shares (`SHARED` while an `ALL` share exists), not editable |
| defaultBackendName | STRING(100) | |
| parametersJson | TEXT | `[{"name","type","defaultValue","label"}]` |
| currentVersion | INTEGER | incremented on every save |
| currentVersion | INTEGER | incremented on every content change, from the notebook UI or the tables |
| createDate, modifyDate | DATE_TIME | |
### `sqlWorkbenchCell`
### `sql_workbench_cell`
| field | type | notes |
|---|---|---|
| id | INTEGER PK | |
| notebookId | INTEGER | FK, PVS to notebook |
| cellKey | STRING(64) | client-stable UUID; snapshots and audit refer to it |
| sortOrder | INTEGER | |
| sortOrder | INTEGER | cells added through the table go last |
| cellType | STRING(16) | `SQL` or `MARKDOWN` |
| title | STRING(255) | |
| content | TEXT | SQL or markdown |
| backendName | STRING(100) | null means the notebook default |
| createDate, modifyDate | DATE_TIME | |
### `sqlWorkbenchNotebookVersion`
Append-only history. Each save writes the full notebook plus cells as JSON.
### `sql_workbench_notebook_version`
Append-only history. Each save writes the full notebook plus cells as JSON: a notebook-UI save writes one version; a table edit of the notebook or one of its cells writes one version per change (`NotebookVersioning`, as the system user, after QQQ has authorized the change).
`id, notebookId, versionNumber, contentJson (TEXT), savedByUserId, savedByName, comment, createDate`. Unique key: `(notebookId, versionNumber)`. Retention: `notebookVersionRetention` (default 100 per notebook, oldest pruned on save).
### `sqlWorkbenchResultSnapshot`
Pinned results.
### `sql_workbench_result_snapshot`
Pinned results. Created only by pinning a cached result in the notebook UI (so the data comes from a real run); through the table only `label` can change, and the owner can delete them.
`id, notebookId, cellKey, executionUuid, label, backendName, sqlText (TEXT), resultJson (TEXT: columns plus up to snapshotMaxRows rows), rowCount, truncated, createdByUserId, createDate`.
### `sqlWorkbenchExecution` (audit, append-only)
### `sql_workbench_notebook_share`
Who, besides the owner, may read a notebook (read only; no shared editing).
| field | type | notes |
|---|---|---|
| id | INTEGER PK | |
| notebookId | INTEGER | FK, PVS to notebook; cannot change |
| audienceType | STRING(16) | `USER`, `GROUP`, or `ALL` (the shared library) |
| audienceId | STRING(255) | the user id or group name; null for `ALL` |
| audienceKey | STRING(300) | computed: `user:<id>`, `group:<name>` (lower-cased), or `all`; unique per notebook; what the share locks match |
| createdByUserId, createdByName | STRING(255) | |
| createDate, modifyDate | DATE_TIME | |
Publishing in the notebook UI inserts (or deletes) the `ALL` share; the share customizer keeps the notebook's `visibility` in step.
### `sql_workbench_execution` (audit, append-only)
| field | type | notes |
|---|---|---|
| id | INTEGER PK | |
@@ -164,14 +190,14 @@ Pinned results.
| clientIp | STRING(64) | from the request, when available |
| createDate | DATE_TIME | |
The audit table has its insert, edit, and delete capabilities disabled in the QQQ metadata, so the UI and generic API cannot change it. The engine writes it through QQQ actions with a system session context. The row is inserted **before** execution (`RUNNING`). If that insert fails, the SQL **does not run** (fail closed). The row is updated at the end. Retention: `auditRetentionDays` (default unlimited; the host prunes).
The audit table has its insert, edit, and delete capabilities disabled in the QQQ metadata, so the UI and generic API do not offer those operations, and its customizer rejects them in the core actions for every session but the system user (admins included). Its record lock shows each user their own rows (admins all). The engine writes it through QQQ actions with a system session context. The row is inserted **before** execution (`RUNNING`). If that insert fails, the SQL **does not run** (fail closed). The row is updated at the end. Retention: `auditRetentionDays` (default unlimited; the host prunes).
---
## 4. Execution engine
### 4.1 Pipeline (every run)
1. **Authorize.** The session must have `sqlWorkbench.hasAccess` (403 otherwise). Write and EXPLAIN ANALYZE also need `sqlWorkbench.write` and `config.writeModeEnabled`.
1. **Authorize.** The session must have `sql_workbench.hasAccess` (403 otherwise). Write and EXPLAIN ANALYZE also need `sql_workbench.write` and `config.writeModeEnabled`.
2. **Resolve the target.** The backend must be an `RDBMSBackendMetaData` in the instance and allowed by `allowedBackendNames` / `excludedBackendNames` (404 otherwise). The dialect comes from the vendor.
3. **Validate.** Check the SQL length (`maxSqlLength`, default 100 KB), then split and classify with `SqlClassifier`. Rules:
- `FORBIDDEN` statements are rejected in every mode. On MySQL this includes any statement with an executable comment (`/*! ... */`, `/*M! ... */`) or optimizer hint (`/*+ ... */`): MySQL runs their contents, so the scanner keeps them as tokens instead of skipping them, and fails closed. MySQL `--` comments need trailing whitespace (`1--1` is arithmetic).
@@ -207,7 +233,7 @@ There is no session-level `SET` leakage: user SQL can never run `SET` / `RESET`
> **Defense in depth.** The READ ONLY transaction is the primary control on Postgres, MySQL, and SQLite. On H2 the classifier is the only control, so production should use Postgres or MySQL with `readOnlyCredentials` (a role with `SELECT` grants only). A READ ONLY transaction does not stop side-effecting *functions* that are not writes, such as `pg_terminate_backend()` or `pg_sleep()`. Only a least-privilege role does. That is why `readOnlyCredentials` is recommended for production.
### 4.3 Write mode
Write mode needs `sqlWorkbench.write`. It is a two-step protocol:
Write mode needs `sql_workbench.write`. It is a two-step protocol:
1. **Preview**: `POST /execute {mode: "WRITE"}` with no `confirmToken`.
- Classify the statements and reject `FORBIDDEN` ones.
@@ -276,7 +302,7 @@ The response is `{executionUuid, dialect, analyze, plan: PlanNode, raw: string}`
SQL errors are results, not HTTP errors: `/execute` and `/explain` return 200 with `status` FAILED, TIMED_OUT, or CANCELLED plus `error` and `sqlState`.
All endpoints need an authenticated QQQ session (cookie or Authorization header, via `ExecutorSessionUtils.setupSession`) and `sqlWorkbench.hasAccess`. Errors are `{"error": "..."}` with 400, 401, 403, 404, 409 (a re-used `executionUuid`), or 500, through `QJavalinUtils.handleException`. Execution UUIDs are case-insensitive on every path (`/execute`, `/executions/{uuid}/rows`, `/export`, `/cancel`, snapshots); the server stores and returns them lower-case. Bodies are JSON. Request and response bodies are never logged.
All endpoints need an authenticated QQQ session (cookie or Authorization header, via `ExecutorSessionUtils.setupSession`) and `sql_workbench.hasAccess`. Errors are `{"error": "..."}` with 400, 401, 403, 404, 409 (a re-used `executionUuid`), or 500, through `QJavalinUtils.handleException`. Execution UUIDs are case-insensitive on every path (`/execute`, `/executions/{uuid}/rows`, `/export`, `/cancel`, snapshots); the server stores and returns them lower-case. Bodies are JSON. Request and response bodies are never logged.
```yaml
GET /capabilities
@@ -308,7 +334,7 @@ POST /notebooks body: Notebook -> Notebook (version 1)
GET /notebooks/{id} -> Notebook (with cells, canEdit)
PUT /notebooks/{id} body: Notebook -> Notebook (cells replaced, new version)
DELETE /notebooks/{id} -> { deleted: true }
POST /notebooks/{id}/publish body: { shared: bool } (needs sqlWorkbench.share)
POST /notebooks/{id}/publish body: { shared: bool } (needs sql_workbench.share)
POST /notebooks/{id}/duplicate -> Notebook (personal copy)
GET /notebooks/{id}/versions -> { versions: [{versionNumber, savedByName, comment, createDate}] }
GET /notebooks/{id}/versions/{n} -> { versionNumber, notebook: Notebook }
@@ -323,7 +349,7 @@ DELETE /notebooks/{id}/snapshots/{snapshotId}
Visibility rules:
- A user can read their own notebooks and all `SHARED` ones.
- Only the owner can edit, delete, snapshot, or restore.
- Publishing or unpublishing needs `sqlWorkbench.share` **and** ownership.
- Publishing or unpublishing needs `sql_workbench.share` **and** ownership.
- Anyone with access can duplicate a readable notebook.
---
@@ -332,7 +358,7 @@ Visibility rules:
| Concern | Control |
|---|---|
| Who | Deny by default. Every endpoint needs `sqlWorkbench.hasAccess` (the app and widget permission base name `sqlWorkbench`, at `HAS_ACCESS_PERMISSION`). Write mode and EXPLAIN ANALYZE need `sqlWorkbench.write`. Publishing to the library needs `sqlWorkbench.share`. The qbit's tables use a separate base name, `sqlWorkbenchAdmin` (`sqlWorkbenchAdmin.hasAccess`), so only administrators reach them through the generic QQQ table UI / API. The names are configurable. |
| Who | Deny by default. Every endpoint needs `sql_workbench.hasAccess` (the app and widget permission base name `sqlWorkbench`, at `HAS_ACCESS_PERMISSION`). Write mode and EXPLAIN ANALYZE need `sql_workbench.write`. Sharing (including publishing to the library) needs `sql_workbench.share` plus insert/edit/delete on the share table. The tables use QQQ's per-table `READ_INSERT_EDIT_DELETE_PERMISSIONS`; `SqlWorkbenchQBitConfig.getUserPermissionNames / getSharePermissionNames / getAdminPermissionNames` list what to grant per role. `sql_workbench_admin.hasAccess` grants the all-access key. The names are configurable. |
| What can run | Read-only transactions by default (§4.2); classifier as defense in depth; `FORBIDDEN` statements in every mode; one statement per read-only run. |
| Writes | Separate permission, two-step preview then commit with a server-side single-use token bound to the user, SQL, and backend, a dry run with rollback, a row guard with acknowledgement, a hard cap, a drift check, and a statement timeout. |
| Credentials | Never returned or logged. Backend metadata goes out only as `{name, label, vendor, dialect}`. `readOnlyCredentials` support `${env.X}` / `${prop.X}` interpolation (`QMetaDataVariableInterpreter`), and the config's `toString` redacts them. |
@@ -340,7 +366,9 @@ Visibility rules:
| Session and CSRF | QQQ middleware session handling. The cookie session is `HttpOnly`; there is no extra CSRF token, matching the rest of `/qqq/v1` (the JSON-only POSTs need `Content-Type: application/json`, which a cross-site form cannot send without a CORS preflight). The route provider rejects non-JSON POST bodies. |
| Isolation between users | Result cache, cancellation, history, and write tokens are keyed by the user's `idReference`. Another user's execution UUID returns 404. |
| QQQ semantics | **Raw SQL bypasses QQQ**: no record security locks, table/field permissions, hidden fields, audit of record changes, or customizers/automations. Rows changed in write mode do **not** trigger QQQ audits, record-change listeners, ESB events, or Quick Search indexing. This is documented in the UI (a banner in write mode) and in the README. Use QQQ processes for business changes. |
| Personal vs shared | A personal notebook is visible only to its owner. The five tables need `sqlWorkbenchAdmin.hasAccess`, which ordinary workbench users do not hold, so `/qqq/v1/table/...` cannot read or write other users' notebooks, cells, versions, pinned snapshots, or audit rows. `NotebookService` and the history endpoint check owner / visibility / share permission in code, then read and write as the system user (`SystemUser`), as `ExecutionAuditor` does for the audit. Cells, versions, snapshots, and the audit have insert / edit / delete disabled; only the qbit writes them. Holders of the admin permission see every user's notebooks and the full audit. |
| Which rows | Record security locks, enforced by QQQ's core actions and therefore for every entry point (table UI, `/qqq/v1` table API, exports, PVS lookups, processes, the workbench endpoints). Two key types: `sql_workbench_user` (the session's user id; all-access key `sql_workbench_all_access` for admins) and `sql_workbench_audience` (`user:<id>`, `group:<name>` per group, `all`). A notebook's lock is OR(owner lock on `ownerUserId`, read-scoped share lock on `sql_workbench_notebook_share.audienceKey` through the share-to-notebook join); its cells, versions and pinned results lock through their notebook (one and two joins); a share row is readable by the notebook's owner or the audience it names; an execution by its `userId`. Writes evaluate only the write-scoped locks, so being shared with is read-only. The RDBMS module de-duplicates rows that match through several shares (`SELECT DISTINCT`, `COUNT(DISTINCT)`). Hosts call `SqlWorkbenchSecurity.applyToSession` from their authentication customizer; sessions without the access permission get no keys and see no rows. |
| Invariants | Table customizers, shared by the notebook UI and the tables: the owner is the inserting session (only the system user may insert for someone else); `visibility`, `currentVersion` and dates are system-managed; cells are validated (type, length, key) and appended in order, never move notebooks, and every change writes a version; shares compute `audienceKey`, need the share permission, and keep `visibility` in step; pinned results are only created by pinning and only relabeled; versions and the audit are append-only for everyone but the system user. `NotebookService` runs as the caller through the same actions (flagging a whole-notebook save so it is versioned once) and keeps its explicit owner / share checks as defense in depth; only version bookkeeping and cascades run as the system user, after QQQ authorized the change. |
| QQQ dependency | Partial updates of child rows (a cell's content, a pinned result's label) pass the joined write locks only with QQQ `4.1.0-RC.4`+ (qqq#1073: locks are validated against the stored row overlaid with the written values). |
---
@@ -358,9 +386,9 @@ Visibility rules:
| `useBackendReadOnlyReplica` | true | use the backend's `readOnlyBackendMetaData` for read-only sessions |
| `writeModeEnabled` | true | a global kill switch for write mode |
| `accessPermissionBaseName` | `sqlWorkbench` | `<base>.hasAccess` |
| `writePermissionName` | `sqlWorkbench.write` | |
| `sharePermissionName` | `sqlWorkbench.share` | |
| `adminPermissionBaseName` | `sqlWorkbenchAdmin` | `<base>.hasAccess` guards the qbit's tables in the generic table UI / API; must differ from `accessPermissionBaseName` |
| `writePermissionName` | `sql_workbench.write` | |
| `sharePermissionName` | `sql_workbench.share` | |
| `adminPermissionBaseName` | `sql_workbench_admin` | `<base>.hasAccess` grants the all-access security key (every user's rows); must differ from `accessPermissionBaseName` |
| `statementTimeoutSeconds` | 30 | read-only and EXPLAIN |
| `writeStatementTimeoutSeconds` | 60 | preview and commit |
| `maxRows` | 10000 | rows kept per result |
@@ -404,7 +432,7 @@ Visibility rules:
│ │ + SQL cell + Markdown cell │
└────────────────────────┴───────────────────────────────────────────────────────────────────┘
Write mode (user has sqlWorkbench.write; toggled per cell):
Write mode (user has sql_workbench.write; toggled per cell):
┌ Confirm write on "postgres" ───────────────────────────────────────────┐
│ ⚠ Raw SQL bypasses QQQ security, audits, automations and indexing. │
│ 1. UPDATE UPDATE web_user SET active = false WHERE … 1,204 rows │
@@ -472,7 +500,7 @@ Plan tab:
| 5 (later) | Saved-query sharing beyond the library, scheduled notebooks, per-backend permissions (`sqlWorkbench.backend.<name>`), result diffing, a core QQQ `availablePermissions` hook for `write` / `share` | backlog |
## 11. Open questions
1. **Permission discovery.** `sqlWorkbench.write` and `sqlWorkbench.share` are plain session permissions. QQQ 4.1 lists available permissions only from tables, processes, apps, reports, and widgets, so these two do not appear in role-management UIs (for example qbit-user-role-permissions). Options: a core hook for custom permissions, or hidden marker widgets. Deferred.
2. **Per-backend authorization.** Should write access be limited per backend (for example `sqlWorkbench.write.<backend>`)? 1.0 uses one global write permission (the owner will refine later).
1. **Permission discovery.** `sql_workbench.write` and `sql_workbench.share` are plain session permissions. QQQ 4.1 lists available permissions only from tables, processes, apps, reports, and widgets, so these two do not appear in role-management UIs (for example qbit-user-role-permissions). Options: a core hook for custom permissions, or hidden marker widgets. Deferred.
2. **Per-backend authorization.** Should write access be limited per backend (for example `sql_workbench.write.<backend>`)? 1.0 uses one global write permission (the owner will refine later).
3. **Audit retention and redaction.** The audit stores parameter values verbatim. Should hosts be able to redact, for example, `password`-like parameter names? 1.0 stores them as is.
4. **H2 in production.** H2 has no enforced read-only transaction. Should the workbench refuse H2 targets in production unless read-only credentials are configured? 1.0 allows it, with a warning in capabilities (`readOnlyEnforced: false`).
+27
View File
@@ -75,6 +75,33 @@ CREATE TABLE IF NOT EXISTS sql_workbench_result_snapshot (
CONSTRAINT fk_sql_workbench_result_snapshot_notebook FOREIGN KEY (notebook_id) REFERENCES sql_workbench_notebook (id)
);
-- Shares (1.0.0-RC.2): who, besides the owner, may read a notebook.
-- audience_key is "user:<id>", "group:<name>" or "all" (the shared library);
-- the record security locks match it against the session's audience keys.
CREATE TABLE IF NOT EXISTS sql_workbench_notebook_share (
id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL,
notebook_id INTEGER NOT NULL,
audience_type VARCHAR(16) NOT NULL,
audience_id VARCHAR(255),
audience_key VARCHAR(300) NOT NULL,
created_by_user_id VARCHAR(255),
created_by_name VARCHAR(255),
create_date TIMESTAMP WITHOUT TIME ZONE,
modify_date TIMESTAMP WITHOUT TIME ZONE,
CONSTRAINT pk_sql_workbench_notebook_share PRIMARY KEY (id),
CONSTRAINT uq_sql_workbench_notebook_share UNIQUE (notebook_id, audience_key),
CONSTRAINT fk_sql_workbench_notebook_share_notebook FOREIGN KEY (notebook_id) REFERENCES sql_workbench_notebook (id)
);
CREATE INDEX IF NOT EXISTS ix_sql_workbench_notebook_share_audience ON sql_workbench_notebook_share (audience_key, notebook_id);
-- Upgrading from 1.0.0-RC.1: notebooks published to the library
-- (visibility SHARED) get their "all" share row. Idempotent.
INSERT INTO sql_workbench_notebook_share (notebook_id, audience_type, audience_id, audience_key, created_by_user_id, created_by_name, create_date, modify_date)
SELECT n.id, 'ALL', NULL, 'all', n.owner_user_id, n.owner_name, COALESCE(n.modify_date, CURRENT_TIMESTAMP), COALESCE(n.modify_date, CURRENT_TIMESTAMP)
FROM sql_workbench_notebook n
WHERE n.visibility = 'SHARED'
AND NOT EXISTS (SELECT 1 FROM sql_workbench_notebook_share s WHERE s.notebook_id = n.id AND s.audience_key = 'all');
-- Audit: append-only. Not foreign-keyed to notebooks, so deleting a
-- notebook never deletes its audit trail.
CREATE TABLE IF NOT EXISTS sql_workbench_execution (
+5 -4
View File
@@ -250,16 +250,17 @@
<profiles>
<!-- Built against qqq 4.1 (QBitMetaDataProducer, runtime services, Javalin 7 -->
<!-- routes). Until qbit-build-parent 2.1.0 imports the released 4.1 BOM, -->
<!-- this profile is active by default and imports qqq-bom-pom 4.1.0-RC.2 -->
<!-- ahead of the parent's 4.0.0 BOM (same as qbit-quick-search, ADR-0007). -->
<!-- routes; the record developer panel contract needs 4.1.0-RC.4). Until -->
<!-- qbit-build-parent 2.1.0 imports the released 4.1 BOM, this profile is -->
<!-- active by default and imports qqq-bom-pom 4.1.0-RC.4 ahead of the -->
<!-- parent's 4.0.0 BOM (same as qbit-quick-search, ADR-0007). -->
<profile>
<id>qqq-snapshot</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<qqq.snapshot.version>4.1.0-RC.2</qqq.snapshot.version>
<qqq.snapshot.version>4.1.0-RC.4</qqq.snapshot.version>
</properties>
<dependencyManagement>
<dependencies>
@@ -0,0 +1,129 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.kingsrook.qbits.sqlworkbench.metadata.SqlWorkbenchAppMetaDataProducer;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchExecution;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
/*******************************************************************************
** Opt-in hooks for wiring the workbench's main tables into a host's ESB and
** Quick Search, without the qbit depending on either module: the host reads
** these names and field lists and passes them to its own qqq-esb
** (EsbTableMetaData publications) and qbit-quick-search
** (SearchableTableConfig) configuration.
**
** The main tables are Notebooks and the Execution audit (the tables in the
** app's nav). The sub-tables (cells, versions, pinned results, shares) are
** reached through a notebook and are left out on purpose.
**
** What leaves the database: a notebook event carries the notebook row
** (title, description, owner, visibility, default backend, parameter
** definitions). An execution event carries the audit row, including the SQL
** text and the parameter values; results are never stored, so they are
** never published or indexed. Search hits are re-read through the tables'
** record locks, so a user only finds what they could open.
*******************************************************************************/
public final class SqlWorkbenchIntegrations
{
/*******************************************************************************
** A table for Quick Search: its fields with weights (most important
** first), the field basepull compares, and the label of a hit.
**
** @param tableName the (prefixed) table name
** @param fieldWeights searchable field names and their weights
** @param basepullTimestamp field whose value moves when a row changes
** @param recordLabelFormat format of a hit's label
** @param recordLabelFields fields for recordLabelFormat
*******************************************************************************/
public record SearchableTable(String tableName, Map<String, Integer> fieldWeights, String basepullTimestamp, String recordLabelFormat, List<String> recordLabelFields)
{
}
/*******************************************************************************
** Utility class - no instances.
*******************************************************************************/
private SqlWorkbenchIntegrations()
{
}
/*******************************************************************************
** The main tables (in nav order), with the config's prefix applied.
*******************************************************************************/
public static List<String> mainTableNames(SqlWorkbenchQBitConfig config)
{
List<String> names = new ArrayList<>();
for(String tableName : SqlWorkbenchAppMetaDataProducer.MAIN_TABLES)
{
names.add(config.applyPrefix(tableName));
}
return (names);
}
/*******************************************************************************
** The tables whose insert / update / delete events a host may publish to
** its ESB (the main tables). Executions are inserted when a run starts and
** updated when it ends.
*******************************************************************************/
public static List<String> esbTableNames(SqlWorkbenchQBitConfig config)
{
return (mainTableNames(config));
}
/*******************************************************************************
** The main tables as Quick Search tables:
** - Notebooks: title, description, owner, default backend; basepull on
** modifyDate (cell edits bump it, as each one saves a new version).
** - Executions: SQL text, user, execution UUID, error, backend; basepull
** on createDate (the audit has no modify date; real-time indexing picks
** up the end-of-run update, and the reconcile catches anything missed).
*******************************************************************************/
public static List<SearchableTable> quickSearchTables(SqlWorkbenchQBitConfig config)
{
Map<String, Integer> notebook = new LinkedHashMap<>();
notebook.put("title", 10);
notebook.put("description", 5);
notebook.put("ownerName", 3);
notebook.put("defaultBackendName", 1);
Map<String, Integer> execution = new LinkedHashMap<>();
execution.put("sqlText", 8);
execution.put("executionUuid", 5);
execution.put("userName", 3);
execution.put("errorMessage", 2);
execution.put("backendName", 1);
return (List.of(
new SearchableTable(config.applyPrefix(SqlWorkbenchNotebook.TABLE_NAME), notebook, "modifyDate", "%s", List.of("title")),
new SearchableTable(config.applyPrefix(SqlWorkbenchExecution.TABLE_NAME), execution, "createDate", "SQL run by %s (%s)", List.of("userName", "mode"))));
}
}
@@ -0,0 +1,111 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench;
import java.util.Map;
import com.kingsrook.qqq.backend.core.actions.permissions.AvailablePermission;
import com.kingsrook.qqq.backend.core.actions.permissions.CustomPermissionChecker;
import com.kingsrook.qqq.backend.core.actions.permissions.PermissionSubType;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QPermissionDeniedException;
import com.kingsrook.qqq.backend.core.model.actions.AbstractActionInput;
import com.kingsrook.qqq.backend.core.model.metadata.code.InitializableViaCodeReference;
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReference;
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReferenceWithProperties;
import com.kingsrook.qqq.backend.core.model.metadata.permissions.MetaDataWithName;
import com.kingsrook.qqq.backend.core.model.metadata.permissions.MetaDataWithPermissionRules;
import com.kingsrook.qqq.backend.core.model.metadata.permissions.QPermissionRules;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.core.utils.ValueUtils;
/*******************************************************************************
** Gates a record action on one named session permission (for example the
** share table's "insert" permission), and lists that same name among the
** instance's available permissions. Unlike QQQ's
** UseTablePermissionCustomPermissionChecker it needs no QContext instance
** when permissions are listed.
*******************************************************************************/
public class SqlWorkbenchPermissionChecker implements CustomPermissionChecker, InitializableViaCodeReference
{
public static final String PERMISSION_PROPERTY = "permissionName";
private String permissionName;
/*******************************************************************************
** A code reference that requires the given permission.
*******************************************************************************/
public static QCodeReference requiring(String permissionName)
{
return (new QCodeReferenceWithProperties(SqlWorkbenchPermissionChecker.class, Map.of(PERMISSION_PROPERTY, permissionName)));
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public void initialize(QCodeReference codeReference)
{
if(codeReference instanceof QCodeReferenceWithProperties withProperties)
{
this.permissionName = ValueUtils.getValueAsString(withProperties.getProperties().get(PERMISSION_PROPERTY));
}
}
/*******************************************************************************
** Deny unless the session holds the permission (fail closed when it is
** not configured).
*******************************************************************************/
@Override
public void checkPermissionsThrowing(AbstractActionInput actionInput, MetaDataWithPermissionRules metaDataWithPermissionRules) throws QPermissionDeniedException
{
QSession session = QContext.getQSession();
if(permissionName == null || session == null || !session.hasPermission(permissionName))
{
throw (new QPermissionDeniedException("Permission denied."));
}
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public boolean handlesBuildAvailablePermission()
{
return (true);
}
/*******************************************************************************
** The permission is already listed for its table; nothing extra to list.
*******************************************************************************/
@Override
public AvailablePermission buildAvailablePermission(QPermissionRules rules, PermissionSubType permissionSubType, String baseName, MetaDataWithName metaDataWithName, String objectType)
{
return (null);
}
}
@@ -22,6 +22,12 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchExecution;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookVersion;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchResultSnapshot;
import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.permissions.PermissionLevel;
@@ -41,9 +47,13 @@ import com.kingsrook.qqq.backend.module.rdbms.model.metadata.RDBMSBackendMetaDat
** excludedBackendNames over the instance's RDBMS backends, optional
** readOnlyCredentials / writeCredentials per backend, and
** useBackendReadOnlyReplica. Permissions: accessPermissionBaseName
** (=> "sqlWorkbench.hasAccess"), writePermissionName, sharePermissionName,
** and adminPermissionBaseName (=> "sqlWorkbenchAdmin.hasAccess"), which
** alone grants the generic QQQ table UI / API over the qbit's tables.
** (=> "sql_workbench.hasAccess"), writePermissionName, sharePermissionName,
** and adminPermissionBaseName (=> "sql_workbench_admin.hasAccess", which
** grants the all-access security key: every user's notebooks and the whole
** audit). The tables use QQQ's per-table read / insert / edit / delete
** permissions; getUserPermissionNames, getSharePermissionNames and
** getAdminPermissionNames list what to grant per role, and
** SqlWorkbenchSecurity.applyToSession puts the security keys in a session.
** Limits: timeouts, rows, bytes, statement counts, row guard, caches.
**
** validate() is list-based and never throws; the producer fails production
@@ -51,10 +61,34 @@ import com.kingsrook.qqq.backend.module.rdbms.model.metadata.RDBMSBackendMetaDat
*******************************************************************************/
public class SqlWorkbenchQBitConfig implements QBitConfig
{
public static final String DEFAULT_ACCESS_PERMISSION_BASE_NAME = "sqlWorkbench";
public static final String DEFAULT_WRITE_PERMISSION_NAME = "sqlWorkbench.write";
public static final String DEFAULT_SHARE_PERMISSION_NAME = "sqlWorkbench.share";
public static final String DEFAULT_ADMIN_PERMISSION_BASE_NAME = "sqlWorkbenchAdmin";
public static final String DEFAULT_ACCESS_PERMISSION_BASE_NAME = "sql_workbench";
public static final String DEFAULT_WRITE_PERMISSION_NAME = "sql_workbench.write";
public static final String DEFAULT_SHARE_PERMISSION_NAME = "sql_workbench.share";
public static final String DEFAULT_ADMIN_PERMISSION_BASE_NAME = "sql_workbench_admin";
public static final String USER_SECURITY_KEY_TYPE_NAME = "sql_workbench_user";
public static final String AUDIENCE_SECURITY_KEY_TYPE_NAME = "sql_workbench_audience";
public static final String ALL_ACCESS_SECURITY_KEY_NAME = "sql_workbench_all_access";
///////////////////////////////////////////////////////////////////////////
// names before 1.0.0-RC.3, when the owner rule (lowercase snake_case, //
// prefixed with the qbit name) was applied; kept only so hosts can //
// migrate stored references (see UPGRADE.md) //
///////////////////////////////////////////////////////////////////////////
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_ACCESS_PERMISSION_BASE_NAME = "sqlWorkbench";
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_WRITE_PERMISSION_NAME = "sqlWorkbench.write";
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_SHARE_PERMISSION_NAME = "sqlWorkbench.share";
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_ADMIN_PERMISSION_BASE_NAME = "sqlWorkbenchAdmin";
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_USER_SECURITY_KEY_TYPE_NAME = "sqlWorkbenchUser";
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_AUDIENCE_SECURITY_KEY_TYPE_NAME = "sqlWorkbenchAudience";
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_ALL_ACCESS_SECURITY_KEY_NAME = "sqlWorkbenchAllAccess";
private String backendName;
private String tableNamePrefix;
@@ -218,11 +252,29 @@ public class SqlWorkbenchQBitConfig implements QBitConfig
/*******************************************************************************
** Apply the table name prefix, if any.
** Apply the table name prefix, if any. The prefix is normalized to
** lowercase snake_case and joined with one "_", so a prefix of "ns",
** "ns_" or "myNs" gives "ns_sql_workbench_notebook" or
** "my_ns_sql_workbench_notebook".
*******************************************************************************/
public String applyPrefix(String name)
{
return (StringUtils.hasContent(tableNamePrefix) ? tableNamePrefix + "_" + name : name);
if(!StringUtils.hasContent(tableNamePrefix))
{
return (name);
}
return (normalizePrefix(tableNamePrefix) + "_" + name);
}
/*******************************************************************************
** Lowercase snake_case form of a prefix, without leading or trailing "_".
*******************************************************************************/
static String normalizePrefix(String prefix)
{
String snake = prefix.trim().replaceAll("([a-z0-9])([A-Z])", "$1_$2").replaceAll("[^A-Za-z0-9]+", "_").toLowerCase();
return (snake.replaceAll("^_+", "").replaceAll("_+$", ""));
}
@@ -262,12 +314,8 @@ public class SqlWorkbenchQBitConfig implements QBitConfig
/*******************************************************************************
** Permission rules for the qbit's tables: has-access on the admin base
** name. Ordinary workbench users do not hold it, so the generic QQQ table
** UI / API (/qqq/v1/table/...) cannot read other users' notebooks,
** snapshots or the execution audit; the workbench's own endpoints read
** and write these tables as the system user after their own owner /
** visibility checks.
** Permission rules for the admin-only surface: has-access on the admin
** base name.
*******************************************************************************/
@JsonIgnore
public QPermissionRules getAdminPermissionRules()
@@ -277,6 +325,123 @@ public class SqlWorkbenchQBitConfig implements QBitConfig
/*******************************************************************************
** Permission rules for the qbit's tables: QQQ's standard per-table
** read / insert / edit / delete permissions ("<tableName>.read", ...).
** Which rows a user sees and may change is decided by the tables' record
** security locks (owner, shares, admin all-access), not by these.
*******************************************************************************/
@JsonIgnore
public QPermissionRules getTablePermissionRules()
{
return (new QPermissionRules().withLevel(PermissionLevel.READ_INSERT_EDIT_DELETE_PERMISSIONS));
}
/*******************************************************************************
** Security key type whose session value is the user's id; the owner
** locks match it.
*******************************************************************************/
@JsonIgnore
public String getUserSecurityKeyTypeName()
{
return (applyPrefix(USER_SECURITY_KEY_TYPE_NAME));
}
/*******************************************************************************
** Security key type whose session values are the user's audience keys
** ("user:<id>", "group:<name>", "all"); the share locks match it.
*******************************************************************************/
@JsonIgnore
public String getAudienceSecurityKeyTypeName()
{
return (applyPrefix(AUDIENCE_SECURITY_KEY_TYPE_NAME));
}
/*******************************************************************************
** All-access key of the user key type: true for workbench admins.
*******************************************************************************/
@JsonIgnore
public String getAllAccessSecurityKeyName()
{
return (applyPrefix(ALL_ACCESS_SECURITY_KEY_NAME));
}
/*******************************************************************************
** Every permission an ordinary workbench user needs: the access
** permission, read / insert / edit / delete on notebooks and cells, read
** on versions, shares and the execution audit, and read / edit (label) /
** delete on pinned snapshots. Record locks narrow every one of these to
** the user's own (or shared-with-them, for reads) rows.
*******************************************************************************/
@JsonIgnore
public List<String> getUserPermissionNames()
{
List<String> names = new ArrayList<>();
names.add(getAccessPermissionName());
addTablePermissions(names, SqlWorkbenchNotebook.TABLE_NAME, "read", "insert", "edit", "delete");
addTablePermissions(names, SqlWorkbenchCell.TABLE_NAME, "read", "insert", "edit", "delete");
addTablePermissions(names, SqlWorkbenchNotebookVersion.TABLE_NAME, "read");
addTablePermissions(names, SqlWorkbenchResultSnapshot.TABLE_NAME, "read", "edit", "delete");
addTablePermissions(names, SqlWorkbenchNotebookShare.TABLE_NAME, "read");
addTablePermissions(names, SqlWorkbenchExecution.TABLE_NAME, "read");
return (names);
}
/*******************************************************************************
** Permissions to share one's notebooks (with users, groups, or the shared
** library): the share permission plus insert / edit / delete on the share
** table.
*******************************************************************************/
@JsonIgnore
public List<String> getSharePermissionNames()
{
List<String> names = new ArrayList<>();
names.add(sharePermissionName);
addTablePermissions(names, SqlWorkbenchNotebookShare.TABLE_NAME, "insert", "edit", "delete");
return (names);
}
/*******************************************************************************
** Permissions for a workbench admin: everything a user has plus the admin
** permission, which (through SqlWorkbenchSecurity.applyToSession) puts the
** all-access key in the session - every user's notebooks and the whole
** execution audit. Versions and the audit stay append-only for admins.
*******************************************************************************/
@JsonIgnore
public List<String> getAdminPermissionNames()
{
List<String> names = new ArrayList<>(getUserPermissionNames());
names.add(getAdminPermissionName());
return (names);
}
/*******************************************************************************
**
*******************************************************************************/
private void addTablePermissions(List<String> names, String table, String... suffixes)
{
for(String suffix : suffixes)
{
names.add(applyPrefix(table) + "." + suffix);
}
}
/*******************************************************************************
** QBitConfig: tables without a backend get this one.
*******************************************************************************/
@@ -0,0 +1,250 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.kingsrook.qbits.sqlworkbench.model.ShareAudienceType;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QNotFoundException;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.security.QSecurityKeyType;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.core.model.session.QSystemUserSession;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
/*******************************************************************************
** The workbench's record-security keys.
**
** Two security key types guard the qbit's tables:
** - sql_workbench_user: the session's user id. Owner locks (a notebook's
** ownerUserId, an execution's userId) match it. Its all-access key,
** sql_workbench_all_access, is set for workbench admins.
** - sql_workbench_audience: the session's audience keys: "user:<id>",
** "group:<name>" per group, and "all". Share locks (a share row's
** audienceKey) match it, for reads only.
**
** Hosts call applyToSession from their authentication customizer, after
** the session's user and permissions are set, passing the user's groups
** (if the app has any). Without the keys the locks deny: the tables then
** show a user nothing, never someone else's rows.
*******************************************************************************/
public final class SqlWorkbenchSecurity
{
public static final String AUDIENCE_ALL = ShareAudienceType.ALL.audienceKey(null);
/*******************************************************************************
**
*******************************************************************************/
private SqlWorkbenchSecurity()
{
}
/*******************************************************************************
** The two security key types, for the producer.
*******************************************************************************/
public static List<QSecurityKeyType> buildSecurityKeyTypes(SqlWorkbenchQBitConfig config)
{
return (List.of(
new QSecurityKeyType().withName(config.getUserSecurityKeyTypeName()).withAllAccessKeyName(config.getAllAccessSecurityKeyName()),
new QSecurityKeyType().withName(config.getAudienceSecurityKeyTypeName())));
}
/*******************************************************************************
** Put the workbench security keys into a session, using the installed
** workbench's configuration. Idempotent: earlier workbench keys are
** replaced.
*******************************************************************************/
public static void applyToSession(QInstance qInstance, QSession session, Collection<String> groups) throws QNotFoundException
{
applyToSession(SqlWorkbenchRuntime.get(qInstance).getConfig(), session, groups);
}
/*******************************************************************************
** Put the workbench security keys into a session: the user id, the
** audience keys (user, each group, all) and, for holders of the admin
** permission, the all-access key. A session without a user id or
** without the workbench access permission gets none, so the locks deny.
** Idempotent.
*******************************************************************************/
public static void applyToSession(SqlWorkbenchQBitConfig config, QSession session, Collection<String> groups)
{
if(session == null || session instanceof QSystemUserSession)
{
return;
}
Map<String, List<Serializable>> keys = session.getSecurityKeyValues();
if(keys != null)
{
keys.remove(config.getUserSecurityKeyTypeName());
keys.remove(config.getAudienceSecurityKeyTypeName());
keys.remove(config.getAllAccessSecurityKeyName());
}
//////////////////////////////////////////////////////////////////////
// no workbench access, no keys: even a code path that skips table //
// permissions then finds no workbench rows (not even the library) //
//////////////////////////////////////////////////////////////////////
String userId = userId(session);
if(!StringUtils.hasContent(userId) || !session.hasPermission(config.getAccessPermissionName()))
{
return;
}
session.withSecurityKeyValue(config.getUserSecurityKeyTypeName(), userId);
for(String audienceKey : audienceKeys(userId, groups))
{
session.withSecurityKeyValue(config.getAudienceSecurityKeyTypeName(), audienceKey);
}
if(session.hasPermission(config.getAdminPermissionName()))
{
session.withSecurityKeyValue(config.getAllAccessSecurityKeyName(), true);
}
}
/*******************************************************************************
** Add the keys only when the session has none yet (keeping the groups a
** host already applied). The workbench endpoints call this, so a host
** that never calls applyToSession still gets owner and library access
** there; group shares need the host to pass the groups.
*******************************************************************************/
public static void ensureSessionKeys(SqlWorkbenchQBitConfig config, QSession session)
{
if(session != null && session.getSecurityKeyValues(config.getUserSecurityKeyTypeName()).isEmpty())
{
applyToSession(config, session, List.of());
}
}
/*******************************************************************************
** The audience keys of a user in some groups: "user:<id>", one
** "group:<name>" per group, and "all".
*******************************************************************************/
public static List<String> audienceKeys(String userId, Collection<String> groups)
{
Set<String> keys = new LinkedHashSet<>();
if(StringUtils.hasContent(userId))
{
keys.add(ShareAudienceType.USER.audienceKey(userId));
}
if(groups != null)
{
for(String group : groups)
{
if(StringUtils.hasContent(group))
{
keys.add(ShareAudienceType.GROUP.audienceKey(group));
}
}
}
keys.add(AUDIENCE_ALL);
return (new ArrayList<>(keys));
}
/*******************************************************************************
** The current session's audience keys, as applied.
*******************************************************************************/
public static List<String> sessionAudienceKeys(SqlWorkbenchQBitConfig config)
{
List<String> keys = new ArrayList<>();
QSession session = QContext.getQSession();
if(session != null)
{
for(Serializable value : session.getSecurityKeyValues(config.getAudienceSecurityKeyTypeName()))
{
keys.add(String.valueOf(value));
}
}
return (keys);
}
/*******************************************************************************
** True for the system user and for sessions holding the all-access key.
*******************************************************************************/
public static boolean hasAllAccess(SqlWorkbenchQBitConfig config)
{
QSession session = QContext.getQSession();
return (session instanceof QSystemUserSession || (session != null && session.hasSecurityKeyValue(config.getAllAccessSecurityKeyName(), true, QFieldType.BOOLEAN)));
}
/*******************************************************************************
** True when the current session is the system user.
*******************************************************************************/
public static boolean isSystemUser()
{
return (QContext.getQSession() instanceof QSystemUserSession);
}
/*******************************************************************************
** The session's user id (the user's id reference, else the session's).
*******************************************************************************/
public static String userId(QSession session)
{
if(session == null)
{
return (null);
}
if(session.getUser() != null && StringUtils.hasContent(session.getUser().getIdReference()))
{
return (session.getUser().getIdReference());
}
return (session.getIdReference());
}
/*******************************************************************************
** The session's user name (full name, else the id).
*******************************************************************************/
public static String userName(QSession session)
{
if(session != null && session.getUser() != null && StringUtils.hasContent(session.getUser().getFullName()))
{
return (session.getUser().getFullName());
}
return (userId(session));
}
}
@@ -40,7 +40,7 @@ import com.kingsrook.qqq.backend.core.utils.StringUtils;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitProducer;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchRuntime;
import com.kingsrook.qbits.sqlworkbench.SystemUser;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchSecurity;
import com.kingsrook.qbits.sqlworkbench.engine.Caller;
import com.kingsrook.qbits.sqlworkbench.engine.ColumnInfo;
import com.kingsrook.qbits.sqlworkbench.engine.ExecutionRequest;
@@ -107,16 +107,12 @@ public class SqlWorkbenchApi
throw (new QPermissionDeniedException("Permission denied."));
}
String userId = session.getUser() != null && StringUtils.hasContent(session.getUser().getIdReference()) ? session.getUser().getIdReference() : session.getIdReference();
String userName = session.getUser() != null && StringUtils.hasContent(session.getUser().getFullName()) ? session.getUser().getFullName() : userId;
if(!StringUtils.hasContent(userId))
if(!StringUtils.hasContent(SqlWorkbenchSecurity.userId(session)))
{
throw (new QAuthenticationException("The session has no user."));
}
boolean canWrite = Boolean.TRUE.equals(config.getWriteModeEnabled()) && session.hasPermission(config.getWritePermissionName());
boolean canShare = session.hasPermission(config.getSharePermissionName());
return (new Caller(userId, userName, canWrite, canShare, clientIp));
SqlWorkbenchSecurity.ensureSessionKeys(config, session);
return (Caller.fromSession(config, session, clientIp));
}
@@ -264,10 +260,10 @@ public class SqlWorkbenchApi
List<Map<String, Object>> executions = new ArrayList<>();
//////////////////////////////////////////////////////////////////////
// the audit table needs the admin permission; the filter above is //
// the boundary (own rows only), so read it as the system user //
// read as the caller: the audit's record lock limits them to their //
// own rows (admins see all, so the filter keeps history personal) //
//////////////////////////////////////////////////////////////////////
for(QRecord record : SystemUser.call(() -> QueryAction.execute(config.applyPrefix(SqlWorkbenchExecution.TABLE_NAME), filter)))
for(QRecord record : QueryAction.execute(config.applyPrefix(SqlWorkbenchExecution.TABLE_NAME), filter))
{
SqlWorkbenchExecution execution = new SqlWorkbenchExecution(record);
Map<String, Object> view = new LinkedHashMap<>();
@@ -307,10 +303,10 @@ public class SqlWorkbenchApi
{
String executionUuid = normalizeUuid(requireString(body, "executionUuid"));
cached(caller, executionUuid);
List<QRecord> audit = SystemUser.call(() -> QueryAction.execute(config.applyPrefix(SqlWorkbenchExecution.TABLE_NAME),
List<QRecord> audit = QueryAction.execute(config.applyPrefix(SqlWorkbenchExecution.TABLE_NAME),
new QQueryFilter(new QFilterCriteria("executionUuid", QCriteriaOperator.EQUALS, executionUuid))
.withCriteria(new QFilterCriteria("userId", QCriteriaOperator.EQUALS, caller.userId()))
.withLimit(1)));
.withLimit(1));
String sqlText = audit.isEmpty() ? null : audit.get(0).getValueString("sqlText");
Map<String, Object> normalized = new LinkedHashMap<>(body);
@@ -0,0 +1,90 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.customizers;
import java.util.List;
import java.util.Optional;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
/*******************************************************************************
** Notebook versions and the execution audit are append-only: only the qbit
** itself (the system user) inserts them, and only its retention pruning
** deletes them. The tables also lack the insert / update / delete
** capabilities, which hides those operations in the UI and API; this
** customizer enforces it in the core actions, whoever calls them (admins
** included).
*******************************************************************************/
public class AppendOnlyTableCustomizer extends WorkbenchTableCustomizer
{
private static final String MESSAGE = "This table is append-only: only the SQL Workbench writes it.";
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preInsert(InsertInput insertInput, List<QRecord> records, boolean isPreview) throws QException
{
return (denyUnlessSystem(records));
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preUpdate(UpdateInput updateInput, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
{
return (denyUnlessSystem(records));
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preDelete(DeleteInput deleteInput, List<QRecord> records, boolean isPreview) throws QException
{
return (denyUnlessSystem(records));
}
/*******************************************************************************
**
*******************************************************************************/
private static List<QRecord> denyUnlessSystem(List<QRecord> records)
{
if(!isSystemUser())
{
for(QRecord record : records)
{
denied(record, MESSAGE);
}
}
return (records);
}
}
@@ -0,0 +1,259 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.customizers;
import java.time.Instant;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.notebook.NotebookService;
import com.kingsrook.qqq.backend.core.actions.tables.QueryAction;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
/*******************************************************************************
** Cell invariants, the same for the notebook UI and the table UI / API:
** type SQL or MARKDOWN, content within the SQL length limit, a stable
** cellKey, at most NotebookService.MAX_CELLS per notebook, new cells
** appended at the end, cells never move between notebooks, and every
** change appends a notebook version. Ownership is the notebook's (the
** record lock checks it through the notebook).
*******************************************************************************/
public class CellTableCustomizer extends WorkbenchTableCustomizer
{
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preInsert(InsertInput insertInput, List<QRecord> records, boolean isPreview) throws QException
{
SqlWorkbenchQBitConfig config = config();
Instant now = clock().instant();
boolean wholeNotebook = insertInput.hasFlag(SqlWorkbenchActionFlag.NOTEBOOK_SAVE) || insertInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL);
Map<Integer, int[]> nextSortOrderAndCount = new HashMap<>();
for(QRecord record : records)
{
Integer notebookId = record.getValueInteger("notebookId");
if(notebookId == null)
{
badInput(record, "A cell needs a notebook.");
continue;
}
validate(config, record, true);
if(!StringUtils.hasContent(record.getValueString("cellKey")))
{
record.setValue("cellKey", UUID.randomUUID().toString());
}
record.setValue("createDate", now);
record.setValue("modifyDate", now);
if(!wholeNotebook)
{
int[] state = nextSortOrderAndCount.get(notebookId);
if(state == null)
{
state = existing(config, notebookId);
nextSortOrderAndCount.put(notebookId, state);
}
if(state[1] >= NotebookService.MAX_CELLS)
{
badInput(record, "A notebook can have at most " + NotebookService.MAX_CELLS + " cells.");
continue;
}
state[1]++;
if(record.getValueInteger("sortOrder") == null)
{
record.setValue("sortOrder", state[0]++);
}
}
}
return (records);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> postInsert(InsertInput insertInput, List<QRecord> records) throws QException
{
if(!insertInput.hasFlag(SqlWorkbenchActionFlag.NOTEBOOK_SAVE) && !insertInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL))
{
appendVersions(notebookIds(records), "Cell added", insertInput.getTransaction());
}
return (records);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preUpdate(UpdateInput updateInput, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
{
if(updateInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL))
{
return (records);
}
SqlWorkbenchQBitConfig config = config();
Instant now = clock().instant();
for(QRecord record : records)
{
if(!ok(record))
{
continue;
}
QRecord old = oldRecord(oldRecordList, record);
if(changes(record, old, "notebookId"))
{
badInput(record, "A cell cannot move to another notebook.");
continue;
}
if(changes(record, old, "cellKey"))
{
badInput(record, "A cell's key cannot change.");
continue;
}
record.removeValue("createDate");
validate(config, record, false);
record.setValue("modifyDate", now);
}
return (records);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> postUpdate(UpdateInput updateInput, List<QRecord> records, Optional<List<QRecord>> oldRecordList) throws QException
{
if(!updateInput.hasFlag(SqlWorkbenchActionFlag.NOTEBOOK_SAVE) && !updateInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL))
{
Set<Integer> ids = new LinkedHashSet<>();
for(QRecord record : records)
{
QRecord old = oldRecord(oldRecordList, record);
if(ok(record) && old != null && old.getValueInteger("notebookId") != null)
{
ids.add(old.getValueInteger("notebookId"));
}
}
appendVersions(ids, "Cell edited", updateInput.getTransaction());
}
return (records);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> postDelete(DeleteInput deleteInput, List<QRecord> records) throws QException
{
if(!deleteInput.hasFlag(SqlWorkbenchActionFlag.NOTEBOOK_SAVE) && !deleteInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL))
{
appendVersions(notebookIds(records), "Cell removed", deleteInput.getTransaction());
}
return (records);
}
/*******************************************************************************
** Type, content and key checks.
*******************************************************************************/
private static void validate(SqlWorkbenchQBitConfig config, QRecord record, boolean inserting)
{
if(inserting || record.getValues().containsKey("cellType"))
{
String type = record.getValueString("cellType");
type = StringUtils.hasContent(type) ? type.trim().toUpperCase(Locale.ROOT) : NotebookService.CELL_SQL;
if(!NotebookService.CELL_SQL.equals(type) && !NotebookService.CELL_MARKDOWN.equals(type))
{
badInput(record, "cellType must be SQL or MARKDOWN.");
return;
}
record.setValue("cellType", type);
}
if(inserting && record.getValue("content") == null)
{
record.setValue("content", "");
}
String content = record.getValueString("content");
if(content != null && content.length() > config.getMaxSqlLength())
{
badInput(record, "A cell is longer than the limit of " + config.getMaxSqlLength() + " characters.");
return;
}
String key = record.getValueString("cellKey");
if(key != null && key.length() > 64)
{
badInput(record, "A cell key is at most 64 characters.");
}
}
/*******************************************************************************
** {next sort order, current cell count} of a notebook, as the session sees
** it (a notebook the session cannot read counts as empty; its insert then
** fails the record lock).
*******************************************************************************/
private static int[] existing(SqlWorkbenchQBitConfig config, Integer notebookId) throws QException
{
List<QRecord> cells = new QueryAction().execute(new QueryInput(config.applyPrefix(SqlWorkbenchCell.TABLE_NAME))
.withFilter(new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.EQUALS, notebookId)))).getRecords();
int max = -1;
for(QRecord cell : cells)
{
Integer sortOrder = cell.getValueInteger("sortOrder");
if(sortOrder != null && sortOrder > max)
{
max = sortOrder;
}
}
return (new int[] { max + 1, cells.size() });
}
}
@@ -0,0 +1,293 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.customizers;
import java.io.Serializable;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import com.fasterxml.jackson.core.type.TypeReference;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchSecurity;
import com.kingsrook.qbits.sqlworkbench.SystemUser;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookVersion;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchResultSnapshot;
import com.kingsrook.qbits.sqlworkbench.notebook.NotebookService;
import com.kingsrook.qqq.backend.core.actions.tables.DeleteAction;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import com.kingsrook.qqq.backend.core.utils.JsonUtils;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
/*******************************************************************************
** Notebook invariants, the same for the notebook UI and the table UI / API:
** - the owner is the creating session's user (only the qbit itself, as the
** system user, may insert for someone else, e.g. a migration);
** - visibility is derived from the shares (an ALL share = the library);
** - currentVersion / createDate are system-managed; a title is required;
** parameters are a JSON list;
** - every content change appends a version;
** - deleting a notebook deletes its cells, versions, pinned results and
** shares (only after QQQ has authorized the delete).
*******************************************************************************/
public class NotebookTableCustomizer extends WorkbenchTableCustomizer
{
private static final List<String> CONTENT_FIELDS = List.of("title", "description", "defaultBackendName", "parametersJson");
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preInsert(InsertInput insertInput, List<QRecord> records, boolean isPreview) throws QException
{
boolean system = isSystemUser();
String userId = SqlWorkbenchSecurity.userId(QContext.getQSession());
String userName = SqlWorkbenchSecurity.userName(QContext.getQSession());
Instant now = clock().instant();
for(QRecord record : records)
{
if(!system)
{
if(!StringUtils.hasContent(userId))
{
denied(record, "The session has no user.");
continue;
}
record.setValue("ownerUserId", userId);
record.setValue("ownerName", userName);
record.setValue("visibility", NotebookService.VISIBILITY_PERSONAL);
record.setValue("currentVersion", 0);
}
else
{
if(!StringUtils.hasContent(record.getValueString("ownerUserId")))
{
badInput(record, "A notebook needs an owner.");
}
if(!StringUtils.hasContent(record.getValueString("visibility")))
{
record.setValue("visibility", NotebookService.VISIBILITY_PERSONAL);
}
if(record.getValue("currentVersion") == null)
{
record.setValue("currentVersion", 0);
}
}
validateContent(record, true);
record.setValue("createDate", now);
record.setValue("modifyDate", now);
}
return (records);
}
/*******************************************************************************
** A new notebook made through the table gets version 1 (the notebook UI
** writes its own, with the cells).
*******************************************************************************/
@Override
public List<QRecord> postInsert(InsertInput insertInput, List<QRecord> records) throws QException
{
if(!insertInput.hasFlag(SqlWorkbenchActionFlag.NOTEBOOK_SAVE) && !insertInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL))
{
Set<Integer> ids = new LinkedHashSet<>();
for(QRecord record : records)
{
if(ok(record) && record.getValueInteger("id") != null)
{
ids.add(record.getValueInteger("id"));
}
}
appendVersions(ids, "Created", insertInput.getTransaction());
}
return (records);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preUpdate(UpdateInput updateInput, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
{
if(isSystemUser() || updateInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL))
{
return (records);
}
Instant now = clock().instant();
for(QRecord record : records)
{
if(!ok(record))
{
continue;
}
QRecord old = oldRecord(oldRecordList, record);
if(changes(record, old, "visibility"))
{
badInput(record, "Visibility follows the notebook's shares: share it with Everyone to publish it to the shared library.");
continue;
}
if(changes(record, old, "ownerUserId"))
{
////////////////////////////////////////////////////////////////
// an admin transfer (the lock allows nobody else): the name //
// follows the new owner id, never a client-supplied value //
////////////////////////////////////////////////////////////////
record.setValue("ownerName", record.getValueString("ownerUserId"));
}
else
{
record.removeValue("ownerName");
}
record.removeValue("currentVersion");
record.removeValue("createDate");
validateContent(record, false);
record.setValue("modifyDate", now);
}
return (records);
}
/*******************************************************************************
** A content edit made through the table appends a version.
*******************************************************************************/
@Override
public List<QRecord> postUpdate(UpdateInput updateInput, List<QRecord> records, Optional<List<QRecord>> oldRecordList) throws QException
{
if(updateInput.hasFlag(SqlWorkbenchActionFlag.NOTEBOOK_SAVE) || updateInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL))
{
return (records);
}
Set<Integer> ids = new LinkedHashSet<>();
for(QRecord record : records)
{
if(ok(record) && record.getValueInteger("id") != null && CONTENT_FIELDS.stream().anyMatch(field -> record.getValues().containsKey(field)))
{
ids.add(record.getValueInteger("id"));
}
}
appendVersions(ids, "Edited", updateInput.getTransaction());
return (records);
}
/*******************************************************************************
** Delete the children of the notebooks QQQ authorized for deletion (the
** foreign keys need them gone first). Inside the delete's transaction
** when it has one (the notebook UI always does); a table-UI / API delete
** has none, so there the children go first, in autocommit. A failed child
** delete aborts the notebook delete.
*******************************************************************************/
@Override
public List<QRecord> preDelete(DeleteInput deleteInput, List<QRecord> records, boolean isPreview) throws QException
{
if(isPreview || deleteInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL))
{
return (records);
}
List<Serializable> ids = new ArrayList<>();
for(QRecord record : records)
{
if(ok(record) && record.getValue("id") != null)
{
ids.add(record.getValue("id"));
}
}
if(ids.isEmpty())
{
return (records);
}
SqlWorkbenchQBitConfig config = config();
SystemUser.run(() ->
{
for(String child : List.of(SqlWorkbenchCell.TABLE_NAME, SqlWorkbenchNotebookVersion.TABLE_NAME, SqlWorkbenchResultSnapshot.TABLE_NAME, SqlWorkbenchNotebookShare.TABLE_NAME))
{
DeleteOutput output = new DeleteAction().execute(new DeleteInput(config.applyPrefix(child))
.withFlag(SqlWorkbenchActionFlag.INTERNAL)
.withTransaction(deleteInput.getTransaction())
.withQueryFilter(new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.IN, ids))));
if(CollectionUtils.nullSafeHasContents(output.getRecordsWithErrors()))
{
throw (new QException("Could not delete the notebook's " + child + " rows: " + output.getRecordsWithErrors().get(0).getErrors().get(0).getMessage()));
}
}
});
return (records);
}
/*******************************************************************************
** Title (required on insert, never blank) and parameters (a JSON list).
*******************************************************************************/
private static void validateContent(QRecord record, boolean inserting)
{
if(inserting || record.getValues().containsKey("title"))
{
String title = record.getValueString("title");
if(!StringUtils.hasContent(title))
{
badInput(record, "A notebook needs a title.");
}
else
{
record.setValue("title", title.trim());
}
}
String parameters = record.getValueString("parametersJson");
if(StringUtils.hasContent(parameters))
{
try
{
JsonUtils.toObject(parameters, new TypeReference<List<Object>>() {});
}
catch(Exception e)
{
badInput(record, "Parameters must be a JSON list.");
}
}
}
}
@@ -0,0 +1,290 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.customizers;
import java.time.Instant;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchSecurity;
import com.kingsrook.qbits.sqlworkbench.SystemUser;
import com.kingsrook.qbits.sqlworkbench.model.ShareAudienceType;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qbits.sqlworkbench.notebook.NotebookService;
import com.kingsrook.qqq.backend.core.actions.QBackendTransaction;
import com.kingsrook.qqq.backend.core.actions.tables.CountAction;
import com.kingsrook.qqq.backend.core.actions.tables.UpdateAction;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.count.CountInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
/*******************************************************************************
** Share invariants, the same for the notebook UI ("publish") and the table
** UI / API:
** - changing shares needs the share permission (the record lock then
** checks the notebook is the caller's own, or the caller is an admin);
** - USER and GROUP shares name their audience, ALL (the library) does not;
** audienceKey is computed here and is what the read locks match;
** - a share never moves to another notebook;
** - the notebook's visibility follows its shares (SHARED while it has an
** ALL share).
*******************************************************************************/
public class ShareTableCustomizer extends WorkbenchTableCustomizer
{
private static final String NEEDS_PERMISSION = "Sharing notebooks needs the share permission.";
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preInsert(InsertInput insertInput, List<QRecord> records, boolean isPreview) throws QException
{
boolean allowed = mayShare();
QSession session = QContext.getQSession();
Instant now = clock().instant();
for(QRecord record : records)
{
if(!allowed)
{
denied(record, NEEDS_PERMISSION);
continue;
}
if(record.getValueInteger("notebookId") == null)
{
badInput(record, "A share needs a notebook.");
continue;
}
applyAudience(record, record.getValueString("audienceType"), record.getValueString("audienceId"));
if(!isSystemUser() || !StringUtils.hasContent(record.getValueString("createdByUserId")))
{
record.setValue("createdByUserId", SqlWorkbenchSecurity.userId(session));
record.setValue("createdByName", SqlWorkbenchSecurity.userName(session));
}
record.setValue("createDate", now);
record.setValue("modifyDate", now);
}
return (records);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> postInsert(InsertInput insertInput, List<QRecord> records) throws QException
{
syncVisibility(insertInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL), notebookIds(records), insertInput.getTransaction());
return (records);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preUpdate(UpdateInput updateInput, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
{
boolean allowed = mayShare();
Instant now = clock().instant();
for(QRecord record : records)
{
if(!ok(record))
{
continue;
}
if(!allowed)
{
denied(record, NEEDS_PERMISSION);
continue;
}
QRecord old = oldRecord(oldRecordList, record);
if(changes(record, old, "notebookId"))
{
badInput(record, "A share cannot move to another notebook.");
continue;
}
record.removeValue("createdByUserId");
record.removeValue("createdByName");
record.removeValue("createDate");
if(record.getValues().containsKey("audienceType") || record.getValues().containsKey("audienceId"))
{
String type = record.getValues().containsKey("audienceType") ? record.getValueString("audienceType") : old == null ? null : old.getValueString("audienceType");
String id = record.getValues().containsKey("audienceId") ? record.getValueString("audienceId") : old == null ? null : old.getValueString("audienceId");
applyAudience(record, type, id);
}
else
{
record.removeValue("audienceKey");
}
record.setValue("modifyDate", now);
}
return (records);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> postUpdate(UpdateInput updateInput, List<QRecord> records, Optional<List<QRecord>> oldRecordList) throws QException
{
Set<Integer> ids = new LinkedHashSet<>();
for(QRecord record : records)
{
QRecord old = oldRecord(oldRecordList, record);
if(ok(record) && old != null && old.getValueInteger("notebookId") != null)
{
ids.add(old.getValueInteger("notebookId"));
}
}
syncVisibility(updateInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL), ids, updateInput.getTransaction());
return (records);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preDelete(DeleteInput deleteInput, List<QRecord> records, boolean isPreview) throws QException
{
if(!mayShare())
{
for(QRecord record : records)
{
if(ok(record))
{
denied(record, NEEDS_PERMISSION);
}
}
}
return (records);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> postDelete(DeleteInput deleteInput, List<QRecord> records) throws QException
{
syncVisibility(deleteInput.hasFlag(SqlWorkbenchActionFlag.INTERNAL), notebookIds(records), deleteInput.getTransaction());
return (records);
}
/*******************************************************************************
** The system user, or a session holding the share permission.
*******************************************************************************/
private static boolean mayShare() throws QException
{
QSession session = QContext.getQSession();
return (isSystemUser() || (session != null && session.hasPermission(config().getSharePermissionName())));
}
/*******************************************************************************
** Validate and normalize the audience; compute the audience key.
*******************************************************************************/
private static void applyAudience(QRecord record, String typeValue, String audienceId)
{
ShareAudienceType type = ShareAudienceType.of(typeValue);
if(type == null)
{
badInput(record, "audienceType must be USER, GROUP or ALL.");
return;
}
record.setValue("audienceType", type.name());
if(type == ShareAudienceType.ALL)
{
record.setValue("audienceId", null);
}
else if(!StringUtils.hasContent(audienceId))
{
badInput(record, type == ShareAudienceType.USER ? "Name the user to share with." : "Name the group to share with.");
return;
}
else
{
record.setValue("audienceId", audienceId.trim());
}
record.setValue("audienceKey", type.audienceKey(audienceId));
}
/*******************************************************************************
** Set each notebook's visibility from its shares: SHARED while it has an
** ALL share, else PERSONAL (system user; the share change was already
** authorized).
*******************************************************************************/
private static void syncVisibility(boolean internal, Set<Integer> notebookIds, QBackendTransaction transaction) throws QException
{
if(internal || notebookIds.isEmpty())
{
return;
}
SqlWorkbenchQBitConfig config = config();
Instant now = clock().instant();
SystemUser.run(() ->
{
for(Integer notebookId : notebookIds)
{
Integer allShares = new CountAction().execute(new CountInput(config.applyPrefix(SqlWorkbenchNotebookShare.TABLE_NAME))
.withTransaction(transaction)
.withFilter(new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.EQUALS, notebookId))
.withCriteria(new QFilterCriteria("audienceKey", QCriteriaOperator.EQUALS, SqlWorkbenchSecurity.AUDIENCE_ALL)))).getCount();
String visibility = allShares != null && allShares > 0 ? NotebookService.VISIBILITY_SHARED : NotebookService.VISIBILITY_PERSONAL;
QRecord updated = new UpdateAction().execute(new UpdateInput(config.applyPrefix(SqlWorkbenchNotebook.TABLE_NAME))
.withFlag(SqlWorkbenchActionFlag.INTERNAL)
.withTransaction(transaction)
.withRecord(new QRecord().withValue("id", notebookId).withValue("visibility", visibility).withValue("modifyDate", now))).getRecords().get(0);
if(!ok(updated))
{
LOG.warn("Could not sync a notebook's visibility with its shares", logPair("notebookId", notebookId), logPair("error", updated.getErrors().get(0).getMessage()));
throw (new QException("Could not update the notebook's visibility: " + updated.getErrors().get(0).getMessage()));
}
}
});
}
}
@@ -0,0 +1,87 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.customizers;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
/*******************************************************************************
** Pinned results are created only by pinning a cached result from the
** notebook UI (the data must come from a real run, never from a client);
** through the table only the label can be edited, and the notebook's owner
** can delete them (the record lock checks ownership through the notebook).
*******************************************************************************/
public class SnapshotTableCustomizer extends WorkbenchTableCustomizer
{
private static final Set<String> EDITABLE = Set.of("id", "label");
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preInsert(InsertInput insertInput, List<QRecord> records, boolean isPreview) throws QException
{
if(!isSystemUser() && !insertInput.hasFlag(SqlWorkbenchActionFlag.SNAPSHOT_PIN))
{
for(QRecord record : records)
{
denied(record, "Results are pinned from the notebook: run a cell, then pin its result.");
}
}
return (records);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> preUpdate(UpdateInput updateInput, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
{
if(isSystemUser())
{
return (records);
}
for(QRecord record : records)
{
if(!ok(record))
{
continue;
}
QRecord old = oldRecord(oldRecordList, record);
for(String field : List.copyOf(record.getValues().keySet()))
{
if(!EDITABLE.contains(field) && changes(record, old, field))
{
badInput(record, "Only a pinned result's label can be edited.");
break;
}
}
}
return (records);
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.customizers;
import com.kingsrook.qqq.backend.core.model.actions.tables.ActionFlag;
/*******************************************************************************
** Flags the qbit puts on its own table actions, so the customizers know a
** write is part of a larger, already-validated operation. Flags are set in
** Java only; no API or UI request can carry them.
*******************************************************************************/
public enum SqlWorkbenchActionFlag implements ActionFlag
{
/** a whole-notebook save from NotebookService: it writes one version itself */
NOTEBOOK_SAVE,
/** pinning a cached result from the notebook UI (snapshot inserts) */
SNAPSHOT_PIN,
/** the qbit's own bookkeeping (version bump, visibility sync, cascades) */
INTERNAL
}
@@ -0,0 +1,281 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.customizers;
import java.io.Serializable;
import java.time.Clock;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchRuntime;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchSecurity;
import com.kingsrook.qbits.sqlworkbench.notebook.NotebookVersioning;
import com.kingsrook.qqq.backend.core.actions.QBackendTransaction;
import com.kingsrook.qqq.backend.core.actions.customizers.AbstractPreInsertCustomizer;
import com.kingsrook.qqq.backend.core.actions.customizers.TableCustomizerInterface;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.logging.QLogger;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.statusmessages.BadInputStatusMessage;
import com.kingsrook.qqq.backend.core.model.statusmessages.PermissionDeniedMessage;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
/*******************************************************************************
** Shared plumbing for the workbench table customizers.
**
** Pre-insert customizers run before QQQ's validations, so they can stamp
** the owner / defaults the record locks and required-field checks then
** verify. Update and delete validations (locks included) run before the
** pre-update / pre-delete customizers, which skip records that already
** carry errors and never rewrite a lock field.
*******************************************************************************/
public abstract class WorkbenchTableCustomizer implements TableCustomizerInterface
{
protected static final QLogger LOG = QLogger.getLogger(WorkbenchTableCustomizer.class);
/*******************************************************************************
** Stamp defaults before QQQ validates (required fields, record locks).
*******************************************************************************/
@Override
public AbstractPreInsertCustomizer.WhenToRun whenToRunPreInsert(InsertInput insertInput, boolean isPreview)
{
return (AbstractPreInsertCustomizer.WhenToRun.BEFORE_ALL_VALIDATIONS);
}
/*******************************************************************************
** Pass-through; subclasses override the hooks they need.
*******************************************************************************/
@Override
public List<QRecord> preInsert(InsertInput insertInput, List<QRecord> records, boolean isPreview) throws QException
{
return (records);
}
/*******************************************************************************
** Pass-through.
*******************************************************************************/
@Override
public List<QRecord> postInsert(InsertInput insertInput, List<QRecord> records) throws QException
{
return (records);
}
/*******************************************************************************
** Pass-through.
*******************************************************************************/
@Override
public List<QRecord> preUpdate(UpdateInput updateInput, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
{
return (records);
}
/*******************************************************************************
** Pass-through.
*******************************************************************************/
@Override
public List<QRecord> postUpdate(UpdateInput updateInput, List<QRecord> records, Optional<List<QRecord>> oldRecordList) throws QException
{
return (records);
}
/*******************************************************************************
** Pass-through.
*******************************************************************************/
@Override
public List<QRecord> preDelete(DeleteInput deleteInput, List<QRecord> records, boolean isPreview) throws QException
{
return (records);
}
/*******************************************************************************
** Pass-through.
*******************************************************************************/
@Override
public List<QRecord> postDelete(DeleteInput deleteInput, List<QRecord> records) throws QException
{
return (records);
}
/*******************************************************************************
** The old record for a record, by primary key "id".
*******************************************************************************/
protected static QRecord oldRecord(Optional<List<QRecord>> oldRecordList, QRecord record)
{
Serializable id = record.getValue("id");
for(QRecord old : oldRecordList.orElse(List.of()))
{
if(id != null && Objects.equals(String.valueOf(id), String.valueOf(old.getValue("id"))))
{
return (old);
}
}
return (null);
}
/*******************************************************************************
** The installed workbench's configuration.
*******************************************************************************/
protected static SqlWorkbenchQBitConfig config() throws QException
{
return (SqlWorkbenchRuntime.get().getConfig());
}
/*******************************************************************************
** The workbench clock.
*******************************************************************************/
protected static Clock clock() throws QException
{
return (SqlWorkbenchRuntime.get().getClock());
}
/*******************************************************************************
** True if the record has no errors yet.
*******************************************************************************/
protected static boolean ok(QRecord record)
{
return (CollectionUtils.nullSafeIsEmpty(record.getErrors()));
}
/*******************************************************************************
** Add a bad-input error to a record.
*******************************************************************************/
protected static void badInput(QRecord record, String message)
{
record.addError(new BadInputStatusMessage(message));
}
/*******************************************************************************
** Add a permission-denied error to a record.
*******************************************************************************/
protected static void denied(QRecord record, String message)
{
record.addError(new PermissionDeniedMessage(message));
}
/*******************************************************************************
** True if the record sets the field to a value different from the old
** record's (absent fields are unchanged).
*******************************************************************************/
protected static boolean changes(QRecord record, QRecord oldRecord, String field)
{
if(!record.getValues().containsKey(field))
{
return (false);
}
Serializable oldValue = oldRecord == null ? null : oldRecord.getValue(field);
return (!Objects.equals(normalize(record.getValue(field)), normalize(oldValue)));
}
/*******************************************************************************
**
*******************************************************************************/
private static String normalize(Serializable value)
{
return (value == null ? null : value.toString());
}
/*******************************************************************************
** The distinct notebook ids of the error-free records.
*******************************************************************************/
protected static Set<Integer> notebookIds(List<QRecord> records)
{
Set<Integer> ids = new LinkedHashSet<>();
for(QRecord record : CollectionUtils.nonNullList(records))
{
if(ok(record) && record.getValueInteger("notebookId") != null)
{
ids.add(record.getValueInteger("notebookId"));
}
}
return (ids);
}
/*******************************************************************************
** Append one version per notebook.
*******************************************************************************/
protected static void appendVersions(Set<Integer> notebookIds, String comment, QBackendTransaction transaction) throws QException
{
for(Integer notebookId : notebookIds)
{
try
{
NotebookVersioning.appendVersion(config(), clock(), notebookId, comment, transaction);
}
catch(QException e)
{
////////////////////////////////////////////////////////////////////
// QQQ turns post-customizer exceptions into record warnings, so //
// log here too: a change without its version must be visible //
////////////////////////////////////////////////////////////////////
LOG.warn("Could not append a notebook version", e, logPair("notebookId", notebookId), logPair("comment", comment));
throw (e);
}
}
}
/*******************************************************************************
** True for the qbit's own (system user) writes.
*******************************************************************************/
protected static boolean isSystemUser()
{
return (SqlWorkbenchSecurity.isSystemUser());
}
}
@@ -0,0 +1,160 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.developer;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qbits.sqlworkbench.sql.SqlDialect;
import com.kingsrook.qqq.backend.module.rdbms.actions.AbstractRDBMSAction;
/*******************************************************************************
** Builds read-only SELECT statements for a QQQ table in its RDBMS backend's
** own names (the table's backend table name and each field's backend column
** name, as QQQ's RDBMS module resolves them), with identifiers quoted and
** the primary-key value written as an escaped literal for the dialect.
**
** The SQL is only shown to the user (and opened, not run, in the
** workbench), so it uses literals rather than parameters.
*******************************************************************************/
public final class RecordSql
{
public static final int TABLE_ROW_LIMIT = 100;
/*******************************************************************************
**
*******************************************************************************/
private RecordSql()
{
}
/*******************************************************************************
** SELECT the table's columns for one primary-key value.
*******************************************************************************/
public static String selectRecord(QTableMetaData table, SqlDialect dialect, Serializable primaryKey)
{
QFieldMetaData primaryKeyField = table.getField(table.getPrimaryKeyField());
return (selectColumns(table, dialect)
+ "\nWHERE " + quoteIdentifier(dialect, AbstractRDBMSAction.getColumnName(primaryKeyField)) + " = " + literal(dialect, primaryKeyField.getType(), primaryKey));
}
/*******************************************************************************
** SELECT the table's columns, first rows only.
*******************************************************************************/
public static String selectTable(QTableMetaData table, SqlDialect dialect)
{
String select = selectColumns(table, dialect);
if(dialect == SqlDialect.GENERIC)
{
return (select + "\nFETCH FIRST " + TABLE_ROW_LIMIT + " ROWS ONLY");
}
return (select + "\nLIMIT " + TABLE_ROW_LIMIT);
}
/*******************************************************************************
** SELECT clause listing every field's column (aliased to the QQQ field
** name where the names differ) and the FROM clause.
*******************************************************************************/
static String selectColumns(QTableMetaData table, SqlDialect dialect)
{
List<String> columns = new ArrayList<>();
for(QFieldMetaData field : table.getFields().values())
{
String column = AbstractRDBMSAction.getColumnName(field);
columns.add(column.equals(field.getName())
? quoteIdentifier(dialect, column)
: quoteIdentifier(dialect, column) + " AS " + quoteIdentifier(dialect, field.getName()));
}
return ("SELECT\n " + String.join(",\n ", columns)
+ "\nFROM " + quoteIdentifier(dialect, AbstractRDBMSAction.getTableName(table)));
}
/*******************************************************************************
** Quote an identifier: backticks for MySQL, double quotes otherwise (ANSI;
** Postgres, SQLite and H2 all accept them). A quote character inside the
** identifier is doubled.
*******************************************************************************/
public static String quoteIdentifier(SqlDialect dialect, String identifier)
{
String quote = dialect == SqlDialect.MYSQL ? "`" : "\"";
return (quote + identifier.replace(quote, quote + quote) + quote);
}
/*******************************************************************************
** A value as a SQL literal: NULL; a plain number for numeric fields whose
** value parses as one; otherwise a single-quoted string with quotes
** doubled (and, for MySQL, backslashes escaped).
*******************************************************************************/
public static String literal(SqlDialect dialect, QFieldType type, Serializable value)
{
if(value == null)
{
return ("NULL");
}
if(type == QFieldType.INTEGER || type == QFieldType.LONG || type == QFieldType.DECIMAL || value instanceof Number)
{
try
{
return (new BigDecimal(value.toString().trim()).toPlainString());
}
catch(NumberFormatException e)
{
///////////////////////////////////////////////////////
// not a number after all: fall through to a string //
///////////////////////////////////////////////////////
}
}
return (stringLiteral(dialect, value.toString()));
}
/*******************************************************************************
** A single-quoted string literal.
*******************************************************************************/
public static String stringLiteral(SqlDialect dialect, String value)
{
String escaped = value;
if(dialect.isBackslashEscapes())
{
escaped = escaped.replace("\\", "\\\\");
}
return ("'" + escaped.replace("'", "''") + "'");
}
}
@@ -0,0 +1,269 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.developer;
import java.io.Serializable;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.exceptions.QNotFoundException;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanel;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanelBlock;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanelContext;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanelLink;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanelProviderInterface;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanelTone;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchRuntime;
import com.kingsrook.qbits.sqlworkbench.engine.WorkbenchBackends;
import com.kingsrook.qbits.sqlworkbench.metadata.SqlWorkbenchAppMetaDataProducer;
import com.kingsrook.qbits.sqlworkbench.metadata.SqlWorkbenchRunnerAppMetaDataProducer;
import com.kingsrook.qqq.backend.module.rdbms.actions.AbstractRDBMSAction;
/*******************************************************************************
** Record developer panel "SQL Workbench": read-only SELECTs for the record
** (and its table) in the backend's own table and column names, with links
** that open them in the workbench as new cells (never run).
**
** Available when the workbench is in the instance (its runtime and app), the
** user has the workbench access permission (the same check its endpoints
** make), and the table's backend is an RDBMS backend the workbench allows.
** The panel only builds text: nothing is executed, and the user's write
** permission makes no difference.
*******************************************************************************/
public class SqlWorkbenchDeveloperPanelProvider implements DeveloperPanelProviderInterface
{
public static final String ID = "sqlWorkbench";
/*******************************************************************************
**
*******************************************************************************/
@Override
public String getId()
{
return (ID);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public String getTitle()
{
return ("SQL Workbench");
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public Integer getOrder()
{
return (450);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public String getIcon()
{
return ("terminal");
}
/*******************************************************************************
** Available for a table on an allowed RDBMS backend, to users with the
** workbench access permission, when the workbench app exists.
*******************************************************************************/
@Override
public boolean isAvailable(DeveloperPanelContext context)
{
return (resolve(context) != null);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public DeveloperPanel build(DeveloperPanelContext context) throws QException
{
Resolved resolved = resolve(context);
if(resolved == null)
{
throw (new QNotFoundException("The SQL Workbench is not available for this table."));
}
QTableMetaData table = context.getTable();
WorkbenchBackends.Target target = resolved.target();
String runnerApp = resolved.config().applyPrefix(SqlWorkbenchRunnerAppMetaDataProducer.APP_NAME);
Serializable primaryKey = context.getPrimaryKey();
DeveloperPanel panel = new DeveloperPanel()
.withDescription("Read-only SQL for this record on the " + target.name() + " backend, in the database's own table and column names.")
.withStatusLabel("Read-only")
.withStatusTone(DeveloperPanelTone.INFO);
DeveloperPanelBlock links = DeveloperPanelBlock.links("Open in SQL Workbench");
if(primaryKey != null)
{
String recordSql = RecordSql.selectRecord(table, target.dialect(), primaryKey);
panel.withBlock(DeveloperPanelBlock.code("Read-only query for this record", "sql", recordSql));
links.withLink(new DeveloperPanelLink()
.withLabel("Open in SQL Workbench")
.withHref(workbenchHref(runnerApp, recordSql, target.name()))
.withDescription("This record's query, as a new cell (not run)"));
}
String tableSql = RecordSql.selectTable(table, target.dialect());
panel.withBlock(DeveloperPanelBlock.code("Read-only query for this table (first " + RecordSql.TABLE_ROW_LIMIT + " rows)", "sql", tableSql));
links.withLink(new DeveloperPanelLink()
.withLabel(primaryKey == null ? "Open in SQL Workbench" : "Open table query in SQL Workbench")
.withHref(workbenchHref(runnerApp, tableSql, target.name()))
.withDescription("The table's query, as a new cell (not run)"));
panel.withBlock(DeveloperPanelBlock.keyValues("Backend")
.withItem("Backend", target.name())
.withItem("Dialect", target.dialect().getUiName())
.withItem("Database table", AbstractRDBMSAction.getTableName(table))
.withItem("Read-only replica", describeReadOnlySource(resolved)));
panel.withBlock(links);
panel.withBlock(DeveloperPanelBlock.text("The workbench runs only what you execute, under your SQL Workbench permissions. "
+ "These queries are SELECTs; opening them does not run them.", DeveloperPanelTone.INFO));
return (panel);
}
/*******************************************************************************
** The Runner page with the SQL to open (as a new cell, never run) and its
** backend.
*******************************************************************************/
static String workbenchHref(String appName, String sql, String backendName)
{
return ("/app/" + encode(appName) + "?sql=" + encode(sql) + "&backend=" + encode(backendName));
}
/*******************************************************************************
** URL-encode a query parameter (spaces as %20, not '+').
*******************************************************************************/
static String encode(String value)
{
return (URLEncoder.encode(value, StandardCharsets.UTF_8).replace("+", "%20"));
}
/*******************************************************************************
** Where read-only workbench sessions on the target connect, as
** WorkbenchBackends.connectionSource resolves it.
*******************************************************************************/
private String describeReadOnlySource(Resolved resolved)
{
if(resolved.backends().hasReadOnlyCredentials(resolved.target()))
{
return ("No (dedicated read-only credentials)");
}
if(Boolean.TRUE.equals(resolved.config().getUseBackendReadOnlyReplica()) && resolved.target().backend().getReadOnlyBackendMetaData() != null)
{
return ("Yes (" + resolved.target().backend().getReadOnlyBackendMetaData().getName() + ")");
}
return ("No");
}
/*******************************************************************************
** The workbench config and target for the context's table, or null when
** the panel is not available.
*******************************************************************************/
private Resolved resolve(DeveloperPanelContext context)
{
QInstance qInstance = QContext.getQInstance();
QSession session = QContext.getQSession();
if(qInstance == null || session == null || context == null || context.getTable() == null)
{
return (null);
}
QTableMetaData table = context.getTable();
if(!StringUtils.hasContent(table.getBackendName()) || !StringUtils.hasContent(table.getPrimaryKeyField()) || table.getField(table.getPrimaryKeyField()) == null)
{
return (null);
}
SqlWorkbenchRuntime runtime;
try
{
runtime = SqlWorkbenchRuntime.get(qInstance);
}
catch(QNotFoundException e)
{
return (null);
}
SqlWorkbenchQBitConfig config = runtime.getConfig();
if(qInstance.getApp(config.applyPrefix(SqlWorkbenchAppMetaDataProducer.APP_NAME)) == null || !session.hasPermission(config.getAccessPermissionName()))
{
return (null);
}
WorkbenchBackends backends = new WorkbenchBackends(qInstance, runtime);
try
{
return (new Resolved(config, backends, backends.getTarget(table.getBackendName())));
}
catch(QNotFoundException e)
{
return (null);
}
}
/*******************************************************************************
** The workbench config, backends and target for a table.
*******************************************************************************/
private record Resolved(SqlWorkbenchQBitConfig config, WorkbenchBackends backends, WorkbenchBackends.Target target)
{
}
}
@@ -17,6 +17,10 @@
package com.kingsrook.qbits.sqlworkbench.engine;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchSecurity;
import com.kingsrook.qqq.backend.core.model.session.QSession;
/*******************************************************************************
** Who is calling, and what they may do (resolved from the QSession by the
** API layer).
@@ -29,4 +33,16 @@ package com.kingsrook.qbits.sqlworkbench.engine;
*******************************************************************************/
public record Caller(String userId, String userName, boolean canWrite, boolean canShare, String clientIp)
{
/*******************************************************************************
** The Caller of a session (which must already be authenticated and hold
** the access permission - the API and the record actions check that).
*******************************************************************************/
public static Caller fromSession(SqlWorkbenchQBitConfig config, QSession session, String clientIp)
{
String userId = SqlWorkbenchSecurity.userId(session);
boolean canWrite = Boolean.TRUE.equals(config.getWriteModeEnabled()) && session.hasPermission(config.getWritePermissionName());
boolean canShare = session.hasPermission(config.getSharePermissionName());
return (new Caller(userId, SqlWorkbenchSecurity.userName(session), canWrite, canShare, clientIp));
}
}
@@ -0,0 +1,297 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.home;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/*******************************************************************************
** Small, theme-neutral HTML pieces for the home widgets: inline styles only
** (no host CSS), colors that read in light and dark themes, text that wraps
** at any width (no inner horizontal scroll), and every value escaped.
*******************************************************************************/
public final class HomeHtml
{
public static final String OK_COLOR = "#16a34a";
public static final String BAD_COLOR = "#dc2626";
public static final String WARN_COLOR = "#d97706";
public static final String INFO_COLOR = "#2563eb";
public static final String OFF_COLOR = "#6b7280";
private static final String MUTED = "opacity:.75";
private static final String WRAP = "overflow-wrap:anywhere;min-width:0";
private static final String TILE = "flex:1 1 170px;min-width:0;border:1px solid rgba(127,127,127,.25);border-radius:10px;padding:12px 14px";
private static final DateTimeFormatter TIME = DateTimeFormatter.ofPattern("h:mm a", Locale.US);
private static final DateTimeFormatter DATE_TIME = DateTimeFormatter.ofPattern("MMM d, h:mm a", Locale.US);
private static final DateTimeFormatter FULL = DateTimeFormatter.ofPattern("MMM d, yyyy, h:mm a", Locale.US);
/*******************************************************************************
** One summary tile: a label, a value (linked when href is set) and an
** optional note.
*******************************************************************************/
public record Tile(String label, String value, String note, String href)
{
}
/*******************************************************************************
** Utility class - no instances.
*******************************************************************************/
private HomeHtml()
{
}
/*******************************************************************************
** HTML-escape a value (null is empty).
*******************************************************************************/
public static String escape(Object value)
{
if(value == null)
{
return ("");
}
String text = String.valueOf(value);
StringBuilder escaped = new StringBuilder(text.length() + 16);
for(char c : text.toCharArray())
{
switch(c)
{
case '<' -> escaped.append("&lt;");
case '>' -> escaped.append("&gt;");
case '&' -> escaped.append("&amp;");
case '"' -> escaped.append("&quot;");
case '\'' -> escaped.append("&#39;");
default -> escaped.append(c);
}
}
return (escaped.toString());
}
/*******************************************************************************
** A link in the text color, underlined.
*******************************************************************************/
public static String link(String href, Object text)
{
return ("<a href=\"" + escape(href) + "\" style=\"color:inherit;text-decoration:underline;text-underline-offset:2px\">" + escape(text) + "</a>");
}
/*******************************************************************************
** Muted (secondary) text.
*******************************************************************************/
public static String muted(Object text)
{
return ("<span style=\"" + MUTED + ";font-size:13px\">" + escape(text) + "</span>");
}
/*******************************************************************************
** A pill with a colored border and text.
*******************************************************************************/
public static String badge(String text, String color)
{
return ("<span style=\"display:inline-block;white-space:nowrap;padding:1px 8px;border-radius:999px;font-size:12px;font-weight:600;line-height:1.5;"
+ "color:" + color + ";border:1px solid " + color + "\">" + escape(text) + "</span>");
}
/*******************************************************************************
** A primary line (already HTML) with muted facts (already HTML) after it,
** wrapping onto more lines on narrow screens.
*******************************************************************************/
public static String card(String primaryHtml, String... factsHtml)
{
List<String> facts = new ArrayList<>();
for(String fact : factsHtml)
{
if(fact != null && !fact.isBlank())
{
facts.add(fact);
}
}
StringBuilder html = new StringBuilder("<span style=\"display:flex;flex-wrap:wrap;align-items:baseline;column-gap:10px;row-gap:2px;min-width:0\">");
html.append("<span style=\"").append(WRAP).append("\">").append(primaryHtml == null ? "" : primaryHtml).append("</span>");
if(!facts.isEmpty())
{
html.append("<span style=\"").append(WRAP).append(';').append(MUTED).append(";font-size:13px\">").append(String.join(" &middot; ", facts)).append("</span>");
}
return (html.append("</span>").toString());
}
/*******************************************************************************
** SQL (or any long text) as monospace, on one line, cut at maxChars and
** wrapped anywhere; the full text is in the title tooltip.
*******************************************************************************/
public static String code(String text, int maxChars)
{
String oneLine = text == null ? "" : text.replaceAll("\\s+", " ").trim();
String shown = oneLine.length() > maxChars ? oneLine.substring(0, maxChars - 1) + "…" : oneLine;
return ("<code title=\"" + escape(oneLine.length() > 2000 ? oneLine.substring(0, 2000) : oneLine) + "\" style=\"font-size:12px;" + WRAP + "\">" + escape(shown) + "</code>");
}
/*******************************************************************************
** Wrapping summary tiles: they grow to fill each row, so there are no holes.
*******************************************************************************/
public static String tiles(List<Tile> tiles)
{
StringBuilder html = new StringBuilder("<div style=\"display:flex;flex-wrap:wrap;gap:12px\">");
for(Tile tile : tiles)
{
html.append("<div style=\"").append(TILE).append("\">");
html.append("<div style=\"font-size:12px;").append(MUTED).append(";text-transform:uppercase;letter-spacing:.04em;").append(WRAP).append("\">")
.append(escape(tile.label())).append("</div>");
String value = tile.href() == null ? escape(tile.value())
: "<a href=\"" + escape(tile.href()) + "\" style=\"color:inherit;text-decoration:none\">" + escape(tile.value()) + "</a>";
html.append("<div style=\"font-size:26px;font-weight:700;line-height:1.3;").append(WRAP).append("\">").append(value).append("</div>");
if(tile.note() != null)
{
html.append("<div style=\"").append(MUTED).append(";font-size:13px;").append(WRAP).append("\">").append(escape(tile.note())).append("</div>");
}
html.append("</div>");
}
return (html.append("</div>").toString());
}
/*******************************************************************************
** Link buttons, wrapping.
*******************************************************************************/
public static String actions(List<String[]> labelsAndHrefs)
{
StringBuilder html = new StringBuilder("<div style=\"display:flex;flex-wrap:wrap;gap:8px;margin-top:12px\">");
for(String[] action : labelsAndHrefs)
{
html.append("<a href=\"").append(escape(action[1]))
.append("\" style=\"display:inline-flex;align-items:center;padding:8px 12px;border-radius:8px;border:1px solid rgba(127,127,127,.3);")
.append("background:rgba(127,127,127,.08);color:inherit;text-decoration:none;font-size:13px;font-weight:500\">")
.append(escape(action[0])).append("</a>");
}
return (html.append("</div>").toString());
}
/*******************************************************************************
** An honest empty or unavailable state.
*******************************************************************************/
public static String emptyState(String message)
{
return ("<div style=\"padding:18px 4px;" + MUTED + ";font-size:14px;" + WRAP + "\">" + escape(message) + "</div>");
}
/*******************************************************************************
** A muted explanatory note under a widget's content.
*******************************************************************************/
public static String note(String text)
{
return ("<div style=\"margin-top:10px;" + MUTED + ";font-size:13px;" + WRAP + "\">" + escape(text) + "</div>");
}
/*******************************************************************************
** A whole number with thousands separators.
*******************************************************************************/
public static String number(long value)
{
return (String.format(Locale.US, "%,d", value));
}
/*******************************************************************************
** "1 run", "2 runs".
*******************************************************************************/
public static String plural(long count, String noun)
{
return (number(count) + " " + noun + (count == 1 ? "" : "s"));
}
/*******************************************************************************
** A duration in milliseconds: "850 ms", "4.2 s", "3m 05s".
*******************************************************************************/
public static String duration(Integer millis)
{
if(millis == null)
{
return ("");
}
if(millis < 1000)
{
return (millis + " ms");
}
if(millis < 60_000)
{
return (String.format(Locale.US, "%.1f s", millis / 1000.0));
}
long seconds = millis / 1000;
return (String.format(Locale.US, "%dm %02ds", seconds / 60, seconds % 60));
}
/*******************************************************************************
** An instant, compact: the time today, the date and time this year, the
** full date otherwise.
*******************************************************************************/
public static String when(Instant instant, Instant now, ZoneId zoneId)
{
if(instant == null)
{
return ("");
}
ZonedDateTime when = instant.atZone(zoneId);
ZonedDateTime today = now.atZone(zoneId);
if(when.toLocalDate().equals(today.toLocalDate()))
{
return (TIME.format(when));
}
if(when.getYear() == today.getYear())
{
return (DATE_TIME.format(when));
}
return (FULL.format(when));
}
}
@@ -0,0 +1,996 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.home;
import java.io.Serializable;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchSecurity;
import com.kingsrook.qbits.sqlworkbench.engine.ExecutionMode;
import com.kingsrook.qbits.sqlworkbench.engine.ExecutionStatus;
import com.kingsrook.qbits.sqlworkbench.metadata.SqlWorkbenchRunnerAppMetaDataProducer;
import com.kingsrook.qbits.sqlworkbench.model.ShareAudienceType;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchExecution;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qqq.backend.core.actions.permissions.PermissionsHelper;
import com.kingsrook.qqq.backend.core.actions.permissions.TablePermissionSubType;
import com.kingsrook.qqq.backend.core.actions.tables.AggregateAction;
import com.kingsrook.qqq.backend.core.actions.tables.CountAction;
import com.kingsrook.qqq.backend.core.actions.tables.QueryAction;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.Aggregate;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateOperator;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateResult;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.GroupBy;
import com.kingsrook.qqq.backend.core.model.actions.tables.count.CountInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.ChartData;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.QWidgetData;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.RawHTML;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.TableData;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.core.utils.ValueUtils;
/*******************************************************************************
** The data behind the SQL Workbench home dashboard and the notebook's
** "Open in Runner" widget.
**
** Everything is read through QQQ's core actions in the viewer's own session,
** so the tables' permissions and record security locks decide what each
** widget counts and lists: a user sees their own runs and the notebooks
** they can open; a workbench admin (all-access key) sees everyone's. A
** widget whose table the user may not read says so instead of showing
** zeros. Nothing here writes, and no SQL text is logged.
*******************************************************************************/
public final class SqlWorkbenchHome
{
public static final String KIND_SUMMARY = "summary";
public static final String KIND_RUNS_PER_DAY = "runsPerDay";
public static final String KIND_RECENT_NOTEBOOKS = "recentNotebooks";
public static final String KIND_RECENT_RUNS = "recentRuns";
public static final String KIND_SLOWEST = "slowest";
public static final String KIND_FAILED = "failed";
public static final String KIND_MOST_USED = "mostUsed";
public static final String KIND_SHARED = "shared";
public static final String KIND_WRITES = "writes";
public static final String KIND_BY_USER = "byUser";
public static final String KIND_NOTEBOOK_RUNNER = "notebookRunner";
static final int DAYS = 30;
static final int ROWS = 10;
static final int USER_ROWS = 25;
static final int SCAN_LIMIT = 20_000;
static final int SHARE_LIMIT = 1_000;
static final int SQL_CHARS = 140;
static final int ERROR_CHARS = 200;
static final List<String> FAILED_STATUSES = List.of(ExecutionStatus.FAILED.name(), ExecutionStatus.TIMED_OUT.name(),
ExecutionStatus.REJECTED.name(), ExecutionStatus.ROLLED_BACK.name());
private static final DateTimeFormatter DAY = DateTimeFormatter.ofPattern("MMM d", Locale.US);
/*******************************************************************************
** Who is looking, and when: the config, the clock's now, the display time
** zone, the viewer's user id, and whether they hold the workbench
** all-access key (see everyone's notebooks and runs).
*******************************************************************************/
record Viewer(SqlWorkbenchQBitConfig config, Instant now, ZoneId zoneId, String userId, boolean allAccess)
{
/*******************************************************************************
** The (prefixed) name of one of the qbit's tables.
*******************************************************************************/
String table(String tableName)
{
return (config.applyPrefix(tableName));
}
/*******************************************************************************
** Start of the window of the last DAYS days.
*******************************************************************************/
Instant windowStart()
{
return (today().minusDays(DAYS - 1L).atStartOfDay(zoneId).toInstant());
}
/*******************************************************************************
** Today in the display zone.
*******************************************************************************/
LocalDate today()
{
return (now.atZone(zoneId).toLocalDate());
}
}
/*******************************************************************************
** Utility class - no instances.
*******************************************************************************/
private SqlWorkbenchHome()
{
}
/*******************************************************************************
** The viewer of the current session.
*******************************************************************************/
static Viewer viewer(SqlWorkbenchQBitConfig config, Instant now)
{
QSession session = QContext.getQSession();
SqlWorkbenchSecurity.ensureSessionKeys(config, session);
String userId = session == null || session.getUser() == null ? null : session.getUser().getIdReference();
return (new Viewer(config, now, ValueUtils.getSessionOrInstanceZoneId(), userId, SqlWorkbenchSecurity.hasAllAccess(config)));
}
/*******************************************************************************
** Load one widget's data.
*******************************************************************************/
static QWidgetData load(String kind, Viewer viewer, RenderWidgetInput input) throws QException
{
return (switch(kind == null ? "" : kind)
{
case KIND_SUMMARY -> summary(viewer, input);
case KIND_RUNS_PER_DAY -> runsPerDay(viewer, input);
case KIND_RECENT_NOTEBOOKS -> recentNotebooks(viewer, input);
case KIND_RECENT_RUNS -> recentRuns(viewer, input);
case KIND_SLOWEST -> slowest(viewer, input);
case KIND_FAILED -> failed(viewer, input);
case KIND_MOST_USED -> mostUsed(viewer, input);
case KIND_SHARED -> shared(viewer, input);
case KIND_WRITES -> writes(viewer, input);
case KIND_BY_USER -> byUser(viewer, input);
case KIND_NOTEBOOK_RUNNER -> notebookRunner(viewer, input);
default -> new RawHTML(null, HomeHtml.emptyState("Unknown SQL Workbench widget."));
});
}
/*******************************************************************************
** Tiles (notebooks, runs today, runs and failures in 7 days, writes in
** DAYS days), what they count, and links to the Runner and the tables.
*******************************************************************************/
static QWidgetData summary(Viewer viewer, RenderWidgetInput input) throws QException
{
String notebooks = viewer.table(SqlWorkbenchNotebook.TABLE_NAME);
String executions = viewer.table(SqlWorkbenchExecution.TABLE_NAME);
List<HomeHtml.Tile> tiles = new ArrayList<>();
if(canRead(input, notebooks))
{
long total = count(notebooks, new QQueryFilter());
long mine = viewer.userId() == null ? 0 : count(notebooks, new QQueryFilter(new QFilterCriteria("ownerUserId", QCriteriaOperator.EQUALS, viewer.userId())));
tiles.add(new HomeHtml.Tile("Notebooks", HomeHtml.number(total),
HomeHtml.number(mine) + " yours, " + HomeHtml.number(total - mine) + (viewer.allAccess() ? " by other users" : " shared with you"), "/app/" + notebooks));
}
if(canRead(input, executions))
{
Instant startOfToday = viewer.today().atStartOfDay(viewer.zoneId()).toInstant();
Instant weekStart = viewer.now().minus(Duration.ofDays(7));
Instant window = viewer.windowStart();
tiles.add(new HomeHtml.Tile("Runs Today", HomeHtml.number(count(executions, since(startOfToday))), null, "/app/" + executions));
tiles.add(new HomeHtml.Tile("Runs, 7 Days", HomeHtml.number(count(executions, since(weekStart))),
HomeHtml.number(count(executions, since(weekStart).withCriteria(new QFilterCriteria("status", QCriteriaOperator.IN, FAILED_STATUSES)))) + " failed", null));
Writes writes = writesSince(executions, window);
tiles.add(new HomeHtml.Tile("Writes, " + DAYS + " Days", HomeHtml.number(writes.count()),
Boolean.TRUE.equals(viewer.config().getWriteModeEnabled()) ? HomeHtml.plural(writes.rowsAffected(), "row") + " affected" : "write mode is off", null));
}
if(tiles.isEmpty())
{
return (new RawHTML(null, HomeHtml.emptyState("You can't read the notebook or execution tables, so there is nothing to summarize.")));
}
String scope = viewer.allAccess()
? "You are a workbench admin: these counts include every user's notebooks and runs."
: "These counts include your own runs and the notebooks you can open (yours, and those shared with you).";
List<String[]> actions = new ArrayList<>();
actions.add(new String[] {"Open the Runner", runnerUrl(viewer, null)});
actions.add(new String[] {"Notebooks", "/app/" + notebooks});
actions.add(new String[] {"Execution Audit", "/app/" + executions});
return (new RawHTML(null, HomeHtml.tiles(tiles) + HomeHtml.actions(actions) + HomeHtml.note(scope)));
}
/*******************************************************************************
** Runs, failures and committed writes per day over the last DAYS days.
*******************************************************************************/
static QWidgetData runsPerDay(Viewer viewer, RenderWidgetInput input) throws QException
{
String executions = viewer.table(SqlWorkbenchExecution.TABLE_NAME);
if(!canRead(input, executions))
{
return (noAccess("the execution audit"));
}
Map<LocalDate, long[]> perDay = new TreeMap<>();
for(LocalDate day = viewer.today().minusDays(DAYS - 1L); !day.isAfter(viewer.today()); day = day.plusDays(1))
{
perDay.put(day, new long[3]);
}
List<QRecord> runs = query(executions, since(viewer.windowStart()), SCAN_LIMIT, Set.of("id", "startTime", "status", "mode"));
long total = 0;
for(QRecord run : runs)
{
Instant start = run.getValueInstant("startTime");
long[] day = start == null ? null : perDay.get(start.atZone(viewer.zoneId()).toLocalDate());
if(day != null)
{
total++;
day[0]++;
day[1] += FAILED_STATUSES.contains(run.getValueString("status")) ? 1 : 0;
day[2] += ExecutionMode.WRITE_COMMIT.name().equals(run.getValueString("mode")) ? 1 : 0;
}
}
List<String> labels = new ArrayList<>();
List<Number> all = new ArrayList<>();
List<Number> failed = new ArrayList<>();
List<Number> writes = new ArrayList<>();
perDay.forEach((day, counts) ->
{
labels.add(DAY.format(day));
all.add(counts[0]);
failed.add(counts[1]);
writes.add(counts[2]);
});
ChartData chart = new ChartData().withChartData(new ChartData.Data()
.withLabels(labels)
.withDatasets(List.of(
new ChartData.Data.Dataset().withLabel("Runs").withData(all).withColor("#0062FF"),
new ChartData.Data.Dataset().withLabel("Failed").withData(failed).withColor(HomeHtml.BAD_COLOR),
new ChartData.Data.Dataset().withLabel("Writes").withData(writes).withColor(HomeHtml.WARN_COLOR))));
String sublabel = total == 0 ? "No runs in the last " + DAYS + " days" : HomeHtml.plural(total, "run") + " in the last " + DAYS + " days, by day";
if(runs.size() >= SCAN_LIMIT)
{
sublabel += " (the latest " + HomeHtml.number(SCAN_LIMIT) + " runs)";
}
chart.setSublabel(sublabel);
return (chart);
}
/*******************************************************************************
** The notebooks changed most recently.
*******************************************************************************/
static QWidgetData recentNotebooks(Viewer viewer, RenderWidgetInput input) throws QException
{
String notebooks = viewer.table(SqlWorkbenchNotebook.TABLE_NAME);
if(!canRead(input, notebooks))
{
return (noAccess("notebooks"));
}
List<QRecord> recent = query(notebooks, new QQueryFilter().withOrderBy(new QFilterOrderBy("modifyDate", false)).withOrderBy(new QFilterOrderBy("id", false)), ROWS,
Set.of("id", "title", "description", "ownerUserId", "ownerName", "modifyDate"));
Map<Integer, String> sharing = sharing(viewer, input, recent);
List<Map<String, Object>> rows = new ArrayList<>();
for(QRecord notebook : recent)
{
String shared = sharing.getOrDefault(notebook.getValueInteger("id"), "personal");
Map<String, Object> row = new HashMap<>();
row.put("notebook", notebookCard(viewer, notebook, HomeHtml.escape(firstLine(notebook.getValueString("description"), 120))));
row.put("owner", HomeHtml.escape(ownerLabel(viewer, notebook)));
row.put("shared", HomeHtml.badge(shared, "personal".equals(shared) ? HomeHtml.OFF_COLOR : HomeHtml.INFO_COLOR));
row.put("modified", HomeHtml.escape(HomeHtml.when(notebook.getValueInstant("modifyDate"), viewer.now(), viewer.zoneId())));
rows.add(row);
}
TableData table = table(List.of(column("Notebook", "notebook"), column("Owner", "owner"), column("Sharing", "shared"), column("Modified", "modified")), rows,
"No notebooks yet. Create one in the Runner.");
return (table.withLinkText("All notebooks").withLinkURL("/app/" + notebooks));
}
/*******************************************************************************
** The latest runs.
*******************************************************************************/
static QWidgetData recentRuns(Viewer viewer, RenderWidgetInput input) throws QException
{
String executions = viewer.table(SqlWorkbenchExecution.TABLE_NAME);
if(!canRead(input, executions))
{
return (noAccess("the execution audit"));
}
List<QRecord> runs = query(executions, new QQueryFilter().withOrderBy(new QFilterOrderBy("startTime", false)).withOrderBy(new QFilterOrderBy("id", false)), ROWS, null);
Map<Integer, String> names = notebookTitles(viewer, input, runs);
List<Map<String, Object>> rows = new ArrayList<>();
for(QRecord run : runs)
{
Map<String, Object> row = runRow(viewer, run, names);
row.put("result", HomeHtml.card(statusBadge(run), HomeHtml.escape(HomeHtml.duration(run.getValueInteger("durationMs"))), HomeHtml.escape(rowsText(run))));
rows.add(row);
}
TableData table = table(List.of(column("When", "when"), column("Who", "who"), column("Query", "query"), column("Result", "result")), rows,
"No runs yet. Queries run in the Runner are recorded here.");
return (table.withLinkText("All executions").withLinkURL("/app/" + executions));
}
/*******************************************************************************
** The slowest runs of the last DAYS days.
*******************************************************************************/
static QWidgetData slowest(Viewer viewer, RenderWidgetInput input) throws QException
{
String executions = viewer.table(SqlWorkbenchExecution.TABLE_NAME);
if(!canRead(input, executions))
{
return (noAccess("the execution audit"));
}
QQueryFilter filter = since(viewer.windowStart())
.withCriteria(new QFilterCriteria("durationMs", QCriteriaOperator.IS_NOT_BLANK))
.withCriteria(new QFilterCriteria("mode", QCriteriaOperator.NOT_EQUALS, ExecutionMode.WRITE_PREVIEW.name()))
.withOrderBy(new QFilterOrderBy("durationMs", false));
List<QRecord> runs = query(executions, filter, ROWS, null);
Map<Integer, String> names = notebookTitles(viewer, input, runs);
List<Map<String, Object>> rows = new ArrayList<>();
for(QRecord run : runs)
{
Map<String, Object> row = runRow(viewer, run, names);
row.put("duration", HomeHtml.card("<strong>" + HomeHtml.escape(HomeHtml.duration(run.getValueInteger("durationMs"))) + "</strong>", statusBadge(run)));
rows.add(row);
}
TableData table = table(List.of(column("Duration", "duration"), column("Query", "query"), column("Who", "who"), column("When", "when")), rows,
"No completed runs in the last " + DAYS + " days.");
return (table.withLinkText("All executions").withLinkURL("/app/" + executions));
}
/*******************************************************************************
** Failed, timed out, rejected and rolled-back runs of the last DAYS days.
*******************************************************************************/
static QWidgetData failed(Viewer viewer, RenderWidgetInput input) throws QException
{
String executions = viewer.table(SqlWorkbenchExecution.TABLE_NAME);
if(!canRead(input, executions))
{
return (noAccess("the execution audit"));
}
QQueryFilter filter = since(viewer.windowStart())
.withCriteria(new QFilterCriteria("status", QCriteriaOperator.IN, FAILED_STATUSES))
.withOrderBy(new QFilterOrderBy("startTime", false)).withOrderBy(new QFilterOrderBy("id", false));
List<QRecord> runs = query(executions, filter, ROWS, null);
Map<Integer, String> names = notebookTitles(viewer, input, runs);
List<Map<String, Object>> rows = new ArrayList<>();
for(QRecord run : runs)
{
Map<String, Object> row = runRow(viewer, run, names);
row.put("error", HomeHtml.card(statusBadge(run), HomeHtml.escape(firstLine(run.getValueString("errorMessage"), ERROR_CHARS))));
rows.add(row);
}
TableData table = table(List.of(column("When", "when"), column("Query", "query"), column("Error", "error"), column("Who", "who")), rows,
"No failed runs in the last " + DAYS + " days.");
return (table.withLinkText("All executions").withLinkURL("/app/" + executions));
}
/*******************************************************************************
** Notebooks with the most runs in the last DAYS days.
*******************************************************************************/
static QWidgetData mostUsed(Viewer viewer, RenderWidgetInput input) throws QException
{
String executions = viewer.table(SqlWorkbenchExecution.TABLE_NAME);
if(!canRead(input, executions))
{
return (noAccess("the execution audit"));
}
Aggregate runs = new Aggregate("id", AggregateOperator.COUNT);
Aggregate users = new Aggregate("userId", AggregateOperator.COUNT_DISTINCT);
Aggregate lastRun = new Aggregate("startTime", AggregateOperator.MAX);
GroupBy notebook = new GroupBy(QFieldType.INTEGER, "notebookId");
List<AggregateResult> results = new AggregateAction().execute(new AggregateInput(executions)
.withFilter(since(viewer.windowStart()).withCriteria(new QFilterCriteria("notebookId", QCriteriaOperator.IS_NOT_BLANK)))
.withAggregate(runs).withAggregate(users).withAggregate(lastRun).withGroupBy(notebook))
.getResults();
results.sort(Comparator.comparing((AggregateResult r) -> ValueUtils.getValueAsLong(r.getAggregateValue(runs))).reversed());
List<AggregateResult> top = results.subList(0, Math.min(ROWS, results.size()));
Set<Integer> ids = new LinkedHashSet<>();
top.forEach(r -> ids.add(ValueUtils.getValueAsInteger(r.getGroupByValue(notebook))));
Map<Integer, String> names = notebookTitles(viewer, input, ids);
List<Map<String, Object>> rows = new ArrayList<>();
for(AggregateResult result : top)
{
Integer id = ValueUtils.getValueAsInteger(result.getGroupByValue(notebook));
Map<String, Object> row = new HashMap<>();
row.put("notebook", notebookLink(viewer, id, names.get(id)));
row.put("runs", HomeHtml.number(ValueUtils.getValueAsLong(result.getAggregateValue(runs))));
row.put("users", HomeHtml.number(ValueUtils.getValueAsLong(result.getAggregateValue(users))));
row.put("lastRun", HomeHtml.escape(HomeHtml.when(ValueUtils.getValueAsInstant(result.getAggregateValue(lastRun)), viewer.now(), viewer.zoneId())));
rows.add(row);
}
return (table(List.of(column("Notebook", "notebook"), column("Runs", "runs", "right"), column("Users", "users", "right"), column("Last Run", "lastRun")), rows,
"No notebook runs in the last " + DAYS + " days."));
}
/*******************************************************************************
** Notebooks shared with users, groups or everyone (the shared library),
** most recently shared first.
*******************************************************************************/
static QWidgetData shared(Viewer viewer, RenderWidgetInput input) throws QException
{
String shares = viewer.table(SqlWorkbenchNotebookShare.TABLE_NAME);
if(!canRead(input, shares))
{
return (noAccess("notebook shares"));
}
Map<Integer, List<QRecord>> byNotebook = new LinkedHashMap<>();
for(QRecord share : query(shares, new QQueryFilter().withOrderBy(new QFilterOrderBy("id", false)), SHARE_LIMIT, Set.of("id", "notebookId", "audienceType", "audienceId", "createDate")))
{
Integer notebookId = share.getValueInteger("notebookId");
if(notebookId != null && (byNotebook.containsKey(notebookId) || byNotebook.size() < ROWS))
{
byNotebook.computeIfAbsent(notebookId, k -> new ArrayList<>()).add(share);
}
}
Map<Integer, QRecord> notebooks = notebooks(viewer, input, byNotebook.keySet());
List<Map<String, Object>> rows = new ArrayList<>();
byNotebook.forEach((id, list) ->
{
List<String> audiences = new ArrayList<>();
for(QRecord share : list)
{
audiences.add(audienceLabel(share));
}
QRecord notebook = notebooks.get(id);
Map<String, Object> row = new HashMap<>();
row.put("notebook", notebook == null ? notebookLink(viewer, id, null) : notebookCard(viewer, notebook));
row.put("owner", HomeHtml.escape(notebook == null ? "" : ownerLabel(viewer, notebook)));
row.put("audience", HomeHtml.escape(String.join(", ", audiences)));
row.put("since", HomeHtml.escape(HomeHtml.when(list.get(list.size() - 1).getValueInstant("createDate"), viewer.now(), viewer.zoneId())));
rows.add(row);
});
return (table(List.of(column("Notebook", "notebook"), column("Owner", "owner"), column("Shared With", "audience"), column("Shared Since", "since")), rows,
"No shared notebooks. Share one from its record (Share Notebook) or in the Runner."));
}
/*******************************************************************************
** Committed writes (gated write mode): who ran what, when, rows affected.
*******************************************************************************/
static QWidgetData writes(Viewer viewer, RenderWidgetInput input) throws QException
{
String executions = viewer.table(SqlWorkbenchExecution.TABLE_NAME);
if(!canRead(input, executions))
{
return (noAccess("the execution audit"));
}
QQueryFilter filter = new QQueryFilter(new QFilterCriteria("mode", QCriteriaOperator.EQUALS, ExecutionMode.WRITE_COMMIT.name()))
.withOrderBy(new QFilterOrderBy("startTime", false)).withOrderBy(new QFilterOrderBy("id", false));
List<QRecord> runs = query(executions, filter, ROWS, null);
Map<Integer, String> names = notebookTitles(viewer, input, runs);
List<Map<String, Object>> rows = new ArrayList<>();
for(QRecord run : runs)
{
Map<String, Object> row = runRow(viewer, run, names);
Integer affected = run.getValueInteger("rowsAffected");
row.put("affected", HomeHtml.card("<strong>" + HomeHtml.escape(affected == null ? "-" : HomeHtml.number(affected)) + "</strong>", statusBadge(run)));
rows.add(row);
}
String writeMode = Boolean.TRUE.equals(viewer.config().getWriteModeEnabled())
? "Write mode is on: users with the write permission can preview a write, then commit it within the confirmation window. Every preview and commit is audited."
: "Write mode is off on this server, so no new writes can be committed.";
TableData table = table(List.of(column("When", "when"), column("Who", "who"), column("Query", "query"), column("Rows Affected", "affected")), rows,
"No writes have been committed.");
table.withFooterHTML(HomeHtml.note(writeMode));
return (table.withLinkText("All executions").withLinkURL("/app/" + executions));
}
/*******************************************************************************
** Per-user activity over the last DAYS days (the widget is limited to
** workbench admins; the locks would show anyone else only themselves).
*******************************************************************************/
static QWidgetData byUser(Viewer viewer, RenderWidgetInput input) throws QException
{
String executions = viewer.table(SqlWorkbenchExecution.TABLE_NAME);
if(!canRead(input, executions))
{
return (noAccess("the execution audit"));
}
Map<String, Object[]> users = new LinkedHashMap<>();
List<QRecord> runs = query(executions, since(viewer.windowStart()).withOrderBy(new QFilterOrderBy("startTime", false)), SCAN_LIMIT,
Set.of("id", "userId", "userName", "status", "mode", "startTime"));
for(QRecord run : runs)
{
String userId = run.getValueString("userId") == null ? "(unknown)" : run.getValueString("userId");
Object[] totals = users.computeIfAbsent(userId, k -> new Object[] {run.getValueString("userName"), 0L, 0L, 0L, run.getValueInstant("startTime")});
totals[1] = (Long) totals[1] + 1;
totals[2] = (Long) totals[2] + (FAILED_STATUSES.contains(run.getValueString("status")) ? 1 : 0);
totals[3] = (Long) totals[3] + (ExecutionMode.WRITE_COMMIT.name().equals(run.getValueString("mode")) ? 1 : 0);
}
List<Map.Entry<String, Object[]>> sorted = new ArrayList<>(users.entrySet());
sorted.sort(Comparator.comparing((Map.Entry<String, Object[]> e) -> (Long) e.getValue()[1]).reversed());
List<Map<String, Object>> rows = new ArrayList<>();
for(Map.Entry<String, Object[]> entry : sorted.subList(0, Math.min(USER_ROWS, sorted.size())))
{
Object[] totals = entry.getValue();
Map<String, Object> row = new HashMap<>();
String name = totals[0] == null ? entry.getKey() : String.valueOf(totals[0]);
row.put("user", HomeHtml.card(HomeHtml.escape(name), name.equals(entry.getKey()) ? null : HomeHtml.escape(entry.getKey()),
HomeHtml.escape("last run " + HomeHtml.when((Instant) totals[4], viewer.now(), viewer.zoneId()))));
row.put("runs", HomeHtml.number((Long) totals[1]));
row.put("failed", HomeHtml.number((Long) totals[2]));
row.put("writes", HomeHtml.number((Long) totals[3]));
rows.add(row);
}
TableData table = table(List.of(column("User", "user"), column("Runs", "runs", "right"), column("Failed", "failed", "right"), column("Writes", "writes", "right")),
rows, "No runs in the last " + DAYS + " days.");
if(!viewer.allAccess())
{
table.withFooterHTML(HomeHtml.note("Only your own runs are shown; workbench admins see every user's."));
}
return (table);
}
/*******************************************************************************
** On a notebook's record view: open it in the Runner.
*******************************************************************************/
static QWidgetData notebookRunner(Viewer viewer, RenderWidgetInput input)
{
Integer id = ValueUtils.getValueAsInteger(input.getQueryParams() == null ? null : input.getQueryParams().get("id"));
if(id == null)
{
return (new RawHTML(null, HomeHtml.emptyState("Save the notebook to open it in the Runner.")));
}
return (new RawHTML(null, HomeHtml.actions(List.<String[]>of(new String[] {"Open in Runner", runnerUrl(viewer, id)}))
+ HomeHtml.note("The Runner edits and runs this notebook's cells. Runs are recorded in the execution audit.")));
}
/*******************************************************************************
** The Runner's URL, opening a notebook when an id is given.
*******************************************************************************/
static String runnerUrl(Viewer viewer, Integer notebookId)
{
String url = "/app/" + viewer.config().applyPrefix(SqlWorkbenchRunnerAppMetaDataProducer.APP_NAME);
return (notebookId == null ? url : url + "?notebook=" + notebookId);
}
/*******************************************************************************
** Committed writes since an instant, and the rows they affected.
*******************************************************************************/
record Writes(long count, long rowsAffected)
{
}
/*******************************************************************************
**
*******************************************************************************/
private static Writes writesSince(String executions, Instant since) throws QException
{
Aggregate count = new Aggregate("id", AggregateOperator.COUNT);
Aggregate affected = new Aggregate("rowsAffected", AggregateOperator.SUM);
List<AggregateResult> results = new AggregateAction().execute(new AggregateInput(executions)
.withFilter(since(since).withCriteria(new QFilterCriteria("mode", QCriteriaOperator.EQUALS, ExecutionMode.WRITE_COMMIT.name())))
.withAggregate(count).withAggregate(affected)).getResults();
if(results.isEmpty())
{
return (new Writes(0, 0));
}
Long total = ValueUtils.getValueAsLong(results.get(0).getAggregateValue(count));
Long rows = ValueUtils.getValueAsLong(results.get(0).getAggregateValue(affected));
return (new Writes(total == null ? 0 : total, rows == null ? 0 : rows));
}
/*******************************************************************************
** The cells every run row shares: when, who, and the query (linked to its
** audit record, with its backend and notebook).
*******************************************************************************/
private static Map<String, Object> runRow(Viewer viewer, QRecord run, Map<Integer, String> notebookNames)
{
Map<String, Object> row = new HashMap<>();
Integer notebookId = run.getValueInteger("notebookId");
String recordUrl = "/app/" + viewer.table(SqlWorkbenchExecution.TABLE_NAME) + "/" + run.getValueInteger("id");
row.put("when", HomeHtml.escape(HomeHtml.when(run.getValueInstant("startTime"), viewer.now(), viewer.zoneId())));
row.put("who", HomeHtml.escape(userLabel(viewer, run.getValueString("userId"), run.getValueString("userName"))));
row.put("query", HomeHtml.card("<a href=\"" + HomeHtml.escape(recordUrl) + "\" style=\"color:inherit;text-decoration:none\">"
+ HomeHtml.code(run.getValueString("sqlText"), SQL_CHARS) + "</a>",
HomeHtml.escape(modeLabel(run.getValueString("mode"))),
HomeHtml.escape(run.getValueString("backendName")),
notebookId == null ? null : notebookLink(viewer, notebookId, notebookNames.get(notebookId))));
return (row);
}
/*******************************************************************************
** A notebook's title linked to its record, with facts (already HTML) and
** an "Open in Runner" link.
*******************************************************************************/
private static String notebookCard(Viewer viewer, QRecord notebook, String... facts)
{
Integer id = notebook.getValueInteger("id");
List<String> all = new ArrayList<>(List.of(facts));
all.add(HomeHtml.link(runnerUrl(viewer, id), "Open in Runner"));
return (HomeHtml.card(HomeHtml.link("/app/" + viewer.table(SqlWorkbenchNotebook.TABLE_NAME) + "/" + id, notebook.getValueString("title")), all.toArray(new String[0])));
}
/*******************************************************************************
** A notebook link by id: its title when the viewer can read it, otherwise
** says so (its runs stay visible in the audit after it is unshared or
** deleted).
*******************************************************************************/
private static String notebookLink(Viewer viewer, Integer id, String title)
{
if(title == null)
{
return (HomeHtml.muted("Notebook #" + id + " (deleted, or not visible to you)"));
}
return (HomeHtml.link("/app/" + viewer.table(SqlWorkbenchNotebook.TABLE_NAME) + "/" + id, title));
}
/*******************************************************************************
** "you", or the owner's name.
*******************************************************************************/
private static String ownerLabel(Viewer viewer, QRecord notebook)
{
return (userLabel(viewer, notebook.getValueString("ownerUserId"), notebook.getValueString("ownerName")));
}
/*******************************************************************************
** "you", the user's name, or their id.
*******************************************************************************/
private static String userLabel(Viewer viewer, String userId, String userName)
{
if(userId != null && userId.equals(viewer.userId()))
{
return ("you");
}
return (userName != null && !userName.isBlank() ? userName : (userId == null ? "unknown user" : userId));
}
/*******************************************************************************
** Plain label of an execution mode.
*******************************************************************************/
static String modeLabel(String mode)
{
if(mode == null)
{
return ("");
}
return (switch(mode)
{
case "READ_ONLY" -> "read-only";
case "WRITE_PREVIEW" -> "write preview";
case "WRITE_COMMIT" -> "write";
case "EXPLAIN" -> "explain";
case "EXPLAIN_ANALYZE" -> "explain analyze";
default -> mode.toLowerCase(Locale.ROOT).replace('_', ' ');
});
}
/*******************************************************************************
** A run's status as a colored badge.
*******************************************************************************/
static String statusBadge(QRecord run)
{
String status = run.getValueString("status");
if(status == null)
{
return ("");
}
String color = switch(status)
{
case "SUCCEEDED" -> HomeHtml.OK_COLOR;
case "RUNNING" -> HomeHtml.INFO_COLOR;
case "CANCELLED" -> HomeHtml.OFF_COLOR;
default -> HomeHtml.BAD_COLOR;
};
return (HomeHtml.badge(status.toLowerCase(Locale.ROOT).replace('_', ' '), color));
}
/*******************************************************************************
** "12 rows" returned, or affected for a write.
*******************************************************************************/
private static String rowsText(QRecord run)
{
Integer affected = run.getValueInteger("rowsAffected");
if(affected != null)
{
return (HomeHtml.plural(affected, "row") + " affected");
}
Integer returned = run.getValueInteger("rowsReturned");
return (returned == null ? "" : HomeHtml.plural(returned, "row"));
}
/*******************************************************************************
** How each notebook is shared, from the share rows the viewer can see:
** "everyone" (the shared library), "shared" (users or groups); absent
** when it has no visible shares.
*******************************************************************************/
private static Map<Integer, String> sharing(Viewer viewer, RenderWidgetInput input, List<QRecord> notebooks) throws QException
{
Map<Integer, String> sharing = new HashMap<>();
String shares = viewer.table(SqlWorkbenchNotebookShare.TABLE_NAME);
List<Serializable> ids = new ArrayList<>();
notebooks.forEach(n -> ids.add(n.getValueInteger("id")));
if(ids.isEmpty() || !canRead(input, shares))
{
return (sharing);
}
for(QRecord share : query(shares, new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.IN, ids)), SHARE_LIMIT, Set.of("id", "notebookId", "audienceType")))
{
boolean everyone = ShareAudienceType.ALL.name().equals(share.getValueString("audienceType"));
sharing.merge(share.getValueInteger("notebookId"), everyone ? "everyone" : "shared", (a, b) -> "everyone".equals(a) ? a : b);
}
return (sharing);
}
/*******************************************************************************
** A share's audience in words.
*******************************************************************************/
private static String audienceLabel(QRecord share)
{
String type = share.getValueString("audienceType");
if(ShareAudienceType.ALL.name().equals(type))
{
return ("everyone");
}
String who = share.getValueString("audienceId");
return (ShareAudienceType.GROUP.name().equals(type) ? "group " + who : who);
}
/*******************************************************************************
** The first line of a text, cut at maxChars.
*******************************************************************************/
static String firstLine(String text, int maxChars)
{
if(text == null)
{
return (null);
}
String line = text.strip().lines().findFirst().orElse("");
return (line.length() > maxChars ? line.substring(0, maxChars - 1) + "…" : line);
}
/*******************************************************************************
** Titles of the notebooks the runs belong to, as far as the viewer can read
** them.
*******************************************************************************/
private static Map<Integer, String> notebookTitles(Viewer viewer, RenderWidgetInput input, List<QRecord> runs) throws QException
{
Set<Integer> ids = new LinkedHashSet<>();
for(QRecord run : runs)
{
if(run.getValueInteger("notebookId") != null)
{
ids.add(run.getValueInteger("notebookId"));
}
}
return (notebookTitles(viewer, input, ids));
}
/*******************************************************************************
**
*******************************************************************************/
private static Map<Integer, String> notebookTitles(Viewer viewer, RenderWidgetInput input, Set<Integer> ids) throws QException
{
Map<Integer, String> titles = new HashMap<>();
notebooks(viewer, input, ids).forEach((id, notebook) -> titles.put(id, notebook.getValueString("title")));
return (titles);
}
/*******************************************************************************
** The readable notebooks among some ids (through the notebook locks).
*******************************************************************************/
private static Map<Integer, QRecord> notebooks(Viewer viewer, RenderWidgetInput input, Set<Integer> ids) throws QException
{
Map<Integer, QRecord> notebooks = new HashMap<>();
String table = viewer.table(SqlWorkbenchNotebook.TABLE_NAME);
if(ids.isEmpty() || !canRead(input, table))
{
return (notebooks);
}
for(QRecord notebook : query(table, new QQueryFilter(new QFilterCriteria("id", QCriteriaOperator.IN, new ArrayList<Serializable>(ids))), ids.size(),
Set.of("id", "title", "ownerUserId", "ownerName")))
{
notebooks.put(notebook.getValueInteger("id"), notebook);
}
return (notebooks);
}
/*******************************************************************************
** Whether the session may read a table.
*******************************************************************************/
static boolean canRead(RenderWidgetInput input, String tableName)
{
return (QContext.getQInstance().getTable(tableName) != null && PermissionsHelper.hasTablePermission(input, tableName, TablePermissionSubType.READ));
}
/*******************************************************************************
** The "you can't read this" state.
*******************************************************************************/
private static QWidgetData noAccess(String what)
{
return (new RawHTML(null, HomeHtml.emptyState("You don't have permission to read " + what + ".")));
}
/*******************************************************************************
** A filter on runs started at or after an instant.
*******************************************************************************/
private static QQueryFilter since(Instant since)
{
return (new QQueryFilter(new QFilterCriteria("startTime", QCriteriaOperator.GREATER_THAN_OR_EQUALS, since)));
}
/*******************************************************************************
** Count through the table's locks.
*******************************************************************************/
private static long count(String tableName, QQueryFilter filter) throws QException
{
Integer count = new CountAction().execute(new CountInput(tableName).withFilter(filter)).getCount();
return (count == null ? 0 : count);
}
/*******************************************************************************
** Query through the table's locks.
*******************************************************************************/
private static List<QRecord> query(String tableName, QQueryFilter filter, int limit, Set<String> fields) throws QException
{
QueryInput queryInput = new QueryInput(tableName).withFilter(filter.withLimit(limit));
if(fields != null)
{
queryInput.withFieldNamesToInclude(fields);
}
return (new QueryAction().execute(queryInput).getRecords());
}
/*******************************************************************************
** A table column of HTML cells, top-aligned.
*******************************************************************************/
private static TableData.Column column(String header, String accessor)
{
return (column(header, accessor, "left"));
}
/*******************************************************************************
**
*******************************************************************************/
private static TableData.Column column(String header, String accessor, String align)
{
return (new TableData.Column("html", header, accessor, null, align).withVerticalAlign("top"));
}
/*******************************************************************************
** A table without pagination controls, with an honest empty state.
*******************************************************************************/
private static TableData table(List<TableData.Column> columns, List<Map<String, Object>> rows, String empty)
{
return (new TableData(null, columns, rows)
.withRowsPerPage(Math.max(ROWS, rows.size()))
.withHidePaginationDropdown(true)
.withNoRowsFoundHTML(HomeHtml.emptyState(empty)));
}
}
@@ -0,0 +1,60 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.home;
import java.io.Serializable;
import java.util.Map;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchRuntime;
import com.kingsrook.qqq.backend.core.actions.dashboard.widgets.AbstractWidgetRenderer;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetOutput;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaData;
/*******************************************************************************
** Renders the SQL Workbench home widgets (and the notebook's "Open in
** Runner" widget). Which one is in the widget's "kind" default value; the
** data is read in the viewer's session, so permissions and record locks
** apply (see SqlWorkbenchHome). The widgets' own permission rules (the
** middleware checks them, and the instance metadata hides denied ones)
** decide who gets each widget at all.
*******************************************************************************/
public class SqlWorkbenchHomeWidgetRenderer extends AbstractWidgetRenderer
{
public static final String KIND = "kind";
/*******************************************************************************
**
*******************************************************************************/
@Override
public RenderWidgetOutput render(RenderWidgetInput input) throws QException
{
SqlWorkbenchRuntime runtime = SqlWorkbenchRuntime.get();
String kind = null;
if(input.getWidgetMetaData() instanceof QWidgetMetaData widget)
{
Map<String, Serializable> defaults = widget.getDefaultValues();
kind = defaults == null || defaults.get(KIND) == null ? null : String.valueOf(defaults.get(KIND));
}
SqlWorkbenchHome.Viewer viewer = SqlWorkbenchHome.viewer(runtime.getConfig(), runtime.getClock().instant());
return (new RenderWidgetOutput(SqlWorkbenchHome.load(kind, viewer, input)));
}
}
@@ -17,6 +17,7 @@
package com.kingsrook.qbits.sqlworkbench.metadata;
import java.util.ArrayList;
import java.util.List;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.metadata.MetaDataProducerMultiOutput;
@@ -32,16 +33,30 @@ import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
/*******************************************************************************
** The sqlWorkbench app: the workbench widget (rendered by the Next UI as the
** notebook UI), plus the notebook and execution-audit tables. Gated by the
** access permission, so the app - and so the feature - is only advertised
** to users who have it. No processes (they would only add left-nav noise).
** The sqlWorkbench app. Its home page is an information dashboard (see
** SqlWorkbenchHomeWidgetsMetaDataProducer); its nav lists:
**
** - Runner: the interactive notebook editor (the SQL_WORKBENCH widget), a
** child app of its own;
** - the main tables people use directly: Notebooks and the Execution
** audit.
**
** The sub-tables (cells, versions, pinned results, shares) are not in the
** nav: they are child lists and relations on a notebook's record view, and
** stay standard QQQ tables reachable by URL and the table API. Gated by the
** access permission; each table also needs its own read permission to
** appear. No processes in the nav (record actions live on the tables).
** Hosts can re-parent it (e.g. under an Admin app); its name stays the same.
*******************************************************************************/
public class SqlWorkbenchAppMetaDataProducer extends QBitComponentMetaDataProducer<QAppMetaData, SqlWorkbenchQBitConfig>
{
public static final String APP_NAME = "sqlWorkbench";
/////////////////////////////////////////////////////////////////////////
// the main tables, in nav order; the rest are reached from a notebook //
/////////////////////////////////////////////////////////////////////////
public static final List<String> MAIN_TABLES = List.of(SqlWorkbenchNotebook.TABLE_NAME, SqlWorkbenchExecution.TABLE_NAME);
/*******************************************************************************
@@ -57,12 +72,17 @@ public class SqlWorkbenchAppMetaDataProducer extends QBitComponentMetaDataProduc
.withLabel("SQL Workbench")
.withIcon(new QIcon("terminal"))
.withPermissionRules(config.getAccessPermissionRules())
.withWidgets(List.of(config.applyPrefix(SqlWorkbenchWidgetMetaDataProducer.NAME)));
.withWidgets(new ArrayList<>(SqlWorkbenchHomeWidgetsMetaDataProducer.homeWidgetNames().stream().map(config::applyPrefix).toList()));
MetaDataProducerMultiOutput produced = QBitProductionContext.peekMetaDataProducerMultiOutput();
if(produced != null)
{
for(String tableName : new String[] { SqlWorkbenchNotebook.TABLE_NAME, SqlWorkbenchExecution.TABLE_NAME })
QAppMetaData runner = produced.get(QAppMetaData.class, config.applyPrefix(SqlWorkbenchRunnerAppMetaDataProducer.APP_NAME));
if(runner != null)
{
app.withChild(runner);
}
for(String tableName : MAIN_TABLES)
{
QTableMetaData table = produced.get(QTableMetaData.class, config.applyPrefix(tableName));
if(table != null)
@@ -77,7 +97,7 @@ public class SqlWorkbenchAppMetaDataProducer extends QBitComponentMetaDataProduc
/*******************************************************************************
** last, so the tables and widget exist in the output
** last, so the tables, widgets and Runner app exist in the output
*******************************************************************************/
@Override
public int getSortOrder()
@@ -25,7 +25,7 @@ import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
/*******************************************************************************
** Produces the sqlWorkbenchCell table.
** Produces the sql_workbench_cell table.
*******************************************************************************/
public class SqlWorkbenchCellTableMetaDataProducer extends QBitComponentMetaDataProducer<QTableMetaData, SqlWorkbenchQBitConfig>
{
@@ -0,0 +1,47 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.metadata;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanelsMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReference;
import com.kingsrook.qqq.backend.core.model.metadata.qbits.QBitComponentMetaDataProducer;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.developer.SqlWorkbenchDeveloperPanelProvider;
/*******************************************************************************
** Registers the "SQL Workbench" record developer panel
** ({@link SqlWorkbenchDeveloperPanelProvider}) in the instance's developer
** panels (idempotent). The provider decides per table and user whether it
** shows.
*******************************************************************************/
public class SqlWorkbenchDeveloperPanelsMetaDataProducer extends QBitComponentMetaDataProducer<DeveloperPanelsMetaData, SqlWorkbenchQBitConfig>
{
/*******************************************************************************
**
*******************************************************************************/
@Override
public DeveloperPanelsMetaData produce(QInstance qInstance) throws QException
{
return (DeveloperPanelsMetaData.ofOrWithNew(qInstance).withProvider(new QCodeReference(SqlWorkbenchDeveloperPanelProvider.class)));
}
}
@@ -25,7 +25,7 @@ import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
/*******************************************************************************
** Produces the sqlWorkbenchExecution (audit) table.
** Produces the sql_workbench_execution (audit) table.
*******************************************************************************/
public class SqlWorkbenchExecutionTableMetaDataProducer extends QBitComponentMetaDataProducer<QTableMetaData, SqlWorkbenchQBitConfig>
{
@@ -0,0 +1,130 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.metadata;
import java.util.ArrayList;
import java.util.List;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.home.SqlWorkbenchHome;
import com.kingsrook.qbits.sqlworkbench.home.SqlWorkbenchHomeWidgetRenderer;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.WidgetType;
import com.kingsrook.qqq.backend.core.model.metadata.MetaDataProducerMultiOutput;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReference;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.permissions.QPermissionRules;
import com.kingsrook.qqq.backend.core.model.metadata.qbits.QBitComponentMetaDataProducer;
/*******************************************************************************
** The sqlWorkbench app's home dashboard, in page order, all full width:
** summary tiles and links, runs per day, recent notebooks, recent runs,
** slowest and failed queries, most-used notebooks, shared notebooks,
** committed writes, and (workbench admins only) activity by user. Plus
** the "Open in Runner" widget on a notebook's record view.
**
** The home owns this data; other dashboards should link here rather than
** repeat it.
*******************************************************************************/
public class SqlWorkbenchHomeWidgetsMetaDataProducer extends QBitComponentMetaDataProducer<MetaDataProducerMultiOutput, SqlWorkbenchQBitConfig>
{
public static final String SUMMARY = "sqlWorkbenchHomeSummary";
public static final String RUNS_PER_DAY = "sqlWorkbenchHomeRunsPerDay";
public static final String RECENT_NOTEBOOKS = "sqlWorkbenchHomeRecentNotebooks";
public static final String RECENT_RUNS = "sqlWorkbenchHomeRecentRuns";
public static final String SLOWEST = "sqlWorkbenchHomeSlowestQueries";
public static final String FAILED = "sqlWorkbenchHomeFailedQueries";
public static final String MOST_USED = "sqlWorkbenchHomeMostUsedNotebooks";
public static final String SHARED = "sqlWorkbenchHomeSharedNotebooks";
public static final String WRITES = "sqlWorkbenchHomeWrites";
public static final String BY_USER = "sqlWorkbenchHomeActivityByUser";
public static final String NOTEBOOK_RUNNER = "sqlWorkbenchNotebookOpenInRunner";
private static final String HTML = WidgetType.HTML.getType();
private static final String TABLE = WidgetType.TABLE.getType();
/*******************************************************************************
** The home widget names, in page order (unprefixed).
*******************************************************************************/
public static List<String> homeWidgetNames()
{
return (List.of(SUMMARY, RUNS_PER_DAY, RECENT_NOTEBOOKS, RECENT_RUNS, SLOWEST, FAILED, MOST_USED, SHARED, WRITES, BY_USER));
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public MetaDataProducerMultiOutput produce(QInstance qInstance) throws QException
{
SqlWorkbenchQBitConfig config = getQBitConfig();
QPermissionRules access = config.getAccessPermissionRules();
List<QWidgetMetaData> widgets = new ArrayList<>();
widgets.add(widget(config, SUMMARY, "SQL Workbench", HTML, SqlWorkbenchHome.KIND_SUMMARY, access));
widgets.add(widget(config, RUNS_PER_DAY, "Runs per Day", WidgetType.LINE_CHART.getType(), SqlWorkbenchHome.KIND_RUNS_PER_DAY, access));
widgets.add(widget(config, RECENT_NOTEBOOKS, "Recent Notebooks", TABLE, SqlWorkbenchHome.KIND_RECENT_NOTEBOOKS, access));
widgets.add(widget(config, RECENT_RUNS, "Recent Runs", TABLE, SqlWorkbenchHome.KIND_RECENT_RUNS, access));
widgets.add(widget(config, SLOWEST, "Slowest Queries, 30 Days", TABLE, SqlWorkbenchHome.KIND_SLOWEST, access));
widgets.add(widget(config, FAILED, "Failed Queries, 30 Days", TABLE, SqlWorkbenchHome.KIND_FAILED, access));
widgets.add(widget(config, MOST_USED, "Most-Used Notebooks, 30 Days", TABLE, SqlWorkbenchHome.KIND_MOST_USED, access));
widgets.add(widget(config, SHARED, "Shared Notebooks", TABLE, SqlWorkbenchHome.KIND_SHARED, access));
widgets.add(widget(config, WRITES, "Writes", TABLE, SqlWorkbenchHome.KIND_WRITES, access));
widgets.add(widget(config, BY_USER, "Activity by User, 30 Days", TABLE, SqlWorkbenchHome.KIND_BY_USER, config.getAdminPermissionRules()));
widgets.add(widget(config, NOTEBOOK_RUNNER, "Open in Runner", HTML, SqlWorkbenchHome.KIND_NOTEBOOK_RUNNER, access).withIsCard(false).withShowReloadButton(false));
MetaDataProducerMultiOutput output = new MetaDataProducerMultiOutput();
widgets.forEach(output::add);
return (output);
}
/*******************************************************************************
** One full-width widget rendered by SqlWorkbenchHomeWidgetRenderer.
*******************************************************************************/
private static QWidgetMetaData widget(SqlWorkbenchQBitConfig config, String name, String label, String type, String kind, QPermissionRules permissionRules)
{
return (new QWidgetMetaData()
.withName(config.applyPrefix(name))
.withLabel(label)
.withType(type)
.withGridColumns(12)
.withIsCard(true)
.withShowReloadButton(true)
.withCodeReference(new QCodeReference(SqlWorkbenchHomeWidgetRenderer.class))
.withDefaultValue(SqlWorkbenchHomeWidgetRenderer.KIND, kind)
.withPermissionRules(permissionRules));
}
/*******************************************************************************
** with the workbench widget, before the apps
*******************************************************************************/
@Override
public int getSortOrder()
{
return (310);
}
}
@@ -0,0 +1,53 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.metadata;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.qbits.QBitComponentMetaDataProducer;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
/*******************************************************************************
** Produces the sql_workbench_notebook_share table.
*******************************************************************************/
public class SqlWorkbenchNotebookShareTableMetaDataProducer extends QBitComponentMetaDataProducer<QTableMetaData, SqlWorkbenchQBitConfig>
{
/*******************************************************************************
**
*******************************************************************************/
@Override
public QTableMetaData produce(QInstance qInstance) throws QException
{
return (SqlWorkbenchTableMetaDataHelper.buildNotebookShareTable(getQBitConfig()));
}
/*******************************************************************************
** tables first, then the PVS, widget and app
*******************************************************************************/
@Override
public int getSortOrder()
{
return (100);
}
}
@@ -25,7 +25,7 @@ import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
/*******************************************************************************
** Produces the sqlWorkbenchNotebook table.
** Produces the sql_workbench_notebook table.
*******************************************************************************/
public class SqlWorkbenchNotebookTableMetaDataProducer extends QBitComponentMetaDataProducer<QTableMetaData, SqlWorkbenchQBitConfig>
{
@@ -25,7 +25,7 @@ import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
/*******************************************************************************
** Produces the sqlWorkbenchNotebookVersion table.
** Produces the sql_workbench_notebook_version table.
*******************************************************************************/
public class SqlWorkbenchNotebookVersionTableMetaDataProducer extends QBitComponentMetaDataProducer<QTableMetaData, SqlWorkbenchQBitConfig>
{
@@ -0,0 +1,139 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.metadata;
import java.util.ArrayList;
import java.util.List;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchPermissionChecker;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.model.ShareAudienceType;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookVersion;
import com.kingsrook.qbits.sqlworkbench.processes.SqlWorkbenchRecordActionSteps;
import com.kingsrook.qqq.backend.core.actions.permissions.TablePermissionSubType;
import com.kingsrook.qqq.backend.core.actions.processes.BackendStep;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.metadata.MetaDataProducerMultiOutput;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReference;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.layout.QIcon;
import com.kingsrook.qqq.backend.core.model.metadata.permissions.PermissionLevel;
import com.kingsrook.qqq.backend.core.model.metadata.permissions.QPermissionRules;
import com.kingsrook.qqq.backend.core.model.metadata.processes.QBackendStepMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.processes.QComponentType;
import com.kingsrook.qqq.backend.core.model.metadata.processes.QFrontendComponentMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.processes.QFrontendStepMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.processes.QFunctionInputMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.processes.QProcessMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.processes.QRecordListMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.processes.QStepMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.qbits.QBitComponentMetaDataProducer;
/*******************************************************************************
** Record actions on the workbench tables (never nav entries):
** - Share Notebook (notebook): share with a user, a group, or everyone (the
** shared library); needs share-table insert permission.
** - Duplicate Notebook (notebook): a personal copy of a readable notebook;
** needs notebook insert permission.
** - Restore Version (notebook version): restore it as a new version of the
** (owned) notebook; needs notebook edit permission.
*******************************************************************************/
public class SqlWorkbenchRecordActionsMetaDataProducer extends QBitComponentMetaDataProducer<MetaDataProducerMultiOutput, SqlWorkbenchQBitConfig>
{
public static final String SHARE_NOTEBOOK = "sqlWorkbenchShareNotebook";
public static final String DUPLICATE_NOTEBOOK = "sqlWorkbenchDuplicateNotebook";
public static final String RESTORE_VERSION = "sqlWorkbenchRestoreNotebookVersion";
/*******************************************************************************
**
*******************************************************************************/
@Override
public MetaDataProducerMultiOutput produce(QInstance qInstance) throws QException
{
SqlWorkbenchQBitConfig config = getQBitConfig();
String notebook = config.applyPrefix(SqlWorkbenchNotebook.TABLE_NAME);
MetaDataProducerMultiOutput output = new MetaDataProducerMultiOutput();
output.add(recordAction(config.applyPrefix(SHARE_NOTEBOOK), "Share Notebook", "share", notebook,
config.applyPrefix(SqlWorkbenchNotebookShare.TABLE_NAME), TablePermissionSubType.INSERT, SqlWorkbenchRecordActionSteps.ShareNotebookStep.class,
List.of(new QFieldMetaData("audienceType", QFieldType.STRING).withLabel("Share With").withIsRequired(true).withPossibleValueSourceName(config.applyPrefix(ShareAudienceType.NAME)),
new QFieldMetaData("audienceId", QFieldType.STRING).withLabel("User or Group (blank for Everyone)"))));
output.add(recordAction(config.applyPrefix(DUPLICATE_NOTEBOOK), "Duplicate Notebook", "content_copy", notebook,
notebook, TablePermissionSubType.INSERT, SqlWorkbenchRecordActionSteps.DuplicateNotebookStep.class, List.of()));
output.add(recordAction(config.applyPrefix(RESTORE_VERSION), "Restore Version", "restore", config.applyPrefix(SqlWorkbenchNotebookVersion.TABLE_NAME),
notebook, TablePermissionSubType.EDIT, SqlWorkbenchRecordActionSteps.RestoreVersionStep.class, List.of()));
return (output);
}
/*******************************************************************************
** A one-record action: optional input form, the backend step (records
** loaded through the table's locks), and a result screen.
*******************************************************************************/
private static QProcessMetaData recordAction(String name, String label, String icon, String tableName, String permissionTable, TablePermissionSubType permission,
Class<? extends BackendStep> step, List<QFieldMetaData> formFields)
{
List<QStepMetaData> steps = new ArrayList<>();
if(!formFields.isEmpty())
{
steps.add(new QFrontendStepMetaData()
.withName("input")
.withComponent(new QFrontendComponentMetaData().withType(QComponentType.EDIT_FORM))
.withFormFields(formFields));
}
steps.add(new QBackendStepMetaData()
.withName("execute")
.withInputData(new QFunctionInputMetaData().withRecordListMetaData(new QRecordListMetaData().withTableName(tableName)))
.withCode(new QCodeReference(step)));
steps.add(new QFrontendStepMetaData()
.withName("result")
.withComponent(new QFrontendComponentMetaData().withType(QComponentType.VIEW_FORM))
.withViewField(new QFieldMetaData(SqlWorkbenchRecordActionSteps.RESULT_MESSAGE, QFieldType.STRING).withLabel("Result")));
return (new QProcessMetaData()
.withName(name)
.withLabel(label)
.withIcon(new QIcon(icon))
.withTableName(tableName)
.withMinInputRecords(1)
.withMaxInputRecords(1)
.withPermissionRules(new QPermissionRules()
.withLevel(PermissionLevel.HAS_ACCESS_PERMISSION)
.withCustomPermissionChecker(SqlWorkbenchPermissionChecker.requiring(permissionTable + "." + permission.getPermissionSuffix())))
.withStepList(steps));
}
/*******************************************************************************
** after the tables and their joins
*******************************************************************************/
@Override
public int getSortOrder()
{
return (160);
}
}
@@ -25,7 +25,7 @@ import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
/*******************************************************************************
** Produces the sqlWorkbenchResultSnapshot table.
** Produces the sql_workbench_result_snapshot table.
*******************************************************************************/
public class SqlWorkbenchResultSnapshotTableMetaDataProducer extends QBitComponentMetaDataProducer<QTableMetaData, SqlWorkbenchQBitConfig>
{
@@ -0,0 +1,68 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.metadata;
import java.util.List;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.layout.QAppMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.layout.QIcon;
import com.kingsrook.qqq.backend.core.model.metadata.qbits.QBitComponentMetaDataProducer;
/*******************************************************************************
** The Runner: a child app of the sqlWorkbench app whose page is the
** SQL_WORKBENCH widget (the interactive notebook editor the Next UI draws).
** It is a custom screen, so it has a nav entry; processes never do. Open a
** notebook with /app/sqlWorkbenchRunner?notebook=<id>. Same permission as
** the app.
*******************************************************************************/
public class SqlWorkbenchRunnerAppMetaDataProducer extends QBitComponentMetaDataProducer<QAppMetaData, SqlWorkbenchQBitConfig>
{
public static final String APP_NAME = "sqlWorkbenchRunner";
public static final String LABEL = "Runner";
/*******************************************************************************
**
*******************************************************************************/
@Override
public QAppMetaData produce(QInstance qInstance) throws QException
{
SqlWorkbenchQBitConfig config = getQBitConfig();
return (new QAppMetaData()
.withName(config.applyPrefix(APP_NAME))
.withLabel(LABEL)
.withIcon(new QIcon("play_circle"))
.withPermissionRules(config.getAccessPermissionRules())
.withWidgets(List.of(config.applyPrefix(SqlWorkbenchWidgetMetaDataProducer.NAME))));
}
/*******************************************************************************
** after the widget, before the sqlWorkbench app (its parent)
*******************************************************************************/
@Override
public int getSortOrder()
{
return (850);
}
}
@@ -0,0 +1,74 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.metadata;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchSecurity;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.metadata.MetaDataProducerMultiOutput;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaDataInterface;
import com.kingsrook.qqq.backend.core.model.metadata.joins.QJoinMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.qbits.QBitComponentMetaDataProducer;
import com.kingsrook.qqq.backend.core.model.metadata.security.QSecurityKeyType;
/*******************************************************************************
** Produces what the tables' locks and record views need: the two security
** key types, the share audience-type PVS, the notebook joins, and the
** child-record list widgets on a notebook's record view.
*******************************************************************************/
public class SqlWorkbenchSecurityAndJoinsMetaDataProducer extends QBitComponentMetaDataProducer<MetaDataProducerMultiOutput, SqlWorkbenchQBitConfig>
{
/*******************************************************************************
**
*******************************************************************************/
@Override
public MetaDataProducerMultiOutput produce(QInstance qInstance) throws QException
{
SqlWorkbenchQBitConfig config = getQBitConfig();
MetaDataProducerMultiOutput output = new MetaDataProducerMultiOutput();
for(QSecurityKeyType keyType : SqlWorkbenchSecurity.buildSecurityKeyTypes(config))
{
output.add(keyType);
}
output.add(SqlWorkbenchTableMetaDataHelper.buildAudienceTypePossibleValueSource(config));
for(QJoinMetaData join : SqlWorkbenchTableMetaDataHelper.buildJoins(config))
{
output.add(join);
}
for(QWidgetMetaDataInterface widget : SqlWorkbenchTableMetaDataHelper.buildChildListWidgets(config))
{
output.add(widget);
}
return (output);
}
/*******************************************************************************
** after the tables, before the PVS, widget and app
*******************************************************************************/
@Override
public int getSortOrder()
{
return (150);
}
}
@@ -17,104 +17,223 @@
package com.kingsrook.qbits.sqlworkbench.metadata;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.data.QRecordEntity;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.layout.QIcon;
import com.kingsrook.qqq.backend.core.model.metadata.possiblevalues.QPossibleValueSource;
import com.kingsrook.qqq.backend.core.model.metadata.tables.Capability;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.tables.UniqueKey;
import java.util.Map;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.customizers.AppendOnlyTableCustomizer;
import com.kingsrook.qbits.sqlworkbench.customizers.CellTableCustomizer;
import com.kingsrook.qbits.sqlworkbench.customizers.NotebookTableCustomizer;
import com.kingsrook.qbits.sqlworkbench.customizers.ShareTableCustomizer;
import com.kingsrook.qbits.sqlworkbench.customizers.SnapshotTableCustomizer;
import com.kingsrook.qbits.sqlworkbench.model.ShareAudienceType;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchExecution;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookVersion;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchResultSnapshot;
import com.kingsrook.qqq.backend.core.actions.customizers.TableCustomizers;
import com.kingsrook.qqq.backend.core.actions.dashboard.widgets.ChildRecordListRenderer;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
import com.kingsrook.qqq.backend.core.model.data.QRecordEntity;
import com.kingsrook.qqq.backend.core.model.metadata.audits.AuditLevel;
import com.kingsrook.qqq.backend.core.model.metadata.audits.QAuditRules;
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReference;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaDataInterface;
import com.kingsrook.qqq.backend.core.model.metadata.fields.AdornmentType;
import com.kingsrook.qqq.backend.core.model.metadata.fields.FieldAdornment;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.joins.JoinOn;
import com.kingsrook.qqq.backend.core.model.metadata.joins.JoinType;
import com.kingsrook.qqq.backend.core.model.metadata.joins.QJoinMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.layout.QIcon;
import com.kingsrook.qqq.backend.core.model.metadata.possiblevalues.QPossibleValueSource;
import com.kingsrook.qqq.backend.core.model.metadata.security.MultiRecordSecurityLock;
import com.kingsrook.qqq.backend.core.model.metadata.security.RecordSecurityLock;
import com.kingsrook.qqq.backend.core.model.metadata.tables.Capability;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QFieldSection;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.tables.Tier;
import com.kingsrook.qqq.backend.core.model.metadata.tables.UniqueKey;
/*******************************************************************************
** Builds the QTableMetaData of the five SQL Workbench tables. Shared by the
** component producers and by tests.
** Builds the SQL Workbench tables, joins and record-view widgets. Shared by
** the component producers and by tests.
**
** All tables are protected by the ADMIN permission
** (sqlWorkbenchAdmin.hasAccess), not the access permission every workbench
** user holds: the generic QQQ table UI / API would otherwise let any user
** read other users' personal notebooks, pinned result data and the full
** execution audit. The workbench endpoints do their own owner / visibility
** checks and then read and write these tables as the system user.
** The tables are ordinary QQQ tables (query, view, edit, export, audits,
** developer mode), secured by QQQ mechanisms rather than by hiding them:
**
** Cells, versions, snapshots and the audit are read-only even for admins
** (insert / edit / delete capabilities disabled); only the qbit writes
** them. Notebooks stay editable for admins (e.g. to clean up).
** - Permissions: QQQ's per-table read / insert / edit / delete permissions
** (SqlWorkbenchQBitConfig lists them per role).
** - Record security locks, enforced by the core actions and so for every
** entry point (table UI, /qqq/v1 table API, processes, PVS lookups,
** exports): a notebook (and its cells, versions and snapshots) is visible
** to its owner (sql_workbench_user = ownerUserId) OR to an audience it is
** shared with (a share row whose audienceKey is one of the session's
** sql_workbench_audience values); only the owner can change it. An
** execution is visible to its user only. Workbench admins hold the
** all-access key and see every row.
** - Append-only: versions and the execution audit have no insert / edit /
** delete capability, and a customizer rejects those writes in the core
** actions unless the qbit itself (system user) makes them.
** - Customizers keep the notebook invariants for edits made through the
** tables exactly as through the notebook UI (ownership, cell order and
** limits, a new version per change, share keys, library visibility).
*******************************************************************************/
public class SqlWorkbenchTableMetaDataHelper
{
public static final String JOIN_NOTEBOOK_CELL = "sql_workbench_notebook_join_sql_workbench_cell";
public static final String JOIN_NOTEBOOK_VERSION = "sql_workbench_notebook_join_sql_workbench_notebook_version";
public static final String JOIN_NOTEBOOK_SNAPSHOT = "sql_workbench_notebook_join_sql_workbench_result_snapshot";
public static final String JOIN_NOTEBOOK_SHARE = "sql_workbench_notebook_join_sql_workbench_notebook_share";
public static final String JOIN_SHARE_NOTEBOOK = "sql_workbench_notebook_share_join_sql_workbench_notebook";
public static final String JOIN_NOTEBOOK_EXECUTION = "sql_workbench_notebook_join_sql_workbench_execution";
/*******************************************************************************
** sqlWorkbenchNotebook
** sql_workbench_notebook
*******************************************************************************/
public static QTableMetaData buildNotebookTable(SqlWorkbenchQBitConfig config) throws QException
{
return (text(base(config, SqlWorkbenchNotebook.TABLE_NAME, "SQL Notebook", "menu_book", SqlWorkbenchNotebook.class), "description", "parametersJson")
QTableMetaData table = text(base(config, SqlWorkbenchNotebook.TABLE_NAME, "SQL Notebook", "menu_book", SqlWorkbenchNotebook.class), "description", "parametersJson")
.withRecordLabelFormat("%s")
.withRecordLabelFields("title"));
.withRecordLabelFields("title")
.withRecordSecurityLock(new MultiRecordSecurityLock()
.withOperator(MultiRecordSecurityLock.BooleanOperator.OR)
.withLock(ownerLock(config, "ownerUserId"))
.withLock(shareLock(config, config.applyPrefix(SqlWorkbenchNotebookShare.TABLE_NAME) + ".audienceKey", config.applyPrefix(JOIN_SHARE_NOTEBOOK))))
.withAuditRules(new QAuditRules().withAuditLevel(AuditLevel.RECORD))
.withSection(new QFieldSection("identity", new QIcon("badge"), Tier.T1, List.of("id", "title", "description", "ownerName", "visibility")))
.withSection(new QFieldSection("runner", "Runner", new QIcon("play_circle"), Tier.T2, null).withWidgetName(config.applyPrefix(SqlWorkbenchHomeWidgetsMetaDataProducer.NOTEBOOK_RUNNER)))
.withSection(new QFieldSection("details", new QIcon("description"), Tier.T2, List.of("defaultBackendName", "parametersJson")))
.withSection(widgetSection(config, JOIN_NOTEBOOK_CELL, "Cells", "notes"))
.withSection(widgetSection(config, JOIN_NOTEBOOK_SHARE, "Shared With", "share"))
.withSection(widgetSection(config, JOIN_NOTEBOOK_SNAPSHOT, "Pinned Results", "push_pin"))
.withSection(widgetSection(config, JOIN_NOTEBOOK_VERSION, "Versions", "history"))
.withSection(widgetSection(config, JOIN_NOTEBOOK_EXECUTION, "Runs", "policy"))
.withSection(new QFieldSection("system", new QIcon("calendar_month"), Tier.T3, List.of("ownerUserId", "currentVersion", "createDate", "modifyDate")));
table.getField("visibility").setIsEditable(false);
table.getField("visibility").setIsRequired(false);
table.getField("visibility").setDefaultValue("PERSONAL");
table.getField("ownerUserId").setIsRequired(false);
table.getField("parametersJson").withFieldAdornment(new FieldAdornment(AdornmentType.CODE_EDITOR).withValue(AdornmentType.CodeEditorValues.languageMode("json")));
return (customize(table, NotebookTableCustomizer.class));
}
/*******************************************************************************
** sqlWorkbenchCell
** sql_workbench_cell
*******************************************************************************/
public static QTableMetaData buildCellTable(SqlWorkbenchQBitConfig config) throws QException
{
QTableMetaData table = text(base(config, SqlWorkbenchCell.TABLE_NAME, "SQL Notebook Cell", "notes", SqlWorkbenchCell.class), "content");
table.setIsHidden(true);
table.withUniqueKey(new UniqueKey("notebookId", "cellKey"));
readOnly(table);
return (linkNotebook(config, table));
QTableMetaData table = text(base(config, SqlWorkbenchCell.TABLE_NAME, "SQL Notebook Cell", "notes", SqlWorkbenchCell.class), "content")
.withRecordLabelFormat("%s cell %s")
.withRecordLabelFields("notebookId", "sortOrder")
.withUniqueKey(new UniqueKey("notebookId", "cellKey"))
.withRecordSecurityLock(childLock(config, JOIN_NOTEBOOK_CELL))
.withAuditRules(new QAuditRules().withAuditLevel(AuditLevel.RECORD))
.withSection(new QFieldSection("identity", new QIcon("badge"), Tier.T1, List.of("id", "notebookId", "sortOrder", "cellType", "title")))
.withSection(new QFieldSection("content", new QIcon("code"), Tier.T2, List.of("content", "backendName")))
.withSection(new QFieldSection("system", new QIcon("calendar_month"), Tier.T3, List.of("cellKey", "createDate", "modifyDate")));
table.getField("cellKey").setIsRequired(false);
table.getField("cellType").setIsRequired(false);
table.getField("cellType").setDefaultValue("SQL");
table.getField("content").withFieldAdornment(new FieldAdornment(AdornmentType.CODE_EDITOR).withValue(AdornmentType.CodeEditorValues.languageMode("sql")));
return (customize(linkNotebook(config, table), CellTableCustomizer.class));
}
/*******************************************************************************
** sqlWorkbenchNotebookVersion
** sql_workbench_notebook_version (append-only)
*******************************************************************************/
public static QTableMetaData buildNotebookVersionTable(SqlWorkbenchQBitConfig config) throws QException
{
QTableMetaData table = text(base(config, SqlWorkbenchNotebookVersion.TABLE_NAME, "SQL Notebook Version", "history", SqlWorkbenchNotebookVersion.class), "contentJson");
table.setIsHidden(true);
table.withUniqueKey(new UniqueKey("notebookId", "versionNumber"));
readOnly(table);
QTableMetaData table = text(base(config, SqlWorkbenchNotebookVersion.TABLE_NAME, "SQL Notebook Version", "history", SqlWorkbenchNotebookVersion.class), "contentJson")
.withRecordLabelFormat("%s v%s")
.withRecordLabelFields("notebookId", "versionNumber")
.withUniqueKey(new UniqueKey("notebookId", "versionNumber"))
.withRecordSecurityLock(childLock(config, JOIN_NOTEBOOK_VERSION))
.withSection(new QFieldSection("identity", new QIcon("badge"), Tier.T1, List.of("id", "notebookId", "versionNumber", "comment")))
.withSection(new QFieldSection("content", new QIcon("code"), Tier.T2, List.of("contentJson")))
.withSection(new QFieldSection("system", new QIcon("calendar_month"), Tier.T3, List.of("savedByName", "savedByUserId", "createDate")));
table.getField("comment").setIsEditable(false);
table.getField("contentJson").withFieldAdornment(new FieldAdornment(AdornmentType.CODE_EDITOR).withValue(AdornmentType.CodeEditorValues.languageMode("json")));
appendOnly(table);
return (linkNotebook(config, table));
}
/*******************************************************************************
** sqlWorkbenchResultSnapshot
** sql_workbench_result_snapshot (pinned from the notebook UI; label edit and
** delete through the table)
*******************************************************************************/
public static QTableMetaData buildResultSnapshotTable(SqlWorkbenchQBitConfig config) throws QException
{
QTableMetaData table = text(base(config, SqlWorkbenchResultSnapshot.TABLE_NAME, "SQL Result Snapshot", "push_pin", SqlWorkbenchResultSnapshot.class), "sqlText", "resultJson");
table.setIsHidden(true);
readOnly(table);
return (linkNotebook(config, table));
QTableMetaData table = text(base(config, SqlWorkbenchResultSnapshot.TABLE_NAME, "SQL Result Snapshot", "push_pin", SqlWorkbenchResultSnapshot.class), "sqlText", "resultJson")
.withRecordLabelFormat("%s %s")
.withRecordLabelFields("notebookId", "label")
.withRecordSecurityLock(childLock(config, JOIN_NOTEBOOK_SNAPSHOT))
.withAuditRules(new QAuditRules().withAuditLevel(AuditLevel.RECORD))
.withSection(new QFieldSection("identity", new QIcon("badge"), Tier.T1, List.of("id", "notebookId", "label", "cellKey")))
.withSection(new QFieldSection("result", new QIcon("table_chart"), Tier.T2, List.of("backendName", "rowCount", "truncated", "executionUuid", "sqlText", "resultJson")))
.withSection(new QFieldSection("system", new QIcon("calendar_month"), Tier.T3, List.of("createdByUserId", "createDate")));
table.getField("sqlText").withFieldAdornment(new FieldAdornment(AdornmentType.CODE_EDITOR).withValue(AdornmentType.CodeEditorValues.languageMode("sql")));
table.getField("resultJson").withFieldAdornment(new FieldAdornment(AdornmentType.CODE_EDITOR).withValue(AdornmentType.CodeEditorValues.languageMode("json")));
table.withoutCapability(Capability.TABLE_INSERT);
return (customize(linkNotebook(config, table), SnapshotTableCustomizer.class));
}
/*******************************************************************************
** sqlWorkbenchExecution (the audit)
** sql_workbench_notebook_share: who, besides the owner, can read a notebook.
*******************************************************************************/
public static QTableMetaData buildNotebookShareTable(SqlWorkbenchQBitConfig config) throws QException
{
QTableMetaData table = base(config, SqlWorkbenchNotebookShare.TABLE_NAME, "SQL Notebook Share", "share", SqlWorkbenchNotebookShare.class)
.withRecordLabelFormat("%s - %s")
.withRecordLabelFields("notebookId", "audienceKey")
.withUniqueKey(new UniqueKey("notebookId", "audienceKey"))
.withRecordSecurityLock(new MultiRecordSecurityLock()
.withOperator(MultiRecordSecurityLock.BooleanOperator.OR)
.withLock(ownerLock(config, config.applyPrefix(SqlWorkbenchNotebook.TABLE_NAME) + ".ownerUserId", config.applyPrefix(JOIN_NOTEBOOK_SHARE)))
.withLock(shareLock(config, "audienceKey")))
.withAuditRules(new QAuditRules().withAuditLevel(AuditLevel.RECORD))
.withSection(new QFieldSection("identity", new QIcon("badge"), Tier.T1, List.of("id", "notebookId", "audienceType", "audienceId")))
.withSection(new QFieldSection("system", new QIcon("calendar_month"), Tier.T3, List.of("audienceKey", "createdByName", "createdByUserId", "createDate", "modifyDate")));
table.getField("audienceType").setPossibleValueSourceName(config.applyPrefix(ShareAudienceType.NAME));
return (customize(linkNotebook(config, table), ShareTableCustomizer.class));
}
/*******************************************************************************
** sql_workbench_execution (the audit; append-only)
*******************************************************************************/
public static QTableMetaData buildExecutionTable(SqlWorkbenchQBitConfig config) throws QException
{
QTableMetaData table = text(base(config, SqlWorkbenchExecution.TABLE_NAME, "SQL Execution Audit", "policy", SqlWorkbenchExecution.class), "sqlText", "parametersJson", "errorMessage");
table.withUniqueKey(new UniqueKey("executionUuid"));
readOnly(table);
table.withRecordLabelFormat("%s %s").withRecordLabelFields("mode", "executionUuid");
QTableMetaData table = text(base(config, SqlWorkbenchExecution.TABLE_NAME, "SQL Execution Audit", "policy", SqlWorkbenchExecution.class), "sqlText", "parametersJson", "errorMessage")
.withUniqueKey(new UniqueKey("executionUuid"))
.withRecordLabelFormat("%s %s")
.withRecordLabelFields("mode", "executionUuid")
.withRecordSecurityLock(ownerLock(config, "userId"))
.withSection(new QFieldSection("identity", new QIcon("badge"), Tier.T1, List.of("id", "executionUuid", "mode", "status", "backendName")))
.withSection(new QFieldSection("run", new QIcon("timer"), Tier.T2, List.of("userName", "userId", "startTime", "endTime", "durationMs", "rowsReturned", "rowsAffected", "truncated",
"statementCount", "statementKinds", "notebookId", "cellKey", "clientIp")))
.withSection(new QFieldSection("sql", new QIcon("code"), Tier.T2, List.of("sqlText", "sqlHash", "parametersJson", "errorMessage")))
.withSection(new QFieldSection("system", new QIcon("calendar_month"), Tier.T3, List.of("createDate")));
table.getField("sqlText").withFieldAdornment(new FieldAdornment(AdornmentType.CODE_EDITOR).withValue(AdornmentType.CodeEditorValues.languageMode("sql")));
table.getField("notebookId").setPossibleValueSourceName(config.applyPrefix(SqlWorkbenchNotebook.TABLE_NAME));
appendOnly(table);
return (table);
}
@@ -122,6 +241,8 @@ public class SqlWorkbenchTableMetaDataHelper
/*******************************************************************************
** Possible value source over the notebook table, for notebookId fields.
** Lookups go through the table's locks, so they only show readable
** notebooks.
*******************************************************************************/
public static QPossibleValueSource buildNotebookPossibleValueSource(SqlWorkbenchQBitConfig config)
{
@@ -131,11 +252,101 @@ public class SqlWorkbenchTableMetaDataHelper
/*******************************************************************************
** All five tables.
** Possible value source for share audience types.
*******************************************************************************/
public static QPossibleValueSource buildAudienceTypePossibleValueSource(SqlWorkbenchQBitConfig config)
{
return (QPossibleValueSource.newForEnum(config.applyPrefix(ShareAudienceType.NAME), ShareAudienceType.values()));
}
/*******************************************************************************
** All six tables.
*******************************************************************************/
public static List<QTableMetaData> buildAllTables(SqlWorkbenchQBitConfig config) throws QException
{
return (List.of(buildNotebookTable(config), buildCellTable(config), buildNotebookVersionTable(config), buildResultSnapshotTable(config), buildExecutionTable(config)));
return (List.of(buildNotebookTable(config), buildCellTable(config), buildNotebookVersionTable(config), buildResultSnapshotTable(config),
buildNotebookShareTable(config), buildExecutionTable(config)));
}
/*******************************************************************************
** The joins: notebook to each child (one-to-many), notebook to its runs in
** the execution audit (one-to-many; the audit is not foreign-keyed, so it
** outlives the notebook), and share to notebook (many-to-one, the
** direction the share locks walk).
*******************************************************************************/
public static List<QJoinMetaData> buildJoins(SqlWorkbenchQBitConfig config)
{
String notebook = config.applyPrefix(SqlWorkbenchNotebook.TABLE_NAME);
Map<String, String> children = new LinkedHashMap<>();
children.put(JOIN_NOTEBOOK_CELL, SqlWorkbenchCell.TABLE_NAME);
children.put(JOIN_NOTEBOOK_VERSION, SqlWorkbenchNotebookVersion.TABLE_NAME);
children.put(JOIN_NOTEBOOK_SNAPSHOT, SqlWorkbenchResultSnapshot.TABLE_NAME);
children.put(JOIN_NOTEBOOK_SHARE, SqlWorkbenchNotebookShare.TABLE_NAME);
children.put(JOIN_NOTEBOOK_EXECUTION, SqlWorkbenchExecution.TABLE_NAME);
List<QJoinMetaData> joins = new ArrayList<>();
for(Map.Entry<String, String> entry : children.entrySet())
{
joins.add(new QJoinMetaData()
.withName(config.applyPrefix(entry.getKey()))
.withLeftTable(notebook)
.withRightTable(config.applyPrefix(entry.getValue()))
.withType(JoinType.ONE_TO_MANY)
.withJoinOn(new JoinOn("id", "notebookId")));
}
joins.add(new QJoinMetaData()
.withName(config.applyPrefix(JOIN_SHARE_NOTEBOOK))
.withLeftTable(config.applyPrefix(SqlWorkbenchNotebookShare.TABLE_NAME))
.withRightTable(notebook)
.withType(JoinType.MANY_TO_ONE)
.withJoinOn(new JoinOn("notebookId", "id")));
return (joins);
}
/*******************************************************************************
** Child-record list widgets on the notebook's record view: cells (add and
** edit in place), shares (add, for users who may share), pinned results,
** versions, and runs (the notebook's executions the viewer may see: their
** own, or everyone's for workbench admins).
*******************************************************************************/
public static List<QWidgetMetaDataInterface> buildChildListWidgets(SqlWorkbenchQBitConfig config)
{
Map<String, QJoinMetaData> joins = new LinkedHashMap<>();
for(QJoinMetaData join : buildJoins(config))
{
joins.put(join.getName(), join);
}
List<QWidgetMetaDataInterface> widgets = new ArrayList<>();
widgets.add(ChildRecordListRenderer.widgetMetaDataBuilder(joins.get(config.applyPrefix(JOIN_NOTEBOOK_CELL)))
.withLabel("Cells").withCanAddChildRecord(true).withMaxRows(200)
.withOnlyIncludeFieldNames(List.of("id", "sortOrder", "cellType", "title", "backendName", "modifyDate"))
.getWidgetMetaData());
widgets.add(ChildRecordListRenderer.widgetMetaDataBuilder(joins.get(config.applyPrefix(JOIN_NOTEBOOK_SHARE)))
.withLabel("Shared With").withCanAddChildRecord(true)
.withOnlyIncludeFieldNames(List.of("id", "audienceType", "audienceId", "createdByName", "createDate"))
.getWidgetMetaData());
widgets.add(ChildRecordListRenderer.widgetMetaDataBuilder(joins.get(config.applyPrefix(JOIN_NOTEBOOK_SNAPSHOT)))
.withLabel("Pinned Results")
.withOnlyIncludeFieldNames(List.of("id", "label", "cellKey", "rowCount", "truncated", "createDate"))
.getWidgetMetaData());
widgets.add(ChildRecordListRenderer.widgetMetaDataBuilder(joins.get(config.applyPrefix(JOIN_NOTEBOOK_VERSION)))
.withLabel("Versions").withMaxRows(25)
.withOrderBys(List.of(new QFilterOrderBy("versionNumber", false)))
.withOnlyIncludeFieldNames(List.of("id", "versionNumber", "comment", "savedByName", "createDate"))
.getWidgetMetaData());
widgets.add(ChildRecordListRenderer.widgetMetaDataBuilder(joins.get(config.applyPrefix(JOIN_NOTEBOOK_EXECUTION)))
.withLabel("Runs").withMaxRows(25)
.withOrderBys(List.of(new QFilterOrderBy("startTime", false), new QFilterOrderBy("id", false)))
.withOnlyIncludeFieldNames(List.of("id", "startTime", "userName", "mode", "status", "durationMs", "rowsReturned", "rowsAffected", "cellKey"))
.getWidgetMetaData());
return (widgets);
}
@@ -151,7 +362,7 @@ public class SqlWorkbenchTableMetaDataHelper
.withIcon(new QIcon(icon))
.withBackendName(config.getBackendName())
.withPrimaryKeyField("id")
.withPermissionRules(config.getAdminPermissionRules())
.withPermissionRules(config.getTablePermissionRules())
.withFieldsFromEntity(entity);
return (table);
}
@@ -159,11 +370,91 @@ public class SqlWorkbenchTableMetaDataHelper
/*******************************************************************************
** Only the qbit (as the system user) writes this table.
** Owner lock: the session's sql_workbench_user value must equal the field
** (read and write).
*******************************************************************************/
private static void readOnly(QTableMetaData table)
static RecordSecurityLock ownerLock(SqlWorkbenchQBitConfig config, String fieldName, String... joinNameChain)
{
RecordSecurityLock lock = new RecordSecurityLock()
.withSecurityKeyType(config.getUserSecurityKeyTypeName())
.withFieldName(fieldName)
.withLockScope(RecordSecurityLock.LockScope.READ_AND_WRITE);
if(joinNameChain.length > 0)
{
lock.setJoinNameChain(List.of(joinNameChain));
}
return (lock);
}
/*******************************************************************************
** Share lock: a share row's audienceKey is one of the session's
** sql_workbench_audience values (read only).
*******************************************************************************/
static RecordSecurityLock shareLock(SqlWorkbenchQBitConfig config, String fieldName, String... joinNameChain)
{
RecordSecurityLock lock = new RecordSecurityLock()
.withSecurityKeyType(config.getAudienceSecurityKeyTypeName())
.withFieldName(fieldName)
.withLockScope(RecordSecurityLock.LockScope.READ);
if(joinNameChain.length > 0)
{
lock.setJoinNameChain(List.of(joinNameChain));
}
return (lock);
}
/*******************************************************************************
** Lock for a notebook child table: the notebook's owner, or (read only)
** an audience the notebook is shared with.
*******************************************************************************/
private static MultiRecordSecurityLock childLock(SqlWorkbenchQBitConfig config, String notebookJoinChild)
{
return (new MultiRecordSecurityLock()
.withOperator(MultiRecordSecurityLock.BooleanOperator.OR)
.withLock(ownerLock(config, config.applyPrefix(SqlWorkbenchNotebook.TABLE_NAME) + ".ownerUserId", config.applyPrefix(notebookJoinChild)))
.withLock(shareLock(config, config.applyPrefix(SqlWorkbenchNotebookShare.TABLE_NAME) + ".audienceKey", config.applyPrefix(JOIN_SHARE_NOTEBOOK), config.applyPrefix(notebookJoinChild))));
}
/*******************************************************************************
** A record-view section that shows one of the child-list widgets.
*******************************************************************************/
private static QFieldSection widgetSection(SqlWorkbenchQBitConfig config, String joinName, String label, String icon)
{
return (new QFieldSection(config.applyPrefix(joinName), label, new QIcon(icon), Tier.T2, null).withWidgetName(config.applyPrefix(joinName)));
}
/*******************************************************************************
** Only the qbit (system user) writes this table: the capabilities hide
** writes in the UI / API, the customizer rejects them in the core actions.
*******************************************************************************/
private static void appendOnly(QTableMetaData table)
{
table.withoutCapabilities(Capability.TABLE_INSERT, Capability.TABLE_UPDATE, Capability.TABLE_DELETE);
customize(table, AppendOnlyTableCustomizer.class);
}
/*******************************************************************************
** Register one customizer class for every insert / update / delete hook.
*******************************************************************************/
private static QTableMetaData customize(QTableMetaData table, Class<?> customizer)
{
QCodeReference reference = new QCodeReference(customizer);
for(TableCustomizers role : List.of(TableCustomizers.PRE_INSERT_RECORD, TableCustomizers.POST_INSERT_RECORD, TableCustomizers.PRE_UPDATE_RECORD,
TableCustomizers.POST_UPDATE_RECORD, TableCustomizers.PRE_DELETE_RECORD, TableCustomizers.POST_DELETE_RECORD))
{
table.withCustomizer(role, reference);
}
return (table);
}
@@ -0,0 +1,122 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.model;
import java.util.Locale;
import com.kingsrook.qqq.backend.core.model.metadata.possiblevalues.PossibleValueEnum;
/*******************************************************************************
** Who a notebook share grants read access to.
*******************************************************************************/
public enum ShareAudienceType implements PossibleValueEnum<String>
{
USER("User", "user"),
GROUP("Group", "group"),
ALL("Everyone (shared library)", "all");
public static final String NAME = "sql_workbench_share_audience_type";
/*******************************************************************************
** The possible-value source name before 1.0.0-RC.3.
*******************************************************************************/
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_NAME = "sqlWorkbenchShareAudienceType";
private final String label;
private final String keyPrefix;
/*******************************************************************************
**
*******************************************************************************/
ShareAudienceType(String label, String keyPrefix)
{
this.label = label;
this.keyPrefix = keyPrefix;
}
/*******************************************************************************
** The audience key for this type and id: "user:alice", "group:analysts",
** or "all". Ids are trimmed and lower-cased, as are the session's values.
*******************************************************************************/
public String audienceKey(String audienceId)
{
if(this == ALL)
{
return (keyPrefix);
}
return (keyPrefix + ":" + normalize(audienceId));
}
/*******************************************************************************
** Trim and lower-case an audience id (null stays null).
*******************************************************************************/
public static String normalize(String audienceId)
{
return (audienceId == null ? null : audienceId.trim().toLowerCase(Locale.ROOT));
}
/*******************************************************************************
** Look up by name, ignoring case; null when unknown.
*******************************************************************************/
public static ShareAudienceType of(Object value)
{
if(value == null)
{
return (null);
}
for(ShareAudienceType type : values())
{
if(type.name().equalsIgnoreCase(value.toString().trim()))
{
return (type);
}
}
return (null);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public String getPossibleValueId()
{
return (name());
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public String getPossibleValueLabel()
{
return (label);
}
}
@@ -29,7 +29,14 @@ import com.kingsrook.qqq.backend.core.model.metadata.fields.ValueTooLongBehavior
*******************************************************************************/
public class SqlWorkbenchCell extends QRecordEntity
{
public static final String TABLE_NAME = "sqlWorkbenchCell";
public static final String TABLE_NAME = "sql_workbench_cell";
/*******************************************************************************
** The table name before 1.0.0-RC.3 (owner rule: lowercase snake_case,
** prefixed with the qbit name). Kept for migrating stored references only.
*******************************************************************************/
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_TABLE_NAME = "sqlWorkbenchCell";
@QField(isEditable = false, isPrimaryKey = true)
private Integer id;
@@ -31,7 +31,14 @@ import com.kingsrook.qqq.backend.core.model.metadata.fields.ValueTooLongBehavior
*******************************************************************************/
public class SqlWorkbenchExecution extends QRecordEntity
{
public static final String TABLE_NAME = "sqlWorkbenchExecution";
public static final String TABLE_NAME = "sql_workbench_execution";
/*******************************************************************************
** The table name before 1.0.0-RC.3 (owner rule: lowercase snake_case,
** prefixed with the qbit name). Kept for migrating stored references only.
*******************************************************************************/
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_TABLE_NAME = "sqlWorkbenchExecution";
@QField(isEditable = false, isPrimaryKey = true)
private Integer id;
@@ -30,7 +30,14 @@ import com.kingsrook.qqq.backend.core.model.metadata.fields.ValueTooLongBehavior
*******************************************************************************/
public class SqlWorkbenchNotebook extends QRecordEntity
{
public static final String TABLE_NAME = "sqlWorkbenchNotebook";
public static final String TABLE_NAME = "sql_workbench_notebook";
/*******************************************************************************
** The table name before 1.0.0-RC.3 (owner rule: lowercase snake_case,
** prefixed with the qbit name). Kept for migrating stored references only.
*******************************************************************************/
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_TABLE_NAME = "sqlWorkbenchNotebook";
@QField(isEditable = false, isPrimaryKey = true)
private Integer id;
@@ -0,0 +1,370 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.model;
import java.time.Instant;
import com.kingsrook.qqq.backend.core.model.data.QField;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.data.QRecordEntity;
import com.kingsrook.qqq.backend.core.model.metadata.fields.ValueTooLongBehavior;
/*******************************************************************************
** One audience a notebook is shared with (read-only): a user, a group, or
** everyone with workbench access (audienceType ALL - the shared library).
** audienceKey ("user:alice", "group:analysts", "all") is computed by the
** table customizer and is what the record security locks match against the
** session's sql_workbench_audience key values.
*******************************************************************************/
public class SqlWorkbenchNotebookShare extends QRecordEntity
{
public static final String TABLE_NAME = "sql_workbench_notebook_share";
/*******************************************************************************
** The table name before 1.0.0-RC.3 (owner rule: lowercase snake_case,
** prefixed with the qbit name). Kept for migrating stored references only.
*******************************************************************************/
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_TABLE_NAME = "sqlWorkbenchNotebookShare";
@QField(isEditable = false, isPrimaryKey = true)
private Integer id;
@QField(isRequired = true, possibleValueSourceName = SqlWorkbenchNotebook.TABLE_NAME)
private Integer notebookId;
@QField(isRequired = true, maxLength = 16, possibleValueSourceName = ShareAudienceType.NAME)
private String audienceType;
@QField(maxLength = 255, label = "User or Group")
private String audienceId;
@QField(isEditable = false, maxLength = 300)
private String audienceKey;
@QField(isEditable = false, maxLength = 255)
private String createdByUserId;
@QField(isEditable = false, maxLength = 255, valueTooLongBehavior = ValueTooLongBehavior.TRUNCATE_ELLIPSIS)
private String createdByName;
@QField(isEditable = false)
private Instant createDate;
@QField(isEditable = false)
private Instant modifyDate;
/*******************************************************************************
** Default constructor.
*******************************************************************************/
public SqlWorkbenchNotebookShare()
{
}
/*******************************************************************************
** Constructor that populates from a QRecord.
*******************************************************************************/
public SqlWorkbenchNotebookShare(QRecord record)
{
populateFromQRecord(record);
}
/*******************************************************************************
** Getter for id
*******************************************************************************/
public Integer getId()
{
return (this.id);
}
/*******************************************************************************
** Setter for id
*******************************************************************************/
public void setId(Integer id)
{
this.id = id;
}
/*******************************************************************************
** Fluent setter for id
*******************************************************************************/
public SqlWorkbenchNotebookShare withId(Integer id)
{
this.id = id;
return (this);
}
/*******************************************************************************
** Getter for notebookId
*******************************************************************************/
public Integer getNotebookId()
{
return (this.notebookId);
}
/*******************************************************************************
** Setter for notebookId
*******************************************************************************/
public void setNotebookId(Integer notebookId)
{
this.notebookId = notebookId;
}
/*******************************************************************************
** Fluent setter for notebookId
*******************************************************************************/
public SqlWorkbenchNotebookShare withNotebookId(Integer notebookId)
{
this.notebookId = notebookId;
return (this);
}
/*******************************************************************************
** Getter for audienceType
*******************************************************************************/
public String getAudienceType()
{
return (this.audienceType);
}
/*******************************************************************************
** Setter for audienceType
*******************************************************************************/
public void setAudienceType(String audienceType)
{
this.audienceType = audienceType;
}
/*******************************************************************************
** Fluent setter for audienceType
*******************************************************************************/
public SqlWorkbenchNotebookShare withAudienceType(String audienceType)
{
this.audienceType = audienceType;
return (this);
}
/*******************************************************************************
** Getter for audienceId
*******************************************************************************/
public String getAudienceId()
{
return (this.audienceId);
}
/*******************************************************************************
** Setter for audienceId
*******************************************************************************/
public void setAudienceId(String audienceId)
{
this.audienceId = audienceId;
}
/*******************************************************************************
** Fluent setter for audienceId
*******************************************************************************/
public SqlWorkbenchNotebookShare withAudienceId(String audienceId)
{
this.audienceId = audienceId;
return (this);
}
/*******************************************************************************
** Getter for audienceKey
*******************************************************************************/
public String getAudienceKey()
{
return (this.audienceKey);
}
/*******************************************************************************
** Setter for audienceKey
*******************************************************************************/
public void setAudienceKey(String audienceKey)
{
this.audienceKey = audienceKey;
}
/*******************************************************************************
** Fluent setter for audienceKey
*******************************************************************************/
public SqlWorkbenchNotebookShare withAudienceKey(String audienceKey)
{
this.audienceKey = audienceKey;
return (this);
}
/*******************************************************************************
** Getter for createdByUserId
*******************************************************************************/
public String getCreatedByUserId()
{
return (this.createdByUserId);
}
/*******************************************************************************
** Setter for createdByUserId
*******************************************************************************/
public void setCreatedByUserId(String createdByUserId)
{
this.createdByUserId = createdByUserId;
}
/*******************************************************************************
** Fluent setter for createdByUserId
*******************************************************************************/
public SqlWorkbenchNotebookShare withCreatedByUserId(String createdByUserId)
{
this.createdByUserId = createdByUserId;
return (this);
}
/*******************************************************************************
** Getter for createdByName
*******************************************************************************/
public String getCreatedByName()
{
return (this.createdByName);
}
/*******************************************************************************
** Setter for createdByName
*******************************************************************************/
public void setCreatedByName(String createdByName)
{
this.createdByName = createdByName;
}
/*******************************************************************************
** Fluent setter for createdByName
*******************************************************************************/
public SqlWorkbenchNotebookShare withCreatedByName(String createdByName)
{
this.createdByName = createdByName;
return (this);
}
/*******************************************************************************
** Getter for createDate
*******************************************************************************/
public Instant getCreateDate()
{
return (this.createDate);
}
/*******************************************************************************
** Setter for createDate
*******************************************************************************/
public void setCreateDate(Instant createDate)
{
this.createDate = createDate;
}
/*******************************************************************************
** Fluent setter for createDate
*******************************************************************************/
public SqlWorkbenchNotebookShare withCreateDate(Instant createDate)
{
this.createDate = createDate;
return (this);
}
/*******************************************************************************
** Getter for modifyDate
*******************************************************************************/
public Instant getModifyDate()
{
return (this.modifyDate);
}
/*******************************************************************************
** Setter for modifyDate
*******************************************************************************/
public void setModifyDate(Instant modifyDate)
{
this.modifyDate = modifyDate;
}
/*******************************************************************************
** Fluent setter for modifyDate
*******************************************************************************/
public SqlWorkbenchNotebookShare withModifyDate(Instant modifyDate)
{
this.modifyDate = modifyDate;
return (this);
}
}
@@ -29,7 +29,14 @@ import com.kingsrook.qqq.backend.core.model.metadata.fields.ValueTooLongBehavior
*******************************************************************************/
public class SqlWorkbenchNotebookVersion extends QRecordEntity
{
public static final String TABLE_NAME = "sqlWorkbenchNotebookVersion";
public static final String TABLE_NAME = "sql_workbench_notebook_version";
/*******************************************************************************
** The table name before 1.0.0-RC.3 (owner rule: lowercase snake_case,
** prefixed with the qbit name). Kept for migrating stored references only.
*******************************************************************************/
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_TABLE_NAME = "sqlWorkbenchNotebookVersion";
@QField(isEditable = false, isPrimaryKey = true)
private Integer id;
@@ -30,7 +30,14 @@ import com.kingsrook.qqq.backend.core.model.metadata.fields.ValueTooLongBehavior
*******************************************************************************/
public class SqlWorkbenchResultSnapshot extends QRecordEntity
{
public static final String TABLE_NAME = "sqlWorkbenchResultSnapshot";
public static final String TABLE_NAME = "sql_workbench_result_snapshot";
/*******************************************************************************
** The table name before 1.0.0-RC.3 (owner rule: lowercase snake_case,
** prefixed with the qbit name). Kept for migrating stored references only.
*******************************************************************************/
@Deprecated(since = "1.0.0-RC.3")
public static final String LEGACY_TABLE_NAME = "sqlWorkbenchResultSnapshot";
@QField(isEditable = false, isPrimaryKey = true)
private Integer id;
@@ -18,26 +18,42 @@ package com.kingsrook.qbits.sqlworkbench.notebook;
import java.io.Serializable;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import com.fasterxml.jackson.core.type.TypeReference;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchRuntime;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchSecurity;
import com.kingsrook.qbits.sqlworkbench.customizers.SqlWorkbenchActionFlag;
import com.kingsrook.qbits.sqlworkbench.engine.Caller;
import com.kingsrook.qbits.sqlworkbench.engine.CollectedResult;
import com.kingsrook.qbits.sqlworkbench.engine.ColumnInfo;
import com.kingsrook.qbits.sqlworkbench.engine.ResultCache;
import com.kingsrook.qbits.sqlworkbench.model.ShareAudienceType;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookVersion;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchResultSnapshot;
import com.kingsrook.qqq.backend.core.actions.QBackendTransaction;
import com.kingsrook.qqq.backend.core.actions.tables.DeleteAction;
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
import com.kingsrook.qqq.backend.core.actions.tables.QueryAction;
import com.kingsrook.qqq.backend.core.actions.tables.UpdateAction;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QBadRequestException;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.exceptions.QNotFoundException;
import com.kingsrook.qqq.backend.core.exceptions.QPermissionDeniedException;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
@@ -46,39 +62,28 @@ import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateOutput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import com.kingsrook.qqq.backend.core.utils.JsonUtils;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchRuntime;
import com.kingsrook.qbits.sqlworkbench.SystemUser;
import com.kingsrook.qbits.sqlworkbench.engine.Caller;
import com.kingsrook.qbits.sqlworkbench.engine.CollectedResult;
import com.kingsrook.qbits.sqlworkbench.engine.ColumnInfo;
import com.kingsrook.qbits.sqlworkbench.engine.ResultCache;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookVersion;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchResultSnapshot;
/*******************************************************************************
** Notebooks, their cells, versions and pinned result snapshots.
** Notebooks, their cells, versions, pinned result snapshots and shares, for
** the notebook UI.
**
** Visibility: a caller reads their own notebooks and every SHARED one; only
** the owner edits, deletes, restores and pins; publishing needs the share
** permission and ownership; anyone with access can duplicate what they can
** read.
** Everything here runs as the caller's own session through the standard
** QQQ table actions, so the same record locks and table customizers apply
** as for the table UI and the /qqq/v1 table API: a caller reads their own
** notebooks and the ones shared with them (a user / group share, or the
** shared library), and only the owner changes one. The explicit checks
** (readable / owned / canShare) stay as defense in depth and to give the
** notebook UI its 404 / 403 answers. Only version bookkeeping runs as the
** system user (NotebookVersioning), because versions are append-only.
**
** These checks are the boundary: the tables themselves are guarded by the
** admin permission (sqlWorkbenchAdmin.hasAccess), which ordinary users do
** not hold, so the generic QQQ table UI / API cannot reach other users'
** notebooks. Every read and write here runs as the system user (SystemUser)
** and every public method checks the Caller first (readable / owned /
** canShare) before it reads content or writes anything.
**
** Saves replace a notebook's cells and append a version (full JSON), in one
** transaction; versions beyond notebookVersionRetention are pruned.
** Saves replace a notebook's cells and append one version (full JSON), in
** one transaction; versions beyond notebookVersionRetention are pruned.
*******************************************************************************/
public class NotebookService
{
@@ -115,24 +120,46 @@ public class NotebookService
/*******************************************************************************
** Notebook summaries: scope "mine" (default) or "shared".
** Make sure the session carries the workbench keys (hosts normally apply
** them at login, with groups).
*******************************************************************************/
private void prepare()
{
SqlWorkbenchSecurity.ensureSessionKeys(config, QContext.getQSession());
}
/*******************************************************************************
** Notebook summaries: scope "mine" (default) or "shared" (shared with the
** caller - directly, through a group, or in the library - and not theirs).
*******************************************************************************/
public List<Map<String, Object>> list(String scope, Caller caller) throws QException
{
return (SystemUser.call(() ->
prepare();
QQueryFilter filter;
if("shared".equalsIgnoreCase(scope))
{
QQueryFilter filter = "shared".equalsIgnoreCase(scope)
? new QQueryFilter(new QFilterCriteria("visibility", QCriteriaOperator.EQUALS, VISIBILITY_SHARED))
: new QQueryFilter(new QFilterCriteria("ownerUserId", QCriteriaOperator.EQUALS, caller.userId()));
filter.withOrderBy(new QFilterOrderBy("modifyDate", false)).withLimit(500);
List<Map<String, Object>> result = new ArrayList<>();
for(QRecord record : QueryAction.execute(table(SqlWorkbenchNotebook.TABLE_NAME), filter))
Set<Serializable> ids = sharedWithCallerNotebookIds();
if(ids.isEmpty())
{
result.add(summary(new SqlWorkbenchNotebook(record), caller));
return (new ArrayList<>());
}
return (result);
}));
filter = new QQueryFilter(new QFilterCriteria("id", QCriteriaOperator.IN, new ArrayList<>(ids)))
.withCriteria(new QFilterCriteria("ownerUserId", QCriteriaOperator.NOT_EQUALS, caller.userId()));
}
else
{
filter = new QQueryFilter(new QFilterCriteria("ownerUserId", QCriteriaOperator.EQUALS, caller.userId()));
}
filter.withOrderBy(new QFilterOrderBy("modifyDate", false)).withLimit(500);
List<Map<String, Object>> result = new ArrayList<>();
for(QRecord record : QueryAction.execute(table(SqlWorkbenchNotebook.TABLE_NAME), filter))
{
result.add(summary(new SqlWorkbenchNotebook(record), caller));
}
return (result);
}
@@ -142,46 +169,38 @@ public class NotebookService
*******************************************************************************/
public Map<String, Object> get(Integer id, Caller caller) throws QException
{
return (SystemUser.call(() ->
{
SqlWorkbenchNotebook notebook = readable(id, caller);
Map<String, Object> view = summary(notebook, caller);
view.put("parameters", parseParameters(notebook.getParametersJson()));
view.put("cells", cells(notebook.getId()).stream().map(NotebookService::cellView).toList());
return (view);
}));
prepare();
SqlWorkbenchNotebook notebook = readable(id, caller);
Map<String, Object> view = summary(notebook, caller);
view.put("parameters", parseParameters(notebook.getParametersJson()));
view.put("cells", NotebookVersioning.cells(config, notebook.getId(), null).stream().map(NotebookService::cellView).toList());
return (view);
}
/*******************************************************************************
** Create a personal notebook (version 1).
** Create a personal notebook (version 1). The table customizer makes the
** caller its owner.
*******************************************************************************/
public Map<String, Object> create(Map<String, Object> body, Caller caller) throws QException
{
return (SystemUser.call(() ->
prepare();
SqlWorkbenchNotebook notebook = new SqlWorkbenchNotebook();
applyFields(notebook, body, true);
List<SqlWorkbenchCell> cells = parseCells(body.get("cells"));
try(QBackendTransaction transaction = QBackendTransaction.openFor(new InsertInput(table(SqlWorkbenchNotebook.TABLE_NAME))))
{
Instant now = runtime.getClock().instant();
SqlWorkbenchNotebook notebook = new SqlWorkbenchNotebook()
.withOwnerUserId(caller.userId())
.withOwnerName(caller.userName())
.withVisibility(VISIBILITY_PERSONAL)
.withCurrentVersion(0)
.withCreateDate(now);
applyFields(notebook, body, true);
List<SqlWorkbenchCell> cells = parseCells(body.get("cells"));
try(QBackendTransaction transaction = QBackendTransaction.openFor(new InsertInput(table(SqlWorkbenchNotebook.TABLE_NAME))))
{
notebook.setModifyDate(now);
InsertOutput output = new InsertAction().execute(new InsertInput(table(SqlWorkbenchNotebook.TABLE_NAME)).withRecordEntity(notebook).withTransaction(transaction));
throwIfErrors(output.getRecords().get(0));
notebook.setId(output.getRecords().get(0).getValueInteger("id"));
saveContent(notebook, cells, caller, (String) body.get("comment"), transaction);
commit(transaction);
}
return (get(notebook.getId(), caller));
}));
InsertOutput output = new InsertAction().execute(new InsertInput(table(SqlWorkbenchNotebook.TABLE_NAME))
.withFlag(SqlWorkbenchActionFlag.NOTEBOOK_SAVE)
.withRecordEntity(notebook)
.withTransaction(transaction));
throwIfErrors(output.getRecords().get(0));
notebook.setId(output.getRecords().get(0).getValueInteger("id"));
saveContent(notebook, cells, (String) body.get("comment"), transaction);
commit(transaction);
}
return (get(notebook.getId(), caller));
}
@@ -191,64 +210,64 @@ public class NotebookService
*******************************************************************************/
public Map<String, Object> update(Integer id, Map<String, Object> body, Caller caller) throws QException
{
return (SystemUser.call(() ->
prepare();
SqlWorkbenchNotebook notebook = owned(id, caller);
applyFields(notebook, body, false);
List<SqlWorkbenchCell> cells = body.containsKey("cells") ? parseCells(body.get("cells")) : NotebookVersioning.cells(config, id, null);
try(QBackendTransaction transaction = QBackendTransaction.openFor(new InsertInput(table(SqlWorkbenchNotebook.TABLE_NAME))))
{
SqlWorkbenchNotebook notebook = owned(id, caller);
applyFields(notebook, body, false);
List<SqlWorkbenchCell> cells = body.containsKey("cells") ? parseCells(body.get("cells")) : cells(id);
try(QBackendTransaction transaction = QBackendTransaction.openFor(new InsertInput(table(SqlWorkbenchNotebook.TABLE_NAME))))
{
saveContent(notebook, cells, caller, (String) body.get("comment"), transaction);
commit(transaction);
}
pruneVersions(id);
return (get(id, caller));
}));
saveContent(notebook, cells, (String) body.get("comment"), transaction);
commit(transaction);
}
return (get(id, caller));
}
/*******************************************************************************
** Delete a notebook and everything under it.
** Delete a notebook; its table customizer deletes everything under it.
*******************************************************************************/
public void delete(Integer id, Caller caller) throws QException
{
SystemUser.run(() ->
prepare();
owned(id, caller);
try(QBackendTransaction transaction = QBackendTransaction.openFor(new InsertInput(table(SqlWorkbenchNotebook.TABLE_NAME))))
{
owned(id, caller);
try(QBackendTransaction transaction = QBackendTransaction.openFor(new InsertInput(table(SqlWorkbenchNotebook.TABLE_NAME))))
{
for(String child : List.of(SqlWorkbenchCell.TABLE_NAME, SqlWorkbenchNotebookVersion.TABLE_NAME, SqlWorkbenchResultSnapshot.TABLE_NAME))
{
new DeleteAction().execute(new DeleteInput(table(child)).withQueryFilter(byNotebook(id)).withTransaction(transaction));
}
new DeleteAction().execute(new DeleteInput(table(SqlWorkbenchNotebook.TABLE_NAME)).withPrimaryKeys(List.of(id)).withTransaction(transaction));
commit(transaction);
}
});
DeleteOutput output = new DeleteAction().execute(new DeleteInput(table(SqlWorkbenchNotebook.TABLE_NAME)).withPrimaryKeys(List.of(id)).withTransaction(transaction));
throwIfErrors(output.getRecordsWithErrors());
commit(transaction);
}
}
/*******************************************************************************
** Publish to (or withdraw from) the shared library.
** Publish to (or withdraw from) the shared library: an ALL share row.
*******************************************************************************/
public Map<String, Object> publish(Integer id, boolean shared, Caller caller) throws QException
{
return (SystemUser.call(() ->
prepare();
if(!caller.canShare())
{
if(!caller.canShare())
{
throw (new QPermissionDeniedException("Publishing to the shared library needs the " + config.getSharePermissionName() + " permission."));
}
SqlWorkbenchNotebook notebook = owned(id, caller);
new UpdateAction().execute(new UpdateInput(table(SqlWorkbenchNotebook.TABLE_NAME)).withRecord(new QRecord()
.withValue("id", notebook.getId())
.withValue("visibility", shared ? VISIBILITY_SHARED : VISIBILITY_PERSONAL)
.withValue("modifyDate", runtime.getClock().instant())));
return (get(id, caller));
}));
throw (new QPermissionDeniedException("Publishing to the shared library needs the " + config.getSharePermissionName() + " permission."));
}
owned(id, caller);
QQueryFilter libraryShare = byNotebook(id).withCriteria(new QFilterCriteria("audienceKey", QCriteriaOperator.EQUALS, SqlWorkbenchSecurity.AUDIENCE_ALL));
boolean published = !QueryAction.execute(table(SqlWorkbenchNotebookShare.TABLE_NAME), libraryShare).isEmpty();
if(shared && !published)
{
InsertOutput output = new InsertAction().execute(new InsertInput(table(SqlWorkbenchNotebookShare.TABLE_NAME)).withRecordEntity(new SqlWorkbenchNotebookShare()
.withNotebookId(id)
.withAudienceType(ShareAudienceType.ALL.name())));
throwIfErrors(output.getRecords().get(0));
}
else if(!shared && published)
{
DeleteOutput output = new DeleteAction().execute(new DeleteInput(table(SqlWorkbenchNotebookShare.TABLE_NAME)).withQueryFilter(libraryShare));
throwIfErrors(output.getRecordsWithErrors());
}
return (get(id, caller));
}
@@ -258,25 +277,23 @@ public class NotebookService
*******************************************************************************/
public Map<String, Object> duplicate(Integer id, Caller caller) throws QException
{
return (SystemUser.call(() ->
prepare();
SqlWorkbenchNotebook source = readable(id, caller);
Map<String, Object> body = new LinkedHashMap<>();
body.put("title", truncate("Copy of " + source.getTitle(), 255));
body.put("description", source.getDescription());
body.put("defaultBackendName", source.getDefaultBackendName());
body.put("parameters", parseParameters(source.getParametersJson()));
List<Map<String, Object>> cells = new ArrayList<>();
for(SqlWorkbenchCell cell : NotebookVersioning.cells(config, id, null))
{
SqlWorkbenchNotebook source = readable(id, caller);
Map<String, Object> body = new LinkedHashMap<>();
body.put("title", truncate("Copy of " + source.getTitle(), 255));
body.put("description", source.getDescription());
body.put("defaultBackendName", source.getDefaultBackendName());
body.put("parameters", parseParameters(source.getParametersJson()));
List<Map<String, Object>> cells = new ArrayList<>();
for(SqlWorkbenchCell cell : cells(id))
{
Map<String, Object> view = cellView(cell);
view.put("cellKey", UUID.randomUUID().toString());
cells.add(view);
}
body.put("cells", cells);
body.put("comment", "Duplicated from notebook " + id);
return (create(body, caller));
}));
Map<String, Object> view = cellView(cell);
view.put("cellKey", UUID.randomUUID().toString());
cells.add(view);
}
body.put("cells", cells);
body.put("comment", "Duplicated from notebook " + id);
return (create(body, caller));
}
@@ -286,23 +303,21 @@ public class NotebookService
*******************************************************************************/
public List<Map<String, Object>> versions(Integer id, Caller caller) throws QException
{
return (SystemUser.call(() ->
prepare();
readable(id, caller);
QQueryFilter filter = byNotebook(id).withOrderBy(new QFilterOrderBy("versionNumber", false));
List<Map<String, Object>> result = new ArrayList<>();
for(QRecord record : QueryAction.execute(table(SqlWorkbenchNotebookVersion.TABLE_NAME), filter))
{
readable(id, caller);
QQueryFilter filter = byNotebook(id).withOrderBy(new QFilterOrderBy("versionNumber", false));
List<Map<String, Object>> result = new ArrayList<>();
for(QRecord record : QueryAction.execute(table(SqlWorkbenchNotebookVersion.TABLE_NAME), filter))
{
SqlWorkbenchNotebookVersion version = new SqlWorkbenchNotebookVersion(record);
Map<String, Object> view = new LinkedHashMap<>();
view.put("versionNumber", version.getVersionNumber());
view.put("savedByName", version.getSavedByName());
view.put("comment", version.getComment());
view.put("createDate", String.valueOf(version.getCreateDate()));
result.add(view);
}
return (result);
}));
SqlWorkbenchNotebookVersion version = new SqlWorkbenchNotebookVersion(record);
Map<String, Object> view = new LinkedHashMap<>();
view.put("versionNumber", version.getVersionNumber());
view.put("savedByName", version.getSavedByName());
view.put("comment", version.getComment());
view.put("createDate", String.valueOf(version.getCreateDate()));
result.add(view);
}
return (result);
}
@@ -312,18 +327,16 @@ public class NotebookService
*******************************************************************************/
public Map<String, Object> version(Integer id, Integer versionNumber, Caller caller) throws QException
{
return (SystemUser.call(() ->
{
readable(id, caller);
SqlWorkbenchNotebookVersion version = findVersion(id, versionNumber);
Map<String, Object> view = new LinkedHashMap<>();
view.put("versionNumber", version.getVersionNumber());
view.put("savedByName", version.getSavedByName());
view.put("comment", version.getComment());
view.put("createDate", String.valueOf(version.getCreateDate()));
view.put("notebook", parseJsonObject(version.getContentJson()));
return (view);
}));
prepare();
readable(id, caller);
SqlWorkbenchNotebookVersion version = findVersion(id, versionNumber);
Map<String, Object> view = new LinkedHashMap<>();
view.put("versionNumber", version.getVersionNumber());
view.put("savedByName", version.getSavedByName());
view.put("comment", version.getComment());
view.put("createDate", String.valueOf(version.getCreateDate()));
view.put("notebook", parseJsonObject(version.getContentJson()));
return (view);
}
@@ -333,14 +346,12 @@ public class NotebookService
*******************************************************************************/
public Map<String, Object> restore(Integer id, Integer versionNumber, Caller caller) throws QException
{
return (SystemUser.call(() ->
{
owned(id, caller);
Map<String, Object> content = parseJsonObject(findVersion(id, versionNumber).getContentJson());
Map<String, Object> body = new LinkedHashMap<>(content);
body.put("comment", "Restored version " + versionNumber);
return (update(id, body, caller));
}));
prepare();
owned(id, caller);
Map<String, Object> content = parseJsonObject(findVersion(id, versionNumber).getContentJson());
Map<String, Object> body = new LinkedHashMap<>(content);
body.put("comment", "Restored version " + versionNumber);
return (update(id, body, caller));
}
@@ -350,16 +361,14 @@ public class NotebookService
*******************************************************************************/
public List<Map<String, Object>> snapshots(Integer id, Caller caller) throws QException
{
return (SystemUser.call(() ->
prepare();
readable(id, caller);
List<Map<String, Object>> result = new ArrayList<>();
for(QRecord record : QueryAction.execute(table(SqlWorkbenchResultSnapshot.TABLE_NAME), byNotebook(id).withOrderBy(new QFilterOrderBy("id", false))))
{
readable(id, caller);
List<Map<String, Object>> result = new ArrayList<>();
for(QRecord record : QueryAction.execute(table(SqlWorkbenchResultSnapshot.TABLE_NAME), byNotebook(id).withOrderBy(new QFilterOrderBy("id", false))))
{
result.add(snapshotView(new SqlWorkbenchResultSnapshot(record)));
}
return (result);
}));
result.add(snapshotView(new SqlWorkbenchResultSnapshot(record)));
}
return (result);
}
@@ -369,38 +378,35 @@ public class NotebookService
*******************************************************************************/
public Map<String, Object> createSnapshot(Integer id, Map<String, Object> body, Caller caller, String sqlText) throws QException
{
return (SystemUser.call(() ->
{
owned(id, caller);
String executionUuid = (String) body.get("executionUuid");
ResultCache.Entry entry = runtime.getResultCache().get(executionUuid, caller.userId())
.orElseThrow(() -> new QNotFoundException("That result is no longer available; run the cell again, then pin it."));
CollectedResult result = entry.result();
int max = config.getSnapshotMaxRows();
List<List<Object>> rows = result.rows().size() > max ? result.rows().subList(0, max) : result.rows();
Map<String, Object> json = new LinkedHashMap<>();
json.put("columns", result.columns().stream().map(ColumnInfo::toMap).toList());
json.put("rows", rows);
SqlWorkbenchResultSnapshot snapshot = new SqlWorkbenchResultSnapshot()
.withNotebookId(id)
.withCellKey((String) body.get("cellKey"))
.withExecutionUuid(executionUuid)
.withLabel(truncate((String) body.get("label"), 255))
.withBackendName(entry.backendName())
.withSqlText(sqlText)
.withResultJson(JsonUtils.toJson(json))
.withRowCount(rows.size())
.withTruncated(result.truncated() || result.rows().size() > max)
.withCreatedByUserId(caller.userId())
.withCreateDate(runtime.getClock().instant());
InsertOutput output = new InsertAction().execute(new InsertInput(table(SqlWorkbenchResultSnapshot.TABLE_NAME)).withRecordEntity(snapshot));
throwIfErrors(output.getRecords().get(0));
snapshot.setId(output.getRecords().get(0).getValueInteger("id"));
return (snapshotView(snapshot));
}));
prepare();
owned(id, caller);
String executionUuid = (String) body.get("executionUuid");
ResultCache.Entry entry = runtime.getResultCache().get(executionUuid, caller.userId())
.orElseThrow(() -> new QNotFoundException("That result is no longer available; run the cell again, then pin it."));
CollectedResult result = entry.result();
int max = config.getSnapshotMaxRows();
List<List<Object>> rows = result.rows().size() > max ? result.rows().subList(0, max) : result.rows();
Map<String, Object> json = new LinkedHashMap<>();
json.put("columns", result.columns().stream().map(ColumnInfo::toMap).toList());
json.put("rows", rows);
SqlWorkbenchResultSnapshot snapshot = new SqlWorkbenchResultSnapshot()
.withNotebookId(id)
.withCellKey((String) body.get("cellKey"))
.withExecutionUuid(executionUuid)
.withLabel(truncate((String) body.get("label"), 255))
.withBackendName(entry.backendName())
.withSqlText(sqlText)
.withResultJson(JsonUtils.toJson(json))
.withRowCount(rows.size())
.withTruncated(result.truncated() || result.rows().size() > max)
.withCreatedByUserId(caller.userId())
.withCreateDate(runtime.getClock().instant());
InsertOutput output = new InsertAction().execute(new InsertInput(table(SqlWorkbenchResultSnapshot.TABLE_NAME))
.withFlag(SqlWorkbenchActionFlag.SNAPSHOT_PIN)
.withRecordEntity(snapshot));
throwIfErrors(output.getRecords().get(0));
snapshot.setId(output.getRecords().get(0).getValueInteger("id"));
return (snapshotView(snapshot));
}
@@ -410,28 +416,33 @@ public class NotebookService
*******************************************************************************/
public void deleteSnapshot(Integer id, Integer snapshotId, Caller caller) throws QException
{
SystemUser.run(() ->
prepare();
owned(id, caller);
QQueryFilter filter = byNotebook(id).withCriteria(new QFilterCriteria("id", QCriteriaOperator.EQUALS, snapshotId));
if(QueryAction.execute(table(SqlWorkbenchResultSnapshot.TABLE_NAME), filter).isEmpty())
{
owned(id, caller);
QQueryFilter filter = byNotebook(id).withCriteria(new QFilterCriteria("id", QCriteriaOperator.EQUALS, snapshotId));
if(QueryAction.execute(table(SqlWorkbenchResultSnapshot.TABLE_NAME), filter).isEmpty())
{
throw (new QNotFoundException("Snapshot not found."));
}
new DeleteAction().execute(new DeleteInput(table(SqlWorkbenchResultSnapshot.TABLE_NAME)).withPrimaryKeys(List.of(snapshotId)));
});
throw (new QNotFoundException("Snapshot not found."));
}
DeleteOutput output = new DeleteAction().execute(new DeleteInput(table(SqlWorkbenchResultSnapshot.TABLE_NAME)).withPrimaryKeys(List.of(snapshotId)));
throwIfErrors(output.getRecordsWithErrors());
}
/*******************************************************************************
** A notebook the caller may read, or 404 (never 403: personal notebooks of
** others do not exist for the caller).
** A notebook the caller may read, or 404 (never 403: notebooks not shared
** with the caller do not exist for them). The record locks already hide
** them; the owner / share check repeats that decision explicitly.
*******************************************************************************/
SqlWorkbenchNotebook readable(Integer id, Caller caller) throws QException
{
SqlWorkbenchNotebook notebook = find(id);
if(notebook == null || !(Objects.equals(notebook.getOwnerUserId(), caller.userId()) || VISIBILITY_SHARED.equals(notebook.getVisibility())))
if(notebook == null)
{
throw (new QNotFoundException("Notebook not found."));
}
boolean owner = Objects.equals(notebook.getOwnerUserId(), caller.userId());
if(!owner && !SqlWorkbenchSecurity.hasAllAccess(config) && !sharedWithCallerNotebookIds().contains(notebook.getId()))
{
throw (new QNotFoundException("Notebook not found."));
}
@@ -441,7 +452,7 @@ public class NotebookService
/*******************************************************************************
** A notebook the caller owns: 404 if unreadable, 403 if shared but not
** A notebook the caller owns: 404 if unreadable, 403 if readable but not
** theirs.
*******************************************************************************/
SqlWorkbenchNotebook owned(Integer id, Caller caller) throws QException
@@ -457,33 +468,37 @@ public class NotebookService
/*******************************************************************************
**
** The notebook, as the caller's session can see it (or null).
*******************************************************************************/
private SqlWorkbenchNotebook find(Integer id) throws QException
{
return (SystemUser.call(() ->
if(id == null)
{
if(id == null)
{
throw (new QBadRequestException("A notebook id is required."));
}
List<QRecord> records = QueryAction.execute(table(SqlWorkbenchNotebook.TABLE_NAME), new QQueryFilter(new QFilterCriteria("id", QCriteriaOperator.EQUALS, id)));
return (records.isEmpty() ? null : new SqlWorkbenchNotebook(records.get(0)));
}));
throw (new QBadRequestException("A notebook id is required."));
}
List<QRecord> records = QueryAction.execute(table(SqlWorkbenchNotebook.TABLE_NAME), new QQueryFilter(new QFilterCriteria("id", QCriteriaOperator.EQUALS, id)));
return (records.isEmpty() ? null : new SqlWorkbenchNotebook(records.get(0)));
}
/*******************************************************************************
**
** Ids of the notebooks shared with one of the session's audiences.
*******************************************************************************/
private List<SqlWorkbenchCell> cells(Integer notebookId) throws QException
private Set<Serializable> sharedWithCallerNotebookIds() throws QException
{
return (SystemUser.call(() ->
Set<Serializable> ids = new LinkedHashSet<>();
List<String> audienceKeys = SqlWorkbenchSecurity.sessionAudienceKeys(config);
if(audienceKeys.isEmpty())
{
QQueryFilter filter = byNotebook(notebookId).withOrderBy(new QFilterOrderBy("sortOrder")).withOrderBy(new QFilterOrderBy("id"));
return (QueryAction.execute(table(SqlWorkbenchCell.TABLE_NAME), SqlWorkbenchCell.class, filter));
}));
return (ids);
}
QQueryFilter filter = new QQueryFilter(new QFilterCriteria("audienceKey", QCriteriaOperator.IN, new ArrayList<>(audienceKeys)));
for(QRecord record : QueryAction.execute(table(SqlWorkbenchNotebookShare.TABLE_NAME), filter))
{
ids.add(record.getValueInteger("notebookId"));
}
return (ids);
}
@@ -493,16 +508,13 @@ public class NotebookService
*******************************************************************************/
private SqlWorkbenchNotebookVersion findVersion(Integer id, Integer versionNumber) throws QException
{
return (SystemUser.call(() ->
QQueryFilter filter = byNotebook(id).withCriteria(new QFilterCriteria("versionNumber", QCriteriaOperator.EQUALS, versionNumber));
List<QRecord> records = QueryAction.execute(table(SqlWorkbenchNotebookVersion.TABLE_NAME), filter);
if(records.isEmpty())
{
QQueryFilter filter = byNotebook(id).withCriteria(new QFilterCriteria("versionNumber", QCriteriaOperator.EQUALS, versionNumber));
List<QRecord> records = QueryAction.execute(table(SqlWorkbenchNotebookVersion.TABLE_NAME), filter);
if(records.isEmpty())
{
throw (new QNotFoundException("Version not found."));
}
return (new SqlWorkbenchNotebookVersion(records.get(0)));
}));
throw (new QNotFoundException("Version not found."));
}
return (new SqlWorkbenchNotebookVersion(records.get(0)));
}
@@ -518,81 +530,46 @@ public class NotebookService
/*******************************************************************************
** Write the notebook row (version + 1), replace its cells, append a
** version - all in the given transaction.
** Write the notebook's fields, replace its cells, append a version - all
** in the given transaction, as the caller (NOTEBOOK_SAVE tells the table
** customizers this is one save, versioned once at the end).
*******************************************************************************/
private void saveContent(SqlWorkbenchNotebook notebook, List<SqlWorkbenchCell> cells, Caller caller, String comment, QBackendTransaction transaction) throws QException
private void saveContent(SqlWorkbenchNotebook notebook, List<SqlWorkbenchCell> cells, String comment, QBackendTransaction transaction) throws QException
{
Instant now = runtime.getClock().instant();
int version = (notebook.getCurrentVersion() == null ? 0 : notebook.getCurrentVersion()) + 1;
notebook.setCurrentVersion(version);
notebook.setModifyDate(now);
UpdateOutput updated = new UpdateAction().execute(new UpdateInput(table(SqlWorkbenchNotebook.TABLE_NAME))
.withFlag(SqlWorkbenchActionFlag.NOTEBOOK_SAVE)
.withTransaction(transaction)
.withRecord(new QRecord()
.withValue("id", notebook.getId())
.withValue("title", notebook.getTitle())
.withValue("description", notebook.getDescription())
.withValue("defaultBackendName", notebook.getDefaultBackendName())
.withValue("parametersJson", notebook.getParametersJson())));
throwIfErrors(updated.getRecords().get(0));
new UpdateAction().execute(new UpdateInput(table(SqlWorkbenchNotebook.TABLE_NAME)).withTransaction(transaction).withRecord(new QRecord()
.withValue("id", notebook.getId())
.withValue("title", notebook.getTitle())
.withValue("description", notebook.getDescription())
.withValue("defaultBackendName", notebook.getDefaultBackendName())
.withValue("parametersJson", notebook.getParametersJson())
.withValue("currentVersion", version)
.withValue("modifyDate", now)));
DeleteOutput deleted = new DeleteAction().execute(new DeleteInput(table(SqlWorkbenchCell.TABLE_NAME))
.withFlag(SqlWorkbenchActionFlag.NOTEBOOK_SAVE)
.withQueryFilter(byNotebook(notebook.getId()))
.withTransaction(transaction));
throwIfErrors(deleted.getRecordsWithErrors());
new DeleteAction().execute(new DeleteInput(table(SqlWorkbenchCell.TABLE_NAME)).withQueryFilter(byNotebook(notebook.getId())).withTransaction(transaction));
for(int i = 0; i < cells.size(); i++)
{
cells.get(i).withId(null).withNotebookId(notebook.getId()).withSortOrder(i).withCreateDate(now).withModifyDate(now);
cells.get(i).withId(null).withNotebookId(notebook.getId()).withSortOrder(i);
}
if(!cells.isEmpty())
{
InsertOutput output = new InsertAction().execute(new InsertInput(table(SqlWorkbenchCell.TABLE_NAME)).withRecordEntities(cells).withTransaction(transaction));
InsertOutput output = new InsertAction().execute(new InsertInput(table(SqlWorkbenchCell.TABLE_NAME))
.withFlag(SqlWorkbenchActionFlag.NOTEBOOK_SAVE)
.withRecordEntities(cells)
.withTransaction(transaction));
for(QRecord record : output.getRecords())
{
throwIfErrors(record);
}
}
Map<String, Object> content = new LinkedHashMap<>();
content.put("title", notebook.getTitle());
content.put("description", notebook.getDescription());
content.put("defaultBackendName", notebook.getDefaultBackendName());
content.put("parameters", parseParameters(notebook.getParametersJson()));
content.put("cells", cells.stream().map(NotebookService::cellView).toList());
SqlWorkbenchNotebookVersion versionRow = new SqlWorkbenchNotebookVersion()
.withNotebookId(notebook.getId())
.withVersionNumber(version)
.withContentJson(JsonUtils.toJson(content))
.withSavedByUserId(caller.userId())
.withSavedByName(caller.userName())
.withComment(truncate(comment, 255))
.withCreateDate(now);
InsertOutput output = new InsertAction().execute(new InsertInput(table(SqlWorkbenchNotebookVersion.TABLE_NAME)).withRecordEntity(versionRow).withTransaction(transaction));
throwIfErrors(output.getRecords().get(0));
}
/*******************************************************************************
** Delete versions beyond the retention, oldest first.
*******************************************************************************/
private void pruneVersions(Integer id) throws QException
{
SystemUser.run(() ->
{
QueryInput input = new QueryInput(table(SqlWorkbenchNotebookVersion.TABLE_NAME))
.withFilter(byNotebook(id).withOrderBy(new QFilterOrderBy("versionNumber", false)));
List<QRecord> records = new QueryAction().execute(input).getRecords();
if(records.size() <= config.getNotebookVersionRetention())
{
return;
}
List<Serializable> stale = new ArrayList<>();
for(QRecord record : records.subList(config.getNotebookVersionRetention(), records.size()))
{
stale.add(record.getValueInteger("id"));
}
new DeleteAction().execute(new DeleteInput(table(SqlWorkbenchNotebookVersion.TABLE_NAME)).withPrimaryKeys(stale));
});
NotebookVersioning.appendVersion(config, runtime.getClock(), notebook.getId(), comment, transaction);
}
@@ -655,26 +632,22 @@ public class NotebookService
throw (new QBadRequestException("Each cell must be an object."));
}
Map<String, Object> cell = (Map<String, Object>) map;
String type = cell.get("cellType") == null ? CELL_SQL : cell.get("cellType").toString().toUpperCase();
if(!CELL_SQL.equals(type) && !CELL_MARKDOWN.equals(type))
{
throw (new QBadRequestException("cellType must be SQL or MARKDOWN."));
}
String content = cell.get("content") == null ? "" : cell.get("content").toString();
if(content.length() > config.getMaxSqlLength())
{
throw (new QBadRequestException("A cell is longer than the limit of " + config.getMaxSqlLength() + " characters."));
}
String key = cell.get("cellKey") == null ? null : cell.get("cellKey").toString();
if(!StringUtils.hasContent(key) || key.length() > 64 || !keys.add(key))
{
key = UUID.randomUUID().toString();
keys.add(key);
}
cells.add(new SqlWorkbenchCell()
.withCellKey(key)
.withCellType(type)
@@ -789,13 +762,32 @@ public class NotebookService
/*******************************************************************************
**
** Throw the first error of a record (a QPermissionDeniedException for
** permission errors, so the notebook UI answers 403).
*******************************************************************************/
private static void throwIfErrors(QRecord record) throws QException
static void throwIfErrors(QRecord record) throws QException
{
if(record.getErrors() != null && !record.getErrors().isEmpty())
if(record != null && CollectionUtils.nullSafeHasContents(record.getErrors()))
{
throw (new QException("Could not save: " + record.getErrors().get(0).getMessage()));
String message = record.getErrors().get(0).getMessage();
if(record.getErrors().get(0) instanceof com.kingsrook.qqq.backend.core.model.statusmessages.PermissionDeniedMessage)
{
throw (new QPermissionDeniedException(message));
}
throw (new QBadRequestException("Could not save: " + message));
}
}
/*******************************************************************************
** Throw the first error of any record.
*******************************************************************************/
static void throwIfErrors(List<QRecord> records) throws QException
{
for(QRecord record : CollectionUtils.nonNullList(records))
{
throwIfErrors(record);
}
}
@@ -821,5 +813,4 @@ public class NotebookService
{
return (value == null || value.length() <= max ? value : value.substring(0, max - 1) + "…");
}
}
@@ -0,0 +1,200 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.notebook;
import java.io.Serializable;
import java.time.Clock;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchSecurity;
import com.kingsrook.qbits.sqlworkbench.SystemUser;
import com.kingsrook.qbits.sqlworkbench.customizers.SqlWorkbenchActionFlag;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookVersion;
import com.kingsrook.qqq.backend.core.actions.QBackendTransaction;
import com.kingsrook.qqq.backend.core.actions.tables.DeleteAction;
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
import com.kingsrook.qqq.backend.core.actions.tables.QueryAction;
import com.kingsrook.qqq.backend.core.actions.tables.UpdateAction;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateOutput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.core.utils.JsonUtils;
/*******************************************************************************
** Notebook versions: every change to a notebook's content (from the
** notebook UI or from the table UI / API) appends one immutable version
** holding the whole notebook, and bumps the notebook's currentVersion.
**
** The caller's change has already passed the record locks (only the owner,
** or an admin, gets here); the bookkeeping itself is written as the system
** user, because versions are append-only for everyone else.
*******************************************************************************/
public final class NotebookVersioning
{
/*******************************************************************************
**
*******************************************************************************/
private NotebookVersioning()
{
}
/*******************************************************************************
** Append a version of the notebook as stored now (in the transaction, if
** any), saved by the current session's user. Returns the new version
** number, or null if the notebook does not exist.
*******************************************************************************/
public static Integer appendVersion(SqlWorkbenchQBitConfig config, Clock clock, Integer notebookId, String comment, QBackendTransaction transaction) throws QException
{
QSession session = QContext.getQSession();
String userId = SqlWorkbenchSecurity.userId(session);
String userName = SqlWorkbenchSecurity.userName(session);
return (SystemUser.call(() ->
{
List<QRecord> found = new QueryAction().execute(new QueryInput(config.applyPrefix(SqlWorkbenchNotebook.TABLE_NAME))
.withFilter(new QQueryFilter(new QFilterCriteria("id", QCriteriaOperator.EQUALS, notebookId)))
.withTransaction(transaction)).getRecords();
if(found.isEmpty())
{
return (null);
}
SqlWorkbenchNotebook notebook = new SqlWorkbenchNotebook(found.get(0));
Instant now = clock.instant();
///////////////////////////////////////////////////////////////////////
// next number: past both currentVersion and the newest stored //
// version, so a stale currentVersion can never wedge later saves //
// on the (notebookId, versionNumber) unique key //
///////////////////////////////////////////////////////////////////////
int latest = notebook.getCurrentVersion() == null ? 0 : notebook.getCurrentVersion();
List<QRecord> newest = new QueryAction().execute(new QueryInput(config.applyPrefix(SqlWorkbenchNotebookVersion.TABLE_NAME))
.withFilter(new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.EQUALS, notebookId)).withOrderBy(new QFilterOrderBy("versionNumber", false)).withLimit(1))
.withTransaction(transaction)).getRecords();
if(!newest.isEmpty() && newest.get(0).getValueInteger("versionNumber") != null)
{
latest = Math.max(latest, newest.get(0).getValueInteger("versionNumber"));
}
int version = latest + 1;
UpdateOutput updated = new UpdateAction().execute(new UpdateInput(config.applyPrefix(SqlWorkbenchNotebook.TABLE_NAME))
.withFlag(SqlWorkbenchActionFlag.INTERNAL)
.withTransaction(transaction)
.withRecord(new QRecord().withValue("id", notebookId).withValue("currentVersion", version).withValue("modifyDate", now)));
NotebookService.throwIfErrors(updated.getRecords().get(0));
SqlWorkbenchNotebookVersion versionRow = new SqlWorkbenchNotebookVersion()
.withNotebookId(notebookId)
.withVersionNumber(version)
.withContentJson(JsonUtils.toJson(content(notebook, cells(config, notebookId, transaction))))
.withSavedByUserId(userId)
.withSavedByName(userName)
.withComment(NotebookService.truncate(comment, 255))
.withCreateDate(now);
InsertOutput output = new InsertAction().execute(new InsertInput(config.applyPrefix(SqlWorkbenchNotebookVersion.TABLE_NAME))
.withFlag(SqlWorkbenchActionFlag.INTERNAL)
.withTransaction(transaction)
.withRecordEntity(versionRow));
NotebookService.throwIfErrors(output.getRecords().get(0));
prune(config, notebookId, transaction);
return (version);
}));
}
/*******************************************************************************
** A notebook's cells, in order (as the current session).
*******************************************************************************/
static List<SqlWorkbenchCell> cells(SqlWorkbenchQBitConfig config, Integer notebookId, QBackendTransaction transaction) throws QException
{
QQueryFilter filter = new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.EQUALS, notebookId))
.withOrderBy(new QFilterOrderBy("sortOrder"))
.withOrderBy(new QFilterOrderBy("id"));
List<SqlWorkbenchCell> cells = new ArrayList<>();
for(QRecord record : new QueryAction().execute(new QueryInput(config.applyPrefix(SqlWorkbenchCell.TABLE_NAME)).withFilter(filter).withTransaction(transaction)).getRecords())
{
cells.add(new SqlWorkbenchCell(record));
}
return (cells);
}
/*******************************************************************************
** The content a version holds.
*******************************************************************************/
static Map<String, Object> content(SqlWorkbenchNotebook notebook, List<SqlWorkbenchCell> cells)
{
Map<String, Object> content = new LinkedHashMap<>();
content.put("title", notebook.getTitle());
content.put("description", notebook.getDescription());
content.put("defaultBackendName", notebook.getDefaultBackendName());
content.put("parameters", NotebookService.parseParameters(notebook.getParametersJson()));
content.put("cells", cells.stream().map(NotebookService::cellView).toList());
return (content);
}
/*******************************************************************************
** Delete versions beyond the retention, oldest first (system user).
*******************************************************************************/
private static void prune(SqlWorkbenchQBitConfig config, Integer notebookId, QBackendTransaction transaction) throws QException
{
QueryInput input = new QueryInput(config.applyPrefix(SqlWorkbenchNotebookVersion.TABLE_NAME))
.withFilter(new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.EQUALS, notebookId)).withOrderBy(new QFilterOrderBy("versionNumber", false)))
.withTransaction(transaction);
List<QRecord> records = new QueryAction().execute(input).getRecords();
if(records.size() <= config.getNotebookVersionRetention())
{
return;
}
List<Serializable> stale = new ArrayList<>();
for(QRecord record : records.subList(config.getNotebookVersionRetention(), records.size()))
{
stale.add(record.getValueInteger("id"));
}
new DeleteAction().execute(new DeleteInput(config.applyPrefix(SqlWorkbenchNotebookVersion.TABLE_NAME))
.withFlag(SqlWorkbenchActionFlag.INTERNAL)
.withTransaction(transaction)
.withPrimaryKeys(stale));
}
}
@@ -0,0 +1,163 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.processes;
import java.util.Map;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchRuntime;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchSecurity;
import com.kingsrook.qbits.sqlworkbench.engine.Caller;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qbits.sqlworkbench.notebook.NotebookService;
import com.kingsrook.qqq.backend.core.actions.processes.BackendStep;
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
import com.kingsrook.qqq.backend.core.model.actions.processes.RunBackendStepInput;
import com.kingsrook.qqq.backend.core.model.actions.processes.RunBackendStepOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertOutput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
/*******************************************************************************
** The backend steps of the workbench's record actions. QQQ loads the
** selected records through the table's record locks (as the session), so a
** step only ever sees rows the user can read; the writes then go through
** the same table actions and customizers as everything else.
*******************************************************************************/
public final class SqlWorkbenchRecordActionSteps
{
public static final String RESULT_MESSAGE = "resultMessage";
/*******************************************************************************
**
*******************************************************************************/
private SqlWorkbenchRecordActionSteps()
{
}
/*******************************************************************************
** The single selected record (min/max input records are 1).
*******************************************************************************/
static QRecord selected(RunBackendStepInput input) throws QException
{
if(CollectionUtils.nullSafeIsEmpty(input.getRecords()))
{
throw (new QUserFacingException("The selected record was not found."));
}
return (input.getRecords().get(0));
}
/*******************************************************************************
**
*******************************************************************************/
static NotebookService notebooks() throws QException
{
return (new NotebookService(SqlWorkbenchRuntime.get()));
}
/*******************************************************************************
**
*******************************************************************************/
static Caller caller() throws QException
{
SqlWorkbenchQBitConfig config = SqlWorkbenchRuntime.get().getConfig();
SqlWorkbenchSecurity.ensureSessionKeys(config, QContext.getQSession());
return (Caller.fromSession(config, QContext.getQSession(), null));
}
/*******************************************************************************
** Share the selected notebook with a user, a group, or everyone (the
** shared library): inserts a share row as the session.
*******************************************************************************/
public static class ShareNotebookStep implements BackendStep
{
/*******************************************************************************
**
*******************************************************************************/
@Override
public void run(RunBackendStepInput input, RunBackendStepOutput output) throws QException
{
QRecord notebook = selected(input);
SqlWorkbenchQBitConfig config = SqlWorkbenchRuntime.get().getConfig();
InsertOutput inserted = new InsertAction().execute(new InsertInput(config.applyPrefix(SqlWorkbenchNotebookShare.TABLE_NAME)).withRecordEntity(new SqlWorkbenchNotebookShare()
.withNotebookId(notebook.getValueInteger("id"))
.withAudienceType(input.getValueString("audienceType"))
.withAudienceId(input.getValueString("audienceId"))));
QRecord share = inserted.getRecords().get(0);
if(CollectionUtils.nullSafeHasContents(share.getErrors()))
{
throw (new QUserFacingException(share.getErrors().get(0).getMessage()));
}
output.addValue(RESULT_MESSAGE, "Shared \"" + notebook.getValueString("title") + "\" (" + share.getValueString("audienceKey") + ").");
}
}
/*******************************************************************************
** Copy the selected (readable) notebook into a new notebook the session
** owns.
*******************************************************************************/
public static class DuplicateNotebookStep implements BackendStep
{
/*******************************************************************************
**
*******************************************************************************/
@Override
public void run(RunBackendStepInput input, RunBackendStepOutput output) throws QException
{
QRecord notebook = selected(input);
Map<String, Object> copy = notebooks().duplicate(notebook.getValueInteger("id"), caller());
output.addValue(RESULT_MESSAGE, "Created \"" + copy.get("title") + "\" (notebook " + copy.get("id") + ").");
}
}
/*******************************************************************************
** Restore the selected version of a notebook the session owns (saved as
** a new version).
*******************************************************************************/
public static class RestoreVersionStep implements BackendStep
{
/*******************************************************************************
**
*******************************************************************************/
@Override
public void run(RunBackendStepInput input, RunBackendStepOutput output) throws QException
{
QRecord version = selected(input);
Map<String, Object> restored = notebooks().restore(version.getValueInteger("notebookId"), version.getValueInteger("versionNumber"), caller());
output.addValue(RESULT_MESSAGE, "Restored version " + version.getValueInteger("versionNumber") + " as version " + restored.get("currentVersion") + ".");
}
}
}
@@ -203,15 +203,28 @@ public class BaseSqlWorkbenchTest
/*******************************************************************************
** Init QContext with a session for one of the test users.
*******************************************************************************/
protected QSession loginAs(String userId)
protected QSession loginAs(String userId, String... groups)
{
QSession session = TestSessionCustomizer.sessionFor(userId);
QContext.init(qInstance, session);
SqlWorkbenchSecurity.applyToSession(config, session, List.of(groups));
return (session);
}
/*******************************************************************************
** Log in as a user and return their Caller (the session and the Caller
** must agree: notebook operations run as the session).
*******************************************************************************/
protected Caller as(String userId, String... groups)
{
loginAs(userId, groups);
return (caller(userId));
}
/*******************************************************************************
** The caller for a test user (permissions as TestSessionCustomizer grants).
*******************************************************************************/
@@ -240,6 +253,14 @@ public class BaseSqlWorkbenchTest
QSession granted = sessionFor(qSession.getUuid());
qSession.setUser(granted.getUser());
qSession.setPermissions(granted.getPermissions());
try
{
SqlWorkbenchSecurity.applyToSession(qInstance, qSession, List.of());
}
catch(Exception e)
{
throw (new IllegalStateException("SQL Workbench is not installed", e));
}
}
@@ -249,20 +270,21 @@ public class BaseSqlWorkbenchTest
*******************************************************************************/
public static QSession sessionFor(String userId)
{
QSession session = new QSession().withUser(new QUser().withIdReference(userId).withFullName("Name of " + userId));
SqlWorkbenchQBitConfig defaults = new SqlWorkbenchQBitConfig();
QSession session = new QSession().withUser(new QUser().withIdReference(userId).withFullName("Name of " + userId));
session.setPermissions(new java.util.HashSet<>());
if(userId != null && (userId.startsWith("reader") || userId.startsWith("writer") || userId.startsWith("admin")))
if(userId != null && (userId.startsWith("reader") || userId.startsWith("writer")))
{
session.withPermission(SqlWorkbenchQBitConfig.DEFAULT_ACCESS_PERMISSION_BASE_NAME + ".hasAccess");
session.getPermissions().addAll(defaults.getUserPermissionNames());
}
if(userId != null && userId.startsWith("admin"))
{
session.withPermission(SqlWorkbenchQBitConfig.DEFAULT_ADMIN_PERMISSION_BASE_NAME + ".hasAccess");
session.getPermissions().addAll(defaults.getAdminPermissionNames());
}
if(userId != null && userId.startsWith("writer"))
{
session.withPermission(SqlWorkbenchQBitConfig.DEFAULT_WRITE_PERMISSION_NAME);
session.withPermission(SqlWorkbenchQBitConfig.DEFAULT_SHARE_PERMISSION_NAME);
session.getPermissions().addAll(defaults.getSharePermissionNames());
}
return (session);
}
@@ -23,6 +23,7 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@@ -41,12 +42,29 @@ import com.kingsrook.qbits.sqlworkbench.engine.SessionGuard;
import com.kingsrook.qbits.sqlworkbench.engine.SqlExecutionEngine;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchExecution;
import com.kingsrook.qbits.sqlworkbench.notebook.NotebookService;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.actions.tables.count.CountInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.actions.tables.CountAction;
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qbits.sqlworkbench.schema.SchemaIntrospector;
import com.kingsrook.qbits.sqlworkbench.sql.SqlDialect;
import com.kingsrook.qqq.backend.module.postgres.model.metadata.PostgreSQLBackendMetaData;
import com.kingsrook.qqq.backend.module.rdbms.jdbc.C3P0PooledConnectionProvider;
import com.kingsrook.qqq.backend.module.rdbms.jdbc.ConnectionManager;
import com.kingsrook.qqq.backend.module.rdbms.model.metadata.ConnectionPoolSettings;
import com.kingsrook.qbits.sqlworkbench.metadata.SqlWorkbenchHomeWidgetsMetaDataProducer;
import com.kingsrook.qqq.backend.core.actions.dashboard.RenderWidgetAction;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.ChartData;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.QWidgetData;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.RawHTML;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.TableData;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaData;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
@@ -117,7 +135,7 @@ class PostgresIntegrationTest
try(Connection connection = java.sql.DriverManager.getConnection(POSTGRES.getJdbcUrl(), POSTGRES.getUsername(), POSTGRES.getPassword());
Statement statement = connection.createStatement())
{
statement.execute("TRUNCATE sql_workbench_result_snapshot, sql_workbench_notebook_version, sql_workbench_cell, sql_workbench_notebook, sql_workbench_execution");
statement.execute("TRUNCATE sql_workbench_notebook_share, sql_workbench_result_snapshot, sql_workbench_notebook_version, sql_workbench_cell, sql_workbench_notebook, sql_workbench_execution");
statement.execute("DROP TABLE IF EXISTS scratch");
}
@@ -231,7 +249,7 @@ class PostgresIntegrationTest
statement.execute("DELETE FROM person WHERE first_name = 'after'");
}
List<SqlWorkbenchExecution> audits = QueryAction.execute(SqlWorkbenchExecution.TABLE_NAME, SqlWorkbenchExecution.class, new QQueryFilter());
List<SqlWorkbenchExecution> audits = SystemUser.call(() -> QueryAction.execute(SqlWorkbenchExecution.TABLE_NAME, SqlWorkbenchExecution.class, new QQueryFilter()));
assertThat(audits).extracting(SqlWorkbenchExecution::getStatus).contains("SUCCEEDED", "TIMED_OUT");
}
@@ -385,4 +403,93 @@ class PostgresIntegrationTest
}
}
/*******************************************************************************
** The record locks on PostgreSQL: owner / user share / library reads
** through the core actions, one row per notebook even when it matches
** several shares (SELECT DISTINCT / COUNT(DISTINCT) over TEXT columns),
** and a reader cannot write a shared notebook's children.
*******************************************************************************/
@Test
void testRecordLocksOnPostgres() throws Exception
{
NotebookService notebooks = new NotebookService(SqlWorkbenchRuntime.get(qInstance));
login("writer-1");
Integer both = (Integer) notebooks.create(Map.of("title", "Both", "description", "long text", "cells", List.of(Map.of("cellType", "SQL", "content", "SELECT 1"))), writer()).get("id");
Integer personal = (Integer) notebooks.create(Map.of("title", "Personal", "cells", List.of(Map.of("cellType", "SQL", "content", "SELECT 2"))), writer()).get("id");
notebooks.publish(both, true, writer());
QRecord share = new InsertAction().execute(new InsertInput(SqlWorkbenchNotebookShare.TABLE_NAME).withRecord(new QRecord()
.withValue("notebookId", both).withValue("audienceType", "USER").withValue("audienceId", "reader-2"))).getRecords().get(0);
assertThat(share.getErrors()).isNullOrEmpty();
login("reader-2");
assertThat(QueryAction.execute(SqlWorkbenchNotebook.TABLE_NAME, new QQueryFilter())).extracting(r -> r.getValueInteger("id")).containsExactly(both);
assertThat(new CountAction().execute(new CountInput(SqlWorkbenchNotebook.TABLE_NAME)).getCount()).isEqualTo(1);
assertThat(QueryAction.execute(SqlWorkbenchCell.TABLE_NAME, new QQueryFilter())).hasSize(1);
assertThat(new CountAction().execute(new CountInput(SqlWorkbenchCell.TABLE_NAME)).getCount()).isEqualTo(1);
assertThat(QueryAction.execute(SqlWorkbenchNotebookShare.TABLE_NAME, new QQueryFilter())).hasSize(2);
assertThat(new InsertAction().execute(new InsertInput(SqlWorkbenchCell.TABLE_NAME).withRecord(new QRecord().withValue("notebookId", both).withValue("content", "x")))
.getRecords().get(0).getErrors()).isNotEmpty();
assertThat(new InsertAction().execute(new InsertInput(SqlWorkbenchCell.TABLE_NAME).withRecord(new QRecord().withValue("notebookId", personal).withValue("content", "x")))
.getRecords().get(0).getErrors()).isNotEmpty();
login("admin-1");
assertThat(new CountAction().execute(new CountInput(SqlWorkbenchNotebook.TABLE_NAME)).getCount()).isEqualTo(2);
login("writer-1");
notebooks.delete(both, writer());
assertThat(new CountAction().execute(new CountInput(SqlWorkbenchNotebookShare.TABLE_NAME)).getCount()).isZero();
}
/*******************************************************************************
** The home dashboard over real engine runs on Postgres (aggregates, IN
** filters and timestamps through the RDBMS module): a reader sees their
** own runs, an admin everyone's, including the committed write.
*******************************************************************************/
@Test
void testHomeDashboardOnPostgres() throws Exception
{
NotebookService notebooks = new NotebookService(SqlWorkbenchRuntime.get(qInstance));
login("writer-1");
Integer id = (Integer) notebooks.create(Map.of("title", "Dashboard source", "cells", List.of(Map.of("cellType", "SQL", "content", "SELECT 1"))), writer()).get("id");
engine.execute(new ExecutionRequest(BACKEND, "SELECT count(*) FROM person", Map.of(), false, id, null, null, null, false), writer());
String sql = "UPDATE person SET salary = salary + 0 WHERE salary IS NOT NULL";
Map<String, Object> preview = engine.execute(new ExecutionRequest(BACKEND, sql, Map.of(), true, null, null, null, null, false), writer());
engine.execute(new ExecutionRequest(BACKEND, sql, Map.of(), true, null, null, null, (String) preview.get("confirmToken"), false), writer());
login("reader-1");
engine.execute(new ExecutionRequest(BACKEND, "SELECT broken FROM nowhere", Map.of(), false, null, null, null, null, false), reader());
login("admin-1");
Map<String, String> html = new HashMap<>();
for(String name : SqlWorkbenchHomeWidgetsMetaDataProducer.homeWidgetNames())
{
QWidgetData data = new RenderWidgetAction().execute(new RenderWidgetInput().withWidgetMetaData((QWidgetMetaData) qInstance.getWidget(name))).getWidgetData();
html.put(name, data instanceof TableData table ? table.getRows().toString() : data instanceof RawHTML raw ? raw.getHtml() : String.valueOf(((ChartData) data).getSublabel()));
}
assertThat(html.get(SqlWorkbenchHomeWidgetsMetaDataProducer.WRITES)).contains("UPDATE person", "<strong>2</strong>");
assertThat(html.get(SqlWorkbenchHomeWidgetsMetaDataProducer.FAILED)).contains("SELECT broken FROM nowhere");
assertThat(html.get(SqlWorkbenchHomeWidgetsMetaDataProducer.MOST_USED)).contains("Dashboard source");
assertThat(html.get(SqlWorkbenchHomeWidgetsMetaDataProducer.BY_USER)).contains("writer-1", "reader-1");
assertThat(html.get(SqlWorkbenchHomeWidgetsMetaDataProducer.SUMMARY)).contains("Writes, 30 Days", "2 rows affected");
login("reader-1");
TableData recent = (TableData) new RenderWidgetAction().execute(new RenderWidgetInput()
.withWidgetMetaData((QWidgetMetaData) qInstance.getWidget(SqlWorkbenchHomeWidgetsMetaDataProducer.RECENT_RUNS))).getWidgetData();
assertThat(recent.getRows().toString()).contains("SELECT broken").doesNotContain("UPDATE person");
}
/*******************************************************************************
** Log in with the workbench security keys.
*******************************************************************************/
private void login(String userId)
{
QSession session = BaseSqlWorkbenchTest.TestSessionCustomizer.sessionFor(userId);
QContext.init(qInstance, session);
SqlWorkbenchSecurity.applyToSession(config, session, List.of());
}
}
@@ -22,17 +22,26 @@ import java.util.List;
import java.util.Map;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.layout.QAppMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.permissions.PermissionLevel;
import com.kingsrook.qqq.backend.core.model.metadata.processes.QProcessMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.modules.backend.implementations.memory.MemoryBackendModule;
import com.kingsrook.qbits.sqlworkbench.metadata.SqlWorkbenchAppMetaDataProducer;
import com.kingsrook.qbits.sqlworkbench.metadata.SqlWorkbenchHomeWidgetsMetaDataProducer;
import com.kingsrook.qbits.sqlworkbench.metadata.SqlWorkbenchRunnerAppMetaDataProducer;
import com.kingsrook.qbits.sqlworkbench.metadata.SqlWorkbenchWidgetMetaDataProducer;
import com.kingsrook.qbits.sqlworkbench.metadata.SqlWorkbenchWidgetRenderer;
import com.kingsrook.qbits.sqlworkbench.model.ShareAudienceType;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchExecution;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookVersion;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchResultSnapshot;
import com.kingsrook.qqq.backend.module.rdbms.model.metadata.RDBMSTableBackendDetails;
import com.kingsrook.qqq.middleware.javalin.QJavalinMetaData;
import org.junit.jupiter.api.Test;
@@ -58,33 +67,70 @@ class SqlWorkbenchQBitProducerTest extends BaseSqlWorkbenchTest
assertThat(q.getArtifactId()).isEqualTo("sql-workbench");
});
for(String name : List.of("sqlWorkbenchNotebook", "sqlWorkbenchCell", "sqlWorkbenchNotebookVersion", "sqlWorkbenchResultSnapshot", "sqlWorkbenchExecution"))
////////////////////////////////////////////////////////////////////////////
// name -> {insert, update, delete} capabilities: notebooks, cells and //
// shares are fully editable; pinned results can be relabeled / deleted //
// but only pinned from the notebook UI; versions and the audit are //
// append-only. Every table is a standard per-table-permission table //
// with a record security lock, and none is hidden. //
////////////////////////////////////////////////////////////////////////////
Map<String, List<Boolean>> capabilities = new java.util.LinkedHashMap<>();
capabilities.put("sql_workbench_notebook", List.of(true, true, true));
capabilities.put("sql_workbench_cell", List.of(true, true, true));
capabilities.put("sql_workbench_notebook_share", List.of(true, true, true));
capabilities.put("sql_workbench_result_snapshot", List.of(false, true, true));
capabilities.put("sql_workbench_notebook_version", List.of(false, false, false));
capabilities.put("sql_workbench_execution", List.of(false, false, false));
for(Map.Entry<String, List<Boolean>> entry : capabilities.entrySet())
{
String name = entry.getKey();
QTableMetaData table = qInstance.getTable(name);
assertThat(table).as(name).isNotNull();
assertThat(table.getBackendName()).isEqualTo(BACKEND_NAME);
assertThat(table.getPermissionRules().getLevel()).isEqualTo(PermissionLevel.HAS_ACCESS_PERMISSION);
assertThat(table.getPermissionRules().getPermissionBaseName()).as("tables need the admin permission, not plain access").isEqualTo("sqlWorkbenchAdmin");
boolean writable = name.equals("sqlWorkbenchNotebook");
assertThat(table.isCapabilityEnabled(qInstance.getBackend(BACKEND_NAME), com.kingsrook.qqq.backend.core.model.metadata.tables.Capability.TABLE_INSERT)).as(name).isEqualTo(writable);
assertThat(table.isCapabilityEnabled(qInstance.getBackend(BACKEND_NAME), com.kingsrook.qqq.backend.core.model.metadata.tables.Capability.TABLE_UPDATE)).as(name).isEqualTo(writable);
assertThat(table.isCapabilityEnabled(qInstance.getBackend(BACKEND_NAME), com.kingsrook.qqq.backend.core.model.metadata.tables.Capability.TABLE_DELETE)).as(name).isEqualTo(writable);
assertThat(table.getIsHidden()).as(name).isFalse();
assertThat(table.getPermissionRules().getLevel()).as(name).isEqualTo(PermissionLevel.READ_INSERT_EDIT_DELETE_PERMISSIONS);
assertThat(table.getRecordSecurityLocks()).as(name).isNotEmpty();
com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData backend = qInstance.getBackend(BACKEND_NAME);
assertThat(table.isCapabilityEnabled(backend, com.kingsrook.qqq.backend.core.model.metadata.tables.Capability.TABLE_INSERT)).as(name + " insert").isEqualTo(entry.getValue().get(0));
assertThat(table.isCapabilityEnabled(backend, com.kingsrook.qqq.backend.core.model.metadata.tables.Capability.TABLE_UPDATE)).as(name + " update").isEqualTo(entry.getValue().get(1));
assertThat(table.isCapabilityEnabled(backend, com.kingsrook.qqq.backend.core.model.metadata.tables.Capability.TABLE_DELETE)).as(name + " delete").isEqualTo(entry.getValue().get(2));
assertThat(table.isCapabilityEnabled(backend, com.kingsrook.qqq.backend.core.model.metadata.tables.Capability.TABLE_EXPORT)).as(name + " export").isTrue();
assertThat(((RDBMSTableBackendDetails) table.getBackendDetails()).getTableName()).startsWith("sql_workbench_");
assertThat(table.getSourceQBitName()).isNotBlank();
}
assertThat(qInstance.getSecurityKeyType("sql_workbench_user").getAllAccessKeyName()).isEqualTo("sql_workbench_all_access");
assertThat(qInstance.getSecurityKeyType("sql_workbench_audience")).isNotNull();
////////////////////////////////////////////////////////////////////////
// role-management UIs list available permissions without a QContext //
// instance; the record actions must not break that //
////////////////////////////////////////////////////////////////////////
QContext.clear();
assertThat(com.kingsrook.qqq.backend.core.actions.permissions.PermissionsHelper.getAllAvailablePermissionNames(qInstance))
.contains("sql_workbench_notebook.read", "sql_workbench_notebook_share.insert", "sql_workbench.hasAccess")
.doesNotContain("sql_workbench_execution.delete", "sql_workbench_notebook_version.insert");
loginAs(READER);
QWidgetMetaData widget = (QWidgetMetaData) qInstance.getWidget(SqlWorkbenchWidgetMetaDataProducer.NAME);
assertThat(widget.getType()).isEqualTo("SQL_WORKBENCH");
QAppMetaData app = qInstance.getApp(SqlWorkbenchAppMetaDataProducer.APP_NAME);
assertThat(app.getWidgets()).containsExactly(SqlWorkbenchWidgetMetaDataProducer.NAME);
assertThat(app.getChildren()).extracting(c -> c.getName()).containsExactly(SqlWorkbenchNotebook.TABLE_NAME, SqlWorkbenchExecution.TABLE_NAME);
assertThat(app.getPermissionRules().getPermissionBaseName()).isEqualTo("sqlWorkbench");
//////////////////////////////////////////////////////////////////
// the qbit produces no processes (only QQQ's generic bulk ones //
// are added to its tables by the enricher) //
//////////////////////////////////////////////////////////////////
assertThat(qInstance.getProcesses() == null ? Map.of() : qInstance.getProcesses()).allSatisfy((name, process) -> assertThat(name).contains("."));
assertThat(app.getWidgets()).containsExactlyElementsOf(SqlWorkbenchHomeWidgetsMetaDataProducer.homeWidgetNames());
assertThat(app.getWidgets()).allSatisfy(name -> assertThat(qInstance.getWidget(name)).isNotNull());
assertThat(app.getPermissionRules().getPermissionBaseName()).isEqualTo("sql_workbench");
////////////////////////////////////////////////////////////////////////
// nav: the Runner (a child app showing the notebook widget), then the //
// main tables only; the qbit's processes are record actions, never //
// nav children //
////////////////////////////////////////////////////////////////////////
assertThat(app.getChildren()).extracting(c -> c.getName()).containsExactly("sqlWorkbenchRunner", "sql_workbench_notebook", "sql_workbench_execution");
QAppMetaData runner = qInstance.getApp(SqlWorkbenchRunnerAppMetaDataProducer.APP_NAME);
assertThat(runner.getLabel()).isEqualTo("Runner");
assertThat(runner.getWidgets()).containsExactly(SqlWorkbenchWidgetMetaDataProducer.NAME);
assertThat(runner.getChildren()).isNullOrEmpty();
assertThat(runner.getPermissionRules().getPermissionBaseName()).isEqualTo("sql_workbench");
assertThat(app.getChildren()).noneMatch(child -> child instanceof QProcessMetaData);
assertThat(QJavalinMetaData.of(qInstance).getAdditionalRouteProviderReferences()).isNotEmpty();
assertThat(SqlWorkbenchRuntime.get(qInstance)).isNotNull();
@@ -106,8 +152,8 @@ class SqlWorkbenchQBitProducerTest extends BaseSqlWorkbenchTest
SqlWorkbenchQBitConfig prefixed = new SqlWorkbenchQBitConfig().withBackendName("other").withRegisterRoutes(false);
new SqlWorkbenchQBitProducer().withQBitConfig(prefixed).withNamespace("ns").produce(instance);
assertThat(instance.getTable("ns_sqlWorkbenchNotebook")).isNotNull();
assertThat(instance.getTable("ns_sqlWorkbenchCell").getField("notebookId").getPossibleValueSourceName()).isEqualTo("ns_sqlWorkbenchNotebook");
assertThat(instance.getTable("ns_sql_workbench_notebook")).isNotNull();
assertThat(instance.getTable("ns_sql_workbench_cell").getField("notebookId").getPossibleValueSourceName()).isEqualTo("ns_sql_workbench_notebook");
assertThat(instance.getApp("ns_sqlWorkbench")).isNotNull();
assertThat(QJavalinMetaData.of(instance)).isNull();
@@ -122,6 +168,65 @@ class SqlWorkbenchQBitProducerTest extends BaseSqlWorkbenchTest
/*******************************************************************************
** Owner naming rule (1.0.0-RC.3): table, possible-value source, permission
** and security key names are lowercase snake_case prefixed with the qbit's
** name; the pre-RC.3 names stay available only as deprecated constants.
*******************************************************************************/
@Test
@SuppressWarnings("deprecation")
void testSnakeCaseNamesAndLegacyConstants() throws Exception
{
List<String> tableNames = List.of(SqlWorkbenchNotebook.TABLE_NAME, SqlWorkbenchCell.TABLE_NAME, SqlWorkbenchNotebookVersion.TABLE_NAME,
SqlWorkbenchResultSnapshot.TABLE_NAME, SqlWorkbenchNotebookShare.TABLE_NAME, SqlWorkbenchExecution.TABLE_NAME);
assertThat(tableNames).containsExactly("sql_workbench_notebook", "sql_workbench_cell", "sql_workbench_notebook_version",
"sql_workbench_result_snapshot", "sql_workbench_notebook_share", "sql_workbench_execution");
assertThat(tableNames).allSatisfy(name -> assertThat(name).matches("sql_workbench_[a-z_]+"));
assertThat(List.of(SqlWorkbenchNotebook.LEGACY_TABLE_NAME, SqlWorkbenchCell.LEGACY_TABLE_NAME, SqlWorkbenchNotebookVersion.LEGACY_TABLE_NAME,
SqlWorkbenchResultSnapshot.LEGACY_TABLE_NAME, SqlWorkbenchNotebookShare.LEGACY_TABLE_NAME, SqlWorkbenchExecution.LEGACY_TABLE_NAME))
.containsExactly("sqlWorkbenchNotebook", "sqlWorkbenchCell", "sqlWorkbenchNotebookVersion", "sqlWorkbenchResultSnapshot",
"sqlWorkbenchNotebookShare", "sqlWorkbenchExecution");
assertThat(ShareAudienceType.NAME).isEqualTo("sql_workbench_share_audience_type");
assertThat(ShareAudienceType.LEGACY_NAME).isEqualTo("sqlWorkbenchShareAudienceType");
SqlWorkbenchQBitConfig config = new SqlWorkbenchQBitConfig();
assertThat(config.getAccessPermissionName()).isEqualTo("sql_workbench.hasAccess");
assertThat(config.getWritePermissionName()).isEqualTo("sql_workbench.write");
assertThat(config.getSharePermissionName()).isEqualTo("sql_workbench.share");
assertThat(config.getAdminPermissionNames()).contains("sql_workbench_admin.hasAccess");
assertThat(config.getUserPermissionNames()).allSatisfy(name -> assertThat(name).startsWith("sql_workbench"));
assertThat(config.getUserSecurityKeyTypeName()).isEqualTo("sql_workbench_user");
assertThat(config.getAudienceSecurityKeyTypeName()).isEqualTo("sql_workbench_audience");
assertThat(config.getAllAccessSecurityKeyName()).isEqualTo("sql_workbench_all_access");
assertThat(SqlWorkbenchQBitConfig.LEGACY_ACCESS_PERMISSION_BASE_NAME).isEqualTo("sqlWorkbench");
assertThat(SqlWorkbenchQBitConfig.LEGACY_ADMIN_PERMISSION_BASE_NAME).isEqualTo("sqlWorkbenchAdmin");
assertThat(SqlWorkbenchQBitConfig.LEGACY_USER_SECURITY_KEY_TYPE_NAME).isEqualTo("sqlWorkbenchUser");
////////////////////////////////////////////////////////////////////
// prefixes are normalized, so a prefixed name stays snake_case //
////////////////////////////////////////////////////////////////////
assertThat(config.withTableNamePrefix("ns").applyPrefix(SqlWorkbenchNotebook.TABLE_NAME)).isEqualTo("ns_sql_workbench_notebook");
assertThat(config.withTableNamePrefix("ns_").applyPrefix(SqlWorkbenchNotebook.TABLE_NAME)).isEqualTo("ns_sql_workbench_notebook");
assertThat(config.withTableNamePrefix("myNs").applyPrefix(SqlWorkbenchNotebook.TABLE_NAME)).isEqualTo("my_ns_sql_workbench_notebook");
assertThat(config.withTableNamePrefix(null).applyPrefix(SqlWorkbenchNotebook.TABLE_NAME)).isEqualTo("sql_workbench_notebook");
//////////////////////////////////////////////////////////////////////
// with no prefix, the default RDBMS backend table name equals the //
// QQQ table name: no inference or DDL is needed //
//////////////////////////////////////////////////////////////////////
QInstance instance = new QInstance();
instance.addBackend(defineH2Backend("plain"));
new SqlWorkbenchQBitProducer().withQBitConfig(new SqlWorkbenchQBitConfig().withBackendName("plain").withRegisterRoutes(false)).produce(instance);
for(String name : tableNames)
{
assertThat(instance.getTable(name)).as(name).isNotNull();
}
assertThat(instance.getPossibleValueSource(ShareAudienceType.NAME)).isNotNull();
assertThat(instance.getSecurityKeyType("sql_workbench_user")).isNotNull();
}
/*******************************************************************************
**
*******************************************************************************/
@@ -156,7 +261,7 @@ class SqlWorkbenchQBitProducerTest extends BaseSqlWorkbenchTest
.anyMatch(e -> e.contains("maxRows")).anyMatch(e -> e.contains("writePermissionName")).anyMatch(e -> e.contains("defaultPageSize must not exceed"));
assertThat(errors(new SqlWorkbenchQBitConfig().withBackendName("db").withMaxAffectedRowsWithoutAcknowledgement(10).withMaxAffectedRows(5), instance))
.anyMatch(e -> e.contains("maxAffectedRowsWithoutAcknowledgement must not exceed"));
assertThat(errors(new SqlWorkbenchQBitConfig().withBackendName("db").withAdminPermissionBaseName("sqlWorkbench"), instance))
assertThat(errors(new SqlWorkbenchQBitConfig().withBackendName("db").withAdminPermissionBaseName("sql_workbench"), instance))
.anyMatch(e -> e.contains("adminPermissionBaseName must differ"));
assertThat(errors(new SqlWorkbenchQBitConfig().withBackendName("db").withAdminPermissionBaseName(""), instance))
.anyMatch(e -> e.contains("adminPermissionBaseName is required"));
@@ -180,8 +285,8 @@ class SqlWorkbenchQBitProducerTest extends BaseSqlWorkbenchTest
.withWriteCredentials("db", new SqlWorkbenchCredentials().withUsername("rw").withPassword("secret2"));
assertThat(config.toString()).doesNotContain("secret").doesNotContain("ro\"").contains("db");
assertThat(config.getReadOnlyCredentials().get("db").toString()).doesNotContain("secret").doesNotContain("ro,");
assertThat(config.getAccessPermissionName()).isEqualTo("sqlWorkbench.hasAccess");
assertThat(config.getAdminPermissionName()).isEqualTo("sqlWorkbenchAdmin.hasAccess");
assertThat(config.getAccessPermissionName()).isEqualTo("sql_workbench.hasAccess");
assertThat(config.getAdminPermissionName()).isEqualTo("sql_workbench_admin.hasAccess");
////////////////////////////////////////////////////////////////
// credentials never serialize - jdbcUrl can carry a password //
@@ -189,7 +294,7 @@ class SqlWorkbenchQBitProducerTest extends BaseSqlWorkbenchTest
String credentialsJson = com.kingsrook.qqq.backend.core.utils.JsonUtils.toJson(new SqlWorkbenchCredentials()
.withUsername("ro").withPassword("secret").withJdbcUrl("jdbc:postgresql://db/app?password=urlsecret"));
assertThat(credentialsJson).doesNotContain("secret").doesNotContain("urlsecret").doesNotContain("jdbc:");
assertThat(config.getAdminPermissionRules().getPermissionBaseName()).isEqualTo("sqlWorkbenchAdmin");
assertThat(config.getAdminPermissionRules().getPermissionBaseName()).isEqualTo("sql_workbench_admin");
assertThat(config.getDefaultBackendNameForTables()).isEqualTo("db");
assertThat(config.applyPrefix("t")).isEqualTo("t");
assertThat(config.withTableNamePrefix("p").applyPrefix("t")).isEqualTo("p_t");
@@ -0,0 +1,271 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.developer;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import com.kingsrook.qqq.backend.core.actions.developer.DeveloperPanelsAction;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanel;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanelBlock;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanelBlockType;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanelContext;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanelLink;
import com.kingsrook.qqq.backend.core.model.developer.DeveloperPanelsMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qbits.sqlworkbench.BaseSqlWorkbenchTest;
import com.kingsrook.qbits.sqlworkbench.sql.SqlDialect;
import com.kingsrook.qqq.backend.module.rdbms.jdbc.ConnectionManager;
import com.kingsrook.qqq.backend.module.rdbms.model.metadata.RDBMSBackendMetaData;
import com.kingsrook.qqq.backend.module.rdbms.model.metadata.RDBMSTableBackendDetails;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/*******************************************************************************
** Unit test for {@link SqlWorkbenchDeveloperPanelProvider} and
** {@link RecordSql}.
*******************************************************************************/
class SqlWorkbenchDeveloperPanelProviderTest extends BaseSqlWorkbenchTest
{
private static final String TABLE_NAME = "orderLine";
private static final String SKU = "AB'C-1";
/*******************************************************************************
** A table whose backend names differ from its QQQ names, with a string
** primary key, plus a table on a non-RDBMS backend.
*******************************************************************************/
@BeforeEach
void setUp() throws Exception
{
qInstance.addTable(new QTableMetaData()
.withName(TABLE_NAME)
.withBackendName(BACKEND_NAME)
.withPrimaryKeyField("sku")
.withBackendDetails(new RDBMSTableBackendDetails().withTableName("order_line"))
.withField(new QFieldMetaData("sku", QFieldType.STRING).withBackendName("line_sku"))
.withField(new QFieldMetaData("quantity", QFieldType.INTEGER))
.withField(new QFieldMetaData("unitPrice", QFieldType.DECIMAL).withBackendName("unit_price")));
qInstance.addBackend(new QBackendMetaData().withName("memory"));
qInstance.addTable(new QTableMetaData()
.withName("memoryThing")
.withBackendName("memory")
.withPrimaryKeyField("id")
.withField(new QFieldMetaData("id", QFieldType.INTEGER)));
try(Connection connection = ConnectionManager.getConnection((RDBMSBackendMetaData) qInstance.getBackend(BACKEND_NAME)); Statement statement = connection.createStatement())
{
statement.execute("DROP TABLE IF EXISTS order_line");
statement.execute("CREATE TABLE order_line (line_sku VARCHAR(40) PRIMARY KEY, quantity INT, unit_price DECIMAL(10,2))");
statement.execute("INSERT INTO order_line VALUES ('AB''C-1', 3, 9.50), ('other', 1, 1.00)");
}
}
/*******************************************************************************
**
*******************************************************************************/
private DeveloperPanelContext context(String tableName, Object primaryKey)
{
QTableMetaData table = qInstance.getTable(tableName);
return (new DeveloperPanelContext().withTable(table).withRecord(new QRecord().withValue(table.getPrimaryKeyField(), (java.io.Serializable) primaryKey)));
}
/*******************************************************************************
** The producer registers the provider; the panel shows through the
** framework action with the record and table queries, backend details,
** links and the note.
*******************************************************************************/
@Test
void testPanel() throws Exception
{
assertThat(DeveloperPanelsMetaData.of(qInstance).getProviders())
.anyMatch(reference -> SqlWorkbenchDeveloperPanelProvider.class.getName().equals(reference.getName()));
DeveloperPanel panel = new DeveloperPanelsAction().execute(context(TABLE_NAME, SKU)).stream()
.filter(p -> SqlWorkbenchDeveloperPanelProvider.ID.equals(p.getId())).findFirst().orElseThrow();
assertNull(panel.getError());
assertEquals("SQL Workbench", panel.getTitle());
assertEquals(450, panel.getOrder());
assertEquals("terminal", panel.getIcon());
List<DeveloperPanelBlock> code = panel.getBlocks().stream().filter(b -> b.getType() == DeveloperPanelBlockType.CODE).toList();
assertEquals(2, code.size());
assertEquals("sql", code.get(0).getLanguage());
assertEquals("""
SELECT
"line_sku" AS "sku",
"quantity",
"unit_price" AS "unitPrice"
FROM "order_line"
WHERE "line_sku" = 'AB''C-1'""", code.get(0).getCode());
assertEquals("""
SELECT
"line_sku" AS "sku",
"quantity",
"unit_price" AS "unitPrice"
FROM "order_line"
LIMIT 100""", code.get(1).getCode());
////////////////////////////////////////////////////////////
// the generated SQL runs on the backend and finds the row //
////////////////////////////////////////////////////////////
try(Connection connection = ConnectionManager.getConnection((RDBMSBackendMetaData) qInstance.getBackend(BACKEND_NAME)); Statement statement = connection.createStatement())
{
try(ResultSet resultSet = statement.executeQuery(code.get(0).getCode()))
{
assertTrue(resultSet.next());
assertEquals(SKU, resultSet.getString("sku"));
assertEquals(3, resultSet.getInt("quantity"));
assertFalse(resultSet.next());
}
try(ResultSet resultSet = statement.executeQuery(code.get(1).getCode()))
{
int rows = 0;
while(resultSet.next())
{
rows++;
}
assertEquals(2, rows);
}
}
DeveloperPanelBlock keyValues = panel.getBlocks().stream().filter(b -> b.getType() == DeveloperPanelBlockType.KEY_VALUES).findFirst().orElseThrow();
assertEquals(BACKEND_NAME, keyValues.getItems().get(0).getValue());
assertEquals("h2", keyValues.getItems().get(1).getValue());
assertEquals("order_line", keyValues.getItems().get(2).getValue());
assertEquals("No", keyValues.getItems().get(3).getValue());
DeveloperPanelBlock links = panel.getBlocks().stream().filter(b -> b.getType() == DeveloperPanelBlockType.LINKS).findFirst().orElseThrow();
assertEquals(2, links.getLinks().size());
DeveloperPanelLink recordLink = links.getLinks().get(0);
assertThat(recordLink.getHref()).startsWith("/app/sqlWorkbenchRunner?sql=").endsWith("&backend=h2").doesNotContain(" ", "+", "'", "\n");
String sqlParam = recordLink.getHref().substring("/app/sqlWorkbenchRunner?sql=".length(), recordLink.getHref().indexOf("&backend="));
assertEquals(code.get(0).getCode(), URLDecoder.decode(sqlParam, StandardCharsets.UTF_8));
assertThat(links.getLinks().get(1).getHref()).contains("LIMIT%20100");
assertTrue(panel.getBlocks().stream().anyMatch(b -> b.getType() == DeveloperPanelBlockType.TEXT && b.getText().contains("runs only what you execute")));
}
/*******************************************************************************
** A prefixed workbench links to its prefixed Runner; the replica is reported.
*******************************************************************************/
@Test
void testPrefixAndReplica() throws Exception
{
config.setTableNamePrefix("ops");
qInstance.getApp("sqlWorkbench").setName("ops_sqlWorkbench");
qInstance.getApps().put("ops_sqlWorkbench", qInstance.getApps().remove("sqlWorkbench"));
config.setUseBackendReadOnlyReplica(true);
((RDBMSBackendMetaData) qInstance.getBackend(BACKEND_NAME)).setReadOnlyBackendMetaData(defineH2Backend("h2Replica"));
DeveloperPanel panel = new SqlWorkbenchDeveloperPanelProvider().build(context("person", 7));
String href = panel.getBlocks().stream().filter(b -> b.getType() == DeveloperPanelBlockType.LINKS).findFirst().orElseThrow().getLinks().get(0).getHref();
assertThat(href).startsWith("/app/ops_sqlWorkbenchRunner?sql=");
assertThat(panel.getBlocks().get(0).getCode()).endsWith("WHERE \"id\" = 7");
DeveloperPanelBlock keyValues = panel.getBlocks().stream().filter(b -> b.getType() == DeveloperPanelBlockType.KEY_VALUES).findFirst().orElseThrow();
assertEquals("Yes (h2Replica)", keyValues.getItems().get(3).getValue());
}
/*******************************************************************************
** Writers get the same (SELECT-only) panel.
*******************************************************************************/
@Test
void testWriterSameQueries() throws Exception
{
String readerSql = new SqlWorkbenchDeveloperPanelProvider().build(context(TABLE_NAME, SKU)).getBlocks().get(0).getCode();
loginAs(WRITER);
assertEquals(readerSql, new SqlWorkbenchDeveloperPanelProvider().build(context(TABLE_NAME, SKU)).getBlocks().get(0).getCode());
}
/*******************************************************************************
** Not available without the access permission, for a non-RDBMS backend,
** for an excluded or not-allowed backend, or without the app.
*******************************************************************************/
@Test
void testNotAvailable()
{
SqlWorkbenchDeveloperPanelProvider provider = new SqlWorkbenchDeveloperPanelProvider();
assertTrue(provider.isAvailable(context(TABLE_NAME, SKU)));
loginAs("nobody-1");
assertFalse(provider.isAvailable(context(TABLE_NAME, SKU)));
assertTrue(new DeveloperPanelsAction().execute(context(TABLE_NAME, SKU)).stream().noneMatch(p -> SqlWorkbenchDeveloperPanelProvider.ID.equals(p.getId())));
loginAs(READER);
assertFalse(provider.isAvailable(context("memoryThing", 1)));
config.setExcludedBackendNames(List.of(BACKEND_NAME));
assertFalse(provider.isAvailable(context(TABLE_NAME, SKU)));
config.setExcludedBackendNames(null);
config.setAllowedBackendNames(List.of("someOtherBackend"));
assertFalse(provider.isAvailable(context(TABLE_NAME, SKU)));
config.setAllowedBackendNames(null);
qInstance.getApps().remove("sqlWorkbench");
assertFalse(provider.isAvailable(context(TABLE_NAME, SKU)));
}
/*******************************************************************************
** Identifier quoting and literals per dialect.
*******************************************************************************/
@Test
void testQuotingAndLiterals()
{
assertEquals("`my``col`", RecordSql.quoteIdentifier(SqlDialect.MYSQL, "my`col"));
assertEquals("\"my\"\"col\"", RecordSql.quoteIdentifier(SqlDialect.POSTGRES, "my\"col"));
assertEquals("'a''b\\c'", RecordSql.stringLiteral(SqlDialect.POSTGRES, "a'b\\c"));
assertEquals("'a''b\\\\c'", RecordSql.stringLiteral(SqlDialect.MYSQL, "a'b\\c"));
assertEquals("42", RecordSql.literal(SqlDialect.POSTGRES, QFieldType.INTEGER, 42));
assertEquals("12.50", RecordSql.literal(SqlDialect.POSTGRES, QFieldType.DECIMAL, new java.math.BigDecimal("12.50")));
assertEquals("'1; DROP TABLE x'", RecordSql.literal(SqlDialect.POSTGRES, QFieldType.INTEGER, "1; DROP TABLE x"));
assertEquals("'42'", RecordSql.literal(SqlDialect.POSTGRES, QFieldType.STRING, "42"));
assertEquals("NULL", RecordSql.literal(SqlDialect.POSTGRES, QFieldType.STRING, null));
assertThat(RecordSql.selectTable(qInstance.getTable("person"), SqlDialect.GENERIC)).endsWith("FETCH FIRST 100 ROWS ONLY");
assertThat(RecordSql.selectTable(qInstance.getTable("person"), SqlDialect.MYSQL)).contains("`first_name` AS `firstName`").endsWith("LIMIT 100");
assertEquals("%27a%20b%27%26c%3D1", SqlWorkbenchDeveloperPanelProvider.encode("'a b'&c=1"));
}
}
@@ -29,6 +29,7 @@ import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qbits.sqlworkbench.BaseSqlWorkbenchTest;
import com.kingsrook.qbits.sqlworkbench.SystemUser;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchRuntime;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchExecution;
import org.junit.jupiter.api.BeforeEach;
@@ -73,7 +74,7 @@ class SqlExecutionEngineTest extends BaseSqlWorkbenchTest
*******************************************************************************/
private List<SqlWorkbenchExecution> audits() throws Exception
{
return (QueryAction.execute(SqlWorkbenchExecution.TABLE_NAME, SqlWorkbenchExecution.class, new QQueryFilter()));
return (SystemUser.call(() -> QueryAction.execute(SqlWorkbenchExecution.TABLE_NAME, SqlWorkbenchExecution.class, new QQueryFilter())));
}
@@ -0,0 +1,397 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.home;
import java.io.Serializable;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.kingsrook.qbits.sqlworkbench.BaseSqlWorkbenchTest;
import com.kingsrook.qbits.sqlworkbench.SystemUser;
import com.kingsrook.qbits.sqlworkbench.metadata.SqlWorkbenchHomeWidgetsMetaDataProducer;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchExecution;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qqq.backend.core.actions.dashboard.RenderWidgetAction;
import com.kingsrook.qqq.backend.core.actions.permissions.PermissionsHelper;
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.ChartData;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.QWidgetData;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.RawHTML;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.TableData;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaData;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.core.model.session.QUser;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/*******************************************************************************
** The home dashboard widgets, rendered through RenderWidgetAction in each
** viewer's session: real data, record locks (own runs; admins see all),
** permission gates, and honest empty states.
*******************************************************************************/
class SqlWorkbenchHomeTest extends BaseSqlWorkbenchTest
{
private Integer orders;
private Integer scratch;
/*******************************************************************************
** writer-1 owns "Orders report" (shared with reader-1) and "Private
** scratch"; runs by writer-1 (read, write, failure) and reader-1 (one in
** the window, one 40 days ago).
*******************************************************************************/
private void seed() throws Exception
{
loginAs(WRITER);
orders = insert(SqlWorkbenchNotebook.TABLE_NAME, Map.of("title", "Orders report", "description", "Daily orders\nsecond line")).getValueInteger("id");
scratch = insert(SqlWorkbenchNotebook.TABLE_NAME, Map.of("title", "Private scratch")).getValueInteger("id");
insert(SqlWorkbenchNotebookShare.TABLE_NAME, Map.of("notebookId", orders, "audienceType", "USER", "audienceId", READER));
Instant now = Instant.now();
SystemUser.run(() ->
{
run(WRITER, "READ_ONLY", "SUCCEEDED", "SELECT * FROM orders WHERE total > 100", orders, 1500, now.minus(Duration.ofMinutes(5)), null, null);
run(WRITER, "WRITE_COMMIT", "SUCCEEDED", "UPDATE person SET first_name = 'x'", null, 40, now.minus(Duration.ofMinutes(10)), 3, null);
run(WRITER, "READ_ONLY", "FAILED", "SELEC broken", scratch, 5, now.minus(Duration.ofDays(3)), null, "syntax error at <x>");
run(READER, "READ_ONLY", "SUCCEEDED", "SELECT count(*) FROM orders", orders, 50, now.minus(Duration.ofDays(1)), null, null);
run(READER, "READ_ONLY", "SUCCEEDED", "SELECT 'old'", orders, 50, now.minus(Duration.ofDays(40)), null, null);
});
}
/*******************************************************************************
** Insert as the session.
*******************************************************************************/
private static QRecord insert(String table, Map<String, Serializable> values) throws QException
{
QRecord record = new QRecord();
values.forEach(record::setValue);
record = new InsertAction().execute(new InsertInput(table).withRecord(record)).getRecords().get(0);
assertThat(record.getErrors()).isNullOrEmpty();
return (record);
}
/*******************************************************************************
** One audit row (as the system user: the audit is append-only).
*******************************************************************************/
private static void run(String user, String mode, String status, String sql, Integer notebookId, int durationMs, Instant start, Integer affected, String error) throws QException
{
Map<String, Serializable> values = new HashMap<>();
values.put("executionUuid", UUID.randomUUID().toString());
values.put("userId", user);
values.put("userName", "Name of " + user);
values.put("backendName", BACKEND_NAME);
values.put("mode", mode);
values.put("status", status);
values.put("sqlText", sql);
values.put("notebookId", notebookId);
values.put("durationMs", durationMs);
values.put("startTime", start);
values.put("endTime", start.plusMillis(durationMs));
values.put("rowsReturned", affected == null ? 10 : null);
values.put("rowsAffected", affected);
values.put("errorMessage", error);
values.put("createDate", start);
insert(SqlWorkbenchExecution.TABLE_NAME, values);
}
/*******************************************************************************
** Render a widget through the core action (widget permission checked).
*******************************************************************************/
private QWidgetData render(String widgetName, Map<String, String> params) throws QException
{
QWidgetMetaData widget = (QWidgetMetaData) qInstance.getWidget(widgetName);
return (new RenderWidgetAction().execute(new RenderWidgetInput().withWidgetMetaData(widget).withUrlParams(new HashMap<>(params))).getWidgetData());
}
/*******************************************************************************
**
*******************************************************************************/
private QWidgetData render(String widgetName) throws QException
{
return (render(widgetName, Map.of()));
}
/*******************************************************************************
** All the HTML of a table widget's cells (and its footer).
*******************************************************************************/
private static String cells(QWidgetData data)
{
StringBuilder html = new StringBuilder();
for(Map<String, Object> row : ((TableData) data).getRows())
{
row.values().forEach(v -> html.append(v).append('|'));
html.append('\n');
}
return (html + String.valueOf(data.getFooterHTML()));
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testSummaryForOwnerAndAdmin() throws Exception
{
seed();
loginAs(WRITER);
String html = ((RawHTML) render(SqlWorkbenchHomeWidgetsMetaDataProducer.SUMMARY)).getHtml();
assertThat(html).contains("Notebooks", ">2<", "2 yours, 0 shared with you", "Runs, 7 Days", "1 failed",
"Writes, 30 Days", "3 rows affected", "/app/sqlWorkbenchRunner", "Open the Runner", "your own runs");
loginAs(READER);
html = ((RawHTML) render(SqlWorkbenchHomeWidgetsMetaDataProducer.SUMMARY)).getHtml();
assertThat(html).contains("0 yours, 1 shared with you", "0 failed", "0 rows affected");
loginAs(ADMIN);
html = ((RawHTML) render(SqlWorkbenchHomeWidgetsMetaDataProducer.SUMMARY)).getHtml();
assertThat(html).contains("0 yours, 2 by other users", "workbench admin", "3 rows affected");
config.setWriteModeEnabled(false);
html = ((RawHTML) render(SqlWorkbenchHomeWidgetsMetaDataProducer.SUMMARY)).getHtml();
assertThat(html).contains("write mode is off");
assertThat(cells(render(SqlWorkbenchHomeWidgetsMetaDataProducer.WRITES))).contains("Write mode is off");
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testRunsAreLockedToTheirUserExceptForAdmins() throws Exception
{
seed();
loginAs(WRITER);
String runs = cells(render(SqlWorkbenchHomeWidgetsMetaDataProducer.RECENT_RUNS));
assertThat(runs).contains("UPDATE person", "SELEC broken", "Orders report", "Private scratch", "you", "3 rows affected", "10 rows")
.doesNotContain("count(*)");
loginAs(READER);
runs = cells(render(SqlWorkbenchHomeWidgetsMetaDataProducer.RECENT_RUNS));
assertThat(runs).contains("SELECT count(*) FROM orders", "SELECT &#39;old&#39;", "Orders report").doesNotContain("UPDATE person");
loginAs(ADMIN);
runs = cells(render(SqlWorkbenchHomeWidgetsMetaDataProducer.RECENT_RUNS));
assertThat(runs).contains("UPDATE person", "count(*)", "Name of writer-1", "Name of reader-1");
TableData byUser = (TableData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.BY_USER);
assertThat(byUser.getRows()).hasSize(2);
assertThat(cells(byUser)).contains("Name of writer-1", "writer-1", "Name of reader-1", "last run").doesNotContain("Only your own");
assertThat(byUser.getColumns()).hasSize(4);
///////////////////////////////////////////////////////////////
// activity by user is admin-only (the widget's permission) //
///////////////////////////////////////////////////////////////
assertThat(PermissionsHelper.hasWidgetPermission(new RenderWidgetInput(), SqlWorkbenchHomeWidgetsMetaDataProducer.BY_USER)).isTrue();
loginAs(WRITER);
assertThat(PermissionsHelper.hasWidgetPermission(new RenderWidgetInput(), SqlWorkbenchHomeWidgetsMetaDataProducer.BY_USER)).isFalse();
assertThat(PermissionsHelper.hasWidgetPermission(new RenderWidgetInput(), SqlWorkbenchHomeWidgetsMetaDataProducer.SUMMARY)).isTrue();
SqlWorkbenchHome.Viewer viewer = SqlWorkbenchHome.viewer(config, Instant.now());
String own = cells(SqlWorkbenchHome.load(SqlWorkbenchHome.KIND_BY_USER, viewer, new RenderWidgetInput()));
assertThat(own).contains("Only your own runs").doesNotContain("reader-1");
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testSlowestFailedWritesAndChart() throws Exception
{
seed();
loginAs(WRITER);
TableData slowest = (TableData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.SLOWEST);
assertThat(slowest.getRows()).hasSize(3);
assertThat(String.valueOf(slowest.getRows().get(0).get("duration"))).contains("1.5 s");
assertThat(slowest.getLinkURL()).isEqualTo("/app/sql_workbench_execution");
String failed = cells(render(SqlWorkbenchHomeWidgetsMetaDataProducer.FAILED));
assertThat(failed).contains("syntax error at &lt;x&gt;", "failed", "/app/sql_workbench_execution/").doesNotContain("UPDATE");
String writes = cells(render(SqlWorkbenchHomeWidgetsMetaDataProducer.WRITES));
assertThat(writes).contains("UPDATE person", "<strong>3</strong>", "Write mode is on").doesNotContain("SELECT");
ChartData chart = (ChartData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.RUNS_PER_DAY);
assertThat(chart.getChartData().getLabels()).hasSize(30);
assertThat(chart.getChartData().getDatasets()).extracting(ChartData.Data.Dataset::getLabel).containsExactly("Runs", "Failed", "Writes");
assertThat(chart.getChartData().getDatasets().get(0).getData().stream().mapToLong(Number::longValue).sum()).isEqualTo(3);
assertThat(chart.getChartData().getDatasets().get(1).getData().stream().mapToLong(Number::longValue).sum()).isEqualTo(1);
assertThat(chart.getChartData().getDatasets().get(2).getData().stream().mapToLong(Number::longValue).sum()).isEqualTo(1);
assertThat(chart.getSublabel()).isEqualTo("3 runs in the last 30 days, by day");
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testNotebookWidgets() throws Exception
{
seed();
loginAs(READER);
TableData recent = (TableData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.RECENT_NOTEBOOKS);
assertThat(recent.getRows()).hasSize(1);
assertThat(cells(recent)).contains("Orders report", "Daily orders", "Name of writer-1", "shared", "/app/sqlWorkbenchRunner?notebook=" + orders)
.doesNotContain("second line", "Private scratch");
String shared = cells(render(SqlWorkbenchHomeWidgetsMetaDataProducer.SHARED));
assertThat(shared).contains("Orders report", "reader-1", "Name of writer-1");
loginAs(ADMIN);
TableData mostUsed = (TableData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.MOST_USED);
assertThat(mostUsed.getRows()).hasSize(2);
assertThat(String.valueOf(mostUsed.getRows().get(0).get("notebook"))).contains("Orders report");
assertThat(mostUsed.getRows().get(0).get("runs")).isEqualTo("2");
assertThat(mostUsed.getRows().get(0).get("users")).isEqualTo("2");
///////////////////////////////////////////////////////////////////////
// reader-1's own runs on a notebook they can't read: labeled //
// honestly, not hidden //
///////////////////////////////////////////////////////////////////////
SystemUser.run(() -> run(READER, "READ_ONLY", "SUCCEEDED", "SELECT 1", scratch, 5, Instant.now(), null, null));
loginAs(READER);
assertThat(cells(render(SqlWorkbenchHomeWidgetsMetaDataProducer.MOST_USED))).contains("Notebook #" + scratch + " (deleted, or not visible to you)");
QWidgetData runner = render(SqlWorkbenchHomeWidgetsMetaDataProducer.NOTEBOOK_RUNNER, Map.of("id", String.valueOf(orders)));
assertThat(((RawHTML) runner).getHtml()).contains("/app/sqlWorkbenchRunner?notebook=" + orders, "Open in Runner");
assertThat(((RawHTML) render(SqlWorkbenchHomeWidgetsMetaDataProducer.NOTEBOOK_RUNNER)).getHtml()).contains("Save the notebook");
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testEmptyStatesAndPermissions() throws Exception
{
loginAs(READER);
assertThat(((TableData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.RECENT_NOTEBOOKS)).getNoRowsFoundHTML()).contains("No notebooks yet");
assertThat(((TableData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.RECENT_RUNS)).getNoRowsFoundHTML()).contains("No runs yet");
assertThat(((TableData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.WRITES)).getNoRowsFoundHTML()).contains("No writes have been committed");
assertThat(((TableData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.SHARED)).getNoRowsFoundHTML()).contains("No shared notebooks");
assertThat(((TableData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.MOST_USED)).getNoRowsFoundHTML()).contains("No notebook runs");
assertThat(((TableData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.FAILED)).getNoRowsFoundHTML()).contains("No failed runs");
assertThat(((TableData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.SLOWEST)).getNoRowsFoundHTML()).contains("No completed runs");
assertThat(((ChartData) render(SqlWorkbenchHomeWidgetsMetaDataProducer.RUNS_PER_DAY)).getSublabel()).isEqualTo("No runs in the last 30 days");
////////////////////////////////////////////////////////////////////////
// no workbench access: every widget is denied (the middleware checks //
// widget permissions); rendered anyway, the locks show nothing //
////////////////////////////////////////////////////////////////////////
loginAs("nobody-1");
for(String name : SqlWorkbenchHomeWidgetsMetaDataProducer.homeWidgetNames())
{
assertThat(PermissionsHelper.hasWidgetPermission(new RenderWidgetInput(), name)).as(name).isFalse();
}
assertThat(((RawHTML) render(SqlWorkbenchHomeWidgetsMetaDataProducer.SUMMARY)).getHtml()).contains("can&#39;t read");
////////////////////////////////////////////////////////////////////////
// workbench access but no table read permissions: says so, no zeros //
////////////////////////////////////////////////////////////////////////
QSession session = new QSession().withUser(new QUser().withIdReference("limited-1"));
session.setPermissions(new HashSet<>(List.of(config.getAccessPermissionName())));
QContext.init(qInstance, session);
assertThat(((RawHTML) render(SqlWorkbenchHomeWidgetsMetaDataProducer.SUMMARY)).getHtml()).contains("can&#39;t read");
List<String> denied = new ArrayList<>();
for(String name : List.of(SqlWorkbenchHomeWidgetsMetaDataProducer.RUNS_PER_DAY, SqlWorkbenchHomeWidgetsMetaDataProducer.RECENT_NOTEBOOKS,
SqlWorkbenchHomeWidgetsMetaDataProducer.RECENT_RUNS, SqlWorkbenchHomeWidgetsMetaDataProducer.SLOWEST, SqlWorkbenchHomeWidgetsMetaDataProducer.FAILED,
SqlWorkbenchHomeWidgetsMetaDataProducer.MOST_USED, SqlWorkbenchHomeWidgetsMetaDataProducer.SHARED, SqlWorkbenchHomeWidgetsMetaDataProducer.WRITES))
{
denied.add(((RawHTML) render(name)).getHtml());
}
assertThat(denied).allSatisfy(html -> assertThat(html).contains("You don&#39;t have permission to read"));
SqlWorkbenchHome.Viewer viewer = new SqlWorkbenchHome.Viewer(config, Instant.now(), ZoneId.of("UTC"), "x", false);
assertThat(((RawHTML) SqlWorkbenchHome.load("nope", viewer, new RenderWidgetInput())).getHtml()).contains("Unknown");
assertThat(((RawHTML) SqlWorkbenchHome.load(null, viewer, new RenderWidgetInput())).getHtml()).contains("Unknown");
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testLabelsAndHtml()
{
assertThat(SqlWorkbenchHome.modeLabel("WRITE_PREVIEW")).isEqualTo("write preview");
assertThat(SqlWorkbenchHome.modeLabel("EXPLAIN")).isEqualTo("explain");
assertThat(SqlWorkbenchHome.modeLabel("EXPLAIN_ANALYZE")).isEqualTo("explain analyze");
assertThat(SqlWorkbenchHome.modeLabel("SOMETHING_ELSE")).isEqualTo("something else");
assertThat(SqlWorkbenchHome.modeLabel(null)).isEmpty();
assertThat(SqlWorkbenchHome.statusBadge(new QRecord())).isEmpty();
assertThat(SqlWorkbenchHome.statusBadge(new QRecord().withValue("status", "RUNNING"))).contains("running", HomeHtml.INFO_COLOR);
assertThat(SqlWorkbenchHome.statusBadge(new QRecord().withValue("status", "CANCELLED"))).contains(HomeHtml.OFF_COLOR);
assertThat(SqlWorkbenchHome.statusBadge(new QRecord().withValue("status", "TIMED_OUT"))).contains("timed out", HomeHtml.BAD_COLOR);
assertThat(SqlWorkbenchHome.firstLine(null, 5)).isNull();
assertThat(SqlWorkbenchHome.firstLine("abcdefgh", 5)).isEqualTo("abcd…");
assertThat(HomeHtml.escape(null)).isEmpty();
assertThat(HomeHtml.escape("<a href='x'>&\"")).isEqualTo("&lt;a href=&#39;x&#39;&gt;&amp;&quot;");
assertThat(HomeHtml.code("SELECT *\n FROM t", 100)).contains(">SELECT * FROM t<");
assertThat(HomeHtml.code(null, 10)).contains("><");
assertThat(HomeHtml.code("x".repeat(3000), 10)).contains("xxxxxxxxx…");
assertThat(HomeHtml.duration(null)).isEmpty();
assertThat(HomeHtml.duration(850)).isEqualTo("850 ms");
assertThat(HomeHtml.duration(4200)).isEqualTo("4.2 s");
assertThat(HomeHtml.duration(185_000)).isEqualTo("3m 05s");
assertThat(HomeHtml.plural(1, "run")).isEqualTo("1 run");
assertThat(HomeHtml.card("a", null, " ")).doesNotContain("&middot;");
ZoneId utc = ZoneId.of("UTC");
Instant now = Instant.parse("2026-10-12T15:00:00Z");
assertThat(HomeHtml.when(null, now, utc)).isEmpty();
assertThat(HomeHtml.when(Instant.parse("2026-10-12T09:05:00Z"), now, utc)).isEqualTo("9:05 AM");
assertThat(HomeHtml.when(Instant.parse("2026-03-01T09:05:00Z"), now, utc)).isEqualTo("Mar 1, 9:05 AM");
assertThat(HomeHtml.when(Instant.parse("2025-03-01T09:05:00Z"), now, utc)).isEqualTo("Mar 1, 2025, 9:05 AM");
assertThat(HomeHtml.tiles(List.of(new HomeHtml.Tile("A", "1", null, null)))).contains(">1<").doesNotContain("href");
}
}
@@ -0,0 +1,219 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.metadata;
import java.io.Serializable;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import com.kingsrook.qbits.sqlworkbench.BaseSqlWorkbenchTest;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchIntegrations;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchQBitConfig;
import com.kingsrook.qbits.sqlworkbench.SystemUser;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchExecution;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookVersion;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchResultSnapshot;
import com.kingsrook.qqq.backend.core.actions.dashboard.RenderWidgetAction;
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.ChildRecordListData;
import com.kingsrook.qqq.backend.core.model.dashboard.widgets.WidgetType;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaDataInterface;
import com.kingsrook.qqq.backend.core.model.metadata.joins.JoinType;
import com.kingsrook.qqq.backend.core.model.metadata.joins.QJoinMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.layout.QAppChildMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.layout.QAppMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QFieldSection;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/*******************************************************************************
** The app's navigation (Runner plus the main tables only), the sub-tables as
** child lists on a notebook's record view (still standard tables), and the
** ESB / Quick Search hooks.
*******************************************************************************/
class AppNavigationTest extends BaseSqlWorkbenchTest
{
private static final List<String> SUB_TABLES = List.of(SqlWorkbenchCell.TABLE_NAME, SqlWorkbenchNotebookVersion.TABLE_NAME,
SqlWorkbenchResultSnapshot.TABLE_NAME, SqlWorkbenchNotebookShare.TABLE_NAME);
/*******************************************************************************
**
*******************************************************************************/
@Test
void testNavListsTheRunnerAndMainTablesOnly()
{
QAppMetaData app = qInstance.getApp(SqlWorkbenchAppMetaDataProducer.APP_NAME);
assertThat(app.getChildren()).extracting(QAppChildMetaData::getName)
.containsExactly(SqlWorkbenchRunnerAppMetaDataProducer.APP_NAME, SqlWorkbenchNotebook.TABLE_NAME, SqlWorkbenchExecution.TABLE_NAME);
//////////////////////////////////////////////////////////////////////
// no app anywhere lists a sub-table, yet each is a standard table //
// with a record label (reachable by URL, the table API and search) //
//////////////////////////////////////////////////////////////////////
for(QAppMetaData any : qInstance.getApps().values())
{
assertThat(any.getChildren() == null ? List.<QAppChildMetaData>of() : any.getChildren()).extracting(QAppChildMetaData::getName)
.as(any.getName()).doesNotContainAnyElementsOf(SUB_TABLES);
}
for(String subTable : SUB_TABLES)
{
QTableMetaData table = qInstance.getTable(subTable);
assertThat(table).as(subTable).isNotNull();
assertThat(table.getRecordLabelFields()).as(subTable).isNotEmpty();
assertThat(table.getField("notebookId").getPossibleValueSourceName()).isEqualTo(SqlWorkbenchNotebook.TABLE_NAME);
}
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testNotebookRecordViewShowsTheSubTablesAndRuns()
{
QTableMetaData notebook = qInstance.getTable(SqlWorkbenchNotebook.TABLE_NAME);
List<String> widgetSections = notebook.getSections().stream().map(QFieldSection::getWidgetName).filter(Objects::nonNull).toList();
assertThat(widgetSections).containsExactly(SqlWorkbenchHomeWidgetsMetaDataProducer.NOTEBOOK_RUNNER,
SqlWorkbenchTableMetaDataHelper.JOIN_NOTEBOOK_CELL, SqlWorkbenchTableMetaDataHelper.JOIN_NOTEBOOK_SHARE, SqlWorkbenchTableMetaDataHelper.JOIN_NOTEBOOK_SNAPSHOT,
SqlWorkbenchTableMetaDataHelper.JOIN_NOTEBOOK_VERSION, SqlWorkbenchTableMetaDataHelper.JOIN_NOTEBOOK_EXECUTION);
for(String joinName : widgetSections.subList(1, widgetSections.size()))
{
QWidgetMetaDataInterface widget = qInstance.getWidget(joinName);
assertThat(widget.getType()).as(joinName).isEqualTo(WidgetType.CHILD_RECORD_LIST.getType());
QJoinMetaData join = qInstance.getJoin(joinName);
assertThat(join.getLeftTable()).isEqualTo(SqlWorkbenchNotebook.TABLE_NAME);
assertThat(join.getType()).isEqualTo(JoinType.ONE_TO_MANY);
}
assertThat(qInstance.getJoin(SqlWorkbenchTableMetaDataHelper.JOIN_NOTEBOOK_EXECUTION).getRightTable()).isEqualTo(SqlWorkbenchExecution.TABLE_NAME);
assertThat(qInstance.getWidget(SqlWorkbenchTableMetaDataHelper.JOIN_NOTEBOOK_EXECUTION).getLabel()).isEqualTo("Runs");
assertThat(notebook.getSections().stream().filter(s -> s.getWidgetName() != null).map(QFieldSection::getLabel).toList())
.containsExactly("Runner", "Cells", "Shared With", "Pinned Results", "Versions", "Runs");
assertThat(notebook.getSections().get(0).getFieldNames()).containsExactly("id", "title", "description", "ownerName", "visibility");
}
/*******************************************************************************
** The Runs child list goes through the execution audit's lock: the
** viewer's own runs of the notebook, every user's for an admin.
*******************************************************************************/
@Test
void testRunsChildListIsLocked() throws Exception
{
loginAs(WRITER);
QRecord notebook = new InsertAction().execute(new InsertInput(SqlWorkbenchNotebook.TABLE_NAME).withRecord(new QRecord().withValue("title", "Shared one"))).getRecords().get(0);
Integer id = notebook.getValueInteger("id");
new InsertAction().execute(new InsertInput(SqlWorkbenchNotebookShare.TABLE_NAME).withRecord(new QRecord()
.withValue("notebookId", id).withValue("audienceType", "USER").withValue("audienceId", READER)));
SystemUser.run(() ->
{
audit(WRITER, id);
audit(READER, id);
audit(READER, id);
});
assertThat(runs(id)).hasSize(1);
loginAs(READER);
assertThat(runs(id)).hasSize(2).allSatisfy(r -> assertThat(r.getValueString("userId")).isEqualTo(READER));
loginAs(ADMIN);
assertThat(runs(id)).hasSize(3);
assertThat(runs(id).get(0).getValueInteger("id")).isGreaterThan(runs(id).get(2).getValueInteger("id"));
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testIntegrationHooks()
{
assertThat(SqlWorkbenchIntegrations.mainTableNames(config)).containsExactly(SqlWorkbenchNotebook.TABLE_NAME, SqlWorkbenchExecution.TABLE_NAME);
assertThat(SqlWorkbenchIntegrations.esbTableNames(config)).isEqualTo(SqlWorkbenchIntegrations.mainTableNames(config));
List<SqlWorkbenchIntegrations.SearchableTable> searchable = SqlWorkbenchIntegrations.quickSearchTables(config);
assertThat(searchable).extracting(SqlWorkbenchIntegrations.SearchableTable::tableName).containsExactlyElementsOf(SqlWorkbenchIntegrations.mainTableNames(config));
for(SqlWorkbenchIntegrations.SearchableTable table : searchable)
{
QTableMetaData tableMetaData = qInstance.getTable(table.tableName());
assertThat(table.fieldWeights().keySet()).allSatisfy(field -> assertThat(tableMetaData.getFields()).containsKey(field));
assertThat(tableMetaData.getFields()).containsKey(table.basepullTimestamp());
assertThat(table.recordLabelFields()).allSatisfy(field -> assertThat(tableMetaData.getFields()).containsKey(field));
}
assertThat(searchable.get(0).fieldWeights()).containsEntry("title", 10).containsKey("description").containsKey("ownerName");
assertThat(searchable.get(1).fieldWeights()).containsKey("sqlText").containsKey("userName").doesNotContainKey("parametersJson");
///////////////////////////////////////
// the hooks follow the table prefix //
///////////////////////////////////////
SqlWorkbenchQBitConfig prefixed = new SqlWorkbenchQBitConfig().withTableNamePrefix("myNs");
assertThat(SqlWorkbenchIntegrations.esbTableNames(prefixed)).containsExactly("my_ns_sql_workbench_notebook", "my_ns_sql_workbench_execution");
assertThat(SqlWorkbenchIntegrations.quickSearchTables(prefixed).get(1).tableName()).isEqualTo("my_ns_sql_workbench_execution");
}
/*******************************************************************************
** The notebook's Runs child list as the session sees it.
*******************************************************************************/
private List<QRecord> runs(Integer notebookId) throws QException
{
QWidgetMetaDataInterface widget = qInstance.getWidget(SqlWorkbenchTableMetaDataHelper.JOIN_NOTEBOOK_EXECUTION);
Map<String, String> params = new HashMap<>();
params.put("id", String.valueOf(notebookId));
ChildRecordListData data = (ChildRecordListData) new RenderWidgetAction().execute(new RenderWidgetInput().withWidgetMetaData(widget).withUrlParams(params)).getWidgetData();
return (data.getQueryOutput().getRecords());
}
/*******************************************************************************
** One audit row.
*******************************************************************************/
private static void audit(String user, Integer notebookId) throws QException
{
Map<String, Serializable> values = new HashMap<>();
values.put("executionUuid", UUID.randomUUID().toString());
values.put("userId", user);
values.put("mode", "READ_ONLY");
values.put("status", "SUCCEEDED");
values.put("sqlText", "SELECT 1");
values.put("notebookId", notebookId);
values.put("startTime", Instant.now());
QRecord record = new QRecord();
values.forEach(record::setValue);
new InsertAction().execute(new InsertInput(SqlWorkbenchExecution.TABLE_NAME).withRecord(record));
}
}
@@ -1,177 +0,0 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.metadata;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.kingsrook.qqq.backend.core.actions.permissions.PermissionsHelper;
import com.kingsrook.qqq.backend.core.actions.permissions.TablePermissionSubType;
import com.kingsrook.qqq.backend.core.exceptions.QNotFoundException;
import com.kingsrook.qqq.backend.core.exceptions.QPermissionDeniedException;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qbits.sqlworkbench.BaseSqlWorkbenchTest;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchRuntime;
import com.kingsrook.qbits.sqlworkbench.api.SqlWorkbenchApi;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchExecution;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookVersion;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchResultSnapshot;
import com.kingsrook.qqq.middleware.javalin.executors.TableQueryExecutor;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableQueryInput;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableQueryOutputInterface;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/*******************************************************************************
** The qbit's tables are not reachable through the generic QQQ table API by
** ordinary workbench users (only by holders of the admin permission), while
** the workbench's own endpoints keep working for them.
*******************************************************************************/
class TableAccessTest extends BaseSqlWorkbenchTest
{
private static final List<String> TABLES = List.of(SqlWorkbenchNotebook.TABLE_NAME, SqlWorkbenchCell.TABLE_NAME, SqlWorkbenchNotebookVersion.TABLE_NAME,
SqlWorkbenchResultSnapshot.TABLE_NAME, SqlWorkbenchExecution.TABLE_NAME);
/*******************************************************************************
** Query a table the way /qqq/v1/table/{table}/query does.
*******************************************************************************/
private static List<QRecord> tableApiQuery(String tableName) throws Exception
{
List<QRecord> records = new ArrayList<>();
TableQueryInput input = new TableQueryInput();
input.setTableName(tableName);
new TableQueryExecutor().execute(input, (TableQueryOutputInterface) records::addAll);
return (records);
}
/*******************************************************************************
** A writer's personal notebook (with a cell, a version and a pinned
** snapshot) and an audited execution.
*******************************************************************************/
private Integer seedWriterData(SqlWorkbenchApi api) throws Exception
{
loginAs(WRITER);
Map<String, Object> notebook = api.notebooks().create(Map.of("title", "Private payroll", "cells",
List.of(Map.of("cellKey", "a", "cellType", "SQL", "content", "SELECT salary FROM person"))), caller(WRITER));
Integer id = (Integer) notebook.get("id");
Map<String, Object> response = api.execute(caller(WRITER), Map.of("backendName", BACKEND_NAME, "sql", "SELECT first_name, salary FROM person ORDER BY id"));
assertThat(response.get("status")).isEqualTo("SUCCEEDED");
api.createSnapshot(caller(WRITER), id, Map.of("executionUuid", response.get("executionUuid"), "cellKey", "a"));
return (id);
}
/*******************************************************************************
** Another ordinary user cannot read the writer's personal notebook, cells,
** versions, snapshot or audit row through the generic table API, nor
** write any of those tables; the owner cannot either (no generic access
** at all). The workbench API still hides the notebook from the other user.
*******************************************************************************/
@Test
void testOrdinaryUsersHaveNoGenericTableAccess() throws Exception
{
SqlWorkbenchApi api = new SqlWorkbenchApi(qInstance, SqlWorkbenchRuntime.get(qInstance));
Integer id = seedWriterData(api);
for(String user : List.of(OTHER_READER, WRITER))
{
loginAs(user);
for(String table : TABLES)
{
assertThatThrownBy(() -> tableApiQuery(table)).as(user + " querying " + table).isInstanceOf(QPermissionDeniedException.class);
for(TablePermissionSubType subType : List.of(TablePermissionSubType.READ, TablePermissionSubType.INSERT, TablePermissionSubType.EDIT, TablePermissionSubType.DELETE))
{
assertThat(PermissionsHelper.hasTablePermission(new QueryInput(table), table, subType)).as(user + " " + subType + " " + table).isFalse();
}
}
}
///////////////////////////////////////////////////////////////////
// through the workbench API the notebook does not exist for the //
// other reader, and their history holds none of the writer's //
///////////////////////////////////////////////////////////////////
loginAs(OTHER_READER);
assertThatThrownBy(() -> api.notebooks().get(id, caller(OTHER_READER))).isInstanceOf(QNotFoundException.class);
assertThatThrownBy(() -> api.notebooks().snapshots(id, caller(OTHER_READER))).isInstanceOf(QNotFoundException.class);
assertThat(api.notebooks().list("mine", caller(OTHER_READER))).isEmpty();
assertThat(api.notebooks().list("shared", caller(OTHER_READER))).isEmpty();
assertThat((List<?>) api.history(caller(OTHER_READER), 50, null).get("executions")).isEmpty();
}
/*******************************************************************************
** The workbench's own endpoints work for an ordinary user (session without
** the admin permission): notebooks, versions, snapshots, history.
*******************************************************************************/
@Test
void testWorkbenchApiStillWorksForOrdinaryUsers() throws Exception
{
SqlWorkbenchApi api = new SqlWorkbenchApi(qInstance, SqlWorkbenchRuntime.get(qInstance));
Integer id = seedWriterData(api);
loginAs(WRITER);
assertThat(api.notebooks().get(id, caller(WRITER)).get("title")).isEqualTo("Private payroll");
assertThat(api.notebooks().update(id, Map.of("title", "Payroll"), caller(WRITER)).get("currentVersion")).isEqualTo(2);
assertThat(api.notebooks().versions(id, caller(WRITER))).hasSize(2);
assertThat(api.notebooks().snapshots(id, caller(WRITER))).hasSize(1);
assertThat(api.notebooks().list("mine", caller(WRITER))).hasSize(1);
assertThat((List<?>) api.history(caller(WRITER), 50, null).get("executions")).hasSize(1);
api.notebooks().publish(id, true, caller(WRITER));
loginAs(READER);
assertThat(api.notebooks().get(id, caller(READER)).get("canEdit")).isEqualTo(false);
assertThat(api.notebooks().duplicate(id, caller(READER)).get("ownerUserId")).isEqualTo(READER);
loginAs(WRITER);
api.notebooks().delete(id, caller(WRITER));
assertThat(api.notebooks().list("mine", caller(WRITER))).isEmpty();
}
/*******************************************************************************
** A holder of the admin permission can read everything through the
** generic table API.
*******************************************************************************/
@Test
void testAdminHasGenericTableAccess() throws Exception
{
SqlWorkbenchApi api = new SqlWorkbenchApi(qInstance, SqlWorkbenchRuntime.get(qInstance));
seedWriterData(api);
loginAs(ADMIN);
assertThat(tableApiQuery(SqlWorkbenchNotebook.TABLE_NAME)).hasSize(1);
assertThat(tableApiQuery(SqlWorkbenchCell.TABLE_NAME)).hasSize(1);
assertThat(tableApiQuery(SqlWorkbenchNotebookVersion.TABLE_NAME)).hasSize(1);
assertThat(tableApiQuery(SqlWorkbenchResultSnapshot.TABLE_NAME)).hasSize(1);
assertThat(tableApiQuery(SqlWorkbenchExecution.TABLE_NAME)).hasSize(1);
}
}
@@ -0,0 +1,994 @@
/*
* Copyright 2026 Kingsrook, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kingsrook.qbits.sqlworkbench.metadata;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import com.kingsrook.qbits.sqlworkbench.BaseSqlWorkbenchTest;
import com.kingsrook.qbits.sqlworkbench.SqlWorkbenchRuntime;
import com.kingsrook.qbits.sqlworkbench.SystemUser;
import com.kingsrook.qbits.sqlworkbench.api.SqlWorkbenchApi;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchCell;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchExecution;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebook;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookShare;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchNotebookVersion;
import com.kingsrook.qbits.sqlworkbench.model.SqlWorkbenchResultSnapshot;
import com.kingsrook.qbits.sqlworkbench.processes.SqlWorkbenchRecordActionSteps;
import com.kingsrook.qqq.backend.core.actions.permissions.PermissionsHelper;
import com.kingsrook.qqq.backend.core.actions.permissions.TablePermissionSubType;
import com.kingsrook.qqq.backend.core.actions.processes.RunProcessAction;
import com.kingsrook.qqq.backend.core.actions.tables.CountAction;
import com.kingsrook.qqq.backend.core.actions.tables.DeleteAction;
import com.kingsrook.qqq.backend.core.actions.tables.GetAction;
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
import com.kingsrook.qqq.backend.core.actions.tables.QueryAction;
import com.kingsrook.qqq.backend.core.actions.tables.UpdateAction;
import com.kingsrook.qqq.backend.core.actions.values.SearchPossibleValueSourceAction;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.exceptions.QNotFoundException;
import com.kingsrook.qqq.backend.core.exceptions.QPermissionDeniedException;
import com.kingsrook.qqq.backend.core.model.actions.processes.RunProcessInput;
import com.kingsrook.qqq.backend.core.model.actions.processes.RunProcessOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.count.CountInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.get.GetInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
import com.kingsrook.qqq.backend.core.model.actions.values.SearchPossibleValueSourceInput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.middleware.javalin.executors.TableCountExecutor;
import com.kingsrook.qqq.middleware.javalin.executors.TableDeleteExecutor;
import com.kingsrook.qqq.middleware.javalin.executors.TableExportExecutor;
import com.kingsrook.qqq.middleware.javalin.executors.TableGetExecutor;
import com.kingsrook.qqq.middleware.javalin.executors.TableInsertExecutor;
import com.kingsrook.qqq.middleware.javalin.executors.TableQueryExecutor;
import com.kingsrook.qqq.middleware.javalin.executors.TableUpdateExecutor;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableCountInput;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableCountOutputInterface;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableDeleteInput;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableDeleteOutputInterface;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableExportInput;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableExportOutputInterface;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableGetInput;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableGetOutputInterface;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableInsertInput;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableInsertOutputInterface;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableQueryInput;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableQueryOutputInterface;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableUpdateInput;
import com.kingsrook.qqq.middleware.javalin.executors.io.TableUpdateOutputInterface;
import com.kingsrook.qqq.backend.core.model.actions.reporting.ReportFormat;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/*******************************************************************************
** The workbench tables' security, role x table x action: who sees which
** rows through the /qqq/v1 table API (query, count, get, export), the core
** actions, PVS lookups and record actions, and who may write them.
**
** Fixture (all created by OWNER through the notebook endpoints or tables):
** - "Private": personal, one cell, one pinned result
** - "For reader-3": shared with the user reader-3
** - "For analysts": shared with the group Analysts (reader-4 is a member)
** - "Library": published to the shared library (everyone)
** - "Other's own": OTHER's personal notebook
** Every notebook has one cell and (at least) one version.
*******************************************************************************/
class TableSecurityMatrixTest extends BaseSqlWorkbenchTest
{
private static final String OWNER = WRITER;
private static final String OTHER = OTHER_READER;
private static final String SHARED_USER = "reader-3";
private static final String GROUP_MEMBER = "reader-4";
private static final String GROUP = "Analysts";
private static final String OTHER_SHARER = "writer-2";
private static final String OUTSIDER = "nobody-1";
private static final List<String> ALL_TITLES = List.of("Private", "For reader-3", "For analysts", "Library", "Other's own");
private SqlWorkbenchApi api;
private Map<String, Integer> ids = new LinkedHashMap<>();
/*******************************************************************************
**
*******************************************************************************/
@BeforeEach
void seed() throws Exception
{
api = new SqlWorkbenchApi(qInstance, SqlWorkbenchRuntime.get(qInstance));
for(String title : List.of("Private", "For reader-3", "For analysts", "Library"))
{
ids.put(title, notebook(OWNER, title));
}
ids.put("Other's own", notebook(OTHER, "Other's own"));
loginAs(OWNER);
insertShare(ids.get("For reader-3"), "USER", SHARED_USER);
tableInsert(SqlWorkbenchNotebookShare.TABLE_NAME, Map.of("notebookId", ids.get("For analysts"), "audienceType", "GROUP", "audienceId", GROUP));
api.notebooks().publish(ids.get("Library"), true, api.caller(null));
Map<String, Object> run = api.execute(as(OWNER), Map.of("backendName", BACKEND_NAME, "sql", "SELECT first_name, salary FROM person ORDER BY id"));
api.createSnapshot(as(OWNER), ids.get("Private"), Map.of("executionUuid", run.get("executionUuid"), "cellKey", "c1"));
api.execute(as(OTHER), Map.of("backendName", BACKEND_NAME, "sql", "SELECT 1"));
}
/*******************************************************************************
** Create a notebook with one cell through the notebook endpoint.
*******************************************************************************/
private Integer notebook(String user, String title) throws Exception
{
return ((Integer) api.notebooks().create(Map.of("title", title, "cells", List.of(Map.of("cellKey", "c1", "cellType", "SQL", "content", "SELECT 1"))), as(user)).get("id"));
}
/*******************************************************************************
** Log in as a user (the group member with their group).
*******************************************************************************/
private void login(String user)
{
if(GROUP_MEMBER.equals(user))
{
loginAs(user, GROUP);
}
else
{
loginAs(user);
}
}
/*******************************************************************************
** The notebooks each user may read.
*******************************************************************************/
private static Set<String> readableTitles(String user)
{
return (new TreeSet<>(switch(user)
{
case OWNER -> List.of("Private", "For reader-3", "For analysts", "Library");
case OTHER -> List.of("Library", "Other's own");
case SHARED_USER -> List.of("For reader-3", "Library");
case GROUP_MEMBER -> List.of("For analysts", "Library");
case OTHER_SHARER -> List.of("Library");
case ADMIN -> ALL_TITLES;
default -> List.<String>of();
}));
}
/*******************************************************************************
** Reads: v1 query / count / get / export, core query, and PVS lookups
** agree for every role on every table.
*******************************************************************************/
@Test
void testReadMatrix() throws Exception
{
for(String user : List.of(OWNER, OTHER, SHARED_USER, GROUP_MEMBER, OTHER_SHARER, ADMIN))
{
login(user);
Set<String> titles = readableTitles(user);
int readable = titles.size();
//////////////////////////////////////////////
// notebooks: v1 query, count, export, get //
//////////////////////////////////////////////
assertThat(new TreeSet<>(v1Query(SqlWorkbenchNotebook.TABLE_NAME).stream().map(r -> r.getValueString("title")).toList())).as(user + " v1 query notebooks").isEqualTo(titles);
assertThat(v1Count(SqlWorkbenchNotebook.TABLE_NAME)).as(user + " v1 count notebooks").isEqualTo(readable);
assertThat(v1ExportRows(SqlWorkbenchNotebook.TABLE_NAME)).as(user + " v1 export notebooks").isEqualTo(readable);
for(String title : ALL_TITLES)
{
Integer id = ids.get(title);
if(titles.contains(title))
{
assertThat(v1Get(SqlWorkbenchNotebook.TABLE_NAME, id).getValueString("title")).as(user + " v1 get " + title).isEqualTo(title);
assertThat(new GetAction().executeForRecord(new GetInput(SqlWorkbenchNotebook.TABLE_NAME).withPrimaryKey(id))).as(user + " core get " + title).isNotNull();
}
else
{
assertThatThrownBy(() -> v1Get(SqlWorkbenchNotebook.TABLE_NAME, id)).as(user + " v1 get " + title).isInstanceOf(QNotFoundException.class);
assertThat(new GetAction().executeForRecord(new GetInput(SqlWorkbenchNotebook.TABLE_NAME).withPrimaryKey(id))).as(user + " core get " + title).isNull();
}
}
/////////////////////////////////////////////////////////////////////////
// children follow their notebook: one cell and one version per //
// readable notebook; only "Private" has a pinned result //
/////////////////////////////////////////////////////////////////////////
assertThat(v1Count(SqlWorkbenchCell.TABLE_NAME)).as(user + " cells").isEqualTo(readable);
assertThat(v1Query(SqlWorkbenchNotebookVersion.TABLE_NAME)).as(user + " versions").extracting(r -> r.getValueInteger("notebookId"))
.containsExactlyInAnyOrderElementsOf(titles.stream().map(ids::get).toList());
assertThat(v1Count(SqlWorkbenchResultSnapshot.TABLE_NAME)).as(user + " snapshots").isEqualTo(titles.contains("Private") ? 1 : 0);
assertThat(v1ExportRows(SqlWorkbenchCell.TABLE_NAME)).as(user + " export cells").isEqualTo(readable);
////////////////////////////////////////////////////////////////////
// shares: the owner sees their notebooks' shares, an audience //
// sees the shares that name it, admins see all //
////////////////////////////////////////////////////////////////////
List<String> shareKeys = v1Query(SqlWorkbenchNotebookShare.TABLE_NAME).stream().map(r -> r.getValueString("audienceKey")).sorted().toList();
assertThat(shareKeys).as(user + " shares").isEqualTo(switch(user)
{
case OWNER, ADMIN -> List.of("all", "group:analysts", "user:reader-3");
case SHARED_USER -> List.of("all", "user:reader-3");
case GROUP_MEMBER -> List.of("all", "group:analysts");
default -> List.of("all");
});
/////////////////////////////////////////////////
// the audit: own executions; admins see all //
/////////////////////////////////////////////////
assertThat(v1Query(SqlWorkbenchExecution.TABLE_NAME)).as(user + " executions").extracting(r -> r.getValueString("userId"))
.containsExactlyInAnyOrderElementsOf(switch(user)
{
case OWNER -> List.of(OWNER);
case OTHER -> List.of(OTHER);
case ADMIN -> List.of(OWNER, OTHER);
default -> List.of();
});
////////////////////////////////////////////////////////////////
// notebook PVS lookups (labels of notebookId fields) only //
// show readable notebooks //
////////////////////////////////////////////////////////////////
List<String> labels = new ArrayList<>();
new SearchPossibleValueSourceAction().execute(new SearchPossibleValueSourceInput().withPossibleValueSourceName(SqlWorkbenchNotebook.TABLE_NAME).withSearchTerm(""))
.getResults().forEach(pv -> labels.add(pv.getLabel()));
assertThat(new TreeSet<>(labels)).as(user + " notebook PVS").isEqualTo(titles);
}
}
/*******************************************************************************
** Users without workbench access: no table permission (v1 refuses), and
** no security keys (the core actions find nothing, not even the library).
*******************************************************************************/
@Test
void testOutsider() throws Exception
{
loginAs(OUTSIDER);
for(String table : AppTables.ALL)
{
assertThatThrownBy(() -> v1Query(table)).as(table).isInstanceOf(QPermissionDeniedException.class);
for(TablePermissionSubType subType : TablePermissionSubType.values())
{
assertThat(PermissionsHelper.hasTablePermission(new QueryInput(table), table, subType)).as(subType + " " + table).isFalse();
}
assertThat(new CountAction().execute(new CountInput(table)).getCount()).as("core count " + table).isZero();
}
}
/*******************************************************************************
** Writes: only the owner (or an admin) changes a notebook or its children;
** being shared with, or seeing it in the library, is read-only.
*******************************************************************************/
@Test
void testWriteMatrix() throws Exception
{
Integer privateId = ids.get("Private");
Integer userShared = ids.get("For reader-3");
Integer groupShared = ids.get("For analysts");
Integer library = ids.get("Library");
//////////////////////////////////////////////////////////////
// readable but not theirs: update / delete / child writes //
//////////////////////////////////////////////////////////////
login(SHARED_USER);
assertThatThrownBy(() -> v1Update(SqlWorkbenchNotebook.TABLE_NAME, userShared, Map.of("title", "hijacked"))).isInstanceOf(QException.class);
assertThat(v1Delete(SqlWorkbenchNotebook.TABLE_NAME, userShared)).isZero();
assertThatThrownBy(() -> tableInsert(SqlWorkbenchCell.TABLE_NAME, Map.of("notebookId", userShared, "content", "SELECT 2"))).isInstanceOf(QException.class);
Integer sharedCell = cellOf(userShared);
assertThatThrownBy(() -> v1Update(SqlWorkbenchCell.TABLE_NAME, sharedCell, Map.of("content", "DROP TABLE x"))).isInstanceOf(QException.class);
assertThat(v1Delete(SqlWorkbenchCell.TABLE_NAME, sharedCell)).isZero();
login(GROUP_MEMBER);
Integer groupCell = cellOf(groupShared);
assertThatThrownBy(() -> v1Update(SqlWorkbenchCell.TABLE_NAME, groupCell, Map.of("content", "x"))).isInstanceOf(QException.class);
login(OTHER);
assertThatThrownBy(() -> v1Update(SqlWorkbenchNotebook.TABLE_NAME, library, Map.of("title", "hijacked"))).isInstanceOf(QException.class);
assertThat(v1Delete(SqlWorkbenchNotebook.TABLE_NAME, library)).isZero();
assertThatThrownBy(() -> v1Update(SqlWorkbenchNotebook.TABLE_NAME, privateId, Map.of("title", "hijacked"))).isInstanceOf(QException.class);
assertThatThrownBy(() -> tableInsert(SqlWorkbenchCell.TABLE_NAME, Map.of("notebookId", privateId, "content", "SELECT 2"))).isInstanceOf(QException.class);
////////////////////////////////////////////////////////////////////////////
// sharing: needs the share permission, and only for one's own notebooks //
////////////////////////////////////////////////////////////////////////////
assertThatThrownBy(() -> tableInsert(SqlWorkbenchNotebookShare.TABLE_NAME, Map.of("notebookId", library, "audienceType", "USER", "audienceId", OTHER)))
.isInstanceOf(QPermissionDeniedException.class);
login(OTHER_SHARER);
assertThatThrownBy(() -> tableInsert(SqlWorkbenchNotebookShare.TABLE_NAME, Map.of("notebookId", library, "audienceType", "USER", "audienceId", OTHER_SHARER)))
.isInstanceOf(QException.class);
Integer libraryShare = shareOf(library);
assertThat(v1Delete(SqlWorkbenchNotebookShare.TABLE_NAME, libraryShare)).isZero();
//////////////////////////////////////////////////////////////
// inserting a notebook "for" someone else makes it yours //
//////////////////////////////////////////////////////////////
login(OTHER);
QRecord forged = tableInsert(SqlWorkbenchNotebook.TABLE_NAME, Map.of("title", "Forged", "ownerUserId", OWNER, "visibility", "SHARED"));
assertThat(forged.getValueString("ownerUserId")).isEqualTo(OTHER);
assertThat(forged.getValueString("visibility")).isEqualTo("PERSONAL");
///////////////////////////////////////////////////
// nothing above changed the owner's notebooks //
///////////////////////////////////////////////////
List<QRecord> stored = SystemUser.call(() -> QueryAction.execute(SqlWorkbenchNotebook.TABLE_NAME, new QQueryFilter(new QFilterCriteria("id", QCriteriaOperator.IN, List.of(privateId, userShared, groupShared, library)))));
assertThat(stored).extracting(r -> r.getValueString("title")).containsExactlyInAnyOrder("Private", "For reader-3", "For analysts", "Library");
assertThat(SystemUser.call(() -> QueryAction.execute(SqlWorkbenchCell.TABLE_NAME, new QQueryFilter()))).extracting(r -> r.getValueString("content")).containsOnly("SELECT 1");
//////////////////////////////////////////////////////
// the owner, and an admin, may change the notebook //
//////////////////////////////////////////////////////
login(OWNER);
assertThat(v1Update(SqlWorkbenchNotebook.TABLE_NAME, privateId, Map.of("title", "Private v2")).getValueString("title")).isEqualTo("Private v2");
login(ADMIN);
assertThat(v1Update(SqlWorkbenchNotebook.TABLE_NAME, privateId, Map.of("title", "Private v3")).getValueString("title")).isEqualTo("Private v3");
assertThat(v1Get(SqlWorkbenchNotebook.TABLE_NAME, privateId).getValueString("ownerUserId")).isEqualTo(OWNER);
}
/*******************************************************************************
** Versions and the audit are append-only for everyone - even an admin
** holding (ad hoc) write permissions on them - and pinned results can
** only be created by pinning, and only relabeled.
*******************************************************************************/
@Test
void testAppendOnlyAndPinnedResults() throws Exception
{
for(String user : List.of(OWNER, ADMIN))
{
login(user);
for(String table : List.of(SqlWorkbenchNotebookVersion.TABLE_NAME, SqlWorkbenchExecution.TABLE_NAME))
{
QContext.getQSession().withPermission(table + ".insert").withPermission(table + ".edit").withPermission(table + ".delete");
Integer id = v1Query(table).get(0).getValueInteger("id");
assertThatThrownBy(() -> v1Update(table, id, Map.of("comment", "rewritten", "sqlText", "rewritten"))).as(user + " update " + table).isInstanceOf(QException.class);
assertThat(v1Delete(table, id)).as(user + " delete " + table).isZero();
assertThatThrownBy(() -> tableInsert(table, Map.of("notebookId", ids.get("Private"), "versionNumber", 99, "executionUuid", "x"))).as(user + " insert " + table).isInstanceOf(QException.class);
}
}
assertThat(SystemUser.call(() -> QueryAction.execute(SqlWorkbenchExecution.TABLE_NAME, new QQueryFilter()))).hasSize(2);
login(OWNER);
QContext.getQSession().withPermission(SqlWorkbenchResultSnapshot.TABLE_NAME + ".insert");
assertThatThrownBy(() -> tableInsert(SqlWorkbenchResultSnapshot.TABLE_NAME, Map.of("notebookId", ids.get("Private"), "resultJson", "{\"forged\":true}")))
.isInstanceOf(QException.class).hasMessageContaining("pinned from the notebook");
Integer snapshotId = v1Query(SqlWorkbenchResultSnapshot.TABLE_NAME).get(0).getValueInteger("id");
assertThat(v1Update(SqlWorkbenchResultSnapshot.TABLE_NAME, snapshotId, Map.of("label", "Payroll")).getValueString("label")).isEqualTo("Payroll");
assertThatThrownBy(() -> v1Update(SqlWorkbenchResultSnapshot.TABLE_NAME, snapshotId, Map.of("rowCount", 999))).isInstanceOf(QException.class);
assertThat(v1Delete(SqlWorkbenchResultSnapshot.TABLE_NAME, snapshotId)).isEqualTo(1);
}
/*******************************************************************************
** Table edits keep the same invariants as the notebook UI: ordering,
** keys, versions, library visibility, cascades.
*******************************************************************************/
@Test
@SuppressWarnings("unchecked")
void testTableEditsKeepNotebookInvariants() throws Exception
{
login(OWNER);
Integer id = ids.get("Private");
assertThat(currentVersion(id)).isEqualTo(1);
////////////////////////////////////////////////////////////////////////
// a cell added through the table goes last, gets a key, and appends //
// a version; the notebook endpoint shows it //
////////////////////////////////////////////////////////////////////////
QRecord cell = tableInsert(SqlWorkbenchCell.TABLE_NAME, Map.of("notebookId", id, "cellType", "markdown", "content", "# Notes"));
assertThat(cell.getValueInteger("sortOrder")).isEqualTo(1);
assertThat(cell.getValueString("cellKey")).isNotBlank();
assertThat(cell.getValueString("cellType")).isEqualTo("MARKDOWN");
assertThat(currentVersion(id)).isEqualTo(2);
List<Map<String, Object>> cells = (List<Map<String, Object>>) api.notebooks().get(id, as(OWNER)).get("cells");
assertThat(cells).extracting(c -> c.get("content")).containsExactly("SELECT 1", "# Notes");
v1Update(SqlWorkbenchCell.TABLE_NAME, cell.getValueInteger("id"), Map.of("content", "# More notes"));
assertThat(currentVersion(id)).isEqualTo(3);
assertThat(api.notebooks().version(id, 3, as(OWNER)).get("notebook").toString()).contains("# More notes");
assertThatThrownBy(() -> v1Update(SqlWorkbenchCell.TABLE_NAME, cell.getValueInteger("id"), Map.of("notebookId", ids.get("Library")))).isInstanceOf(QException.class);
assertThatThrownBy(() -> v1Update(SqlWorkbenchCell.TABLE_NAME, cell.getValueInteger("id"), Map.of("cellType", "PYTHON"))).isInstanceOf(QException.class);
assertThatThrownBy(() -> tableInsert(SqlWorkbenchCell.TABLE_NAME, Map.of("notebookId", id, "content", "x".repeat(config.getMaxSqlLength() + 1)))).isInstanceOf(QException.class);
assertThat(v1Delete(SqlWorkbenchCell.TABLE_NAME, cell.getValueInteger("id"))).isEqualTo(1);
assertThat(currentVersion(id)).isEqualTo(4);
///////////////////////////////////////////////////////////////
// notebook edits through the table: versioned; visibility //
// and system fields cannot be set directly //
///////////////////////////////////////////////////////////////
v1Update(SqlWorkbenchNotebook.TABLE_NAME, id, Map.of("description", "via the table", "currentVersion", 99));
assertThat(currentVersion(id)).isEqualTo(5);
assertThatThrownBy(() -> v1Update(SqlWorkbenchNotebook.TABLE_NAME, id, Map.of("visibility", "SHARED"))).isInstanceOf(QException.class);
assertThatThrownBy(() -> v1Update(SqlWorkbenchNotebook.TABLE_NAME, id, Map.of("title", " "))).isInstanceOf(QException.class);
assertThatThrownBy(() -> v1Update(SqlWorkbenchNotebook.TABLE_NAME, id, Map.of("parametersJson", "{not a list"))).isInstanceOf(QException.class);
QRecord created = tableInsert(SqlWorkbenchNotebook.TABLE_NAME, Map.of("title", "Made in the table"));
assertThat(created.getValueInteger("currentVersion")).isEqualTo(0);
assertThat(currentVersion(created.getValueInteger("id"))).isEqualTo(1);
////////////////////////////////////////////////////////////////////
// publishing through the share table = publishing in the UI //
////////////////////////////////////////////////////////////////////
QRecord share = tableInsert(SqlWorkbenchNotebookShare.TABLE_NAME, Map.of("notebookId", id, "audienceType", "all", "audienceId", "ignored"));
assertThat(share.getValueString("audienceKey")).isEqualTo("all");
assertThat(share.getValueString("audienceId")).isNull();
assertThat(api.notebooks().get(id, as(OWNER)).get("visibility")).isEqualTo("SHARED");
assertThat(api.notebooks().list("shared", as(OTHER))).extracting(n -> n.get("id")).contains(id);
login(OWNER);
assertThatThrownBy(() -> tableInsert(SqlWorkbenchNotebookShare.TABLE_NAME, Map.of("notebookId", id, "audienceType", "ALL"))).isInstanceOf(QException.class);
assertThatThrownBy(() -> tableInsert(SqlWorkbenchNotebookShare.TABLE_NAME, Map.of("notebookId", id, "audienceType", "USER"))).isInstanceOf(QException.class);
assertThatThrownBy(() -> tableInsert(SqlWorkbenchNotebookShare.TABLE_NAME, Map.of("notebookId", id, "audienceType", "EVERYONE"))).isInstanceOf(QException.class);
assertThat(v1Delete(SqlWorkbenchNotebookShare.TABLE_NAME, share.getValueInteger("id"))).isEqualTo(1);
assertThat(api.notebooks().get(id, as(OWNER)).get("visibility")).isEqualTo("PERSONAL");
////////////////////////////////////////////////////////////////
// a user share with different case still reaches the user //
////////////////////////////////////////////////////////////////
login(OWNER);
tableInsert(SqlWorkbenchNotebookShare.TABLE_NAME, Map.of("notebookId", id, "audienceType", "USER", "audienceId", " READER-2 "));
assertThat(api.notebooks().get(id, as(OTHER)).get("canEdit")).isEqualTo(false);
////////////////////////////////////////////////////////////////
// deleting a notebook through the table removes its children //
////////////////////////////////////////////////////////////////
login(OWNER);
assertThat(v1Delete(SqlWorkbenchNotebook.TABLE_NAME, id)).isEqualTo(1);
for(String child : List.of(SqlWorkbenchCell.TABLE_NAME, SqlWorkbenchNotebookVersion.TABLE_NAME, SqlWorkbenchResultSnapshot.TABLE_NAME, SqlWorkbenchNotebookShare.TABLE_NAME))
{
assertThat(SystemUser.call(() -> QueryAction.execute(child, new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.EQUALS, id))))).as(child).isEmpty();
}
assertThat(SystemUser.call(() -> QueryAction.execute(SqlWorkbenchExecution.TABLE_NAME, new QQueryFilter()))).as("the audit survives").hasSize(2);
}
/*******************************************************************************
** Mixed batches and re-targeting: only the allowed rows change, and the
** qbit's own bookkeeping (versions, visibility, cascades) never touches
** a notebook the caller may not change.
*******************************************************************************/
@Test
void testMixedBatchesRetargetingAndPlanting() throws Exception
{
Integer privateId = ids.get("Private");
Integer library = ids.get("Library");
Integer othersOwn = ids.get("Other's own");
///////////////////////////////////////////////////////////////
// one insert batch: a cell in my notebook and one in OTHER's //
///////////////////////////////////////////////////////////////
login(OWNER);
List<QRecord> inserted = new InsertAction().execute(new InsertInput(SqlWorkbenchCell.TABLE_NAME).withRecords(List.of(
new QRecord().withValue("notebookId", privateId).withValue("content", "SELECT 'mine'"),
new QRecord().withValue("notebookId", othersOwn).withValue("content", "SELECT 'theirs'")))).getRecords();
assertThat(inserted.get(0).getErrors()).isNullOrEmpty();
assertThat(inserted.get(1).getErrors()).isNotEmpty();
assertThat(currentVersion(privateId)).isEqualTo(2);
assertThat(currentVersion(othersOwn)).isEqualTo(1);
assertThat(SystemUser.call(() -> QueryAction.execute(SqlWorkbenchCell.TABLE_NAME, new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.EQUALS, othersOwn))))).hasSize(1);
/////////////////////////////////////////////////////////////////////
// one update batch: OTHER's own cell and a (readable) library cell //
/////////////////////////////////////////////////////////////////////
login(OTHER);
List<QRecord> updated = new UpdateAction().execute(new UpdateInput(SqlWorkbenchCell.TABLE_NAME).withRecords(List.of(
new QRecord().withValue("id", cellOf(othersOwn)).withValue("content", "SELECT 'edited'"),
new QRecord().withValue("id", cellOf(library)).withValue("content", "SELECT 'hijacked'")))).getRecords();
assertThat(updated.get(0).getErrors()).isNullOrEmpty();
assertThat(updated.get(1).getErrors()).isNotEmpty();
assertThat(currentVersion(othersOwn)).isEqualTo(2);
assertThat(currentVersion(library)).isEqualTo(1);
//////////////////////////////////////////////////////////////////////
// re-targeting someone else's rows into my notebook is refused by //
// the stored-row lock (the new value alone would pass) //
//////////////////////////////////////////////////////////////////////
assertThatThrownBy(() -> v1Update(SqlWorkbenchCell.TABLE_NAME, cellOf(library), Map.of("notebookId", othersOwn))).isInstanceOf(QException.class);
login(OTHER_SHARER);
Integer sharersOwn = notebook(OTHER_SHARER, "Sharer's own");
login(OTHER_SHARER);
assertThatThrownBy(() -> v1Update(SqlWorkbenchNotebookShare.TABLE_NAME, shareOf(library), Map.of("notebookId", sharersOwn))).isInstanceOf(QException.class);
assertThat(SystemUser.call(() -> new GetAction().executeForRecord(new GetInput(SqlWorkbenchNotebookShare.TABLE_NAME).withPrimaryKey(shareOf(library)))).getValueInteger("notebookId")).isEqualTo(library);
////////////////////////////////////////////////////////////////////////
// planting: an owner cannot hand a notebook to someone else //
////////////////////////////////////////////////////////////////////////
login(OWNER);
assertThatThrownBy(() -> v1Update(SqlWorkbenchNotebook.TABLE_NAME, privateId, Map.of("ownerUserId", OTHER))).isInstanceOf(QException.class);
assertThat(SystemUser.call(() -> new GetAction().executeForRecord(new GetInput(SqlWorkbenchNotebook.TABLE_NAME).withPrimaryKey(privateId))).getValueString("ownerUserId")).isEqualTo(OWNER);
///////////////////////////////////////////////////////////////////////
// a reader the notebook is shared with sees its pinned result, but //
// can neither relabel nor delete it //
///////////////////////////////////////////////////////////////////////
insertShare(privateId, "USER", SHARED_USER);
login(SHARED_USER);
Integer snapshotId = v1Query(SqlWorkbenchResultSnapshot.TABLE_NAME).stream().filter(r -> privateId.equals(r.getValueInteger("notebookId"))).findFirst().orElseThrow().getValueInteger("id");
assertThatThrownBy(() -> v1Update(SqlWorkbenchResultSnapshot.TABLE_NAME, snapshotId, Map.of("label", "mine now"))).isInstanceOf(QException.class);
assertThat(v1Delete(SqlWorkbenchResultSnapshot.TABLE_NAME, snapshotId)).isZero();
///////////////////////////////////////////////////////////////////////
// denied notebook deletes never cascade: children all still there //
///////////////////////////////////////////////////////////////////////
login(OTHER);
assertThat(v1Delete(SqlWorkbenchNotebook.TABLE_NAME, library)).isZero();
login(SHARED_USER);
assertThat(v1Delete(SqlWorkbenchNotebook.TABLE_NAME, privateId)).isZero();
for(Integer id : List.of(library, privateId))
{
for(String child : List.of(SqlWorkbenchCell.TABLE_NAME, SqlWorkbenchNotebookVersion.TABLE_NAME, SqlWorkbenchNotebookShare.TABLE_NAME))
{
assertThat(SystemUser.call(() -> QueryAction.execute(child, new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.EQUALS, id))))).as(child + " of " + id).isNotEmpty();
}
}
assertThat(SystemUser.call(() -> QueryAction.execute(SqlWorkbenchResultSnapshot.TABLE_NAME, new QQueryFilter()))).hasSize(1);
}
/*******************************************************************************
** Editing a share re-targets it (new audience key, visibility in step);
** audiences cannot edit or remove the share that names them.
*******************************************************************************/
@Test
void testShareEdits() throws Exception
{
Integer notebookId = ids.get("For reader-3");
Integer shareId = shareOf(notebookId);
login(OWNER);
QRecord retargeted = v1Update(SqlWorkbenchNotebookShare.TABLE_NAME, shareId, Map.of("audienceId", GROUP_MEMBER, "audienceKey", "all", "createdByUserId", "forged"));
assertThat(retargeted.getValueString("audienceKey")).isEqualTo("user:reader-4");
login(GROUP_MEMBER);
assertThat(v1Get(SqlWorkbenchNotebook.TABLE_NAME, notebookId).getValueString("title")).isEqualTo("For reader-3");
login(SHARED_USER);
assertThatThrownBy(() -> v1Get(SqlWorkbenchNotebook.TABLE_NAME, notebookId)).isInstanceOf(QNotFoundException.class);
login(OWNER);
assertThatThrownBy(() -> v1Update(SqlWorkbenchNotebookShare.TABLE_NAME, shareId, Map.of("notebookId", ids.get("Private")))).isInstanceOf(QException.class);
v1Update(SqlWorkbenchNotebookShare.TABLE_NAME, shareId, Map.of("audienceType", "ALL"));
assertThat(api.notebooks().get(notebookId, as(OWNER)).get("visibility")).isEqualTo("SHARED");
login(OWNER);
v1Update(SqlWorkbenchNotebookShare.TABLE_NAME, shareId, Map.of("audienceType", "USER", "audienceId", SHARED_USER));
assertThat(api.notebooks().get(notebookId, as(OWNER)).get("visibility")).isEqualTo("PERSONAL");
assertThat(SystemUser.call(() -> new GetAction().executeForRecord(new GetInput(SqlWorkbenchNotebookShare.TABLE_NAME).withPrimaryKey(shareId))).getValueString("createdByUserId")).isEqualTo(OWNER);
////////////////////////////////////////////////////////////////////////
// the audience can read its share row, but not change or remove it, //
// even through the core actions (no table-permission check there) //
////////////////////////////////////////////////////////////////////////
login(SHARED_USER);
assertThat(v1Get(SqlWorkbenchNotebookShare.TABLE_NAME, shareId).getValueString("audienceKey")).isEqualTo("user:reader-3");
QRecord coreUpdate = new UpdateAction().execute(new UpdateInput(SqlWorkbenchNotebookShare.TABLE_NAME)
.withRecord(new QRecord().withValue("id", shareId).withValue("audienceId", OTHER))).getRecords().get(0);
assertThat(coreUpdate.getErrors()).isNotEmpty();
DeleteOutput coreDelete = new DeleteAction().execute(new DeleteInput(SqlWorkbenchNotebookShare.TABLE_NAME).withPrimaryKeys(List.of(shareId)));
assertThat(coreDelete.getDeletedRecordCount()).isZero();
assertThat(SystemUser.call(() -> new GetAction().executeForRecord(new GetInput(SqlWorkbenchNotebookShare.TABLE_NAME).withPrimaryKey(shareId))).getValueString("audienceKey")).isEqualTo("user:reader-3");
}
/*******************************************************************************
** Record actions: Share, Duplicate, Restore Version - permission-gated,
** and their records come through the locks.
*******************************************************************************/
@Test
void testRecordActions() throws Exception
{
Integer privateId = ids.get("Private");
Integer library = ids.get("Library");
login(OWNER);
RunProcessOutput shared = runRecordAction(SqlWorkbenchRecordActionsMetaDataProducer.SHARE_NOTEBOOK, privateId, Map.of("audienceType", "USER", "audienceId", SHARED_USER));
assertThat(shared.getException()).isEmpty();
assertThat(shared.getValue(SqlWorkbenchRecordActionSteps.RESULT_MESSAGE).toString()).contains("user:reader-3");
login(SHARED_USER);
assertThat(v1Get(SqlWorkbenchNotebook.TABLE_NAME, privateId).getValueString("title")).isEqualTo("Private");
///////////////////////////////////////////////////////////////////////
// a reader without share permission cannot see or run Share; a //
// record they cannot read is simply not found by the action //
///////////////////////////////////////////////////////////////////////
login(OTHER);
assertThat(PermissionsHelper.hasProcessPermission(new RunProcessInput(), SqlWorkbenchRecordActionsMetaDataProducer.SHARE_NOTEBOOK)).isFalse();
assertThat(PermissionsHelper.hasProcessPermission(new RunProcessInput(), SqlWorkbenchRecordActionsMetaDataProducer.DUPLICATE_NOTEBOOK)).isTrue();
RunProcessOutput copy = runRecordAction(SqlWorkbenchRecordActionsMetaDataProducer.DUPLICATE_NOTEBOOK, library, Map.of());
assertThat(copy.getException()).isEmpty();
assertThat(api.notebooks().list("mine", as(OTHER))).extracting(n -> n.get("title")).contains("Copy of Library");
login(OTHER);
assertRecordActionFails(SqlWorkbenchRecordActionsMetaDataProducer.DUPLICATE_NOTEBOOK, privateId, "Too few records");
///////////////////////////////////////////////////////
// restore: the owner, as a new version; not others //
///////////////////////////////////////////////////////
login(OWNER);
api.notebooks().update(privateId, Map.of("title", "Private v2"), as(OWNER));
login(OWNER);
Integer v1 = SystemUser.call(() -> QueryAction.execute(SqlWorkbenchNotebookVersion.TABLE_NAME, new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.EQUALS, privateId))
.withCriteria(new QFilterCriteria("versionNumber", QCriteriaOperator.EQUALS, 1)))).get(0).getValueInteger("id");
RunProcessOutput restored = runRecordAction(SqlWorkbenchRecordActionsMetaDataProducer.RESTORE_VERSION, v1, Map.of());
assertThat(restored.getException()).isEmpty();
assertThat(v1Get(SqlWorkbenchNotebook.TABLE_NAME, privateId).getValueString("title")).isEqualTo("Private");
assertThat(currentVersion(privateId)).isEqualTo(3);
login(SHARED_USER);
assertRecordActionFails(SqlWorkbenchRecordActionsMetaDataProducer.RESTORE_VERSION, v1, "Only the notebook's owner");
}
/*******************************************************************************
** The notebook endpoints see exactly what the tables show.
*******************************************************************************/
@Test
void testNotebookEndpointsMatchTheTables() throws Exception
{
for(String user : List.of(OWNER, OTHER, SHARED_USER, GROUP_MEMBER, ADMIN))
{
login(user);
Set<String> fromTables = new TreeSet<>(v1Query(SqlWorkbenchNotebook.TABLE_NAME).stream().map(r -> r.getValueString("title")).toList());
login(user);
List<Map<String, Object>> mine = api.notebooks().list("mine", api.caller(null));
List<Map<String, Object>> shared = api.notebooks().list("shared", api.caller(null));
Set<String> seen = new TreeSet<>();
mine.forEach(n -> seen.add((String) n.get("title")));
shared.forEach(n -> seen.add((String) n.get("title")));
if(ADMIN.equals(user))
{
//////////////////////////////////////////////////////////////////
// admins read every notebook through the tables; the notebook //
// UI still lists only their own and what is shared with them //
//////////////////////////////////////////////////////////////////
assertThat(seen).isEqualTo(new TreeSet<>(List.of("Library")));
assertThat(fromTables).isEqualTo(new TreeSet<>(ALL_TITLES));
}
else
{
assertThat(seen).as(user).isEqualTo(fromTables);
}
for(String title : ALL_TITLES)
{
Integer id = ids.get(title);
login(user);
if(fromTables.contains(title))
{
assertThat(api.notebooks().get(id, api.caller(null)).get("title")).as(user + " " + title).isEqualTo(title);
}
else
{
assertThatThrownBy(() -> api.notebooks().get(id, api.caller(null))).as(user + " " + title).isInstanceOf(QNotFoundException.class);
}
}
}
}
/*******************************************************************************
**
*******************************************************************************/
private RunProcessOutput runRecordAction(String processName, Integer recordId, Map<String, Serializable> values) throws QException
{
RunProcessInput input = new RunProcessInput()
.withProcessName(processName)
.withFrontendStepBehavior(RunProcessInput.FrontendStepBehavior.SKIP)
.withValues(new LinkedHashMap<>(values))
.withValue("recordIds", String.valueOf(recordId));
return (new RunProcessAction().execute(input));
}
/*******************************************************************************
** A record action that must not succeed (QQQ either throws, e.g. when the
** locks hide the record, or returns the step's exception).
*******************************************************************************/
private void assertRecordActionFails(String processName, Integer recordId, String message)
{
Exception failure;
try
{
failure = runRecordAction(processName, recordId, Map.of()).getException().orElse(null);
}
catch(QException e)
{
failure = e;
}
assertThat(failure).as(processName + " on " + recordId).isNotNull();
assertThat(failure).hasMessageContaining(message);
}
/*******************************************************************************
**
*******************************************************************************/
private Integer currentVersion(Integer notebookId) throws QException
{
return (SystemUser.call(() -> QueryAction.execute(SqlWorkbenchNotebook.TABLE_NAME, new QQueryFilter(new QFilterCriteria("id", QCriteriaOperator.EQUALS, notebookId))).get(0).getValueInteger("currentVersion")));
}
/*******************************************************************************
**
*******************************************************************************/
private Integer cellOf(Integer notebookId) throws QException
{
return (SystemUser.call(() -> QueryAction.execute(SqlWorkbenchCell.TABLE_NAME, new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.EQUALS, notebookId))).get(0).getValueInteger("id")));
}
/*******************************************************************************
**
*******************************************************************************/
private Integer shareOf(Integer notebookId) throws QException
{
return (SystemUser.call(() -> QueryAction.execute(SqlWorkbenchNotebookShare.TABLE_NAME, new QQueryFilter(new QFilterCriteria("notebookId", QCriteriaOperator.EQUALS, notebookId))).get(0).getValueInteger("id")));
}
/*******************************************************************************
** Insert a share through the core action (as the session).
*******************************************************************************/
private void insertShare(Integer notebookId, String type, String audienceId) throws QException
{
QRecord record = new InsertAction().execute(new InsertInput(SqlWorkbenchNotebookShare.TABLE_NAME).withRecord(new QRecord()
.withValue("notebookId", notebookId).withValue("audienceType", type).withValue("audienceId", audienceId))).getRecords().get(0);
assertThat(record.getErrors()).isNullOrEmpty();
}
/*******************************************************************************
** v1 table API: query.
*******************************************************************************/
private static List<QRecord> v1Query(String tableName) throws Exception
{
List<QRecord> records = new ArrayList<>();
TableQueryInput input = new TableQueryInput();
input.setTableName(tableName);
new TableQueryExecutor().execute(input, (TableQueryOutputInterface) records::addAll);
return (records);
}
/*******************************************************************************
** v1 table API: count.
*******************************************************************************/
private static int v1Count(String tableName) throws Exception
{
long[] count = new long[1];
TableCountInput input = new TableCountInput();
input.setTableName(tableName);
new TableCountExecutor().execute(input, new TableCountOutputInterface()
{
@Override
public void setCount(Long value)
{
count[0] = value;
}
@Override
public void setDistinctCount(Long value)
{
}
});
return ((int) count[0]);
}
/*******************************************************************************
** v1 table API: get.
*******************************************************************************/
private static QRecord v1Get(String tableName, Integer id) throws Exception
{
QRecord[] record = new QRecord[1];
TableGetInput input = new TableGetInput();
input.setTableName(tableName);
input.setPrimaryKey(String.valueOf(id));
new TableGetExecutor().execute(input, (TableGetOutputInterface) r -> record[0] = r);
return (record[0]);
}
/*******************************************************************************
** v1 table API: export (CSV), returning the data row count.
*******************************************************************************/
private static int v1ExportRows(String tableName) throws Exception
{
InputStream[] stream = new InputStream[1];
TableExportInput input = new TableExportInput();
input.setTableName(tableName);
input.setFormat(ReportFormat.CSV.name());
input.setFieldNames(List.of("id"));
new TableExportExecutor().execute(input, new TableExportOutputInterface()
{
@Override
public void setReportFormat(ReportFormat format)
{
}
@Override
public void setFilename(String filename)
{
}
@Override
public void setInputStream(InputStream inputStream)
{
stream[0] = inputStream;
}
});
String csv = new String(stream[0].readAllBytes(), StandardCharsets.UTF_8).trim();
return (csv.isEmpty() ? 0 : csv.split("\n").length - 1);
}
/*******************************************************************************
** v1 table API: update (throws on record errors).
*******************************************************************************/
private static QRecord v1Update(String tableName, Integer id, Map<String, Serializable> values) throws Exception
{
QRecord[] record = new QRecord[1];
TableUpdateInput input = new TableUpdateInput();
input.setTableName(tableName);
input.setPrimaryKey(String.valueOf(id));
input.setRecordValues(new LinkedHashMap<>(values));
new TableUpdateExecutor().execute(input, new TableUpdateOutputInterface()
{
@Override
public void setRecord(QRecord r)
{
record[0] = r;
}
@Override
public void setErrors(List<String> errors)
{
}
@Override
public void setWarnings(List<String> warnings)
{
}
});
return (record[0]);
}
/*******************************************************************************
** v1 table API: delete, returning the deleted count.
*******************************************************************************/
private static int v1Delete(String tableName, Integer id) throws Exception
{
int[] deleted = new int[1];
TableDeleteInput input = new TableDeleteInput();
input.setTableName(tableName);
input.setPrimaryKey(String.valueOf(id));
new TableDeleteExecutor().execute(input, new TableDeleteOutputInterface()
{
@Override
public void setDeletedRecordCount(Integer count)
{
deleted[0] = count == null ? 0 : count;
}
@Override
public void setErrors(List<String> errors)
{
}
});
return (deleted[0]);
}
/*******************************************************************************
** v1 table API: insert (throws on record errors).
*******************************************************************************/
private static QRecord tableInsert(String tableName, Map<String, Serializable> values) throws Exception
{
QRecord[] record = new QRecord[1];
TableInsertInput input = new TableInsertInput();
input.setTableName(tableName);
input.setRecordValues(new LinkedHashMap<>(values));
new TableInsertExecutor().execute(input, new TableInsertOutputInterface()
{
@Override
public void setRecord(QRecord r)
{
record[0] = r;
}
@Override
public void setErrors(List<String> errors)
{
}
@Override
public void setWarnings(List<String> warnings)
{
}
});
return (record[0]);
}
/*******************************************************************************
** The workbench table names.
*******************************************************************************/
private static final class AppTables
{
static final List<String> ALL = List.of(SqlWorkbenchNotebook.TABLE_NAME, SqlWorkbenchCell.TABLE_NAME, SqlWorkbenchNotebookVersion.TABLE_NAME,
SqlWorkbenchResultSnapshot.TABLE_NAME, SqlWorkbenchNotebookShare.TABLE_NAME, SqlWorkbenchExecution.TABLE_NAME);
}
}
@@ -107,7 +107,7 @@ class NotebookServiceTest extends BaseSqlWorkbenchTest
@SuppressWarnings("unchecked")
void testCreateGetUpdateVersions() throws Exception
{
Map<String, Object> created = service.create(body("People", List.of(cell("a", "MARKDOWN", "# Hi"), cell("b", "sql", "SELECT 1"))), caller(READER));
Map<String, Object> created = service.create(body("People", List.of(cell("a", "MARKDOWN", "# Hi"), cell("b", "sql", "SELECT 1"))), as(READER));
Integer id = (Integer) created.get("id");
assertThat(created.get("visibility")).isEqualTo("PERSONAL");
assertThat(created.get("currentVersion")).isEqualTo(1);
@@ -117,20 +117,20 @@ class NotebookServiceTest extends BaseSqlWorkbenchTest
Map<String, Object> update = body("People v2", List.of(cell("b", "SQL", "SELECT 2")));
update.put("comment", "trim");
Map<String, Object> updated = service.update(id, update, caller(READER));
Map<String, Object> updated = service.update(id, update, as(READER));
assertThat(updated.get("title")).isEqualTo("People v2");
assertThat(updated.get("currentVersion")).isEqualTo(2);
assertThat((List<?>) updated.get("cells")).hasSize(1);
assertThat(QueryAction.execute(SqlWorkbenchCell.TABLE_NAME, new QQueryFilter())).hasSize(1);
List<Map<String, Object>> versions = service.versions(id, caller(READER));
List<Map<String, Object>> versions = service.versions(id, as(READER));
assertThat(versions).extracting(v -> v.get("versionNumber")).containsExactly(2, 1);
assertThat(versions.get(0).get("comment")).isEqualTo("trim");
Map<String, Object> v1 = service.version(id, 1, caller(READER));
Map<String, Object> v1 = service.version(id, 1, as(READER));
assertThat(((Map<String, Object>) v1.get("notebook")).get("title")).isEqualTo("People");
Map<String, Object> restored = service.restore(id, 1, caller(READER));
Map<String, Object> restored = service.restore(id, 1, as(READER));
assertThat(restored.get("title")).isEqualTo("People");
assertThat(restored.get("currentVersion")).isEqualTo(3);
assertThat((List<?>) restored.get("cells")).hasSize(2);
@@ -138,11 +138,11 @@ class NotebookServiceTest extends BaseSqlWorkbenchTest
////////////////////////////////////
// retention keeps the newest 3 //
////////////////////////////////////
service.update(id, Map.of("title", "People v4"), caller(READER));
service.update(id, Map.of("title", "People v4"), as(READER));
assertThat(QueryAction.execute(SqlWorkbenchNotebookVersion.TABLE_NAME, new QQueryFilter())).hasSize(3);
assertThat(service.get(id, caller(READER)).get("cells")).asList().hasSize(2);
assertThat(service.get(id, as(READER)).get("cells")).asList().hasSize(2);
assertThatThrownBy(() -> service.version(id, 1, caller(READER))).isInstanceOf(QNotFoundException.class);
assertThatThrownBy(() -> service.version(id, 1, as(READER))).isInstanceOf(QNotFoundException.class);
}
@@ -153,31 +153,31 @@ class NotebookServiceTest extends BaseSqlWorkbenchTest
@Test
void testVisibilityAndSharing() throws Exception
{
Integer mine = (Integer) service.create(body("Mine", List.of()), caller(WRITER)).get("id");
Integer mine = (Integer) service.create(body("Mine", List.of()), as(WRITER)).get("id");
assertThat(service.list("mine", caller(WRITER))).hasSize(1);
assertThat(service.list("mine", caller(READER))).isEmpty();
assertThat(service.list("shared", caller(READER))).isEmpty();
assertThatThrownBy(() -> service.get(mine, caller(READER))).isInstanceOf(QNotFoundException.class);
assertThat(service.list("mine", as(WRITER))).hasSize(1);
assertThat(service.list("mine", as(READER))).isEmpty();
assertThat(service.list("shared", as(READER))).isEmpty();
assertThatThrownBy(() -> service.get(mine, as(READER))).isInstanceOf(QNotFoundException.class);
assertThatThrownBy(() -> service.publish(mine, true, caller(READER))).isInstanceOf(QPermissionDeniedException.class);
assertThat(service.publish(mine, true, caller(WRITER)).get("visibility")).isEqualTo("SHARED");
assertThatThrownBy(() -> service.publish(mine, true, as(READER))).isInstanceOf(QPermissionDeniedException.class);
assertThat(service.publish(mine, true, as(WRITER)).get("visibility")).isEqualTo("SHARED");
assertThat(service.list("shared", caller(READER))).hasSize(1);
assertThat(service.get(mine, caller(READER)).get("canEdit")).isEqualTo(false);
assertThatThrownBy(() -> service.update(mine, Map.of("title", "x"), caller(READER))).isInstanceOf(QPermissionDeniedException.class);
assertThatThrownBy(() -> service.delete(mine, caller(READER))).isInstanceOf(QPermissionDeniedException.class);
assertThat(service.list("shared", as(READER))).hasSize(1);
assertThat(service.get(mine, as(READER)).get("canEdit")).isEqualTo(false);
assertThatThrownBy(() -> service.update(mine, Map.of("title", "x"), as(READER))).isInstanceOf(QPermissionDeniedException.class);
assertThatThrownBy(() -> service.delete(mine, as(READER))).isInstanceOf(QPermissionDeniedException.class);
Map<String, Object> copy = service.duplicate(mine, caller(READER));
Map<String, Object> copy = service.duplicate(mine, as(READER));
assertThat(copy.get("title")).isEqualTo("Copy of Mine");
assertThat(copy.get("ownerUserId")).isEqualTo(READER);
assertThat(copy.get("visibility")).isEqualTo("PERSONAL");
assertThat(service.publish(mine, false, caller(WRITER)).get("visibility")).isEqualTo("PERSONAL");
assertThat(service.list("shared", caller(READER))).isEmpty();
assertThat(service.publish(mine, false, as(WRITER)).get("visibility")).isEqualTo("PERSONAL");
assertThat(service.list("shared", as(READER))).isEmpty();
service.delete(mine, caller(WRITER));
assertThatThrownBy(() -> service.get(mine, caller(WRITER))).isInstanceOf(QNotFoundException.class);
service.delete(mine, as(WRITER));
assertThatThrownBy(() -> service.get(mine, as(WRITER))).isInstanceOf(QNotFoundException.class);
}
@@ -188,22 +188,22 @@ class NotebookServiceTest extends BaseSqlWorkbenchTest
@Test
void testValidation() throws Exception
{
assertThatThrownBy(() -> service.create(body(" ", List.of()), caller(READER))).isInstanceOf(QBadRequestException.class);
assertThatThrownBy(() -> service.create(body("x", List.of(cell("a", "PYTHON", "x"))), caller(READER))).isInstanceOf(QBadRequestException.class);
assertThatThrownBy(() -> service.create(body(" ", List.of()), as(READER))).isInstanceOf(QBadRequestException.class);
assertThatThrownBy(() -> service.create(body("x", List.of(cell("a", "PYTHON", "x"))), as(READER))).isInstanceOf(QBadRequestException.class);
Map<String, Object> notAList = body("x", null);
notAList.put("cells", "nope");
assertThatThrownBy(() -> service.create(notAList, caller(READER))).isInstanceOf(QBadRequestException.class);
assertThatThrownBy(() -> service.create(notAList, as(READER))).isInstanceOf(QBadRequestException.class);
Map<String, Object> notObjects = body("x", null);
notObjects.put("cells", List.of("nope"));
assertThatThrownBy(() -> service.create(notObjects, caller(READER))).isInstanceOf(QBadRequestException.class);
assertThatThrownBy(() -> service.create(notObjects, as(READER))).isInstanceOf(QBadRequestException.class);
List<Map<String, Object>> tooMany = new ArrayList<>();
for(int i = 0; i <= NotebookService.MAX_CELLS; i++)
{
tooMany.add(cell("k" + i, "SQL", "SELECT 1"));
}
assertThatThrownBy(() -> service.create(body("x", tooMany), caller(READER))).isInstanceOf(QBadRequestException.class);
assertThatThrownBy(() -> service.get(null, caller(READER))).isInstanceOf(QBadRequestException.class);
assertThatThrownBy(() -> service.create(body("x", tooMany), as(READER))).isInstanceOf(QBadRequestException.class);
assertThatThrownBy(() -> service.get(null, as(READER))).isInstanceOf(QBadRequestException.class);
///////////////////////////////////////////////////////
// duplicate or missing keys are replaced by UUIDs //
@@ -226,26 +226,26 @@ class NotebookServiceTest extends BaseSqlWorkbenchTest
@SuppressWarnings("unchecked")
void testSnapshots() throws Exception
{
Integer id = (Integer) service.create(body("Snap", List.of(cell("c1", "SQL", "SELECT * FROM person"))), caller(READER)).get("id");
Integer id = (Integer) service.create(body("Snap", List.of(cell("c1", "SQL", "SELECT * FROM person"))), as(READER)).get("id");
SqlExecutionEngine engine = new SqlExecutionEngine(qInstance, SqlWorkbenchRuntime.get(qInstance));
Map<String, Object> response = engine.execute(ExecutionRequest.readOnly(BACKEND_NAME, "SELECT * FROM person", Map.of()), caller(READER));
Map<String, Object> response = engine.execute(ExecutionRequest.readOnly(BACKEND_NAME, "SELECT * FROM person", Map.of()), as(READER));
String uuid = (String) response.get("executionUuid");
assertThatThrownBy(() -> service.createSnapshot(id, Map.of("executionUuid", "missing"), caller(READER), null)).isInstanceOf(QNotFoundException.class);
assertThatThrownBy(() -> service.createSnapshot(id, Map.of("executionUuid", "missing"), as(READER), null)).isInstanceOf(QNotFoundException.class);
Map<String, Object> snapshot = service.createSnapshot(id, Map.of("executionUuid", uuid, "cellKey", "c1", "label", "people"), caller(READER), "SELECT * FROM person");
Map<String, Object> snapshot = service.createSnapshot(id, Map.of("executionUuid", uuid, "cellKey", "c1", "label", "people"), as(READER), "SELECT * FROM person");
assertThat(snapshot.get("rowCount")).isEqualTo(2);
assertThat(snapshot.get("truncated")).isEqualTo(true);
assertThat(((List<?>) ((Map<String, Object>) snapshot.get("result")).get("rows"))).hasSize(2);
List<Map<String, Object>> snapshots = service.snapshots(id, caller(READER));
List<Map<String, Object>> snapshots = service.snapshots(id, as(READER));
assertThat(snapshots).hasSize(1);
Integer snapshotId = (Integer) snapshot.get("id");
assertThatThrownBy(() -> service.deleteSnapshot(id, 999, caller(READER))).isInstanceOf(QNotFoundException.class);
service.deleteSnapshot(id, snapshotId, caller(READER));
assertThat(service.snapshots(id, caller(READER))).isEmpty();
assertThatThrownBy(() -> service.deleteSnapshot(id, 999, as(READER))).isInstanceOf(QNotFoundException.class);
service.deleteSnapshot(id, snapshotId, as(READER));
assertThat(service.snapshots(id, as(READER))).isEmpty();
}
}
+16
View File
@@ -1,4 +1,5 @@
-- H2 (MODE=MySQL) version of docs/ddl/postgres.sql, for unit tests.
DROP TABLE IF EXISTS sql_workbench_notebook_share;
DROP TABLE IF EXISTS sql_workbench_result_snapshot;
DROP TABLE IF EXISTS sql_workbench_notebook_version;
DROP TABLE IF EXISTS sql_workbench_cell;
@@ -70,6 +71,21 @@ CREATE TABLE IF NOT EXISTS sql_workbench_result_snapshot (
CONSTRAINT fk_sql_workbench_result_snapshot_notebook FOREIGN KEY (notebook_id) REFERENCES sql_workbench_notebook (id)
);
CREATE TABLE IF NOT EXISTS sql_workbench_notebook_share (
id INTEGER AUTO_INCREMENT NOT NULL,
notebook_id INTEGER NOT NULL,
audience_type VARCHAR(16) NOT NULL,
audience_id VARCHAR(255),
audience_key VARCHAR(300) NOT NULL,
created_by_user_id VARCHAR(255),
created_by_name VARCHAR(255),
create_date TIMESTAMP,
modify_date TIMESTAMP,
CONSTRAINT pk_sql_workbench_notebook_share PRIMARY KEY (id),
CONSTRAINT uq_sql_workbench_notebook_share UNIQUE (notebook_id, audience_key),
CONSTRAINT fk_sql_workbench_notebook_share_notebook FOREIGN KEY (notebook_id) REFERENCES sql_workbench_notebook (id)
);
-- Audit: append-only. Not foreign-keyed to notebooks, so deleting a
-- notebook never deletes its audit trail.
CREATE TABLE IF NOT EXISTS sql_workbench_execution (