Skip to content

feat(db): DB ビューワで入力した SQL を実行できるようにし、VIEW の表示とレコードの表記を直す - #648

Merged
kan merged 6 commits into
mainfrom
feature/646-db-sql
Nov 23, 2026
Merged

kan merged 6 commits into
mainfrom
feature/646-db-sql

Conversation

@kan

@kan kan commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Closes #646

変更点

  • SQL の実行:DB タブに「SQL」の表示を足した(db_execute)。「実行」か Ctrl+Enter で実行し、範囲を選んでいればそこだけを実行する
    • 結果を返す文が 1 つだけなら、レコードと同じ表で見せる(MySQL は --batch、PostgreSQL は COPY ... CSV で包む)
    • DDL、書き込み、複数の文は、クライアントの人向けの出力をそのまま見せる(MySQL は --table -vv、PostgreSQL は -q を外す)。実行のあとはテーブルの一覧を読み直す
    • PostgreSQL のこの形は SQL を標準入力で渡す。--command に複数の文を渡すと 1 つの暗黙のトランザクションになり、途中の失敗で前の文まで取り消されるため(実測)。コンテナの経路は、exec の環境変数で渡してコンテナの sh で流し込む(exec の標準入力を閉じると接続ごと切れるため)
    • PostgreSQL で COPY が拒む文(RETURNING の無い WITH ... UPDATE、SELECT ... INTO)は、SQLSTATE と出どころの関数名で見分けてログの形で流し直す(本文はサーバーの lc_messages で翻訳されるので照合しない)
  • VIEW:テーブルの一覧で、ビューは件数の代わりに VIEW と出す
  • 文言:表示の切り替えの「中身」を「レコード」にした

Redis の対応は #647 に分けた。

確認

  • just check
  • 実測:PostgreSQL 18(pgvector のコンテナ)と MariaDB 11.8 のコンテナで、出力の形、トランザクションの挙動、COPY の拒否の判定を確かめた
  • GUI での動作確認済み
  • WSL やローカルのシェルから psql を使う経路は確かめていない(手元の WSL に psql が無い)

🤖 Generated with Claude Code

https://claude.ai/code/session_01PZw9ixppashkuJvbtNqB54

- DB タブに「SQL」の表示を足す。結果を返す文が 1 つなら表で、DDL・書き込み・複数の文は
  クライアントの出力で見せる(db_execute)
- PostgreSQL のログの形は SQL を標準入力で渡し、文ごとに確定させる
- テーブルの一覧で VIEW は件数の代わりに VIEW と出す
- 表示の切り替えの「中身」を「レコード」にする

Closes #646

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PZw9ixppashkuJvbtNqB54
@kan kan added the enhancement New feature or request label Nov 23, 2026
Copilot AI balanced review requested due to automatic review settings November 23, 2026 02:46
@kan kan added the enhancement New feature or request label Nov 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical issues allow whitespace-only selections to execute unselected SQL and psql metacommands to run OS commands outside the database role’s permissions.

6 open issues
What changed in this PR

Adds SQL execution to Pike’s DB viewer, alongside clearer record labels and explicit VIEW badges.

Changes:

  • Runs selected SQL or the full input, displaying query results as tables and other output as logs.
  • Adds backend execution routing, output limits, error handling, and table-list refreshes.
  • Updates translations, shortcut guidance, documentation, and tests.
