diff --git a/core/crates/core/src/browse/browse_sql_error.rs b/core/crates/core/src/browse/browse_sql_error.rs index 78a4c918ac..45591d6b62 100644 --- a/core/crates/core/src/browse/browse_sql_error.rs +++ b/core/crates/core/src/browse/browse_sql_error.rs @@ -36,3 +36,49 @@ pub enum BrowseSqlError { #[error(transparent)] Bind(#[from] BindError), } + +impl BrowseSqlError { + // Stable across releases, for an app outside Rust that words the error itself. + pub fn code(&self) -> &'static str { + match self { + Self::NoColumns => "browse.no_columns", + Self::UnknownSortColumn(_) => "browse.unknown_sort_column", + Self::UnsupportedPosition { .. } => "browse.unsupported_position", + Self::SortUnsupported => "browse.sort_unsupported", + Self::StalePagingState => "browse.stale_paging_state", + Self::NullAnchor => "browse.null_anchor", + Self::UnreadableAnchor => "browse.unreadable_anchor", + Self::ShapeMismatch { .. } => "browse.shape_mismatch", + Self::Filter(_) => "browse.filter", + Self::Build(_) => "browse.build", + Self::Bind(_) => "browse.bind", + } + } + + // Outermost first, down to the filter's or the statement builder's own code. + pub fn codes(&self) -> Vec<&'static str> { + let mut codes = vec![self.code()]; + match self { + Self::Filter(filter) => codes.extend(filter.codes()), + Self::Build(build) => codes.push(build.code()), + _ => {} + } + codes + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::edit::EditParseError; + + #[test] + fn a_refused_filter_value_names_the_page_the_filter_and_the_parse() { + let error = BrowseSqlError::Filter(BuildFilterError::Parse { + column: "id".to_owned(), + source: EditParseError::InvalidInteger, + }); + + assert_eq!(error.codes(), ["browse.filter", "filter.parse", "edit.invalid_integer"]); + } +} diff --git a/core/crates/core/src/filter/build_filter_error.rs b/core/crates/core/src/filter/build_filter_error.rs index 2d024b738d..53533fd691 100644 --- a/core/crates/core/src/filter/build_filter_error.rs +++ b/core/crates/core/src/filter/build_filter_error.rs @@ -41,3 +41,70 @@ pub enum BuildFilterError { #[error("field {0} has a dot in its name or starts with $, so a filter cannot name it")] UnaddressableField(String), } + +impl BuildFilterError { + // Stable across releases, for an app outside Rust that words the error itself. + pub fn code(&self) -> &'static str { + match self { + Self::UnknownColumn(_) => "filter.unknown_column", + Self::MissingValue(_) => "filter.missing_value", + Self::BetweenMissingBound => "filter.between_missing_bound", + Self::EmptyInList => "filter.empty_in_list", + Self::WrongValueShape { .. } => "filter.wrong_value_shape", + Self::Parse { .. } => "filter.parse", + Self::Bind(_) => "filter.bind", + Self::OperatorNotAllowed { .. } => "filter.operator_not_allowed", + Self::CombinatorNotAllowed(_) => "filter.combinator_not_allowed", + Self::RawPredicate(_) => "filter.raw_predicate", + Self::RawDocument(_) => "filter.raw_document", + Self::RawNotDocument => "filter.raw_not_document", + Self::UnaddressableField(_) => "filter.unaddressable_field", + } + } + + // Outermost first: a value the column cannot take adds the parse error's own code. + pub fn codes(&self) -> Vec<&'static str> { + match self { + Self::Parse { source, .. } => vec![self.code(), source.code()], + _ => vec![self.code()], + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::edit::EditParseError; + + #[test] + fn a_value_the_column_cannot_take_names_the_parse_error_too() { + let error = BuildFilterError::Parse { + column: "id".to_owned(), + source: EditParseError::InvalidInteger, + }; + + assert_eq!(error.codes(), ["filter.parse", "edit.invalid_integer"]); + } + + #[test] + fn each_code_is_under_filter() { + let errors = [ + BuildFilterError::UnknownColumn("x".to_owned()), + BuildFilterError::MissingValue(FilterOp::Eq), + BuildFilterError::BetweenMissingBound, + BuildFilterError::EmptyInList, + BuildFilterError::WrongValueShape { op: FilterOp::In }, + BuildFilterError::OperatorNotAllowed { + column: "x".to_owned(), + op: FilterOp::Ilike, + }, + BuildFilterError::CombinatorNotAllowed(Combinator::Or), + BuildFilterError::RawNotDocument, + BuildFilterError::UnaddressableField("a.b".to_owned()), + ]; + + for error in errors { + assert!(error.code().starts_with("filter."), "{}", error.code()); + } + } +} diff --git a/core/crates/ffi/src/core_handle.rs b/core/crates/ffi/src/core_handle.rs index 2fac15fbc0..3208939990 100644 --- a/core/crates/ffi/src/core_handle.rs +++ b/core/crates/ffi/src/core_handle.rs @@ -16,19 +16,20 @@ use tablepro_session::{ }; use tablepro_ssh::{InProcessSsh, SshAuth}; use tablepro_storage::{ConnectionStore, CredentialDemand, KeyringCredentials, SavedConnection, StoragePaths}; +use tokio::sync::OnceCell; use uuid::Uuid; use crate::cancel_token::{CancelToken, token}; use crate::drivers::{self, DriverInfo}; use crate::error::CoreError; +use crate::history::QueryHistoryStore; use crate::host::{HostConfig, HostSecretStore, HostSecrets}; 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; -/// The library's root: the linked engines, the runtime and what the app -/// passed in. An app makes one and keeps it. +// An app makes one and keeps it. #[derive(uniffi::Object)] pub struct Core { registry: Arc, @@ -36,21 +37,22 @@ pub struct Core { deadlines: Arc, vault: Arc, items: Arc, + paths: Option, store: Option, + // Opened on first use, so a core that never shows history never migrates or prunes it. + history: Arc>>, pub(crate) tasks: Tasks, pub(crate) ssh: Arc, } -/// A database file to open. #[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] pub struct FileOpenRequest { - /// An absolute path. + // Absolute. pub path: String, - /// The engine to open it with. `None` lets the file's first bytes - /// decide, then its name. + // None lets the file's first bytes decide the engine, then its name. pub driver_id: Option, pub read_only: bool, - /// Make a new database at `path`, which must not exist yet. + // `path` must not exist yet. pub create: bool, } @@ -71,7 +73,6 @@ impl Core { Ok(Self::build(config, vault, items, tasks)) } - /// Every engine linked in, by name. pub fn drivers(&self) -> Vec { let mut drivers: Vec = self .registry @@ -91,6 +92,20 @@ impl Core { ))) } + // Every caller shares one store. A failed open is tried again on the next call. + pub async fn history(&self) -> Result, CoreError> { + let paths = self.paths.clone().ok_or_else(no_storage)?; + let history = self.history.clone(); + let tasks = self.tasks.clone(); + on_runtime(&self.tasks, async move { + history + .get_or_try_init(|| QueryHistoryStore::open(paths, tasks)) + .await + .cloned() + }) + .await + } + pub async fn open_file( &self, request: FileOpenRequest, @@ -277,17 +292,20 @@ impl Core { ssh: ssh.clone(), liveness, }; - let store = config + let paths = config .storage_dir .filter(|dir| !dir.trim().is_empty()) - .map(|dir| ConnectionStore::new(&storage_paths(Path::new(&dir)))); + .map(|dir| storage_paths(Path::new(&dir))); + let store = paths.as_ref().map(ConnectionStore::new); Arc::new(Self { registry: Arc::new(drivers::registry()), connector: Arc::new(DriverConnector::new(services, deadlines.clone())), deadlines, vault, items, + paths, store, + history: Arc::default(), tasks, ssh, }) diff --git a/core/crates/ffi/src/definition.rs b/core/crates/ffi/src/definition.rs new file mode 100644 index 0000000000..fc291e46b5 --- /dev/null +++ b/core/crates/ffi/src/definition.rs @@ -0,0 +1,98 @@ +use tablepro_core::meta::{ObjectDefinition, ObjectInfo, ObjectKind}; + +use crate::catalog::{ObjectEntry, ObjectKindTag}; +use crate::error::CoreError; + +// `database`: where `sql` runs when it names none. `opens_in_editor`: false when an editor run would not make it. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct ObjectSource { + pub sql: String, + pub database: Option, + pub opens_in_editor: bool, +} + +impl From for ObjectSource { + fn from(definition: ObjectDefinition) -> Self { + Self { + sql: definition.sql, + database: definition.database, + opens_in_editor: definition.refusal.is_none(), + } + } +} + +// A key's kind and a kind this build does not name carry too little for a driver to find the object by. +impl TryFrom for ObjectInfo { + type Error = CoreError; + + fn try_from(entry: ObjectEntry) -> Result { + let kind = match entry.kind { + ObjectKindTag::Table => ObjectKind::Table, + ObjectKindTag::PartitionedTable => ObjectKind::PartitionedTable, + ObjectKindTag::View => ObjectKind::View, + ObjectKindTag::MaterializedView => ObjectKind::MaterializedView, + ObjectKindTag::ForeignTable => ObjectKind::ForeignTable, + ObjectKindTag::Sequence => ObjectKind::Sequence, + ObjectKindTag::Function => ObjectKind::Function, + ObjectKindTag::Procedure => ObjectKind::Procedure, + ObjectKindTag::Trigger => ObjectKind::Trigger, + ObjectKindTag::Dictionary => ObjectKind::Dictionary, + ObjectKindTag::Key | ObjectKindTag::Other => { + return Err(CoreError::failed( + "ffi.no_definition", + "a key or an object of an unknown kind has no definition to read", + )); + } + }; + Ok(Self { + schema: entry.schema, + name: entry.name, + kind, + parent_table: entry.parent_table, + }) + } +} + +#[cfg(test)] +mod tests { + use tablepro_core::meta::EditorRefusal; + + use super::*; + + fn entry(kind: ObjectKindTag) -> ObjectEntry { + ObjectEntry { + schema: Some("public".to_owned()), + name: "audit".to_owned(), + kind, + parent_table: Some("orders".to_owned()), + } + } + + #[test] + fn a_trigger_keeps_its_table_on_the_way_in() { + let info = ObjectInfo::try_from(entry(ObjectKindTag::Trigger)).unwrap(); + + assert_eq!(info, ObjectInfo::trigger(Some("public".to_owned()), "audit", "orders")); + } + + #[test] + fn a_key_and_an_unknown_kind_are_refused() { + for kind in [ObjectKindTag::Key, ObjectKindTag::Other] { + let refused = ObjectInfo::try_from(entry(kind)).unwrap_err(); + + assert_eq!(refused.code(), "ffi.no_definition"); + } + } + + #[test] + fn a_refusal_keeps_the_text_out_of_the_editor() { + let source = ObjectSource::from(ObjectDefinition { + sql: "CREATE PROCEDURE p AS SELECT 1".to_owned(), + database: Some("shop".to_owned()), + refusal: Some(EditorRefusal::SplitsApart), + }); + + assert!(!source.opens_in_editor); + assert_eq!(source.database.as_deref(), Some("shop")); + } +} diff --git a/core/crates/ffi/src/editor.rs b/core/crates/ffi/src/editor.rs index 1efd9a6bcb..7396bc2205 100644 --- a/core/crates/ffi/src/editor.rs +++ b/core/crates/ffi/src/editor.rs @@ -1,9 +1,10 @@ use std::sync::Arc; use std::time::Duration; -use tablepro_core::{ByteLimit, EditorSession, EditorSessionState, RowLimit, RunOptions, UserSql}; +use tablepro_core::{ByteLimit, CallOptions, EditorSession, EditorSessionState, RowLimit, RunOptions, UserSql}; use tablepro_session::runtime::{Tasks, catch_panic}; +use crate::cancel_token::{CancelToken, token}; use crate::error::CoreError; use crate::run::{QueryRun, RunListener, RunOutcome, RunSink, RunState, TransactionStateTag}; use crate::runtime::on_runtime; @@ -70,6 +71,16 @@ impl QueryEditor { self.editor.state() == EditorSessionState::Lost } + // Where a definition names no database of its own, its text has to run in the one it was read from. + pub async fn use_database(&self, database: String, cancel: Option>) -> Result<(), CoreError> { + let editor = self.editor.clone(); + let cancel = token(cancel); + on_runtime(&self.tasks, async move { + Ok(editor.use_database(&database, CallOptions::new(None, cancel)).await?) + }) + .await + } + /// Closes the editor's connection, ending its transaction. pub async fn close(&self) -> Result<(), CoreError> { let editor = self.editor.clone(); diff --git a/core/crates/ffi/src/error.rs b/core/crates/ffi/src/error.rs index 645e35b115..ef326cf51d 100644 --- a/core/crates/ffi/src/error.rs +++ b/core/crates/ffi/src/error.rs @@ -271,6 +271,14 @@ impl From<&OperationError> for CoreError { OperationError::Driver(driver) => Self::Failed { info: ErrorInfo::wrapping(&[error.code()], driver, error.to_string()).into(), }, + // The filter's own code says which rule the app has to word, down to a value the column cannot take. + OperationError::Browse(browse) => { + let mut codes = vec![error.code()]; + codes.extend(browse.codes()); + Self::Failed { + info: ErrorInfo::plain(&codes, error.to_string()).into(), + } + } other => Self::Failed { info: ErrorInfo::plain(&[other.code()], other.to_string()).into(), }, @@ -360,6 +368,21 @@ mod tests { } } + #[test] + fn a_refused_filter_names_the_rule_the_app_words() { + let error = OperationError::Browse(tablepro_core::browse::BrowseSqlError::Filter( + tablepro_core::filter::BuildFilterError::UnknownColumn("ghost".to_owned()), + )); + + let info = info(error.into()); + + assert_eq!( + info.codes, + ["operation.browse", "browse.filter", "filter.unknown_column"] + ); + assert_eq!(info.code, "filter.unknown_column"); + } + #[test] fn a_config_error_carries_its_own_code() { let info = info(OperationError::Driver(DriverError::Config(ConfigError::RelativePath)).into()); diff --git a/core/crates/ffi/src/export.rs b/core/crates/ffi/src/export.rs new file mode 100644 index 0000000000..c07ad3ec92 --- /dev/null +++ b/core/crates/ffi/src/export.rs @@ -0,0 +1,662 @@ +use std::fs::File; +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex, PoisonError}; + +use async_trait::async_trait; +use tablepro_core::browse::{DeclaredPrecisionSink, ExportFields, ExportRequest, SortKey, build_export}; +use tablepro_core::column::ResultColumn; +use tablepro_core::dialect::SqlDialect; +use tablepro_core::export::{ + CsvEncoder, CsvOptions, EncodeError, HtmlEncoder, JsonArrayEncoder, MarkdownEncoder, RenderError, RowEncoder, + SqlInsertEncoder, XLSX_MAX_DATA_ROWS, XlsxSheetWriter, XmlEncoder, +}; +use tablepro_core::filter::FilterSet; +use tablepro_core::meta::{RowIdentity, TableRef}; +use tablepro_core::value::{JsonText, OtherValue, UndecodableReason, UndecodedValue}; +use tablepro_core::{ColumnInfo, ReadStatement, ResultEvent, ResultSink, SinkFlow, Truncation, Value}; +use tablepro_session::OperationKind; +use tablepro_session::runtime::Tasks; +use tokio_util::sync::CancellationToken; +use uuid::Uuid; + +use crate::cancel_token::{CancelToken, token}; +use crate::catalog::TableName; +use crate::error::CoreError; +use crate::filter::{TableFilterArg, filter_set}; +use crate::results::{ResultSet, ValueKind}; +use crate::runtime::on_runtime; +use crate::session::{ColumnSort, DatabaseSession, sort_keys}; + +// Written a chunk at a time, so neither a row nor the whole result is one write. +const CHUNK_BYTES: usize = 64 * 1024; +// Held rows encoded between two looks at the cancel. +const ROWS_PER_BATCH: u64 = 1_024; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] +pub enum ExportFormatTag { + Csv, + Json, + Excel, + Markdown, + Html, + Xml, + SqlInsert, +} + +#[uniffi::export] +impl DatabaseSession { + // Only the first `visible_columns` columns, since a table page carries row addresses after them. + pub async fn export_rows( + &self, + rows: Arc, + visible_columns: u32, + format: ExportFormatTag, + table: Option, + path: String, + cancel: Option>, + ) -> Result { + let session = self.session().clone(); + let tasks = self.tasks().clone(); + let cancel = token(cancel); + on_runtime(self.tasks(), async move { + let target = destination(&path)?; + // A set still reading would write the rows held so far as if they were all of them. + if !rows.is_finished() { + return Err(CoreError::failed( + "ffi.export_unfinished", + "the rows are still arriving", + )); + } + let table = table.map(TableRef::from); + if format == ExportFormatTag::Excel && rows.row_count() > XLSX_MAX_DATA_ROWS as u64 { + return Err(sheet_full()); + } + let insert = match (format, &table) { + (ExportFormatTag::SqlInsert, None) => return Err(needs_table()), + (ExportFormatTag::SqlInsert, Some(table)) => { + let columns = tokio::select! { + biased; + () = cancel.cancelled() => return Err(CoreError::Cancelled), + columns = session.columns(table.clone()) => columns?, + }; + Some(InsertTarget::of(table, &columns)) + } + _ => None, + }; + let recipe = Recipe { + format, + title: title(table.as_ref(), &target), + insert, + }; + let held = rows.result_columns(); + let visible = usize::try_from(visible_columns).unwrap_or(usize::MAX).min(held.len()); + let columns: Arc<[ResultColumn]> = held.iter().take(visible).cloned().collect(); + let part = Arc::new(PartFile::beside(target)?); + let ending = Ending::default(); + let mut sink = FileSink::new( + recipe, + session.current_dialect(), + part.clone(), + tasks.clone(), + ending.clone(), + ); + let fed = feed(&mut sink, &rows, &columns, &cancel).await; + drop(sink); + settle(part, &tasks, &ending, fed).await + }) + .await + } + + // Every row the filter selects, in the sort's order, streamed to the file on a connection of the read's own. + pub async fn export_table( + &self, + table: TableName, + sort: Option, + filter: Option, + format: ExportFormatTag, + path: String, + cancel: Option>, + ) -> Result { + let session = self.session().clone(); + let tasks = self.tasks().clone(); + let cancel = token(cancel); + on_runtime(self.tasks(), async move { + let target = destination(&path)?; + let table: TableRef = table.into(); + let dialect = session.current_dialect().ok_or_else(not_connected)?; + let filter = filter_set(filter, &*dialect)?; + let columns = tokio::select! { + biased; + () = cancel.cancelled() => return Err(CoreError::Cancelled), + columns = session.columns(table.clone()) => columns?, + }; + let identity = session + .run_cancellable(OperationKind::Metadata, &cancel, |lease, options| { + let table = table.clone(); + async move { Ok(lease.row_identity(&table, options).await?) } + }) + .await?; + let recipe = Recipe { + format, + title: title(Some(&table), &target), + insert: (format == ExportFormatTag::SqlInsert).then(|| InsertTarget::of(&table, &columns)), + }; + let read = Arc::new(TableRead { + table, + columns, + filter, + sort: sort_keys(sort), + identity, + }); + let part = Arc::new(PartFile::beside(target)?); + let ending = Ending::default(); + let streamed = session + .run_cancellable(OperationKind::Export, &cancel, |lease, options| { + let read = read.clone(); + let sink = FileSink::new( + recipe.clone(), + Some(lease.dialect()), + part.clone(), + tasks.clone(), + ending.clone(), + ); + async move { + let statement = build_export(&*lease.dialect(), &read.request())?; + let sink = DeclaredPrecisionSink::new(Box::new(sink), read.columns.clone()); + Ok(lease + .stream_read(&ReadStatement::Generated(statement), Box::new(sink), options) + .await?) + } + }) + .await; + settle(part, &tasks, &ending, streamed.map(|_| ()).map_err(CoreError::from)).await + }) + .await + } +} + +struct TableRead { + table: TableRef, + columns: Arc<[ColumnInfo]>, + filter: FilterSet, + sort: Vec, + identity: RowIdentity, +} + +impl TableRead { + fn request(&self) -> ExportRequest<'_> { + ExportRequest { + table: &self.table, + columns: &self.columns, + filter: &self.filter, + sort: &self.sort, + identity: &self.identity, + fields: ExportFields::Every, + } + } +} + +#[derive(Clone)] +struct Recipe { + format: ExportFormatTag, + title: String, + insert: Option, +} + +#[derive(Clone)] +struct InsertTarget { + table: TableRef, + // Computed by the server, so the statements leave them out. + generated: Vec, + // Numbered by the server, so they keep their values through the engine's override. + numbered: Vec, +} + +impl InsertTarget { + fn of(table: &TableRef, columns: &[ColumnInfo]) -> Self { + Self { + table: table.clone(), + generated: columns + .iter() + .filter(|column| column.is_generated) + .map(|column| column.name.clone()) + .collect(), + numbered: columns + .iter() + .filter(|column| column.is_auto_increment) + .map(|column| column.name.clone()) + .collect(), + } + } +} + +enum Writer { + Rows(Box), + // A workbook is a zip archive, written whole once the last row is in; its sheet spills rows to disk meanwhile. + Sheet(Box), +} + +impl Recipe { + // The columns are the ones the read announced, which are the ones its rows carry. + fn writer(&self, columns: Arc<[ResultColumn]>, dialect: Option>) -> Result { + let encoder: Box = match self.format { + ExportFormatTag::Csv => Box::new(CsvEncoder::new(columns, CsvOptions::default())), + ExportFormatTag::Json => Box::new(JsonArrayEncoder::new(columns)), + ExportFormatTag::Markdown => Box::new(MarkdownEncoder::new(columns)), + ExportFormatTag::Html => Box::new(HtmlEncoder::new(columns, self.title.clone())), + ExportFormatTag::Xml => Box::new(XmlEncoder::new(columns)), + ExportFormatTag::Excel => { + let sheet = XlsxSheetWriter::new(columns, &self.title).map_err(render_error)?; + return Ok(Writer::Sheet(Box::new(sheet))); + } + ExportFormatTag::SqlInsert => { + let target = self.insert.as_ref().ok_or_else(needs_table)?; + let dialect = dialect.ok_or_else(not_connected)?; + Box::new(SqlInsertEncoder::new( + dialect, + &target.table, + columns, + &target.generated, + &target.numbered, + )) + } + }; + Ok(Writer::Rows(encoder)) + } +} + +// The rows written, or why the sink stopped, which explains a read that ended early better than the read can. +type Ending = Arc>>>; + +// File work runs on the blocking pool, so a slow disk never holds a runtime thread. +struct FileSink { + recipe: Recipe, + dialect: Option>, + part: Arc, + tasks: Tasks, + ending: Ending, + file: Option, + writer: Option, + buffer: Vec, + rows: u64, + sets: u32, +} + +impl FileSink { + fn new( + recipe: Recipe, + dialect: Option>, + part: Arc, + tasks: Tasks, + ending: Ending, + ) -> Self { + Self { + recipe, + dialect, + part, + tasks, + ending, + file: None, + writer: None, + buffer: Vec::with_capacity(CHUNK_BYTES), + rows: 0, + sets: 0, + } + } + + async fn start(&mut self, columns: Arc<[ResultColumn]>) -> Result<(), CoreError> { + self.sets += 1; + // A file holds one result, and a second would come out glued to the first. + if self.sets > 1 { + return Err(CoreError::failed( + "ffi.export_several_results", + "the read returned more than one result", + )); + } + let recipe = self.recipe.clone(); + let dialect = self.dialect.clone(); + let part = self.part.clone(); + let (file, mut writer) = blocking(&self.tasks, move || { + let file = File::create(&part.path).map_err(|error| io_error(&part.target, &error))?; + Ok((file, recipe.writer(columns, dialect)?)) + }) + .await?; + if let Writer::Rows(encoder) = &mut writer { + encoder.begin(&mut self.buffer); + } + self.file = Some(file); + self.writer = Some(writer); + Ok(()) + } + + async fn take(&mut self, rows: Vec>) -> Result<(), CoreError> { + match self.writer.take() { + Some(Writer::Rows(mut encoder)) => { + let encoded = self.encode(&mut *encoder, &rows).await; + self.writer = Some(Writer::Rows(encoder)); + encoded + } + Some(Writer::Sheet(mut sheet)) => { + let count = rows.len() as u64; + let sheet = blocking(&self.tasks, move || { + sheet.write_rows(&rows).map_err(render_error)?; + Ok(sheet) + }) + .await?; + self.writer = Some(Writer::Sheet(sheet)); + self.rows += count; + Ok(()) + } + None => Err(CoreError::failed( + "ffi.export_failed", + "rows arrived before their result", + )), + } + } + + async fn encode(&mut self, encoder: &mut dyn RowEncoder, rows: &[Vec]) -> Result<(), CoreError> { + for row in rows { + encoder.row(&mut self.buffer, &mut row.iter()).map_err(encode_error)?; + self.rows += 1; + if self.buffer.len() >= CHUNK_BYTES { + self.flush().await?; + } + } + Ok(()) + } + + async fn flush(&mut self) -> Result<(), CoreError> { + if self.buffer.is_empty() { + return Ok(()); + } + let mut file = self.file.take().ok_or_else(file_gone)?; + let bytes = std::mem::take(&mut self.buffer); + let part = self.part.clone(); + let (file, mut bytes) = blocking(&self.tasks, move || { + file.write_all(&bytes).map_err(|error| io_error(&part.target, &error))?; + Ok((file, bytes)) + }) + .await?; + bytes.clear(); + self.buffer = bytes; + self.file = Some(file); + Ok(()) + } + + async fn finish(&mut self) -> Result { + match self.writer.take() { + Some(Writer::Rows(mut encoder)) => encoder.finish(&mut self.buffer), + Some(Writer::Sheet(sheet)) => { + self.buffer = blocking(&self.tasks, move || { + XlsxSheetWriter::finish(*sheet).map_err(render_error) + }) + .await?; + } + None => return Err(no_result()), + } + self.flush().await?; + let file = self.file.take().ok_or_else(file_gone)?; + let part = self.part.clone(); + // On disk before the rename, so the name never points at a file still short of its rows. + blocking(&self.tasks, move || { + file.sync_all().map_err(|error| io_error(&part.target, &error)) + }) + .await?; + Ok(self.rows) + } + + // A failure is kept over a finish, since a second result after the first one fails the export. + fn end(&self, ending: Result) { + let mut slot = self.ending.lock().unwrap_or_else(PoisonError::into_inner); + if !matches!(*slot, Some(Err(_))) { + *slot = Some(ending); + } + } +} + +#[async_trait] +impl ResultSink for FileSink { + async fn accept(&mut self, event: ResultEvent) -> SinkFlow { + let step = match event { + ResultEvent::ResultSetStarted { columns } => self.start(columns).await, + ResultEvent::Rows(rows) => self.take(rows).await, + ResultEvent::ResultSetFinished { .. } => self.finish().await.map(|rows| self.end(Ok(rows))), + _ => Ok(()), + }; + match step { + Ok(()) => SinkFlow::Continue, + Err(error) => { + self.end(Err(error)); + SinkFlow::ConsumerGone + } + } + } +} + +// The rows a set holds, through the same sink a streamed read feeds, so both write the same bytes. +async fn feed( + sink: &mut FileSink, + set: &ResultSet, + columns: &Arc<[ResultColumn]>, + cancel: &CancellationToken, +) -> Result<(), CoreError> { + let total = set.row_count(); + let started = ResultEvent::ResultSetStarted { + columns: columns.clone(), + }; + if sink.accept(started).await == SinkFlow::ConsumerGone { + return Ok(()); + } + let mut next = 0; + while next < total { + if cancel.is_cancelled() { + return Err(CoreError::Cancelled); + } + let end = total.min(next + ROWS_PER_BATCH); + let batch = (next..end).map(|row| held_row(set, row, columns)).collect(); + if sink.accept(ResultEvent::Rows(batch)).await == SinkFlow::ConsumerGone { + return Ok(()); + } + next = end; + } + if cancel.is_cancelled() { + return Err(CoreError::Cancelled); + } + let finished = ResultEvent::ResultSetFinished { + retained: total, + total_rows: Some(total), + truncation: Truncation::Complete, + }; + sink.accept(finished).await; + Ok(()) +} + +fn held_row(set: &ResultSet, row: u64, columns: &[ResultColumn]) -> Vec { + columns + .iter() + .enumerate() + .map(|(index, column)| held_value(set, row, index, column)) + .collect() +} + +// The value where the store kept it exactly, else read back from the text the core's export wrote for it on arrival. +fn held_value(set: &ResultSet, row: u64, index: usize, column: &ResultColumn) -> Value { + if let Some(value) = set.stored_value(row, index) { + return value; + } + let text = set.full_text(row, index).flatten().unwrap_or_default(); + let other = |text: String| { + Value::Other(Box::new(OtherValue { + type_name: column.column_type.name.clone(), + text, + })) + }; + match set.cell_kind(row, index).flatten() { + Some(ValueKind::Float) => match text.parse::() { + Ok(number) => Value::Float64(number), + Err(_) => other(text), + }, + Some(ValueKind::Json) => match JsonText::parse(text.clone()) { + Ok(json) => Value::Json(json), + Err(_) => other(text), + }, + // Refused by every encoder, as a streamed export refuses it. + Some(ValueKind::Undecodable) => Value::Undecodable(Box::new(UndecodedValue { + type_name: column.column_type.name.clone(), + reason: UndecodableReason::UnsupportedType, + })), + _ => other(text), + } +} + +// The sink's own failure explains why the read stopped, so it goes before the read's. +async fn settle( + part: Arc, + tasks: &Tasks, + ending: &Ending, + read: Result<(), CoreError>, +) -> Result { + let ended = ending.lock().unwrap_or_else(PoisonError::into_inner).take(); + let rows = match (ended, read) { + (Some(Err(error)), _) | (_, Err(error)) => return Err(error), + (Some(Ok(rows)), Ok(())) => rows, + (None, Ok(())) => return Err(no_result()), + }; + blocking(tasks, move || part.place()).await?; + Ok(rows) +} + +// Removed by whoever lets go of it last unless put in place, so a sink still finishing cannot leave it behind. +struct PartFile { + path: PathBuf, + target: PathBuf, + placed: AtomicBool, +} + +impl PartFile { + fn beside(target: PathBuf) -> Result { + let name = target + .file_name() + .ok_or_else(|| bad_path(&target))? + .to_string_lossy() + .into_owned(); + Ok(Self { + path: target.with_file_name(format!(".{name}.{}.part", Uuid::new_v4().simple())), + target, + placed: AtomicBool::new(false), + }) + } + + // A rename replaces the file that was there in one step. + fn place(&self) -> Result<(), CoreError> { + std::fs::rename(&self.path, &self.target).map_err(|error| io_error(&self.target, &error))?; + self.placed.store(true, Ordering::Release); + Ok(()) + } +} + +impl Drop for PartFile { + fn drop(&mut self) { + if *self.placed.get_mut() { + return; + } + match std::fs::remove_file(&self.path) { + Ok(()) => {} + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => tracing::warn!(%error, "could not remove an unfinished export"), + } + } +} + +async fn blocking(tasks: &Tasks, work: F) -> Result +where + T: Send + 'static, + F: FnOnce() -> Result + Send + 'static, +{ + tasks + .spawn_blocking_task(work) + .await + .map_err(|failure| CoreError::failed("ffi.panicked", failure.to_string()))? +} + +fn destination(path: &str) -> Result { + let target = PathBuf::from(path); + match target.is_absolute() && target.file_name().is_some() { + true => Ok(target), + false => Err(bad_path(&target)), + } +} + +fn title(table: Option<&TableRef>, target: &Path) -> String { + match table { + Some(table) => table.name.clone(), + None => target + .file_stem() + .map(|stem| stem.to_string_lossy().into_owned()) + .unwrap_or_default(), + } +} + +fn io_error(path: &Path, error: &std::io::Error) -> CoreError { + let mut failed = CoreError::failed("ffi.export_io", format!("{}: {error}", path.display())); + if let CoreError::Failed { info } = &mut failed { + info.path = Some(path.display().to_string()); + } + failed +} + +fn render_error(error: RenderError) -> CoreError { + let code = match &error { + RenderError::TooManyRows { .. } => "ffi.export_sheet_full", + RenderError::TooManyColumns { .. } => "ffi.export_sheet_too_wide", + RenderError::TextTooLong { .. } => "ffi.export_cell_too_long", + RenderError::Encode(encode) => return encode_error(encode.clone()), + RenderError::Csv(_) | RenderError::Flush(_) | RenderError::Utf8(_) | RenderError::Xlsx(_) => { + "ffi.export_failed" + } + }; + CoreError::failed(code, error.to_string()) +} + +fn encode_error(error: EncodeError) -> CoreError { + let code = match &error { + EncodeError::Undecodable { .. } => "ffi.export_undecodable", + EncodeError::NotXmlText { .. } => "ffi.export_not_xml_text", + }; + CoreError::failed(code, error.to_string()) +} + +fn bad_path(path: &Path) -> CoreError { + CoreError::failed( + "ffi.export_bad_path", + format!("{} is not an absolute file path", path.display()), + ) +} + +fn needs_table() -> CoreError { + CoreError::failed( + "ffi.export_needs_table", + "SQL INSERT needs the table the rows came from", + ) +} + +fn sheet_full() -> CoreError { + CoreError::failed( + "ffi.export_sheet_full", + format!("a sheet holds {XLSX_MAX_DATA_ROWS} rows"), + ) +} + +fn no_result() -> CoreError { + CoreError::failed("ffi.export_no_result", "the read ended before its result did") +} + +fn file_gone() -> CoreError { + CoreError::failed("ffi.export_failed", "the export's file was closed early") +} + +fn not_connected() -> CoreError { + CoreError::failed("ffi.not_connected", "the session has no connection") +} diff --git a/core/crates/ffi/src/filter.rs b/core/crates/ffi/src/filter.rs new file mode 100644 index 0000000000..075c2d00db --- /dev/null +++ b/core/crates/ffi/src/filter.rs @@ -0,0 +1,320 @@ +use tablepro_core::ColumnInfo; +use tablepro_core::dialect::SqlDialect; +use tablepro_core::filter::{Combinator, FilterOp, FilterRule, FilterSet, FilterValue}; +use tablepro_core::sql_syntax::SqlGrammar; + +use crate::error::CoreError; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] +pub enum FilterOpTag { + Eq, + NotEq, + Lt, + LtEq, + Gt, + GtEq, + // The core adds the wildcards and escapes the user's own, so the text matches as typed. + Contains, + StartsWith, + EndsWith, + // The user's own pattern, wildcards and all. + Like, + NotLike, + Ilike, + IsNull, + IsNotNull, + In, + NotIn, + Between, +} + +// What a rule compares against, as typed. The core parses it as the column's type and binds it. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Enum)] +pub enum FilterValueArg { + Single { value: String }, + Pair { low: String, high: String }, + List { values: Vec }, +} + +// `value` is None for the null tests. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct FilterRuleArg { + pub column: String, + pub op: FilterOpTag, + pub value: Option, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] +pub enum CombinatorTag { + And, + Or, +} + +// `raw_sql` joins the rules as written, so the core reads it as exactly one expression first. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct TableFilterArg { + pub combinator: CombinatorTag, + pub rules: Vec, + pub raw_sql: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct FilterColumn { + pub name: String, + pub operators: Vec, +} + +// The operators the engine offers per column, the combinators it has, and whether it takes a raw SQL predicate. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct FilterChoices { + pub columns: Vec, + pub combinators: Vec, + pub accepts_raw_sql: bool, +} + +// A document engine takes a query document in place of SQL, which a field named for SQL would misname. +pub(crate) fn choices(dialect: &dyn SqlDialect, columns: &[ColumnInfo]) -> FilterChoices { + FilterChoices { + columns: columns + .iter() + .map(|column| FilterColumn { + name: column.name.clone(), + operators: dialect + .filter_operators(column) + .iter() + .copied() + .map(FilterOpTag::from) + .collect(), + }) + .collect(), + combinators: dialect + .filter_combinators() + .iter() + .copied() + .map(CombinatorTag::from) + .collect(), + accepts_raw_sql: takes_raw_sql(dialect), + } +} + +// A session file written by hand can hold any text, and an engine that is not SQL reads it in its own language. +pub(crate) fn filter_set(filter: Option, dialect: &dyn SqlDialect) -> Result { + let Some(filter) = filter else { + return Ok(FilterSet::default()); + }; + let raw = filter.raw_sql.as_deref().is_some_and(|sql| !sql.trim().is_empty()); + if raw && !takes_raw_sql(dialect) { + return Err(CoreError::failed( + "filter.raw_unsupported", + "this engine takes no SQL condition", + )); + } + Ok(filter.into()) +} + +fn takes_raw_sql(dialect: &dyn SqlDialect) -> bool { + SqlGrammar::SQL.contains(&dialect.grammar()) +} + +impl From for FilterSet { + fn from(filter: TableFilterArg) -> Self { + Self { + combinator: filter.combinator.into(), + rules: filter.rules.into_iter().map(FilterRule::from).collect(), + extra_sql: filter.raw_sql, + } + } +} + +impl From for FilterRule { + fn from(rule: FilterRuleArg) -> Self { + Self { + column: rule.column, + op: rule.op.into(), + value: rule.value.map(FilterValue::from), + } + } +} + +impl From for FilterValue { + fn from(value: FilterValueArg) -> Self { + match value { + FilterValueArg::Single { value } => Self::Single(value), + FilterValueArg::Pair { low, high } => Self::Pair(low, high), + FilterValueArg::List { values } => Self::List(values), + } + } +} + +impl From for Combinator { + fn from(combinator: CombinatorTag) -> Self { + match combinator { + CombinatorTag::And => Self::And, + CombinatorTag::Or => Self::Or, + } + } +} + +impl From for CombinatorTag { + fn from(combinator: Combinator) -> Self { + match combinator { + Combinator::And => Self::And, + Combinator::Or => Self::Or, + } + } +} + +impl From for FilterOp { + fn from(op: FilterOpTag) -> Self { + match op { + FilterOpTag::Eq => Self::Eq, + FilterOpTag::NotEq => Self::NotEq, + FilterOpTag::Lt => Self::Lt, + FilterOpTag::LtEq => Self::LtEq, + FilterOpTag::Gt => Self::Gt, + FilterOpTag::GtEq => Self::GtEq, + FilterOpTag::Contains => Self::Contains, + FilterOpTag::StartsWith => Self::StartsWith, + FilterOpTag::EndsWith => Self::EndsWith, + FilterOpTag::Like => Self::Like, + FilterOpTag::NotLike => Self::NotLike, + FilterOpTag::Ilike => Self::Ilike, + FilterOpTag::IsNull => Self::IsNull, + FilterOpTag::IsNotNull => Self::IsNotNull, + FilterOpTag::In => Self::In, + FilterOpTag::NotIn => Self::NotIn, + FilterOpTag::Between => Self::Between, + } + } +} + +impl From for FilterOpTag { + fn from(op: FilterOp) -> Self { + match op { + FilterOp::Eq => Self::Eq, + FilterOp::NotEq => Self::NotEq, + FilterOp::Lt => Self::Lt, + FilterOp::LtEq => Self::LtEq, + FilterOp::Gt => Self::Gt, + FilterOp::GtEq => Self::GtEq, + FilterOp::Contains => Self::Contains, + FilterOp::StartsWith => Self::StartsWith, + FilterOp::EndsWith => Self::EndsWith, + FilterOp::Like => Self::Like, + FilterOp::NotLike => Self::NotLike, + FilterOp::Ilike => Self::Ilike, + FilterOp::IsNull => Self::IsNull, + FilterOp::IsNotNull => Self::IsNotNull, + FilterOp::In => Self::In, + FilterOp::NotIn => Self::NotIn, + FilterOp::Between => Self::Between, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const EVERY_OP: [FilterOpTag; 17] = [ + FilterOpTag::Eq, + FilterOpTag::NotEq, + FilterOpTag::Lt, + FilterOpTag::LtEq, + FilterOpTag::Gt, + FilterOpTag::GtEq, + FilterOpTag::Contains, + FilterOpTag::StartsWith, + FilterOpTag::EndsWith, + FilterOpTag::Like, + FilterOpTag::NotLike, + FilterOpTag::Ilike, + FilterOpTag::IsNull, + FilterOpTag::IsNotNull, + FilterOpTag::In, + FilterOpTag::NotIn, + FilterOpTag::Between, + ]; + + #[test] + fn every_operator_crosses_and_comes_back_as_itself() { + for op in EVERY_OP { + assert_eq!(FilterOpTag::from(FilterOp::from(op)), op); + } + } + + #[test] + fn a_filter_keeps_its_rules_in_order_with_their_values_and_its_sql() { + let filter = TableFilterArg { + combinator: CombinatorTag::Or, + rules: vec![ + FilterRuleArg { + column: "age".to_owned(), + op: FilterOpTag::Between, + value: Some(FilterValueArg::Pair { + low: "18".to_owned(), + high: "30".to_owned(), + }), + }, + FilterRuleArg { + column: "note".to_owned(), + op: FilterOpTag::IsNull, + value: None, + }, + FilterRuleArg { + column: "id".to_owned(), + op: FilterOpTag::In, + value: Some(FilterValueArg::List { + values: vec!["1".to_owned(), "2".to_owned()], + }), + }, + ], + raw_sql: Some("length(name) > 3".to_owned()), + }; + + let set = FilterSet::from(filter); + + assert_eq!(set.combinator, Combinator::Or); + assert_eq!( + set.rules, + [ + FilterRule { + column: "age".to_owned(), + op: FilterOp::Between, + value: Some(FilterValue::Pair("18".to_owned(), "30".to_owned())), + }, + FilterRule { + column: "note".to_owned(), + op: FilterOp::IsNull, + value: None, + }, + FilterRule { + column: "id".to_owned(), + op: FilterOp::In, + value: Some(FilterValue::List(vec!["1".to_owned(), "2".to_owned()])), + }, + ] + ); + assert_eq!(set.raw_predicate(), Some("length(name) > 3")); + } + + #[cfg(feature = "engines")] + #[test] + fn raw_text_reaches_only_an_engine_that_reads_sql() { + let raw = |sql: &str| TableFilterArg { + combinator: CombinatorTag::And, + rules: Vec::new(), + raw_sql: Some(sql.to_owned()), + }; + + let refused = filter_set( + Some(raw("{ \"$where\": \"true\" }")), + &tablepro_driver_mongodb::MongoDialect, + ) + .unwrap_err(); + let blank = filter_set(Some(raw(" ")), &tablepro_driver_mongodb::MongoDialect).unwrap(); + + assert_eq!(refused.code(), "filter.raw_unsupported"); + assert!(blank.is_empty()); + } +} diff --git a/core/crates/ffi/src/history.rs b/core/crates/ffi/src/history.rs new file mode 100644 index 0000000000..ac7f3eeab2 --- /dev/null +++ b/core/crates/ffi/src/history.rs @@ -0,0 +1,278 @@ +use std::sync::Arc; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; + +use tablepro_core::sql_syntax::HistoryRecording; +use tablepro_session::Session; +use tablepro_session::runtime::Tasks; +use tablepro_storage::query_history::{ + Entry, HistoryCursor, HistoryPageRequest, HistoryQuery, MAX_PAGE_SIZE, MAX_QUERY_BYTES, NewEntry, Outcome, +}; +use tablepro_storage::{QueryHistory, StoragePaths}; +use uuid::Uuid; + +use crate::error::CoreError; +use crate::library::parse_id; +use crate::runtime::on_runtime; +use crate::session::DatabaseSession; + +#[derive(uniffi::Object)] +pub struct QueryHistoryStore { + history: QueryHistory, + tasks: Tasks, +} + +// A run as the editor saw it end. Times are milliseconds from the Unix epoch; the store keeps whole seconds. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct HistoryRun { + pub sql: String, + pub executed_at_ms: i64, + pub duration_ms: Option, + pub rows_affected: Option, + pub outcome: HistoryOutcomeTag, +} + +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Enum)] +pub enum HistoryOutcomeTag { + Succeeded, + Failed { message: String }, + Cancelled, +} + +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct HistoryEntryInfo { + pub id: i64, + pub sql: String, + // None for a run stored without one. + pub connection_id: Option, + pub connection_name: String, + pub driver_id: String, + pub executed_at_ms: i64, + pub duration_ms: Option, + pub rows_affected: Option, + pub outcome: HistoryOutcomeTag, + pub pinned: bool, +} + +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct HistoryPageInfo { + pub entries: Vec, + // None on the last page. + pub next: Option, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Record)] +pub struct HistoryCursorArg { + pub pinned: bool, + pub executed_at_secs: i64, + pub id: i64, +} + +impl QueryHistoryStore { + // The prune keeps every entry however old and drops the oldest past the store's cap. + pub(crate) async fn open(paths: StoragePaths, tasks: Tasks) -> Result, CoreError> { + let history = QueryHistory::open(&paths).await?; + match history.prune(0).await { + Ok(report) if report.removed_anything() => { + tracing::info!(over_cap = report.over_cap, "pruned the query history"); + } + Ok(_) => {} + Err(error) => tracing::warn!(%error, "could not prune the query history"), + } + Ok(Arc::new(Self { history, tasks })) + } +} + +#[uniffi::export] +impl QueryHistoryStore { + // None when the engine withholds the statement, the session is offline, or the text is past the store's cap. + pub async fn record(&self, session: Arc, run: HistoryRun) -> Result, CoreError> { + let history = self.history.clone(); + let session = session.session().clone(); + on_runtime(&self.tasks, async move { + let Some(entry) = kept(&session, run) else { + return Ok(None); + }; + Ok(Some(history.record(entry).await?)) + }) + .await + } + + // Every word of `text` appears in the entry, in any case. Pinned entries first, then newest. + pub async fn search( + &self, + text: String, + connection_id: Option, + after: Option, + limit: u32, + ) -> Result { + let mut query = HistoryQuery::from_input(&text); + query.connection = connection_id + .as_deref() + .filter(|id| !id.trim().is_empty()) + .map(parse_id) + .transpose()?; + let request = HistoryPageRequest { + after: after.as_ref().map(HistoryCursorArg::cursor), + limit: usize::try_from(limit).unwrap_or(MAX_PAGE_SIZE).min(MAX_PAGE_SIZE), + }; + let history = self.history.clone(); + on_runtime(&self.tasks, async move { + let page = history.search_page(&query, request).await?; + let next = page.next.and(page.entries.last()).map(HistoryCursorArg::after); + Ok(HistoryPageInfo { + entries: page.entries.into_iter().map(HistoryEntryInfo::from).collect(), + next, + }) + }) + .await + } + + pub async fn delete(&self, ids: Vec) -> Result<(), CoreError> { + let history = self.history.clone(); + on_runtime(&self.tasks, async move { + history.delete_returning(&ids).await?; + Ok(()) + }) + .await + } + + pub async fn clear(&self) -> Result<(), CoreError> { + let history = self.history.clone(); + on_runtime(&self.tasks, async move { + history.clear_all().await?; + Ok(()) + }) + .await + } +} + +// Without a dialect nobody can say which statements hold a secret, so nothing is kept. +fn kept(session: &Session, run: HistoryRun) -> Option { + let connection = session.id(); + let Some(dialect) = session.current_dialect() else { + tracing::debug!(%connection, "no dialect, so the run is not recorded"); + return None; + }; + // The store refuses it anyway, and reading it for secrets would lex it once per way the server may read it. + if run.sql.len() > MAX_QUERY_BYTES { + tracing::debug!(%connection, "a run too large for the history was not recorded"); + return None; + } + if let HistoryRecording::Withhold(reason) = dialect.history_recording(&run.sql) { + tracing::debug!(%connection, ?reason, "a run was kept out of the history"); + return None; + } + let metadata = session.metadata(); + Some(NewEntry { + query: run.sql, + driver_id: metadata.driver_id.clone(), + connection_id: metadata.id, + connection_name: metadata.name.clone(), + executed_at: from_millis(run.executed_at_ms), + duration_ms: run.duration_ms, + rows_affected: run.rows_affected, + outcome: run.outcome.into(), + }) +} + +impl HistoryCursorArg { + fn after(entry: &Entry) -> Self { + Self { + pinned: entry.pinned, + executed_at_secs: seconds(entry.executed_at), + id: entry.id, + } + } + + // The store makes its cursor only from an entry, so one is rebuilt from the three fields the cursor reads. + fn cursor(&self) -> HistoryCursor { + HistoryCursor::after(&Entry { + id: self.id, + query: String::new(), + driver_id: String::new(), + connection_id: Uuid::nil(), + connection_name: String::new(), + executed_at: from_seconds(self.executed_at_secs), + duration_ms: None, + rows_affected: None, + success: true, + cancelled: false, + pinned: self.pinned, + error: None, + }) + } +} + +impl From for HistoryEntryInfo { + fn from(entry: Entry) -> Self { + let outcome = match (entry.cancelled, entry.success) { + (true, _) => HistoryOutcomeTag::Cancelled, + (false, true) => HistoryOutcomeTag::Succeeded, + (false, false) => HistoryOutcomeTag::Failed { + message: entry.error.unwrap_or_default(), + }, + }; + Self { + id: entry.id, + sql: entry.query, + connection_id: (!entry.connection_id.is_nil()).then(|| entry.connection_id.to_string()), + connection_name: entry.connection_name, + driver_id: entry.driver_id, + executed_at_ms: millis(entry.executed_at), + duration_ms: entry.duration_ms, + rows_affected: entry.rows_affected, + outcome, + pinned: entry.pinned, + } + } +} + +impl From for Outcome { + fn from(outcome: HistoryOutcomeTag) -> Self { + match outcome { + HistoryOutcomeTag::Succeeded => Self::Success, + HistoryOutcomeTag::Failed { message } => Self::Error(message), + HistoryOutcomeTag::Cancelled => Self::Cancelled, + } + } +} + +// A time past what the clock holds lands on the epoch rather than panicking. +fn from_millis(millis: i64) -> SystemTime { + from_epoch(Duration::from_millis(millis.unsigned_abs()), millis < 0) +} + +fn from_seconds(seconds: i64) -> SystemTime { + from_epoch(Duration::from_secs(seconds.unsigned_abs()), seconds < 0) +} + +fn from_epoch(span: Duration, before: bool) -> SystemTime { + let time = match before { + true => UNIX_EPOCH.checked_sub(span), + false => UNIX_EPOCH.checked_add(span), + }; + time.unwrap_or(UNIX_EPOCH) +} + +fn millis(time: SystemTime) -> i64 { + match time.duration_since(UNIX_EPOCH) { + Ok(after) => i64::try_from(after.as_millis()).unwrap_or(i64::MAX), + Err(before) => i64::try_from(before.duration().as_millis()).map_or(i64::MIN, |value| -value), + } +} + +// As the store counts them, so a rebuilt cursor compares as the one it stands for. +fn seconds(time: SystemTime) -> i64 { + match time.duration_since(UNIX_EPOCH) { + Ok(after) => i64::try_from(after.as_secs()).unwrap_or(i64::MAX), + Err(before) => i64::try_from(before.duration().as_secs()).map_or(i64::MIN, |value| -value), + } +} + +impl std::fmt::Debug for QueryHistoryStore { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("QueryHistoryStore") + .field("path", &self.history.path()) + .finish_non_exhaustive() + } +} diff --git a/core/crates/ffi/src/lib.rs b/core/crates/ffi/src/lib.rs index 55034ff845..be55b26f6e 100644 --- a/core/crates/ffi/src/lib.rs +++ b/core/crates/ffi/src/lib.rs @@ -7,9 +7,13 @@ mod connection_url; mod core_handle; #[cfg(windows)] mod credential_manager; +mod definition; mod drivers; mod editor; mod error; +mod export; +mod filter; +mod history; mod host; mod host_key; mod library; @@ -21,6 +25,7 @@ mod runtime; mod safe_mode_gate; mod session; mod statement_at; +mod structure; mod syntax; pub mod window_abi; @@ -29,9 +34,17 @@ pub use catalog::{ColumnEntry, ObjectEntry, ObjectKindTag, ObjectScopeArg, Schem pub use cloud::{CloudConnectionFields, CloudGroupFields, CloudLibraryPull, CloudLibraryRecords}; pub use connection_url::{ParsedConnectionUrl, parse_connection_url}; pub use core_handle::{Core, FileOpenRequest}; +pub use definition::ObjectSource; pub use drivers::{DataModelTag, DriverInfo, EndpointInfo, EngineCategoryTag, ServerNameOverrideTag}; pub use editor::{QueryEditor, RunSettings}; pub use error::{BoxedErrorInfo, CoreError, ErrorInfo, HostKeyFailure, ServerMessage, TlsFailureTag}; +pub use export::ExportFormatTag; +pub use filter::{ + CombinatorTag, FilterChoices, FilterColumn, FilterOpTag, FilterRuleArg, FilterValueArg, TableFilterArg, +}; +pub use history::{ + HistoryCursorArg, HistoryEntryInfo, HistoryOutcomeTag, HistoryPageInfo, HistoryRun, QueryHistoryStore, +}; pub use host::{HostConfig, HostSecretError, HostSecretStore, HostSecrets, SecretItemTag, SecretKindTag}; pub use library::{ ConnectionLibrary, ConnectionRecord, ConnectionSecrets, GroupRecord, ImportSummary, LibrarySnapshot, SafeModeTag, @@ -45,8 +58,9 @@ pub use row_edits::{ }; 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 session::{ColumnSort, DatabaseSession, FailureCauseTag, SessionHealthTag, TablePageRead}; pub use statement_at::StatementSpan; +pub use structure::{StructureColumn, StructureForeignKey, StructureIndex, TableStructure}; use secrecy::{ExposeSecret, SecretString}; diff --git a/core/crates/ffi/src/session.rs b/core/crates/ffi/src/session.rs index ebc122001b..41c4c0b5a1 100644 --- a/core/crates/ffi/src/session.rs +++ b/core/crates/ffi/src/session.rs @@ -1,22 +1,28 @@ use std::sync::Arc; +use std::time::Instant; -use tablepro_core::EditorSessionOptions; -use tablepro_core::browse::{PageRequest, PagingMode, SortKey, build_count, build_page, paging_mode}; +use tablepro_core::browse::{PageRequest, PagingMode, SortKey, build_count, build_page, paging_mode, read_page}; use tablepro_core::filter::FilterSet; -use tablepro_core::meta::{PagePosition, TableRef}; +use tablepro_core::meta::{ObjectInfo, ObjectScope, PagePosition, RowIdentity, TableRef}; use tablepro_core::sql_syntax::{SortReference, sort_reference, with_order_by}; -use tablepro_session::runtime::Tasks; +use tablepro_core::{ByteLimit, ColumnInfo, EditorSessionOptions, QueryResult, ResultEvent, ResultSink, Truncation}; +use tablepro_session::runtime::{Tasks, catch_panic}; use tablepro_session::{FailureCause, MonitorSignal, OperationError, OperationKind, Session, SessionHealth}; +use tokio_util::sync::CancellationToken; use crate::cancel_token::{CancelToken, token}; -use crate::catalog::{ColumnEntry, ObjectEntry, ObjectScopeArg, SchemaEntry, TableName}; +use crate::catalog::{ColumnEntry, ObjectEntry, ObjectKindTag, ObjectScopeArg, SchemaEntry, TableName}; +use crate::definition::ObjectSource; use crate::editor::QueryEditor; use crate::error::{BoxedErrorInfo, CoreError}; +use crate::filter::{self, FilterChoices, TableFilterArg}; use crate::library::SafeModeTag; use crate::row_edits::TableEditor; +use crate::run::{QueryRun, RunListener, RunOutcome, RunSink, RunState, TransactionStateTag}; use crate::runtime::on_runtime; use crate::safe_mode_gate::{self, RunGate}; use crate::statement_at::{self, StatementSpan}; +use crate::structure::{self, TableStructure}; #[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Record)] pub struct ColumnSort { @@ -25,11 +31,35 @@ pub struct ColumnSort { } // Row-address columns may follow the visible ones. An engine that pages on the server cannot seek past page one. -#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] -pub struct TableQuery { - pub sql: String, - pub visible_columns: u32, - pub can_seek: bool, +#[derive(uniffi::Object)] +pub struct TablePageRead { + run: Arc, + visible_columns: u32, + can_seek: bool, +} + +#[uniffi::export] +impl TablePageRead { + pub fn run(&self) -> Arc { + self.run.clone() + } + + pub fn visible_columns(&self) -> u32 { + self.visible_columns + } + + pub fn can_seek(&self) -> bool { + self.can_seek + } +} + +impl std::fmt::Debug for TablePageRead { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TablePageRead") + .field("visible_columns", &self.visible_columns) + .field("can_seek", &self.can_seek) + .finish_non_exhaustive() + } } // How the session's connection is doing. A reconnect replaces it on its own; a failed one waits for reconnect(). @@ -156,83 +186,61 @@ impl DatabaseSession { .await } - // The page starts offset rows in, so the statement binds nothing and runs as editor text. - pub async fn table_query( + // What the filter bar offers on this table, read from the catalog's columns and the engine's dialect. + pub async fn filter_choices( + &self, + table: TableName, + cancel: Option>, + ) -> Result { + let session = self.session.clone(); + self.cancellable(cancel, async move { + let columns = session.columns(table.into()).await?; + let dialect = session.current_dialect().ok_or_else(not_connected)?; + Ok(filter::choices(&*dialect, &columns)) + }) + .await + } + + // Returns once the page is built and its read started, so a bad filter fails the call; rows come as run events. + #[expect( + clippy::too_many_arguments, + reason = "the page's table, sort, filter and window, the app's listener and its cancel, as the app passes them" + )] + pub async fn read_table_page( &self, table: TableName, sort: Option, + filter: Option, limit: u64, offset: u64, + listener: Box, cancel: Option>, - ) -> Result { + ) -> Result, CoreError> { let session = self.session.clone(); + let tasks = self.tasks.clone(); let cancel = token(cancel); on_runtime(&self.tasks, async move { - let table: TableRef = table.into(); - let columns = tokio::select! { - biased; - () = cancel.cancelled() => return Err(CoreError::Cancelled), - columns = session.columns(table.clone()) => columns?, - }; - let identity = session - .run_cancellable(OperationKind::Metadata, &cancel, |lease, options| { - let table = table.clone(); - async move { Ok(lease.row_identity(&table, options).await?) } - }) - .await?; let dialect = session.current_dialect().ok_or_else(not_connected)?; - let sort: Vec = sort - .map(|sort| SortKey { - column: sort.column as usize, - ascending: sort.ascending, - }) - .into_iter() - .collect(); - let can_seek = paging_mode(dialect.table_paging(), &identity, &sort) != PagingMode::ServerPaging; - if offset > 0 && !can_seek { - return Err(CoreError::failed( - "ffi.page_unsupported", - "the engine reads a table only from its start", - )); - } - let position = match offset { - 0 => PagePosition::First, - rows => PagePosition::Offset(rows), - }; - let spec = build_page( - &*dialect, - &PageRequest { - table: &table, - columns: &columns, - filter: &FilterSet::default(), - sort: &sort, - identity: &identity, - position: &position, - limit, - }, - ) - .map_err(OperationError::from)?; - if !spec.statement.params().is_empty() { - return Err(CoreError::failed( - "ffi.bound_statement", - "the table's first page binds values", - )); - } - Ok(TableQuery { - sql: spec.statement.sql().to_owned(), - visible_columns: u32::try_from(spec.visible_columns()).unwrap_or(u32::MAX), - can_seek, - }) + let filter = filter::filter_set(filter, &*dialect)?; + let plan = plan_page(&session, table, sort_keys(sort), filter, limit, offset, &cancel).await?; + Ok(Arc::new(start_page(session, &tasks, plan, listener))) }) .await } - // Every row of the table, counted by the server, for the pager's last page. - pub async fn table_row_count(&self, table: TableName, cancel: Option>) -> Result { + // Every row the filter selects, counted by the server, for the pager's last page. + pub async fn table_row_count( + &self, + table: TableName, + filter: Option, + cancel: Option>, + ) -> Result { let session = self.session.clone(); let cancel = token(cancel); on_runtime(&self.tasks, async move { let table: TableRef = table.into(); + let dialect = session.current_dialect().ok_or_else(not_connected)?; + let filter = filter::filter_set(filter, &*dialect)?; let columns = tokio::select! { biased; () = cancel.cancelled() => return Err(CoreError::Cancelled), @@ -242,9 +250,11 @@ impl DatabaseSession { .run_cancellable(OperationKind::Count, &cancel, |lease, options| { let table = table.clone(); let columns = columns.clone(); + let filter = filter.clone(); async move { - let statement = build_count(&*lease.dialect(), &table, &columns, &FilterSet::default()) - .map_err(OperationError::from)?; + // Built before the count runs, so a filter the engine cannot take never reaches it. + let statement = + build_count(&*lease.dialect(), &table, &columns, &filter).map_err(OperationError::from)?; Ok(lease.count_rows(&statement, options).await?) } }) @@ -253,6 +263,60 @@ impl DatabaseSession { .await } + // The table as the server defines it now. `kind` decides whether a CREATE script belongs: a view gets none. + pub async fn table_structure( + &self, + table: TableName, + kind: ObjectKindTag, + cancel: Option>, + ) -> Result { + let session = self.session.clone(); + let cancel = token(cancel); + on_runtime(&self.tasks, async move { + let table: TableRef = table.into(); + // The catalog's kind wins over the app's, which a session saved before it was kept does not carry. + let listed = tokio::select! { + biased; + () = cancel.cancelled() => return Err(CoreError::Cancelled), + listed = catalog_kind(&session, &table) => listed, + }; + let kind = listed.unwrap_or(kind); + // The script is written in the dialect of the connection that read the definition. + let (definition, dialect) = session + .run_cancellable(OperationKind::Metadata, &cancel, |lease, options| { + let table = table.clone(); + async move { + let definition = lease.fetch_table_definition(&table, options).await?; + Ok((definition, lease.dialect())) + } + }) + .await?; + Ok(structure::table_structure(&definition, &*dialect, kind)) + }) + .await + } + + // None when the object is gone or the login cannot read its text, which servers do not tell apart. + pub async fn object_definition( + &self, + object: ObjectEntry, + cancel: Option>, + ) -> Result, CoreError> { + let object = ObjectInfo::try_from(object)?; + let session = self.session.clone(); + let cancel = token(cancel); + on_runtime(&self.tasks, async move { + let definition = session + .run_cancellable(OperationKind::Metadata, &cancel, |lease, options| { + let object = object.clone(); + async move { Ok(lease.fetch_object_definition(&object, options).await?) } + }) + .await?; + Ok(definition.map(ObjectSource::from)) + }) + .await + } + pub fn health(&self) -> SessionHealthTag { health_tag(&self.session, &self.session.health().borrow()) } @@ -367,6 +431,14 @@ impl DatabaseSession { } impl DatabaseSession { + pub(crate) fn session(&self) -> &Arc { + &self.session + } + + pub(crate) fn tasks(&self) -> &Tasks { + &self.tasks + } + // A catalog read is shared, so a cancel abandons this caller's wait and leaves the read to finish for the others. async fn cancellable(&self, cancel: Option>, work: F) -> Result where @@ -409,10 +481,179 @@ fn health_tag(session: &Session, health: &SessionHealth) -> SessionHealthTag { } } +// The kind the catalog lists the table under, where the catalog has been read and holds it. +async fn catalog_kind(session: &Session, table: &TableRef) -> Option { + let scope = match &table.schema { + Some(schema) => ObjectScope::Schema(schema.clone()), + None => ObjectScope::DefaultSchema, + }; + let objects = session.objects(scope).await.ok()?; + objects + .iter() + .find(|object| object.name == table.name && object.kind.is_browsable()) + .map(|object| ObjectKindTag::from(object.kind)) +} + fn not_connected() -> CoreError { CoreError::failed("ffi.not_connected", "the session has no connection") } +pub(crate) fn sort_keys(sort: Option) -> Vec { + sort.map(|sort| SortKey { + column: usize::try_from(sort.column).unwrap_or(usize::MAX), + ascending: sort.ascending, + }) + .into_iter() + .collect() +} + +// The read builds its SQL again from its own lease, so it is spelled for that server even after a reconnect. +struct PagePlan { + table: TableRef, + columns: Arc<[ColumnInfo]>, + identity: RowIdentity, + filter: FilterSet, + sort: Vec, + position: PagePosition, + limit: u64, + visible_columns: u32, + can_seek: bool, +} + +async fn plan_page( + session: &Session, + table: TableName, + sort: Vec, + filter: FilterSet, + limit: u64, + offset: u64, + cancel: &CancellationToken, +) -> Result { + let table: TableRef = table.into(); + let columns = tokio::select! { + biased; + () = cancel.cancelled() => return Err(CoreError::Cancelled), + columns = session.columns(table.clone()) => columns?, + }; + let identity = session + .run_cancellable(OperationKind::Metadata, cancel, |lease, options| { + let table = table.clone(); + async move { Ok(lease.row_identity(&table, options).await?) } + }) + .await?; + let dialect = session.current_dialect().ok_or_else(not_connected)?; + let can_seek = paging_mode(dialect.table_paging(), &identity, &sort) != PagingMode::ServerPaging; + if offset > 0 && !can_seek { + return Err(CoreError::failed( + "ffi.page_unsupported", + "the engine reads a table only from its start", + )); + } + let position = match offset { + 0 => PagePosition::First, + rows => PagePosition::Offset(rows), + }; + // Built once here, so a filter the engine cannot take fails the call and never reaches the server. + let spec = build_page( + &*dialect, + &PageRequest { + table: &table, + columns: &columns, + filter: &filter, + sort: &sort, + identity: &identity, + position: &position, + limit, + }, + ) + .map_err(OperationError::from)?; + Ok(PagePlan { + visible_columns: u32::try_from(spec.visible_columns()).unwrap_or(u32::MAX), + can_seek, + table, + columns, + identity, + filter, + sort, + position, + limit, + }) +} + +fn start_page(session: Arc, tasks: &Tasks, plan: PagePlan, listener: Box) -> TablePageRead { + let state = Arc::new(RunState::new(listener)); + let visible_columns = plan.visible_columns; + let can_seek = plan.can_seek; + let run = state.clone(); + drop(tasks.spawn_task(async move { + let started = Instant::now(); + let outcome = match catch_panic(read_into(&session, &plan, &run)).await { + Ok(Ok(())) => RunOutcome::Completed { + elapsed_ms: u64::try_from(started.elapsed().as_millis()).unwrap_or(u64::MAX), + }, + Ok(Err(error)) => RunOutcome::from(CoreError::from(error)), + Err(panic) => RunOutcome::from(CoreError::failed( + "ffi.panicked", + format!("the page read panicked: {panic}"), + )), + }; + // A pooled connection reads the page, so no transaction of the user's is left open. + run.finish(outcome, TransactionStateTag::Idle); + })); + TablePageRead { + run: Arc::new(QueryRun::new(state, tasks.clone())), + visible_columns, + can_seek, + } +} + +// The run's cancel reaches the driver through the operation, so a cancel stops the statement on the server. +async fn read_into(session: &Session, plan: &PagePlan, state: &Arc) -> Result<(), OperationError> { + let read = session + .run_cancellable(OperationKind::Page, &state.cancel, move |lease, options| async move { + let request = PageRequest { + table: &plan.table, + columns: &plan.columns, + filter: &plan.filter, + sort: &plan.sort, + identity: &plan.identity, + position: &plan.position, + limit: plan.limit, + }; + read_page::(&*lease, &request, &options).await + }) + .await?; + // An engine that pages on the server hands back where more rows start, and only page one can be read here. + let truncation = match read.result.truncated || (read.mode == PagingMode::ServerPaging && read.next.is_some()) { + true => Truncation::ReadPastLimit, + false => Truncation::Complete, + }; + deliver( + RunSink::new(state.clone(), ByteLimit::EDITOR_DEFAULT), + read.result, + truncation, + ) + .await; + Ok(()) +} + +async fn deliver(mut sink: RunSink, result: QueryResult, truncation: Truncation) { + let retained = u64::try_from(result.rows.len()).unwrap_or(u64::MAX); + sink.accept(ResultEvent::ResultSetStarted { + columns: result.columns, + }) + .await; + if !result.rows.is_empty() { + sink.accept(ResultEvent::Rows(result.rows)).await; + } + sink.accept(ResultEvent::ResultSetFinished { + retained, + total_rows: None, + truncation, + }) + .await; +} + impl std::fmt::Debug for DatabaseSession { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("DatabaseSession") diff --git a/core/crates/ffi/src/structure.rs b/core/crates/ffi/src/structure.rs new file mode 100644 index 0000000000..2b0fdd98c5 --- /dev/null +++ b/core/crates/ffi/src/structure.rs @@ -0,0 +1,195 @@ +use tablepro_core::ForeignKeyInfo; +use tablepro_core::column::ColumnDefault; +use tablepro_core::ddl::{ColumnDefinition, ReferentialAction, TableDefinition, default_text}; +use tablepro_core::dialect::SqlDialect; +use tablepro_core::meta::{IndexInfo, IndexKey}; + +use crate::catalog::ObjectKindTag; + +// `ddl` is the engine's CREATE script for a table; None for a view, or where the engine writes none. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct TableStructure { + pub columns: Vec, + pub indexes: Vec, + pub foreign_keys: Vec, + pub ddl: Option, +} + +// `default_value` is SQL the user could type back in; None when the column has no default. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct StructureColumn { + pub name: String, + pub type_name: String, + pub nullable: bool, + pub default_value: Option, + pub primary_key: bool, + pub auto_increment: bool, + pub generated: bool, + pub comment: Option, +} + +// Each key is a column name or an expression, with " DESC" after a descending one. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct StructureIndex { + pub name: String, + pub keys: Vec, + pub unique: bool, + pub primary: bool, + pub predicate: Option, +} + +// An action is None where the server spelled one this build does not know. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct StructureForeignKey { + pub name: String, + pub columns: Vec, + pub ref_schema: Option, + pub ref_table: String, + pub ref_columns: Vec, + pub on_delete: Option, + pub on_update: Option, +} + +pub(crate) fn table_structure( + definition: &TableDefinition, + dialect: &dyn SqlDialect, + kind: ObjectKindTag, +) -> TableStructure { + TableStructure { + columns: definition + .columns + .iter() + .map(|column| structure_column(column, dialect)) + .collect(), + indexes: definition.indexes.iter().map(StructureIndex::from).collect(), + foreign_keys: definition.foreign_keys.iter().map(StructureForeignKey::from).collect(), + ddl: match kind { + ObjectKindTag::Table | ObjectKindTag::PartitionedTable => create_script(definition, dialect), + // Some engines would build a CREATE TABLE from a view's columns, which is not how the view was made. + _ => None, + }, + } +} + +fn structure_column(column: &ColumnDefinition, dialect: &dyn SqlDialect) -> StructureColumn { + let info = &column.info; + StructureColumn { + name: info.name.clone(), + type_name: info.column_type.name.clone(), + nullable: info.nullable, + default_value: match &info.default { + ColumnDefault::None => None, + default => Some(default_text(dialect, default, &info.column_type)), + }, + primary_key: info.primary_key, + auto_increment: info.is_auto_increment, + generated: info.is_generated || column.attributes.generated.is_some(), + comment: info.comment.clone(), + } +} + +fn create_script(definition: &TableDefinition, dialect: &dyn SqlDialect) -> Option { + if definition.columns.is_empty() { + return None; + } + let script = match dialect.ddl()?.create_table_script(definition) { + Ok(script) => script, + Err(error) => { + tracing::warn!(%error, "the table's CREATE script could not be built"); + return None; + } + }; + (!script.is_empty()).then(|| script.preview()) +} + +impl From<&IndexInfo> for StructureIndex { + fn from(index: &IndexInfo) -> Self { + Self { + name: index.name.clone(), + keys: index.keys.iter().map(key_text).collect(), + unique: index.unique, + primary: index.primary, + predicate: index.predicate.clone(), + } + } +} + +fn key_text(key: &IndexKey) -> String { + let text = match key { + IndexKey::Column { name, .. } => name, + IndexKey::Expression { sql, .. } => sql, + }; + match key.descending() { + true => format!("{text} DESC"), + false => text.clone(), + } +} + +impl From<&ForeignKeyInfo> for StructureForeignKey { + fn from(key: &ForeignKeyInfo) -> Self { + Self { + name: key.name.clone(), + columns: key.columns.clone(), + ref_schema: key.ref_schema.clone(), + ref_table: key.ref_table.clone(), + ref_columns: key.ref_columns.clone(), + on_delete: key.on_delete.map(action_text), + on_update: key.on_update.map(action_text), + } + } +} + +fn action_text(action: ReferentialAction) -> String { + action.as_sql().to_owned() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_descending_key_says_so_after_its_column_or_expression() { + let index = IndexInfo { + name: "recent".to_owned(), + keys: vec![ + IndexKey::Column { + name: "created_at".to_owned(), + descending: true, + }, + IndexKey::Expression { + sql: "lower(email)".to_owned(), + descending: false, + }, + ], + unique: true, + primary: false, + predicate: Some("deleted_at IS NULL".to_owned()), + definition: None, + }; + + let shown = StructureIndex::from(&index); + + assert_eq!(shown.keys, ["created_at DESC", "lower(email)"]); + assert!(shown.unique); + assert_eq!(shown.predicate.as_deref(), Some("deleted_at IS NULL")); + } + + #[test] + fn a_foreign_key_names_its_actions_as_sql_and_leaves_an_unknown_one_out() { + let key = ForeignKeyInfo { + name: "orders_customer".to_owned(), + columns: vec!["customer_id".to_owned()], + ref_schema: Some("public".to_owned()), + ref_table: "customers".to_owned(), + ref_columns: vec!["id".to_owned()], + on_delete: Some(ReferentialAction::SetNull), + on_update: None, + }; + + let shown = StructureForeignKey::from(&key); + + assert_eq!(shown.on_delete.as_deref(), Some("SET NULL")); + assert_eq!(shown.on_update, None); + assert_eq!(shown.ref_columns, ["id"]); + } +} diff --git a/core/crates/ffi/tests/browse.rs b/core/crates/ffi/tests/browse.rs new file mode 100644 index 0000000000..cf6912bf32 --- /dev/null +++ b/core/crates/ffi/tests/browse.rs @@ -0,0 +1,497 @@ +#![cfg(feature = "engines")] +#![expect( + clippy::unwrap_used, + reason = "the setup helpers fail a test the way the test functions themselves do" +)] + +use std::path::Path; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +use secrecy::SecretString; +use tablepro_ffi::{ + ColumnSort, CombinatorTag, Core, CoreError, DatabaseSession, FileOpenRequest, FilterOpTag, FilterRuleArg, + FilterValueArg, HostConfig, HostSecretError, HostSecrets, ObjectEntry, ObjectKindTag, ObjectScopeArg, RunEvent, + RunListener, RunOutcome, RunSettings, SecretItemTag, SecretKindTag, TableFilterArg, TableName, TablePageRead, + TransactionStateTag, TruncationTag, +}; + +struct NoSecrets; + +impl HostSecrets for NoSecrets { + fn load(&self, _: String, _: SecretKindTag) -> Result, HostSecretError> { + Ok(None) + } + + fn store(&self, _: String, _: SecretKindTag, _: SecretString, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn relabel(&self, _: String, _: SecretKindTag, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete(&self, _: String, _: SecretKindTag) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete_connection(&self, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn load_item(&self, _: SecretItemTag) -> Result, HostSecretError> { + Ok(None) + } + + fn store_item(&self, _: SecretItemTag, _: SecretString) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete_item(&self, _: SecretItemTag) -> Result<(), HostSecretError> { + Ok(()) + } +} + +struct Quiet; + +impl RunListener for Quiet { + fn events_pending(&self) {} +} + +const PEOPLE: &str = "CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER); \ + INSERT INTO people (id, name, age) VALUES (1, 'Ada', 36), (2, 'Grace', 45), (3, 'Linus', 21), \ + (4, 'Edsger', 72), (5, 'Barbara', 50), (6, '50% off', NULL), (7, '500 units', NULL)"; + +const LEAGUE: &str = "CREATE TABLE teams (id INTEGER PRIMARY KEY, name TEXT NOT NULL); \ + CREATE TABLE players (id INTEGER PRIMARY KEY, team_id INTEGER REFERENCES teams (id) ON DELETE CASCADE, \ + name TEXT NOT NULL DEFAULT 'anon', score REAL); \ + CREATE INDEX players_by_score ON players (score DESC, name); \ + CREATE VIEW top_players AS SELECT name, score FROM players; \ + CREATE TRIGGER players_scored AFTER INSERT ON players \ + BEGIN UPDATE players SET score = 0 WHERE id = new.id AND score IS NULL; END"; + +async fn fixture(dir: &Path, sql: &str) -> (Arc, Arc) { + let core = Core::new( + HostConfig { + client_version: "test".to_owned(), + storage_dir: None, + }, + Box::new(NoSecrets), + ) + .unwrap(); + let request = FileOpenRequest { + path: dir.join("browse.sqlite").display().to_string(), + driver_id: None, + read_only: false, + create: true, + }; + let session = core.open_file(request, None).await.unwrap(); + let editor = session.open_editor(None).await.unwrap(); + let run = editor.run(sql.to_owned(), RunSettings::default(), Box::new(Quiet)); + let outcome = run.outcome().await.unwrap(); + assert!(matches!(outcome, RunOutcome::Completed { .. }), "{outcome:?}"); + editor.close().await.unwrap(); + (core, session) +} + +fn table(name: &str) -> TableName { + TableName { + schema: None, + name: name.to_owned(), + } +} + +fn rule(column: &str, op: FilterOpTag, value: Option<&str>) -> FilterRuleArg { + FilterRuleArg { + column: column.to_owned(), + op, + value: value.map(|value| FilterValueArg::Single { + value: value.to_owned(), + }), + } +} + +fn all(rules: Vec) -> Option { + Some(TableFilterArg { + combinator: CombinatorTag::And, + rules, + raw_sql: None, + }) +} + +fn raw(sql: &str) -> Option { + Some(TableFilterArg { + combinator: CombinatorTag::And, + rules: Vec::new(), + raw_sql: Some(sql.to_owned()), + }) +} + +async fn read( + session: &DatabaseSession, + sort: Option, + filter: Option, + limit: u64, + offset: u64, +) -> Result, CoreError> { + session + .read_table_page(table("people"), sort, filter, limit, offset, Box::new(Quiet), None) + .await +} + +// The page's texts in one column, once its read has finished. +async fn column(page: &TablePageRead, column: u32) -> Vec> { + let run = page.run(); + let outcome = run.outcome().await.unwrap(); + assert!(matches!(outcome, RunOutcome::Completed { .. }), "{outcome:?}"); + let set = run.result_set(0).unwrap(); + (0..set.row_count()) + .map(|row| set.cell_text(row, column).unwrap()) + .collect() +} + +fn texts(values: &[&str]) -> Vec> { + values.iter().map(|value| Some((*value).to_owned())).collect() +} + +#[tokio::test] +async fn a_filtered_page_reads_only_the_matching_rows_and_counts_them() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path(), PEOPLE).await; + let older = all(vec![rule("age", FilterOpTag::Gt, Some("40"))]); + + let page = read(&session, None, older.clone(), 100, 0).await.unwrap(); + let run = page.run(); + let outcome = run.outcome().await.unwrap(); + let events = run.drain_events(); + let ids = column(&page, 0).await; + let count = session.table_row_count(table("people"), older, None).await.unwrap(); + let everyone = session.table_row_count(table("people"), None, None).await.unwrap(); + session.close().await.unwrap(); + + assert!(matches!(outcome, RunOutcome::Completed { .. }), "{outcome:?}"); + assert_eq!((page.visible_columns(), page.can_seek()), (3, true)); + assert_eq!(ids, texts(&["2", "4", "5"])); + assert_eq!((count, everyone), (3, 7)); + assert!( + matches!(&events[0], RunEvent::ResultSetStarted { set_index: 0, columns } if columns.len() == 3), + "{events:?}" + ); + assert_eq!( + events[1..], + [ + RunEvent::RowsAppended { + set_index: 0, + row_count: 3 + }, + RunEvent::ResultSetFinished { + set_index: 0, + row_count: 3, + total_rows: None, + truncation: TruncationTag::Complete, + }, + RunEvent::Finished { + outcome, + transaction: TransactionStateTag::Idle, + }, + ] + ); +} + +#[tokio::test] +async fn contains_finds_a_percent_sign_as_typed_and_like_keeps_the_users_wildcards() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path(), PEOPLE).await; + + let contains = read( + &session, + None, + all(vec![rule("name", FilterOpTag::Contains, Some("50%"))]), + 100, + 0, + ) + .await + .unwrap(); + let like = read( + &session, + None, + all(vec![rule("name", FilterOpTag::Like, Some("50%"))]), + 100, + 0, + ) + .await + .unwrap(); + let contained = column(&contains, 1).await; + let liked = column(&like, 1).await; + session.close().await.unwrap(); + + assert_eq!(contained, texts(&["50% off"])); + assert_eq!(liked, texts(&["50% off", "500 units"])); +} + +#[tokio::test] +async fn a_filter_on_a_column_the_table_lacks_is_refused_before_any_read() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path(), PEOPLE).await; + let salary = all(vec![rule("salary", FilterOpTag::Gt, Some("1000"))]); + + let refused = read(&session, None, salary.clone(), 100, 0).await.unwrap_err(); + let uncounted = session + .table_row_count(table("people"), salary, None) + .await + .unwrap_err(); + session.close().await.unwrap(); + + // The call fails itself: no run was started, so nothing went to the server. + assert_eq!(refused.code(), "filter.unknown_column"); + assert!(refused.to_string().contains("salary"), "{refused}"); + assert_eq!(uncounted.code(), "filter.unknown_column"); +} + +#[tokio::test] +async fn a_raw_predicate_of_two_statements_is_refused_and_the_table_stays() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path(), PEOPLE).await; + + let refused = read(&session, None, raw("1=1; DROP TABLE people"), 100, 0) + .await + .unwrap_err(); + let uncounted = session + .table_row_count(table("people"), raw("1=1; DROP TABLE people"), None) + .await + .unwrap_err(); + let count = session.table_row_count(table("people"), None, None).await.unwrap(); + let long_names = read(&session, None, raw("length(name) > 6"), 100, 0).await.unwrap(); + let ids = column(&long_names, 0).await; + session.close().await.unwrap(); + + assert_eq!(refused.code(), "filter.raw_predicate"); + assert_eq!(uncounted.code(), "filter.raw_predicate"); + assert_eq!(count, 7); + assert_eq!(ids, texts(&["5", "6", "7"])); +} + +#[tokio::test] +async fn a_value_the_column_cannot_hold_is_refused() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path(), PEOPLE).await; + + let refused = read( + &session, + None, + all(vec![rule("age", FilterOpTag::Eq, Some("forty"))]), + 100, + 0, + ) + .await + .unwrap_err(); + let missing = read(&session, None, all(vec![rule("age", FilterOpTag::Eq, None)]), 100, 0) + .await + .unwrap_err(); + session.close().await.unwrap(); + + assert_eq!(refused.code(), "edit.invalid_integer"); + assert!(refused.to_string().contains("age"), "{refused}"); + assert_eq!(missing.code(), "filter.missing_value"); +} + +#[tokio::test] +async fn a_filtered_page_sorts_and_pages_past_its_first_rows() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path(), PEOPLE).await; + let with_age = || all(vec![rule("age", FilterOpTag::IsNotNull, None)]); + let by_name = Some(ColumnSort { + column: 1, + ascending: true, + }); + let oldest_first = Some(ColumnSort { + column: 2, + ascending: false, + }); + + let sorted = read( + &session, + oldest_first, + all(vec![rule("age", FilterOpTag::Lt, Some("50"))]), + 100, + 0, + ) + .await + .unwrap(); + let second_by_name = read(&session, by_name, with_age(), 2, 2).await.unwrap(); + let past_three = read(&session, None, with_age(), 2, 3).await.unwrap(); + let names = column(&sorted, 1).await; + let second_names = column(&second_by_name, 1).await; + let ids = column(&past_three, 0).await; + session.close().await.unwrap(); + + assert_eq!(names, texts(&["Grace", "Ada", "Linus"])); + assert_eq!(second_names, texts(&["Edsger", "Grace"])); + assert_eq!(ids, texts(&["4", "5"])); +} + +#[tokio::test] +async fn a_cancel_stops_a_page_read_on_the_engine() { + let dir = tempfile::tempdir().unwrap(); + let endless = "CREATE VIEW endless AS \ + WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c) SELECT count(*) AS n FROM c"; + let (_core, session) = fixture(dir.path(), &format!("{PEOPLE}; {endless}")).await; + + let page = session + .read_table_page(table("endless"), None, None, 100, 0, Box::new(Quiet), None) + .await + .unwrap(); + let run = page.run(); + tokio::time::sleep(Duration::from_millis(200)).await; + let asked = Instant::now(); + run.cancel(); + let outcome = run.outcome().await.unwrap(); + let stopped = asked.elapsed(); + let after = session.table_row_count(table("people"), None, None).await.unwrap(); + session.close().await.unwrap(); + + assert_eq!(outcome, RunOutcome::Cancelled); + assert!(stopped < Duration::from_secs(5), "{stopped:?}"); + assert_eq!(after, 7); +} + +#[tokio::test] +async fn the_choices_are_what_the_engine_evaluates_on_each_column() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path(), PEOPLE).await; + + let choices = session.filter_choices(table("people"), None).await.unwrap(); + session.close().await.unwrap(); + + let names: Vec<&str> = choices.columns.iter().map(|column| column.name.as_str()).collect(); + assert_eq!(names, ["id", "name", "age"]); + assert!(choices.columns[0].operators.contains(&FilterOpTag::Between)); + assert!(!choices.columns[0].operators.contains(&FilterOpTag::Contains)); + assert!(choices.columns[1].operators.contains(&FilterOpTag::Contains)); + assert_eq!(choices.combinators, [CombinatorTag::And, CombinatorTag::Or]); + assert!(choices.accepts_raw_sql); +} + +#[tokio::test] +async fn a_tables_structure_lists_its_columns_index_foreign_key_and_create_text() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path(), LEAGUE).await; + + let players = session + .table_structure(table("players"), ObjectKindTag::Table, None) + .await + .unwrap(); + let view = session + .table_structure(table("top_players"), ObjectKindTag::View, None) + .await + .unwrap(); + // A tab restored from a session saved before tabs kept their kind says Table for every object. The fixture + // made its objects after the session read the catalog, so it is read again, as the tree reads it. + session + .refresh_objects(ObjectScopeArg::DefaultSchema, None) + .await + .unwrap(); + let view_as_table = session + .table_structure(table("top_players"), ObjectKindTag::Table, None) + .await + .unwrap(); + let in_schema = TableName { + schema: Some("main".to_owned()), + name: "top_players".to_owned(), + }; + let view_in_schema = session + .table_structure(in_schema, ObjectKindTag::Table, None) + .await + .unwrap(); + session.close().await.unwrap(); + + let names: Vec<&str> = players.columns.iter().map(|column| column.name.as_str()).collect(); + assert_eq!(names, ["id", "team_id", "name", "score"]); + assert!(players.columns[0].primary_key); + assert!(!players.columns[2].nullable); + assert_eq!(players.columns[2].default_value.as_deref(), Some("'anon'")); + assert_eq!(players.columns[3].default_value, None); + let index = players + .indexes + .iter() + .find(|index| index.name == "players_by_score") + .unwrap(); + assert_eq!(index.keys, ["score DESC", "name"]); + assert!(!index.unique && !index.primary); + let [key] = players.foreign_keys.as_slice() else { + unreachable!("one foreign key: {:?}", players.foreign_keys) + }; + assert_eq!(key.columns, ["team_id"]); + assert_eq!(key.ref_table, "teams"); + assert_eq!(key.ref_columns, ["id"]); + assert_eq!(key.on_delete.as_deref(), Some("CASCADE")); + let ddl = players.ddl.unwrap(); + assert!(ddl.contains("CREATE TABLE"), "{ddl}"); + assert!(ddl.contains("players_by_score"), "{ddl}"); + let view_names: Vec<&str> = view.columns.iter().map(|column| column.name.as_str()).collect(); + assert_eq!(view_names, ["name", "score"]); + assert_eq!(view.ddl, None, "a view is not made by CREATE TABLE"); + assert_eq!(view_as_table.ddl, None, "the catalog's kind wins over the app's"); + assert_eq!( + view_in_schema.ddl, None, + "a schema the table names is the catalog's scope" + ); +} + +#[tokio::test] +async fn a_trigger_reads_back_and_a_function_is_unsupported_on_sqlite() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path(), LEAGUE).await; + let entry = |name: &str, kind| ObjectEntry { + schema: None, + name: name.to_owned(), + kind, + parent_table: None, + }; + + let objects = session + .refresh_objects(ObjectScopeArg::DefaultSchema, None) + .await + .unwrap(); + let trigger = objects + .iter() + .find(|object| object.kind == ObjectKindTag::Trigger) + .unwrap() + .clone(); + let source = session.object_definition(trigger.clone(), None).await.unwrap().unwrap(); + let function = session + .object_definition(entry("total", ObjectKindTag::Function), None) + .await + .unwrap_err(); + let key = session + .object_definition(entry("total", ObjectKindTag::Key), None) + .await + .unwrap_err(); + session.close().await.unwrap(); + + assert_eq!(trigger.name, "players_scored"); + assert_eq!(trigger.parent_table.as_deref(), Some("players")); + assert!(source.sql.contains("CREATE TRIGGER"), "{}", source.sql); + assert!(source.sql.contains("players_scored"), "{}", source.sql); + assert!(source.opens_in_editor); + assert_eq!(source.database, None); + let CoreError::Failed { info } = function else { + unreachable!("a function was read: {function:?}") + }; + assert_eq!(info.codes, ["operation.driver", "driver.unsupported"]); + assert_eq!(key.code(), "ffi.no_definition"); +} + +#[tokio::test] +async fn an_editor_moves_to_a_database_only_where_the_engine_switches_one() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path(), PEOPLE).await; + let editor = session.open_editor(None).await.unwrap(); + + // SQLite binds its database to the file it opened, so a definition from elsewhere cannot run there. + let refused = editor.use_database("shop".to_owned(), None).await.unwrap_err(); + editor.close().await.unwrap(); + session.close().await.unwrap(); + + assert_eq!(refused.code(), "driver.unsupported"); +} diff --git a/core/crates/ffi/tests/export.rs b/core/crates/ffi/tests/export.rs new file mode 100644 index 0000000000..af7535cc0a --- /dev/null +++ b/core/crates/ffi/tests/export.rs @@ -0,0 +1,406 @@ +#![cfg(feature = "engines")] +#![expect( + clippy::unwrap_used, + reason = "the setup helpers fail a test the way the test functions themselves do" +)] + +use std::path::Path; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +use secrecy::SecretString; +use tablepro_ffi::{ + CancelToken, ColumnSort, CombinatorTag, Core, CoreError, DatabaseSession, ExportFormatTag, FileOpenRequest, + FilterOpTag, FilterRuleArg, FilterValueArg, HostConfig, HostSecretError, HostSecrets, ResultSet, RunListener, + RunOutcome, RunSettings, SecretItemTag, SecretKindTag, TableFilterArg, TableName, +}; + +struct NoSecrets; + +impl HostSecrets for NoSecrets { + fn load(&self, _: String, _: SecretKindTag) -> Result, HostSecretError> { + Ok(None) + } + + fn store(&self, _: String, _: SecretKindTag, _: SecretString, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn relabel(&self, _: String, _: SecretKindTag, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete(&self, _: String, _: SecretKindTag) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete_connection(&self, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn load_item(&self, _: SecretItemTag) -> Result, HostSecretError> { + Ok(None) + } + + fn store_item(&self, _: SecretItemTag, _: SecretString) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete_item(&self, _: SecretItemTag) -> Result<(), HostSecretError> { + Ok(()) + } +} + +struct Quiet; + +impl RunListener for Quiet { + fn events_pending(&self) {} +} + +fn table(name: &str) -> TableName { + TableName { + schema: None, + name: name.to_owned(), + } +} + +async fn run(session: &DatabaseSession, sql: &str) -> Option> { + let editor = session.open_editor(None).await.unwrap(); + let run = editor.run(sql.to_owned(), RunSettings::default(), Box::new(Quiet)); + let outcome = run.outcome().await.unwrap(); + assert!(matches!(outcome, RunOutcome::Completed { .. }), "{outcome:?}"); + let set = run.result_set(0); + editor.close().await.unwrap(); + set +} + +async fn fixture(dir: &Path) -> (Arc, Arc) { + let core = Core::new( + HostConfig { + client_version: "test".to_owned(), + storage_dir: None, + }, + Box::new(NoSecrets), + ) + .unwrap(); + let request = FileOpenRequest { + path: dir.join("app.sqlite").display().to_string(), + driver_id: None, + read_only: false, + create: true, + }; + let session = core.open_file(request, None).await.unwrap(); + run( + &session, + "CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT NOT NULL, score REAL, note TEXT); \ + INSERT INTO people VALUES (1, 'Ada', 1.5, NULL), (2, 'Grace, \"Amazing\"', 2.25, 'line one\nline two')", + ) + .await; + (core, session) +} + +fn text(path: &Path) -> String { + std::fs::read_to_string(path).unwrap() +} + +fn named(dir: &Path, part: &str) -> Vec { + let mut names: Vec = std::fs::read_dir(dir) + .unwrap() + .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()) + .filter(|name| name.contains(part)) + .collect(); + names.sort(); + names +} + +fn spelled(path: &Path) -> String { + path.display().to_string() +} + +#[tokio::test] +async fn csv_and_json_of_a_result_set_hold_exactly_its_rows() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + let set = run(&session, "SELECT id, name, score, note FROM people ORDER BY id") + .await + .unwrap(); + let csv = dir.path().join("people.csv"); + let json = dir.path().join("people.json"); + std::fs::write(&csv, "an older export").unwrap(); + + let csv_rows = session + .export_rows(set.clone(), 4, ExportFormatTag::Csv, None, spelled(&csv), None) + .await + .unwrap(); + let json_rows = session + .export_rows(set, 4, ExportFormatTag::Json, None, spelled(&json), None) + .await + .unwrap(); + + assert_eq!((csv_rows, json_rows), (2, 2)); + assert_eq!( + text(&csv), + "id,name,score,note\n1,Ada,1.5,\n2,\"Grace, \"\"Amazing\"\"\",2.25,\"line one\nline two\"\n" + ); + assert_eq!( + text(&json), + concat!( + "[\n", + r#"{"id":1,"name":"Ada","score":1.5,"note":null}"#, + ",\n", + r#"{"id":2,"name":"Grace, \"Amazing\"","score":2.25,"note":"line one\nline two"}"#, + "\n]" + ) + ); + assert_eq!(named(dir.path(), "people."), ["people.csv", "people.json"]); +} + +#[tokio::test] +async fn an_excel_export_is_a_workbook() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + let set = run(&session, "SELECT id, name, score, note FROM people ORDER BY id") + .await + .unwrap(); + let path = dir.path().join("people.xlsx"); + + let rows = session + .export_rows(set, 4, ExportFormatTag::Excel, None, spelled(&path), None) + .await + .unwrap(); + + assert_eq!(rows, 2); + assert!( + std::fs::read(&path).unwrap().starts_with(b"PK"), + "a workbook is a zip archive" + ); +} + +#[tokio::test] +async fn a_page_exports_only_its_visible_columns() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + run( + &session, + "CREATE TABLE notes (body TEXT); INSERT INTO notes VALUES ('first'), ('second')", + ) + .await; + let page = session + .read_table_page(table("notes"), None, None, 100, 0, Box::new(Quiet), None) + .await + .unwrap(); + let read = page.run(); + let outcome = read.outcome().await.unwrap(); + assert!(matches!(outcome, RunOutcome::Completed { .. }), "{outcome:?}"); + let set = read.result_set(0).unwrap(); + let visible = page.visible_columns(); + assert!( + set.columns().len() > visible as usize, + "a table with no key pages by its row address" + ); + let path = dir.path().join("notes.csv"); + + let rows = session + .export_rows( + set, + visible, + ExportFormatTag::Csv, + Some(table("notes")), + spelled(&path), + None, + ) + .await + .unwrap(); + + assert_eq!(rows, 2); + assert_eq!(text(&path), "body\nfirst\nsecond\n"); +} + +#[tokio::test] +async fn sql_insert_of_a_table_names_it_and_leaves_out_what_the_server_computes() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + run( + &session, + "CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT NOT NULL, \ + loud TEXT GENERATED ALWAYS AS (upper(name)) VIRTUAL); \ + INSERT INTO items (name) VALUES ('pen'), ('ink')", + ) + .await; + let path = dir.path().join("items.sql"); + + let rows = session + .export_table( + table("items"), + None, + None, + ExportFormatTag::SqlInsert, + spelled(&path), + None, + ) + .await + .unwrap(); + + assert_eq!(rows, 2); + assert_eq!( + text(&path), + "INSERT INTO \"items\" (\"id\", \"name\") VALUES (1, 'pen');\n\ + INSERT INTO \"items\" (\"id\", \"name\") VALUES (2, 'ink');\n" + ); +} + +#[tokio::test] +async fn sql_insert_without_a_table_is_refused_before_any_file() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + let set = run(&session, "SELECT id, name FROM people").await.unwrap(); + let path = dir.path().join("people.sql"); + + let error = session + .export_rows(set, 2, ExportFormatTag::SqlInsert, None, spelled(&path), None) + .await + .unwrap_err(); + + assert_eq!(error.code(), "ffi.export_needs_table"); + assert_eq!(named(dir.path(), "people.sql"), Vec::::new()); +} + +#[tokio::test] +async fn a_table_export_writes_every_row_the_filter_selects_in_the_sort_order() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + run( + &session, + "CREATE TABLE numbers (n INTEGER PRIMARY KEY, label TEXT); \ + WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c WHERE x < 3000) \ + INSERT INTO numbers (n, label) SELECT x, 'n' || x FROM c", + ) + .await; + let filter = TableFilterArg { + combinator: CombinatorTag::And, + rules: vec![FilterRuleArg { + column: "n".to_owned(), + op: FilterOpTag::Gt, + value: Some(FilterValueArg::Single { + value: "1000".to_owned(), + }), + }], + raw_sql: None, + }; + let newest_first = ColumnSort { + column: 0, + ascending: false, + }; + let path = dir.path().join("numbers.csv"); + + let rows = session + .export_table( + table("numbers"), + Some(newest_first), + Some(filter), + ExportFormatTag::Csv, + spelled(&path), + None, + ) + .await + .unwrap(); + + let written = text(&path); + let lines: Vec<&str> = written.lines().collect(); + assert_eq!(rows, 2_000); + assert_eq!(lines.len(), 2_001); + assert_eq!(lines[0], "n,label"); + assert_eq!(lines[1], "3000,n3000"); + assert_eq!(lines[2_000], "1001,n1001"); +} + +#[tokio::test] +async fn a_cancelled_export_leaves_no_file_and_no_part_file() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + run( + &session, + "CREATE VIEW endless AS WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c) SELECT x FROM c", + ) + .await; + let path = spelled(&dir.path().join("endless.csv")); + let cancel = CancelToken::new(); + let export = session.export_table( + table("endless"), + None, + None, + ExportFormatTag::Csv, + path, + Some(cancel.clone()), + ); + // Cancelled once its part file is on disk, so the cancel lands mid-write. + let watch = async { + let started = Instant::now(); + while named(dir.path(), "endless.csv").is_empty() { + assert!(started.elapsed() < Duration::from_secs(10), "the export never wrote"); + tokio::time::sleep(Duration::from_millis(5)).await; + } + cancel.cancel(); + }; + let (exported, ()) = tokio::join!(export, watch); + let error = exported.unwrap_err(); + + assert_eq!(error, CoreError::Cancelled); + assert_eq!(named(dir.path(), "endless.csv"), Vec::::new()); +} + +#[tokio::test] +async fn a_failed_export_keeps_the_file_it_would_have_replaced() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + run(&session, "INSERT INTO people VALUES (3, 'bell' || char(7), NULL, NULL)").await; + let set = run(&session, "SELECT id, name FROM people ORDER BY id").await.unwrap(); + let path = dir.path().join("people.xml"); + std::fs::write(&path, "an older export").unwrap(); + + let error = session + .export_rows(set, 2, ExportFormatTag::Xml, None, spelled(&path), None) + .await + .unwrap_err(); + + assert_eq!(error.code(), "ffi.export_not_xml_text"); + assert_eq!(text(&path), "an older export"); + assert_eq!(named(dir.path(), "people.xml"), ["people.xml"]); +} + +#[tokio::test] +async fn rows_still_arriving_are_not_exported() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + let editor = session.open_editor(None).await.unwrap(); + let settings = RunSettings { + row_limit: Some(u64::MAX), + timeout_ms: None, + byte_limit: Some(u64::MAX), + }; + let run = editor.run( + "WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c) SELECT x FROM c".to_owned(), + settings, + Box::new(Quiet), + ); + let started = Instant::now(); + let set = loop { + if let Some(set) = run.result_set(0) { + break set; + } + assert!(started.elapsed() < Duration::from_secs(10), "no set started"); + tokio::time::sleep(Duration::from_millis(5)).await; + }; + let path = spelled(&dir.path().join("endless.csv")); + + let refused = session + .export_rows(set, 1, ExportFormatTag::Csv, None, path, None) + .await + .unwrap_err(); + run.cancel(); + let _ended = run.outcome().await; + editor.close().await.unwrap(); + + assert_eq!(refused.code(), "ffi.export_unfinished"); + assert_eq!(named(dir.path(), "endless.csv"), Vec::::new()); +} diff --git a/core/crates/ffi/tests/history.rs b/core/crates/ffi/tests/history.rs new file mode 100644 index 0000000000..fa738a8d74 --- /dev/null +++ b/core/crates/ffi/tests/history.rs @@ -0,0 +1,238 @@ +#![cfg(feature = "engines")] +#![expect( + clippy::unwrap_used, + reason = "the setup helpers fail a test the way the test functions themselves do" +)] + +use std::path::Path; +use std::sync::Arc; + +use secrecy::SecretString; +use tablepro_ffi::{ + Core, DatabaseSession, FileOpenRequest, HistoryOutcomeTag, HistoryRun, HostConfig, HostSecretError, HostSecrets, + SecretItemTag, SecretKindTag, +}; + +struct NoSecrets; + +impl HostSecrets for NoSecrets { + fn load(&self, _: String, _: SecretKindTag) -> Result, HostSecretError> { + Ok(None) + } + + fn store(&self, _: String, _: SecretKindTag, _: SecretString, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn relabel(&self, _: String, _: SecretKindTag, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete(&self, _: String, _: SecretKindTag) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete_connection(&self, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn load_item(&self, _: SecretItemTag) -> Result, HostSecretError> { + Ok(None) + } + + fn store_item(&self, _: SecretItemTag, _: SecretString) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete_item(&self, _: SecretItemTag) -> Result<(), HostSecretError> { + Ok(()) + } +} + +fn started(storage: Option<&Path>) -> Arc { + Core::new( + HostConfig { + client_version: "test".to_owned(), + storage_dir: storage.map(|dir| dir.display().to_string()), + }, + Box::new(NoSecrets), + ) + .unwrap() +} + +async fn open(core: &Core, path: &Path) -> Arc { + let request = FileOpenRequest { + path: path.display().to_string(), + driver_id: None, + read_only: false, + create: true, + }; + core.open_file(request, None).await.unwrap() +} + +// Whole seconds, which is what the store keeps. +fn ran(sql: &str, at_secs: i64) -> HistoryRun { + HistoryRun { + sql: sql.to_owned(), + executed_at_ms: at_secs * 1_000, + duration_ms: Some(12), + rows_affected: Some(3), + outcome: HistoryOutcomeTag::Succeeded, + } +} + +#[tokio::test] +async fn a_recorded_run_is_found_by_its_words() { + let dir = tempfile::tempdir().unwrap(); + let core = started(Some(dir.path())); + let session = open(&core, &dir.path().join("app.sqlite")).await; + let history = core.history().await.unwrap(); + + let kept = history + .record(session.clone(), ran("SELECT name FROM people", 1_700_000_000)) + .await + .unwrap() + .unwrap(); + let failed = HistoryRun { + outcome: HistoryOutcomeTag::Failed { + message: "no such column: nope".to_owned(), + }, + ..ran("SELECT nope FROM people", 1_700_000_001) + }; + let refused = history.record(session.clone(), failed).await.unwrap().unwrap(); + let named = history.search("name people".to_owned(), None, None, 50).await.unwrap(); + let both = history.search("PEOPLE".to_owned(), None, None, 50).await.unwrap(); + + let [entry] = named.entries.as_slice() else { + panic!("{:?}", named.entries) + }; + assert_eq!(entry.id, kept); + assert_eq!(entry.sql, "SELECT name FROM people"); + assert_eq!(entry.connection_id, Some(session.id())); + assert_eq!(entry.connection_name, session.name()); + assert_eq!(entry.driver_id, "sqlite"); + assert_eq!(entry.executed_at_ms, 1_700_000_000_000); + assert_eq!((entry.duration_ms, entry.rows_affected), (Some(12), Some(3))); + assert_eq!(entry.outcome, HistoryOutcomeTag::Succeeded); + assert!(!entry.pinned); + assert_eq!(named.next, None); + let ids: Vec = both.entries.iter().map(|entry| entry.id).collect(); + assert_eq!(ids, [refused, kept], "newest first"); + assert_eq!( + both.entries[0].outcome, + HistoryOutcomeTag::Failed { + message: "no such column: nope".to_owned() + } + ); +} + +#[tokio::test] +async fn a_search_keeps_to_the_connection_it_names() { + let dir = tempfile::tempdir().unwrap(); + let core = started(Some(dir.path())); + let first = open(&core, &dir.path().join("first.sqlite")).await; + let second = open(&core, &dir.path().join("second.sqlite")).await; + let history = core.history().await.unwrap(); + + let mine = history + .record(first.clone(), ran("SELECT 1", 1_700_000_000)) + .await + .unwrap() + .unwrap(); + history + .record(second.clone(), ran("SELECT 2", 1_700_000_001)) + .await + .unwrap() + .unwrap(); + let only_first = history.search(String::new(), Some(first.id()), None, 50).await.unwrap(); + let every = history.search(String::new(), None, None, 50).await.unwrap(); + + let ids: Vec = only_first.entries.iter().map(|entry| entry.id).collect(); + assert_eq!(ids, [mine]); + assert_eq!(every.entries.len(), 2); +} + +#[tokio::test] +async fn pages_follow_the_cursor_and_show_no_run_twice() { + let dir = tempfile::tempdir().unwrap(); + let core = started(Some(dir.path())); + let session = open(&core, &dir.path().join("app.sqlite")).await; + let history = core.history().await.unwrap(); + let mut ids = Vec::new(); + for (index, at) in [1_700_000_001, 1_700_000_002, 1_700_000_003].into_iter().enumerate() { + let sql = format!("SELECT {index}"); + ids.push(history.record(session.clone(), ran(&sql, at)).await.unwrap().unwrap()); + } + + let first = history.search(String::new(), None, None, 2).await.unwrap(); + let second = history.search(String::new(), None, first.next, 2).await.unwrap(); + + let shown: Vec = first.entries.iter().map(|entry| entry.id).collect(); + let rest: Vec = second.entries.iter().map(|entry| entry.id).collect(); + assert_eq!(shown, [ids[2], ids[1]]); + assert!(first.next.is_some()); + assert_eq!(rest, [ids[0]]); + assert_eq!(second.next, None); +} + +#[tokio::test] +async fn a_deleted_run_is_gone_and_clear_removes_the_rest() { + let dir = tempfile::tempdir().unwrap(); + let core = started(Some(dir.path())); + let session = open(&core, &dir.path().join("app.sqlite")).await; + let history = core.history().await.unwrap(); + let gone = history + .record(session.clone(), ran("SELECT 1", 1_700_000_000)) + .await + .unwrap() + .unwrap(); + let left = history + .record(session.clone(), ran("SELECT 2", 1_700_000_001)) + .await + .unwrap() + .unwrap(); + + history.delete(vec![gone]).await.unwrap(); + let after_delete = history.search(String::new(), None, None, 50).await.unwrap(); + history.clear().await.unwrap(); + let after_clear = history.search(String::new(), None, None, 50).await.unwrap(); + + let ids: Vec = after_delete.entries.iter().map(|entry| entry.id).collect(); + assert_eq!(ids, [left]); + assert!(after_clear.entries.is_empty()); +} + +// SQLite withholds a statement that sets an encryption key, and text whose quote never closes. +#[tokio::test] +async fn a_statement_the_engine_withholds_is_not_stored() { + let dir = tempfile::tempdir().unwrap(); + let core = started(Some(dir.path())); + let session = open(&core, &dir.path().join("app.sqlite")).await; + let history = core.history().await.unwrap(); + + let key = history + .record(session.clone(), ran("PRAGMA key = 'hunter2'", 1_700_000_000)) + .await + .unwrap(); + let unclosed = history + .record(session.clone(), ran("SELECT 'hunter2", 1_700_000_001)) + .await + .unwrap(); + let stored = history.search(String::new(), None, None, 50).await.unwrap(); + + assert_eq!((key, unclosed), (None, None)); + assert!(stored.entries.is_empty(), "{:?}", stored.entries); +} + +#[tokio::test] +async fn every_caller_shares_one_store_and_a_core_without_storage_has_none() { + let dir = tempfile::tempdir().unwrap(); + let core = started(Some(dir.path())); + + let first = core.history().await.unwrap(); + let again = core.history().await.unwrap(); + let error = started(None).history().await.unwrap_err(); + + assert!(Arc::ptr_eq(&first, &again)); + assert_eq!(error.code(), "ffi.no_storage"); +} diff --git a/core/crates/ffi/tests/library.rs b/core/crates/ffi/tests/library.rs index 02bc1dee6f..f0550159a7 100644 --- a/core/crates/ffi/tests/library.rs +++ b/core/crates/ffi/tests/library.rs @@ -546,27 +546,35 @@ async fn a_saved_sqlite_file_connects_lists_its_tables_and_becomes_recent() { .connect_saved(saved.id.clone(), ConnectionSecrets::default(), None, None) .await .unwrap(); + struct Quiet; + impl tablepro_ffi::RunListener for Quiet { + fn events_pending(&self) {} + } let objects = session.objects(ObjectScopeArg::DefaultSchema, None).await.unwrap(); let browse = session - .table_query( + .read_table_page( TableName { schema: None, name: "people".to_owned(), }, None, + None, 100, 0, + Box::new(Quiet), None, ) .await .unwrap(); + let outcome = browse.run().outcome().await.unwrap(); + let rows = browse.run().result_set(0).map(|set| set.row_count()); session.close().await.unwrap(); let snapshot = library.snapshot().await.unwrap(); assert!(objects.iter().any(|object| object.name == "people")); - assert!(browse.sql.contains("FROM \"people\""), "{browse:?}"); - assert!(browse.sql.contains("100"), "{browse:?}"); - assert_eq!(browse.visible_columns, 2); + assert!(matches!(outcome, RunOutcome::Completed { .. }), "{outcome:?}"); + assert_eq!(rows, Some(0)); + assert_eq!(browse.visible_columns(), 2); assert_eq!(snapshot.recent, [saved.id]); assert!(snapshot.connections[0].last_opened_ms.is_some()); } diff --git a/core/crates/ffi/tests/row_edits.rs b/core/crates/ffi/tests/row_edits.rs index f089cc0d1d..84c6e88134 100644 --- a/core/crates/ffi/tests/row_edits.rs +++ b/core/crates/ffi/tests/row_edits.rs @@ -109,10 +109,19 @@ async fn seeded(core: &Core, path: &Path) { session.close().await.unwrap(); } -// The page as a table tab reads it, through the core's own statement. +// The page as a table tab reads it. +async fn page_of(session: &DatabaseSession, table: TableName) -> Arc { + let read = session + .read_table_page(table, None, None, 100, 0, Box::new(Quiet), None) + .await + .unwrap(); + let run = read.run(); + assert!(matches!(run.outcome().await.unwrap(), RunOutcome::Completed { .. })); + run.result_set(0).unwrap() +} + async fn page(session: &DatabaseSession) -> Arc { - let query = session.table_query(people(), None, 100, 0, None).await.unwrap(); - run(session, &query.sql).await.unwrap() + page_of(session, people()).await } async fn rows(session: &DatabaseSession) -> Vec>> { @@ -490,8 +499,7 @@ async fn a_page_whose_key_cannot_be_named_exactly_is_locked() { name: "readings".to_owned(), }; let editor = session.table_editor(readings.clone(), false, None).await.unwrap(); - let query = session.table_query(readings, None, 100, 0, None).await.unwrap(); - let shown = run(&session, &query.sql).await.unwrap(); + let shown = page_of(&session, readings).await; let people_editor = session.table_editor(people(), false, None).await.unwrap(); let people_page = page(&session).await; @@ -525,8 +533,7 @@ async fn a_new_row_leaves_a_blob_null_and_a_refresh_reads_the_columns_again() { name: "files".to_owned(), }; let editor = session.table_editor(files.clone(), false, None).await.unwrap(); - let query = session.table_query(files.clone(), None, 100, 0, None).await.unwrap(); - let shown = run(&session, &query.sql).await.unwrap(); + let shown = page_of(&session, files.clone()).await; let typed = editor.check(2, text("0x00"), true).unwrap_err(); let save = editor diff --git a/core/crates/ffi/tests/sqlite_path.rs b/core/crates/ffi/tests/sqlite_path.rs index 9ab13ef427..6c2063a050 100644 --- a/core/crates/ffi/tests/sqlite_path.rs +++ b/core/crates/ffi/tests/sqlite_path.rs @@ -367,8 +367,32 @@ fn texts(values: &[&str]) -> Vec> { values.iter().map(|value| Some((*value).to_owned())).collect() } +// The width of a table's page and the texts of one of its columns, once the read has finished. +async fn page_texts( + session: &DatabaseSession, + table: TableName, + sort: Option, + limit: u64, + offset: u64, + column: u32, +) -> (u32, bool, u32, Vec>) { + let page = session + .read_table_page(table, sort, None, limit, offset, Box::new(Wakes::default()), None) + .await + .unwrap(); + let run = page.run(); + let outcome = run.outcome().await.unwrap(); + assert!(matches!(outcome, RunOutcome::Completed { .. }), "{outcome:?}"); + let set = run.result_set(0).unwrap(); + let width = u32::try_from(set.columns().len()).unwrap(); + let texts = (0..set.row_count()) + .map(|row| set.cell_text(row, column).unwrap()) + .collect(); + (page.visible_columns(), page.can_seek(), width, texts) +} + #[tokio::test] -async fn a_table_query_sorts_on_the_server_and_names_the_columns_a_grid_shows() { +async fn a_table_page_sorts_on_the_server_and_names_the_columns_a_grid_shows() { let dir = tempfile::tempdir().unwrap(); let (_core, session, editor) = fixture(dir.path()).await; let (outcome, _, _) = run( @@ -391,34 +415,21 @@ async fn a_table_query_sorts_on_the_server_and_names_the_columns_a_grid_shows() ascending: false, }); - let by_name = session - .table_query(people.clone(), descending, 100, 0, None) - .await - .unwrap(); - let first_two = session.table_query(people, descending, 2, 0, None).await.unwrap(); - let by_body = session - .table_query( - notes, - Some(ColumnSort { - column: 0, - ascending: true, - }), - 100, - 0, - None, - ) - .await - .unwrap(); - let (_, names) = column_texts(&editor, &by_name.sql, 1).await; - let (_, limited) = column_texts(&editor, &first_two.sql, 1).await; - let (width, bodies) = column_texts(&editor, &by_body.sql, 0).await; + let by_body = Some(ColumnSort { + column: 0, + ascending: true, + }); + + let (visible, _, _, names) = page_texts(&session, people.clone(), descending, 100, 0, 1).await; + let (_, _, _, limited) = page_texts(&session, people, descending, 2, 0, 1).await; + let (body_visible, _, width, bodies) = page_texts(&session, notes, by_body, 100, 0, 0).await; - assert_eq!(by_name.visible_columns, 3); + assert_eq!(visible, 3); assert_eq!(names, texts(&["Zoë 👋", "Grace", "Ada"])); assert_eq!(limited, texts(&["Zoë 👋", "Grace"])); assert_eq!(bodies, texts(&["a", "b"])); // A table without a key pages by its rowid, which follows the visible column. - assert_eq!((by_body.visible_columns, width), (1, 2)); + assert_eq!((body_visible, width), (1, 2)); } #[tokio::test] @@ -501,7 +512,7 @@ async fn a_result_that_cannot_run_again_sorts_the_rows_it_holds() { #[tokio::test] async fn a_table_pages_past_its_first_rows_and_counts_them_all() { let dir = tempfile::tempdir().unwrap(); - let (_core, session, editor) = fixture(dir.path()).await; + let (_core, session, _editor) = fixture(dir.path()).await; let people = TableName { schema: None, name: "people".to_owned(), @@ -511,13 +522,11 @@ async fn a_table_pages_past_its_first_rows_and_counts_them_all() { ascending: true, }); - let second = session.table_query(people.clone(), None, 2, 2, None).await.unwrap(); - let sorted_second = session.table_query(people.clone(), by_name, 2, 1, None).await.unwrap(); - let count = session.table_row_count(people, None).await.unwrap(); - let (_, ids) = column_texts(&editor, &second.sql, 0).await; - let (_, names) = column_texts(&editor, &sorted_second.sql, 1).await; + let (_, can_seek, _, ids) = page_texts(&session, people.clone(), None, 2, 2, 0).await; + let (_, _, _, names) = page_texts(&session, people.clone(), by_name, 2, 1, 1).await; + let count = session.table_row_count(people, None, None).await.unwrap(); - assert!(second.can_seek); + assert!(can_seek); assert_eq!(ids, texts(&["3"])); assert_eq!(names, texts(&["Grace", "Zoë 👋"])); assert_eq!(count, 3); diff --git a/core/docs/decisions/0022-browsing-through-the-ffi.md b/core/docs/decisions/0022-browsing-through-the-ffi.md new file mode 100644 index 0000000000..54d741919a --- /dev/null +++ b/core/docs/decisions/0022-browsing-through-the-ffi.md @@ -0,0 +1,39 @@ +# 0022: A table page is read on the session with its filter bound, and structure, definitions, export and history cross the FFI + +- **Status**: Accepted +- **Date**: 2026-11-12 + +## Context + +The Windows app reaches the core only through `crates/ffi`. A table tab read its page as editor text: `table_query` built the page with an empty filter, refused any statement that bound a value, and the tab ran the SQL on an editor connection of its own. A filter cannot work that way, since `build_filter` parses each value as the column's type and binds it. The FFI also had no read for a table's indexes, foreign keys and CREATE script, a routine's or trigger's source, an export, or the query history the Linux app reads through `tablepro-storage`. + +## Decision + +The FFI exposes what the Linux app calls, and does the work the Linux app does, so an app on it builds no SQL and keeps no store of its own. + +- `DatabaseSession::read_table_page` reads the catalog's columns and the row identity, builds the page once with the filter so a value the column cannot take or a column the table lacks fails the call before anything is sent, then reads it with `browse::read_page` on a pooled connection (`OperationKind::Page`). The rows arrive as the run events an editor run gives (`ResultSetStarted`, `RowsAppended`, `ResultSetFinished`, `Finished` with an idle transaction), so a host shows a page and a query result the same way. `table_query` is gone, and a table tab no longer holds an editor connection. +- `table_row_count` takes the same filter. `filter_choices` gives each column's operators from `SqlDialect::filter_operators`, the engine's combinators, and whether it takes a raw SQL predicate, which `validate_raw_predicate` reads as exactly one expression. MongoDB's raw filter is a query document, not SQL, so it is not offered there yet, and raw text for an engine that is not SQL is refused (`filter.raw_unsupported`). +- A refused filter carries the codes of each layer: `operation.browse`, `browse.filter`, the rule's `filter.*`, and for a value the column cannot take, the parse's `edit.*` (`BrowseSqlError::codes`, `BuildFilterError::codes`). +- `table_structure` reads `fetch_table_definition` and gives its columns, indexes and foreign keys as display text, and the CREATE script from `DdlDialect::create_table_script` only for a table or a partitioned table, by the kind the catalog lists it under where it has one: on some engines the script for a view would be a CREATE TABLE made from its columns. +- `QueryEditor::use_database` moves an editor's connection to a database, for a definition whose text names none (MySQL's), as the Linux app opens such a definition. +- `object_definition` reads `fetch_object_definition` for a function, procedure, trigger or sequence, with whether running the text in an editor would make the object as it is. +- `export_rows` writes the rows a finished `ResultSet` holds, its visible columns only; `export_table` streams every row the filter and sort select through `build_export` and `stream_read`, as the Linux app exports a table. Both use the core's encoders (CSV with its default options, JSON, Excel, Markdown, HTML, XML, and SQL INSERT from a table), write beside the target and rename once complete, so a failed or cancelled export leaves no file. +- `Core::history` opens the query history at `/state/history.db` once. `record` takes the session, so the engine's `history_recording` withholds a statement that holds a secret, as on Linux; `search` pages by the store's keyset; `delete` and `clear` change it. + +## Rationale + +- One place builds the WHERE, binds its values and pages the table, for every app. +- Delivering a page as run events keeps the host's results code single, and a pooled connection is what the Linux app reads a page on. +- An editor connection per table tab held a connection the tab only read through. + +## Consequences + +- A table tab's page is no longer on a connection of its own, so a session setting made in a query tab does not reach it, as on Linux. +- A page is read whole, under `OperationKind::Page`'s deadline, before its rows reach the app. +- History is pruned to the store's cap when it opens; a retention setting, as Linux has, is the host's to add. +- An array value in `export_rows` is written as its text, since the result store keeps arrays only as text; `export_table` writes it as an array. + +## Alternatives considered + +- **Inline the filter's values as literals in editor text.** The values would be spliced into SQL, which the filter's design refuses. +- **A bound-statement run on the editor connection.** `EditorSession` runs only user text, and a page needs no transaction of the user's. diff --git a/core/docs/decisions/README.md b/core/docs/decisions/README.md index 8984d95167..999d23906b 100644 --- a/core/docs/decisions/README.md +++ b/core/docs/decisions/README.md @@ -29,3 +29,4 @@ The format, the statuses and how a later ADR changes an earlier one follow [the | [0019](0019-row-edits-through-the-ffi.md) | A table tab's row edits cross the FFI as typed text, and the core plans and runs the save | Accepted | `TableEditor` over the catalog's primary key reads keys and stored values from the result set, parses typed text with `parse_edit_text` and plans with `plan_save`; `RowSave` shows its statements with their values, gates at `SafeModeLevel::gate(Write)`, plans again on the writing lease and reports saved, rolled back or unknown. | | [0020](0020-a-run-keeps-a-byte-limit-of-rows.md) | A run keeps a byte limit of rows | Accepted | `RunOptions::byte_limit`, 256 MB by default, bounds the rows an editor run keeps, measured by `Value::retained_bytes`; the session wraps every editor session in `ByteLimitSink`, which keeps whole rows, never stops the statement and finishes a cut set as `Truncation::ByteLimit`; MongoDB stops reading itself; table pages are exempt. | | [0021](0021-an-int32-keeps-its-width.md) | An int32 keeps its width, and an edited number keeps its type | Accepted | `Value::Int32` holds MongoDB's int32, equal to the `Int` of the same number; a save carries each edited cell's read value, and a number binds in the read's BSON type when it is exactly one, else the column's. | +| [0022](0022-browsing-through-the-ffi.md) | A table page is read on the session with its filter bound, and structure, definitions, export and history cross the FFI | Accepted | `read_table_page` builds the page with the filter, binds its values and reads it with `read_page` on a pooled connection as run events; `table_structure`, `object_definition`, `export_rows`, `export_table` and `Core::history` give the Linux app's reads to an app on the FFI. | diff --git a/windows/AGENTS.md b/windows/AGENTS.md index 6740cccc03..0d8b3977d5 100644 --- a/windows/AGENTS.md +++ b/windows/AGENTS.md @@ -41,7 +41,8 @@ This is the guide for work under `windows/`. The root `AGENTS.md` holds the rule - `QueryEditor.Run` returns at once. Read the run's events with `ReadEventsAsync` on the UI thread; rows that arrive while it is busy come as one event. - A grid reads a `ResultSet` through `ResultCursor.Fetch`, one window per visible range. A cursor belongs to one thread, and a window cannot be read after the cursor's next fetch. - `DataGridView.SetData(new ResultSetData(set, columns))` shows a set. Call the grid's `RowsChanged` after each `RowsAppended`. Sorting is the host's: the grid raises `SortRequested` and shows `Sort`. -- The core writes every sorted statement: `GetTableQueryAsync` for a table, `SortedQuery` for a query result, null when the statement cannot run again or the engine cannot name the column. `ResultSet.SortedAsync` then sorts the rows held (`core/docs/decisions/0007-sorting-a-result.md`). `GetTableQueryAsync` takes the page's offset, and `CanSeek` says whether the engine has later pages; `CountRowsAsync` counts the table. +- The core writes every sorted statement: `ReadTablePageAsync` reads a table's page with its sort and filter on a pooled connection and returns its run, `SortedQuery` writes a query result's, null when the statement cannot run again or the engine cannot name the column. `ResultSet.SortedAsync` then sorts the rows held (`core/docs/decisions/0007-sorting-a-result.md`). `ReadTablePageAsync` takes the page's offset, and `CanSeek` says whether the engine has later pages; `CountRowsAsync` counts the rows the filter picks. The filter's values are bound by the core, never written into SQL here (`core/docs/decisions/0022-browsing-through-the-ffi.md`). +- `GetStructureAsync` gives a table's columns, indexes, foreign keys and CREATE script, `GetObjectDefinitionAsync` a routine's, trigger's or sequence's source, `ExportRowsAsync` and `ExportTableAsync` write a file through the core's encoders, and `TableProCore.OpenHistoryAsync` the query history, which withholds what the engine says holds a secret. - A table tab edits through `DatabaseSession.OpenTableEditorAsync`: the app sends what was typed (`CellEdit`) per row of the page's `ResultSet`, and `TableEditor.Plan` gives a `RowSave` with its statements and Safe Mode gate, which `RunAsync` saves (`core/docs/decisions/0019-row-edits-through-the-ffi.md`). - `QueryEvent.Finished.Transaction` is the transaction the editor's connection is left in. A session's `Health` is waited on with `WaitForHealthChangeAsync`, which returns once it differs from the one passed in. - The app starts the core with `TableProCore.CreateWithSystemSecrets`, so the core keeps secrets in Credential Manager itself. `ISecretStore` is for tests and hosts with a store of their own; the core calls it on a thread of its own. diff --git a/windows/docs/decisions/0004-app-shell.md b/windows/docs/decisions/0004-app-shell.md index 81d03c8015..dacdf28d41 100644 --- a/windows/docs/decisions/0004-app-shell.md +++ b/windows/docs/decisions/0004-app-shell.md @@ -1,6 +1,6 @@ # 0004: The app shell: one window, a rail of connections, a page per surface -- **Status**: Accepted; packaging amended by [0005](0005-distribution.md) +- **Status**: Accepted; packaging amended by [0005](0005-distribution.md), table reads by [0007](0007-browsing-a-connection.md) - **Date**: 2026-11-02 ## Context @@ -110,8 +110,8 @@ On 2026-11-05 the maintainer had the app show what the server prints, PostgreSQL - The object tree is a `TreeView` in ItemsSource mode with lazy children (`HasUnrealizedChildren`, `Expanding`) ([Tree view](https://learn.microsoft.com/en-us/windows/apps/develop/ui/controls/tree-view)). Schemas show only when there is more than one that is not a system schema. Objects go into a folder per kind with their count. - 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. +- Invoking a table or view opens its table tab, or brings the open one forward. The core reads the page (`DatabaseSession.ReadTablePageAsync`, over the core's `read_page`), quoted, ordered, filtered and limited as the engine writes it, on one of the session's pooled connections (amended by 0007). +- A query tab is the SQL editor over the run's results, on its own editor connection opened on first use; a table tab is the results alone, read on the session's pooled connections (0007). - 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." diff --git a/windows/docs/decisions/0007-browsing-a-connection.md b/windows/docs/decisions/0007-browsing-a-connection.md new file mode 100644 index 0000000000..42043674a5 --- /dev/null +++ b/windows/docs/decisions/0007-browsing-a-connection.md @@ -0,0 +1,50 @@ +# 0007: Filtering, structure, definitions, export and query history + +- **Status**: Accepted +- **Date**: 2026-11-12 + +## Context + +A workspace opened tables and views and nothing else (audit item WN-09). Functions, procedures, triggers and sequences did nothing when clicked; a table tab had no WHERE filter and no view of its columns, indexes and keys; there was no export and no query history. The macOS app has all of them, and the core has every read the Linux app uses for them, which the FFI now carries ([core 0022](../../../core/docs/decisions/0022-browsing-through-the-ffi.md)). + +## Decision + +Each one works as the macOS app's does, with its words in Windows sentence case, over the core's reads; the logic sits in `src/TablePro.App.Model` with fakes in `tests/TablePro.App.Model.Tests`, and the views lay out. + +### Table tab + +- A `SelectorBar` at the top of a table tab switches between Data and Structure. +- Data reads its page through `ReadTablePageAsync` on the session's pooled connections; the tab opens no editor connection of its own (amends 0004). +- The filter bar (Filter in the tab's command bar, Query > Filter rows, Ctrl+Shift+F) holds one row per rule: a column, an operator the engine offers for it, and its value, two for between, a comma-separated list for in list. A Raw SQL row takes a WHERE condition where the engine reads SQL. Match all or Match any shows once there are two conditions, Any only where the engine has OR. Apply reads the first page and counts the rows the filter picks; Clear reads the whole table. A rule missing its value is refused before anything is sent. A filter the core refuses keeps the rows and the filter shown. One the server refuses, or a read that is stopped or times out, keeps the filter, sort and page the rows were read with, and the grid is empty until Refresh reads them again. The results pane says why from the core's code (`filter.*`, down to a value's own `edit.*`), without naming the rule. Changing the filter asks before it discards pending edits, as a sort does. The filter is kept with the tab in the session. +- Structure shows Columns, Indexes, Foreign keys and DDL (`GetStructureAsync`). The lists are the read-only data grid over `TextGridData`, with the macOS headers and YES or NO; DDL is the engine's CREATE script in a read-only text box with Copy, and none for a view, whose CREATE TABLE some engines would build from its columns. Structure is read once and again on Refresh. + +### Objects without rows + +- A function, procedure, trigger or sequence opens a definition tab: its source read only (`GetObjectDefinitionAsync`), Reload, Copy, and Open in editor, which opens a query tab with the text and is hidden where the core says running it would not make the object as it is. A definition whose text names no database, as MySQL's, opens a query tab that moves its connection to that database before its first run (`QueryEditor.UseDatabaseAsync`), and refuses to run where the move fails. "Source unavailable" says why, or that the object is gone or the login cannot read it, with Try again. +- The tree's context menu offers Open, Show structure and Copy name for objects with rows, and Show DDL and Copy name for the others. +- Definition tabs are not kept in session.json: an older build reads an unknown tab kind as a damaged file and starts empty, and the tree brings one back in a click. + +### Export + +- File > Export results (Ctrl+Shift+E) asks for a format, CSV, JSON, Excel, Markdown, HTML, XML, or SQL INSERT from a table, and on a table tab for Current page or Entire table, then for a file with the system save picker. The core writes the file (`ExportRowsAsync`, `ExportTableAsync`): the rows held, or every row the tab's filter and sort select, streamed. A file appears only once complete. + +### Query history + +- Every typed run of a query tab is recorded once it ends, with its time, duration, rows affected and outcome. The core withholds a statement that holds a secret. A sort's rerun, Commit, Roll back and a run Safe Mode kept back are not recorded. +- View > Show query history opens a pane beside the tabs with the connection's runs, newest first, searched as typed. A file opened without saving gets a new connection id each time, so its pane lists every connection's runs. An entry opens in a new query tab; Copy and Delete are on its menu, and Clear history asks first. + +## Rationale + +- The core builds every WHERE and binds its values, writes every file and keeps every history row, so Windows writes no SQL and keeps no store the Linux app does not. +- Stock WinUI controls: SelectorBar, ComboBox, TextBox, InfoBar, ListView, ContentDialog and the Windows App SDK's FileSavePicker. + +## Consequences + +- No completion in the SQL editor yet: the caret analysis lives in the Linux app and moves to the core first, then the editor needs its popup. +- MongoDB's typed filter document is not offered; its rules are. +- No CSV options and no history retention setting yet: the core's CSV defaults and the store's cap apply. +- The history keeps no database, so a run reopened from it runs where the connection is. +- A table tab's page no longer shares a connection with anything, so a session setting from a query tab does not reach it. +- A page is read whole before it shows, under the core's page deadline of 60 seconds, as the Linux app reads one; the editor path streamed it with no deadline. Show all rows on a large table shows nothing until the last row is in. +- A restored filter runs on the first read, Raw SQL included, as the rules do: one expression, read on a pooled connection. Text an engine that is not SQL would read as its own query language is refused by the core. +- An export the app is killed in leaves its `.part` file beside the target. diff --git a/windows/docs/decisions/README.md b/windows/docs/decisions/README.md index c20ab1f7f8..5f547dbf3e 100644 --- a/windows/docs/decisions/README.md +++ b/windows/docs/decisions/README.md @@ -11,6 +11,7 @@ The format, the statuses and how a later ADR changes an earlier one follow [the | [0001](0001-toolchain-baseline.md) | Windows toolchain and build baseline | Accepted; amended by 0005 | Build Tools for Visual Studio 2026 with MSVC for x64 and Arm64, Windows SDK 10.0.26100, .NET 10 and Rust with both MSVC targets; cargo runs from the csproj; Native AOT; Windows 10 1809 as the floor, with runtime checks for Windows 11 behavior. | | [0002](0002-sql-editor-control.md) | TablePro's own SQL editor control | Accepted | A rope with its own undo in a plain .NET model, drawn with Direct2D and DirectWrite into a SwapChainPanel, TSF as Windows Terminal does it, a UIA text provider, and the core's lexer for colors. | | [0003](0003-data-grid-control.md) | TablePro's own data grid control | Accepted | A WinUI 3 control that edits cells in place: ItemsRepeater rows that draw their visible cells with DirectWrite into composition surfaces through the editor's drawing layer, cells as UIA peers with no element, data read from the core in windows of cells with the host's pending changes laid over them. | -| [0004](0004-app-shell.md) | The app shell | Accepted; packaging amended by 0005 | One window with the TitleBar, a MenuBar and a NavigationView rail of open connections, each with its own TabView; the Connections page, an owned connection form window, Settings as a page; the core keeps connections and Credential Manager secrets, the app its settings and session in LocalState. | +| [0004](0004-app-shell.md) | The app shell | Accepted; packaging amended by 0005, table reads by 0007 | One window with the TitleBar, a MenuBar and a NavigationView rail of open connections, each with its own TabView; the Connections page, an owned connection form window, Settings as a page; the core keeps connections and Credential Manager secrets, the app its settings and session in LocalState. | | [0005](0005-distribution.md) | Shipping the app | Accepted | One self-contained Native AOT MSIX per architecture in an `.msixbundle` signed with Azure Artifact Signing, on GitHub Releases and winget; the core on the hybrid CRT; the 1809 floor checked in the package and on Windows Server 2022. | | [0006](0006-editing-table-rows.md) | Editing a table's rows | Accepted | A table tab edits as the macOS app does: F2, Enter or a double-click edits a cell, rows are added, duplicated and deleted, every change stays pending until Save, reloads and closes ask first, Safe Mode's Confirm writes shows the statements, and the core plans and runs the save (core 0019). | +| [0007](0007-browsing-a-connection.md) | Filtering, structure, definitions, export and query history | Accepted | A table tab has Data and Structure and a filter bar over the core's bound page read; functions, procedures, triggers and sequences open their source read only; Export results writes through the core's encoders; Show query history lists the runs the core recorded. | diff --git a/windows/src/TablePro.App.Model/DefinitionTab.cs b/windows/src/TablePro.App.Model/DefinitionTab.cs new file mode 100644 index 0000000000..f3205f198b --- /dev/null +++ b/windows/src/TablePro.App.Model/DefinitionTab.cs @@ -0,0 +1,156 @@ +using TablePro.Interop; + +namespace TablePro.App.Model; + +// What a definition tab shows; the view words the object's kind beside its name. +public interface IDefinitionTabView +{ + void ShowLoading(); + + void ShowSource(ObjectSource source); + + // Null failure: the object is gone, or the login cannot read its definition. + void ShowUnavailable(CoreError? failure); +} + +// A function, procedure, trigger or sequence, shown read only as the engine writes its definition. +public sealed class DefinitionTab : IWorkspaceTab +{ + private readonly ITabSession _session; + private readonly IDefinitionTabView _view; + private readonly TabWork _work = new(); + private Task? _loading; + private Task? _closing; + + public DefinitionTab(ITabSession session, IDefinitionTabView view, ObjectEntry entry) + { + ArgumentNullException.ThrowIfNull(session); + ArgumentNullException.ThrowIfNull(view); + ArgumentNullException.ThrowIfNull(entry); + _session = session; + _view = view; + Entry = entry; + } + + public event EventHandler? StateChanged; + + public ObjectEntry Entry { get; } + + public string Title => Entry.Name; + + public TabKind Kind => TabKind.Definition; + + public bool IsRunning { get; private set; } + + // The read runs on one of the session's pooled connections, which holds no transaction. + public TransactionState Transaction => TransactionState.Idle; + + public bool HasUnsavedChanges => false; + + // What the view shows: null while unavailable, and until the first read ends. + public ObjectSource? Source { get; private set; } + + // Null when the tab names no object, or one of a kind this build cannot read, as one a later build saved. + public static ObjectEntry? EntryOf(SessionTab tab) + { + ArgumentNullException.ThrowIfNull(tab); + if (tab is not { Kind: TabKind.Definition, Table: { Length: > 0 } name }) + { + return null; + } + + var kind = tab.ObjectKindOr(ObjectKind.Other); + return kind is ObjectKind.Other or ObjectKind.Key + ? null + : new ObjectEntry(tab.Schema, name, kind, tab.ParentTable); + } + + public Task ConfirmCloseAsync(bool offerSave) => Task.FromResult(true); + + public SessionTab Snapshot() => new() + { + Kind = TabKind.Definition, + Title = Title, + Schema = Entry.Schema, + Table = Entry.Name, + ObjectKind = Enum.GetName(Entry.Kind), + ParentTable = Entry.ParentTable, + }; + + // Also Reload. A call during a read shares it, since a second read of the same object adds only a server trip. + public async Task LoadAsync() + { + if (_work.IsClosed) + { + return; + } + + _loading ??= ReadAsync(); + var loading = _loading; + try + { + await loading.ConfigureAwait(true); + } + finally + { + if (ReferenceEquals(_loading, loading)) + { + _loading = null; + } + } + } + + public Task CloseAsync() => _closing ??= CloseOnceAsync(); + + // The read in flight stops and ends before the session it uses is let go. + private async Task CloseOnceAsync() + { + await _work.CloseAsync().ConfigureAwait(true); + Source = null; + await _session.ReleaseAsync().ConfigureAwait(true); + } + + private Task ReadAsync() => _work.RunAsync(ReadSourceAsync); + + private async Task ReadSourceAsync(CancellationToken cancellationToken) + { + SetRunning(true); + _view.ShowLoading(); + try + { + var source = await _session.GetObjectDefinitionAsync(Entry, cancellationToken).ConfigureAwait(true); + if (_work.IsClosed) + { + return; + } + + Source = source; + if (source is null) + { + _view.ShowUnavailable(null); + } + else + { + _view.ShowSource(source); + } + } + catch (TableProException exception) + { + if (!_work.IsClosed) + { + Source = null; + _view.ShowUnavailable(exception.Error); + } + } + finally + { + SetRunning(false); + } + } + + private void SetRunning(bool running) + { + IsRunning = running; + StateChanged?.Invoke(this, EventArgs.Empty); + } +} diff --git a/windows/src/TablePro.App.Model/ExportPlan.cs b/windows/src/TablePro.App.Model/ExportPlan.cs new file mode 100644 index 0000000000..dbddfb2042 --- /dev/null +++ b/windows/src/TablePro.App.Model/ExportPlan.cs @@ -0,0 +1,72 @@ +using TablePro.Interop; + +namespace TablePro.App.Model; + +public enum ExportScope +{ + // A query tab's result as the grid holds it. + ShownRows, + CurrentPage, + EntireTable, +} + +// What a tab can export, each list in the order the dialog offers it. +public sealed class ExportPlan +{ + private const string RefusedInFileName = "<>:\"/\\|?*"; + + private ExportPlan(IReadOnlyList scopes, IReadOnlyList formats, string fileName) + { + Scopes = scopes; + Formats = formats; + FileName = fileName; + } + + public IReadOnlyList Scopes { get; } + + public IReadOnlyList Formats { get; } + + // Without an extension: the save picker adds the one of the format chosen. + public string FileName { get; } + + // SQL INSERT writes rows back into the table they came from, so only a table offers it. + public static ExportPlan For(TableName? table, string resultsName) + { + ArgumentNullException.ThrowIfNull(resultsName); + ExportFormat[] rows = + [ + ExportFormat.Csv, + ExportFormat.Json, + ExportFormat.Excel, + ExportFormat.Markdown, + ExportFormat.Html, + ExportFormat.Xml, + ]; + return table is null + ? new ExportPlan([ExportScope.ShownRows], rows, resultsName) + : new ExportPlan( + [ExportScope.CurrentPage, ExportScope.EntireTable], + [.. rows, ExportFormat.SqlInsert], + FileNameOf(table.Name, resultsName)); + } + + public static string Extension(ExportFormat format) => format switch + { + ExportFormat.Csv => ".csv", + ExportFormat.Json => ".json", + ExportFormat.Excel => ".xlsx", + ExportFormat.Markdown => ".md", + ExportFormat.Html => ".html", + ExportFormat.Xml => ".xml", + ExportFormat.SqlInsert => ".sql", + _ => throw new ArgumentOutOfRangeException(nameof(format), format, null), + }; + + // Windows refuses these characters in a file name, and drops a dot or a space at its end. + private static string FileNameOf(string table, string fallback) + { + var safe = table.Select(c => char.IsControl(c) || RefusedInFileName.Contains(c) ? '_' : c).ToArray(); + var name = new string(safe).TrimEnd('.', ' '); + return string.IsNullOrWhiteSpace(name) ? fallback : name; + } +} diff --git a/windows/src/TablePro.App.Model/HistoryList.cs b/windows/src/TablePro.App.Model/HistoryList.cs new file mode 100644 index 0000000000..27d22d01dc --- /dev/null +++ b/windows/src/TablePro.App.Model/HistoryList.cs @@ -0,0 +1,181 @@ +using TablePro.Interop; + +namespace TablePro.App.Model; + +// The runs the core keeps, as the history pane reads and changes them. +public interface IHistoryStore +{ + Task SearchAsync( + string text, + Guid? connectionId, + HistoryCursor? after, + int limit, + CancellationToken cancellationToken); + + Task DeleteAsync(IReadOnlyList ids, CancellationToken cancellationToken); + + Task ClearAsync(CancellationToken cancellationToken); +} + +public sealed class CoreHistoryStore(QueryHistory history) : IHistoryStore +{ + public Task SearchAsync( + string text, + Guid? connectionId, + HistoryCursor? after, + int limit, + CancellationToken cancellationToken) => + history.SearchAsync(text, connectionId, after, limit, cancellationToken); + + public Task DeleteAsync(IReadOnlyList ids, CancellationToken cancellationToken) => + history.DeleteAsync(ids, cancellationToken); + + public Task ClearAsync(CancellationToken cancellationToken) => history.ClearAsync(cancellationToken); +} + +// One connection's runs, a page at a time in the store's order. A failure lands in Failure and is never thrown. +public sealed class HistoryList +{ + private readonly IHistoryStore _store; + private readonly Guid? _connectionId; + private readonly int _pageSize; + + // The store never gives a deleted id to another run, so a page read before a delete landed still leaves it out. + private readonly HashSet _deleted = []; + private HistoryCursor? _next; + + // A newer search or a clear moves it on, and a page read for an older one is dropped. + private int _generation; + + public HistoryList(IHistoryStore store, Guid? connectionId, int pageSize = 100) + { + ArgumentNullException.ThrowIfNull(store); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(pageSize); + _store = store; + _connectionId = connectionId; + _pageSize = pageSize; + } + + // Null: every connection's runs. + public Guid? ConnectionId => _connectionId; + + public event EventHandler? Changed; + + // A new list on each change, so a list the view holds never changes under it. + public IReadOnlyList Entries { get; private set; } = []; + + public string Text { get; private set; } = string.Empty; + + public bool HasMore => _next is not null; + + public bool IsLoading { get; private set; } + + // The last call's failure, null once one succeeds. + public CoreError? Failure { get; private set; } + + public Task SearchAsync(string text) + { + ArgumentNullException.ThrowIfNull(text); + Text = text; + return ReadAsync(after: null); + } + + // The same text again, as after a run was recorded. + public Task RefreshAsync() => ReadAsync(after: null); + + public Task LoadMoreAsync() => IsLoading || _next is not { } next ? Task.CompletedTask : ReadAsync(next); + + public async Task DeleteAsync(HistoryEntry entry) + { + ArgumentNullException.ThrowIfNull(entry); + try + { + await _store.DeleteAsync([entry.Id], CancellationToken.None).ConfigureAwait(true); + } + catch (TableProException exception) + { + Failure = exception.Error; + OnChanged(); + return; + } + + _deleted.Add(entry.Id); + Entries = [.. Entries.Where(shown => shown.Id != entry.Id)]; + Failure = null; + OnChanged(); + } + + // The core's store clears every connection's runs, not only this list's. + public async Task ClearAsync() + { + try + { + await _store.ClearAsync(CancellationToken.None).ConfigureAwait(true); + } + catch (TableProException exception) + { + Failure = exception.Error; + OnChanged(); + return; + } + + // A page read before the clear would bring back what it removed. + _generation++; + Entries = []; + _next = null; + IsLoading = false; + Failure = null; + OnChanged(); + } + + private async Task ReadAsync(HistoryCursor? after) + { + var generation = after is null ? ++_generation : _generation; + var text = Text; + IsLoading = true; + OnChanged(); + HistoryPage? page = null; + CoreError? failure = null; + try + { + page = await _store.SearchAsync(text, _connectionId, after, _pageSize, CancellationToken.None) + .ConfigureAwait(true); + } + catch (TableProException exception) + { + failure = exception.Error; + } + + if (generation != _generation) + { + return; + } + + if (page is not null) + { + var found = page.Entries.Where(entry => !_deleted.Contains(entry.Id)); + if (after is null) + { + Entries = [.. found]; + } + else + { + Entries = [.. Entries, .. found]; + } + + _next = page.Next; + } + else if (after is null) + { + // The runs of the last search do not answer what was typed now. + Entries = []; + _next = null; + } + + Failure = failure; + IsLoading = false; + OnChanged(); + } + + private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty); +} diff --git a/windows/src/TablePro.App.Model/HistoryRecorder.cs b/windows/src/TablePro.App.Model/HistoryRecorder.cs new file mode 100644 index 0000000000..feb48f20ae --- /dev/null +++ b/windows/src/TablePro.App.Model/HistoryRecorder.cs @@ -0,0 +1,35 @@ +using TablePro.Interop; + +namespace TablePro.App.Model; + +// Where a query tab keeps each run the user typed, once it ends. +public interface IHistoryRecorder +{ + Task RecordAsync(HistoryRun run); +} + +// Recorded runs on the caller's context once the store kept the run, so a history pane can read it again. +public sealed class CoreHistoryRecorder(QueryHistory history, DatabaseSession session, Action? recorded = null) + : IHistoryRecorder +{ + // No token: a close waits for the write rather than stopping it, since the session must outlive the call. + public async Task RecordAsync(HistoryRun run) + { + ArgumentNullException.ThrowIfNull(run); + long? id; + try + { + id = await history.RecordAsync(session, run, CancellationToken.None).ConfigureAwait(true); + } + catch (TableProException) + { + // The run's result stands whether or not its history line was written. + return; + } + + if (id is not null) + { + recorded?.Invoke(); + } + } +} diff --git a/windows/src/TablePro.App.Model/IResultsView.cs b/windows/src/TablePro.App.Model/IResultsView.cs index af8997e6a9..504cc764f7 100644 --- a/windows/src/TablePro.App.Model/IResultsView.cs +++ b/windows/src/TablePro.App.Model/IResultsView.cs @@ -20,6 +20,12 @@ public interface IResultsView TransactionState Transaction { get; } + // Null from the start of each ShowRunAsync until its Finished arrives, so a run that never ends has none. + QueryOutcome? Outcome { get; } + + // The counts the run's CommandCompleted events carried, summed; null when none carried one. + long? RowsAffected { get; } + ResultSet? ShownSet { get; } event EventHandler? ShownSetChanged; diff --git a/windows/src/TablePro.App.Model/ObjectFolders.cs b/windows/src/TablePro.App.Model/ObjectFolders.cs index 15019d0837..f4b978ed4a 100644 --- a/windows/src/TablePro.App.Model/ObjectFolders.cs +++ b/windows/src/TablePro.App.Model/ObjectFolders.cs @@ -56,6 +56,10 @@ [.. group.OrderBy(entry => entry.Name, StringComparer.CurrentCultureIgnoreCase)] public static bool HasRows(ObjectKind kind) => kind is ObjectKind.Table or ObjectKind.PartitionedTable or ObjectKind.View or ObjectKind.MaterializedView or ObjectKind.ForeignTable; + // As the core's ObjectKind::has_definition: it opens as SQL to read rather than rows to page. + public static bool HasDefinition(ObjectKind kind) => kind is ObjectKind.Function or ObjectKind.Procedure + or ObjectKind.Sequence or ObjectKind.Trigger; + public static IReadOnlyList Filter(IEnumerable objects, string text) { ArgumentNullException.ThrowIfNull(objects); diff --git a/windows/src/TablePro.App.Model/QueryTab.cs b/windows/src/TablePro.App.Model/QueryTab.cs index b9cd2ba3c2..7a8833b3e7 100644 --- a/windows/src/TablePro.App.Model/QueryTab.cs +++ b/windows/src/TablePro.App.Model/QueryTab.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using TablePro.DataGrid.Model; using TablePro.Interop; @@ -15,6 +16,11 @@ public interface IQueryTabView int Caret { get; } + // The history keeps a run's failure in the words the results pane showed for it. + string TimedOutMessage { get; } + + string WordError(CoreError failure); + void SelectStatement(int start, int length); void ShowNoStatement(); @@ -45,6 +51,7 @@ public sealed class QueryTab : IWorkspaceTab private readonly IResultsView _results; private readonly DataModel _dataModel; private readonly Func _settings; + private readonly IHistoryRecorder? _history; private readonly TabWork _work = new(); private ITabEditor? _editor; private Task? _closing; @@ -60,7 +67,9 @@ public QueryTab( IQueryTabView view, IResultsView results, DataModel dataModel, - Func settings) + Func settings, + IHistoryRecorder? history = null, + string? database = null) { ArgumentNullException.ThrowIfNull(title); ArgumentNullException.ThrowIfNull(session); @@ -73,6 +82,8 @@ public QueryTab( _results = results; _dataModel = dataModel; _settings = settings; + _history = history; + Database = string.IsNullOrEmpty(database) ? null : database; } public event EventHandler? StateChanged; @@ -81,6 +92,9 @@ public QueryTab( public TabKind Kind => TabKind.Query; + // Each editor connection the tab opens moves here before its first run. + public string? Database { get; } + public bool IsRunning { get; private set; } public TransactionState Transaction { get; private set; } @@ -90,7 +104,8 @@ public QueryTab( // A query tab's text is kept in the session, so closing loses nothing to ask about. public Task ConfirmCloseAsync(bool offerSave) => Task.FromResult(true); - public SessionTab Snapshot() => new() { Kind = TabKind.Query, Title = Title, Sql = _view.WholeText }; + public SessionTab Snapshot() => + new() { Kind = TabKind.Query, Title = Title, Sql = _view.WholeText, Database = Database }; public async Task RunAsync() { @@ -161,7 +176,7 @@ public async Task SortAsync(GridSort sort) } _sort = sort; - await RunSqlAsync(sorted).ConfigureAwait(true); + await RunSqlAsync(sorted, record: false).ConfigureAwait(true); } // The grid shows another set of the run, unsorted. @@ -202,7 +217,7 @@ private async Task RunTypedAsync(string sql) _statement = sql; _sort = GridSort.None; - await RunSqlAsync(sql).ConfigureAwait(true); + await RunSqlAsync(sql, record: true).ConfigureAwait(true); } // Typed SQL only: Commit and Roll back end a transaction at every level, and a sort runs again only a read. @@ -231,10 +246,13 @@ private async Task SafeModeAllowsAsync(string sql) } } - private Task RunSqlAsync(string sql) => _work.RunAsync(token => RunOnEditorAsync(sql, token)); + // Record: a run the user typed, which the history keeps; a sort and Commit or Roll back are not. + private Task RunSqlAsync(string sql, bool record) => + _work.RunAsync(token => RunOnEditorAsync(sql, record, token)); - private async Task RunOnEditorAsync(string sql, CancellationToken cancellationToken) + private async Task RunOnEditorAsync(string sql, bool record, CancellationToken cancellationToken) { + HistoryRun? ran = null; SetRunning(true); try { @@ -245,17 +263,43 @@ private async Task RunOnEditorAsync(string sql, CancellationToken cancellationTo } // An editor that opens as the tab closes stays in _editor, which the close disposes. - _editor ??= await _session.OpenEditorAsync(cancellationToken).ConfigureAwait(true); + if (_editor is null) + { + var opened = await _session.OpenEditorAsync(cancellationToken).ConfigureAwait(true); + _editor = opened; + if (Database is { } database && !_work.IsClosed) + { + try + { + await opened.UseDatabaseAsync(database, cancellationToken).ConfigureAwait(true); + } + catch (TableProException) + { + // Run elsewhere, the text would make its object in the wrong database, so the editor goes. + _editor = null; + await opened.DisposeAsync().ConfigureAwait(true); + throw; + } + } + } + if (_work.IsClosed) { return; } _run?.Dispose(); + var startedAt = DateTimeOffset.Now; + var started = Stopwatch.GetTimestamp(); _run = _editor.Run(sql, RunOptions(_settings()), cancellationToken); await _results.ShowRunAsync(_run, _sort, int.MaxValue, cancellationToken).ConfigureAwait(true); if (!_work.IsClosed) { + if (record) + { + ran = Ran(sql, startedAt, Stopwatch.GetElapsedTime(started)); + } + await AfterRunAsync().ConfigureAwait(true); } } @@ -270,6 +314,46 @@ private async Task RunOnEditorAsync(string sql, CancellationToken cancellationTo { SetRunning(false); } + + // After the tab is idle, and inside its work, so a close waits for the write before the session goes. + if (ran is not null && _history is { } history && !_work.IsClosed) + { + try + { + await history.RecordAsync(ran).ConfigureAwait(true); + } + catch (TableProException) + { + // The run's result stands whether or not the history kept it. + } + } + } + + // As the Linux app keeps a run: a timeout is a failure, and a run with no end is not kept. + private HistoryRun? Ran(string sql, DateTimeOffset startedAt, TimeSpan waited) + { + if (_history is null) + { + return null; + } + + var outcome = _results.Outcome; + HistoryOutcome? kept = outcome switch + { + QueryOutcome.Completed => HistoryOutcome.Succeeded.Instance, + QueryOutcome.Failed failed => new HistoryOutcome.Failed(_view.WordError(failed.Error)), + QueryOutcome.TimedOut => new HistoryOutcome.Failed(_view.TimedOutMessage), + QueryOutcome.Cancelled => HistoryOutcome.Cancelled.Instance, + _ => null, + }; + if (kept is null) + { + return null; + } + + // The time the results pane showed, where the core gave one. + var duration = outcome is QueryOutcome.Completed completed ? completed.Elapsed : waited; + return new HistoryRun(sql, startedAt, duration, _results.RowsAffected, kept); } private static QueryOptions RunOptions(AppSettings settings) => new( @@ -325,7 +409,7 @@ private async Task EndTransactionAsync(bool commit) } _statement = null; - await RunSqlAsync(WorkspaceNotices.EndTransaction(_dataModel, commit)).ConfigureAwait(true); + await RunSqlAsync(WorkspaceNotices.EndTransaction(_dataModel, commit), record: false).ConfigureAwait(true); } private void SetRunning(bool running) diff --git a/windows/src/TablePro.App.Model/SessionState.cs b/windows/src/TablePro.App.Model/SessionState.cs index 6d1f367bad..60d867e6fc 100644 --- a/windows/src/TablePro.App.Model/SessionState.cs +++ b/windows/src/TablePro.App.Model/SessionState.cs @@ -1,9 +1,14 @@ +using TablePro.Interop; + namespace TablePro.App.Model; public enum TabKind { Query, Table, + + // A function, procedure, trigger or sequence, shown as its definition. + Definition, } public sealed record SessionTab @@ -22,6 +27,119 @@ public sealed record SessionTab public int? PageSize { get; set; } public long? Offset { get; set; } + + // The ObjectKind name a table or definition tab was opened for. + public string? ObjectKind { get; set; } + + // The table a trigger fires on. + public string? ParentTable { get; set; } + + public SavedFilter? Filter { get; set; } + + // The database a query tab opened from a definition runs in, where the definition names none. + public string? Database { get; set; } + + // A name this build does not define, as from a later build, reads as the fallback. + public ObjectKind ObjectKindOr(ObjectKind fallback) => + SavedName.TryParse(ObjectKind, out ObjectKind kind) ? kind : fallback; +} + +// A table tab's filter as the session file keeps it, its operators and combinator by name. +public sealed record SavedFilter +{ + public string Combinator { get; set; } = nameof(FilterCombinator.And); + + public IReadOnlyList Rules { get; set; } = []; + + public string? RawSql { get; set; } + + public static SavedFilter From(TableFilter filter) + { + ArgumentNullException.ThrowIfNull(filter); + return new SavedFilter + { + Combinator = Enum.GetName(filter.Combinator) ?? nameof(FilterCombinator.And), + Rules = [.. filter.Rules.Select(rule => new SavedFilterRule + { + Column = rule.Column, + Operator = Enum.GetName(rule.Operator) ?? string.Empty, + Values = ValuesOf(rule.Value), + })], + RawSql = filter.RawSql, + }; + } + + // A rule this build cannot read back, as an operator a later build added, is left out rather than refused. + public TableFilter? ToFilter() + { + List rules = []; + foreach (var saved in Rules ?? []) + { + if (saved is { Column.Length: > 0 } + && SavedName.TryParse(saved.Operator, out FilterOperator op) + && TryValue(op, saved.Values ?? [], out var value)) + { + rules.Add(new FilterRule(saved.Column, op, value)); + } + } + + var raw = string.IsNullOrWhiteSpace(RawSql) ? null : RawSql; + if (rules.Count == 0 && raw is null) + { + return null; + } + + var combinator = SavedName.TryParse(Combinator, out FilterCombinator named) ? named : FilterCombinator.And; + return new TableFilter(combinator, rules, raw); + } + + private static string[] ValuesOf(FilterValue? value) => value switch + { + FilterValue.One single => [single.Value], + FilterValue.Pair pair => [pair.Low, pair.High], + FilterValue.List list => [.. list.Values], + _ => [], + }; + + // The operator says how many values it takes, as the filter bar reads it. + private static bool TryValue(FilterOperator op, IReadOnlyList values, out FilterValue? value) + { + value = null; + switch (TableFilterDraft.ShapeOf(op)) + { + case FilterValueShape.None: + return true; + case FilterValueShape.One when values.Count == 1: + value = new FilterValue.One(values[0]); + return true; + case FilterValueShape.Two when values.Count == 2: + value = new FilterValue.Pair(values[0], values[1]); + return true; + case FilterValueShape.List when values.Count > 0: + value = new FilterValue.List([.. values]); + return true; + default: + return false; + } + } +} + +public sealed record SavedFilterRule +{ + public string Column { get; set; } = string.Empty; + + public string Operator { get; set; } = string.Empty; + + // None for the null tests, one value, the two bounds of Between, or the items of In and Not in. + public IReadOnlyList Values { get; set; } = []; +} + +internal static class SavedName +{ + // Only a name this build defines, never a number, so a file from another build cannot pick some other value. + public static bool TryParse(string? name, out T value) + where T : struct, Enum => + Enum.TryParse(name, out value) && string.Equals(Enum.GetName(value), name, StringComparison.Ordinal); } // A saved connection is reopened by id, a file opened without saving by its path. diff --git a/windows/src/TablePro.App.Model/SessionTabs.cs b/windows/src/TablePro.App.Model/SessionTabs.cs new file mode 100644 index 0000000000..189e76a58d --- /dev/null +++ b/windows/src/TablePro.App.Model/SessionTabs.cs @@ -0,0 +1,31 @@ +namespace TablePro.App.Model; + +public static class SessionTabs +{ + // Definition tabs are left out: an older build reads an unknown TabKind as a broken file and loses the session. + public static (IReadOnlyList Tabs, int Selected) Persisted( + IReadOnlyList tabs, + int selected) + { + ArgumentNullException.ThrowIfNull(tabs); + List kept = []; + var keptSelected = 0; + for (var index = 0; index < tabs.Count; index++) + { + if (tabs[index].Kind == TabKind.Definition) + { + continue; + } + + // A left-out tab that was selected hands the selection to the nearest kept tab before it. + if (index <= selected) + { + keptSelected = kept.Count; + } + + kept.Add(tabs[index]); + } + + return (kept, keptSelected); + } +} diff --git a/windows/src/TablePro.App.Model/TabSession.cs b/windows/src/TablePro.App.Model/TabSession.cs index 5110915991..12e456e188 100644 --- a/windows/src/TablePro.App.Model/TabSession.cs +++ b/windows/src/TablePro.App.Model/TabSession.cs @@ -13,14 +13,23 @@ public interface ITabSession Task OpenEditorAsync(CancellationToken cancellationToken); - Task GetTableQueryAsync( + // Starts the page's read on a pooled connection and returns at once; the token stops the read as well. + Task ReadTablePageAsync( TableName table, ColumnSort? sort, + TableFilter? filter, long limit, long offset, CancellationToken cancellationToken); - Task CountRowsAsync(TableName table, CancellationToken cancellationToken); + Task CountRowsAsync(TableName table, TableFilter? filter, CancellationToken cancellationToken); + + Task GetFilterChoicesAsync(TableName table, CancellationToken cancellationToken); + + Task GetStructureAsync(TableName table, ObjectKind kind, CancellationToken cancellationToken); + + // Null when the object is gone or the engine cannot read its definition. + Task GetObjectDefinitionAsync(ObjectEntry entry, CancellationToken cancellationToken); RunGate GateRun(string sql); @@ -34,6 +43,9 @@ Task GetTableQueryAsync( ValueTask ReleaseAsync(); } +// The run gives the page's rows as an editor run's events do; columns past VisibleColumns are the row's address. +public sealed record TablePageRead(ITabRun Run, int VisibleColumns, bool CanSeek); + // A table's rows as a save sees them. `rows` is the set a page was read into, null when no set is shown. public interface ITableEditor : IDisposable { @@ -72,6 +84,8 @@ public interface ITabEditor : IAsyncDisposable bool IsLost { get; } ITabRun Run(string sql, QueryOptions options, CancellationToken cancellationToken); + + Task UseDatabaseAsync(string database, CancellationToken cancellationToken); } public interface ITabRun : IDisposable @@ -103,16 +117,33 @@ public CoreTabSession(SessionShare share) public async Task OpenEditorAsync(CancellationToken cancellationToken) => new CoreTabEditor(await _share.Session.OpenEditorAsync(cancellationToken).ConfigureAwait(true)); - public Task GetTableQueryAsync( + public async Task ReadTablePageAsync( TableName table, ColumnSort? sort, + TableFilter? filter, long limit, long offset, + CancellationToken cancellationToken) + { + var page = await _share.Session.ReadTablePageAsync(table, sort, filter, limit, offset, cancellationToken) + .ConfigureAwait(true); + return new TablePageRead(new CoreTabRun(page.Run), page.VisibleColumns, page.CanSeek); + } + + public Task CountRowsAsync(TableName table, TableFilter? filter, CancellationToken cancellationToken) => + _share.Session.CountRowsAsync(table, filter, cancellationToken); + + public Task GetFilterChoicesAsync(TableName table, CancellationToken cancellationToken) => + _share.Session.GetFilterChoicesAsync(table, cancellationToken); + + public Task GetStructureAsync( + TableName table, + ObjectKind kind, CancellationToken cancellationToken) => - _share.Session.GetTableQueryAsync(table, sort, limit, offset, cancellationToken); + _share.Session.GetStructureAsync(table, kind, cancellationToken); - public Task CountRowsAsync(TableName table, CancellationToken cancellationToken) => - _share.Session.CountRowsAsync(table, cancellationToken); + public Task GetObjectDefinitionAsync(ObjectEntry entry, CancellationToken cancellationToken) => + _share.Session.GetObjectDefinitionAsync(entry, cancellationToken); public RunGate GateRun(string sql) => _share.Session.GateRun(sql); @@ -176,6 +207,9 @@ internal sealed class CoreTabEditor(QueryEditor editor) : ITabEditor public ITabRun Run(string sql, QueryOptions options, CancellationToken cancellationToken) => new CoreTabRun(editor.Run(sql, options, cancellationToken)); + public Task UseDatabaseAsync(string database, CancellationToken cancellationToken) => + editor.UseDatabaseAsync(database, cancellationToken); + public ValueTask DisposeAsync() => editor.DisposeAsync(); } diff --git a/windows/src/TablePro.App.Model/TableFilterDraft.cs b/windows/src/TablePro.App.Model/TableFilterDraft.cs new file mode 100644 index 0000000000..b9d1e81564 --- /dev/null +++ b/windows/src/TablePro.App.Model/TableFilterDraft.cs @@ -0,0 +1,227 @@ +using TablePro.Interop; + +namespace TablePro.App.Model; + +// How many values an operator takes, which decides the boxes a rule row shows. +public enum FilterValueShape +{ + None, + One, + Two, + List, +} + +public enum FilterProblemKind +{ + NoColumn, + NoValue, + NoSecondValue, +} + +// The rule row that keeps the filter from being applied, and why; nothing is sent while one is left. +public sealed record FilterProblem(int Row, FilterProblemKind Kind); + +// One rule row as typed. Value holds the bound from for Between and the comma-separated items for In and Not in. +public sealed class FilterDraftRule +{ + public string Column { get; set; } = string.Empty; + + public FilterOperator Operator { get; set; } + + public string Value { get; set; } = string.Empty; + + // The bound to of Between, kept while another operator is picked, as macOS keeps it. + public string SecondValue { get; set; } = string.Empty; + + public FilterValueShape Shape => TableFilterDraft.ShapeOf(Operator); +} + +// What the filter bar edits: rule rows, Match all or any, and the raw SQL row, over what the engine offers. +public sealed class TableFilterDraft +{ + private readonly List _rules = []; + private readonly string[] _columns; + + public TableFilterDraft(FilterChoices choices) + { + ArgumentNullException.ThrowIfNull(choices); + Choices = choices; + _columns = [.. choices.Columns.Select(column => column.Name)]; + } + + public FilterChoices Choices { get; } + + public IReadOnlyList Columns => _columns; + + public IReadOnlyList Rules => _rules; + + // Read as Match all where the engine has no OR. + public bool MatchAny { get; set; } + + // Sent only where the engine takes a raw predicate; blank is no predicate. + public string RawSql { get; set; } = string.Empty; + + public bool AcceptsRawSql => Choices.AcceptsRawSql; + + public bool OffersMatchAny => Choices.Combinators.Contains(FilterCombinator.Or); + + // All or any means something only once two clauses meet: two rules, or a rule and the SQL. + public bool ShowsMatch => OffersMatchAny && (_rules.Count >= 2 || (_rules.Count == 1 && HasRawSql)); + + public bool IsEmpty => _rules.Count == 0 && !HasRawSql; + + private bool HasRawSql => AcceptsRawSql && !string.IsNullOrWhiteSpace(RawSql); + + public static FilterValueShape ShapeOf(FilterOperator op) => op switch + { + FilterOperator.IsNull or FilterOperator.IsNotNull => FilterValueShape.None, + FilterOperator.Between => FilterValueShape.Two, + FilterOperator.In or FilterOperator.NotIn => FilterValueShape.List, + _ => FilterValueShape.One, + }; + + // As macOS reads a list: split on commas, each item trimmed, blank items dropped. + public static IReadOnlyList SplitList(string text) + { + ArgumentNullException.ThrowIfNull(text); + return [.. text.Split(',').Select(item => item.Trim()).Where(item => item.Length > 0)]; + } + + // A filter applied before, shown again as rows; a list shows its items joined as they are typed. + public static TableFilterDraft From(TableFilter? filter, FilterChoices choices) + { + var draft = new TableFilterDraft(choices); + if (filter is null) + { + return draft; + } + + draft.MatchAny = filter.Combinator == FilterCombinator.Or; + draft.RawSql = filter.RawSql ?? string.Empty; + foreach (var rule in filter.Rules) + { + var row = new FilterDraftRule { Column = rule.Column, Operator = rule.Operator }; + switch (rule.Value) + { + case FilterValue.One single: + row.Value = single.Value; + break; + case FilterValue.Pair pair: + row.Value = pair.Low; + row.SecondValue = pair.High; + break; + case FilterValue.List list: + row.Value = string.Join(", ", list.Values); + break; + default: + break; + } + + draft._rules.Add(row); + } + + return draft; + } + + // Empty for a column the engine did not list. + public IReadOnlyList OperatorsFor(string column) + { + ArgumentNullException.ThrowIfNull(column); + return Choices.Columns.FirstOrDefault(choice => string.Equals(choice.Name, column, StringComparison.Ordinal)) + ?.Operators ?? []; + } + + // On the first column and its first operator, as the Linux app adds a rule. + public FilterDraftRule AddRule() + { + var first = Choices.Columns.Count > 0 ? Choices.Columns[0] : null; + var rule = new FilterDraftRule + { + Column = first?.Name ?? string.Empty, + Operator = first is { Operators.Count: > 0 } ? first.Operators[0] : FilterOperator.Equal, + }; + _rules.Add(rule); + return rule; + } + + public void RemoveRule(FilterDraftRule rule) + { + ArgumentNullException.ThrowIfNull(rule); + _rules.Remove(rule); + } + + // The operator stays where the new column offers it, else the column's first is picked. + public void SetColumn(FilterDraftRule rule, string column) + { + ArgumentNullException.ThrowIfNull(rule); + ArgumentNullException.ThrowIfNull(column); + rule.Column = column; + var operators = OperatorsFor(column); + if (operators.Count > 0 && !operators.Contains(rule.Operator)) + { + rule.Operator = operators[0]; + } + } + + public void Clear() + { + _rules.Clear(); + RawSql = string.Empty; + MatchAny = false; + } + + // Null with no rule and no SQL. A row missing its value is refused before anything is sent. + public bool TryBuild(out TableFilter? filter, out FilterProblem? problem) + { + filter = null; + problem = null; + List rules = []; + for (var row = 0; row < _rules.Count; row++) + { + var rule = _rules[row]; + if (ProblemOf(rule) is { } kind) + { + problem = new FilterProblem(row, kind); + return false; + } + + rules.Add(new FilterRule(rule.Column, rule.Operator, ValueOf(rule))); + } + + var raw = HasRawSql ? RawSql : null; + if (rules.Count > 0 || raw is not null) + { + var combinator = MatchAny && OffersMatchAny ? FilterCombinator.Or : FilterCombinator.And; + filter = new TableFilter(combinator, rules, raw); + } + + return true; + } + + // As macOS checks a row: a value of spaces is no value. + private static FilterProblemKind? ProblemOf(FilterDraftRule rule) + { + if (string.IsNullOrWhiteSpace(rule.Column)) + { + return FilterProblemKind.NoColumn; + } + + return rule.Shape switch + { + FilterValueShape.One or FilterValueShape.Two when string.IsNullOrWhiteSpace(rule.Value) => + FilterProblemKind.NoValue, + FilterValueShape.Two when string.IsNullOrWhiteSpace(rule.SecondValue) => FilterProblemKind.NoSecondValue, + FilterValueShape.List when SplitList(rule.Value).Count == 0 => FilterProblemKind.NoValue, + _ => null, + }; + } + + // A value is sent as typed; the core reads it as the column's type and binds it. + private static FilterValue? ValueOf(FilterDraftRule rule) => rule.Shape switch + { + FilterValueShape.None => null, + FilterValueShape.One => new FilterValue.One(rule.Value), + FilterValueShape.Two => new FilterValue.Pair(rule.Value, rule.SecondValue), + _ => new FilterValue.List(SplitList(rule.Value)), + }; +} diff --git a/windows/src/TablePro.App.Model/TablePage.cs b/windows/src/TablePro.App.Model/TablePage.cs index 48695e12e0..2cd2e192bc 100644 --- a/windows/src/TablePro.App.Model/TablePage.cs +++ b/windows/src/TablePro.App.Model/TablePage.cs @@ -6,7 +6,7 @@ public sealed record TablePage(long Offset, int Size, long Rows, long? Total, bo // Past this a typed size is a mistake, not an intent, as macOS caps its Custom size. public const int MaximumSize = 1_000_000; - // All rows can go further; a run's limit is one past the page, so the page stays under int.MaxValue. + // All rows can go further, short of int.MaxValue, which the grid counts rows in. public const int LargestSize = int.MaxValue - 1; // The sizes the macOS page size menu offers. diff --git a/windows/src/TablePro.App.Model/TableTab.cs b/windows/src/TablePro.App.Model/TableTab.cs index ec00fb6812..957ff0a71d 100644 --- a/windows/src/TablePro.App.Model/TableTab.cs +++ b/windows/src/TablePro.App.Model/TableTab.cs @@ -9,6 +9,7 @@ public enum ReloadReason Sort, Page, PageSize, + Filter, } public enum SaveBeforeClose @@ -106,14 +107,17 @@ public interface ITableTabView Task ShowPreviewAsync(IReadOnlyList statements, CoreError? failure); Task ShowSaveFailedAsync(CoreError failure, bool incomplete); + + // The rows and their edits stay as they are. + void ShowStructureFailed(CoreError failure); + + // Without the columns and their operators the filter bar has nothing to offer. + void ShowFilterChoicesFailed(CoreError failure); } -// A sort is an ORDER BY in the page's statement, so it orders the whole table, not the rows already read. +// A sort and a filter are the page's ORDER BY and WHERE, so they order and pick from the whole table. public sealed class TableTab : IWorkspaceTab { - // A page is bounded by its size, and the pager reads a short page as the last, so no byte limit cuts one. - private const long PageByteLimit = long.MaxValue; - // The refusals the core makes before it sends anything, so a save after one cannot write twice. private static readonly HashSet NothingSent = new(StringComparer.Ordinal) { @@ -130,13 +134,17 @@ public sealed class TableTab : IWorkspaceTab private readonly IResultsView _results; private readonly Func _settings; private readonly TabWork _work = new(); - private ITabEditor? _editor; + private readonly KeptRead _structure; + private readonly KeptRead _choices; private ITableEditor? _tableEditor; private bool _reopenTableEditor; private Task? _closing; private ITabRun? _run; private GridSort _sort = GridSort.None; private GridSort _shownSort = GridSort.None; + + // The filter the rows shown were read with, which a refused filter goes back to. + private TableFilter? _shownFilter; private TablePage _page; // The size this tab was given, as macOS keeps one per table tab; without one, Settings decides. @@ -154,6 +162,7 @@ public TableTab( ITableTabView view, IResultsView results, TableName table, + ObjectKind kind, Func settings, SessionTab? restore = null) { @@ -167,9 +176,21 @@ public TableTab( _results = results; _settings = settings; Table = table; + ObjectKind = kind; _chosenSize = restore?.PageSize is { } size ? Math.Clamp(size, 1, TablePage.LargestSize) : null; var first = TablePage.First(PageSize); _page = first.At((restore?.Offset ?? 0) / first.Size * first.Size); + // A restored filter stays until the user changes it, even when the first read refuses it. + Filter = restore?.Filter?.ToFilter(); + _shownFilter = Filter; + _structure = new KeptRead( + _work, + token => _session.GetStructureAsync(Table, ObjectKind, token), + _view.ShowStructureFailed); + _choices = new KeptRead( + _work, + token => _session.GetFilterChoicesAsync(Table, token), + _view.ShowFilterChoicesFailed); _results.ShownSetChanged += OnShownSetChanged; } @@ -180,6 +201,9 @@ public TableTab( public TableName Table { get; } + // What the structure is read as: a table, a view or another object with rows. + public ObjectKind ObjectKind { get; } + public string Title => Table.Name; public TabKind Kind => TabKind.Table; @@ -188,9 +212,18 @@ public TableTab( public bool IsSaving { get; private set; } - // A table tab only reads on its own connection; a save commits on the session's before it returns. + // A page is read on one of the session's pooled connections, and a save commits before it returns. public TransactionState Transaction => TransactionState.Idle; + // The filter the rows shown were read with; null reads the whole table. + public TableFilter? Filter { get; private set; } + + // Null until LoadFilterChoicesAsync has read them. + public FilterChoices? FilterChoices => _choices.Value; + + // Null until LoadStructureAsync has read it. + public TableStructure? Structure => _structure.Value; + public TablePage Page => _page; // Null until a run has read the page; a count that arrives later adds the total, as on macOS. @@ -255,8 +288,10 @@ public bool Shows(string? schema, string table) => Title = Title, Schema = Table.Schema, Table = Table.Name, + ObjectKind = Enum.GetName(ObjectKind), PageSize = _chosenSize, Offset = _page.Offset > 0 ? _page.Offset : null, + Filter = Filter is { } filter ? SavedFilter.From(filter) : null, }; // The edits name rows of the page shown, so a reload asks first while any is pending, as macOS asks. @@ -292,17 +327,37 @@ public async Task RunAsync() } } - // A refresh counts the table again and reads its columns again. + // A refresh counts the table again and reads its columns again; its structure and filter choices go stale. public async Task RefreshAsync() { if (await MayReloadNowAsync(ReloadReason.Refresh).ConfigureAwait(true)) { _page = _page with { Total = null }; _reopenTableEditor = true; - await LoadAsync(_page.Offset).ConfigureAwait(true); + _structure.MarkStale(); + _choices.MarkStale(); + await LoadAsync(_page.Offset, recount: true).ConfigureAwait(true); + } + } + + // From the first page, counting the rows the filter picks; a filter the core refuses leaves the one shown. + public async Task ApplyFilterAsync(TableFilter? filter) + { + if (!await MayReloadNowAsync(ReloadReason.Filter).ConfigureAwait(true)) + { + return; } + + Filter = filter is { Rules.Count: 0 } && string.IsNullOrWhiteSpace(filter.RawSql) ? null : filter; + await LoadAsync(0, recount: true).ConfigureAwait(true); } + // Read once per tab, and again after a refresh, since the table's columns can change. + public Task LoadFilterChoicesAsync() => _choices.LoadAsync(refresh: false); + + // Read beside the page, never in its way, and kept until a refresh of the tab or of the structure. + public Task LoadStructureAsync(bool refresh) => _structure.LoadAsync(refresh); + public async Task SortAsync(GridSort sort) { if (IsRunning || !await MayReloadNowAsync(ReloadReason.Sort).ConfigureAwait(true)) @@ -561,7 +616,7 @@ private Task Track(Task save) return save; } - // The page in flight, its count and a save stop or end before what they use is let go. + // The page in flight, its count, a structure read and a save stop or end before what they use is let go. private async Task CloseOnceAsync() { _run?.Cancel(); @@ -572,12 +627,6 @@ private async Task CloseOnceAsync() SetEdits(null, null); _tableEditor?.Dispose(); _tableEditor = null; - if (_editor is not null) - { - await _editor.DisposeAsync().ConfigureAwait(true); - _editor = null; - } - await _session.ReleaseAsync().ConfigureAwait(true); } @@ -620,15 +669,16 @@ private async Task ResizeToAsync(TablePage page, int chosen) } } - private async Task LoadAsync(long offset) + // Recount after a filter, a refresh or a save, which can change the total; it is counted once the page is read. + private async Task LoadAsync(long offset, bool recount = false) { if (!IsRunning) { - await _work.RunAsync(token => LoadPageAsync(offset, token)).ConfigureAwait(true); + await _work.RunAsync(token => LoadPageAsync(offset, recount, token)).ConfigureAwait(true); } } - private async Task LoadPageAsync(long offset, CancellationToken cancellationToken) + private async Task LoadPageAsync(long offset, bool recount, CancellationToken cancellationToken) { Finished = null; // Every reload asked first or saved, so nothing pending is lost here. @@ -636,14 +686,6 @@ private async Task LoadPageAsync(long offset, CancellationToken cancellationToke SetRunning(true); try { - if (_editor is { IsLost: true } lost) - { - _editor = null; - await lost.DisposeAsync().ConfigureAwait(true); - } - - // An editor that opens as the tab closes stays in _editor, which the close disposes. - _editor ??= await _session.OpenEditorAsync(cancellationToken).ConfigureAwait(true); await OpenTableEditorAsync(cancellationToken).ConfigureAwait(true); var page = _page.At(offset); // A default changed in Settings applies from the page holding the first row shown. @@ -655,24 +697,49 @@ private async Task LoadPageAsync(long offset, CancellationToken cancellationToke var sort = _sort.Direction == SortDirection.None ? null : new ColumnSort(_sort.Column, _sort.Direction == SortDirection.Ascending); - var query = await _session.GetTableQueryAsync(Table, sort, page.Size, page.Offset, cancellationToken) + var filter = Filter; + // The core binds the filter's values and stops the read at the page's size. + var read = await _session.ReadTablePageAsync(Table, sort, filter, page.Size, page.Offset, cancellationToken) .ConfigureAwait(true); if (_work.IsClosed) { + // The read started as the tab closed, so nothing else will stop it. + read.Run.Cancel(); + read.Run.Dispose(); return; } + if (recount) + { + // A count of what was read before would land as this page's total. + _counting?.Cancel(); + _counting = null; + } + _run?.Dispose(); - _page = page with { CanSeek = query.CanSeek }; - // The statement holds the page; a run limit one past it keeps a full page from reading as cut short. - _run = _editor.Run(query.Sql, new QueryOptions(page.Size + 1, ByteLimit: PageByteLimit), cancellationToken); - await _results.ShowRunAsync(_run, _sort, query.VisibleColumns, cancellationToken).ConfigureAwait(true); + _run = read.Run; + var shownPage = _page; + _page = page with { CanSeek = read.CanSeek, Total = recount ? null : _page.Total }; + await _results.ShowRunAsync(_run, _sort, read.VisibleColumns, cancellationToken).ConfigureAwait(true); if (_work.IsClosed) { return; } + // A read that brought no page back (refused, timed out or stopped) leaves the tab on what last read. + if (_results.Outcome is not QueryOutcome.Completed) + { + _sort = _shownSort; + Filter = _shownFilter; + _results.Sort = _shownSort; + _page = shownPage with { Rows = _results.RowCount }; + BindEdits(); + _view.UpdatePager(); + return; + } + _shownSort = _sort; + _shownFilter = filter; _page = _page with { Rows = _results.RowCount }; // The server's rows are read again, so a save whose outcome was unknown may be judged by them now. _saveBlocked = false; @@ -683,8 +750,9 @@ private async Task LoadPageAsync(long offset, CancellationToken cancellationToke { if (!_work.IsClosed) { - // An engine that reads a table only in its own order refuses the sort; the rows shown keep theirs. + // A sort the engine cannot run or a filter the core cannot build; the rows shown keep theirs. _sort = _shownSort; + Filter = _shownFilter; _results.Sort = _shownSort; _results.ShowError(exception.Error); BindEdits(); @@ -990,7 +1058,7 @@ private async Task AfterSaveAsync( { // Rows came and went, so the page is read again and the table counted again, without asking. _page = _page with { Total = null }; - await LoadAsync(_page.Offset).ConfigureAwait(true); + await LoadAsync(_page.Offset, recount: true).ConfigureAwait(true); } return true; @@ -1053,9 +1121,9 @@ private async Task CountAsync(CancellationToken closing) _counting = counting; try { - var total = await _session.CountRowsAsync(Table, counting.Token).ConfigureAwait(true); - // A count that lands as the tab closes is not shown. - if (_work.IsClosed) + var total = await _session.CountRowsAsync(Table, _shownFilter, counting.Token).ConfigureAwait(true); + // A count that lands as the tab closes, or once a read that counts again took its place, is not shown. + if (_work.IsClosed || !ReferenceEquals(_counting, counting)) { return; } @@ -1083,3 +1151,79 @@ private void SetRunning(bool running) StateChanged?.Invoke(this, EventArgs.Empty); } } + +// A read a tab keeps until a refresh makes it stale. Asked for again while it runs, it is waited for, not run twice. +internal sealed class KeptRead(TabWork work, Func> read, Action failed) + where T : class +{ + private int _version; + private int _keptVersion = -1; + private Task? _reading; + private int _readingVersion; + + public T? Value { get; private set; } + + public void MarkStale() => _version++; + + public async Task LoadAsync(bool refresh) + { + if (refresh) + { + _version++; + } + + if (Value is { } kept && _keptVersion == _version) + { + return kept; + } + + // A read that started before the refresh runs on, but only a newer one replaces what it keeps. + if (_reading is null || _readingVersion != _version) + { + _readingVersion = _version; + _reading = ReadAsync(_version); + } + + var reading = _reading; + try + { + return await reading.ConfigureAwait(true); + } + finally + { + if (ReferenceEquals(_reading, reading)) + { + _reading = null; + } + } + } + + // In the tab's work, so a close stops it and waits for it before the session is let go. + private async Task ReadAsync(int version) + { + T? value = null; + await work.RunAsync(async token => + { + try + { + value = await read(token).ConfigureAwait(true); + } + catch (TableProException exception) + { + if (!work.IsClosed) + { + failed(exception.Error); + } + + return; + } + + if (!work.IsClosed && version > _keptVersion) + { + Value = value; + _keptVersion = version; + } + }).ConfigureAwait(true); + return work.IsClosed ? null : value; + } +} diff --git a/windows/src/TablePro.App/MainWindow.xaml b/windows/src/TablePro.App/MainWindow.xaml index 57f8c7d935..42f65e274f 100644 --- a/windows/src/TablePro.App/MainWindow.xaml +++ b/windows/src/TablePro.App/MainWindow.xaml @@ -68,6 +68,13 @@ + + + + + + + @@ -133,6 +140,7 @@ + @@ -169,6 +177,12 @@ + + + + + + diff --git a/windows/src/TablePro.App/MainWindow.xaml.cs b/windows/src/TablePro.App/MainWindow.xaml.cs index c98d0ad783..98af6e7404 100644 --- a/windows/src/TablePro.App/MainWindow.xaml.cs +++ b/windows/src/TablePro.App/MainWindow.xaml.cs @@ -1150,13 +1150,13 @@ private void OnSessionEnding() private SessionState SessionSnapshot() { - var connections = _open.Select(open => open.Snapshot()).ToList(); + var connections = _open.Select(open => Persisted(open.Snapshot())).ToList(); var front = _front is null ? (connections.Count > 0 ? 0 : -1) : _open.IndexOf(_front); var windows = _tabWindows .Where(window => window._front is not null) .Select(window => new TabWindowState { - Connection = window._front!.Snapshot(), + Connection = Persisted(window._front!.Snapshot()), Placement = WindowInterop.Placement(window), }) .Where(window => window.Connection.Tabs.Count > 0) @@ -1171,6 +1171,13 @@ private SessionState SessionSnapshot() }; } + // Only session.json leaves definition tabs out; a reconnect and a tab window keep them. + private static SessionConnection Persisted(SessionConnection connection) + { + var (tabs, selected) = SessionTabs.Persisted(connection.Tabs, connection.SelectedTab); + return connection with { Tabs = tabs, SelectedTab = selected }; + } + private void UpdateCommands() { var workspace = Workspace; @@ -1183,13 +1190,17 @@ private void UpdateCommands() MenuShowObjects.IsEnabled = workspace is not null; MenuShowObjects.IsChecked = workspace?.ObjectsVisible ?? true; MenuFilterObjects.IsEnabled = workspace is not null; + MenuShowHistory.IsEnabled = workspace is not null; + MenuShowHistory.IsChecked = workspace?.HistoryVisible ?? false; + MenuExportResults.IsEnabled = workspace?.CanExport ?? false; MenuRefresh.IsEnabled = workspace is not null; MenuEditConnection.IsEnabled = front?.ConnectionId is not null; MenuReconnect.IsEnabled = front is { State: ConnectionState.Connected or ConnectionState.Failed }; MenuDisconnect.IsEnabled = front is not null; - MenuRun.IsEnabled = tab is { IsRunning: false }; + // A definition tab reads its text with Reload, not Run, and has nothing to stop. + MenuRun.IsEnabled = tab is QueryTabView { IsRunning: false } or TableTabView { IsRunning: false }; MenuRunStatement.IsEnabled = tab is QueryTabView { IsRunning: false }; - MenuStop.IsEnabled = tab is { IsRunning: true }; + MenuStop.IsEnabled = tab is QueryTabView { IsRunning: true } or TableTabView { IsRunning: true }; var editor = tab?.Editor is not null; foreach (var item in new[] { MenuUndo, MenuRedo, MenuCut, MenuPaste, MenuFind }) { @@ -1200,6 +1211,7 @@ private void UpdateCommands() MenuSelectAll.IsEnabled = tab is not null; // A query tab never edits; a table tab says what its page can do now. var table = tab as TableTabView; + MenuFilterRows.IsEnabled = table is not null; MenuSaveChanges.IsEnabled = table is { CanSave: true }; MenuDiscardChanges.IsEnabled = table is { CanDiscard: true }; MenuPreviewSql.IsEnabled = table is { CanPreview: true }; @@ -1240,8 +1252,8 @@ private async void OnCopy(object sender, RoutedEventArgs e) case { MessagesTarget: { } messages }: messages.CopySelectionToClipboard(); break; - case { GridIsTarget: true } tab: - await tab.Grid.CopySelectionAsync(); + case { GridIsTarget: true, Grid: { } grid }: + await grid.CopySelectionAsync(); break; case { Editor: { } editor }: editor.Copy(); @@ -1268,10 +1280,10 @@ private void OnSelectAll(object sender, RoutedEventArgs e) return; } - if (Workspace?.ActiveTab is { GridIsTarget: true } tab) + if (Workspace?.ActiveTab is { GridIsTarget: true, Grid: { } grid }) { - tab.Grid.SelectAll(); - tab.Grid.Focus(FocusState.Programmatic); + grid.SelectAll(); + grid.Focus(FocusState.Programmatic); return; } @@ -1311,6 +1323,26 @@ private void OnToggleObjects(object sender, RoutedEventArgs e) private void OnFilterObjects(object sender, RoutedEventArgs e) => Workspace?.FocusFilter(); + private async void OnToggleHistory(object sender, RoutedEventArgs e) + { + if (Workspace is { } workspace) + { + await workspace.ToggleHistoryAsync(); + } + + UpdateCommands(); + } + + private async void OnExportResults(object sender, RoutedEventArgs e) + { + if (Workspace is { } workspace) + { + await workspace.ExportAsync(); + } + } + + private void OnFilterRows(object sender, RoutedEventArgs e) => (Workspace?.ActiveTab as TableTabView)?.ToggleFilter(); + private async void OnRefresh(object sender, RoutedEventArgs e) { if (Workspace is { } workspace) diff --git a/windows/src/TablePro.App/Services/Workbench.cs b/windows/src/TablePro.App/Services/Workbench.cs index d0e212fc3e..02a227ba9b 100644 --- a/windows/src/TablePro.App/Services/Workbench.cs +++ b/windows/src/TablePro.App/Services/Workbench.cs @@ -7,6 +7,7 @@ internal sealed class Workbench : IDisposable { private readonly SettingsStore _settings; private bool _settingsUnsaved; + private Task? _history; public Workbench(TableProCore core, string folder) { @@ -81,6 +82,21 @@ private void SaveSettings() public void NotifyLibraryChanged() => LibraryChanged?.Invoke(this, EventArgs.Empty); + // Opened once and shared by every window. Null when the store cannot open: queries still run, unrecorded. + public Task GetHistoryAsync() => _history ??= OpenHistoryAsync(); + + private async Task OpenHistoryAsync() + { + try + { + return await Core.OpenHistoryAsync().ConfigureAwait(true); + } + catch (TableProException) + { + return null; + } + } + // The Chinook sample is copied out of the app once, so edits to it stay. public async Task InstallSampleAsync() { @@ -107,5 +123,13 @@ public async Task InstallSampleAsync() return saved; } - public void Dispose() => Core.Dispose(); + public void Dispose() + { + if (_history is { IsCompletedSuccessfully: true, Result: { } history }) + { + history.Dispose(); + } + + Core.Dispose(); + } } diff --git a/windows/src/TablePro.App/Strings/en-US/Resources.resw b/windows/src/TablePro.App/Strings/en-US/Resources.resw index af7fe3acca..c2bfc57d21 100644 --- a/windows/src/TablePro.App/Strings/en-US/Resources.resw +++ b/windows/src/TablePro.App/Strings/en-US/Resources.resw @@ -1968,4 +1968,574 @@ New fingerprint: {2} The save was sent and no answer came back, so it may have been written. + + Export results... + + + E + + + Show query history + + + H + + + Filter rows + + + F + + + Open + + + Show structure + + + Show DDL + + + Copy name + + + Function: {0} + + + Procedure: {0} + + + Trigger: {0} + + + Sequence: {0} + + + Definition commands + + + Read only + + + Open in editor + + + Open the text in a new query tab + + + Copy + + + Copy the definition + + + Reload + + + Reload from the database + + + Run in an editor, this text would not make the object as it is, so it does not open in one. + + + Definition + + + Reading the definition + + + Source unavailable + + + The object is gone, or this login cannot read its definition. + + + Try again + + + Query history commands + + + Query history + + + Clear history + + + Clear the query history of every connection + + + Close query history + + + Close query history + + + Search history + + + Search query history + + + Queries run on this connection + + + No queries yet + + + No matches + + + Query history is unavailable + + + The history database couldn't be opened, so queries aren't recorded. + + + The history couldn't be read + + + Clear the query history of every connection? + + + Every query run on any connection is removed from the history. This can't be undone. + + + Clear history + + + Open in new tab + + + Copy + + + Delete + + + {0} · {1} + + + {0:N0} ms + + + {0:N2} s + + + Failed: {0} + + + Cancelled + + + Export results + + + Format + + + Rows + + + Export... + + + CSV + + + JSON + + + Excel + + + Markdown + + + HTML + + + XML + + + SQL INSERT + + + Rows shown + + + Current page + + + Entire table + + + Results + + + Exporting to {0}... + + + Exported {0:N0} row to {1} + + + Exported {0:N0} rows to {1} + + + The export was cancelled. No file was written. + + + The rows changed before they were exported. Export them again. + + + Cancel + + + Exporting + + + The export needs a full path to a file. + + + SQL INSERT needs the table the rows came from. + + + The file couldn't be written. + + + These rows don't fit on one Excel sheet, which holds 1,048,575 rows under its header. + + + These columns don't fit on one Excel sheet, which holds 16,384 columns. + + + A value is longer than the 32,767 characters an Excel cell holds. + + + A value couldn't be read from the server, so it can't be written to the file. + + + A value holds characters XML can't carry. + + + The export read more than one result. + + + The read ended before its rows did, so no file was written. + + + The export stopped on an internal error. No file was written. + + + The connection is offline. + + + The table's rows couldn't be read with this filter or sort. + + + This object has no definition to show. + + + This database doesn't support that. + + + Data or structure + + + Data + + + Structure + + + Filter + + + Filter rows (Ctrl+Shift+F) + + + Applying or clearing filters will reload data and discard all unsaved changes. + + + Filter rows + + + Try again + + + Reading the columns + + + Raw SQL + + + WHERE clause + + + WHERE clause + + + Add filter + + + Add filter + + + Filter logic mode + + + Match all + + + Match any + + + Clear + + + Apply + + + equals + + + not equals + + + less than + + + less or equal + + + greater than + + + greater or equal + + + contains + + + starts with + + + ends with + + + like + + + not like + + + like, any case + + + is NULL + + + is not NULL + + + in list + + + not in list + + + between + + + Filter {0} column + + + Filter {0} operator + + + Filter {0} value + + + Filter {0} second value + + + and + + + Value + + + a, b, c + + + Add filter + + + Remove filter + + + Remove filter {0} + + + Select a column. + + + Value is required. + + + Second value is required for between. + + + Structure sections + + + Columns + + + Indexes + + + Foreign keys + + + DDL + + + Copy + + + Refresh structure + + + Refresh structure + + + Try again + + + Reading the structure + + + Structure + + + DDL + + + Name + + + Type + + + Nullable + + + Default + + + Primary key + + + Auto increment + + + Comment + + + Columns + + + Unique + + + Condition + + + Ref table + + + Ref columns + + + Ref schema + + + On delete + + + On update + + + YES + + + NO + + + No columns + + + No indexes + + + No foreign keys + + + No CREATE statement for this object + + + The filter names a column this table doesn't have. + + + A filter rule needs a value. + + + Between needs both values. + + + In list needs at least one value. + + + This operator takes a different number of values. + + + A filter value can't be sent as its column's type. + + + This operator doesn't apply to the column's type. + + + This engine can't use Match any. Use Match all. + + + The Raw SQL filter must be one condition, such as age > 30. + + + The filter doesn't read as a document. + + + The filter must be a document, such as { qty: { $gt: 1 } }. + + + A filter can't name a field with a dot in its name or one that starts with $. + + + This engine reads a table only in its own order, so it can't be sorted. + + + The rows are still arriving. Export them once the run ends. + + + This engine takes no Raw SQL filter. Clear the filter and add rules instead. + + + Runs in {0} + + + Queries run on every connection + + + {0} · {1} + diff --git a/windows/src/TablePro.App/Views/DefinitionTabView.xaml b/windows/src/TablePro.App/Views/DefinitionTabView.xaml new file mode 100644 index 0000000000..4f6480630d --- /dev/null +++ b/windows/src/TablePro.App/Views/DefinitionTabView.xaml @@ -0,0 +1,109 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +