From d5bf327adb359569ba2b8b44495497c34c0eb7fd Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 08:24:52 +0700 Subject: [PATCH 1/3] fix(windows): run the statement at the caret, keep the session through crashes and fix core dial, MySQL limit, MCP size and secret handling --- core/ARCHITECTURE.md | 6 +- .../drivers/mysql/src/column_origins.rs | 12 +- core/crates/drivers/mysql/src/editor_run.rs | 429 ++++++++++++++++-- .../drivers/mysql/src/editor_session.rs | 41 +- .../drivers/mysql/src/session_statements.rs | 24 + core/crates/drivers/mysql/src/stream_read.rs | 39 +- .../mysql/tests/integration/editor_session.rs | 109 ++++- .../mysql/tests/integration/row_limit.rs | 103 ++++- core/crates/ffi/src/connection_url.rs | 31 +- core/crates/ffi/src/core_handle.rs | 96 +++- core/crates/ffi/src/host_key.rs | 16 +- core/crates/ffi/src/lib.rs | 2 + core/crates/ffi/src/library.rs | 247 ++++++++-- core/crates/ffi/src/prompter.rs | 30 +- core/crates/ffi/src/run/run_state.rs | 128 +++++- core/crates/ffi/src/session.rs | 6 + core/crates/ffi/src/statement_at.rs | 131 ++++++ core/crates/ffi/tests/library.rs | 292 +++++++++++- core/crates/mcp/src/collected_results.rs | 343 +++++++++++++- core/crates/mcp/src/export_tools.rs | 50 +- core/crates/mcp/src/lib.rs | 1 + core/crates/mcp/src/query_tools.rs | 39 +- core/crates/mcp/src/schema_tools.rs | 46 +- core/crates/mcp/src/tool_context.rs | 8 +- core/crates/mcp/src/tool_server.rs | 15 +- core/crates/mcp/tests/mcp_server.rs | 139 +++++- core/crates/net/src/dial.rs | 190 +++++++- core/crates/net/src/direct_tcp.rs | 21 +- linux/docs/adding-drivers.md | 2 +- linux/docs/drivers/mysql.md | 5 +- windows/docs/decisions/0004-app-shell.md | 17 +- windows/src/TablePro.App.Model/AppSettings.cs | 2 + windows/src/TablePro.App.Model/RunResults.cs | 69 +++ .../src/TablePro.App.Model/SessionWriter.cs | 75 +++ windows/src/TablePro.App/App.xaml.cs | 15 +- windows/src/TablePro.App/MainWindow.xaml | 5 + windows/src/TablePro.App/MainWindow.xaml.cs | 306 +++++++++++-- windows/src/TablePro.App/Services/CrashLog.cs | 2 +- .../src/TablePro.App/Services/SessionEnd.cs | 80 ++++ .../src/TablePro.App/Services/Workbench.cs | 25 +- .../TablePro.App/Strings/en-US/Resources.resw | 39 ++ .../TablePro.App/Views/QueryTabView.xaml.cs | 30 ++ .../src/TablePro.App/Views/ResultsPane.xaml | 17 + .../TablePro.App/Views/ResultsPane.xaml.cs | 264 ++++++++--- .../TablePro.App/Views/SettingsView.xaml.cs | 15 +- .../TablePro.App/Views/WorkspaceView.xaml.cs | 77 +++- .../DataGridView.Interaction.cs | 15 +- windows/src/TablePro.DataGrid/DataGridView.cs | 2 + .../src/TablePro.Editor/SqlEditor.Keyboard.cs | 27 +- .../src/TablePro.Editor/SqlEditor.Pointer.cs | 12 +- .../src/TablePro.Graphics/ClipboardText.cs | 66 +++ .../src/TablePro.Interop/DatabaseSession.cs | 10 + .../Generated/tablepro_ffi.cs | 100 ++++ .../RunResultsTests.cs | 70 +++ .../SessionWriterTests.cs | 74 +++ .../tests/TablePro.App.UITests/AppSession.cs | 9 + .../CloseAndSessionTests.cs | 119 +++++ .../TablePro.App.UITests/RunStatementTests.cs | 53 +++ .../InteractionTests.cs | 86 ++++ .../StatementAtTests.cs | 67 +++ 60 files changed, 3894 insertions(+), 455 deletions(-) create mode 100644 core/crates/ffi/src/statement_at.rs create mode 100644 windows/src/TablePro.App.Model/RunResults.cs create mode 100644 windows/src/TablePro.App.Model/SessionWriter.cs create mode 100644 windows/src/TablePro.App/Services/SessionEnd.cs create mode 100644 windows/src/TablePro.Graphics/ClipboardText.cs create mode 100644 windows/tests/TablePro.App.Model.Tests/RunResultsTests.cs create mode 100644 windows/tests/TablePro.App.Model.Tests/SessionWriterTests.cs create mode 100644 windows/tests/TablePro.App.UITests/CloseAndSessionTests.cs create mode 100644 windows/tests/TablePro.App.UITests/RunStatementTests.cs create mode 100644 windows/tests/TablePro.Interop.Tests/StatementAtTests.cs diff --git a/core/ARCHITECTURE.md b/core/ARCHITECTURE.md index c5d67fbddb..2998edc0e1 100644 --- a/core/ARCHITECTURE.md +++ b/core/ARCHITECTURE.md @@ -111,7 +111,7 @@ Each boundary error has a stable `code()`, such as `driver.timeout` or `ssh.host `ffi` is the library an app outside Rust loads: the Windows app as a DLL, the Apple apps as a static library in an XCFramework, built without the `engines` feature by `apple/scripts/build-core.sh`. Core ADRs 0002, on the bindings, 0003, on result windows, and 0009, on the macOS slices, hold the reasons. -- One UniFFI API. `Core` lists the engines with the family each is listed under, opens a file, connects a saved connection or tests one from a form, and parses a connection URL with `tablepro_core::connection_url`, refusing one that needs a setting its record has no field for. Its `ConnectionLibrary` reads and changes the saved connections, groups, favourites and tags in `connections.json` and stores their secrets, and gives the Apple apps the library's CloudKit records and merges a pull into it (`tablepro_storage::cloudkit`). A `DatabaseSession` reads the catalog one level at a time, writes a table's sorted page at an offset and counts the table, writes a query re-run with a sort (`docs/decisions/0007-sorting-a-result.md`), says what Safe Mode lets an editor run do at the session's level and takes a level saved while it is open (`gate_run`, over `SafeModeLevel::gate`, and `set_safe_mode`), reports and waits on the session's health, and opens a `QueryEditor`. `QueryEditor::run` returns a `QueryRun` at once; the app drains its events after a `RunListener` call, which comes only when no earlier one is waiting to be drained, and the last says which transaction the editor is left in. Each result set is a `ResultSet`, whose `sorted` gives the rows it holds in a column's order. +- One UniFFI API. `Core` lists the engines with the family each is listed under, opens a file, connects a saved connection or tests one from a form, and parses a connection URL with `tablepro_core::connection_url`, refusing one that needs a setting its record has no field for. Its `ConnectionLibrary` reads and changes the saved connections, groups, favourites and tags in `connections.json` and stores their secrets, and gives the Apple apps the library's CloudKit records and merges a pull into it (`tablepro_storage::cloudkit`). A `DatabaseSession` reads the catalog one level at a time, writes a table's sorted page at an offset and counts the table, writes a query re-run with a sort (`docs/decisions/0007-sorting-a-result.md`), picks the statement at a caret with the session's grammar (`statement_at`), says what Safe Mode lets an editor run do at the session's level and takes a level saved while it is open (`gate_run`, over `SafeModeLevel::gate`, and `set_safe_mode`), reports and waits on the session's health, and opens a `QueryEditor`. `QueryEditor::run` returns a `QueryRun` at once; the app drains its events after a `RunListener` call, which comes only when no earlier one is waiting to be drained, and the last says which transaction the editor is left in. Each result set is a `ResultSet`, whose `sorted` gives the rows it holds in a column's order. - The grid reads a `ResultSet` through the C ABI in `src/window_abi.rs`: `tablepro_cursor_fetch` packs the display text of one visible range, and `tablepro_cursor_free` ends the cursor. - The library owns one tokio runtime. Each async export runs on it and is aborted when the foreign side drops the call; a `CancelToken` stops it sooner, since UniFFI cannot cancel. - A result set takes rows as the driver reads them. Each column keeps a kind byte, a raw `i64` and the exact text `tablepro_core::export::value_text` writes, or a binary value's bytes, in chunks of 65,536 rows. A window only cuts and copies. @@ -178,7 +178,7 @@ The cancel token is checked between stages, and a stage that finishes after a ca | | `DirectTcpTransport` (`tablepro-net`) | `SshForwardTransport` (`tablepro-ssh`) | `InProcessForward` (`tablepro-ssh`, `in-process`) | |---|---|---|---| -| `open` | Resolves the name, then tries each address in turn under the connect timeout | Connects to the forward's Unix socket under the connect timeout | Opens a `direct-tcpip` channel under the channel open timeout | +| `open` | Resolves the name and races its addresses as Happy Eyeballs does (RFC 8305): families alternate, each attempt starts 250 ms after the last or as soon as one fails, and the lookup and every attempt share one connect timeout | Connects to the forward's Unix socket under the connect timeout | Opens a `direct-tcpip` channel under the channel open timeout | | Socket options | `TCP_NODELAY`; keepalive after 30 s idle, every 10 s, 3 probes; `TCP_USER_TIMEOUT` 45 s | OpenSSH's own: `ServerAliveInterval=15`, `ServerAliveCountMax=3` | The first hop's dial is a `DirectTcpTransport`'s; SSH keepalives every 15 s, 3 unanswered end the session | | `reroute` | A new direct transport | A new forward on the same master | A forward on the same session | | `explain_closed_stream` | Nothing to add | The master's exit, or the "open failed" line ssh printed | Why the session ended | @@ -228,7 +228,7 @@ Where each platform's system store comes from: `linux/ARCHITECTURE.md`, Trust. | Bound | Value | |---|---| -| TCP connect, per address | 10 s | +| TCP connect, the name lookup and every address together | 10 s | | Login, including TLS | 15 s | | TCP keepalive | after 30 s idle, every 10 s, 3 probes | | `TCP_USER_TIMEOUT` | 45 s | diff --git a/core/crates/drivers/mysql/src/column_origins.rs b/core/crates/drivers/mysql/src/column_origins.rs index 45faa8cc8c..7cb87ee8fc 100644 --- a/core/crates/drivers/mysql/src/column_origins.rs +++ b/core/crates/drivers/mysql/src/column_origins.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use sqlx::mysql::{MySqlConnection, MySqlRow}; +use sqlx::mysql::{MySqlColumn, MySqlConnection}; use sqlx::{AssertSqlSafe, Column as _, Executor as _, Row as _}; use tablepro_core::ResultEvent; use tablepro_core::column::{ColumnOrigin, ResultColumn}; @@ -22,9 +22,13 @@ pub(crate) struct TracedSet { } impl TracedSet { - pub(crate) fn new(set_index: usize, columns: Arc<[ResultColumn]>, row: &MySqlRow, statement: SourceTable) -> Self { - let wire = row - .columns() + pub(crate) fn new( + set_index: usize, + columns: Arc<[ResultColumn]>, + wire_columns: &[MySqlColumn], + statement: SourceTable, + ) -> Self { + let wire = wire_columns .iter() .map(|column| { column diff --git a/core/crates/drivers/mysql/src/editor_run.rs b/core/crates/drivers/mysql/src/editor_run.rs index 5dd874a4c7..1f166cedc5 100644 --- a/core/crates/drivers/mysql/src/editor_run.rs +++ b/core/crates/drivers/mysql/src/editor_run.rs @@ -1,19 +1,13 @@ -//! One editor run over the text protocol, as result events. -//! -//! A set starts with its first row, since that is where sqlx gives out -//! the columns, and ends with the OK packet behind it. The server stops -//! a plain SELECT at `sql_select_limit`, one row past the run's limit; -//! a SELECT with a LIMIT of its own, or one inside a routine, is read to -//! its end, and the rows past the limit are counted, never decoded. - +use std::ops::Range; use std::sync::Arc; -use futures_util::TryStreamExt; -use sqlx::mysql::{MySqlConnection, MySqlRow}; -use sqlx::{AssertSqlSafe, Either, Executor as _, Row as _}; +use futures_util::{Stream, TryStreamExt}; +use sqlx::mysql::{MySqlColumn, MySqlConnection, MySqlQueryResult, MySqlRow}; +use sqlx::{AssertSqlSafe, Either, Executor as _, Row as _, SqlSafeStr as _, Statement as _}; use tablepro_core::column::ResultColumn; -use tablepro_core::sql_syntax::{SourceTable, SqlGrammar, source_table}; -use tablepro_core::{DriverError, ResultEvent, ResultSink, RowLimit, SinkFlow, Truncation, Value}; +use tablepro_core::sql_syntax::script::{LexicalSettings, ScriptPlan, ScriptTokenKind}; +use tablepro_core::sql_syntax::{SourceTable, SqlGrammar, StatementAccess, source_table, statement_access}; +use tablepro_core::{DriverError, ErrorCategory, ResultEvent, ResultSink, RowLimit, SinkFlow, Truncation, Value}; use tokio_util::sync::CancellationToken; use crate::catalog::result_column; @@ -24,10 +18,50 @@ use crate::flavour::MySqlFlavour; use crate::mysql_timestamp_mode::MySqlTimestampMode; use crate::mysql_wire_format::MySqlWireFormat; use crate::script_split; +use crate::session_statements::SelectLimit; -/// How many rows travel in one `Rows` event. const ROWS_PER_EVENT: usize = 256; +const SELECT_OPENERS: [&str; 4] = ["SELECT", "WITH", "TABLE", "VALUES"]; + +// Ordered, so a text the server runs whole takes its strictest statement's. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +enum TextKind { + Other, + // No INTO, lock or state change: the limit stops it, and its OK alone is an empty set. + PlainRead, + // A SELECT the limit would cut beyond what it shows, or an EXECUTE that may run one. + CutSelect, +} + +pub(crate) struct EditorLink { + pub connection: MySqlConnection, + // None once a SET failed, since a KILL that stopped it may have come after it took. + pub select_limit: Option, +} + +impl EditorLink { + pub(crate) fn new(connection: MySqlConnection) -> Self { + Self { + connection, + select_limit: Some(SelectLimit::Server), + } + } + + async fn limit_selects(&mut self, flavour: MySqlFlavour, wanted: SelectLimit) -> Result<(), DriverError> { + if self.select_limit == Some(wanted) { + return Ok(()); + } + self.select_limit = None; + self.connection + .execute(AssertSqlSafe(wanted.statement())) + .await + .map_err(|error| map_error(&error, flavour))?; + self.select_limit = Some(wanted); + Ok(()) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Default)] pub(crate) struct RunOutcome { pub consumer_gone: bool, @@ -38,7 +72,7 @@ pub(crate) struct RunOutcome { // A sink that stops reading cancels `stopped`; the text already sent is still read to its end, unshown, since only that frees the connection. pub(crate) async fn run_script( - connection: &mut MySqlConnection, + link: &mut EditorLink, flavour: MySqlFlavour, sql: &str, limit: RowLimit, @@ -56,30 +90,44 @@ pub(crate) async fn run_script( sink, limit: limit.get(), open: None, + held_back: None, sets: 0, gone: false, trace, traced: None, }; + let kinds: Vec = texts.iter().map(|text| text_kind(text)).collect(); + // Once, before the first statement: a SET between two would reset ROW_COUNT() and the warnings. + if let Some(wanted) = wanted_select_limit(&kinds, limit) { + link.limit_selects(flavour, wanted).await?; + } + let stopping = || stopped.is_cancelled() || halt(); let mut halted = false; - for text in texts { + for (text, kind) in texts.into_iter().zip(kinds) { if emitter.gone { break; } - if stopped.is_cancelled() || halt() { + if stopping() { halted = true; break; } - let results = connection.fetch_many(sqlx::raw_sql(AssertSqlSafe(text.to_owned()))); - let mut results = std::pin::pin!(results); - while let Some(item) = results.try_next().await.map_err(|error| map_error(&error, flavour))? { - match item { - Either::Right(row) => emitter.row(&row).await, - Either::Left(done) => emitter.done(done.rows_affected()).await, - } - if emitter.gone { - stopped.cancel(); - } + let results = link + .connection + .fetch_many(sqlx::raw_sql(AssertSqlSafe(text.to_owned()))); + let read = emitter + .read(results, flavour, stopped, kind == TextKind::PlainRead) + .await; + if read.is_err() { + emitter.release().await; + } + read?; + // sqlx gives an empty set as its OK alone, and the prepare's columns tell it from a statement that sends no rows. + if let Some(rows_affected) = emitter.held_back.take() { + let columns = match stopping() { + true => Vec::new(), + false => described(&mut link.connection, flavour, text).await?, + }; + emitter.answered(rows_affected, &columns).await; } } Ok(RunOutcome { @@ -89,6 +137,85 @@ pub(crate) async fn run_script( }) } +// The server cuts every top-level SELECT without a LIMIT, INTO OUTFILE and locking ones too, so one of those lifts it for the run. +fn wanted_select_limit(kinds: &[TextKind], limit: RowLimit) -> Option { + if kinds.contains(&TextKind::CutSelect) { + return Some(SelectLimit::Server); + } + kinds + .contains(&TextKind::PlainRead) + .then_some(SelectLimit::Editor(limit.get())) +} + +// Read with backslash escapes on and off, since the session's sql_mode decides where its strings end. +fn text_kind(text: &str) -> TextKind { + [true, false] + .into_iter() + .map(|backslash_escapes| { + let plan = ScriptPlan::build(text, SqlGrammar::MySql, LexicalSettings { backslash_escapes }); + plan.statements() + .iter() + .map(|statement| statement_kind(&plan, text, statement.range.clone())) + .max() + .unwrap_or(TextKind::Other) + }) + .max() + .unwrap_or(TextKind::Other) +} + +fn statement_kind(plan: &ScriptPlan, text: &str, range: Range) -> TextKind { + let Some(word) = opening_word(plan, text, range.clone()) else { + return TextKind::Other; + }; + if !SELECT_OPENERS.iter().any(|opener| opener.eq_ignore_ascii_case(word)) { + return match word.eq_ignore_ascii_case("EXECUTE") { + true => TextKind::CutSelect, + false => TextKind::Other, + }; + } + // statement_access reads INTO, FOR UPDATE, FOR SHARE and LOCK IN SHARE MODE as writes. + match text + .get(range) + .is_some_and(|statement| statement_access(statement, SqlGrammar::MySql) == StatementAccess::Read) + { + true => TextKind::PlainRead, + false => TextKind::CutSelect, + } +} + +fn opening_word<'a>(plan: &ScriptPlan, text: &'a str, range: Range) -> Option<&'a str> { + let first = plan.tokens(range).find(|token| { + !token.kind.is_trivia() + && !matches!( + token.kind, + ScriptTokenKind::OpenParen | ScriptTokenKind::ExecutableMarker + ) + })?; + match first.kind { + ScriptTokenKind::Word => text.get(first.range), + _ => None, + } +} + +// The session caches no statement, so sqlx closes this one at once. +async fn described( + connection: &mut MySqlConnection, + flavour: MySqlFlavour, + text: &str, +) -> Result, DriverError> { + let prepared = (&mut *connection) + .prepare(AssertSqlSafe(text.to_owned()).into_sql_str()) + .await; + match prepared { + Ok(statement) => Ok(statement.columns().to_vec()), + Err(error) => match map_error(&error, flavour) { + lost if lost.category() == ErrorCategory::ConnectionLost => Err(lost), + // Not every statement can be prepared, and text the server reads as several cannot. + _ => Ok(Vec::new()), + }, + } +} + struct OpenSet { batch: Vec>, retained: u64, @@ -99,6 +226,8 @@ struct Emitter<'a> { sink: &'a mut dyn ResultSink, limit: u64, open: Option, + // The rows affected of a text's first answer, an OK with no set, kept until the text ends. + held_back: Option, sets: usize, gone: bool, trace: Option, @@ -106,29 +235,43 @@ struct Emitter<'a> { } impl Emitter<'_> { + async fn read( + &mut self, + results: S, + flavour: MySqlFlavour, + stopped: &CancellationToken, + plain_read: bool, + ) -> Result<(), DriverError> + where + S: Stream, sqlx::Error>>, + { + let mut results = std::pin::pin!(results); + let mut first = true; + while let Some(item) = results.try_next().await.map_err(|error| map_error(&error, flavour))? { + match item { + Either::Right(row) => self.row(&row).await, + Either::Left(done) => self.done(done.rows_affected(), first && plain_read).await, + } + first = false; + if self.gone { + stopped.cancel(); + } + } + Ok(()) + } + async fn row(&mut self, row: &MySqlRow) { if self.gone { return; } if self.open.is_none() { - let columns: Arc<[ResultColumn]> = row - .columns() - .iter() - .map(|column| result_column(column, MySqlTimestampMode::SessionLocal)) - .collect(); - // Taken at the first set, so a second one leaves none. - let set_index = self.sets; - self.traced = self - .trace - .take() - .map(|statement| TracedSet::new(set_index, columns.clone(), row, statement)); - self.sets += 1; + self.release().await; + self.open_set(row.columns()).await; self.open = Some(OpenSet { batch: Vec::new(), retained: 0, seen: 0, }); - self.send(ResultEvent::ResultSetStarted { columns }).await; } let limit = self.limit; let Some(set) = self.open.as_mut() else { @@ -147,11 +290,16 @@ impl Emitter<'_> { } } - async fn done(&mut self, rows_affected: u64) { + async fn done(&mut self, rows_affected: u64, may_be_an_empty_set: bool) { if self.gone { return; } let Some(set) = self.open.take() else { + if may_be_an_empty_set { + self.held_back = Some(rows_affected); + return; + } + self.release().await; self.send(ResultEvent::CommandCompleted { rows_affected: Some(rows_affected), }) @@ -173,6 +321,48 @@ impl Emitter<'_> { .await; } + async fn answered(&mut self, rows_affected: u64, columns: &[MySqlColumn]) { + if columns.is_empty() { + self.send(ResultEvent::CommandCompleted { + rows_affected: Some(rows_affected), + }) + .await; + return; + } + self.open_set(columns).await; + self.send(ResultEvent::ResultSetFinished { + retained: 0, + total_rows: Some(0), + truncation: Truncation::Complete, + }) + .await; + } + + // A held back OK that another answer of the same text followed was no empty set of its own. + async fn release(&mut self) { + if let Some(rows_affected) = self.held_back.take() { + self.send(ResultEvent::CommandCompleted { + rows_affected: Some(rows_affected), + }) + .await; + } + } + + async fn open_set(&mut self, wire_columns: &[MySqlColumn]) { + let columns: Arc<[ResultColumn]> = wire_columns + .iter() + .map(|column| result_column(column, MySqlTimestampMode::SessionLocal)) + .collect(); + // Taken at the first set, so a second one leaves none. + let set_index = self.sets; + self.traced = self + .trace + .take() + .map(|statement| TracedSet::new(set_index, columns.clone(), wire_columns, statement)); + self.sets += 1; + self.send(ResultEvent::ResultSetStarted { columns }).await; + } + async fn send(&mut self, event: ResultEvent) { if self.gone { return; @@ -181,10 +371,7 @@ impl Emitter<'_> { } } -/// Why a set ended where it did. Exactly one row past the limit is -/// where `sql_select_limit` stops the server, so how many more there -/// were is unknown; past that the set was read to its end and the total -/// is known. +// One row past the limit is where sql_select_limit stops the server, so the total is unknown there. pub(crate) fn truncation(seen: u64, limit: u64) -> (Truncation, Option) { match seen { seen if seen <= limit => (Truncation::Complete, Some(seen)), @@ -197,6 +384,57 @@ pub(crate) fn truncation(seen: u64, limit: u64) -> (Truncation, Option) { mod tests { use super::*; + fn limit() -> RowLimit { + RowLimit::new(100).expect("a limit") + } + + fn run_wants(script: &[&str]) -> Option { + let kinds: Vec = script.iter().map(|text| text_kind(text)).collect(); + wanted_select_limit(&kinds, limit()) + } + + #[derive(Default)] + struct Kept(Vec); + + #[async_trait::async_trait] + impl ResultSink for Kept { + async fn accept(&mut self, event: ResultEvent) -> SinkFlow { + self.0.push(event); + SinkFlow::Continue + } + } + + fn command(rows_affected: u64) -> ResultEvent { + ResultEvent::CommandCompleted { + rows_affected: Some(rows_affected), + } + } + + #[tokio::test] + async fn an_ok_held_back_for_its_columns_keeps_its_place() { + let mut kept = Kept::default(); + let mut emitter = Emitter { + sink: &mut kept, + limit: 100, + open: None, + held_back: None, + sets: 0, + gone: false, + trace: None, + traced: None, + }; + + emitter.done(0, true).await; + let held = emitter.held_back; + emitter.done(2, false).await; + emitter.done(5, true).await; + let last = emitter.held_back.take().unwrap(); + emitter.answered(last, &[]).await; + + assert_eq!(held, Some(0)); + assert_eq!(kept.0, [command(0), command(2), command(5)]); + } + #[test] fn a_set_cut_by_the_server_is_told_from_one_read_past_the_limit() { assert_eq!(truncation(40, 100), (Truncation::Complete, Some(40))); @@ -204,4 +442,105 @@ mod tests { assert_eq!(truncation(101, 100), (Truncation::ServerStopped, None)); assert_eq!(truncation(150_000, 100), (Truncation::ReadPastLimit, Some(150_000))); } + + #[test] + fn the_opening_word_is_past_comments_markers_and_parentheses() { + fn first_word(text: &str) -> Option<&str> { + let plan = ScriptPlan::build(text, SqlGrammar::MySql, LexicalSettings::default_for(SqlGrammar::MySql)); + opening_word(&plan, text, 0..text.len()) + } + assert_eq!(first_word("SELECT 1"), Some("SELECT")); + assert_eq!( + first_word(" -- note\n/* more */ (select 1) UNION (SELECT 2)"), + Some("select") + ); + assert_eq!(first_word("/*!50000 SHOW */ TABLES"), Some("SHOW")); + assert_eq!(first_word("# note\nWITH x AS (SELECT 1) SELECT * FROM x"), Some("WITH")); + assert_eq!(first_word("'text'"), None); + assert_eq!(first_word(""), None); + } + + // script_split sends these whole, for the backslash, the `/*!` comment and the string the session's sql_mode decides. + #[test] + fn a_text_the_server_runs_whole_takes_its_strictest_statement() { + for cut in [ + r"INSERT INTO log VALUES ('C:\\tmp'); SELECT * FROM big INTO OUTFILE '/tmp/big.csv'", + "/*!40101 SET NAMES utf8mb4 */; SELECT id FROM big FOR UPDATE", + r"INSERT INTO log VALUES ('C:\'); SELECT * FROM big INTO OUTFILE '/tmp/big.csv'", + ] { + assert_eq!(text_kind(cut), TextKind::CutSelect, "{cut}"); + } + assert_eq!( + text_kind(r"INSERT INTO log VALUES ('C:\\tmp'); SELECT id FROM big"), + TextKind::PlainRead + ); + assert_eq!( + text_kind(r"INSERT INTO log VALUES ('C:\\tmp'); DELETE FROM log"), + TextKind::Other + ); + } + + #[test] + fn a_select_is_a_plain_read_only_without_into_a_lock_or_a_write() { + for read in [ + "SELECT * FROM big", + "(SELECT id FROM big) UNION ALL (SELECT id FROM big)", + "WITH ids AS (SELECT id FROM big) SELECT id FROM ids", + "TABLE big", + "VALUES ROW(1), ROW(2)", + "SELECT share, mode FROM big", + ] { + assert_eq!(text_kind(read), TextKind::PlainRead, "{read}"); + } + for cut in [ + "SELECT * FROM big INTO OUTFILE '/tmp/big.csv'", + "SELECT * FROM big INTO DUMPFILE '/tmp/big.bin'", + "SELECT COUNT(*) FROM big INTO @rows", + "SELECT * FROM big FOR UPDATE", + "SELECT * FROM big FOR SHARE", + "SELECT * FROM big for share skip locked", + "SELECT * FROM big LOCK IN SHARE MODE", + "TABLE big INTO OUTFILE '/tmp/big.csv'", + "EXECUTE every_id", + ] { + assert_eq!(text_kind(cut), TextKind::CutSelect, "{cut}"); + } + for other in [ + "SHOW PROCESSLIST", + "DESC big", + "EXPLAIN SELECT * FROM big", + "INSERT INTO copied SELECT id FROM big", + "CREATE TABLE copied AS SELECT id FROM big", + "CALL every_id()", + "SET SESSION sql_select_limit = 5", + "UPDATE big SET id = id", + "BEGIN", + ] { + assert_eq!(text_kind(other), TextKind::Other, "{other}"); + } + } + + #[test] + fn a_run_sets_the_limit_once_and_only_when_a_select_needs_it() { + let editor = Some(SelectLimit::Editor(100)); + assert_eq!( + run_wants(&["UPDATE t SET a = 1 WHERE id < 3", "SELECT ROW_COUNT()"]), + editor + ); + assert_eq!( + run_wants(&["SET SESSION sql_select_limit = 5", "SELECT * FROM t"]), + editor + ); + assert_eq!(run_wants(&["INSERT INTO t VALUES ('long')", "SHOW WARNINGS"]), None); + assert_eq!(run_wants(&["CALL every_id()", "BEGIN", "COMMIT"]), None); + assert_eq!(run_wants(&[]), None); + assert_eq!( + run_wants(&["SELECT * FROM big", "SELECT * FROM big INTO OUTFILE '/tmp/big.csv'"]), + Some(SelectLimit::Server) + ); + assert_eq!( + run_wants(&[r"INSERT INTO log VALUES ('C:\\tmp'); SELECT * FROM big INTO OUTFILE '/tmp/big.csv'"]), + Some(SelectLimit::Server) + ); + } } diff --git a/core/crates/drivers/mysql/src/editor_session.rs b/core/crates/drivers/mysql/src/editor_session.rs index b278728e6f..e8b5cd17b0 100644 --- a/core/crates/drivers/mysql/src/editor_session.rs +++ b/core/crates/drivers/mysql/src/editor_session.rs @@ -8,7 +8,7 @@ use sqlx::{AssertSqlSafe, Connection as _, Executor as _}; use tablepro_core::sql_syntax::{SqlGrammar, may_change_database}; use tablepro_core::{ CallOptions, DriverError, EditorSession, EditorSessionId, EditorSessionOptions, EditorSessionState, LivenessPolicy, - LossPhase, ResultSink, RowLimit, RunEnd, RunOptions, RunSummary, TimeoutPhase, TransactionState, UserSql, Value, + LossPhase, ResultSink, RunEnd, RunOptions, RunSummary, TimeoutPhase, TransactionState, UserSql, Value, }; use tokio::sync::OwnedMutexGuard; use tokio::time::Instant; @@ -18,21 +18,15 @@ use crate::cancel::ControlLink; use crate::column_origins::{TracedSet, resolved_columns}; use crate::decode::decode_row; use crate::dialect::quote; -use crate::editor_run::{RunOutcome, run_script}; +use crate::editor_run::{EditorLink, RunOutcome, run_script}; use crate::errors::map_error; use crate::flavour::MySqlFlavour; use crate::mysql_timestamp_mode::MySqlTimestampMode; use crate::mysql_wire_format::MySqlWireFormat; use crate::physical::{MysqlEndpoint, SessionKind}; -use crate::session_statements::{SessionLimits, select_limit}; +use crate::session_statements::SessionLimits; use crate::transaction_probe::TransactionProbe; -/// The tab's own connection and the row limit last set on it. -struct Held { - connection: MySqlConnection, - select_limit: Option, -} - /// One editor tab's own connection to the server. /// /// The session runs on the server's default settings, as the mysql @@ -48,7 +42,7 @@ pub(crate) struct MysqlEditorSession { /// and settles the session, rather than a statement left running for /// nobody. struct Shared { - held: Arc>>, + held: Arc>>, connection_id: u64, flavour: MySqlFlavour, control: ControlLink, @@ -85,10 +79,7 @@ impl MysqlEditorSession { Ok(Arc::new(Self { id: EditorSessionId::new(), shared: Arc::new(Shared { - held: Arc::new(tokio::sync::Mutex::new(Some(Held { - connection: physical.connection, - select_limit: None, - }))), + held: Arc::new(tokio::sync::Mutex::new(Some(EditorLink::new(physical.connection)))), connection_id: physical.connection_id, flavour: physical.flavour, control: ControlLink::new(endpoint), @@ -117,7 +108,7 @@ impl Shared { // The connection `guard` holds is given up when it cannot take another run. async fn run_held( &self, - mut guard: OwnedMutexGuard>, + mut guard: OwnedMutexGuard>, sql: UserSql, options: RunOptions, mut sink: Box, @@ -196,7 +187,7 @@ impl Shared { // A stop is KILL QUERY on a connection of the session's own; the statement then has the cancel grace to end, and nothing after it is sent. async fn supervise( &self, - held: &mut Held, + held: &mut EditorLink, sql: &UserSql, options: &RunOptions, sink: &mut dyn ResultSink, @@ -208,11 +199,7 @@ impl Shared { // Read synchronously before each statement goes out, since the // select below sees a stop only once the run waits. let halt = || self.stop_due(options, deadline).is_some(); - let flavour = self.flavour; - let running = async { - set_select_limit(held, flavour, limit).await?; - run_script(&mut held.connection, flavour, sql.as_str(), limit, sink, &stopped, halt).await - }; + let running = run_script(held, self.flavour, sql.as_str(), limit, sink, &stopped, halt); let mut running = std::pin::pin!(running); let stop = tokio::select! { outcome = &mut running => { @@ -317,18 +304,6 @@ fn confirms_stop(error: &DriverError) -> bool { ) } -async fn set_select_limit(held: &mut Held, flavour: MySqlFlavour, limit: RowLimit) -> Result<(), DriverError> { - if held.select_limit == Some(limit.get()) { - return Ok(()); - } - held.connection - .execute(AssertSqlSafe(select_limit(limit.get()))) - .await - .map_err(|error| map_error(&error, flavour))?; - held.select_limit = Some(limit.get()); - Ok(()) -} - fn finished(outcome: Result) -> Result { match outcome { Ok(RunOutcome { diff --git a/core/crates/drivers/mysql/src/session_statements.rs b/core/crates/drivers/mysql/src/session_statements.rs index 856a0e50f0..cf359007fa 100644 --- a/core/crates/drivers/mysql/src/session_statements.rs +++ b/core/crates/drivers/mysql/src/session_statements.rs @@ -75,6 +75,22 @@ pub(crate) fn select_limit(rows: u64) -> String { format!("SET SESSION sql_select_limit = {}", rows.saturating_add(1)) } +// What an editor session's sql_select_limit holds. A new session has the server's. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum SelectLimit { + Server, + Editor(u64), +} + +impl SelectLimit { + pub(crate) fn statement(self) -> String { + match self { + Self::Server => "SET SESSION sql_select_limit = DEFAULT".to_owned(), + Self::Editor(rows) => select_limit(rows), + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -137,5 +153,13 @@ mod tests { select_limit(u64::MAX), "SET SESSION sql_select_limit = 18446744073709551615" ); + assert_eq!( + SelectLimit::Editor(100).statement(), + "SET SESSION sql_select_limit = 101" + ); + assert_eq!( + SelectLimit::Server.statement(), + "SET SESSION sql_select_limit = DEFAULT" + ); } } diff --git a/core/crates/drivers/mysql/src/stream_read.rs b/core/crates/drivers/mysql/src/stream_read.rs index d3989ba11a..946f66b94b 100644 --- a/core/crates/drivers/mysql/src/stream_read.rs +++ b/core/crates/drivers/mysql/src/stream_read.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use futures_util::TryStreamExt; use sqlx::mysql::{MySqlConnection, MySqlDatabaseError}; -use sqlx::{AssertSqlSafe, Either, Executor as _, Row as _, SqlSafeStr as _}; +use sqlx::{AssertSqlSafe, Either, Executor as _, Row as _, SqlSafeStr as _, Statement as _}; use tablepro_core::column::ResultColumn; use tablepro_core::sql_syntax::{SqlGrammar, StatementAccess, statement_access}; use tablepro_core::{ @@ -28,10 +28,13 @@ pub(crate) async fn stream( read: &ReadStatement, sink: &mut dyn ResultSink, ) -> Result { - if let ReadStatement::User(sql) = read { - refuse_writes(sql.as_str())?; - refuse_more_than_one(connection, flavour, sql.as_str()).await?; - } + let declared = match read { + ReadStatement::User(sql) => { + refuse_writes(sql.as_str())?; + prepare_as_one(connection, flavour, sql.as_str()).await? + } + ReadStatement::Generated(_) => Vec::new(), + }; connection .execute("START TRANSACTION READ ONLY") .await @@ -61,7 +64,14 @@ pub(crate) async fn stream( } ReadStatement::User(sql) => { let results = connection.fetch_many(sqlx::raw_sql(AssertSqlSafe(sql.as_str().to_owned()))); - emitter.results(results, flavour).await + match emitter.results(results, flavour).await { + // sqlx gives an empty set as its OK alone, so its columns come from the prepare. + Ok(()) if emitter.rows == 0 && !declared.is_empty() => { + emitter.start(declared.into()).await?; + emitter.finish().await + } + streamed => streamed, + } } }; let committed = connection @@ -90,26 +100,27 @@ pub(crate) fn refuse_writes(sql: &str) -> Result<(), DriverError> { /// prepared-statement protocol does not run its kind. const UNSUPPORTED_IN_PREPARE: u16 = 1295; -/// The read runs over the text protocol, which runs every statement a -/// text holds. A prepare parses the text the way the server will, with -/// multiple statements off, so a text the server reads as more than one -/// fails here whatever the client's lexer made of it. -async fn refuse_more_than_one( +// The text protocol runs every statement a text holds; a prepare fails one the server reads as several, whatever the client's lexer made of it. +async fn prepare_as_one( connection: &mut MySqlConnection, flavour: MySqlFlavour, sql: &str, -) -> Result<(), DriverError> { +) -> Result, DriverError> { let prepared = (&mut *connection) .prepare(AssertSqlSafe(sql.to_owned()).into_sql_str()) .await; match prepared { - Ok(_) => Ok(()), + Ok(statement) => Ok(statement + .columns() + .iter() + .map(|column| result_column(column, MySqlTimestampMode::WorkspaceUtc)) + .collect()), Err(sqlx::Error::Database(error)) if error .try_downcast_ref::() .is_some_and(|mysql| mysql.number() == UNSUPPORTED_IN_PREPARE) => { - Ok(()) + Ok(Vec::new()) } Err(error) => Err(map_error(&error, flavour)), } diff --git a/core/crates/drivers/mysql/tests/integration/editor_session.rs b/core/crates/drivers/mysql/tests/integration/editor_session.rs index 4b09b574ef..552c9a6ada 100644 --- a/core/crates/drivers/mysql/tests/integration/editor_session.rs +++ b/core/crates/drivers/mysql/tests/integration/editor_session.rs @@ -7,7 +7,7 @@ use std::time::Duration; use tablepro_core::column::ColumnOrigin; use tablepro_core::{ CallOptions, EditorSession, EditorSessionState, ResultEvent, RowLimit, RunEnd, RunOptions, RunSummary, TableRef, - TransactionState, UserSql, + TransactionState, Truncation, UserSql, }; use tablepro_test_fixtures::MysqlFlavour; use tokio_util::sync::CancellationToken; @@ -291,6 +291,108 @@ async fn a_use_moves_the_session_and_it_says_where() { } } +fn started_columns(events: &Events) -> Vec> { + events + .all() + .into_iter() + .filter_map(|event| match event { + ResultEvent::ResultSetStarted { columns } => { + Some(columns.iter().map(|column| column.name.clone()).collect()) + } + _ => None, + }) + .collect() +} + +// sqlx gives an empty set as its OK alone, with no columns. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn a_read_that_finds_no_rows_is_a_set_with_its_columns() { + for flavour in FLAVOURS { + let fixture = start(flavour).await; + let connection = connect(&fixture, false).await; + setup( + connection.as_ref(), + &[ + "CREATE TABLE t (id int PRIMARY KEY, name varchar(20))", + "INSERT INTO t VALUES (1, 'a')", + ], + ) + .await; + let editor = session(connection.as_ref()).await; + + let no_rows = (0, Some(0), Truncation::Complete); + + let empty = run_ok(editor.as_ref(), "SELECT id, name FROM t WHERE id = -1").await; + assert_eq!(started_columns(&empty), [["id", "name"]], "{flavour:?}"); + assert_eq!(empty.finished(), [no_rows], "{flavour:?}"); + assert!(empty.affected().is_empty(), "{flavour:?}"); + + let both = run_ok(editor.as_ref(), "SELECT name FROM t WHERE id = -1; SELECT id FROM t").await; + assert_eq!(started_columns(&both), [["name"], ["id"]], "{flavour:?}"); + assert_eq!( + both.finished(), + [no_rows, (1, Some(1), Truncation::Complete)], + "{flavour:?}" + ); + + let into = run_ok(editor.as_ref(), "SELECT COUNT(*) INTO @rows FROM t").await; + assert_eq!(into.started(), 0, "{flavour:?}: a statement that sends no rows"); + assert_eq!(into.affected().len(), 1, "{flavour:?}"); + + let written = run_ok(editor.as_ref(), "INSERT INTO t VALUES (2, 'b')").await; + assert_eq!(written.started(), 0, "{flavour:?}"); + assert_eq!(written.affected(), [Some(1)], "{flavour:?}"); + editor.close().await; + connection.close().await.expect("a close"); + } +} + +fn warning_codes(events: &Events) -> Vec { + events.rows().iter().filter_map(|row| row.get(1)).map(text).collect() +} + +// Nothing goes between two statements of a run: a SET or a prepare there resets ROW_COUNT() or the warnings. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn a_run_keeps_row_count_and_warnings_between_its_statements() { + for flavour in FLAVOURS { + let fixture = start(flavour).await; + let connection = connect(&fixture, false).await; + setup( + connection.as_ref(), + &[ + "CREATE TABLE w (id int PRIMARY KEY, code varchar(2))", + "INSERT INTO w VALUES (1, 'a'), (2, 'b'), (3, 'c')", + ], + ) + .await; + let editor = session(connection.as_ref()).await; + + let counted = run_ok( + editor.as_ref(), + "UPDATE w SET code = 'z' WHERE id < 3; SELECT ROW_COUNT()", + ) + .await; + let truncated = run_ok( + editor.as_ref(), + "INSERT IGNORE INTO w VALUES (4, 'long'); SHOW WARNINGS", + ) + .await; + let no_data = run_ok(editor.as_ref(), "SELECT id INTO @x FROM w WHERE id = -1; SHOW WARNINGS").await; + + assert_eq!( + counted.rows().first().and_then(|row| row.first()).map(text), + Some("2".to_owned()), + "{flavour:?}" + ); + assert!(warning_codes(&truncated).contains(&"1265".to_owned()), "{flavour:?}"); + assert!(warning_codes(&no_data).contains(&"1329".to_owned()), "{flavour:?}"); + editor.close().await; + connection.close().await.expect("a close"); + } +} + fn traced(schema: &str, table: &str, column: &str) -> Option { Some(ColumnOrigin::new(TableRef::new(Some(schema.to_owned()), table), column)) } @@ -361,6 +463,11 @@ async fn only_a_plain_read_of_a_permanent_table_traces_its_columns() { [table("a"), table("id")], "{flavour:?}" ); + assert_eq!( + origins(editor, "SELECT * FROM t WHERE id = -1").await, + [table("id"), table("a"), table("b")], + "{flavour:?}: a read that finds no rows" + ); assert_eq!( origins(editor, "SELECT id, b + 1 AS c FROM t").await, [table("id"), None], diff --git a/core/crates/drivers/mysql/tests/integration/row_limit.rs b/core/crates/drivers/mysql/tests/integration/row_limit.rs index 28a1854d6c..75fa5ca0c7 100644 --- a/core/crates/drivers/mysql/tests/integration/row_limit.rs +++ b/core/crates/drivers/mysql/tests/integration/row_limit.rs @@ -5,10 +5,10 @@ use std::sync::Arc; use tablepro_core::{Connection, EditorSession, RowLimit, RunEnd, RunOptions, Truncation}; -use tablepro_test_fixtures::MysqlFixture; +use tablepro_test_fixtures::{MysqlFixture, MysqlOptions}; use tokio_util::sync::CancellationToken; -use crate::support::{FLAVOURS, connect, count, run_ok, run_with, session, setup, start}; +use crate::support::{FLAVOURS, connect, connect_root, count, run_ok, run_with, session, setup, start, start_with}; const BIG_ROWS: u64 = 200_000; @@ -140,6 +140,105 @@ async fn insert_select_copies_200000() { } } +// The server cuts INTO OUTFILE at sql_select_limit too, and a plain read before it leaves the limit set. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn select_into_outfile_after_a_limited_read_writes_every_row() { + for flavour in FLAVOURS { + let fixture = start_with( + MysqlOptions::new(flavour) + .allowing_plaintext() + .trusting_function_creators() + .with_server_argument("--secure-file-priv=/tmp"), + ) + .await; + let (connection, owner) = with_big(&fixture).await; + owner.close().await; + // FILE is a global privilege, which only root holds here. + let root = connect_root(&fixture).await; + let editor = session(root.as_ref()).await; + + let (_, before) = run_with(editor.as_ref(), "SELECT id FROM big", limited(100)).await; + let (summary, written) = run_with( + editor.as_ref(), + "SELECT id FROM big INTO OUTFILE '/tmp/every_id.csv'", + limited(100), + ) + .await; + + assert_eq!( + before.finished(), + [(100, None, Truncation::ServerStopped)], + "{flavour:?}" + ); + assert_eq!(summary.end, RunEnd::Completed, "{flavour:?}"); + assert_eq!(written.started(), 0, "{flavour:?}"); + assert_eq!(written.affected(), [Some(BIG_ROWS)], "{flavour:?}"); + + // The backslash makes the driver send this whole, so its first word is an INSERT. + setup(root.as_ref(), &["CREATE TABLE log (path varchar(20))"]).await; + run_with(editor.as_ref(), "SELECT id FROM big", limited(100)).await; + let (summary, batch) = run_with( + editor.as_ref(), + r"INSERT INTO log VALUES ('C:\\tmp'); SELECT id FROM big INTO OUTFILE '/tmp/whole_batch.csv'", + limited(100), + ) + .await; + + assert_eq!(summary.end, RunEnd::Completed, "{flavour:?}"); + assert_eq!(batch.affected(), [Some(1), Some(BIG_ROWS)], "{flavour:?}"); + editor.close().await; + root.close().await.expect("a close"); + connection.close().await.expect("a close"); + } +} + +// A locking read that stopped at the limit would lock only the rows before it. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn locking_reads_read_past_limit() { + for flavour in FLAVOURS { + let fixture = start(flavour).await; + let (connection, editor) = with_big(&fixture).await; + + for sql in ["SELECT id FROM big FOR UPDATE", "SELECT id FROM big LOCK IN SHARE MODE"] { + let (summary, events) = run_with(editor.as_ref(), sql, limited(100)).await; + + assert_eq!(summary.end, RunEnd::Completed, "{flavour:?}: {sql}"); + assert_eq!(events.rows().len(), 100, "{flavour:?}: {sql}"); + assert_eq!( + events.finished(), + [(100, Some(BIG_ROWS), Truncation::ReadPastLimit)], + "{flavour:?}: {sql}" + ); + } + editor.close().await; + connection.close().await.expect("a close"); + } +} + +// The limit is set before a run's first statement, so a SET of the user's in the run holds after it. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn a_users_own_select_limit_holds_for_the_rest_of_its_run() { + for flavour in FLAVOURS { + let fixture = start(flavour).await; + let (connection, editor) = with_big(&fixture).await; + + let (summary, events) = run_with( + editor.as_ref(), + "SET SESSION sql_select_limit = 2; SELECT id FROM big", + limited(100), + ) + .await; + + assert_eq!(summary.end, RunEnd::Completed, "{flavour:?}"); + assert_eq!(events.finished(), [(2, Some(2), Truncation::Complete)], "{flavour:?}"); + editor.close().await; + connection.close().await.expect("a close"); + } +} + /// A SELECT inside a routine is not cut by `sql_select_limit`, so the /// editor reads it to its end and keeps the limit's worth. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] diff --git a/core/crates/ffi/src/connection_url.rs b/core/crates/ffi/src/connection_url.rs index f8bafe28c3..993fb36439 100644 --- a/core/crates/ffi/src/connection_url.rs +++ b/core/crates/ffi/src/connection_url.rs @@ -5,9 +5,9 @@ use tablepro_core::connection_url::{self, ConnectionUrl}; use tablepro_core::is_local_socket_host; use crate::error::CoreError; -use crate::library::TlsModeTag; +use crate::library::{Redacted, TlsModeTag}; -#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +#[derive(Clone, PartialEq, Eq, uniffi::Record)] pub struct ParsedConnectionUrl { pub driver_id: String, pub host: String, @@ -23,6 +23,24 @@ pub struct ParsedConnectionUrl { pub server_name: Option, } +impl std::fmt::Debug for ParsedConnectionUrl { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ParsedConnectionUrl") + .field("driver_id", &self.driver_id) + .field("host", &self.host) + .field("port", &self.port) + .field("username", &self.username) + .field("password", &self.password.as_ref().map(|_| Redacted)) + .field("database", &self.database) + .field("tls_mode", &self.tls_mode) + .field("ca_file", &self.ca_file) + .field("client_certificate", &self.client_certificate) + .field("client_key", &self.client_key) + .field("server_name", &self.server_name) + .finish() + } +} + #[uniffi::export] pub fn parse_connection_url(url: String) -> Result { let parsed = connection_url::parse(&url).map_err(|error| CoreError::failed(error.code(), error.to_string()))?; @@ -102,6 +120,15 @@ mod tests { assert_eq!(parsed.tls_mode, Some(TlsModeTag::VerifyFull)); } + #[test] + fn debug_output_shows_the_url_but_not_its_password() { + let shown = format!("{:?}", parse("postgresql://app:hunter2@db.example.com/shop")); + + assert!(!shown.contains("hunter2"), "{shown}"); + assert!(shown.contains("db.example.com"), "{shown}"); + assert!(shown.contains("password: Some([REDACTED])"), "{shown}"); + } + #[test] fn tls_modes_cross_as_tags() { assert_eq!(parse("mysql://root@localhost/app").tls_mode, None); diff --git a/core/crates/ffi/src/core_handle.rs b/core/crates/ffi/src/core_handle.rs index 295f1f7ca9..33f763036e 100644 --- a/core/crates/ffi/src/core_handle.rs +++ b/core/crates/ffi/src/core_handle.rs @@ -22,7 +22,7 @@ use crate::cancel_token::{CancelToken, token}; use crate::drivers::{self, DriverInfo}; use crate::error::CoreError; use crate::host::{HostConfig, HostSecretStore, HostSecrets}; -use crate::library::{ConnectionLibrary, ConnectionRecord, ConnectionSecrets, apply, blank, parse_id}; +use crate::library::{ConnectionLibrary, ConnectionRecord, ConnectionSecrets, apply, blank, parse_id, tls_config}; use crate::prompter::{HostPrompter, HostPrompterBridge}; use crate::runtime::{self, on_runtime}; use crate::session::DatabaseSession; @@ -172,7 +172,7 @@ impl Core { .find(|saved| saved.id == id) .cloned() .ok_or_else(|| CoreError::failed("storage.connection_not_found", format!("no connection {id}")))?; - let credentials = Arc::new(Prefilled::new(&saved, vault, &secrets)); + let credentials = Arc::new(Prefilled::new(&saved, vault, secrets)); let refreshed = resolve_saved( &saved, ®istry, @@ -221,19 +221,33 @@ impl Core { cancel: Option>, ) -> Result<(), CoreError> { let id = match record.id.trim() { - "" => Uuid::new_v4(), - id => parse_id(id)?, + "" => None, + id => Some(parse_id(id)?), }; - let mut saved = blank(id); + let tls = tls_config(&record.tls)?; let name = record.name.trim().to_owned(); - apply(&record, name, None, &mut saved); + let store = self.store.clone(); let registry = self.registry.clone(); let connector = self.connector.clone(); let vault = self.vault.clone(); + let tasks = self.tasks.clone(); let prompter = self.prompter(prompter); let cancel = token(cancel); on_runtime(&self.tasks, async move { - let credentials = Prefilled::new(&saved, vault, &secrets); + // Over the saved entry, as a save is, so settings the form does not show are tested as Connect uses them. + let stored = match (id, store) { + (Some(id), Some(store)) => tasks + .spawn_blocking_task(move || store.load_blocking()) + .await + .map_err(|failure| CoreError::failed("ffi.panicked", failure.to_string()))?? + .iter() + .find(|saved| saved.id == id) + .cloned(), + _ => None, + }; + let mut saved = stored.unwrap_or_else(|| blank(id.unwrap_or_else(Uuid::new_v4))); + apply(&record, &tls, name, saved.group_id, &mut saved); + let credentials = Prefilled::new(&saved, vault, secrets); let refreshed = resolve_saved( &saved, ®istry, @@ -341,8 +355,8 @@ struct Prefilled { } impl Prefilled { - fn new(saved: &SavedConnection, vault: Arc, secrets: &ConnectionSecrets) -> Self { - let typed = secrets.typed(); + fn new(saved: &SavedConnection, vault: Arc, secrets: ConnectionSecrets) -> Self { + let typed = secrets.into_typed(); Self { typed: Arc::new(Mutex::new(typed)), keyring: KeyringCredentials::for_saved(vault, saved), @@ -479,7 +493,7 @@ mod tests { use async_trait::async_trait; use tablepro_core::credentials::{ - CredentialPrompt, PromptPurpose, PromptReason, PromptReply, SecretError, SecretLookup, + CredentialPrompt, PromptPurpose, PromptReason, PromptReply, SecretError, SecretItem, SecretLookup, }; use tablepro_storage::{SavedSshAuth, SavedSshConfig}; @@ -520,6 +534,64 @@ mod tests { } } + struct NoItems; + + #[async_trait] + impl SecretItems for NoItems { + async fn load_item(&self, _: SecretItem) -> Result, SecretError> { + Ok(None) + } + + async fn store_item(&self, _: SecretItem, _: &SecretString) -> Result<(), SecretError> { + Ok(()) + } + + async fn delete_item(&self, _: SecretItem) -> Result<(), SecretError> { + Ok(()) + } + } + + #[tokio::test] + async fn a_test_of_a_saved_connection_uses_the_settings_the_form_does_not_show() { + let dir = tempfile::tempdir().unwrap(); + let core = Core::build( + HostConfig { + client_version: "test".to_owned(), + storage_dir: Some(dir.path().display().to_string()), + }, + Arc::new(Vault::default()), + Arc::new(NoItems), + runtime::tasks().unwrap(), + ); + let mut stored = blank(Uuid::new_v4()); + stored.name = "shop".to_owned(); + stored.driver_id = "postgres".to_owned(); + stored.host = "127.0.0.1".to_owned(); + stored.port = 1; + stored.database = "shop".to_owned(); + stored.username = "app".to_owned(); + stored.tls = tablepro_core::TlsConfig::disabled(); + // An imported file can carry an option the form has no field for; this one PostgreSQL refuses before dialling. + stored.options.dns_seed_list = true; + core.store.as_ref().unwrap().upsert_blocking(stored.clone()).unwrap(); + let typed = || ConnectionSecrets { + database_password: Some("typed".to_owned()), + ..ConnectionSecrets::default() + }; + + let connect = core + .connect_saved(stored.id.to_string(), typed(), None, None) + .await + .unwrap_err(); + let test = core + .test_connection(ConnectionRecord::from(&stored), typed(), None, None) + .await + .unwrap_err(); + + assert_eq!(connect.code(), "config.network_option_unsupported"); + assert_eq!(test.code(), connect.code()); + } + struct Answer { value: &'static str, asked: Mutex>, @@ -601,7 +673,7 @@ mod tests { .store(saved.id, kind, &SecretString::from("stored before"), "saved") .await .unwrap(); - let credentials = Prefilled::new(&saved, vault, &secrets("")); + let credentials = Prefilled::new(&saved, vault, secrets("")); let first = resolve_saved( &saved, &drivers::registry(), @@ -630,7 +702,7 @@ mod tests { for kind in SecretKind::ALL { for replacement in ["corrected", ""] { let saved = saved(kind); - let credentials = Prefilled::new(&saved, Arc::new(Vault::default()), &secrets("original")); + let credentials = Prefilled::new(&saved, Arc::new(Vault::default()), secrets("original")); let first = resolve_saved( &saved, &drivers::registry(), diff --git a/core/crates/ffi/src/host_key.rs b/core/crates/ffi/src/host_key.rs index c8f14d7424..482c254e04 100644 --- a/core/crates/ffi/src/host_key.rs +++ b/core/crates/ffi/src/host_key.rs @@ -3,11 +3,10 @@ use std::path::Path; use tablepro_core::{DriverError, TransportError}; use tablepro_session::connect::ssh_config; use tablepro_ssh::{ForgetHostKeyError, InProcessSsh}; -use uuid::Uuid; use crate::core_handle::Core; use crate::error::CoreError; -use crate::library::{ConnectionRecord, apply, blank}; +use crate::library::{ConnectionRecord, saved_ssh}; use crate::runtime::on_runtime; #[uniffi::export] @@ -32,11 +31,10 @@ async fn forget( host: &str, known_hosts: &Path, ) -> Result<(), CoreError> { - let mut saved = blank(Uuid::new_v4()); - apply(record, record.name.trim().to_owned(), None, &mut saved); - let settings = saved - .ssh - .ok_or_else(|| CoreError::failed("ssh.host_key_not_this_connection", "the connection has no SSH settings"))?; + let settings = + record.ssh.as_ref().map(saved_ssh).ok_or_else(|| { + CoreError::failed("ssh.host_key_not_this_connection", "the connection has no SSH settings") + })?; let config = ssh_config(&settings).map_err(|error| DriverError::from(TransportError::Ssh(error.into())))?; ssh.forget_changed_host_key(&config, host, known_hosts) .await @@ -50,8 +48,10 @@ async fn forget( #[cfg(test)] mod tests { + use uuid::Uuid; + use super::*; - use crate::library::{SshAuthTag, SshSettings}; + use crate::library::{SshAuthTag, SshSettings, blank}; #[tokio::test] async fn a_host_or_file_outside_this_connection_cannot_be_forgotten() { diff --git a/core/crates/ffi/src/lib.rs b/core/crates/ffi/src/lib.rs index e0372d600b..0ac0879f76 100644 --- a/core/crates/ffi/src/lib.rs +++ b/core/crates/ffi/src/lib.rs @@ -19,6 +19,7 @@ mod run; mod runtime; mod safe_mode_gate; mod session; +mod statement_at; mod syntax; pub mod window_abi; @@ -40,5 +41,6 @@ pub use results::{ResultColumnInfo, ResultSet, ValueKind}; pub use run::{QueryRun, RunEvent, RunListener, RunOutcome, TransactionStateTag, TruncationTag}; pub use safe_mode_gate::RunGate; pub use session::{ColumnSort, DatabaseSession, FailureCauseTag, SessionHealthTag, TableQuery}; +pub use statement_at::StatementSpan; uniffi::setup_scaffolding!(); diff --git a/core/crates/ffi/src/library.rs b/core/crates/ffi/src/library.rs index dce95fe34d..c38c1e5d6e 100644 --- a/core/crates/ffi/src/library.rs +++ b/core/crates/ffi/src/library.rs @@ -3,8 +3,10 @@ use std::sync::Arc; use chrono::{DateTime, Utc}; use secrecy::SecretString; -use tablepro_core::credentials::{SecretKind, SecretVault}; -use tablepro_core::{AuthMode, ClientIdentity, NetworkOptions, SafeModeLevel, TlsConfig, TlsMode}; +use tablepro_core::credentials::{SecretKind, SecretLookup, SecretVault}; +use tablepro_core::{ + AuthMode, ClientIdentity, ConfigError, DriverError, NetworkOptions, SafeModeLevel, TlsConfig, TlsMode, +}; use tablepro_session::runtime::Tasks; use tablepro_storage::{ ConnectionColor, ConnectionGroup, ConnectionGroupName, ConnectionStore, ConnectionTag, ConnectionTagName, @@ -116,7 +118,7 @@ pub struct ConnectionRecord { } // Each secret as the form holds it: None keeps what is stored, an empty string is stored as one. -#[derive(Debug, Clone, Default, PartialEq, Eq, uniffi::Record)] +#[derive(Clone, Default, PartialEq, Eq, uniffi::Record)] pub struct ConnectionSecrets { pub database_password: Option, pub ssh_password: Option, @@ -124,18 +126,38 @@ pub struct ConnectionSecrets { } impl ConnectionSecrets { - pub(crate) fn typed(&self) -> Vec<(SecretKind, SecretString)> { + // Moves each string into a SecretString, which wipes it on drop, instead of leaving a plain copy behind. + pub(crate) fn into_typed(self) -> Vec<(SecretKind, SecretString)> { [ - (SecretKind::DatabasePassword, &self.database_password), - (SecretKind::SshPassword, &self.ssh_password), - (SecretKind::SshPassphrase, &self.ssh_passphrase), + (SecretKind::DatabasePassword, self.database_password), + (SecretKind::SshPassword, self.ssh_password), + (SecretKind::SshPassphrase, self.ssh_passphrase), ] .into_iter() - .filter_map(|(kind, secret)| secret.as_ref().map(|secret| (kind, SecretString::from(secret.clone())))) + .filter_map(|(kind, secret)| secret.map(|secret| (kind, SecretString::from(secret)))) .collect() } } +impl std::fmt::Debug for ConnectionSecrets { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ConnectionSecrets") + .field("database_password", &self.database_password.as_ref().map(|_| Redacted)) + .field("ssh_password", &self.ssh_password.as_ref().map(|_| Redacted)) + .field("ssh_passphrase", &self.ssh_passphrase.as_ref().map(|_| Redacted)) + .finish() + } +} + +// Stands in for a secret in Debug output, so a log line or a panic message never carries one. +pub(crate) struct Redacted; + +impl std::fmt::Debug for Redacted { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("[REDACTED]") + } +} + #[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] pub struct GroupRecord { pub id: String, @@ -171,6 +193,9 @@ pub struct ImportSummary { const RECENT_LIMIT: usize = 8; +// A kind as the vault held it before a save, None for no value or one that could not be read. +type HeldValue = (SecretKind, Option); + #[derive(uniffi::Object)] pub struct ConnectionLibrary { store: ConnectionStore, @@ -194,6 +219,101 @@ impl ConnectionLibrary { .await } + // A value that cannot be read counts as none, so putting it back removes the new one rather than keep it. + async fn held_values(&self, id: Uuid, kinds: Vec) -> Result, CoreError> { + let vault = self.secrets.clone(); + on_runtime(&self.tasks, async move { + let mut held = Vec::new(); + for kind in kinds { + held.push((kind, vault.load(id, kind).await.ok().and_then(SecretLookup::found))); + } + Ok(held) + }) + .await + } + + // Stops at the first refusal. The kinds tried end with the refused one, which a failed call may still have written. + async fn store_secrets( + &self, + saved: &SavedConnection, + typed: Vec<(SecretKind, SecretString)>, + ) -> Result)>, CoreError> { + let vault = self.secrets.clone(); + let stored = saved.clone(); + on_runtime(&self.tasks, async move { + let mut tried = Vec::new(); + for (kind, secret) in typed { + tried.push(kind); + let label = english_secret_label(kind, &stored.secret_target(kind)); + if let Err(error) = vault.store(stored.id, kind, &secret, &label).await { + return Ok(Some((secret_error(error), tried))); + } + } + Ok(None) + }) + .await + } + + // So a second Save does not add the connection twice; it stays only to name a secret known to be held. + async fn take_back(&self, id: Uuid, wrote_one: bool) { + if let Err(error) = self.delete_secrets(vec![id]).await { + tracing::warn!( + code = error.code(), + "could not remove the secrets of a new connection whose save was refused" + ); + if wrote_one { + return; + } + } + if let Err(error) = self.blocking(move |store| Ok(store.remove_blocking(id)?)).await { + tracing::warn!( + code = error.code(), + "could not remove a new connection whose secrets were refused" + ); + } + } + + // Secrets are filed by id alone, so an edit left half done would send one host's password to another. + async fn put_back(&self, before: SavedConnection, previous: Vec, tried: Vec) { + let vault = self.secrets.clone(); + let entry = before.clone(); + let restored = on_runtime(&self.tasks, async move { + for (kind, value) in previous.into_iter().filter(|(kind, _)| tried.contains(kind)) { + let put = match value { + Some(value) => { + let label = english_secret_label(kind, &entry.secret_target(kind)); + vault.store(entry.id, kind, &value, &label).await + } + None => vault.delete_kind(entry.id, kind).await, + }; + if let Err(error) = put { + tracing::warn!( + code = error.code(), + "could not put back a secret of a connection whose edit was refused" + ); + } + } + Ok(()) + }) + .await; + if let Err(error) = restored { + tracing::warn!( + code = error.code(), + "could not put back the secrets of a connection whose edit was refused" + ); + } + let id = before.id; + if let Err(error) = self + .blocking(move |store| Ok(store.update_blocking(id, |saved| *saved = before)?)) + .await + { + tracing::warn!( + code = error.code(), + "could not put back a connection whose edit was refused" + ); + } + } + pub(crate) async fn blocking(&self, work: F) -> Result where T: Send + 'static, @@ -263,50 +383,59 @@ impl ConnectionLibrary { "" => None, id => Some(parse_id(id)?), }; - let saved = self + let tls = tls_config(&record.tls)?; + let typed = secrets.into_typed(); + let kinds = typed.iter().map(|(kind, _)| *kind).collect(); + // Read before anything is written, for a refused secret to put back. + let previous = match id { + Some(id) => self.held_values(id, kinds).await?, + None => Vec::new(), + }; + let (saved, before) = self .blocking(move |store| { - if let Some(id) = id - && let Some(updated) = - store.update_blocking(id, |saved| apply(&record, name.clone(), group_id, saved))? - { - return Ok(updated); + if let Some(id) = id { + let mut before = None; + let updated = store.update_blocking(id, |saved| { + before = Some(saved.clone()); + apply(&record, &tls, name.clone(), group_id, saved); + })?; + if let Some(updated) = updated { + return Ok((updated, before)); + } } let mut created = blank(id.unwrap_or_else(Uuid::new_v4)); - apply(&record, name, group_id, &mut created); + apply(&record, &tls, name, group_id, &mut created); store.upsert_blocking(created.clone())?; - Ok(store + let listed = store .list_blocking()? .connections .iter() .find(|saved| saved.id == created.id) .cloned() - .unwrap_or(created)) + .unwrap_or(created); + Ok((listed, None)) }) .await?; - let vault = self.secrets.clone(); - let stored = saved.clone(); - on_runtime(&self.tasks, async move { - for (kind, secret) in secrets.typed() { - let label = english_secret_label(kind, &stored.secret_target(kind)); - vault - .store(stored.id, kind, &secret, &label) - .await - .map_err(secret_error)?; + if let Some((error, tried)) = self.store_secrets(&saved, typed).await? { + match before { + Some(before) => self.put_back(before, previous, tried).await, + // Every kind before the refused one went in. + None => self.take_back(saved.id, tried.len() > 1).await, } - Ok(()) - }) - .await?; + return Err(error); + } Ok(ConnectionRecord::from(&saved)) } + // Secrets go first, so a refused store leaves the connection listed to delete again, never a secret no entry names. pub async fn delete(&self, id: String) -> Result<(), CoreError> { let id = parse_id(&id)?; + self.delete_secrets(vec![id]).await?; self.blocking(move |store| { store.remove_blocking(id)?; Ok(()) }) - .await?; - self.delete_secrets(vec![id]).await + .await } pub async fn set_favorite(&self, id: String, favorite: bool) -> Result<(), CoreError> { @@ -540,7 +669,14 @@ pub(crate) fn blank(id: Uuid) -> SavedConnection { } } -pub(crate) fn apply(record: &ConnectionRecord, name: String, group_id: Option, saved: &mut SavedConnection) { +// `tls` is `tls_config` of the record's settings, read before any write so a refused pair writes nothing. +pub(crate) fn apply( + record: &ConnectionRecord, + tls: &TlsConfig, + name: String, + group_id: Option, + saved: &mut SavedConnection, +) { saved.name = name; saved.driver_id.clone_from(&record.driver_id); saved.host = record.host.trim().to_owned(); @@ -551,8 +687,8 @@ pub(crate) fn apply(record: &ConnectionRecord, name: String, group_id: Option AuthMode::Kerberos, false => AuthMode::Password, }; - saved.tls = tls_config(&record.tls); - saved.ssh = record.ssh.as_ref().map(ssh_config); + saved.tls = tls.clone(); + saved.ssh = record.ssh.as_ref().map(saved_ssh); saved.safe_mode = record.safe_mode.into(); saved.query_timeout_secs = record.query_timeout_secs; saved.color = record.color.as_deref().and_then(ConnectionColor::from_id); @@ -565,7 +701,8 @@ fn path(text: Option<&String>) -> Option { .map(PathBuf::from) } -fn tls_config(settings: &TlsSettings) -> TlsConfig { +// Half a client identity is refused: dropped, it would save as none and fail later as the server's own refusal. +pub(crate) fn tls_config(settings: &TlsSettings) -> Result { let mut config = TlsConfig::disabled(); config.mode = settings.mode.into(); config.ca_file = path(settings.ca_file.as_ref()); @@ -574,17 +711,18 @@ fn tls_config(settings: &TlsSettings) -> TlsConfig { path(settings.client_key.as_ref()), ) { (Some(certificate), Some(key)) => Some(ClientIdentity { certificate, key }), - _ => None, + (None, None) => None, + _ => return Err(DriverError::from(ConfigError::ClientIdentityIncomplete).into()), }; config.server_name = settings .server_name .as_ref() .map(|name| name.trim().to_owned()) .filter(|name| !name.is_empty()); - config + Ok(config) } -fn ssh_config(settings: &SshSettings) -> SavedSshConfig { +pub(crate) fn saved_ssh(settings: &SshSettings) -> SavedSshConfig { SavedSshConfig { host: settings.host.trim().to_owned(), port: settings.port, @@ -690,3 +828,36 @@ impl From<&ConnectionTag> for TagRecord { } } } + +#[cfg(test)] +mod tests { + use secrecy::ExposeSecret; + + use super::*; + + #[test] + fn debug_output_says_which_secrets_are_held_but_not_what_they_are() { + let secrets = ConnectionSecrets { + database_password: Some("hunter2".to_owned()), + ssh_password: None, + ssh_passphrase: Some(String::new()), + }; + + assert_eq!( + format!("{secrets:?}"), + "ConnectionSecrets { database_password: Some([REDACTED]), ssh_password: None, \ + ssh_passphrase: Some([REDACTED]) }" + ); + let typed = secrets.into_typed(); + assert_eq!( + typed + .iter() + .map(|(kind, secret)| (*kind, secret.expose_secret())) + .collect::>(), + [ + (SecretKind::DatabasePassword, "hunter2"), + (SecretKind::SshPassphrase, "") + ] + ); + } +} diff --git a/core/crates/ffi/src/prompter.rs b/core/crates/ffi/src/prompter.rs index cd5f96fc59..3983551973 100644 --- a/core/crates/ffi/src/prompter.rs +++ b/core/crates/ffi/src/prompter.rs @@ -5,6 +5,8 @@ use secrecy::SecretString; use tablepro_core::credentials::{CredentialPrompt, CredentialPrompter, PromptPurpose, PromptReason, PromptReply}; use tablepro_session::runtime::Tasks; +use crate::library::Redacted; + // The app asks the user and returns the answer; the core calls it on a thread it lets block. #[uniffi::export(callback_interface)] pub trait HostPrompter: Send + Sync { @@ -60,13 +62,26 @@ pub struct PromptFieldInfo { pub secret: bool, } -#[derive(Debug, Clone, PartialEq, Eq, uniffi::Enum)] +#[derive(Clone, PartialEq, Eq, uniffi::Enum)] pub enum PromptAnswer { // One value per field; a host key or a confirmation is trusted with none. Submitted { values: Vec, remember: bool }, Cancelled, } +impl std::fmt::Debug for PromptAnswer { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Submitted { values, remember } => f + .debug_struct("Submitted") + .field("values", &values.iter().map(|_| Redacted).collect::>()) + .field("remember", remember) + .finish(), + Self::Cancelled => f.write_str("Cancelled"), + } + } +} + pub(crate) struct HostPrompterBridge { host: Arc, tasks: Tasks, @@ -259,6 +274,19 @@ mod tests { ); } + #[test] + fn debug_output_of_an_answer_holds_no_typed_value() { + let answer = PromptAnswer::Submitted { + values: vec!["correct horse".to_owned()], + remember: true, + }; + + assert_eq!( + format!("{answer:?}"), + "Submitted { values: [[REDACTED]], remember: true }" + ); + } + #[tokio::test] async fn a_callback_that_fails_counts_as_cancelled() { let reply = bridge(Failing).prompt(passphrase()).await; diff --git a/core/crates/ffi/src/run/run_state.rs b/core/crates/ffi/src/run/run_state.rs index fff9b5efd7..d073acd62a 100644 --- a/core/crates/ffi/src/run/run_state.rs +++ b/core/crates/ffi/src/run/run_state.rs @@ -1,3 +1,4 @@ +use std::panic::{AssertUnwindSafe, catch_unwind}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, PoisonError}; @@ -45,25 +46,34 @@ impl RunState { // Called on a runtime thread. pub(crate) fn post(&self, event: RunEvent) { - { - let mut queue = self.lock(); - match (queue.events.last_mut(), &event) { - ( - Some(RunEvent::RowsAppended { - set_index: queued, - row_count, - }), - RunEvent::RowsAppended { - set_index, - row_count: latest, - }, - ) if queued == set_index => *row_count = *latest, - _ => queue.events.push(event), - } + self.enqueue(event); + self.wake(); + } + + fn enqueue(&self, event: RunEvent) { + let mut queue = self.lock(); + match (queue.events.last_mut(), &event) { + ( + Some(RunEvent::RowsAppended { + set_index: queued, + row_count, + }), + RunEvent::RowsAppended { + set_index, + row_count: latest, + }, + ) if queued == set_index => *row_count = *latest, + _ => queue.events.push(event), + } + } + + // Outside the lock. UniFFI panics when the app's callback throws, which loses this wake and not the run. + fn wake(&self) { + if self.wake_pending.swap(true, Ordering::AcqRel) { + return; } - // Outside the lock: foreign code never runs under one. - if !self.wake_pending.swap(true, Ordering::AcqRel) { - self.listener.events_pending(); + if catch_unwind(AssertUnwindSafe(|| self.listener.events_pending())).is_err() { + tracing::warn!("the app's run listener failed, so it was not told about waiting events"); } } @@ -76,11 +86,13 @@ impl RunState { pub(crate) fn finish(&self, outcome: RunOutcome, transaction: TransactionStateTag) { let sets = self.lock().sets.clone(); sets.iter().for_each(|store| store.finish()); - self.post(RunEvent::Finished { + self.enqueue(RunEvent::Finished { outcome: outcome.clone(), transaction, }); + // Before the wake, so `QueryRun::outcome` returns whatever the listener does. self.outcome.send_replace(Some(outcome)); + self.wake(); } pub(crate) fn outcome(&self) -> watch::Receiver> { @@ -94,6 +106,7 @@ impl RunState { #[cfg(test)] mod tests { + use std::sync::OnceLock; use std::sync::atomic::AtomicUsize; use super::*; @@ -159,4 +172,81 @@ mod tests { ); assert_eq!(*outcome.borrow(), Some(RunOutcome::Cancelled)); } + + // What UniFFI does when the app's callback throws: the call panics on the Rust side. + #[derive(Clone, Default)] + struct ThrowingListener { + calls: Arc, + failures: usize, + } + + impl RunListener for ThrowingListener { + fn events_pending(&self) { + if self.calls.fetch_add(1, Ordering::SeqCst) < self.failures { + panic!("Callback interface failure: the dispatcher is shut down"); + } + } + } + + #[test] + fn a_listener_that_throws_at_the_end_still_ends_the_run() { + let state = RunState::new(Box::new(ThrowingListener { + failures: usize::MAX, + ..ThrowingListener::default() + })); + let outcome = state.outcome(); + + state.finish(RunOutcome::Cancelled, TransactionStateTag::Idle); + + assert_eq!(*outcome.borrow(), Some(RunOutcome::Cancelled)); + assert_eq!( + state.drain(), + [RunEvent::Finished { + outcome: RunOutcome::Cancelled, + transaction: TransactionStateTag::Idle, + }] + ); + } + + #[test] + fn a_listener_that_throws_loses_one_wake_and_no_events() { + let listener = ThrowingListener { + failures: 1, + ..ThrowingListener::default() + }; + let state = RunState::new(Box::new(listener.clone())); + + state.post(rows(0, 1)); + state.post(rows(0, 2)); + assert_eq!(listener.calls.load(Ordering::SeqCst), 1); + assert_eq!(state.drain(), [rows(0, 2)]); + + state.post(rows(0, 3)); + assert_eq!(listener.calls.load(Ordering::SeqCst), 2); + assert_eq!(state.drain(), [rows(0, 3)]); + } + + #[derive(Clone, Default)] + struct OutcomeAtWake { + outcome: Arc>>>, + seen: Arc>>>, + } + + impl RunListener for OutcomeAtWake { + fn events_pending(&self) { + let outcome = self.outcome.get().and_then(|outcome| outcome.borrow().clone()); + self.seen.lock().unwrap().push(outcome); + } + } + + #[test] + fn the_outcome_is_set_before_the_last_wake() { + let listener = OutcomeAtWake::default(); + let state = RunState::new(Box::new(listener.clone())); + listener.outcome.set(state.outcome()).unwrap(); + + state.finish(RunOutcome::Cancelled, TransactionStateTag::Idle); + + assert_eq!(*listener.seen.lock().unwrap(), [Some(RunOutcome::Cancelled)]); + } } diff --git a/core/crates/ffi/src/session.rs b/core/crates/ffi/src/session.rs index 8fd5bd9c10..c77a71ec81 100644 --- a/core/crates/ffi/src/session.rs +++ b/core/crates/ffi/src/session.rs @@ -15,6 +15,7 @@ use crate::error::{BoxedErrorInfo, CoreError}; use crate::library::SafeModeTag; use crate::runtime::on_runtime; use crate::safe_mode_gate::{self, RunGate}; +use crate::statement_at::{self, StatementSpan}; #[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Record)] pub struct ColumnSort { @@ -316,6 +317,11 @@ impl DatabaseSession { safe_mode_gate::gate(&sql, self.session.grammar(), self.session.safe_mode()) } + // `caret` and the span count UTF-16 units. Split by the grammar `gate_run` and the driver read the text with. + pub fn statement_at(&self, sql: String, caret: u32) -> Option { + statement_at::statement_at(&sql, caret, self.session.grammar()) + } + // A level saved while the session is open applies to it at once. False when `level` is on the other side of // Read-only, which takes a new connection; the session keeps its level until then. pub fn set_safe_mode(&self, level: SafeModeTag) -> bool { diff --git a/core/crates/ffi/src/statement_at.rs b/core/crates/ffi/src/statement_at.rs new file mode 100644 index 0000000000..390d9ef5bb --- /dev/null +++ b/core/crates/ffi/src/statement_at.rs @@ -0,0 +1,131 @@ +use tablepro_core::sql_syntax::SqlGrammar; +use tablepro_core::sql_syntax::script::{LexicalSettings, ScriptPlan}; + +// UTF-16 offsets, which is how the host's own text counts. +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Record)] +pub struct StatementSpan { + pub start: u32, + pub end: u32, +} + +// The statement the caret is in, or the last one that starts before it, as the Linux editor's Run Statement picks it. +pub(crate) fn statement_at(sql: &str, caret: u32, grammar: SqlGrammar) -> Option { + let plan = ScriptPlan::build(sql, grammar, LexicalSettings::default_for(grammar)); + let byte = byte_at_unit(sql, usize::try_from(caret).ok()?); + let statement = plan.statement_at(byte)?; + Some(StatementSpan { + start: units_before(sql, statement.range.start)?, + end: units_before(sql, statement.range.end)?, + }) +} + +// A unit inside a surrogate pair belongs to the character the pair makes. +fn byte_at_unit(text: &str, unit: usize) -> usize { + let mut units = 0; + for (byte, c) in text.char_indices() { + units += c.len_utf16(); + if units > unit { + return byte; + } + } + text.len() +} + +fn units_before(text: &str, byte: usize) -> Option { + let units: usize = text + .char_indices() + .take_while(|(start, _)| *start < byte) + .map(|(_, c)| c.len_utf16()) + .sum(); + u32::try_from(units).ok() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn picked(sql: &str, caret: u32, grammar: SqlGrammar) -> Option { + let span = statement_at(sql, caret, grammar)?; + let units: Vec = sql.encode_utf16().collect(); + Some(String::from_utf16_lossy(&units[span.start as usize..span.end as usize])) + } + + #[test] + fn the_caret_picks_the_statement_it_sits_in() { + let sql = "SELECT 1; DELETE FROM t"; + + assert_eq!(picked(sql, 4, SqlGrammar::PostgreSql).as_deref(), Some("SELECT 1")); + assert_eq!( + picked(sql, 20, SqlGrammar::PostgreSql).as_deref(), + Some("DELETE FROM t") + ); + } + + #[test] + fn a_caret_after_a_terminator_or_past_the_end_picks_the_statement_before_it() { + let sql = "SELECT 1;\n\nSELECT 2;"; + + assert_eq!(picked(sql, 9, SqlGrammar::Sqlite).as_deref(), Some("SELECT 1")); + assert_eq!(picked(sql, 10, SqlGrammar::Sqlite).as_deref(), Some("SELECT 1")); + assert_eq!(picked(sql, 9999, SqlGrammar::Sqlite).as_deref(), Some("SELECT 2")); + } + + #[test] + fn a_semicolon_inside_a_string_is_not_a_boundary() { + let sql = "INSERT INTO t VALUES ('a;b'); SELECT 2"; + + assert_eq!( + picked(sql, 24, SqlGrammar::MySql).as_deref(), + Some("INSERT INTO t VALUES ('a;b')") + ); + } + + #[test] + fn a_dollar_quoted_body_is_one_statement() { + let sql = "CREATE FUNCTION f() RETURNS int AS $$ SELECT 1; $$ LANGUAGE sql;\nSELECT 2;"; + + let statement = picked(sql, 40, SqlGrammar::PostgreSql).unwrap_or_default(); + + assert!(statement.starts_with("CREATE FUNCTION"), "{statement}"); + assert!(statement.ends_with("LANGUAGE sql"), "{statement}"); + } + + #[test] + fn a_mysql_procedure_between_delimiter_lines_is_one_statement() { + let sql = "DELIMITER //\nCREATE PROCEDURE p() BEGIN SELECT 1; SELECT 2; END//\nDELIMITER ;\nSELECT 3;"; + + assert_eq!( + picked(sql, 40, SqlGrammar::MySql).as_deref(), + Some("CREATE PROCEDURE p() BEGIN SELECT 1; SELECT 2; END") + ); + } + + #[test] + fn a_sql_server_batch_runs_whole_up_to_its_go_line() { + let sql = "SELECT 1; SELECT 2\nGO\nSELECT 3"; + + assert_eq!(picked(sql, 3, SqlGrammar::MsSql).as_deref(), Some("SELECT 1; SELECT 2")); + assert_eq!(picked(sql, 24, SqlGrammar::MsSql).as_deref(), Some("SELECT 3")); + } + + #[test] + fn offsets_count_utf16_units_past_characters_outside_the_basic_plane() { + // The emoji is two UTF-16 units and four UTF-8 bytes. + let sql = "SELECT '😀'; SELECT 'tên'"; + + let span = statement_at(sql, 14, SqlGrammar::PostgreSql); + + assert_eq!(span, Some(StatementSpan { start: 13, end: 25 })); + assert_eq!(picked(sql, 14, SqlGrammar::PostgreSql).as_deref(), Some("SELECT 'tên'")); + assert_eq!(picked(sql, 8, SqlGrammar::PostgreSql).as_deref(), Some("SELECT '😀'")); + } + + #[test] + fn text_with_no_statement_has_none() { + assert_eq!(statement_at("", 0, SqlGrammar::PostgreSql), None); + assert_eq!( + statement_at(" \n -- only a comment\n", 3, SqlGrammar::PostgreSql), + None + ); + } +} diff --git a/core/crates/ffi/tests/library.rs b/core/crates/ffi/tests/library.rs index 6dc6d69183..521fe87921 100644 --- a/core/crates/ffi/tests/library.rs +++ b/core/crates/ffi/tests/library.rs @@ -6,6 +6,7 @@ use std::collections::HashMap; use std::path::Path; +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use russh::keys::HashAlg; @@ -66,7 +67,87 @@ impl HostSecrets for MemorySecrets { } } -fn core(dir: &Path, secrets: MemorySecrets) -> Arc { +// Refuses a store of the kinds listed, and every delete while deletes are refused, as a locked keychain does. +#[derive(Default, Clone)] +struct RefusingSecrets { + memory: MemorySecrets, + refused_kinds: Arc>>, + refuse_deletes: Arc, + // A refused store that wrote before it failed, as a call that times out can. + write_refused: Arc, +} + +impl RefusingSecrets { + fn refuse(&self, kinds: &[SecretKindTag], deletes: bool) { + *self.refused_kinds.lock().unwrap() = kinds.to_vec(); + self.refuse_deletes.store(deletes, Ordering::SeqCst); + } + + fn held(&self) -> Vec<(String, SecretKindTag)> { + self.memory.0.lock().unwrap().keys().cloned().collect() + } + + fn value(&self, id: &str, kind: SecretKindTag) -> Option { + self.memory.0.lock().unwrap().get(&(id.to_owned(), kind)).cloned() + } + + fn deleting(&self) -> Result<(), HostSecretError> { + match self.refuse_deletes.load(Ordering::SeqCst) { + true => Err(HostSecretError::Locked), + false => Ok(()), + } + } +} + +impl HostSecrets for RefusingSecrets { + fn load(&self, connection_id: String, kind: SecretKindTag) -> Result, HostSecretError> { + self.memory.load(connection_id, kind) + } + + fn store( + &self, + connection_id: String, + kind: SecretKindTag, + secret: String, + label: String, + ) -> Result<(), HostSecretError> { + if self.refused_kinds.lock().unwrap().contains(&kind) { + if self.write_refused.load(Ordering::SeqCst) { + self.memory.store(connection_id, kind, secret, label)?; + } + return Err(HostSecretError::Locked); + } + self.memory.store(connection_id, kind, secret, label) + } + + fn relabel(&self, connection_id: String, kind: SecretKindTag, label: String) -> Result<(), HostSecretError> { + self.memory.relabel(connection_id, kind, label) + } + + fn delete(&self, connection_id: String, kind: SecretKindTag) -> Result<(), HostSecretError> { + self.deleting()?; + self.memory.delete(connection_id, kind) + } + + fn delete_connection(&self, connection_id: String) -> Result<(), HostSecretError> { + self.deleting()?; + self.memory.delete_connection(connection_id) + } + + fn load_item(&self, item: SecretItemTag) -> Result, HostSecretError> { + self.memory.load_item(item) + } + + fn store_item(&self, item: SecretItemTag, secret: String) -> Result<(), HostSecretError> { + self.memory.store_item(item, secret) + } + + fn delete_item(&self, item: SecretItemTag) -> Result<(), HostSecretError> { + self.memory.delete_item(item) + } +} + +fn core(dir: &Path, secrets: impl HostSecrets + 'static) -> Arc { Core::new( HostConfig { client_version: "test".to_owned(), @@ -210,6 +291,215 @@ async fn a_saved_password_goes_to_the_secret_store_and_leaves_with_the_connectio assert!(library.snapshot().await.unwrap().connections.is_empty()); } +fn with_password(password: &str) -> ConnectionSecrets { + ConnectionSecrets { + database_password: Some(password.to_owned()), + ..ConnectionSecrets::default() + } +} + +#[tokio::test] +async fn a_delete_the_secret_store_refuses_keeps_the_connection_and_its_password() { + let dir = tempfile::tempdir().unwrap(); + let secrets = RefusingSecrets::default(); + let core = core(dir.path(), secrets.clone()); + let library = core.library().unwrap(); + let saved = library + .save(record("shop", "mysql", "shop"), with_password("hunter2")) + .await + .unwrap(); + + secrets.refuse(&[], true); + let refused = library.delete(saved.id.clone()).await.unwrap_err(); + + assert_eq!(refused.code(), "secret.locked"); + assert_eq!(library.snapshot().await.unwrap().connections.len(), 1); + assert_eq!(secrets.held(), [(saved.id.clone(), SecretKindTag::DatabasePassword)]); + + secrets.refuse(&[], false); + library.delete(saved.id).await.unwrap(); + + assert!(library.snapshot().await.unwrap().connections.is_empty()); + assert!(secrets.held().is_empty()); +} + +#[tokio::test] +async fn a_new_connection_whose_password_is_refused_is_not_saved_so_a_retry_adds_it_once() { + let dir = tempfile::tempdir().unwrap(); + let secrets = RefusingSecrets::default(); + let core = core(dir.path(), secrets.clone()); + let library = core.library().unwrap(); + + secrets.refuse(&[SecretKindTag::DatabasePassword], true); + let refused = library + .save(record("shop", "mysql", "shop"), with_password("hunter2")) + .await + .unwrap_err(); + + assert_eq!(refused.code(), "secret.locked"); + assert!(library.snapshot().await.unwrap().connections.is_empty()); + + secrets.refuse(&[], false); + let saved = library + .save(record("shop", "mysql", "shop"), with_password("hunter2")) + .await + .unwrap(); + + assert_eq!(library.snapshot().await.unwrap().connections.len(), 1); + assert_eq!(secrets.held(), [(saved.id, SecretKindTag::DatabasePassword)]); +} + +#[tokio::test] +async fn a_refused_ssh_password_takes_back_the_new_connection_and_its_database_password() { + let dir = tempfile::tempdir().unwrap(); + let secrets = RefusingSecrets::default(); + let core = core(dir.path(), secrets.clone()); + let library = core.library().unwrap(); + let both = ConnectionSecrets { + ssh_password: Some("tunnel".to_owned()), + ..with_password("hunter2") + }; + + secrets.refuse(&[SecretKindTag::SshPassword], false); + let refused = library.save(through_ssh(22), both.clone()).await.unwrap_err(); + + assert_eq!(refused.code(), "secret.locked"); + assert!(library.snapshot().await.unwrap().connections.is_empty()); + assert!(secrets.held().is_empty()); + + // With the database password in and no way to take it out, the entry stays to name it. + secrets.refuse(&[SecretKindTag::SshPassword], true); + let kept = library.save(through_ssh(22), both).await.unwrap_err(); + let snapshot = library.snapshot().await.unwrap(); + + assert_eq!(kept.code(), "secret.locked"); + assert_eq!(snapshot.connections.len(), 1); + assert_eq!( + secrets.held(), + [(snapshot.connections[0].id.clone(), SecretKindTag::DatabasePassword)] + ); +} + +#[tokio::test] +async fn a_refused_store_that_wrote_anyway_leaves_no_secret_behind_a_new_connection() { + let dir = tempfile::tempdir().unwrap(); + let secrets = RefusingSecrets::default(); + let core = core(dir.path(), secrets.clone()); + let library = core.library().unwrap(); + + secrets.refuse(&[SecretKindTag::DatabasePassword], false); + secrets.write_refused.store(true, Ordering::SeqCst); + let refused = library + .save(record("shop", "mysql", "shop"), with_password("hunter2")) + .await + .unwrap_err(); + + assert_eq!(refused.code(), "secret.locked"); + assert!(library.snapshot().await.unwrap().connections.is_empty()); + assert!(secrets.held().is_empty()); +} + +#[tokio::test] +async fn an_edit_whose_password_is_refused_keeps_the_old_host_and_the_old_password() { + let dir = tempfile::tempdir().unwrap(); + let secrets = RefusingSecrets::default(); + let core = core(dir.path(), secrets.clone()); + let library = core.library().unwrap(); + let mut prod = record("shop", "mysql", "shop"); + prod.host = "prod-db.internal".to_owned(); + prod.port = 3306; + let saved = library.save(prod, with_password("prod-secret")).await.unwrap(); + let mut staging = saved.clone(); + staging.host = "staging-db.internal".to_owned(); + + secrets.refuse(&[SecretKindTag::DatabasePassword], false); + secrets.write_refused.store(true, Ordering::SeqCst); + let refused = library + .save(staging, with_password("staging-secret")) + .await + .unwrap_err(); + let snapshot = library.snapshot().await.unwrap(); + + assert_eq!(refused.code(), "secret.locked"); + assert_eq!(snapshot.connections[0].host, "prod-db.internal"); + assert_eq!( + secrets.value(&saved.id, SecretKindTag::DatabasePassword).as_deref(), + Some("prod-secret") + ); +} + +#[tokio::test] +async fn an_edit_refused_at_its_second_secret_takes_back_the_first() { + let dir = tempfile::tempdir().unwrap(); + let secrets = RefusingSecrets::default(); + let core = core(dir.path(), secrets.clone()); + let library = core.library().unwrap(); + let saved = library + .save( + through_ssh(22), + ConnectionSecrets { + ssh_password: Some("old-tunnel".to_owned()), + ..ConnectionSecrets::default() + }, + ) + .await + .unwrap(); + let mut moved = saved.clone(); + moved.host = "10.0.0.9".to_owned(); + + secrets.refuse(&[SecretKindTag::SshPassword], false); + let refused = library + .save( + moved, + ConnectionSecrets { + ssh_password: Some("new-tunnel".to_owned()), + ..with_password("new-db") + }, + ) + .await + .unwrap_err(); + let snapshot = library.snapshot().await.unwrap(); + + assert_eq!(refused.code(), "secret.locked"); + assert_eq!(snapshot.connections[0].host, saved.host); + // The database password had none before the edit, so the one the edit wrote is gone again. + assert_eq!(secrets.value(&saved.id, SecretKindTag::DatabasePassword), None); + assert_eq!( + secrets.value(&saved.id, SecretKindTag::SshPassword).as_deref(), + Some("old-tunnel") + ); +} + +#[tokio::test] +async fn half_a_client_certificate_is_refused_by_save_and_by_test() { + let dir = tempfile::tempdir().unwrap(); + let core = core(dir.path(), MemorySecrets::default()); + let library = core.library().unwrap(); + let mut certificate_only = record("shop", "postgres", "shop"); + certificate_only.host = "127.0.0.1".to_owned(); + certificate_only.port = 1; + certificate_only.tls.mode = TlsModeTag::Require; + certificate_only.tls.client_certificate = Some("/certs/client.pem".to_owned()); + let mut key_only = certificate_only.clone(); + key_only.tls.client_certificate = None; + key_only.tls.client_key = Some("/certs/client.key".to_owned()); + + for half in [certificate_only, key_only] { + let saved = library + .save(half.clone(), ConnectionSecrets::default()) + .await + .unwrap_err(); + let tested = core + .test_connection(half, with_password("typed"), None, None) + .await + .unwrap_err(); + + assert_eq!(saved.code(), "config.client_identity_incomplete"); + assert_eq!(tested.code(), "config.client_identity_incomplete"); + } + assert!(library.snapshot().await.unwrap().connections.is_empty()); +} + #[tokio::test] async fn a_saved_sqlite_file_connects_lists_its_tables_and_becomes_recent() { let dir = tempfile::tempdir().unwrap(); diff --git a/core/crates/mcp/src/collected_results.rs b/core/crates/mcp/src/collected_results.rs index 8712a1a366..cb348ecaee 100644 --- a/core/crates/mcp/src/collected_results.rs +++ b/core/crates/mcp/src/collected_results.rs @@ -17,6 +17,24 @@ const MAX_NOTICES: usize = 20; /// loop sends one per pass, each under the row limit. const MAX_RESULT_SETS: usize = 20; +// The app holds the kept rows and their JSON at once, and a client's context takes far less. +pub const MAX_ANSWER_BYTES: usize = 4 * 1024 * 1024; + +pub const MAX_VALUE_BYTES: usize = 64 * 1024; + +const VALUES_CUT_NOTE: &str = "Some values were longer than 64 KB and were cut there; each one cut ends in a mark \ +with its full length. Read such a value in parts with a narrower query."; + +const SIZE_LIMIT_NOTE: &str = "The rows reached TablePro's 4 MB limit for one answer, so a result set marked \ +truncated holds only the rows that fit; ask for fewer columns or rows."; + +// Every value `encoded_len` does not measure renders to fewer bytes than this. +const SCALAR_BYTES: usize = 40; + +// A cell's quotes, colon and comma, and a row's braces and comma. +const CELL_BYTES: usize = 6; +const ROW_BYTES: usize = 3; + #[derive(Debug, Default)] pub struct CollectedResults { pub sets: Vec, @@ -28,6 +46,10 @@ pub struct CollectedResults { /// The run sent more result sets than are kept; every one after the /// last kept is left out. pub sets_left_out: bool, + // What the kept rows render to, as `encoded_len` counts it. + pub bytes: usize, + pub values_cut: bool, + pub size_limited: bool, } #[derive(Debug)] @@ -35,24 +57,56 @@ pub struct CollectedSet { pub columns: Arc<[ResultColumn]>, pub rows: Vec>, pub truncated: bool, + // The answer's size limit, not the row limit, ended the set. + pub size_limited: bool, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum LongValues { + Cut, + // An export is data to keep exactly, so a row that does not fit is left out instead. + Whole, +} + +// `rows` is per result set, `bytes` for every set of the answer together. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Keep { + pub rows: usize, + pub bytes: usize, + pub long_values: LongValues, +} + +impl Keep { + pub fn results(rows: usize) -> Self { + Self { + rows, + bytes: MAX_ANSWER_BYTES, + long_values: LongValues::Cut, + } + } + + pub fn export(rows: usize, bytes: usize) -> Self { + Self { + rows, + bytes, + long_values: LongValues::Whole, + } + } } -/// Keeps at most `limit` rows of each result set. -/// -/// A streamed read has no limit of its own, so with `stop_at_limit` the -/// row past the limit ends it. An editor run is limited by the driver -/// and keeps going for what it writes. +// A streamed read has no limit of its own, so with `stop_at_limit` the first row it cannot keep ends it. An editor +// run keeps going for what it writes. pub struct ResultCollector { state: Arc>, - limit: usize, + keep: Keep, stop_at_limit: bool, } impl ResultCollector { - pub fn new(state: Arc>, limit: usize, stop_at_limit: bool) -> Self { + pub fn new(state: Arc>, keep: Keep, stop_at_limit: bool) -> Self { Self { state, - limit, + keep, stop_at_limit, } } @@ -72,6 +126,7 @@ impl ResultCollector { columns, rows: Vec::new(), truncated: false, + size_limited: false, }); } } @@ -104,7 +159,11 @@ impl ResultCollector { } ResultEvent::Notice(notice) => { if state.notices.len() < MAX_NOTICES { - state.notices.push(format!("{}: {}", notice.severity, notice.message)); + let message = match notice.message.len() > MAX_VALUE_BYTES { + true => cut_text(¬ice.message, notice.message.len()), + false => notice.message, + }; + state.notices.push(format!("{}: {message}", notice.severity)); } } // The answer keeps far fewer notices than a run does. @@ -114,27 +173,48 @@ impl ResultCollector { } fn keep_rows(&self, state: &mut CollectedResults, rows: Vec>) -> SinkFlow { - let mut undecodable = false; - let limit = self.limit; let Some(set) = state.sets.last_mut() else { return SinkFlow::Continue; }; for mut row in rows { - if set.rows.len() >= limit { + // A set keeps a prefix of its rows, so once one is left out none after it is kept. + if set.truncated { + break; + } + if set.rows.len() >= self.keep.rows { set.truncated = true; break; } - for value in &mut row { + let (mut undecodable, mut cut) = (false, false); + let mut size = ROW_BYTES; + for (index, value) in row.iter_mut().enumerate() { if matches!(value, Value::Undecodable(_)) { *value = Value::Null; undecodable = true; } + if self.keep.long_values == LongValues::Cut + && let Some(shortened) = cut_value(value) + { + *value = shortened; + cut = true; + } + let name = set.columns.get(index).map_or(0, |column| column.name.len()); + size = size + .saturating_add(name + CELL_BYTES) + .saturating_add(encoded_len(value)); } + if state.size_limited || state.bytes.saturating_add(size) > self.keep.bytes { + set.truncated = true; + set.size_limited = true; + state.size_limited = true; + break; + } + state.bytes += size; + state.undecodable |= undecodable; + state.values_cut |= cut; set.rows.push(row); } - let past_limit = set.truncated; - state.undecodable |= undecodable; - if past_limit && self.stop_at_limit { + if set.truncated && self.stop_at_limit { state.stopped_at_limit = true; return SinkFlow::ConsumerGone; } @@ -142,6 +222,55 @@ impl ResultCollector { } } +// Measured without rendering, since rendering a value is what the limit is there to avoid. +fn encoded_len(value: &Value) -> usize { + match value { + Value::Text(text) => text.len(), + // `0x` and two hex digits a byte. + Value::Bytes(bytes) => bytes.len().saturating_mul(2).saturating_add(2), + Value::Json(json) => json.as_str().len(), + Value::Other(other) => other.text.len(), + Value::Bits(bits) => usize::try_from(bits.bit_len()).unwrap_or(usize::MAX), + Value::Array(values) => values + .iter() + .fold(2, |sum, item| sum.saturating_add(encoded_len(item)).saturating_add(1)), + _ => SCALAR_BYTES, + } +} + +// A JSON document or an array cut short is no longer one, so every cut value goes out as text. +fn cut_value(value: &Value) -> Option { + if encoded_len(value) <= MAX_VALUE_BYTES { + return None; + } + let cut = match value { + Value::Text(text) => cut_text(text, text.len()), + Value::Json(json) => cut_text(json.as_str(), json.as_str().len()), + Value::Other(other) => cut_text(&other.text, other.text.len()), + Value::Bytes(bytes) => { + let head = bytes.get(..MAX_VALUE_BYTES / 2).unwrap_or(bytes); + let text = tablepro_core::export::value_to_text(&Value::Bytes(head.to_vec())).unwrap_or_default(); + format!("{text}{}", cut_mark(bytes.len())) + } + other => { + let text = tablepro_core::export::value_to_text(other).unwrap_or_default(); + cut_text(&text, text.len()) + } + }; + Some(Value::Text(cut)) +} + +fn cut_text(text: &str, total: usize) -> String { + let head = text + .get(..text.floor_char_boundary(MAX_VALUE_BYTES)) + .unwrap_or_default(); + format!("{head}{}", cut_mark(total)) +} + +fn cut_mark(total: usize) -> String { + format!(" [TablePro cut this value: {total} bytes in all]") +} + #[async_trait] impl ResultSink for ResultCollector { async fn accept(&mut self, event: ResultEvent) -> SinkFlow { @@ -206,7 +335,13 @@ impl CollectedResults { if self.sets_left_out { notes.push("The statement sent more than 20 result sets; only the first 20 are here."); } - if self.sets.iter().any(|set| set.truncated) { + if self.values_cut { + notes.push(VALUES_CUT_NOTE); + } + if self.size_limited { + notes.push(SIZE_LIMIT_NOTE); + } + if self.sets.iter().any(|set| set.truncated && !set.size_limited) { notes.push( "A result set has more rows than the limit; ask for fewer with a WHERE clause or raise max_rows.", ); @@ -224,6 +359,7 @@ impl CollectedResults { #[cfg(test)] mod tests { + use tablepro_core::ServerNotice; use tablepro_core::column::ColumnType; use super::*; @@ -236,10 +372,21 @@ mod tests { (0..count).map(|n| vec![Value::Int(n)]).collect() } + fn text_columns(names: &[&str]) -> Arc<[ResultColumn]> { + names + .iter() + .map(|name| ResultColumn::new(*name, ColumnType::untyped_text("text"))) + .collect() + } + + fn long_text(bytes: usize) -> Value { + Value::Text("x".repeat(bytes)) + } + #[tokio::test] async fn a_streamed_read_stops_at_the_row_past_the_limit() { let state = Arc::new(Mutex::new(CollectedResults::default())); - let mut sink = ResultCollector::new(state.clone(), 3, true); + let mut sink = ResultCollector::new(state.clone(), Keep::results(3), true); sink.accept(ResultEvent::ResultSetStarted { columns: columns() }).await; let first = sink.accept(ResultEvent::Rows(rows(2))).await; @@ -256,7 +403,7 @@ mod tests { #[tokio::test] async fn a_run_keeps_going_past_the_limit_for_what_it_writes() { let state = Arc::new(Mutex::new(CollectedResults::default())); - let mut sink = ResultCollector::new(state.clone(), 1, false); + let mut sink = ResultCollector::new(state.clone(), Keep::results(1), false); sink.accept(ResultEvent::ResultSetStarted { columns: columns() }).await; let flow = sink.accept(ResultEvent::Rows(rows(5))).await; @@ -275,7 +422,7 @@ mod tests { async fn result_sets_past_the_cap_are_left_out_and_end_a_streamed_read() { for stop_at_limit in [true, false] { let state = Arc::new(Mutex::new(CollectedResults::default())); - let mut sink = ResultCollector::new(state.clone(), 5, stop_at_limit); + let mut sink = ResultCollector::new(state.clone(), Keep::results(5), stop_at_limit); let mut flows = Vec::new(); for _ in 0..=MAX_RESULT_SETS { @@ -301,7 +448,7 @@ mod tests { #[tokio::test] async fn the_answer_keeps_exact_digits_and_column_order() { let state = Arc::new(Mutex::new(CollectedResults::default())); - let mut sink = ResultCollector::new(state.clone(), 10, true); + let mut sink = ResultCollector::new(state.clone(), Keep::results(10), true); let columns: Arc<[ResultColumn]> = vec![ ResultColumn::new("z", ColumnType::untyped_text("bigint")), ResultColumn::new("a", ColumnType::untyped_text("text")), @@ -324,4 +471,158 @@ mod tests { assert!(json.contains(r#""execution_time_ms":7"#), "{json}"); assert!(!json.contains("rows_affected"), "{json}"); } + + #[tokio::test] + async fn a_value_past_the_cap_goes_out_cut_with_its_length() { + let state = Arc::new(Mutex::new(CollectedResults::default())); + let mut sink = ResultCollector::new(state.clone(), Keep::results(10), true); + // A two-byte character across the cap, so the cut has to fall before it. + let text = format!("{}é{}", "x".repeat(MAX_VALUE_BYTES - 1), "y".repeat(10)); + sink.accept(ResultEvent::ResultSetStarted { + columns: text_columns(&["text", "bytes", "short"]), + }) + .await; + + let flow = sink + .accept(ResultEvent::Rows(vec![vec![ + Value::Text(text.clone()), + Value::Bytes(vec![0xab; MAX_VALUE_BYTES]), + Value::Text("kept".to_owned()), + ]])) + .await; + + assert_eq!(flow, SinkFlow::Continue); + let state = state.lock().unwrap(); + let row = &state.sets[0].rows[0]; + let expected = format!("{}{}", "x".repeat(MAX_VALUE_BYTES - 1), cut_mark(text.len())); + assert_eq!(row[0], Value::Text(expected)); + let Value::Text(hex) = &row[1] else { + panic!("the bytes were not cut to text: {:?}", row[1]); + }; + assert!(hex.starts_with("0xabab"), "{}", &hex[..10]); + assert!(hex.ends_with(&cut_mark(MAX_VALUE_BYTES)), "{}", &hex[hex.len() - 60..]); + assert_eq!(hex.len(), 2 + MAX_VALUE_BYTES + cut_mark(MAX_VALUE_BYTES).len()); + assert_eq!(row[2], Value::Text("kept".to_owned())); + assert!(state.values_cut); + assert!(!state.sets[0].truncated); + let json = state.to_json(Duration::ZERO, &[]).expect("JSON"); + assert!( + json.contains(VALUES_CUT_NOTE), + "the answer does not say values were cut" + ); + } + + #[tokio::test] + async fn rows_stop_at_the_answers_size_and_end_a_streamed_read() { + let state = Arc::new(Mutex::new(CollectedResults::default())); + let mut sink = ResultCollector::new(state.clone(), Keep::results(1_000), true); + sink.accept(ResultEvent::ResultSetStarted { + columns: text_columns(&["b"]), + }) + .await; + + let flow = sink + .accept(ResultEvent::Rows( + (0..100).map(|_| vec![long_text(MAX_VALUE_BYTES)]).collect(), + )) + .await; + + assert_eq!(flow, SinkFlow::ConsumerGone); + let state = state.lock().unwrap(); + let per_row = ROW_BYTES + "b".len() + CELL_BYTES + MAX_VALUE_BYTES; + assert_eq!(state.sets[0].rows.len(), MAX_ANSWER_BYTES / per_row); + assert!(state.stopped_at_limit); + assert!(state.size_limited && state.sets[0].truncated && state.sets[0].size_limited); + assert!(!state.values_cut, "a value at the cap was cut"); + let json = state.to_json(Duration::ZERO, &[]).expect("JSON"); + assert!(json.len() < MAX_ANSWER_BYTES, "{} bytes", json.len()); + assert!( + json.contains(SIZE_LIMIT_NOTE), + "the answer does not say it reached its size" + ); + assert!(!json.contains("raise max_rows"), "the row limit is blamed for the size"); + } + + #[tokio::test] + async fn a_write_runs_on_past_the_answers_size_and_keeps_no_row_after_it() { + let state = Arc::new(Mutex::new(CollectedResults::default())); + let mut sink = ResultCollector::new(state.clone(), Keep::results(1_000), false); + sink.accept(ResultEvent::ResultSetStarted { + columns: text_columns(&["b"]), + }) + .await; + + let first = sink + .accept(ResultEvent::Rows( + (0..100).map(|_| vec![long_text(MAX_VALUE_BYTES)]).collect(), + )) + .await; + let second = sink + .accept(ResultEvent::Rows(vec![vec![Value::Text("small".to_owned())]])) + .await; + sink.accept(ResultEvent::CommandCompleted { + rows_affected: Some(100), + }) + .await; + + assert_eq!((first, second), (SinkFlow::Continue, SinkFlow::Continue)); + let state = state.lock().unwrap(); + assert!(!state.stopped_at_limit); + assert!(state.size_limited); + assert!( + state.sets[0] + .rows + .iter() + .all(|row| row[0] != Value::Text("small".to_owned())), + "a row after one left out was kept" + ); + assert_eq!(state.rows_affected, Some(100)); + } + + #[tokio::test] + async fn an_export_keeps_values_whole_and_leaves_out_a_row_that_does_not_fit() { + let state = Arc::new(Mutex::new(CollectedResults::default())); + let mut sink = ResultCollector::new(state.clone(), Keep::export(10, 2 * MAX_VALUE_BYTES), true); + sink.accept(ResultEvent::ResultSetStarted { + columns: text_columns(&["b"]), + }) + .await; + + let flow = sink + .accept(ResultEvent::Rows(vec![ + vec![long_text(MAX_VALUE_BYTES + 1)], + vec![long_text(MAX_VALUE_BYTES + 1)], + vec![Value::Text("small".to_owned())], + ])) + .await; + + assert_eq!(flow, SinkFlow::ConsumerGone); + let state = state.lock().unwrap(); + assert_eq!(state.sets[0].rows, vec![vec![long_text(MAX_VALUE_BYTES + 1)]]); + assert!(!state.values_cut); + assert!(state.size_limited && state.sets[0].truncated); + } + + #[tokio::test] + async fn a_long_notice_is_cut_like_a_value() { + let state = Arc::new(Mutex::new(CollectedResults::default())); + let mut sink = ResultCollector::new(state.clone(), Keep::results(10), true); + + sink.accept(ResultEvent::Notice(ServerNotice { + severity: "NOTICE".to_owned(), + message: "n".repeat(3 * MAX_VALUE_BYTES), + code: None, + })) + .await; + + let state = state.lock().unwrap(); + assert_eq!( + state.notices, + [format!( + "NOTICE: {}{}", + "n".repeat(MAX_VALUE_BYTES), + cut_mark(3 * MAX_VALUE_BYTES) + )] + ); + } } diff --git a/core/crates/mcp/src/export_tools.rs b/core/crates/mcp/src/export_tools.rs index 05a06671b4..971b6b73a6 100644 --- a/core/crates/mcp/src/export_tools.rs +++ b/core/crates/mcp/src/export_tools.rs @@ -10,6 +10,7 @@ use tablepro_session::Session; use tokio_util::sync::CancellationToken; use crate::browse_request::filter_set; +use crate::collected_results::{Keep, MAX_ANSWER_BYTES}; use crate::connection_tools::to_json; use crate::query_tools::{DEFAULT_TIMEOUT_SECONDS, row_limit, stream, table_shape}; use crate::tool_args::{ExportArgs, ExportFormat}; @@ -55,21 +56,31 @@ pub async fn export_data( return Err(ToolError::InvalidArgument("tables holds an empty name".to_owned())); } let session = context - .held_session(&connection, args.database.as_deref()) + .held_session(&connection, args.database.as_deref(), &cancel) .await? .session; let mut exports = Vec::with_capacity(args.tables.len()); - let mut undecodable = false; + let (mut undecodable, mut size_limited, mut spent) = (false, false, 0_usize); for name in args.tables { let table = TableRef::new(args.schema.clone(), name); - let (export, lossy) = export_table(&session, table, args.format, limit, &cancel).await?; + // Every table of the call shares one answer, so each gets what the ones before it left. + let keep = Keep::export(limit, MAX_ANSWER_BYTES.saturating_sub(spent)); + let (export, read) = export_table(&session, table, args.format, keep, &cancel).await?; exports.push(export); - undecodable |= lossy; + undecodable |= read.undecodable; + size_limited |= read.size_limited; + spent = spent.saturating_add(read.bytes); + } + let mut notes = Vec::new(); + if undecodable { + notes.push("Some values could not be decoded by TablePro and are written as null."); + } + if size_limited { + notes.push( + "The rows reached TablePro's 4 MB limit for one answer, so a table marked truncated holds only the rows \ +that fit, and the tables after it may hold none; export fewer tables, columns or rows per call.", + ); } - let notes = match undecodable { - true => vec!["Some values could not be decoded by TablePro and are written as null."], - false => Vec::new(), - }; to_json(&ExportDocument { format: args.format, exports, @@ -77,22 +88,28 @@ pub async fn export_data( }) } -// The flag says a value the driver could not decode went out as null. +struct TableRead { + // A value the driver could not decode went out as null. + undecodable: bool, + size_limited: bool, + bytes: usize, +} + async fn export_table( session: &Session, table: TableRef, format: ExportFormat, - limit: usize, + keep: Keep, cancel: &CancellationToken, -) -> Result<(TableExport, bool), ToolError> { - let (columns, identity) = table_shape(session, &table).await?; +) -> Result<(TableExport, TableRead), ToolError> { + let (columns, identity) = table_shape(session, &table, cancel).await?; let catalog: Arc<[ColumnInfo]> = columns.into(); let filter = filter_set(&[], None)?; let read_table = table.clone(); let read_catalog = catalog.clone(); let mut collected = stream( session, - limit, + keep, Duration::from_secs(u64::from(DEFAULT_TIMEOUT_SECONDS)), cancel, Some(catalog.clone()), @@ -124,7 +141,12 @@ async fn export_table( data, table: table.name, }; - Ok((export, collected.undecodable)) + let read = TableRead { + undecodable: collected.undecodable, + size_limited: collected.size_limited, + bytes: collected.bytes, + }; + Ok((export, read)) } fn render( diff --git a/core/crates/mcp/src/lib.rs b/core/crates/mcp/src/lib.rs index 5154d9f34f..8a2390a60b 100644 --- a/core/crates/mcp/src/lib.rs +++ b/core/crates/mcp/src/lib.rs @@ -20,6 +20,7 @@ mod tool_error; mod tool_server; pub use access_token::{AccessToken, TokenError}; +pub use collected_results::{MAX_ANSWER_BYTES, MAX_VALUE_BYTES}; pub use export_tools::MAX_EXPORT_TABLES; pub use mcp_host::{McpHost, OpenOutcome, WriteApproval}; pub use mcp_server::{MCP_PATH, McpServer, StartError}; diff --git a/core/crates/mcp/src/query_tools.rs b/core/crates/mcp/src/query_tools.rs index 8250756f51..cb8b47c0a8 100644 --- a/core/crates/mcp/src/query_tools.rs +++ b/core/crates/mcp/src/query_tools.rs @@ -16,7 +16,7 @@ use tablepro_storage::query_history::{NewEntry, Outcome}; use tokio_util::sync::CancellationToken; use crate::browse_request::{filter_set, sort_keys}; -use crate::collected_results::{CollectedResults, ResultCollector}; +use crate::collected_results::{CollectedResults, Keep, ResultCollector}; use crate::connection_tools::to_json; use crate::mcp_host::WriteApproval; use crate::schema_tools::no_such_table; @@ -47,7 +47,9 @@ pub async fn execute_query( args.query.len() ))); } - let HeldSession { session, database } = context.held_session(&connection, args.database.as_deref()).await?; + let HeldSession { session, database } = context + .held_session(&connection, args.database.as_deref(), &cancel) + .await?; let dialect = session .current_dialect() .ok_or_else(|| ToolError::NotConnected(format!("“{}” has no connection right now", connection.name)))?; @@ -182,7 +184,10 @@ type Ran = Result<(CollectedResults, Vec<&'static str>), ToolError>; /// read-only, so a write the statement gate did not see still fails. async fn run_read(session: &Session, text: &str, limit: usize, timeout: Duration, cancel: &CancellationToken) -> Ran { let read = ReadStatement::User(UserSql::new(text)); - let collected = stream(session, limit, timeout, cancel, None, move |_| Ok(read.clone())).await?; + let collected = stream(session, Keep::results(limit), timeout, cancel, None, move |_| { + Ok(read.clone()) + }) + .await?; Ok((collected, Vec::new())) } @@ -198,7 +203,7 @@ async fn run_write(session: &Session, text: &str, limit: usize, timeout: Duratio .await .map_err(|error| ToolError::from_operation(&error))?; let state = Arc::new(Mutex::new(CollectedResults::default())); - let sink = ResultCollector::new(state.clone(), limit, false); + let sink = ResultCollector::new(state.clone(), Keep::results(limit), false); let stop = cancel.child_token(); // What the user approved runs to its end. A driver stops fetching at // the limit, and with it a statement's side effects; the answer keeps @@ -238,13 +243,10 @@ async fn run_write(session: &Session, text: &str, limit: usize, timeout: Duratio } } -/// Stream the read `build` makes from the lease that runs it, keeping -/// `limit` rows of each result set and stopping at the row past them. -/// A table's rows pass through `precision`, so a decimal keeps the scale -/// its column declares, as in an export. +// A table's rows pass through `precision`, so a decimal keeps the scale its column declares, as in an export. pub(crate) async fn stream( session: &Session, - limit: usize, + keep: Keep, timeout: Duration, cancel: &CancellationToken, precision: Option>, @@ -263,7 +265,7 @@ where async move { *state.lock().unwrap_or_else(PoisonError::into_inner) = CollectedResults::default(); let read = build(&lease)?; - let collector: Box = Box::new(ResultCollector::new(state, limit, true)); + let collector: Box = Box::new(ResultCollector::new(state, keep, true)); let sink: Box = match precision { Some(catalog) => Box::new(DeclaredPrecisionSink::new(collector, catalog)), None => collector, @@ -294,13 +296,13 @@ pub async fn browse_table( let filter = filter_set(&args.filters, args.logic)?; let session = context.session(&connection).await?; let table = TableRef::new(args.schema, args.table); - let (columns, identity) = table_shape(&session, &table).await?; + let (columns, identity) = table_shape(&session, &table, &cancel).await?; let sort = sort_keys(&args.sort, &columns)?; let catalog: Arc<[ColumnInfo]> = columns.into(); let started = Instant::now(); let collected = stream( &session, - limit, + Keep::results(limit), Duration::from_secs(u64::from(DEFAULT_TIMEOUT_SECONDS)), &cancel, Some(catalog.clone()), @@ -323,9 +325,10 @@ pub async fn browse_table( pub(crate) async fn table_shape( session: &Session, table: &TableRef, + cancel: &CancellationToken, ) -> Result<(Vec, RowIdentity), ToolError> { let read = session - .run(OperationKind::Metadata, |lease, call| { + .run_cancellable(OperationKind::Metadata, cancel, |lease, call| { let table = table.clone(); async move { let columns = lease.fetch_columns(&table, call.clone()).await?; @@ -350,14 +353,18 @@ struct RowCount { row_count: u64, } -pub async fn count_rows(context: &ToolContext, args: CountArgs) -> Result { +pub async fn count_rows( + context: &ToolContext, + args: CountArgs, + cancel: CancellationToken, +) -> Result { let connection = context.connection(&args.connection_id)?; let filter = filter_set(&args.filters, args.logic)?; let session = context.session(&connection).await?; let table = TableRef::new(args.schema, args.table); - let (columns, _) = table_shape(&session, &table).await?; + let (columns, _) = table_shape(&session, &table, &cancel).await?; let count = session - .run(OperationKind::Count, |lease, call| { + .run_cancellable(OperationKind::Count, &cancel, |lease, call| { let (table, columns, filter) = (&table, &columns, &filter); async move { let statement = build_count(&*lease.dialect(), table, columns, filter)?; diff --git a/core/crates/mcp/src/schema_tools.rs b/core/crates/mcp/src/schema_tools.rs index 933436b0fc..bfc678caa1 100644 --- a/core/crates/mcp/src/schema_tools.rs +++ b/core/crates/mcp/src/schema_tools.rs @@ -4,6 +4,7 @@ use tablepro_core::ddl::BuildDdlError; use tablepro_core::meta::{IndexKey, ObjectKind, ObjectScope, RowCountEstimate, SchemaInfo}; use tablepro_core::{ColumnInfo, DriverError, ForeignKeyInfo, IndexInfo, TableRef}; use tablepro_session::{OperationError, OperationKind}; +use tokio_util::sync::CancellationToken; use crate::connection_tools::to_json; use crate::tool_args::{ConnectionArgs, DatabaseArgs, SchemaArgs, TableArgs}; @@ -22,12 +23,17 @@ struct ListedSchema { is_system: bool, } -pub async fn list_schemas(context: &ToolContext, args: DatabaseArgs) -> Result { +pub async fn list_schemas( + context: &ToolContext, + args: DatabaseArgs, + cancel: CancellationToken, +) -> Result { let connection = context.connection(&args.connection_id)?; let session = context - .held_session(&connection, args.database.as_deref()) + .held_session(&connection, args.database.as_deref(), &cancel) .await? .session; + // The catalog's reads are shared with the window, so a client that goes away does not stop them. let schemas = session .schemas() .await @@ -55,11 +61,15 @@ struct ListedDatabase { is_current: bool, } -pub async fn list_databases(context: &ToolContext, args: ConnectionArgs) -> Result { +pub async fn list_databases( + context: &ToolContext, + args: ConnectionArgs, + cancel: CancellationToken, +) -> Result { let connection = context.connection(&args.connection_id)?; let session = context.session(&connection).await?; let databases = session - .run(OperationKind::Metadata, |lease, call| async move { + .run_cancellable(OperationKind::Metadata, &cancel, |lease, call| async move { Ok(lease.list_databases(call).await?) }) .await @@ -101,10 +111,14 @@ struct ListedTable { /// Read again rather than from the window's catalog, so a table a /// client just created is there; the read also refreshes the sidebar. -pub async fn list_tables(context: &ToolContext, args: SchemaArgs) -> Result { +pub async fn list_tables( + context: &ToolContext, + args: SchemaArgs, + cancel: CancellationToken, +) -> Result { let connection = context.connection(&args.connection_id)?; let session = context - .held_session(&connection, args.database.as_deref()) + .held_session(&connection, args.database.as_deref(), &cancel) .await? .session; let scope = match args.schema { @@ -193,15 +207,19 @@ struct DescribedForeignKey { type TableParts = (Vec, Vec, Vec, RowCountEstimate); -pub async fn describe_table(context: &ToolContext, args: TableArgs) -> Result { +pub async fn describe_table( + context: &ToolContext, + args: TableArgs, + cancel: CancellationToken, +) -> Result { let connection = context.connection(&args.connection_id)?; let session = context - .held_session(&connection, args.database.as_deref()) + .held_session(&connection, args.database.as_deref(), &cancel) .await? .session; let table = TableRef::new(args.schema, args.table); let read: Result = session - .run(OperationKind::Metadata, |lease, call| { + .run_cancellable(OperationKind::Metadata, &cancel, |lease, call| { let table = table.clone(); async move { let columns = lease.fetch_columns(&table, call.clone()).await?; @@ -300,15 +318,19 @@ struct TableDdl { /// The statements Show CREATE TABLE gives: the server's own text where /// it keeps one, otherwise the table's definition written in its /// dialect. -pub async fn get_table_ddl(context: &ToolContext, args: TableArgs) -> Result { +pub async fn get_table_ddl( + context: &ToolContext, + args: TableArgs, + cancel: CancellationToken, +) -> Result { let connection = context.connection(&args.connection_id)?; let session = context - .held_session(&connection, args.database.as_deref()) + .held_session(&connection, args.database.as_deref(), &cancel) .await? .session; let table = TableRef::new(args.schema, args.table); let read = session - .run(OperationKind::Metadata, |lease, call| { + .run_cancellable(OperationKind::Metadata, &cancel, |lease, call| { let table = table.clone(); async move { let definition = lease.fetch_table_definition(&table, call).await?; diff --git a/core/crates/mcp/src/tool_context.rs b/core/crates/mcp/src/tool_context.rs index 2e8c23c5b3..e3a32c674a 100644 --- a/core/crates/mcp/src/tool_context.rs +++ b/core/crates/mcp/src/tool_context.rs @@ -3,6 +3,7 @@ use std::sync::Arc; use tablepro_core::dialect::DatabaseLayout; use tablepro_session::{OperationKind, Session}; use tablepro_storage::{ConnectionList, ConnectionListState, ConnectionStore, SavedConnection}; +use tokio_util::sync::CancellationToken; use uuid::Uuid; use crate::mcp_host::McpHost; @@ -75,6 +76,7 @@ impl ToolContext { &self, connection: &SavedConnection, database: Option<&str>, + cancel: &CancellationToken, ) -> Result { let own = self.session(connection).await?; let Some(database) = database else { @@ -102,7 +104,7 @@ impl ToolContext { database: Some(database.to_owned()), }); } - if own.database().is_none() && opened_in(&own, database).await { + if own.database().is_none() && opened_in(&own, database, cancel).await { return Ok(HeldSession { session: own, database: None, @@ -117,9 +119,9 @@ impl ToolContext { } // A connection saved without a database opens the login's default, which only the server can name. -async fn opened_in(session: &Session, database: &str) -> bool { +async fn opened_in(session: &Session, database: &str, cancel: &CancellationToken) -> bool { session - .run(OperationKind::Metadata, |lease, call| async move { + .run_cancellable(OperationKind::Metadata, cancel, |lease, call| async move { Ok(lease.list_databases(call).await?) }) .await diff --git a/core/crates/mcp/src/tool_server.rs b/core/crates/mcp/src/tool_server.rs index ccb43177f5..0035a7c2c2 100644 --- a/core/crates/mcp/src/tool_server.rs +++ b/core/crates/mcp/src/tool_server.rs @@ -23,7 +23,8 @@ is_open is false, and the user may have to answer TablePro first. Call describe_ against a table. execute_query runs one statement: a read runs read-only, and a write runs only where the \ connection's External Clients level allows it and after the user approves it in TablePro. A refusal is the \ user's setting, not a passing failure, so do not retry it. A tool that takes database reaches only a database \ -the user already opened in TablePro beside the connection."; +the user already opened in TablePro beside the connection. An answer holds about 4 MB of rows at most, and a \ +statement's or a browse's answer cuts a value longer than 64 KB; its notes say when."; /// The tools, as a stateless handler: every call reads the saved /// connections and the windows again. @@ -52,13 +53,13 @@ impl ToolServer { match request.name.as_ref() { "list_connections" => connection_tools::list_connections(tools, parse(arguments)?).await, "connect" => connection_tools::connect(tools, parse(arguments)?).await, - "list_databases" => schema_tools::list_databases(tools, parse(arguments)?).await, - "list_schemas" => schema_tools::list_schemas(tools, parse(arguments)?).await, - "list_tables" => schema_tools::list_tables(tools, parse(arguments)?).await, - "describe_table" => schema_tools::describe_table(tools, parse(arguments)?).await, - "get_table_ddl" => schema_tools::get_table_ddl(tools, parse(arguments)?).await, + "list_databases" => schema_tools::list_databases(tools, parse(arguments)?, cancel).await, + "list_schemas" => schema_tools::list_schemas(tools, parse(arguments)?, cancel).await, + "list_tables" => schema_tools::list_tables(tools, parse(arguments)?, cancel).await, + "describe_table" => schema_tools::describe_table(tools, parse(arguments)?, cancel).await, + "get_table_ddl" => schema_tools::get_table_ddl(tools, parse(arguments)?, cancel).await, "browse_table" => query_tools::browse_table(tools, parse(arguments)?, cancel).await, - "count_rows" => query_tools::count_rows(tools, parse(arguments)?).await, + "count_rows" => query_tools::count_rows(tools, parse(arguments)?, cancel).await, "execute_query" => query_tools::execute_query(tools, parse(arguments)?, cancel).await, "export_data" => export_tools::export_data(tools, parse(arguments)?, cancel).await, other => Err(ToolError::NotFound(format!("there is no tool named {other}"))), diff --git a/core/crates/mcp/tests/mcp_server.rs b/core/crates/mcp/tests/mcp_server.rs index a87ec76e6f..3e4a7b807e 100644 --- a/core/crates/mcp/tests/mcp_server.rs +++ b/core/crates/mcp/tests/mcp_server.rs @@ -13,6 +13,7 @@ use std::collections::HashMap; use std::path::Path; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; +use std::time::Duration; use async_trait::async_trait; use secrecy::{ExposeSecret, SecretString}; @@ -21,14 +22,14 @@ use tablepro_core::credentials::CredentialInteraction; use tablepro_core::dialect::SqlDialect; use tablepro_core::{ ConnectEnvironment, ExternalAccess, FileEndpoint, LivenessPolicy, ResultEvent, ResultSink, RunOptions, - SafeModeLevel, SinkFlow, TlsConfig, UserSql, WorkspaceSettings, + SafeModeLevel, SinkFlow, TableRef, TlsConfig, UserSql, WorkspaceSettings, }; use tablepro_driver_sqlite::SqliteDriver; use tablepro_mcp::{AccessToken, McpHost, McpServer, OpenOutcome, ToolContext, WriteApproval}; use tablepro_session::runtime::Tasks; use tablepro_session::{ ConnectRequest, ConnectServices, ConnectionMetadata, Connector, DefaultDeadlines, DriverConnector, - NoStoredCredentials, RequestTarget, Session, SessionDeps, + NoStoredCredentials, OperationKind, RequestTarget, Session, SessionDeps, }; use tablepro_ssh::{SshRuntimeCell, SshServices}; use tablepro_storage::query_history::NewEntry; @@ -289,6 +290,26 @@ struct Reply { /// One request on a connection of its own, written by hand so the test /// controls every header a client sends. async fn send(port: u16, path: &str, headers: &[(&str, &str)], body: &str) -> Reply { + let mut stream = open_request(port, path, headers, body).await; + let mut raw = Vec::new(); + stream.read_to_end(&mut raw).await.expect("the response"); + let text = String::from_utf8(raw).expect("a UTF-8 response"); + let (head, body) = text.split_once("\r\n\r\n").expect("a header block"); + let status = head + .split(' ') + .nth(1) + .and_then(|code| code.parse().ok()) + .expect("a status code"); + let headers = head.to_ascii_lowercase(); + let body = match headers.contains("transfer-encoding: chunked") { + true => unchunk(body), + false => body.to_owned(), + }; + Reply { status, headers, body } +} + +// The request sent and its answer not read yet, so a test can go away while the server works on it. +async fn open_request(port: u16, path: &str, headers: &[(&str, &str)], body: &str) -> tokio::net::TcpStream { let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", port)) .await .expect("a connection to the server"); @@ -305,21 +326,7 @@ async fn send(port: u16, path: &str, headers: &[(&str, &str)], body: &str) -> Re request.push_str("\r\n"); request.push_str(body); stream.write_all(request.as_bytes()).await.expect("the request"); - let mut raw = Vec::new(); - stream.read_to_end(&mut raw).await.expect("the response"); - let text = String::from_utf8(raw).expect("a UTF-8 response"); - let (head, body) = text.split_once("\r\n\r\n").expect("a header block"); - let status = head - .split(' ') - .nth(1) - .and_then(|code| code.parse().ok()) - .expect("a status code"); - let headers = head.to_ascii_lowercase(); - let body = match headers.contains("transfer-encoding: chunked") { - true => unchunk(body), - false => body.to_owned(), - }; - Reply { status, headers, body } + stream } fn unchunk(mut body: &str) -> String { @@ -1093,3 +1100,101 @@ async fn an_export_outside_its_limits_is_refused() { .await; assert!(missing_failed, "{missing}"); } + +// A SQLite session has one connection, so a read of its own waits while another call holds it. +async fn connection_is_held(session: &Session) -> bool { + let table = TableRef::new(None, "orders"); + let read = session.run(OperationKind::Metadata, |lease, call| { + let table = table.clone(); + async move { Ok(lease.fetch_columns(&table, call).await?) } + }); + tokio::time::timeout(Duration::from_millis(200), read).await.is_err() +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_count_stops_when_its_client_goes_away() { + let harness = harness().await; + let writable = harness + .host + .session(harness.ids.writable) + .await + .expect("the writable session"); + let readable = harness + .host + .session(harness.ids.readable) + .await + .expect("the readable session"); + run_all( + &writable, + &["CREATE VIEW endless AS WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n) SELECT x FROM n"], + ) + .await; + let body = json!({ + "jsonrpc": "2.0", + "id": 1, + "method": "tools/call", + "params": { + "name": "count_rows", + "arguments": { "connection_id": harness.ids.readable.to_string(), "table": "endless" }, + }, + }) + .to_string(); + let client = open_request(harness.server.address().port(), "/mcp", &MCP_HEADERS, &body).await; + let mut counting = false; + for _ in 0..50 { + if connection_is_held(&readable).await { + counting = true; + break; + } + tokio::time::sleep(Duration::from_millis(50)).await; + } + assert!(counting, "the count never started"); + + drop(client); + let counted = tokio::time::timeout(Duration::from_secs(10), order_count(&harness)).await; + + assert_eq!( + counted.ok(), + Some(3), + "the count kept the connection after its client left" + ); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn an_answer_cuts_long_values_and_stops_at_its_size() { + let harness = harness().await; + + let (failed, text) = call( + &harness, + "execute_query", + json!({ + "connection_id": harness.ids.readable.to_string(), + "query": "WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 200) \ + SELECT x, replace(hex(zeroblob(100000)), '0', 'x') AS body FROM n", + "max_rows": 200, + }), + ) + .await; + + assert!(!failed, "{text}"); + assert!(text.len() < tablepro_mcp::MAX_ANSWER_BYTES, "{} bytes", text.len()); + let answer = parsed(&text); + let set = &answer["result_sets"][0]; + let rows = set["rows"].as_array().expect("rows"); + assert!(!rows.is_empty() && rows.len() < 200, "{} rows", rows.len()); + assert_eq!(set["truncated"], true); + let cut = format!( + "{} [TablePro cut this value: 200000 bytes in all]", + "x".repeat(tablepro_mcp::MAX_VALUE_BYTES) + ); + assert_eq!(rows[0]["body"].as_str(), Some(cut.as_str())); + let notes: Vec<&str> = answer["notes"] + .as_array() + .expect("notes") + .iter() + .filter_map(Json::as_str) + .collect(); + assert!(notes.iter().any(|note| note.contains("64 KB")), "{notes:?}"); + assert!(notes.iter().any(|note| note.contains("4 MB")), "{notes:?}"); + assert!(!notes.iter().any(|note| note.contains("max_rows")), "{notes:?}"); +} diff --git a/core/crates/net/src/dial.rs b/core/crates/net/src/dial.rs index e9b68e69a7..fde8778c14 100644 --- a/core/crates/net/src/dial.rs +++ b/core/crates/net/src/dial.rs @@ -1,11 +1,17 @@ use std::io; use std::net::SocketAddr; +use std::time::Duration; use tablepro_core::{LivenessPolicy, NetworkEndpoint, TimeoutPhase, TransportError}; use tokio::net::TcpStream; +use tokio::task::JoinSet; +use tokio::time::Instant; use crate::socket_options::apply_socket_options; +// RFC 8305, section 5: the recommended Connection Attempt Delay. +const CONNECTION_ATTEMPT_DELAY: Duration = Duration::from_millis(250); + #[derive(Debug)] pub(crate) enum AttemptFailure { Refused, @@ -13,31 +19,87 @@ pub(crate) enum AttemptFailure { Other(io::Error), } +impl AttemptFailure { + fn from_io(error: io::Error) -> Self { + match error.kind() { + io::ErrorKind::ConnectionRefused => Self::Refused, + _ => Self::Other(error), + } + } +} + +// Happy Eyeballs (RFC 8305): attempts start a delay apart, or at once after a failure, under one deadline. pub(crate) async fn dial_addresses( addresses: Vec, endpoint: &NetworkEndpoint, liveness: &LivenessPolicy, + deadline: Instant, ) -> Result { - let mut failures = Vec::with_capacity(addresses.len()); - for address in addresses { - match tokio::time::timeout(liveness.connect_timeout, TcpStream::connect(address)).await { - Ok(Ok(stream)) => { - apply_socket_options(&stream, liveness).map_err(|error| TransportError::Unreachable { - endpoint: endpoint.clone(), - detail: error.to_string(), - })?; - return Ok(stream); - } - Ok(Err(error)) if error.kind() == io::ErrorKind::ConnectionRefused => { - failures.push(AttemptFailure::Refused) + let mut waiting = interleave_families(addresses).into_iter(); + let mut attempts = JoinSet::new(); + let mut failures = Vec::with_capacity(waiting.len()); + let mut next_start = Instant::now(); + loop { + if Instant::now() >= next_start + && let Some(address) = waiting.next() + { + attempts.spawn(TcpStream::connect(address)); + next_start = Instant::now() + CONNECTION_ATTEMPT_DELAY; + } + if attempts.is_empty() { + break; + } + let staggering = !waiting.as_slice().is_empty(); + tokio::select! { + biased; + Some(joined) = attempts.join_next() => match joined { + Ok(Ok(stream)) => { + apply_socket_options(&stream, liveness).map_err(|error| TransportError::Unreachable { + endpoint: endpoint.clone(), + detail: error.to_string(), + })?; + return Ok(stream); + } + Ok(Err(error)) => { + failures.push(AttemptFailure::from_io(error)); + next_start = Instant::now(); + } + Err(stopped) => { + failures.push(AttemptFailure::Other(stopped.into())); + next_start = Instant::now(); + } + }, + () = tokio::time::sleep_until(deadline) => { + failures.extend(std::iter::repeat_with(|| AttemptFailure::TimedOut).take(attempts.len())); + break; } - Ok(Err(error)) => failures.push(AttemptFailure::Other(error)), - Err(_) => failures.push(AttemptFailure::TimedOut), + () = tokio::time::sleep_until(next_start), if staggering => {} } } Err(aggregate_failures(endpoint, failures)) } +// RFC 8305, section 4: alternating families, a dead one costs one delay rather than one per address. +fn interleave_families(addresses: Vec) -> Vec { + let Some(first_is_v6) = addresses.first().map(SocketAddr::is_ipv6) else { + return addresses; + }; + let (preferred, other): (Vec<_>, Vec<_>) = addresses + .into_iter() + .partition(|address| address.is_ipv6() == first_is_v6); + let mut preferred = preferred.into_iter(); + let mut other = other.into_iter(); + let mut ordered = Vec::with_capacity(preferred.len() + other.len()); + loop { + let (next_preferred, next_other) = (preferred.next(), other.next()); + if next_preferred.is_none() && next_other.is_none() { + return ordered; + } + ordered.extend(next_preferred); + ordered.extend(next_other); + } +} + pub(crate) fn aggregate_failures(endpoint: &NetworkEndpoint, failures: Vec) -> TransportError { let every = |predicate: fn(&AttemptFailure) -> bool| !failures.is_empty() && failures.iter().all(predicate); if every(|failure| matches!(failure, AttemptFailure::Refused)) { @@ -65,7 +127,6 @@ pub(crate) fn aggregate_failures(endpoint: &NetworkEndpoint, failures: Vec LivenessPolicy { + LivenessPolicy { + connect_timeout: Duration::from_secs(3), + ..LivenessPolicy::DESKTOP + } + } + + fn deadline(liveness: &LivenessPolicy) -> Instant { + Instant::now() + liveness.connect_timeout + } + fn full_backlog_listener() -> (Socket, SocketAddr, Vec) { let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap(); socket.bind(&SocketAddr::from(([127, 0, 0, 1], 0)).into()).unwrap(); @@ -95,38 +168,113 @@ mod tests { let live = StdListener::bind("127.0.0.1:0").unwrap(); let live_address = live.local_addr().unwrap(); let endpoint = NetworkEndpoint::new("127.0.0.1", live_address.port()).unwrap(); + let liveness = quick_liveness(); - let stream = dial_addresses(vec![dropped, live_address], &endpoint, &quick_liveness()) + let stream = dial_addresses(vec![dropped, live_address], &endpoint, &liveness, deadline(&liveness)) .await .unwrap(); assert_eq!(stream.peer_addr().unwrap(), live_address); } #[tokio::test] - async fn full_backlog_listener_then_live_listener_connects_second() { + async fn black_holed_first_address_connects_inside_one_connect_timeout() { let (_full, full_address, _fillers) = full_backlog_listener(); let live = StdListener::bind("127.0.0.1:0").unwrap(); let live_address = live.local_addr().unwrap(); let endpoint = NetworkEndpoint::new("127.0.0.1", live_address.port()).unwrap(); + let liveness = racing_liveness(); - let stream = dial_addresses(vec![full_address, live_address], &endpoint, &quick_liveness()) - .await - .unwrap(); + // The bound the SQL Server, ClickHouse and Redis drivers put around `Transport::open`. + let dialled = tokio::time::timeout( + liveness.connect_timeout, + dial_addresses( + vec![full_address, live_address], + &endpoint, + &liveness, + deadline(&liveness), + ), + ) + .await; + + let stream = dialled.expect("the second address within the connect timeout").unwrap(); assert_eq!(stream.peer_addr().unwrap(), live_address); } + #[tokio::test] + async fn every_address_black_holed_is_connect_timeout_at_the_deadline() { + let (_first, first_address, _first_fillers) = full_backlog_listener(); + let (_second, second_address, _second_fillers) = full_backlog_listener(); + let endpoint = NetworkEndpoint::new("127.0.0.1", first_address.port()).unwrap(); + let liveness = LivenessPolicy { + connect_timeout: Duration::from_millis(600), + ..LivenessPolicy::DESKTOP + }; + let started = Instant::now(); + + let dialled = dial_addresses( + vec![first_address, second_address], + &endpoint, + &liveness, + deadline(&liveness), + ) + .await; + + assert!(matches!( + dialled, + Err(TransportError::Timeout { + phase: TimeoutPhase::Connect + }) + )); + assert!(started.elapsed() < liveness.connect_timeout * 2); + } + #[tokio::test] async fn full_backlog_listener_alone_is_connect_timeout() { let (_full, full_address, _fillers) = full_backlog_listener(); let endpoint = NetworkEndpoint::new("127.0.0.1", full_address.port()).unwrap(); + let liveness = quick_liveness(); assert!(matches!( - dial_addresses(vec![full_address], &endpoint, &quick_liveness()).await, + dial_addresses(vec![full_address], &endpoint, &liveness, deadline(&liveness)).await, Err(TransportError::Timeout { phase: TimeoutPhase::Connect }) )); } + #[tokio::test] + async fn every_address_refused_is_refused() { + let first = StdListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap(); + let second = StdListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap(); + let endpoint = NetworkEndpoint::new("127.0.0.1", first.port()).unwrap(); + let liveness = racing_liveness(); + + assert_eq!( + dial_addresses(vec![first, second], &endpoint, &liveness, deadline(&liveness)) + .await + .err(), + Some(TransportError::Refused { + endpoint: endpoint.clone() + }) + ); + } + + #[test] + fn interleave_families_alternates_from_the_first_family() { + let v6 = |last: u16| SocketAddr::from(([0x2001, 0xdb8, 0, 0, 0, 0, 0, last], 5432)); + let v4 = |last: u8| SocketAddr::from(([192, 0, 2, last], 5432)); + + assert_eq!( + interleave_families(vec![v6(1), v6(2), v6(3), v4(1), v4(2)]), + vec![v6(1), v4(1), v6(2), v4(2), v6(3)] + ); + assert_eq!( + interleave_families(vec![v4(1), v4(2), v6(1)]), + vec![v4(1), v6(1), v4(2)] + ); + assert_eq!(interleave_families(vec![v4(1), v4(2)]), vec![v4(1), v4(2)]); + assert!(interleave_families(Vec::new()).is_empty()); + } + fn endpoint() -> NetworkEndpoint { NetworkEndpoint::new("db.example.com", 5432).unwrap() } diff --git a/core/crates/net/src/direct_tcp.rs b/core/crates/net/src/direct_tcp.rs index 2a73bc098f..86d98ebfe0 100644 --- a/core/crates/net/src/direct_tcp.rs +++ b/core/crates/net/src/direct_tcp.rs @@ -3,8 +3,10 @@ use std::sync::Arc; use async_trait::async_trait; use tablepro_core::{ - LivenessPolicy, NetworkEndpoint, Transport, TransportError, TransportKind, TransportRoute, TransportStream, + LivenessPolicy, NetworkEndpoint, TimeoutPhase, Transport, TransportError, TransportKind, TransportRoute, + TransportStream, }; +use tokio::time::Instant; use crate::dial::dial_addresses; @@ -38,15 +40,22 @@ impl Transport for DirectTcpTransport { } async fn open(&self) -> Result, TransportError> { - let resolved = tokio::net::lookup_host((self.endpoint.host(), self.endpoint.port())).await; - let addresses: Vec = match resolved { - Ok(addresses) => addresses.collect(), - Err(error) => { + // The lookup shares the dial's deadline, since callers bound the whole open by the connect timeout. + let deadline = Instant::now() + self.liveness.connect_timeout; + let resolving = tokio::net::lookup_host((self.endpoint.host(), self.endpoint.port())); + let addresses: Vec = match tokio::time::timeout_at(deadline, resolving).await { + Ok(Ok(addresses)) => addresses.collect(), + Ok(Err(error)) => { return Err(TransportError::NameResolution { host: self.endpoint.host().to_owned(), detail: error.to_string(), }); } + Err(_) => { + return Err(TransportError::Timeout { + phase: TimeoutPhase::Connect, + }); + } }; if addresses.is_empty() { return Err(TransportError::NameResolution { @@ -54,7 +63,7 @@ impl Transport for DirectTcpTransport { detail: "the name resolved to no addresses".to_owned(), }); } - let stream = dial_addresses(addresses, &self.endpoint, &self.liveness).await?; + let stream = dial_addresses(addresses, &self.endpoint, &self.liveness, deadline).await?; Ok(Box::new(stream)) } diff --git a/linux/docs/adding-drivers.md b/linux/docs/adding-drivers.md index 0e6bea2e41..6c01685906 100644 --- a/linux/docs/adding-drivers.md +++ b/linux/docs/adding-drivers.md @@ -321,7 +321,7 @@ A run streams `ResultEvent`s into a `ResultSink`: `RowLimit` wraps a `NonZeroU64`; `RowLimit::EDITOR_DEFAULT` is 10,000. - `query_bound` keeps at most the limit and reads one more row to mark the result truncated. -- An editor run keeps at most the limit in each result set and sends `RowLimitReached` once. A set the server cut at one row past the limit ends with `Truncation::ServerStopped` and no `total_rows`: PostgreSQL reports it when its cursor stopped the set, MySQL when `sql_select_limit` did. A set read past the limit ends with `Truncation::ReadPastLimit`. Where the engine allows, the rest of the set is stopped on the server rather than read: a `FETCH` of one row past the limit (PostgreSQL), `sql_select_limit` (MySQL), `KILL QUERY` (ClickHouse). SQLite stops stepping and DuckDB stops fetching the chunks of its streamed result, so the rest is never produced, and the Cassandra driver asks the server for no more pages (`ServerStopped`). Otherwise the rows past the limit are counted without being decoded (SQL Server, and MySQL where `sql_select_limit` does not apply). +- An editor run keeps at most the limit in each result set and sends `RowLimitReached` once. A set the server cut at one row past the limit ends with `Truncation::ServerStopped` and no `total_rows`: PostgreSQL reports it when its cursor stopped the set, MySQL when `sql_select_limit` did. A set read past the limit ends with `Truncation::ReadPastLimit`. Where the engine allows, the rest of the set is stopped on the server rather than read: a `FETCH` of one row past the limit (PostgreSQL), `sql_select_limit` (MySQL), `KILL QUERY` (ClickHouse). SQLite stops stepping and DuckDB stops fetching the chunks of its streamed result, so the rest is never produced, and the Cassandra driver asks the server for no more pages (`ServerStopped`). Otherwise the rows past the limit are counted without being decoded (SQL Server, and MySQL in a run with a `SELECT` that writes to a file or locks, since `sql_select_limit` would also cut what it writes or locks). - The limit never ends the script, the user's transaction or the statements after the limited one. The conformance case `row_limit_keeps_transaction_and_later_statements` checks this on every engine with editor transactions. - The driver never rewrites the user's SQL to add a `LIMIT`. - `stream_read` has no limit. diff --git a/linux/docs/drivers/mysql.md b/linux/docs/drivers/mysql.md index 7993c32035..3fbc382f07 100644 --- a/linux/docs/drivers/mysql.md +++ b/linux/docs/drivers/mysql.md @@ -100,7 +100,7 @@ A call that timed out or lost its connection is checked before the link is decla `ReadOnlyEnforcement::SessionDefault`: `SET SESSION TRANSACTION READ ONLY` on every session. Saves are refused before anything is sent, and a write through generated SQL fails with 1792. At Safe Mode Read-Only the app refuses every editor statement that does not read before it is sent, `SET TRANSACTION READ WRITE` included; a function a `SELECT` calls is the server's to refuse. For a guarantee, connect with a login that has no write privileges. -An export, on any connection, runs in `START TRANSACTION READ ONLY` and refuses user SQL that Safe Mode would not count as a read (`tablepro_core::sql_syntax::statement_access`). The text protocol runs every statement a text holds, so user SQL is first prepared, which the server parses with multiple statements off: a text it reads as two fails there, and one it parses but does not prepare (1295, such as some `SHOW`) still runs. +An export, on any connection, runs in `START TRANSACTION READ ONLY` and refuses user SQL that Safe Mode would not count as a read (`tablepro_core::sql_syntax::statement_access`). The text protocol runs every statement a text holds, so user SQL is first prepared, which the server parses with multiple statements off: a text it reads as two fails there, and one it parses but does not prepare (1295, such as some `SHOW`) still runs. A read that finds no rows takes its columns from that prepare, since sqlx gives an empty set as its OK alone, so the export still has its header. Safe Mode reads text every way the client or a server may read it, and the strictest reading stands (`tablepro_core::sql_syntax::script::ScriptPlan::server_readings`): @@ -161,7 +161,8 @@ A definition is the server's `SHOW CREATE`, which names neither the object's dat - The editor sends one statement per run. Text with several statements is split where the mysql client splits it, at each `;` or at the delimiter a `DELIMITER` line sets, and goes one statement per round trip. Sent whole, the server starts each next statement as soon as the last one's result is written, read or not, so a stop could not keep it from running. A `/*!` or `/*M!` comment is code to the split, as it is to the mysql client, so each `/*!40101 ... */;` line of a dump is a statement of its own and a `/*!50100 PARTITION BY ... */` stays with its table. - Text without `DELIMITER` lines goes as written, as the server's own batch, when it holds one statement, or when only the server can tell where its statements end: a statement that does not open with a word known to start one with no body, such as a stored program or a compound statement, a `/*!` comment, or text the session's `sql_mode` reads its own way (a backslash in a string, `[name]`, `$$` quotes). -- The row limit is `SET SESSION sql_select_limit = `, sent when the limit changes. The server stops a plain `SELECT` there, and the set ends with `Truncation::ServerStopped` and no total. A `SELECT` with a `LIMIT` of its own, or one inside a routine, is read to its end: the rows past the limit are counted, never decoded, and the set ends with `Truncation::ReadPastLimit` and its total. A set that ends exactly one row past the limit reads as `ServerStopped` either way. +- The row limit is `SET SESSION sql_select_limit = `. Both servers cut every top-level `SELECT` without a `LIMIT` there, one that writes `INTO OUTFILE` or locks `FOR UPDATE`, `FOR SHARE` or `LOCK IN SHARE MODE` too. So a run that holds such a statement, or an `EXECUTE`, runs under `SET SESSION sql_select_limit = DEFAULT`; a run whose statements opening with `SELECT`, `WITH`, `TABLE` or `VALUES` are all plain reads (`statement_access` reads them as reads) runs under the limit; and a run with neither sends no `SET` and keeps what the session holds. Each statement of a text sent as the server's own batch counts, read with backslash escapes both on and off, since the session's `sql_mode` decides where its strings end. The `SET` goes once, before the run's first statement, and only when the value changes or the last one failed, since one between two statements would reset `ROW_COUNT()` and, on MySQL, the warnings. A user's own `SET sql_select_limit` therefore holds for the rest of its run. The server stops a plain read at the limit, and the set ends with `Truncation::ServerStopped` and no total. A `SELECT` with a `LIMIT` of its own, a read in a run whose limit was lifted, or one inside a routine is read to its end: the rows past the limit are counted, never decoded, and the set ends with `Truncation::ReadPastLimit` and its total. A set that ends exactly one row past the limit reads as `ServerStopped` either way. +- sqlx gives a set with no rows as its closing OK alone, without its columns. A plain read answered by an OK alone is prepared once it has run, and the prepare's columns make the answer an empty set, traced like any other. The prepare replaces the read's warnings, so a `SHOW WARNINGS` after an empty read shows none. Any other statement answered by an OK alone stays a command and is not prepared: `SELECT ... INTO`, a locking read, `SHOW`, `DESC`, `EXPLAIN`, and a plain read the server will not prepare or that sits in a text the server runs as several statements. - A stop sends `KILL QUERY` on the session's own control connection and gives the statement the cancel grace to end, and nothing after it is sent, whatever the `KILL` did. A statement that has not ended by then ends the session, because its answer is still on the way, and so does a `KILL` that has not answered, because it may still land on whatever runs next. The row limit's `SET` is watched the same way. - A run stopped at its deadline reads as `TimedOut { server_cancelled: true }` only when the statement ended with 1317 or the server's own timeout. A bare `SELECT SLEEP(n)` the `KILL` stopped returns 1 with no error, so it reads as `false`. - When the grid stops reading, the run sends the same `KILL QUERY` and ends as cancelled. The statement already sent is read to its end, unshown, and nothing after it is sent. diff --git a/windows/docs/decisions/0004-app-shell.md b/windows/docs/decisions/0004-app-shell.md index d98cc64c58..26d8a47ef2 100644 --- a/windows/docs/decisions/0004-app-shell.md +++ b/windows/docs/decisions/0004-app-shell.md @@ -41,7 +41,7 @@ On 2026-11-05 the maintainer had the app show what the server prints, PostgreSQL - A tab keeps the session it was opened on wherever it goes: each workspace and each tab holds the session (`SessionShare`), and the last to let go disposes it. A tab therefore joins any workspace of its connection, in any window, after a relaunch too. - A tab is dropped only into a workspace of its own connection, never into a window kept hidden for a drag, and never from the primary window when it is that window's last tab: TabView drags the whole window when its strip holds one tab, and hides and closes the dragged window once its tab joins another strip, which for the primary would end the app. - A connected primary workspace keeps a fresh query tab when its last tab is closed or moved out. Native `TabView::GetTabInsertionIndex` rejects an empty strip, so the remaining query keeps a drop target for returning tabs. -- Closing a tab window's last tab closes the window. A window whose last tab was dragged into another strip is closed by TabView. Closing the primary window saves every window and closes them all, since the rail, Settings and the connection form live there. +- Closing a tab window's last tab closes the window. A window whose last tab was dragged into another strip is closed by TabView. Closing the primary window saves every window and closes them all, since the rail, Settings and the connection form live there. Closing a tab window puts its tabs in the primary's closed tabs for the same connection, so Reopen closed tab brings them back. - Each tab window keeps its own tabs and its place: `session.json` lists them with their `GetWindowPlacement` bounds, restored as the primary's are, and they reopen with the session. - TabView raises `TabTearOutWindowRequested` on every press on a tab, before any drag, and shows and hides that window at once (microsoft/microsoft-ui-xaml#10154, #10155). So the primary keeps one such window per session, hidden, cloaked (`DWMWA_CLOAK`) and out of the task switchers until a tab arrives, and cancels the close TabView sends it after a press that tore nothing out. - The window supplied at `TabTearOutWindowRequested` is retained for `TabTearOutRequested`, as in the native sample. WinUI 2.6.4 never assigns the latter event's `NewWindowId`. An external drop carries the source window ID into the receiving workspace so the tab can leave it again within the same drag. @@ -69,7 +69,7 @@ On 2026-11-05 the maintainer had the app show what the server prints, PostgreSQL | Ctrl+T, Ctrl+W, Ctrl+Shift+T | New query tab, close the tab, reopen the last closed tab; File also has Move tab to new window | | Ctrl+Tab, Ctrl+Shift+Tab, Ctrl+F4 | TabView's own | | Ctrl+1 to Ctrl+8, Ctrl+9 | That tab, the last tab ([Tab view](https://learn.microsoft.com/en-us/windows/apps/develop/ui/controls/tab-view)) | -| Ctrl+Enter, Shift+F5 | Run the selection or the whole text, stop | +| Ctrl+Enter, Ctrl+Shift+Enter, Shift+F5 | Run the selection or the whole text, run the statement at the cursor, stop | | F5 | Refresh the object tree | | Ctrl+B, Ctrl+P | Show or hide the object tree, filter objects | | Ctrl+Shift+H | The Connections page | @@ -112,7 +112,8 @@ On 2026-11-05 the maintainer had the app show what the server prints, PostgreSQL - TreeView is not virtualized, so a folder realizes its first 500 objects, then "Show all N", which opens the whole folder in the virtualized `ListView`, and "Type in the filter box to find the rest". Typing in the filter switches to the same list of matches across schemas, names that start with the text first. - Invoking a table or view opens its table tab, or brings the open one forward. The core writes the statement (`DatabaseSession.GetTableQueryAsync`, over the core's `build_page`), quoted, ordered and limited as the engine writes it. - A query tab is the SQL editor over the run's results; a table tab is the results alone. Each opens its own editor connection on first use. -- Results: the run's first result set in `DataGridView` (ADR 0003), given to the grid once it has its first rows, or once it ends empty, and grown on each `RowsAppended`. The rows shown stay until then, so a sort or a refresh never shows an empty grid between the two results. Columns are sized to their content, and a sort or a refresh keeps their widths. Copy and Select All in the Edit menu reach the grid when it had focus last. +- Run statement (Query menu, Ctrl+Shift+Enter, as on Linux): the core picks the statement the cursor is in, or the last one before it, with the session's own grammar (`DatabaseSession.statement_at`, over `ScriptPlan::statement_at`, in UTF-16 offsets). The editor selects it, so the tab shows which statement ran, and Safe Mode judges that text alone. +- Results: the run's first result set in `DataGridView` (ADR 0003), given to the grid once it has its first rows, or once it ends empty, and grown on each `RowsAppended`. The rows shown stay until then, so a sort or a refresh never shows an empty grid between the two results. A run with more than one set shows a `ComboBox` over the grid, "Result 1 (3)", "Result 2 (50)", as the Linux app labels them, with the count the engine gave or a plus when the row limit cut a set short; picking one shows its rows in arrival order and its count in the status. The pane holds every set of the run until the next run's first set replaces them. Columns are sized to their content, and a sort or a refresh keeps their widths. Copy and Select All in the Edit menu reach the grid when it had focus last. - Messages: a run the server printed something in gets a `SelectorBar`, Results | Messages, over its results. Messages is a read-only monospace `TextBox` with one line per notice, `{severity}: {message}`, as the Linux app prints the same core's notices, set once per batch of events rather than per notice. A run with no rows opens on Messages, one with rows on Results unless Messages was picked while it ran. Past the 10,000 notices the core keeps per run (`NoticeLimitReached`), the text ends with "The output was cut short." - A header click on a table tab runs the table's query again with the sort: `build_page` writes the ORDER BY, with the row key as the tiebreak, so it orders the whole table, not the rows already read. A row address the engine adds for edits (SQLite's rowid, PostgreSQL's ctid) follows the visible columns and is not shown. An engine that pages in its own order refuses the sort, and the grid keeps the shown one. - A header click on a query result does what the macOS app does: the result's statement runs again with the column's ORDER BY in place of its own and before its row limit (`DatabaseSession.SortedQuery`, the core's `with_order_by`). The ORDER BY names the column, or gives its position where a name is not unique or not a plain column (core ADR 0007). Only one statement that reads runs again. For a script, a write, SHOW or EXPLAIN, and where the engine takes no position, the core sorts the rows the result holds (`ResultSet.SortedAsync`), as macOS sorts them where they are, and the status says so. @@ -121,8 +122,8 @@ On 2026-11-05 the maintainer had the app show what the server prints, PostgreSQL - Table tabs page as macOS pages a table: a page is the default page size long (Settings, Pagination: 100, 500, 1,000, 5,000 or 10,000 rows, 1,000 unless changed, as macOS); First, Previous, Next and Last sit in the tab's `CommandBar`, and its one status words the page as the macOS status bar does, with the run's time: "1,001-2,000 of 3,503 rows · 15 ms", or "Rows 1,001-2,000 · 15 ms" until the count lands. A table that fits one page keeps "Rows: 347 · 12 ms". The core writes each page (`table_query` takes an offset, over `build_page`'s `PagePosition::Offset`) and counts the table (`table_row_count`, `build_count` as the Linux pager counts), in the background after the first full page; until the count lands, Next stays while pages come back full. A sort or a refresh goes back to the first page, and a refresh counts again. An engine that pages on the server (Cassandra) cannot seek by offset, so it has only its first page, as on macOS, and the core refuses another (`ffi.page_unsupported`). - Rows per page: a "1,000 rows" button before First page opens the macOS sizes, 5, 10, 20, 100, 500 and 1,000, as `RadioMenuFlyoutItem`s, then All rows..., which asks first and needs the count, and Custom..., up to 1,000,000. A new size keeps the page that holds the first row shown, as macOS does. The size belongs to the tab: `session.json` keeps it with the tab's page, and Reopen closed tab brings both back. - Go to page: between Previous and Next, "Page 3 of 36" opens a flyout with a `NumberBox` from 1 to the page count, as macOS asks in a popover. It is offered once the table is counted, as on macOS, and the box keeps a typed number inside that range (`InvalidInputOverwritten`). -- An open transaction: each run's `Finished` event carries the transaction the editor's connection is left in (the core's `RunSummary.transaction_state`). The tab that holds one shows a Warning `InfoBar` above its results, "Transaction open", with Roll back, which asks first, and Commit, hidden on a read-only connection; a failed one is an Error bar with Roll back only. Both send COMMIT or ROLLBACK as typed SQL, EXEC or DISCARD for a key-value engine, as the Linux app does. Over the tabs, a bar names another tab that holds one, with Show; macOS shows its bar above every tab of the connection, and here the owning tab carries the actions, since the transaction lives on that tab's own connection. Closing a tab that holds one asks first, since closing rolls it back. Window close and Exit confirm every affected held transaction before closing any tabs; cancelling keeps them open. Disconnect, Reconnect and connecting an edited connection ask the same way first, and Reconnect and an edited connection bring the tabs back with their text on the new session. Cancelling that connect keeps the connection with Retry, so the tabs still come back. A window with a dialog already open waits for it to be dismissed before accepting a close request. WinUI opens one `ContentDialog` per thread, even across windows, so a question asked while another window shows one is declined, as Cancel would decline it (`WindowDialogs`). -- Safe Mode: before a typed run the core judges each statement at the connection's level (`DatabaseSession.gate_run`, over `SafeModeLevel::gate`, as the Linux editor judges its batches). Confirm writes asks before any write, and Off before a DROP, a TRUNCATE, or a DELETE or UPDATE with no WHERE. The question is a `ContentDialog` that lists each statement in full, with Cancel as the default. Read only keeps the whole run back, since the editor sends its text whole. The driver splits that text with the session's own settings, so when the whole text reads stricter than every statement, the question shows the whole text. A MySQL script with DELIMITER lines is judged statement by statement, since the driver sends its statements apart. A level saved while the connection is open applies at once to its sessions in every window (`set_safe_mode`), as on Linux; a change to or from Read only takes a new connection, so those sessions reconnect once their held transactions are confirmed. A session that keeps its connection shows "Changes apply the next time you connect", as Linux says it, and one that cannot take Read only asks before every write until then. Statements are judged by the connection's own engine, also while its link is down and a tab's own connection still runs (`Session::grammar`). Commit, Roll back and a sort's rerun are not asked about. Closing a tab stops its run and waits for it to end before its connection closes. +- An open transaction: each run's `Finished` event carries the transaction the editor's connection is left in (the core's `RunSummary.transaction_state`). The tab that holds one shows a Warning `InfoBar` above its results, "Transaction open", with Roll back, which asks first, and Commit, hidden on a read-only connection; a failed one is an Error bar with Roll back only. Both send COMMIT or ROLLBACK as typed SQL, EXEC or DISCARD for a key-value engine, as the Linux app does. Over the tabs, a bar names another tab that holds one, with Show; macOS shows its bar above every tab of the connection, and here the owning tab carries the actions, since the transaction lives on that tab's own connection. Closing a tab that holds one asks first, since closing rolls it back. Window close and Exit confirm every affected held transaction before closing any tabs; cancelling keeps them open. Then one dialog lists every query tab still running, as macOS asks before it quits, and says closing rolls back a transaction a run has open; a table tab loading a page is not asked about. Exit then hides the windows and closes every connection, each tab stopping its run, for up to 5 seconds before the process ends. Disconnect, Reconnect and connecting an edited connection ask about held transactions the same way first, in the primary and in a tab window, but not about a running query, which they stop, and Reconnect and an edited connection bring the tabs back with their text on the new session. Cancelling that connect keeps the connection with Retry, so the tabs still come back. A window with a dialog already open waits for it to be dismissed before accepting a close request. WinUI opens one `ContentDialog` per thread, even across windows, so a question asked while another window shows one is declined, as Cancel would decline it (`WindowDialogs`). +- Safe Mode: before a typed run the core judges each statement at the connection's level (`DatabaseSession.gate_run`, over `SafeModeLevel::gate`, as the Linux editor judges its batches). Confirm writes asks before any write, and Off before a DROP, a TRUNCATE, or a DELETE or UPDATE with no WHERE. The question is a `ContentDialog` that lists each statement in full, with Cancel as the default. Read only keeps the whole run back, since the editor sends the text it runs whole: the selection, the whole text, or with Run statement the one statement. The driver splits that text with the session's own settings, so when the whole text reads stricter than every statement, the question shows the whole text. A MySQL script with DELIMITER lines is judged statement by statement, since the driver sends its statements apart. A level saved while the connection is open applies at once to its sessions in every window (`set_safe_mode`), as on Linux; a change to or from Read only takes a new connection, so those sessions reconnect once their held transactions are confirmed. A session that keeps its connection shows "Changes apply the next time you connect", as Linux says it, and one that cannot take Read only asks before every write until then. Statements are judged by the connection's own engine, also while its link is down and a tab's own connection still runs (`Session::grammar`). Commit, Roll back and a sort's rerun are not asked about. Closing a tab stops its run and waits for it to end before its connection closes. - Reconnecting: the workspace waits on the core's session health (`health_change`) and shows a bar over the tabs while it is not online: Warning while it reconnects, with the attempt, or while the server cannot be reached, and Error once the session parks with the reason the core gives, with Reconnect (`reconnect`, the monitor's retry). An editor connection is never opened again by itself, since its transaction would not come back with it: a query tab whose connection was lost opens a new one on its next run and says that an open transaction went with it. ### Settings @@ -135,12 +136,12 @@ A page in the window, built from `SettingsCard` and `SettingsExpander`: Appearan |---|---|---| | Saved connections, groups, favourites | `connections.json` through the core, in the data folder | Core ADR 0006; the core's `StoragePaths` roots all point at the data folder | | Passwords and passphrases | Credential Manager, written by the core | Generic credentials named `TablePro/connection//`, persisted for the user on this PC, the core's label in the comment | -| Settings | `settings.json` in the data folder | No shared settings spec: Linux keeps its preferences in GSettings | -| Session, window placement, object pane width, tab windows | `session.json` in the data folder, written as the primary window closes | No shared spec: Linux keeps its workspace store in the app | +| Settings | `settings.json` in the data folder; a write another program blocks is tried again at the next change and on exit | No shared settings spec: Linux keeps its preferences in GSettings | +| Session, window placement, object pane width, tab windows | `session.json` in the data folder, written 2 seconds after a change (a write the file refuses is tried again, up to 30 seconds apart), as the primary window closes, when Windows ends the session (`WM_ENDSESSION`) and before an exception nothing caught ends the app | No shared spec: Linux keeps its workspace store in the app | The data folder is the package's `ApplicationData.Current.LocalFolder` (`%LOCALAPPDATA%\Packages\\LocalState`), which Windows removes with the app ([Store and retrieve settings and other app data](https://learn.microsoft.com/en-us/windows/apps/design/app-settings/store-and-retrieve-app-data)). An unpackaged development build uses `%LOCALAPPDATA%\TablePro`, and `TABLEPRO_DATA_DIR` overrides both for the UI tests. The session holds each open connection (a saved one by id, a file by path), its tabs (a query tab's text, a table tab's table with its page size and page), the selected tab, the front connection, and each tab window with its connection, its tabs and its bounds. -One process owns a data folder. `Program.Main` registers it with `AppInstance.FindOrRegisterForKey` under a hash of its path (`InstanceKey`) before XAML starts, so a second launch on the same folder redirects its activation to the running app and ends, and the running app brings its primary window to the front ([Create a single-instanced app](https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/applifecycle/applifecycle-single-instance)). `RedirectActivationToAsync` waits for the owner without a limit, so the launch also stops waiting when the owner's process ends or after 5 seconds, then looks again or starts its own window (`LaunchClaim`); a launch always ends with a window. The primary window gives the key up (`UnregisterKey`) when it closes, after the session is saved, and a failure anywhere in the guard is written to `crash.log` and the app starts as usual. Two processes would otherwise each write their copy of `session.json` and `settings.json` over the other's. The core also locks `connections.json` for each write and reads it again first, which covers a writer outside the app. +One process owns a data folder. `Program.Main` registers it with `AppInstance.FindOrRegisterForKey` under a hash of its path (`InstanceKey`) before XAML starts, so a second launch on the same folder redirects its activation to the running app and ends, and the running app brings its primary window to the front ([Create a single-instanced app](https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/applifecycle/applifecycle-single-instance)). `RedirectActivationToAsync` waits for the owner without a limit, so the launch also stops waiting when the owner's process ends or after 5 seconds, then looks again or starts its own window (`LaunchClaim`); a launch always ends with a window. The primary window gives the key up (`UnregisterKey`) once the last session is saved, before its connections close, and a failure anywhere in the guard is written to `crash.log` and the app starts as usual. Two processes would otherwise each write their copy of `session.json` and `settings.json` over the other's. The core also locks `connections.json` for each write and reads it again first, which covers a writer outside the app. ### Strings and accessibility diff --git a/windows/src/TablePro.App.Model/AppSettings.cs b/windows/src/TablePro.App.Model/AppSettings.cs index 8546ed69a3..576f20a721 100644 --- a/windows/src/TablePro.App.Model/AppSettings.cs +++ b/windows/src/TablePro.App.Model/AppSettings.cs @@ -98,6 +98,8 @@ public static void Write(string path, T value, System.Text.Json.Serialization using (var stream = File.Create(temporary)) { JsonSerializer.Serialize(stream, value, type); + // On disk before the rename is, so a power cut after the move cannot leave a file of zeros. + stream.Flush(flushToDisk: true); } File.Move(temporary, path, overwrite: true); diff --git a/windows/src/TablePro.App.Model/RunResults.cs b/windows/src/TablePro.App.Model/RunResults.cs new file mode 100644 index 0000000000..34e5046a47 --- /dev/null +++ b/windows/src/TablePro.App.Model/RunResults.cs @@ -0,0 +1,69 @@ +using System.Globalization; +using TablePro.Interop; + +namespace TablePro.App.Model; + +public sealed class RunResult(int setIndex, IReadOnlyList columns) +{ + public int SetIndex { get; } = setIndex; + + public IReadOnlyList Columns { get; } = columns; + + public long Rows { get; internal set; } + + // Every row the statement produced, where the engine counted past the limit. + public long? TotalRows { get; internal set; } + + public bool Truncated { get; internal set; } +} + +// Each result set of a run in the order it started, labelled "Result {n} ({rows})" as the Linux app lists them. +public sealed class RunResults(string label, string labelMore) +{ + private readonly List _results = []; + + public IReadOnlyList Results => _results; + + public RunResult Add(int setIndex, IReadOnlyList columns) + { + var result = new RunResult(setIndex, columns); + _results.Add(result); + return result; + } + + // The listed set the event changed, or null. + public RunResult? Apply(QueryEvent queryEvent) + { + switch (queryEvent) + { + case QueryEvent.RowsAppended appended when Find(appended.SetIndex) is { } growing: + growing.Rows = appended.RowCount; + return growing; + case QueryEvent.RowLimitReached limit when Find(limit.SetIndex) is { } cut: + cut.Truncated = true; + return cut; + case QueryEvent.ResultSetFinished finished when Find(finished.SetIndex) is { } ended: + ended.Rows = finished.RowCount; + ended.TotalRows = finished.TotalRows; + ended.Truncated |= finished.Truncation != Truncation.Complete; + return ended; + default: + return null; + } + } + + // A set the limit cut short shows the total the engine counted, or its rows with a plus when nobody counted. + public string Label(RunResult result) + { + ArgumentNullException.ThrowIfNull(result); + var position = _results.IndexOf(result) + 1; + return result switch + { + { Truncated: false } => string.Format(CultureInfo.CurrentCulture, label, position, result.Rows), + { TotalRows: { } total } => string.Format(CultureInfo.CurrentCulture, label, position, total), + _ => string.Format(CultureInfo.CurrentCulture, labelMore, position, result.Rows), + }; + } + + private RunResult? Find(int setIndex) => _results.Find(result => result.SetIndex == setIndex); +} diff --git a/windows/src/TablePro.App.Model/SessionWriter.cs b/windows/src/TablePro.App.Model/SessionWriter.cs new file mode 100644 index 0000000000..7d2068dbe2 --- /dev/null +++ b/windows/src/TablePro.App.Model/SessionWriter.cs @@ -0,0 +1,75 @@ +namespace TablePro.App.Model; + +public enum SessionSave +{ + Written, + + // A newer save came first, or the last save was already written. + Skipped, + + // The file refused it, as when another program holds it; the one before stays on disk. + Refused, +} + +// Saves are written one at a time and never over a newer one, so a save from the pool cannot undo a later one. +public sealed class SessionWriter(SessionStore store) +{ + // Taking a turn never waits on a write, so the UI thread that takes one is never held by a slow disk. + private readonly Lock _turns = new(); + private readonly Lock _writes = new(); + private long _taken; + private bool _ended; + private long _attempted; + + // On the pool, so a large session never stalls the window that asked. + public Task SaveInBackgroundAsync(SessionState state) + { + ArgumentNullException.ThrowIfNull(state); + var turn = Take(end: false); + return turn == 0 ? Task.FromResult(SessionSave.Skipped) : Task.Run(() => Write(turn, state)); + } + + // On this thread. The last save, as the app closes or its session ends, also refuses every save after it. + public SessionSave Save(SessionState state, bool last = false) + { + ArgumentNullException.ThrowIfNull(state); + var turn = Take(last); + return turn == 0 ? SessionSave.Skipped : Write(turn, state); + } + + private long Take(bool end) + { + lock (_turns) + { + if (_ended) + { + return 0; + } + + _ended = end; + return ++_taken; + } + } + + private SessionSave Write(long turn, SessionState state) + { + lock (_writes) + { + if (turn <= _attempted) + { + return SessionSave.Skipped; + } + + _attempted = turn; + try + { + store.Save(state); + return SessionSave.Written; + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + return SessionSave.Refused; + } + } + } +} diff --git a/windows/src/TablePro.App/App.xaml.cs b/windows/src/TablePro.App/App.xaml.cs index a5f524762e..98b25a93a8 100644 --- a/windows/src/TablePro.App/App.xaml.cs +++ b/windows/src/TablePro.App/App.xaml.cs @@ -13,8 +13,15 @@ public partial class App : Application public App() { InitializeComponent(); - UnhandledException += (_, args) => CrashLog.Write(args.Exception); + // Never marked handled: nothing here can tell whether the app is still sound, so it ends as WinUI ends it. + UnhandledException += (_, args) => + { + CrashLog.Write(args.Exception); + _window?.SaveSessionBeforeCrash(); + }; AppDomain.CurrentDomain.UnhandledException += (_, args) => CrashLog.Write(args.ExceptionObject as Exception); + // A faulted task no one awaited ends nothing, so it is only written down. + TaskScheduler.UnobservedTaskException += (_, args) => CrashLog.Write(args.Exception); } protected override void OnLaunched(LaunchActivatedEventArgs args) @@ -23,6 +30,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) Directory.CreateDirectory(folder); var core = TableProCore.CreateWithSystemSecrets(new HostConfiguration(AppStorage.Version, folder)); _window = new MainWindow(new Workbench(core, folder)); + _window.SessionSaved += (_, _) => ReleaseFolder(); _window.Closed += (_, _) => ReleaseFolder(); _window.ActivateRestored(); // Off the first grid's path: the fallback fonts for non-Latin cells load in the background. @@ -49,6 +57,11 @@ internal void BringToFront() // The session is saved by now, so a launch from here on starts its own window instead of handing over to this one. private void ReleaseFolder() { + if (_closed) + { + return; + } + _closed = true; try { diff --git a/windows/src/TablePro.App/MainWindow.xaml b/windows/src/TablePro.App/MainWindow.xaml index 881a1b7d4e..24584493a7 100644 --- a/windows/src/TablePro.App/MainWindow.xaml +++ b/windows/src/TablePro.App/MainWindow.xaml @@ -131,6 +131,11 @@ + + + + + diff --git a/windows/src/TablePro.App/MainWindow.xaml.cs b/windows/src/TablePro.App/MainWindow.xaml.cs index d5d233fbb9..decd65926c 100644 --- a/windows/src/TablePro.App/MainWindow.xaml.cs +++ b/windows/src/TablePro.App/MainWindow.xaml.cs @@ -1,7 +1,9 @@ using Microsoft.UI; using Microsoft.UI.Composition.SystemBackdrops; +using Microsoft.UI.Dispatching; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Automation; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; using Microsoft.Windows.Storage.Pickers; @@ -19,6 +21,15 @@ namespace TablePro.App; // shell's own commands (the Connections page, Settings, the connection form) back to the primary. public sealed partial class MainWindow : Window, IShell, IWorkspaceHost { + // A change is on disk this long after it, however fast the typing goes on. + private static readonly TimeSpan SaveDelay = TimeSpan.FromSeconds(2); + + // A session file another program holds is tried again, each time later, until this far apart. + private static readonly TimeSpan LongestRetry = TimeSpan.FromSeconds(30); + + // A server that does not answer holds the exit only this long; the process ends what is left. + private static readonly TimeSpan CloseLimit = TimeSpan.FromSeconds(5); + private readonly Workbench _workbench; private readonly MainWindow _primary; private readonly HomeView? _home; @@ -29,6 +40,11 @@ public sealed partial class MainWindow : Window, IShell, IWorkspaceHost private readonly OverlappedPresenter _presenter = OverlappedPresenter.Create(); private readonly bool _restoreMaximized; private readonly IReadOnlyList _restoreWindows = []; + + // The primary's alone: it writes the session for every window. + private readonly SessionWriter? _saves; + private readonly DispatcherQueueTimer? _saveTimer; + private TimeSpan _nextRetry = SaveDelay; private SettingsView? _settings; private OpenConnection? _front; @@ -45,6 +61,18 @@ internal MainWindow(Workbench workbench) InitializeComponent(); Prepare(); Title = "TablePro"; + _saves = new SessionWriter(workbench.Sessions); + _saveTimer = DispatcherQueue.CreateTimer(); + _saveTimer.Interval = SaveDelay; + _saveTimer.IsRepeating = false; + _saveTimer.Tick += async (timer, _) => + { + timer.Stop(); + timer.Interval = SaveDelay; + await SaveSessionInBackgroundAsync(); + }; + var sessionEnd = new SessionEnd(this, OnSessionEnding); + Closed += (_, _) => sessionEnd.Dispose(); // Where the window stood last time, whether or not its connections come back. var session = workbench.Sessions.Load(); @@ -92,6 +120,9 @@ private MainWindow( Show(connection); } + // Raised once the last session is written, before the connections close, so a new launch may own the data folder. + internal event EventHandler? SessionSaved; + public bool IsPrimary => ReferenceEquals(_primary, this); public bool CanMoveTabs => !_primary._checkingClose && !_closing; @@ -437,6 +468,7 @@ private async Task RefreshMarksAsync() if (saved.Name != open.Name) { open.Rename(saved.Name); + SessionChanged(); if (ReferenceEquals(_front, open)) { ShowName(open); @@ -458,9 +490,11 @@ private OpenConnection Add(OpenConnection open) open.StateChanged += (_, _) => UpdateCommands(); open.Cancelled += async (_, _) => await CloseConnectionAsync(open); open.View.CommandsChanged += (_, _) => UpdateCommands(); + open.View.SessionChanged += (_, _) => SessionChanged(); open.View.EditRequested += async (_, _) => await EditAsync(open); open.View.Host = this; _open.Add(open); + SessionChanged(); if (IsPrimary) { var index = Rail.MenuItems.IndexOf(RailSeparator) + _open.Count; @@ -500,6 +534,7 @@ private void Show(OpenConnection open) } UpdateCommands(); + SessionChanged(); } private void Show(UIElement page) @@ -567,11 +602,12 @@ private void UpdateRail() } } + // Disconnect asks about held transactions in either window, and about a running query in neither, as Reconnect. private async Task CloseConnectionAsync(OpenConnection open) { if (!IsPrimary) { - await RequestCloseAsync(); + await RequestCloseAsync(askAboutRunning: false); return; } @@ -595,6 +631,7 @@ private async Task CloseConnectionAsync(OpenConnection open) _front = null; } + SessionChanged(); UpdateRail(); if (wasShown) { @@ -803,7 +840,7 @@ private void OnClosing(AppWindow sender, AppWindowClosingEventArgs args) } } - private async Task RequestCloseAsync() + private async Task RequestCloseAsync(bool askAboutRunning = true) { if (_primary._checkingClose || _closing) { @@ -821,33 +858,37 @@ private async Task RequestCloseAsync() try { var confirmed = new HashSet(); - while (windows.SelectMany(window => window._open).Any(open => open.View.HasUnconfirmedTransactions(confirmed))) + // Apart from confirmed, so a run that ends during the question and leaves a transaction open is asked about. + var stopped = new HashSet(); + while (true) { - foreach (var window in windows) + if (!await ConfirmTransactionsAsync(windows, confirmed)) { - foreach (var open in window._open.Where(open => open.View.HasUnconfirmedTransactions(confirmed)).ToList()) - { - window.Activate(); - if (window.IsPrimary) - { - window.Select(open); - } - else - { - window.Show(open); - } + return; + } - if (!await open.View.ConfirmCloseAsync(confirmed)) - { - return; - } - } + // A tab whose transaction was confirmed is not asked again: that question says its work goes with it. + var running = askAboutRunning ? RunningQueries(windows, confirmed, stopped) : []; + if (running.Count == 0) + { + break; } + + if (!await ConfirmStopRunningAsync(running)) + { + return; + } + + stopped.UnionWith(running.Select(each => each.Tab)); } if (IsPrimary) { - ShutDown(); + await ShutDownAsync(); + } + else + { + HandTabsToPrimary(); } CloseForGood(); @@ -859,6 +900,87 @@ private async Task RequestCloseAsync() } } + private static async Task ConfirmTransactionsAsync(List windows, HashSet confirmed) + { + while (windows.SelectMany(window => window._open).Any(open => open.View.HasUnconfirmedTransactions(confirmed))) + { + foreach (var window in windows) + { + foreach (var open in window._open.Where(open => open.View.HasUnconfirmedTransactions(confirmed)).ToList()) + { + window.Activate(); + if (window.IsPrimary) + { + window.Select(open); + } + else + { + window.Show(open); + } + + if (!await open.View.ConfirmCloseAsync(confirmed)) + { + return false; + } + } + } + } + + return true; + } + + private static List<(string Connection, ITabContent Tab)> RunningQueries( + List windows, HashSet confirmed, HashSet stopped) => + [.. windows.SelectMany(window => window._open) + .SelectMany(open => open.View.RunningQueries() + .Where(tab => !confirmed.Contains(tab) && !stopped.Contains(tab)) + .Select(tab => (Connection: open.Name, Tab: tab)))]; + + // One question for every query still running, as the macOS app asks before it quits; Cancel keeps every window. + private async Task ConfirmStopRunningAsync(List<(string Connection, ITabContent Tab)> running) + { + Activate(); + var one = running.Count == 1; + var content = new StackPanel { Spacing = 12 }; + content.Children.Add(new TextBlock + { + Text = Strings.Get(one ? "CloseRunningRollsBackOne" : "CloseRunningRollsBackMany"), + TextWrapping = TextWrapping.Wrap, + }); + var tabs = new TextBlock + { + Text = string.Join("\n", running.Select(each => Strings.Format("CloseRunningTab", each.Tab.Title, each.Connection))), + TextWrapping = TextWrapping.Wrap, + }; + AutomationProperties.SetAutomationId(tabs, "CloseRunningTabs"); + content.Children.Add(tabs); + var dialog = new ContentDialog + { + XamlRoot = Root.XamlRoot, + RequestedTheme = Root.ActualTheme, + Title = one ? Strings.Get("CloseRunningTitleOne") : Strings.Format("CloseRunningTitleMany", running.Count), + Content = content, + PrimaryButtonText = Strings.Get("CloseRunningButton"), + CloseButtonText = Strings.Get("Cancel"), + DefaultButton = ContentDialogButton.Close, + }; + return await WindowDialogs.ShowAsync(dialog, Root) == ContentDialogResult.Primary; + } + + // A tab window's tabs close with it, so the primary reopens them as it reopens a tab closed there. + private void HandTabsToPrimary() + { + foreach (var open in _open) + { + var tabs = open.Snapshot().Tabs; + if (tabs.Count > 0 + && _primary._open.FirstOrDefault(each => each.Matches(open.ConnectionId, open.Origin.FilePath)) is { } home) + { + home.View.KeepClosed(tabs); + } + } + } + private void RefreshTabTransfers() { foreach (var window in _primary.AllWindows.Where(window => !window._closing)) @@ -873,7 +995,7 @@ private void RefreshTabTransfers() } // Window.Close raises no AppWindow.Closing, so Exit runs this itself before it closes the primary. - private void ShutDown() + private async Task ShutDownAsync() { if (_closing) { @@ -881,24 +1003,48 @@ private void ShutDown() } _closing = true; + _saveTimer?.Stop(); foreach (var form in _forms.Values.ToList()) { form.Close(); } - SaveSession(); - foreach (var open in _open) + // Before any tab closes, since a closed tab is no longer in what the session lists. + _ = _saves?.Save(SessionSnapshot(), last: true); + _workbench.SaveUnsavedSettings(); + SessionSaved?.Invoke(this, EventArgs.Empty); + DropSpare(); + var windows = AllWindows.ToList(); + foreach (var window in windows) { - open.CancelConnect(); + window.AppWindow.Hide(); } - DropSpare(); + await CloseConnectionsAsync(windows); foreach (var window in _tabWindows.ToList()) { window.CloseForGood(); } } + // Each tab stops its run and closes its own connection, so a server ends them as a disconnect, not a dropped link. + private static async Task CloseConnectionsAsync(List windows) + { + var closing = windows.SelectMany(window => window._open).Select(open => open.DisconnectAsync()).ToList(); + try + { + await Task.WhenAll(closing).WaitAsync(CloseLimit).ConfigureAwait(true); + } + catch (TimeoutException) + { + // The process ends the connections still closing. + } + catch (TableProException) + { + // A connection that could not close cleanly ends with the process. + } + } + private void OnClosed(object sender, WindowEventArgs args) { _workbench.SettingsChanged -= OnSettingsChanged; @@ -914,13 +1060,93 @@ private void OnClosed(object sender, WindowEventArgs args) _primary._spare = null; } + _primary.SessionChanged(); foreach (var open in _open) { _ = open.DisconnectAsync(); } } - private void SaveSession() + // A crash, a kill or a lost power then keeps what the tabs held a moment ago, not what the last exit saved. + private void SessionChanged() + { + if (!IsPrimary) + { + _primary.SessionChanged(); + return; + } + + // Started by the first change and not again until it fires, so steady typing still saves every SaveDelay. + if (!_closing && _saveTimer is { IsRunning: false } timer) + { + timer.Start(); + } + } + + private async Task SaveSessionInBackgroundAsync() + { + if (_closing || _saves is null || _saveTimer is null) + { + return; + } + + var saved = await _saves.SaveInBackgroundAsync(SessionSnapshot()); + if (saved == SessionSave.Written) + { + _nextRetry = SaveDelay; + if (!_saveTimer.IsRunning) + { + _saveTimer.Interval = SaveDelay; + } + } + else if (saved == SessionSave.Refused && !_closing && !_saveTimer.IsRunning) + { + // Without a next change, the text the file refused would live only in memory until the exit. + _saveTimer.Interval = _nextRetry; + _nextRetry = TimeSpan.FromTicks(Math.Min(_nextRetry.Ticks * 2, LongestRetry.Ticks)); + _saveTimer.Start(); + } + } + + // The app ends after an exception nothing caught, so what the tabs hold is written first. + internal void SaveSessionBeforeCrash() + { + if (_closing || _saves is null) + { + return; + } + + try + { + _saveTimer?.Stop(); + _ = _saves.Save(SessionSnapshot(), last: true); + } + catch (Exception exception) + { + CrashLog.Write(exception); + } + } + + // Windows may end the process once this returns, and closes no window first. + private void OnSessionEnding() + { + if (_closing || _saves is null) + { + return; + } + + _saveTimer?.Stop(); + _ = _saves.Save(SessionSnapshot(), last: true); + _workbench.SaveUnsavedSettings(); + // Restart Manager asks the app to end itself; at a sign-out the process may be gone before this runs. + DispatcherQueue.TryEnqueue(async () => + { + await ShutDownAsync(); + CloseForGood(); + }); + } + + private SessionState SessionSnapshot() { var connections = _open.Select(open => open.Snapshot()).ToList(); var front = _front is null ? (connections.Count > 0 ? 0 : -1) : _open.IndexOf(_front); @@ -933,21 +1159,14 @@ private void SaveSession() }) .Where(window => window.Connection.Tabs.Count > 0) .ToList(); - try - { - _workbench.Sessions.Save(new SessionState - { - Connections = connections, - Front = front, - Window = WindowInterop.Placement(this), - ObjectPaneWidth = _workbench.ObjectPaneWidth, - TabWindows = windows, - }); - } - catch (IOException) - { - // A session that cannot be written only means the next launch starts on the Connections page. - } + return new SessionState + { + Connections = connections, + Front = front, + Window = WindowInterop.Placement(this), + ObjectPaneWidth = _workbench.ObjectPaneWidth, + TabWindows = windows, + }; } private void UpdateCommands() @@ -967,6 +1186,7 @@ private void UpdateCommands() MenuReconnect.IsEnabled = front is { State: ConnectionState.Connected or ConnectionState.Failed }; MenuDisconnect.IsEnabled = front is not null; MenuRun.IsEnabled = tab is { IsRunning: false }; + MenuRunStatement.IsEnabled = tab is QueryTabView { IsRunning: false }; MenuStop.IsEnabled = tab is { IsRunning: true }; var editor = tab?.Editor is not null; foreach (var item in new[] { MenuUndo, MenuRedo, MenuCut, MenuPaste, MenuFind }) @@ -1117,6 +1337,8 @@ private async void OnDisconnect(object sender, RoutedEventArgs e) private void OnRun(object sender, RoutedEventArgs e) => Workspace?.ActiveTab?.Run(); + private void OnRunStatement(object sender, RoutedEventArgs e) => (Workspace?.ActiveTab as QueryTabView)?.RunStatement(); + private void OnStop(object sender, RoutedEventArgs e) => Workspace?.ActiveTab?.Stop(); private async void OnDocumentation(object sender, RoutedEventArgs e) => diff --git a/windows/src/TablePro.App/Services/CrashLog.cs b/windows/src/TablePro.App/Services/CrashLog.cs index ffdaaa7401..4f6a5ae87d 100644 --- a/windows/src/TablePro.App/Services/CrashLog.cs +++ b/windows/src/TablePro.App/Services/CrashLog.cs @@ -20,7 +20,7 @@ public static void Write(Exception? exception) var stamp = DateTimeOffset.Now.ToString("O", CultureInfo.InvariantCulture); File.AppendAllText(Path, $"{stamp} {AppStorage.Version}{Environment.NewLine}{exception}{Environment.NewLine}"); } - catch (IOException) + catch (Exception logged) when (logged is IOException or UnauthorizedAccessException) { // Nothing is left to tell when the log itself cannot be written. } diff --git a/windows/src/TablePro.App/Services/SessionEnd.cs b/windows/src/TablePro.App/Services/SessionEnd.cs new file mode 100644 index 0000000000..62865cae65 --- /dev/null +++ b/windows/src/TablePro.App/Services/SessionEnd.cs @@ -0,0 +1,80 @@ +using System.ComponentModel; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Microsoft.UI.Xaml; +using WinRT.Interop; + +namespace TablePro.App.Services; + +// A sign-out, a shutdown or a Windows Update restart ends the process without closing its windows (WM_ENDSESSION). +internal sealed unsafe partial class SessionEnd : IDisposable +{ + private const uint EndSessionMessage = 0x0016; + private const uint NonClientDestroyMessage = 0x0082; + private const nuint SubclassId = 2; + + private readonly IntPtr _window; + private readonly Action _ending; + private GCHandle _handle; + + public SessionEnd(Window window, Action ending) + { + _window = WindowNative.GetWindowHandle(window); + _ending = ending; + _handle = GCHandle.Alloc(this); + if (!SetWindowSubclass(_window, &WindowProcedure, SubclassId, (nuint)GCHandle.ToIntPtr(_handle))) + { + _handle.Free(); + throw new Win32Exception(Marshal.GetLastPInvokeError()); + } + } + + public void Dispose() + { + if (_handle.IsAllocated) + { + _ = RemoveWindowSubclass(_window, &WindowProcedure, SubclassId); + _handle.Free(); + } + + GC.SuppressFinalize(this); + } + + // The session may end as soon as this returns, so the work is done before it does. + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] + private static nint WindowProcedure(nint window, uint message, nuint wParam, nint lParam, nuint id, nuint data) + { + try + { + var end = (SessionEnd)GCHandle.FromIntPtr((nint)data).Target!; + if (message == EndSessionMessage && wParam != 0) + { + end._ending(); + } + + if (message == NonClientDestroyMessage) + { + end.Dispose(); + } + } + catch (Exception exception) + { + CrashLog.Write(exception); + } + + return DefSubclassProc(window, message, wParam, lParam); + } + + [LibraryImport("comctl32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool SetWindowSubclass(nint window, + delegate* unmanaged[Stdcall] procedure, nuint id, nuint data); + + [LibraryImport("comctl32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool RemoveWindowSubclass(nint window, + delegate* unmanaged[Stdcall] procedure, nuint id); + + [LibraryImport("comctl32.dll")] + private static partial nint DefSubclassProc(nint window, uint message, nuint wParam, nint lParam); +} diff --git a/windows/src/TablePro.App/Services/Workbench.cs b/windows/src/TablePro.App/Services/Workbench.cs index 5715d3b89b..d0e212fc3e 100644 --- a/windows/src/TablePro.App/Services/Workbench.cs +++ b/windows/src/TablePro.App/Services/Workbench.cs @@ -6,6 +6,7 @@ namespace TablePro.App.Services; internal sealed class Workbench : IDisposable { private readonly SettingsStore _settings; + private bool _settingsUnsaved; public Workbench(TableProCore core, string folder) { @@ -52,10 +53,32 @@ public void UpdateSettings(Func change) } Settings = next; - _settings.Save(next); + SaveSettings(); SettingsChanged?.Invoke(this, EventArgs.Empty); } + public void SaveUnsavedSettings() + { + if (_settingsUnsaved) + { + SaveSettings(); + } + } + + // A file another program holds, as a virus scan can, keeps the change for this run until a later save lands. + private void SaveSettings() + { + try + { + _settings.Save(Settings); + _settingsUnsaved = false; + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + _settingsUnsaved = true; + } + } + public void NotifyLibraryChanged() => LibraryChanged?.Invoke(this, EventArgs.Empty); // The Chinook sample is copied out of the app once, so edits to it stay. diff --git a/windows/src/TablePro.App/Strings/en-US/Resources.resw b/windows/src/TablePro.App/Strings/en-US/Resources.resw index 5228a24fb6..2113dc86a8 100644 --- a/windows/src/TablePro.App/Strings/en-US/Resources.resw +++ b/windows/src/TablePro.App/Strings/en-US/Resources.resw @@ -195,6 +195,12 @@ R + + Run statement at cursor + + + T + Stop @@ -744,6 +750,9 @@ Rows: {0:N0}, the row limit · {1:N0} ms + + No statement at the cursor + Done · {0:N0} ms @@ -1575,4 +1584,34 @@ New fingerprint: {2} The client certificate or key could not be read. Check both files. + + Result {0:N0} ({1:N0}) + + + Result {0:N0} ({1:N0}+) + + + Result set + + + Stop the running query? + + + Stop {0} running queries? + + + Closing stops it before it finishes, and rolls back any transaction it has open. + + + Closing stops them before they finish, and rolls back any transaction they have open. + + + {0} on {1} + + + Stop and close + + + A client certificate needs both a certificate file and a key file. + diff --git a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs index 09340d9292..b6a1388c03 100644 --- a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs @@ -40,6 +40,7 @@ internal QueryTabView(Workbench workbench, SessionShare share, string title, str AutomationProperties.SetName(SqlText, Strings.Format("EditorName", title)); Results.StatusChanged += (_, status) => StatusText.Text = status; Results.SortRequested += OnSortRequested; + Results.ResultPicked += (_, _) => _sort = GridSort.None; SqlText.GotFocus += (_, _) => _target = EditTarget.Editor; Results.GridView.GotFocus += (_, _) => _target = EditTarget.Grid; Results.MessagesBox.GotFocus += (_, _) => _target = EditTarget.Messages; @@ -64,6 +65,8 @@ internal QueryTabView(Workbench workbench, SessionShare share, string title, str public async void Run() => await RunAsync().ConfigureAwait(true); + public async void RunStatement() => await RunStatementAsync().ConfigureAwait(true); + public void Stop() => _run?.Cancel(); public void FocusContent() => SqlText.Focus(FocusState.Programmatic); @@ -104,6 +107,33 @@ private async Task RunAsync() sql = SqlText.Text; } + await RunTypedAsync(sql).ConfigureAwait(true); + } + + // The statement is selected first so the tab shows which of its statements ran, as the Linux app does. + private async Task RunStatementAsync() + { + if (IsRunning || _confirming || _work.IsClosed) + { + return; + } + + var text = SqlText.Document.Snapshot(); + if (_session.StatementAt(text, SqlText.Model.Primary.Active) is not { } statement) + { + StatusText.Text = Strings.Get("StatusNoStatement"); + return; + } + + var (start, length) = statement.GetOffsetAndLength(text.Length); + SqlText.Model.SetSelection(start + length, start); + SqlText.EnsureCaretVisible(); + await RunTypedAsync(text[statement]).ConfigureAwait(true); + } + + // Safe Mode judges the text that is sent, whichever part of the editor it is. + private async Task RunTypedAsync(string sql) + { if (string.IsNullOrWhiteSpace(sql)) { return; diff --git a/windows/src/TablePro.App/Views/ResultsPane.xaml b/windows/src/TablePro.App/Views/ResultsPane.xaml index 80d942ddb6..d315e8ed22 100644 --- a/windows/src/TablePro.App/Views/ResultsPane.xaml +++ b/windows/src/TablePro.App/Views/ResultsPane.xaml @@ -41,14 +41,31 @@ + + + + + + + + _held = []; + private HeldSet? _picked; + private ResultSet? _sorted; + private long? _milliseconds; + private bool _picking; private ResultsView _view; - private ResultsView? _picked; + private ResultsView? _pickedView; private bool _messagesQueued; public ResultsPane() @@ -33,10 +36,14 @@ public ResultsPane() // The grid already shows the new sort; the tab runs it, or sets Sort back. public event EventHandler? SortRequested; + // The grid shows another set of the run, unsorted. + public event EventHandler? ResultPicked; + public DataGridView GridView => ResultGrid; public TextBox MessagesBox => MessagesText; + // The columns of the set the grid shows. public IReadOnlyList Columns { get; private set; } = []; // Only a run with one result set can be sorted by running its statement again. @@ -55,34 +62,30 @@ public GridSort Sort set => ResultGrid.Sort = value; } - public long ShownRows => _shown?.RowCount ?? 0; + public long ShownRows => (_sorted ?? _picked?.Set)?.RowCount ?? 0; - // False when no set is shown, or a run replaced it while the rows were being sorted. + // False when no set is shown, or a run or a pick replaced it while the rows were being sorted. internal async Task SortHeldRowsAsync(GridSort sort) { - var source = _source; - if (source is null || sort.Column < 0 || sort.Column >= Columns.Count) + var picked = _picked; + if (picked is null || sort.Column < 0 || sort.Column >= Columns.Count) { return false; } - var sorted = await source.SortedAsync(sort.Column, sort.Direction == SortDirection.Ascending) + var sorted = await picked.Set.SortedAsync(sort.Column, sort.Direction == SortDirection.Ascending) .ConfigureAwait(true); - if (!ReferenceEquals(source, _source)) + if (!ReferenceEquals(picked, _picked)) { sorted.Dispose(); return false; } - var previous = _shown; - _shown = sorted; + var previous = _sorted; + _sorted = sorted; ResultGrid.SetData(new ResultSetData(sorted, Columns)); ResultGrid.Sort = sort; - if (!ReferenceEquals(previous, source)) - { - previous?.Dispose(); - } - + previous?.Dispose(); return true; } @@ -104,7 +107,7 @@ public void Clear() SelectView(ResultsView.Results); } - // The rows shown stay until the new set has rows or ends, so a sort or a refresh never blanks the grid. + // The sets shown stay until the new run's first set has rows or ends, so a sort or a refresh never blanks the grid. internal async Task ShowRunAsync( QueryRun run, GridSort sort, @@ -115,24 +118,26 @@ internal async Task ShowRunAsync( ErrorBar.IsOpen = false; Status(Strings.Get("StatusRunning")); ResultSetCount = 0; + _milliseconds = null; // The last run's messages stay on screen until this one's arrive or it ends, as its rows do. _messages.Reset(); - _picked = null; - var shownIndex = -1; - (ResultSet Set, IReadOnlyList Columns)? waiting = null; - long rows = 0; + _pickedView = null; + var results = new RunResults(Strings.Get("ResultSetItem"), Strings.Get("ResultSetItemMore")); + var incoming = new List(); + var adopted = false; long? affected = null; - var truncated = false; - void ShowWaiting() + void Adopt() { - if (waiting is { } next) + if (adopted || incoming.Count == 0) { - waiting = null; - Show(next.Set, next.Columns, sort); - if (_picked is null) - { - SelectView(ResultsView.Results); - } + return; + } + + adopted = true; + ShowRun(incoming, sort); + if (_pickedView is null) + { + SelectView(ResultsView.Results); } } @@ -144,26 +149,29 @@ void ShowWaiting() { case QueryEvent.ResultSetStarted started: ResultSetCount++; - if (shownIndex < 0 && run.GetResultSet(started.SetIndex) is { } set) + if (run.GetResultSet(started.SetIndex) is { } set) { - shownIndex = started.SetIndex; - waiting = (set, started.Columns.Take(visibleColumns).ToList()); + var result = results.Add(started.SetIndex, started.Columns.Take(visibleColumns).ToList()); + var item = new ComboBoxItem { Content = results.Label(result) }; + incoming.Add(new HeldSet(result, set, item)); + if (ReferenceEquals(_held, incoming)) + { + ListInPicker(item); + } } break; - case QueryEvent.RowsAppended appended when appended.SetIndex == shownIndex: - rows = appended.RowCount; - ShowWaiting(); - ResultGrid.RowsChanged(); - break; - case QueryEvent.RowLimitReached limit when limit.SetIndex == shownIndex: - truncated = true; - break; - case QueryEvent.ResultSetFinished finished when finished.SetIndex == shownIndex: - rows = finished.RowCount; - truncated |= finished.Truncation != Truncation.Complete; - ShowWaiting(); - ResultGrid.RowsChanged(); + case QueryEvent.RowsAppended or QueryEvent.RowLimitReached or QueryEvent.ResultSetFinished: + if (results.Apply(queryEvent) is { } changed + && incoming.Find(entry => ReferenceEquals(entry.Result, changed)) is { } grown) + { + grown.Item.Content = results.Label(changed); + if (queryEvent is not QueryEvent.RowLimitReached) + { + RowsArrived(grown, ended: queryEvent is QueryEvent.ResultSetFinished); + } + } + break; case QueryEvent.CommandCompleted command: affected = (affected ?? 0) + (command.RowsAffected ?? 0); @@ -173,12 +181,12 @@ void ShowWaiting() QueueMessages(); break; case QueryEvent.Finished finished: - ShowWaiting(); - RowCount = shownIndex >= 0 ? rows : 0; + Adopt(); + RowCount = adopted ? (_picked?.Result.Rows ?? 0) : 0; Transaction = finished.Transaction; - Finish(finished.Outcome, shownIndex >= 0, rows, affected, truncated); + Finish(finished.Outcome, adopted, affected); ShowMessages(); - SelectView(_messages.ViewAtEnd(shownIndex >= 0, _picked)); + SelectView(_messages.ViewAtEnd(adopted, _pickedView)); break; default: break; @@ -187,47 +195,152 @@ void ShowWaiting() } finally { - // A run read no further, as when its tab closed, never shows the set it held back. - waiting?.Set.Dispose(); + // A run read no further, as when its tab closed, never shows the sets it held back. + if (!ReferenceEquals(_held, incoming)) + { + DisposeSets(incoming, null); + } + } + + void RowsArrived(HeldSet held, bool ended) + { + if (ReferenceEquals(held, incoming[0])) + { + Adopt(); + } + + if (ReferenceEquals(held, _picked)) + { + ResultGrid.RowsChanged(); + if (ended) + { + ShowPickedLabel(); + } + } } } - private void Show(ResultSet set, IReadOnlyList columns, GridSort sort) + // The run's sets replace the last run's, and the grid shows its first. + private void ShowRun(List incoming, GridSort sort) { - var (shown, source) = (_shown, _source); - _shown = set; - _source = set; - Columns = columns; - ResultGrid.SetData(new ResultSetData(set, columns)); + var (held, sorted) = (_held, _sorted); + var first = incoming[0]; + _held = incoming; + _picked = first; + _sorted = null; + Columns = first.Result.Columns; + ResultGrid.SetData(new ResultSetData(first.Set, first.Result.Columns)); ResultGrid.Sort = sort; ResultGrid.Visibility = Visibility.Visible; Placeholder.Visibility = Visibility.Collapsed; // The grid disposed the old set's cursor in SetData. - Dispose(shown, source); + DisposeSets(held, sorted); + ListAllInPicker(); + } + + private void Pick(HeldSet held) + { + var sorted = _sorted; + _picked = held; + _sorted = null; + Columns = held.Result.Columns; + ResultGrid.SetData(new ResultSetData(held.Set, held.Result.Columns)); + ResultGrid.Sort = GridSort.None; + sorted?.Dispose(); + if (_milliseconds is { } milliseconds) + { + Status(RowsText(held.Result, milliseconds)); + } + + ResultPicked?.Invoke(this, EventArgs.Empty); } private void Release() { + var (held, sorted) = (_held, _sorted); ResultGrid.SetData(null); ResultGrid.Visibility = Visibility.Collapsed; Placeholder.Visibility = Visibility.Visible; - Dispose(_shown, _source); - _shown = null; - _source = null; + _held = []; + _picked = null; + _sorted = null; Columns = []; + DisposeSets(held, sorted); + ListAllInPicker(); + } + + private static void DisposeSets(List held, ResultSet? sorted) + { + sorted?.Dispose(); + foreach (var entry in held) + { + entry.Set.Dispose(); + } + } + + private void ListAllInPicker() + { + _picking = true; + try + { + ResultPicker.Items.Clear(); + foreach (var entry in _held) + { + ResultPicker.Items.Add(entry.Item); + } + + ResultPicker.SelectedItem = _picked?.Item; + } + finally + { + _picking = false; + } + + ResultPicker.Visibility = _held.Count > 1 ? Visibility.Visible : Visibility.Collapsed; } - private static void Dispose(ResultSet? shown, ResultSet? source) + // Sets come one after another, so the first has ended by the time a second makes the picker appear. + private void ListInPicker(ComboBoxItem item) { - if (!ReferenceEquals(shown, source)) + ResultPicker.Items.Add(item); + if (_held.Count == 2) { - shown?.Dispose(); + ShowPickedLabel(); } - source?.Dispose(); + ResultPicker.Visibility = _held.Count > 1 ? Visibility.Visible : Visibility.Collapsed; } - private void Finish(QueryOutcome outcome, bool hasRows, long rows, long? affected, bool truncated) + // A closed ComboBox shows the content its selected item had when it was selected. + private void ShowPickedLabel() + { + _picking = true; + try + { + ResultPicker.SelectedItem = null; + ResultPicker.SelectedItem = _picked?.Item; + } + finally + { + _picking = false; + } + } + + // The picker's own changes come back here too, which _picking turns away. + private void OnResultPicked(object sender, SelectionChangedEventArgs e) + { + if (_picking + || ResultPicker.SelectedItem is not ComboBoxItem item + || _held.Find(entry => ReferenceEquals(entry.Item, item)) is not { } picked + || ReferenceEquals(picked, _picked)) + { + return; + } + + Pick(picked); + } + + private void Finish(QueryOutcome outcome, bool hasRows, long? affected) { if (!hasRows) { @@ -238,10 +351,10 @@ private void Finish(QueryOutcome outcome, bool hasRows, long rows, long? affecte { case QueryOutcome.Completed completed: var milliseconds = (long)completed.Elapsed.TotalMilliseconds; - if (hasRows) + _milliseconds = milliseconds; + if (_picked is { } picked) { - Status(RowsStatus?.Invoke(rows, milliseconds, truncated) - ?? Strings.Format(truncated ? "StatusRowsLimited" : "StatusRows", rows, milliseconds)); + Status(RowsText(picked.Result, milliseconds)); } else { @@ -265,6 +378,10 @@ private void Finish(QueryOutcome outcome, bool hasRows, long rows, long? affecte } } + private string RowsText(RunResult result, long milliseconds) => + RowsStatus?.Invoke(result.Rows, milliseconds, result.Truncated) + ?? Strings.Format(result.Truncated ? "StatusRowsLimited" : "StatusRows", result.Rows, milliseconds); + // Notices that arrive together are laid out once, after the batch: a TextBox lays out all its text on each change. private void QueueMessages() { @@ -298,9 +415,12 @@ private void OnViewChanged(SelectorBar sender, SelectorBarSelectionChangedEventA return; } - _picked = view; + _pickedView = view; SelectView(view); } private void Status(string text) => StatusChanged?.Invoke(this, text); + + // The pane owns each set until the next run replaces them or the tab closes. + private sealed record HeldSet(RunResult Result, ResultSet Set, ComboBoxItem Item); } diff --git a/windows/src/TablePro.App/Views/SettingsView.xaml.cs b/windows/src/TablePro.App/Views/SettingsView.xaml.cs index 945cdc2366..40f57936bc 100644 --- a/windows/src/TablePro.App/Views/SettingsView.xaml.cs +++ b/windows/src/TablePro.App/Views/SettingsView.xaml.cs @@ -116,6 +116,17 @@ private async void OnReportIssue(object sender, RoutedEventArgs e) => await Launcher.LaunchUriAsync(IssuesUri); // LaunchFolderPathAsync needs Windows 10 1903; this form runs on 1809. - private async void OnOpenDataFolder(object sender, RoutedEventArgs e) => - await Launcher.LaunchFolderAsync(await Windows.Storage.StorageFolder.GetFolderFromPathAsync(_workbench.Folder)); + private async void OnOpenDataFolder(object sender, RoutedEventArgs e) + { + try + { + // A folder removed while the app runs is made again, as the app's next write would make it. + Directory.CreateDirectory(_workbench.Folder); + await Launcher.LaunchFolderAsync(await Windows.Storage.StorageFolder.GetFolderFromPathAsync(_workbench.Folder)); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + // The card names the folder, so it can still be opened by hand. + } + } } diff --git a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs index d3ae53c05e..0d27b2b496 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs @@ -1,4 +1,5 @@ using System.Collections.ObjectModel; +using System.Runtime.InteropServices; using Microsoft.UI; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; @@ -6,6 +7,7 @@ using Microsoft.UI.Xaml.Input; using TablePro.App.Model; using TablePro.App.Services; +using TablePro.Editor.Model; using TablePro.Interop; using Windows.ApplicationModel.DataTransfer; using Windows.System; @@ -28,6 +30,9 @@ public sealed partial class WorkspaceView : UserControl // Schemas a refresh reads again, the next time each one opens. private readonly HashSet _stale = []; private readonly ClosedTabs _closed = new(); + + // What each query tab's text read at its document's version, since the session is written every few seconds. + private readonly Dictionary _snapshots = []; private readonly HashSet _closingTabs = []; private CancellationTokenSource? _watching; private Windows.Foundation.Point _stripOrigin; @@ -69,6 +74,9 @@ internal WorkspaceView(Workbench workbench, OpenConnection connection) // The user closed the last tab; a window a tab was moved into closes with it. public event EventHandler? Emptied; + // Something session.json keeps changed: a tab, its text, its page, the one selected or the pane's width. + public event EventHandler? SessionChanged; + internal IWorkspaceHost? Host { get; set; } public int TabCount => Tabs.TabItems.Count; @@ -212,14 +220,52 @@ public SessionConnection Snapshot(SessionConnection origin) var tabs = Tabs.TabItems.OfType() .Select(item => item.Content) .OfType() - .Select(content => content.Snapshot()) + .Select(SnapshotOf) .ToList(); return origin with { Tabs = tabs, SelectedTab = Math.Max(0, Tabs.SelectedIndex) }; } + // A query tab's snapshot is its fixed title and its text, so the text is copied out again only once it changed. + private SessionTab SnapshotOf(ITabContent content) + { + if (content.Editor?.Document is not { } document) + { + return content.Snapshot(); + } + + if (_snapshots.TryGetValue(content, out var kept) + && kept.Version == document.Version + && kept.Ending == document.LineEnding) + { + return kept.Tab; + } + + var tab = content.Snapshot(); + _snapshots[content] = (document.Version, document.LineEnding, tab); + return tab; + } + internal bool HasUnconfirmedTransactions(ISet confirmed) => Tabs.TabItems.OfType() .Any(item => item.Content is ITabContent content && WorkspaceNotices.Holds(content.Transaction) && !confirmed.Contains(content)); + // A table tab loading a page is not work the user started, so only a query tab's run is asked about. + internal List RunningQueries() => [.. Tabs.TabItems.OfType() + .Select(item => item.Content) + .OfType() + .Where(view => view.IsRunning)]; + + // The tabs of a window that closed, so Reopen closed tab brings them back here in their order. + internal void KeepClosed(IReadOnlyList tabs) + { + ArgumentNullException.ThrowIfNull(tabs); + for (var index = tabs.Count - 1; index >= 0; index--) + { + _closed.Push(tabs[index], int.MaxValue); + } + + CommandsChanged?.Invoke(this, EventArgs.Empty); + } + internal async Task ConfirmCloseAsync(ISet confirmed) { foreach (var item in Tabs.TabItems.OfType().ToList()) @@ -264,11 +310,13 @@ public async Task CloseTabsAsync(bool keepClosed = false) } Tabs.TabItems.Clear(); + _snapshots.Clear(); Objects.Clear(); ObjectList.ItemsSource = null; _everyObject = null; WorkspaceGrid.Visibility = Visibility.Collapsed; CommandsChanged?.Invoke(this, EventArgs.Empty); + SessionChanged?.Invoke(this, EventArgs.Empty); } public void NewQueryTab(string? sql = null, string? title = null) @@ -427,6 +475,10 @@ internal void Take(TabViewItem item, int? index = null) if (item.Content is ITabContent content) { content.StateChanged += OnTabStateChanged; + if (content.Editor is { } editor) + { + editor.TextChanged += OnEditorTextChanged; + } } if (index is { } at && at >= 0 && at <= Tabs.TabItems.Count) @@ -441,6 +493,7 @@ internal void Take(TabViewItem item, int? index = null) Tabs.SelectedItem = item; UpdateElsewhereBar(); CommandsChanged?.Invoke(this, EventArgs.Empty); + SessionChanged?.Invoke(this, EventArgs.Empty); } // The tab keeps its content, its connection and its run; only this workspace lets go of it. @@ -450,6 +503,12 @@ internal void Release(TabViewItem item) if (item.Content is ITabContent content) { content.StateChanged -= OnTabStateChanged; + if (content.Editor is { } editor) + { + editor.TextChanged -= OnEditorTextChanged; + } + + _snapshots.Remove(content); } Tabs.TabItems.Remove(item); @@ -461,6 +520,7 @@ internal void Release(TabViewItem item) UpdateElsewhereBar(); CommandsChanged?.Invoke(this, EventArgs.Empty); + SessionChanged?.Invoke(this, EventArgs.Empty); } public void MoveActiveTabToNewWindow() @@ -506,8 +566,11 @@ private void OnTabStateChanged(object? sender, EventArgs e) { UpdateElsewhereBar(); CommandsChanged?.Invoke(this, EventArgs.Empty); + SessionChanged?.Invoke(this, EventArgs.Empty); } + private void OnEditorTextChanged(object? sender, EventArgs e) => SessionChanged?.Invoke(this, EventArgs.Empty); + // Built when asked for, since a menu keeps the window it first opened in and a tab can move to another. private void OnTabContextRequested(UIElement sender, ContextRequestedEventArgs args) { @@ -840,6 +903,7 @@ private void OnObjectsPaneSizeChanged(object sender, SizeChangedEventArgs e) if (ObjectsVisible && e.NewSize.Width > 0) { _workbench.ObjectPaneWidth = ObjectPane.Clamp(e.NewSize.Width); + SessionChanged?.Invoke(this, EventArgs.Empty); } } @@ -1146,6 +1210,7 @@ private void OnTabSelectionChanged(object sender, SelectionChangedEventArgs e) ActiveTab?.FocusContent(); UpdateElsewhereBar(); CommandsChanged?.Invoke(this, EventArgs.Empty); + SessionChanged?.Invoke(this, EventArgs.Empty); } private void OnCancelConnect(object sender, RoutedEventArgs e) => _connection.CancelConnect(); @@ -1158,6 +1223,14 @@ private void OnCopyDetails(object sender, RoutedEventArgs e) { var package = new DataPackage(); package.SetText(_failureDetails); - Clipboard.SetContent(package); + try + { + Clipboard.SetContent(package); + Clipboard.Flush(); + } + catch (COMException) + { + // Another program holds the clipboard open; pressing the button again copies once it lets go. + } } } diff --git a/windows/src/TablePro.DataGrid/DataGridView.Interaction.cs b/windows/src/TablePro.DataGrid/DataGridView.Interaction.cs index 16aa5d74c6..65a3a66aaa 100644 --- a/windows/src/TablePro.DataGrid/DataGridView.Interaction.cs +++ b/windows/src/TablePro.DataGrid/DataGridView.Interaction.cs @@ -5,7 +5,7 @@ using Microsoft.UI.Xaml.Input; using TablePro.DataGrid.Automation; using TablePro.DataGrid.Model; -using Windows.ApplicationModel.DataTransfer; +using TablePro.Graphics; using Windows.Foundation; using Windows.System; using Windows.UI.Core; @@ -164,10 +164,10 @@ public async Task CopySelectionAsync() using var reader = data.OpenReader(); return GridCopy.Tsv(reader, range, cancel.Token); }, cancel.Token).ConfigureAwait(true); - var package = new DataPackage(); - package.SetText(text); - Clipboard.SetContent(package); - Clipboard.Flush(); + if (cancel.IsCancellationRequested || !ClipboardText.TrySet(text)) + { + return false; + } Copies++; return true; } @@ -175,6 +175,11 @@ public async Task CopySelectionAsync() { return false; } + catch (ObjectDisposedException) when (cancel.IsCancellationRequested) + { + // The host let go of the data it replaced while this read it. + return false; + } finally { if (ReferenceEquals(_copy, cancel)) diff --git a/windows/src/TablePro.DataGrid/DataGridView.cs b/windows/src/TablePro.DataGrid/DataGridView.cs index daf2e477fc..79e7db0fc5 100644 --- a/windows/src/TablePro.DataGrid/DataGridView.cs +++ b/windows/src/TablePro.DataGrid/DataGridView.cs @@ -337,6 +337,8 @@ void IGridWindow.PrepareHeader(UIElement header, int column) // Shows data, or nothing for null. The grid opens its own reader on this thread and disposes it with the data. public void SetData(IGridData? data) { + // A copy still reading the old data would read it after the host lets it go, then put it on the clipboard. + _copy?.Cancel(); _cache?.Dispose(); _cache = null; _data = data; diff --git a/windows/src/TablePro.Editor/SqlEditor.Keyboard.cs b/windows/src/TablePro.Editor/SqlEditor.Keyboard.cs index a341532f48..11286107cf 100644 --- a/windows/src/TablePro.Editor/SqlEditor.Keyboard.cs +++ b/windows/src/TablePro.Editor/SqlEditor.Keyboard.cs @@ -1,7 +1,7 @@ using Microsoft.UI.Input; using Microsoft.UI.Xaml.Input; using TablePro.Editor.Model; -using Windows.ApplicationModel.DataTransfer; +using TablePro.Graphics; using Windows.System; using Windows.UI.Core; @@ -220,37 +220,30 @@ public void Copy() var text = _model.CopyText(); if (text.Length > 0) { - SetClipboard(text); + _ = SetClipboard(text); } } + // The text leaves the document only once the clipboard took it, so a held clipboard loses nothing. public void Cut() { - string text = string.Empty; - Edit(() => text = _model.Cut()); - if (text.Length > 0) + var text = _model.CopyText(); + if (text.Length > 0 && SetClipboard(text)) { - SetClipboard(text); + Edit(() => _model.Cut()); } } public async Task PasteAsync() { - var content = Clipboard.GetContent(); - if (!content.Contains(StandardDataFormats.Text)) + if (await ClipboardText.TryGetAsync() is { } text) { - return; + Edit(() => _model.Paste(text)); } - var text = await content.GetTextAsync(); - Edit(() => _model.Paste(text)); } - private void SetClipboard(string text) - { - var package = new DataPackage(); - package.SetText(Document.LineEnding == LineEnding.CrLf ? text.Replace("\n", "\r\n", StringComparison.Ordinal) : text); - Clipboard.SetContent(package); - } + private bool SetClipboard(string text) => ClipboardText.TrySet( + Document.LineEnding == LineEnding.CrLf ? text.Replace("\n", "\r\n", StringComparison.Ordinal) : text); private static bool Down(VirtualKey key) => InputKeyboardSource.GetKeyStateForCurrentThread(key).HasFlag(CoreVirtualKeyStates.Down); diff --git a/windows/src/TablePro.Editor/SqlEditor.Pointer.cs b/windows/src/TablePro.Editor/SqlEditor.Pointer.cs index e791561527..7a24334fb7 100644 --- a/windows/src/TablePro.Editor/SqlEditor.Pointer.cs +++ b/windows/src/TablePro.Editor/SqlEditor.Pointer.cs @@ -268,7 +268,17 @@ private async void OnDrop(object sender, DragEventArgs e) var deferral = e.GetDeferral(); try { - var text = await e.DataView.GetTextAsync(); + string text; + try + { + text = await e.DataView.GetTextAsync(); + } + catch (System.Runtime.InteropServices.COMException) + { + // The program the text was dragged from no longer answers, so nothing is dropped. + return; + } + var target = OffsetAt(e.GetPosition(_panel)); var move = _draggingTextOut && e.AcceptedOperation == DataPackageOperation.Move; Edit(() => _model.DropText(text, target, move)); diff --git a/windows/src/TablePro.Graphics/ClipboardText.cs b/windows/src/TablePro.Graphics/ClipboardText.cs new file mode 100644 index 0000000000..6d9ea489bd --- /dev/null +++ b/windows/src/TablePro.Graphics/ClipboardText.cs @@ -0,0 +1,66 @@ +using System.Runtime.InteropServices; +using Windows.ApplicationModel.DataTransfer; + +namespace TablePro.Graphics; + +// A program holding the clipboard open makes each call throw CLIPBRD_E_CANT_OPEN for a moment, so it is tried again. +internal static class ClipboardText +{ + private const int Attempts = 5; + private static readonly TimeSpan Pause = TimeSpan.FromMilliseconds(20); + + // False when the clipboard stayed held, and the text never reached it. + public static bool TrySet(string text) + { + ArgumentNullException.ThrowIfNull(text); + var package = new DataPackage(); + package.SetText(text); + if (!Retry(() => Clipboard.SetContent(package))) + { + return false; + } + + // Kept once the app has closed, as a copy is expected to be; the text is on the clipboard either way. + _ = Retry(Clipboard.Flush); + return true; + } + + // Null when there is no text, or the clipboard or the program that put the text there does not answer. + public static async Task TryGetAsync() + { + DataPackageView? content = null; + if (!Retry(() => content = Clipboard.GetContent()) || content is null) + { + return null; + } + + try + { + return content.Contains(StandardDataFormats.Text) ? await content.GetTextAsync() : null; + } + catch (COMException) + { + return null; + } + } + + private static bool Retry(Action call) + { + for (var attempt = 1; ; attempt++) + { + try + { + call(); + return true; + } + catch (COMException) when (attempt < Attempts) + { + Thread.Sleep(Pause); + } + catch (COMException) + { + return false; + } + } + } +} diff --git a/windows/src/TablePro.Interop/DatabaseSession.cs b/windows/src/TablePro.Interop/DatabaseSession.cs index 5321cbdb9a..d0e29f8b9c 100644 --- a/windows/src/TablePro.Interop/DatabaseSession.cs +++ b/windows/src/TablePro.Interop/DatabaseSession.cs @@ -132,6 +132,16 @@ public RunGate GateRun(string sql) return new RunGate(gate.Refused, [.. gate.Confirm], gate.Destructive); } + // The statement the caret is in, or the last one before it, split as GateRun and the driver split the text. + public Range? StatementAt(string sql, int caret) + { + ArgumentNullException.ThrowIfNull(sql); + ArgumentOutOfRangeException.ThrowIfNegative(caret); + return _session.StatementAt(sql, checked((uint)caret)) is { } span + ? new Range(checked((int)span.Start), checked((int)span.End)) + : null; + } + // A level saved while the session is open applies to it at once. False when it crosses Read only, which only a // new connection can do; the session keeps its level until then. public bool SetSafeMode(SafeMode level) => _session.SetSafeMode(Mapping.Level(level)); diff --git a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs index 6c6aa3f2f7..0eab50fb97 100644 --- a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs +++ b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs @@ -1018,6 +1018,8 @@ public struct UniffiVTableCallbackInterfaceRunListener + + @@ -1845,6 +1847,17 @@ public static extern RustBuffer uniffi_tablepro_ffi_fn_method_databasesession_sorted_query(ulong @ptr,RustBuffer @sql,RustBuffer @columns,uint @column,sbyte @ascending,ref UniffiRustCallStatus _uniffi_out_err ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + RustBuffer uniffi_tablepro_ffi_fn_method_databasesession_statement_at(ulong @ptr,RustBuffer @sql,uint @caret,ref UniffiRustCallStatus _uniffi_out_err + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -3121,6 +3134,17 @@ public static extern ushort uniffi_tablepro_ffi_checksum_method_databasesession_sorted_query( ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_databasesession_statement_at( + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -3654,6 +3678,12 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_sorted_query` checksum `35269`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_statement_at(); + if (checksum != 63657) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_statement_at` checksum `63657`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_table_query(); if (checksum != 23159) { @@ -4901,6 +4931,7 @@ internal interface IDatabaseSession { bool SetSafeMode(SafeModeTag @level); /// string? SortedQuery(string @sql, string[] @columns, uint @column, bool @ascending); + StatementSpan? StatementAt(string @sql, uint @caret); /// Task TableQuery(TableName @table, ColumnSort? @sort, ulong @limit, ulong @offset, CancelToken? @cancel); /// @@ -5219,6 +5250,14 @@ public bool SetSafeMode(SafeModeTag @level) { } + public StatementSpan? StatementAt(string @sql, uint @caret) { + return CallWithPointer(thisPtr => FfiConverterOptionalTypeStatementSpan.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_statement_at(thisPtr, FfiConverterString.INSTANCE.Lower(@sql), FfiConverterUInt32.INSTANCE.Lower(@caret), ref _status) +))); + } + + /// public async Task TableQuery(TableName @table, ColumnSort? @sort, ulong @limit, ulong @offset, CancelToken? @cancel) { return await _UniFFIAsync.UniffiRustCallAsync( @@ -7063,6 +7102,36 @@ public override void Write(SshSettings value, BigEndianStream stream) { +internal record StatementSpan ( + uint Start, + uint End +) { +} + +class FfiConverterTypeStatementSpan: FfiConverterRustBuffer { + public static FfiConverterTypeStatementSpan INSTANCE = new FfiConverterTypeStatementSpan(); + + public override StatementSpan Read(BigEndianStream stream) { + return new StatementSpan( + Start: FfiConverterUInt32.INSTANCE.Read(stream), + End: FfiConverterUInt32.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(StatementSpan value) { + return 0 + + FfiConverterUInt32.INSTANCE.AllocationSize(value.Start) + + FfiConverterUInt32.INSTANCE.AllocationSize(value.End); + } + + public override void Write(StatementSpan value, BigEndianStream stream) { + FfiConverterUInt32.INSTANCE.Write(value.Start, stream); + FfiConverterUInt32.INSTANCE.Write(value.End, stream); + } +} + + + /// /// A table, named the way the server qualifies it. No schema means the /// engine has none, or the connection's default one. @@ -10034,6 +10103,37 @@ public override void Write(SshSettings? value, BigEndianStream stream) { +class FfiConverterOptionalTypeStatementSpan: FfiConverterRustBuffer { + public static FfiConverterOptionalTypeStatementSpan INSTANCE = new FfiConverterOptionalTypeStatementSpan(); + + public override StatementSpan? Read(BigEndianStream stream) { + if (stream.ReadByte() == 0) { + return null; + } + return FfiConverterTypeStatementSpan.INSTANCE.Read(stream); + } + + public override int AllocationSize(StatementSpan? value) { + if (value == null) { + return 1; + } else { + return 1 + FfiConverterTypeStatementSpan.INSTANCE.AllocationSize((StatementSpan)value); + } + } + + public override void Write(StatementSpan? value, BigEndianStream stream) { + if (value == null) { + stream.WriteByte(0); + } else { + stream.WriteByte(1); + FfiConverterTypeStatementSpan.INSTANCE.Write((StatementSpan)value, stream); + } + } +} + + + + class FfiConverterOptionalTypeSecretKindTag: FfiConverterRustBuffer { public static FfiConverterOptionalTypeSecretKindTag INSTANCE = new FfiConverterOptionalTypeSecretKindTag(); diff --git a/windows/tests/TablePro.App.Model.Tests/RunResultsTests.cs b/windows/tests/TablePro.App.Model.Tests/RunResultsTests.cs new file mode 100644 index 0000000000..6b2491e548 --- /dev/null +++ b/windows/tests/TablePro.App.Model.Tests/RunResultsTests.cs @@ -0,0 +1,70 @@ +using TablePro.Interop; + +namespace TablePro.App.Model.Tests; + +public sealed class RunResultsTests +{ + private static readonly ResultColumn[] Columns = [new("id", "INTEGER", ValueKind.Integer)]; + + [Fact] + public void EverySetOfARunIsListedInTheOrderItStarted() + { + var results = New(); + var first = results.Add(0, Columns); + var second = results.Add(1, Columns); + + results.Apply(new QueryEvent.ResultSetFinished(0, 3, null, Truncation.Complete)); + results.Apply(new QueryEvent.RowsAppended(1, 40)); + results.Apply(new QueryEvent.ResultSetFinished(1, 50, null, Truncation.Complete)); + + Assert.Equal([first, second], results.Results); + Assert.Equal(3, first.Rows); + Assert.Equal(50, second.Rows); + Assert.Equal(["Result 1 (3)", "Result 2 (50)"], results.Results.Select(results.Label)); + } + + [Fact] + public void RowsOfOneSetNeverCountTowardAnother() + { + var results = New(); + var first = results.Add(0, Columns); + results.Add(1, Columns); + + var changed = results.Apply(new QueryEvent.RowsAppended(1, 7)); + + Assert.NotSame(first, changed); + Assert.Equal(0, first.Rows); + Assert.Equal(7, changed?.Rows); + } + + [Fact] + public void ASetTheLimitCutShortShowsTheCountedTotalOrAPlus() + { + var results = New(); + var counted = results.Add(0, Columns); + var uncounted = results.Add(1, Columns); + + results.Apply(new QueryEvent.ResultSetFinished(0, 10_000, 25_000, Truncation.ReadPastLimit)); + results.Apply(new QueryEvent.RowLimitReached(1, 10_000)); + results.Apply(new QueryEvent.ResultSetFinished(1, 10_000, null, Truncation.ServerStopped)); + + Assert.True(counted.Truncated); + Assert.True(uncounted.Truncated); + Assert.Equal("Result 1 (25000)", results.Label(counted)); + Assert.Equal("Result 2 (10000+)", results.Label(uncounted)); + } + + [Fact] + public void EventsAboutNoListedSetChangeNothing() + { + var results = New(); + var only = results.Add(0, Columns); + + Assert.Null(results.Apply(new QueryEvent.RowsAppended(4, 9))); + Assert.Null(results.Apply(new QueryEvent.CommandCompleted(2))); + Assert.Equal(0, only.Rows); + } + + // No number format, so the labels read the same under every culture. + private static RunResults New() => new("Result {0} ({1})", "Result {0} ({1}+)"); +} diff --git a/windows/tests/TablePro.App.Model.Tests/SessionWriterTests.cs b/windows/tests/TablePro.App.Model.Tests/SessionWriterTests.cs new file mode 100644 index 0000000000..ad6c4889e4 --- /dev/null +++ b/windows/tests/TablePro.App.Model.Tests/SessionWriterTests.cs @@ -0,0 +1,74 @@ +namespace TablePro.App.Model.Tests; + +public sealed class SessionWriterTests : IDisposable +{ + private readonly string _directory = Directory.CreateTempSubdirectory("tablepro-session-").FullName; + + public void Dispose() => Directory.Delete(_directory, recursive: true); + + private string SessionFile => Path.Combine(_directory, "session.json"); + + private static SessionState Holding(string sql) => new() + { + Connections = + [ + new SessionConnection + { + FilePath = @"C:\data\app.sqlite", + Name = "app", + EngineId = "sqlite", + Tabs = [new SessionTab { Kind = TabKind.Query, Title = "Query 1", Sql = sql }], + }, + ], + Front = 0, + }; + + private static string? SqlIn(SessionStore store) => + store.Load().Connections.SingleOrDefault()?.Tabs.SingleOrDefault()?.Sql; + + [Fact] + public async Task ASaveFromThePoolNeverLandsOverALaterOne() + { + var store = new SessionStore(SessionFile); + var writer = new SessionWriter(store); + + for (var round = 0; round < 50; round++) + { + var earlier = writer.SaveInBackgroundAsync(Holding($"SELECT {round}")); + Assert.Equal(SessionSave.Written, writer.Save(Holding($"SELECT {round} -- later"))); + await earlier; + + Assert.Equal($"SELECT {round} -- later", SqlIn(store)); + } + } + + [Fact] + public async Task NothingIsWrittenAfterTheLastSave() + { + var store = new SessionStore(SessionFile); + var writer = new SessionWriter(store); + + Assert.Equal(SessionSave.Written, writer.Save(Holding("SELECT 'last'"), last: true)); + var after = await writer.SaveInBackgroundAsync(Holding("SELECT 'emptied as the tabs close'")); + + Assert.Equal(SessionSave.Skipped, after); + Assert.Equal(SessionSave.Skipped, writer.Save(Holding("SELECT 'again'"))); + Assert.Equal("SELECT 'last'", SqlIn(store)); + } + + [Fact] + public async Task ASaveTheFileRefusesSaysSoAndLeavesTheOneBefore() + { + var store = new SessionStore(SessionFile); + var writer = new SessionWriter(store); + Assert.Equal(SessionSave.Written, await writer.SaveInBackgroundAsync(Holding("SELECT 1"))); + File.Delete(SessionFile); + Directory.CreateDirectory(SessionFile); + + Assert.Equal(SessionSave.Refused, await writer.SaveInBackgroundAsync(Holding("SELECT 2"))); + + Directory.Delete(SessionFile); + Assert.Equal(SessionSave.Written, writer.Save(Holding("SELECT 3"))); + Assert.Equal("SELECT 3", SqlIn(store)); + } +} diff --git a/windows/tests/TablePro.App.UITests/AppSession.cs b/windows/tests/TablePro.App.UITests/AppSession.cs index 81fa832019..bb1bb79ac8 100644 --- a/windows/tests/TablePro.App.UITests/AppSession.cs +++ b/windows/tests/TablePro.App.UITests/AppSession.cs @@ -514,6 +514,15 @@ public void Close() Retry.WhileFalse(() => _application.HasExited, Wait, throwOnTimeout: false); } + public bool HasExited => _application.HasExited; + + // As a crash, Task Manager or a power cut ends it: nothing runs on the way out. + public void Kill() + { + _application.Kill(); + Retry.WhileFalse(() => _application.HasExited, Wait, throwOnTimeout: false); + } + public void Dispose() { if (!_application.HasExited) diff --git a/windows/tests/TablePro.App.UITests/CloseAndSessionTests.cs b/windows/tests/TablePro.App.UITests/CloseAndSessionTests.cs new file mode 100644 index 0000000000..fe17df4a8b --- /dev/null +++ b/windows/tests/TablePro.App.UITests/CloseAndSessionTests.cs @@ -0,0 +1,119 @@ +using FlaUI.Core.AutomationElements; +using FlaUI.Core.Tools; +using FlaUI.Core.WindowsAPI; + +namespace TablePro.App.UITests; + +public sealed partial class ShellTests +{ + [Theory] + [MemberData(nameof(Themes))] + public void QueryTextComesBackAfterTheAppIsKilled(string theme) + { + const string Text = "SELECT 42 AS kept_after_a_kill"; + var folder = AppSession.NewDataFolder(theme); + var session = Path.Combine(folder, "session.json"); + using (var app = AppSession.Relaunch(folder, theme)) + { + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + app.FindByName("Track", app.Find("ObjectTree")); + app.TypeInto("Query 1", Text); + + // On disk a moment after the typing, with the app still open. + Assert.True(Retry.WhileFalse(() => FileHolds(session, Text), AppSession.Wait, throwOnTimeout: false).Success); + app.Kill(); + } + + using var again = AppSession.Relaunch(folder, theme); + var back = Retry.WhileFalse( + () => AppSession.EditorText(again.FindEditor("Query 1")).TrimEnd('\r', '\n') == Text, + AppSession.Wait, + throwOnTimeout: false).Success; + Assert.True(back); + } + + [Theory] + [MemberData(nameof(Themes))] + public void ATabWindowClosedWithItsTabsLeavesThemToReopen(string theme) + { + const string Text = "SELECT 7 AS from_the_closed_window"; + using var app = AppSession.Launch(theme); + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + app.FindByName("Track", app.Find("ObjectTree")); + var tabs = app.Find("Tabs"); + bool Open() => tabs.FindFirstDescendant(condition => condition.ByName("Query 2")) is not null; + app.Chord(Open, VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_T); + app.TypeInto("Query 2", Text); + app.Click(app.Find("FileMenu"), () => app.TryFindMenuItem("Move tab to new window") is not null); + app.FindMenuItem("Move tab to new window").Patterns.Invoke.Pattern.Invoke(); + var torn = app.WaitForWindow(SampleWindow); + + app.Chord(torn, () => !app.HasWindow(SampleWindow), VirtualKeyShort.ALT, VirtualKeyShort.F4); + app.WaitForClose(SampleWindow); + app.Chord(Open, VirtualKeyShort.CONTROL, VirtualKeyShort.SHIFT, VirtualKeyShort.KEY_T); + + var text = string.Empty; + Retry.WhileFalse( + () => (text = AppSession.EditorText(app.FindEditor("Query 2"))).Contains(Text, StringComparison.Ordinal), + AppSession.Wait, + throwOnTimeout: false); + Assert.Contains(Text, text, StringComparison.Ordinal); + } + + [Theory] + [MemberData(nameof(Themes))] + public void ASettingChangedWhileItsFileIsHeldIsSavedOnExit(string theme) + { + using var app = AppSession.Launch(theme); + var path = Path.Combine(app.DataFolder, "settings.json"); + app.Find("SettingsButton").AsButton().Invoke(); + app.Find("SettingsPage"); + + // As a virus scan holds a file it reads: the app cannot replace it until it lets go. + using (new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)) + { + app.Find("ReopenSwitch").AsToggleButton().Toggle(); + Thread.Sleep(1_000); + Assert.False(app.HasExited); + } + + Assert.DoesNotContain("\"reopenSession\": false", File.ReadAllText(path), StringComparison.Ordinal); + app.Find("AboutExpander"); + app.Close(); + Assert.Contains("\"reopenSession\": false", File.ReadAllText(path), StringComparison.Ordinal); + } + + [Theory] + [MemberData(nameof(Themes))] + public void ClosingWhileAQueryRunsAsksFirstThenStopsIt(string theme) + { + using var app = AppSession.Launch(theme); + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + app.FindByName("Track", app.Find("ObjectTree")); + app.TypeInto("Query 1", Endless); + app.Chord(() => app.Find("StopButton").IsEnabled, VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER); + + app.Chord(() => app.TryFind("CloseRunningTabs") is not null, VirtualKeyShort.ALT, VirtualKeyShort.F4); + Assert.Contains("Query 1", app.Find("CloseRunningTabs").Name, StringComparison.Ordinal); + app.Screenshot("43-close-running-asked"); + app.CloseDialog(); + Assert.True(app.Find("StopButton").IsEnabled); + + app.Chord(() => app.TryFind("CloseRunningTabs") is not null, VirtualKeyShort.ALT, VirtualKeyShort.F4); + app.PressDialogButton(); + Assert.True(Retry.WhileFalse(() => app.HasExited, AppSession.Wait, throwOnTimeout: false).Success); + } + + // The app replaces the file as it writes, so a read can meet it mid-swap. + private static bool FileHolds(string path, string text) + { + try + { + return File.ReadAllText(path).Contains(text, StringComparison.Ordinal); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + return false; + } + } +} diff --git a/windows/tests/TablePro.App.UITests/RunStatementTests.cs b/windows/tests/TablePro.App.UITests/RunStatementTests.cs new file mode 100644 index 0000000000..c79149e58a --- /dev/null +++ b/windows/tests/TablePro.App.UITests/RunStatementTests.cs @@ -0,0 +1,53 @@ +using FlaUI.Core.AutomationElements; +using FlaUI.Core.WindowsAPI; + +namespace TablePro.App.UITests; + +public sealed partial class ShellTests +{ + private const string TwoSelects = "SELECT 1 AS a; SELECT 2 AS b"; + + [Theory] + [MemberData(nameof(Themes))] + public void RunStatementRunsOnlyTheStatementAtTheCursorAndSelectsIt(string theme) + { + using var app = AppSession.Launch(theme); + + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + app.FindByName("Track", app.Find("ObjectTree")); + app.TypeInto("Query 1", TwoSelects); + app.Chord( + () => app.Find("QueryStatus").Name.StartsWith("Rows: 1", StringComparison.Ordinal), + VirtualKeyShort.CONTROL, + VirtualKeyShort.SHIFT, + VirtualKeyShort.ENTER); + + Assert.Equal("2", app.WaitForCell(app.Find("ResultsGrid"), 0, 0, value => value == "2")); + Assert.Equal("SELECT 2 AS b", app.FindEditor("Query 1").Patterns.Text.Pattern.GetSelection()[0].GetText(-1)); + Assert.True(app.TryFind("ResultPicker") is null or { IsOffscreen: true }); + app.Screenshot("41-run-statement"); + } + + [Theory] + [MemberData(nameof(Themes))] + public void EveryResultSetOfARunCanBePicked(string theme) + { + using var app = AppSession.Launch(theme); + + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + app.FindByName("Track", app.Find("ObjectTree")); + app.TypeInto("Query 1", TwoSelects); + app.Chord( + () => app.TryFind("ResultPicker") is { IsOffscreen: false }, + VirtualKeyShort.CONTROL, + VirtualKeyShort.ENTER); + Assert.Equal("1", app.WaitForCell(app.Find("ResultsGrid"), 0, 0, value => value == "1")); + + var picker = app.Find("ResultPicker").AsComboBox(); + picker.Select("Result 2 (1)"); + picker.Collapse(); + + Assert.Equal("2", app.WaitForCell(app.Find("ResultsGrid"), 0, 0, value => value == "2")); + app.Screenshot("42-result-picker"); + } +} diff --git a/windows/tests/TablePro.DataGrid.Tests/InteractionTests.cs b/windows/tests/TablePro.DataGrid.Tests/InteractionTests.cs index 6fe5bf26d8..b763784cee 100644 --- a/windows/tests/TablePro.DataGrid.Tests/InteractionTests.cs +++ b/windows/tests/TablePro.DataGrid.Tests/InteractionTests.cs @@ -1,3 +1,4 @@ +using System.Runtime.InteropServices; using System.Text; using TablePro.DataGrid.Model; using Windows.ApplicationModel.DataTransfer; @@ -95,6 +96,40 @@ public Task ASelectionLargerThanTheScreenCopiesEveryCellFromTheModel() => WinUiH Assert.Equal(Expected(new CellRange(100, 0, 2_099, 11)), copied); }); + [Fact] + public Task ACopyWhileAnotherProgramHoldsTheClipboardFailsAndTheNextOneCopies() => WinUiHost.RunAsync(async () => + { + var grid = await WinUiHost.ShowAsync(new SyntheticData()); + grid.SelectCell(3, 2, extend: false); + using (var held = HeldClipboard.Hold()) + { + Assert.True(held.IsHeld); + Assert.False(await grid.CopySelectionAsync()); + } + + Assert.True(await grid.CopySelectionAsync()); + Assert.Equal(Expected(new CellRange(3, 2, 3, 2)), await ClipboardTextAsync()); + }); + + [Fact] + public Task ACopyOfDataTheGridNoLongerShowsIsDropped() => WinUiHost.RunAsync(async () => + { + var old = new SyntheticData(columns: 4); + var grid = await WinUiHost.ShowAsync(old); + var before = new DataPackage(); + before.SetText("before the copy"); + Clipboard.SetContent(before); + grid.SelectAll(); + + // As a refresh or a closing tab gives the grid new data and then lets the old set go. + var copy = grid.CopySelectionAsync(); + grid.SetData(new SyntheticData(rows: 10, columns: 2)); + old.Release(); + + Assert.False(await copy); + Assert.Equal("before the copy", await ClipboardTextAsync()); + }); + [Fact] public Task AHeaderClickAsksForASortAndARepeatFlipsIt() => WinUiHost.RunAsync(async () => { @@ -185,3 +220,54 @@ private static async Task ClipboardTextAsync() return content.Contains(StandardDataFormats.Text) ? await content.GetTextAsync() : ""; } } + +// Another program with the clipboard open, as a clipboard manager or a remote desktop session has it for a moment. +internal sealed partial class HeldClipboard : IDisposable +{ + private readonly ManualResetEventSlim _release = new(); + private readonly Thread _thread; + + private HeldClipboard(ManualResetEventSlim opened) + { + _thread = new Thread(() => + { + IsHeld = OpenClipboard(IntPtr.Zero); + opened.Set(); + _release.Wait(); + if (IsHeld) + { + _ = CloseClipboard(); + } + }) + { + IsBackground = true, + Name = "Clipboard holder", + }; + _thread.Start(); + } + + public bool IsHeld { get; private set; } + + public static HeldClipboard Hold() + { + using var opened = new ManualResetEventSlim(); + var held = new HeldClipboard(opened); + opened.Wait(); + return held; + } + + public void Dispose() + { + _release.Set(); + _thread.Join(); + _release.Dispose(); + } + + [LibraryImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool OpenClipboard(IntPtr owner); + + [LibraryImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool CloseClipboard(); +} diff --git a/windows/tests/TablePro.Interop.Tests/StatementAtTests.cs b/windows/tests/TablePro.Interop.Tests/StatementAtTests.cs new file mode 100644 index 0000000000..d5b568d264 --- /dev/null +++ b/windows/tests/TablePro.Interop.Tests/StatementAtTests.cs @@ -0,0 +1,67 @@ +namespace TablePro.Interop.Tests; + +public sealed class StatementAtTests +{ + [Fact] + public async Task TheCaretPicksItsOwnStatementAndOnlyThatOneRuns() + { + const string Script = "SELECT name FROM people WHERE id = 1; DELETE FROM people"; + await using var database = await SqliteDatabase.CreateAsync(); + + var statement = Picked(database.Session, Script, caret: 5); + using var run = database.Editor.Run(statement ?? string.Empty, cancellationToken: TestContext.Current.CancellationToken); + var outcome = await run.Completion; + using var count = database.Editor.Run("SELECT COUNT(*) FROM people", cancellationToken: TestContext.Current.CancellationToken); + await count.Completion; + using var counted = count.GetResultSet(0); + + Assert.Equal("SELECT name FROM people WHERE id = 1", statement); + Assert.Empty(database.Session.GateRun(statement ?? string.Empty).Confirm); + Assert.IsType(outcome); + Assert.Equal("3", counted?.GetCellText(0, 0)); + } + + [Fact] + public async Task ACaretPastTheLastTerminatorPicksTheLastStatement() + { + const string Script = "SELECT 1;\nSELECT 2;\n"; + await using var database = await SqliteDatabase.CreateAsync(); + + Assert.Equal("SELECT 2", Picked(database.Session, Script, Script.Length)); + } + + [Fact] + public async Task ASqliteTriggerBodyIsOneStatement() + { + const string Script = "CREATE TRIGGER noted AFTER INSERT ON people BEGIN UPDATE people SET note = 'x'; END; SELECT 1"; + await using var database = await SqliteDatabase.CreateAsync(); + + var statement = Picked(database.Session, Script, Script.IndexOf("UPDATE", StringComparison.Ordinal)); + + Assert.Equal("CREATE TRIGGER noted AFTER INSERT ON people BEGIN UPDATE people SET note = 'x'; END", statement); + } + + [Fact] + public async Task OffsetsCountUtf16UnitsPastAnEmoji() + { + const string Script = "SELECT '😀'; SELECT 2"; + await using var database = await SqliteDatabase.CreateAsync(); + + var picked = database.Session.StatementAt(Script, Script.IndexOf("SELECT 2", StringComparison.Ordinal) + 1); + + Assert.Equal(new Range(13, 21), picked); + Assert.Equal("SELECT 2", Picked(database.Session, Script, 14)); + } + + [Fact] + public async Task TextWithNoStatementHasNone() + { + await using var database = await SqliteDatabase.CreateAsync(); + + Assert.Null(database.Session.StatementAt(string.Empty, 0)); + Assert.Null(database.Session.StatementAt(" -- only a comment\n", 3)); + } + + private static string? Picked(DatabaseSession session, string sql, int caret) => + session.StatementAt(sql, caret) is { } range ? sql[range] : null; +} From 6a477f910f267a616fa3f1a7861b4ba1fb646b4c Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 08:25:14 +0700 Subject: [PATCH 2/3] docs(linux): add the changelog fragments for #4180 --- linux/changelog.d/4180.fixed.1.md | 1 + linux/changelog.d/4180.fixed.2.md | 1 + linux/changelog.d/4180.fixed.3.md | 1 + linux/changelog.d/4180.fixed.4.md | 1 + linux/changelog.d/4180.fixed.5.md | 1 + linux/changelog.d/4180.fixed.md | 1 + 6 files changed, 6 insertions(+) create mode 100644 linux/changelog.d/4180.fixed.1.md create mode 100644 linux/changelog.d/4180.fixed.2.md create mode 100644 linux/changelog.d/4180.fixed.3.md create mode 100644 linux/changelog.d/4180.fixed.4.md create mode 100644 linux/changelog.d/4180.fixed.5.md create mode 100644 linux/changelog.d/4180.fixed.md diff --git a/linux/changelog.d/4180.fixed.1.md b/linux/changelog.d/4180.fixed.1.md new file mode 100644 index 0000000000..727a79e9e1 --- /dev/null +++ b/linux/changelog.d/4180.fixed.1.md @@ -0,0 +1 @@ +A MySQL or MariaDB query that matched no rows showed as a command, without its column headers. diff --git a/linux/changelog.d/4180.fixed.2.md b/linux/changelog.d/4180.fixed.2.md new file mode 100644 index 0000000000..e5b8667692 --- /dev/null +++ b/linux/changelog.d/4180.fixed.2.md @@ -0,0 +1 @@ +Exporting a MySQL or MariaDB editor query that matched no rows wrote a file without its header. diff --git a/linux/changelog.d/4180.fixed.3.md b/linux/changelog.d/4180.fixed.3.md new file mode 100644 index 0000000000..df7ef0eedd --- /dev/null +++ b/linux/changelog.d/4180.fixed.3.md @@ -0,0 +1 @@ +On MySQL and MariaDB, SELECT ... INTO OUTFILE in the editor wrote only as many rows as the editor's row limit. diff --git a/linux/changelog.d/4180.fixed.4.md b/linux/changelog.d/4180.fixed.4.md new file mode 100644 index 0000000000..d2883981be --- /dev/null +++ b/linux/changelog.d/4180.fixed.4.md @@ -0,0 +1 @@ +An MCP client reading rows with very large values could make TablePro use gigabytes of memory. diff --git a/linux/changelog.d/4180.fixed.5.md b/linux/changelog.d/4180.fixed.5.md new file mode 100644 index 0000000000..e699bc3386 --- /dev/null +++ b/linux/changelog.d/4180.fixed.5.md @@ -0,0 +1 @@ +An MCP client that gave up on a row count or a table lookup left it running and holding the connection. diff --git a/linux/changelog.d/4180.fixed.md b/linux/changelog.d/4180.fixed.md new file mode 100644 index 0000000000..1b205c2a16 --- /dev/null +++ b/linux/changelog.d/4180.fixed.md @@ -0,0 +1 @@ +Connecting to a host whose first address did not answer timed out instead of trying its next address. From e38e63b964f757f4e5bcbdbdb4a0d7112466ea31 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 08:53:22 +0700 Subject: [PATCH 3/3] fix(windows): name the WinUI DispatcherQueueTimer in MainWindow for CS0104 --- windows/src/TablePro.App/MainWindow.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/windows/src/TablePro.App/MainWindow.xaml.cs b/windows/src/TablePro.App/MainWindow.xaml.cs index decd65926c..113f91de4d 100644 --- a/windows/src/TablePro.App/MainWindow.xaml.cs +++ b/windows/src/TablePro.App/MainWindow.xaml.cs @@ -43,7 +43,7 @@ public sealed partial class MainWindow : Window, IShell, IWorkspaceHost // The primary's alone: it writes the session for every window. private readonly SessionWriter? _saves; - private readonly DispatcherQueueTimer? _saveTimer; + private readonly Microsoft.UI.Dispatching.DispatcherQueueTimer? _saveTimer; private TimeSpan _nextRetry = SaveDelay; private SettingsView? _settings; private OpenConnection? _front;