File Description
tests/​dbViewer.test.ts Tests table row counts and VIEW badges.
src/​types/​db.ts Defines SQL execution result types.
src/​lib/​tauri.ts Adds the dbExecute frontend bridge.
src/​lib/​dbViewer.ts Adds table badge formatting.
src/​i18n/​ja.ts Adds Japanese SQL controls and record labels.
src/​i18n/​en.ts Adds English SQL controls and record labels.
src/​components/​tabs/​DbTab.vue Adds SQL input, execution, results, and logs.
src/​components/​KeyboardShortcuts.vue Lists the SQL execution shortcut.
src-tauri/​src/​lib.rs Registers the SQL execution command.
src-tauri/​src/​db/​sql.rs Classifies SQL and handles client output modes.
src-tauri/​src/​db/​mod.rs Executes SQL and collects bounded results and logs.
docs/​manual/​shortcuts-and-cli.md Documents the SQL execution shortcut.
docs/​manual/​panels.md Explains SQL execution, limits, and VIEW badges.
CHANGELOG.md Records the DB viewer enhancements.
.claude/​structure.md Updates DB module descriptions.
.claude/​rules/​db.md Documents SQL execution design and constraints.

Review effort: Balanced

Comment thread src-tauri/src/db/mod.rs Resolved
Comment thread src/components/tabs/DbTab.vue Resolved
Comment thread src-tauri/src/db/mod.rs Resolved
Comment thread src-tauri/src/db/mod.rs Resolved
Comment thread src-tauri/src/db/sql.rs Resolved
Comment thread src/components/tabs/DbTab.vue Resolved
kan and others added 2 commits November 23, 2026 11:57
Security Check の cargo audit が、url 2.5.8 の脆弱性(入れ子の blob: の URL で
Url::origin() がスタックオーバーフローする)で落ちていた。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PZw9ixppashkuJvbtNqB54
- クライアントのコマンド(\! や MySQL の system など)を含む SQL は実行しない。
  psql は字句で見つけて断り、MySQL は標準入力から読むときに --binary-mode で止める
- 入力した SQL はどの経路でも標準入力で渡す(引数の長さの上限を避ける)。
  コンテナの経路は 64 KiB ずつの環境変数に分け、1 MiB を超えたら断る
- 空白だけを範囲選択したときに全体を実行しない
- ログの上限を超える行は入るぶんだけ残す
- MySQL の WITH ... UPDATE と SELECT ... INTO はログの形で実行する
- 一覧の読み直しが次の実行の結果を上書きしないようにする
- コンテナに sh が無いときに「クライアントが無い」と言わない

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PZw9ixppashkuJvbtNqB54
@kan
kan requested a balanced review from Copilot November 23, 2026 05:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src-tauri/src/db/sql.rs Resolved
Comment thread src-tauri/src/db/sql.rs Resolved
Comment thread src-tauri/src/db/sql.rs Resolved
Copilot の再レビューで、字句の検出をすり抜ける例が 3 つ出た(COPY ... FROM STDIN の
データ、standard_conforming_strings = off、foo$E'...')。どれも実際に OS のコマンドが動いた。

- psql はスクリプトの先頭で乱数の鍵の \restrict をし、psql の側で止める。
  \restrict の無い古い psql では実行せず、更新を促す
- 字句の検出は理由を見せる補助として残し、$E の境界を直す
- MySQL の名前の付いたコマンドは文の頭だけを見る(列名の source を断らない)
- 古い psql の判定は psql:<stdin>:1: の行だけで行う
- 成功時の NOTICE / WARNING をログに添える
- 表の形のエラーから psql:<stdin>:N: を外し、ログの形は行番号を入力に合わせる
- LOCATION の行は .c 以外(scan.l など)も落とす

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PZw9ixppashkuJvbtNqB54
@kan
kan requested a balanced review from Copilot November 23, 2026 06:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Unresolved critical flaws can turn invalid SQL into executable statements, expose client-side files through LOCAL INFILE, and cause unbounded memory growth while buffering logs.

3 open issues
3 previously missed issues

In code that hasn't changed since last review

High severity Disable LOCAL INFILE when executing user SQL

src-tauri/​src/​db/​sql.rs:127

--binary-mode は LOAD DATA LOCAL INFILE によるクライアント側のファイル読み込みを止めません。LOCAL が有効な MySQL/MariaDB では、例えば LOAD DATA LOCAL INFILE '/path/to/file' INTO TABLE t がこの検査を通り、DB の FILE 権限なしでホスト・WSL・コンテナ上のファイルを読み込んでサーバーへ送信できます。DB ユーザーの権限の外でファイルに触れないよう、入力した SQL の実行では --local-infile=0 も明示してください。

High severity Reject unterminated block comments instead of stripping them

src-tauri/​src/​db/​sql.rs:554

閉じていないブロックコメントもここで読み飛ばすため、PostgreSQL の SELECT nextval('seq') /* unfinished は末尾のコメントが削られ、COPY (SELECT nextval('seq') ...) として実行されます。本来は構文エラーになる入力なのに、関数の実行やシーケンスの更新が起きてしまいます。block_comment_end でコメントが閉じたかも返し、未完のコメント(入れ子も含む)は削らずに残すか、実行前にエラーにしてください。

High severity Bound log-mode buffering before accumulating full output lines

src-tauri/​src/​db/​sql.rs:767

ログの上限は LogSink::record に届いてからしか効かず、その前に RecordReader::push が改行までの全バイトを pending に保持します。例えば COPY (SELECT repeat('x', 268435456)) TO STDOUT はログ経路になり、表示は 1 MiB でも Rust 側では 256 MiB の行を保持し、ここでさらに全行を複製します。ログモードでは pending に蓄積する前にバイト数を制限し、上限後もプロセスの出力自体は読み捨てて排出するようにしてください。

1 resolved since last review

Review effort: Balanced

- MySQL の入力した SQL に --local-infile=0 も付け、LOAD DATA LOCAL INFILE で
  クライアントの環境のファイルを読ませない
- 閉じていないブロックコメントは削らずに残す(構文エラーになるはずの文を実行しない)
- ログの形は改行の来ていない 1 行も 1 MiB までしか溜めない(文字の途中では切らない)
- stderr は先頭と末尾を残す(NOTICE が上限を埋めても最後の ERROR を消さない)
- 空の SQL のエラーを文言にする

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PZw9ixppashkuJvbtNqB54
@kan
kan requested a balanced review from Copilot November 23, 2026 07:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Long UTF-8 log lines can lose bytes without setting truncated, preventing the UI from warning users that output was omitted.

1 open issue
3 resolved since last review

Review effort: Balanced

Comment thread src-tauri/src/db/sql.rs
Comment on lines +775 to +779
if bytes.get(take).is_some_and(|&b| b & 0xC0 == 0x80) {
while self.pending.last().is_some_and(|&b| b & 0xC0 == 0x80) {
self.pending.pop();
}
self.pending.pop();

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

採りました(c6949f4e)。1 行を溜める上限を LOG_MAX_BYTES と同じにしていたため、文字の境界まで戻すと残る行が上限より短くなり、LogSink が切ったことに気付いていませんでした。

溜める上限を LOG_MAX_BYTES + 4(LOG_LINE_CAP)にしました。境界まで戻すのは最大 3 バイトなので、切った行は必ず LogSink の上限を超え、そこで切られて truncated が立ちます。ご指摘の形(repeat('あ', 349526) の 1 行が 64 KiB ずつ届き、改行が別の塊で来る)で truncated が立つことを確かめるテストも足しました。

1 行を溜める上限を LOG_MAX_BYTES と同じにしていたので、文字の境界まで戻すと
残る行が上限より短くなり、LogSink が切ったことに気付かなかった。上限を 4 バイト
大きくし、切った行が必ず LogSink の上限を超えるようにする(Copilot のレビュー)。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PZw9ixppashkuJvbtNqB54
@kan
kan merged commit 2247245 into main Nov 23, 2026
10 checks passed
@kan
kan deleted the feature/646-db-sql branch November 23, 2026 08:44
@github-actions github-actions Bot mentioned this pull request Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DB ビューワ: 文言の変更・VIEW の表示・任意の SQL の実行

2 participants