From dd9cfaced843bd6750190cfff2a476267bea5cf7 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 09:46:04 +0700 Subject: [PATCH 01/16] feat(windows): keep filters per table, filter by a cell, truncate and drop tables, create and drop schemas and databases, export progress --- core/crates/ffi/src/catalog_change.rs | 625 ++++++ core/crates/ffi/src/export.rs | 160 +- core/crates/ffi/src/lib.rs | 7 +- core/crates/ffi/src/references.rs | 15 + core/crates/ffi/src/row_edits.rs | 2 +- core/crates/ffi/tests/catalog_change.rs | 268 +++ core/crates/ffi/tests/export.rs | 119 +- .../0022-browsing-through-the-ffi.md | 2 +- windows/AGENTS.md | 1 + .../decisions/0007-browsing-a-connection.md | 6 +- ...tions-table-filters-and-export-progress.md | 40 + windows/docs/decisions/README.md | 3 +- windows/src/TablePro.App.Model/AppSettings.cs | 1 + windows/src/TablePro.App.Model/CellFilters.cs | 73 + windows/src/TablePro.App.Model/CellValue.cs | 15 +- windows/src/TablePro.App.Model/ExportPlan.cs | 9 +- .../TablePro.App.Model/TableFilterMemory.cs | 145 ++ windows/src/TablePro.App.Model/TableTab.cs | 27 +- .../src/TablePro.App.Model/WorkspaceTabs.cs | 29 + windows/src/TablePro.App/MainWindow.xaml | 4 + windows/src/TablePro.App/MainWindow.xaml.cs | 18 + .../src/TablePro.App/Services/Workbench.cs | 4 + .../TablePro.App/Strings/en-US/Resources.resw | 267 +++ .../src/TablePro.App/Views/CatalogDialogs.cs | 420 ++++ .../src/TablePro.App/Views/FilterBar.xaml.cs | 11 + windows/src/TablePro.App/Views/GridMenus.cs | 18 +- .../TablePro.App/Views/QueryTabView.xaml.cs | 3 + .../TablePro.App/Views/ResultsPane.xaml.cs | 20 + .../TablePro.App/Views/StructureView.xaml.cs | 3 + .../TablePro.App/Views/TableTabView.xaml.cs | 87 +- .../Views/WorkspaceView.Catalog.cs | 317 +++ .../TablePro.App/Views/WorkspaceView.xaml.cs | 195 +- windows/src/TablePro.Interop/CatalogChange.cs | 305 +++ .../DatabaseSession.Export.cs | 81 +- windows/src/TablePro.Interop/Export.cs | 2 +- .../Generated/tablepro_ffi.cs | 1843 ++++++++++++++++- windows/src/TablePro.Interop/TableKeys.cs | 4 + .../CellFiltersTests.cs | 91 + .../ExportPlanTests.cs | 3 +- .../TableFilterMemoryTests.cs | 67 + .../TableTabFilterTests.cs | 83 +- .../WorkspaceTabsTests.cs | 17 + .../TablePro.App.UITests/CatalogTests.cs | 167 ++ .../CatalogChangeTests.cs | 67 + .../TablePro.Interop.Tests/ExportTests.cs | 90 +- 45 files changed, 5569 insertions(+), 165 deletions(-) create mode 100644 core/crates/ffi/src/catalog_change.rs create mode 100644 core/crates/ffi/tests/catalog_change.rs create mode 100644 windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md create mode 100644 windows/src/TablePro.App.Model/CellFilters.cs create mode 100644 windows/src/TablePro.App.Model/TableFilterMemory.cs create mode 100644 windows/src/TablePro.App/Views/CatalogDialogs.cs create mode 100644 windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs create mode 100644 windows/src/TablePro.Interop/CatalogChange.cs create mode 100644 windows/tests/TablePro.App.Model.Tests/CellFiltersTests.cs create mode 100644 windows/tests/TablePro.App.Model.Tests/TableFilterMemoryTests.cs create mode 100644 windows/tests/TablePro.App.UITests/CatalogTests.cs create mode 100644 windows/tests/TablePro.Interop.Tests/CatalogChangeTests.cs diff --git a/core/crates/ffi/src/catalog_change.rs b/core/crates/ffi/src/catalog_change.rs new file mode 100644 index 0000000000..c388f2537f --- /dev/null +++ b/core/crates/ffi/src/catalog_change.rs @@ -0,0 +1,625 @@ +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; + +use tablepro_core::ddl::{ + self, BuildDdlError, CatalogCapabilities, DatabaseChoices, DatabaseDraft, DatabaseNoun, DdlPlan, + DropDatabaseOptions, DropSchemaOptions, DropTableOptions, KeyspaceReplication, SchemaDraft, TruncateTableOptions, +}; +use tablepro_core::dialect::SqlDialect; +use tablepro_core::meta::TableRef; +use tablepro_core::sql_syntax::StatementAccess; +use tablepro_core::statement::SqlBuildError; +use tablepro_core::{CommitResolution, DriverError, ErrorCategory, SafeModeLevel, WriteGate, WriteReport}; +use tablepro_session::runtime::Tasks; +use tablepro_session::{OperationError, OperationKind, Session}; + +use crate::cancel_token::{CancelToken, token}; +use crate::catalog::TableName; +use crate::error::{BoxedErrorInfo, CoreError}; +use crate::row_edits::{SaveGate, resolve_commit}; +use crate::runtime::on_runtime; +use crate::session::DatabaseSession; + +// What the engine does to whole tables, schemas and databases, so a menu offers only those. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, uniffi::Record)] +pub struct CatalogAbilities { + pub truncate_table: bool, + pub truncate_cascade: bool, + pub truncate_restart_identity: bool, + // The server refuses to empty a table another table's foreign key points at, whatever its rows. + pub truncate_refused_when_referenced: bool, + pub drop_table: bool, + pub drop_table_cascade: bool, + pub create_schema: bool, + pub drop_schema: bool, + pub drop_schema_cascade: bool, + pub schema_owner: bool, + pub create_database: bool, + pub drop_database: bool, + pub drop_database_force: bool, + pub database_fields: DatabaseFieldsInfo, + // The engine calls a database a keyspace. + pub keyspaces: bool, + // A statement can run on every server of a cluster. + pub on_cluster: bool, +} + +// The options a new database takes beside its name. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, uniffi::Record)] +pub struct DatabaseFieldsInfo { + pub owner: bool, + pub template: bool, + pub encoding: bool, + pub locale: bool, + pub character_set: bool, + pub collation: bool, + pub replication: bool, + pub tablets: bool, + // MongoDB keeps a database only once it holds a collection. + pub first_collection: bool, +} + +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct CharacterSetInfo { + pub name: String, + pub default_collation: String, + pub collations: Vec, +} + +// What the server offers for a new database's options; an empty list leaves only the server's default. +#[derive(Debug, Clone, PartialEq, Eq, Default, uniffi::Record)] +pub struct DatabaseChoicesInfo { + pub owners: Vec, + pub templates: Vec, + pub encodings: Vec, + pub locales: Vec, + pub character_sets: Vec, + pub collations: Vec, + pub clusters: Vec, + pub data_centers: Vec, + pub default_owner: Option, + pub default_encoding: Option, + pub default_locale: Option, + pub default_character_set: Option, + pub default_collation: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct DataCenterReplicas { + pub data_center: String, + pub replicas: u32, +} + +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Enum)] +pub enum ReplicationArg { + Simple { factor: u32 }, + NetworkTopology { factors: Vec }, +} + +// A new database or keyspace. None leaves an option to the server. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct DatabaseDraftArg { + pub name: String, + pub owner: Option, + pub template: Option, + pub encoding: Option, + pub locale: Option, + pub character_set: Option, + pub collation: Option, + pub cluster: Option, + pub replication: Option, + pub durable_writes: bool, + pub tablets: Option, + pub first_collection: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Enum)] +pub enum CatalogOperation { + TruncateTable { + table: TableName, + cascade: bool, + restart_identity: bool, + cluster: Option, + }, + DropTable { + table: TableName, + cascade: bool, + }, + CreateSchema { + name: String, + owner: Option, + }, + DropSchema { + name: String, + cascade: bool, + }, + CreateDatabase { + draft: DatabaseDraftArg, + }, + DropDatabase { + name: String, + force: bool, + cluster: Option, + }, +} + +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Enum)] +pub enum CatalogChangeOutcome { + Done, + // Nothing changed. + Failed { error: BoxedErrorInfo }, + // The change may have landed in part or in full. + Unknown { error: BoxedErrorInfo }, +} + +#[uniffi::export] +impl DatabaseSession { + // From the connection's dialect now; nothing while there is no connection to ask. + pub fn catalog_abilities(&self) -> CatalogAbilities { + self.session() + .current_dialect() + .map(|dialect| abilities(&*dialect)) + .unwrap_or_default() + } + + // The engine keeps the database for itself, so it is never offered for a drop. + pub fn protects_database(&self, name: String) -> bool { + self.session() + .current_dialect() + .is_some_and(|dialect| ddl::protects_database(&*dialect, &name)) + } + + pub fn protects_schema(&self, name: String) -> bool { + self.session() + .current_dialect() + .is_some_and(|dialect| ddl::protects_schema(&*dialect, &name)) + } + + pub async fn database_choices(&self, cancel: Option>) -> Result { + let session = self.session().clone(); + let cancel = token(cancel); + on_runtime(self.tasks(), async move { + let choices = session + .run_cancellable(OperationKind::Metadata, &cancel, |lease, options| async move { + Ok(lease.fetch_database_choices(options).await?) + }) + .await?; + Ok(choices.into()) + }) + .await + } + + // The statements the operation runs, at the session's Safe Mode level now. An option the engine has no syntax + // for, or a name it cannot take, is refused here, before anything is shown. + pub fn plan_catalog_change(&self, operation: CatalogOperation) -> Result, CoreError> { + let session = self.session().clone(); + let dialect = session + .current_dialect() + .ok_or_else(|| CoreError::failed("ffi.not_connected", "the session has no connection"))?; + let plan = operation.plan(&*dialect).map_err(build_error)?; + let level = session.safe_mode(); + Ok(Arc::new(CatalogChange { + session, + tasks: self.tasks().clone(), + target: operation.target(), + access: operation.access(), + operation, + plan, + level, + started: AtomicBool::new(false), + })) + } + + // Read the schemas again, as after one was created or dropped. + pub async fn refresh_schemas( + &self, + cancel: Option>, + ) -> Result, CoreError> { + let session = self.session().clone(); + let cancel = token(cancel); + on_runtime(self.tasks(), async move { + let schemas = tokio::select! { + biased; + () = cancel.cancelled() => return Err(CoreError::Cancelled), + schemas = session.refresh_schemas() => schemas?, + }; + Ok(schemas.iter().map(crate::catalog::SchemaEntry::from).collect()) + }) + .await + } +} + +// One catalog change, which runs only the statements it was planned with. +#[derive(uniffi::Object)] +pub struct CatalogChange { + session: Arc, + tasks: Tasks, + operation: CatalogOperation, + target: TableRef, + access: StatementAccess, + plan: DdlPlan, + level: SafeModeLevel, + started: AtomicBool, +} + +#[uniffi::export] +impl CatalogChange { + pub fn statements(&self) -> Vec { + self.plan.sql().into_iter().map(str::to_owned).collect() + } + + // Emptying or dropping asks at every level but Read-only, which refuses; creating asks only where Safe Mode asks + // before every write. + pub fn gate(&self) -> SaveGate { + catalog_gate(self.level, self.access, self.session.read_only()) + } + + pub fn destructive(&self) -> bool { + self.access == StatementAccess::Destructive + } + + // Runs the statements shown, planned again on the connection that writes them. `confirmed` says the user answered + // the question the gate asks. Another level than the one planned at, or another plan, refuses before anything is + // sent. + pub async fn run( + &self, + confirmed: bool, + cancel: Option>, + ) -> Result { + let planned_gate = self.gate(); + match planned_gate { + SaveGate::Refuse => { + return Err(CoreError::failed( + "edit.read_only_connection", + "the connection is read-only", + )); + } + SaveGate::Confirm if !confirmed => { + return Err(CoreError::failed( + "catalog.not_confirmed", + "Safe Mode asks before this change and it was not answered", + )); + } + _ => {} + } + if self.gate_now() != planned_gate { + return Err(safe_mode_changed()); + } + if self.started.swap(true, Ordering::AcqRel) { + return Err(CoreError::failed("catalog.already_run", "this change already ran")); + } + let session = self.session.clone(); + let cancel = token(cancel); + let operation = self.operation.clone(); + let target = self.target.clone(); + let shown = self.plan.clone(); + let (access, level) = (self.access, planned_gate); + on_runtime(&self.tasks, async move { + let gated = session.clone(); + let kind = shown.kind; + let written = session + .run_cancellable(OperationKind::Save(kind), &cancel, move |lease, options| { + let (operation, target, shown, gated) = + (operation.clone(), target.clone(), shown.clone(), gated.clone()); + async move { + // Checked again on the lease, so a level saved while the change waited for it holds. + if catalog_gate(gated.safe_mode(), access, gated.read_only()) != level { + return Ok(Err(safe_mode_changed())); + } + let plan = operation.plan(&*lease.dialect()).map_err(SqlBuildError::from)?; + // A connection replaced since the plan can write other SQL than the user was shown. + if plan != shown { + return Ok(Err(CoreError::failed( + "catalog.not_as_planned", + "the connection would run other statements than the ones planned", + ))); + } + Ok(Ok(lease.write_batch(&plan.into_write_batch(target), options).await?)) + } + }) + .await; + let written = match written { + Ok(Err(refused)) => return Err(refused), + Ok(Ok(report)) => Ok(report), + Err(error) => Err(error), + }; + let resolution = resolve_commit(&session, &written).await; + let outcome = outcome(written, resolution); + if !matches!(outcome, CatalogChangeOutcome::Failed { .. }) { + session.invalidate_columns(); + } + Ok(outcome) + }) + .await + } +} + +impl CatalogChange { + fn gate_now(&self) -> SaveGate { + catalog_gate(self.session.safe_mode(), self.access, self.session.read_only()) + } +} + +impl CatalogOperation { + fn plan(&self, dialect: &dyn SqlDialect) -> Result { + match self { + Self::TruncateTable { + table, + cascade, + restart_identity, + cluster, + } => { + let options = TruncateTableOptions { + cascade: *cascade, + restart_identity: *restart_identity, + cluster: cluster.clone(), + }; + ddl::plan_truncate_table(dialect, &table.clone().into(), &options) + } + Self::DropTable { table, cascade } => { + let options = DropTableOptions { + if_exists: false, + cascade: *cascade, + }; + dialect + .ddl() + .ok_or(BuildDdlError::Unsupported(ddl::DdlOperation::DropTable))? + .plan_drop_table(&table.clone().into(), options) + } + Self::CreateSchema { name, owner } => ddl::plan_create_schema( + dialect, + &SchemaDraft { + name: name.clone(), + owner: owner.clone(), + }, + ), + Self::DropSchema { name, cascade } => { + ddl::plan_drop_schema(dialect, name, DropSchemaOptions { cascade: *cascade }) + } + Self::CreateDatabase { draft } => ddl::plan_create_database(dialect, &draft.clone().into()), + Self::DropDatabase { name, force, cluster } => ddl::plan_drop_database( + dialect, + name, + &DropDatabaseOptions { + force: *force, + cluster: cluster.clone(), + }, + ), + } + } + + // What the write batch names: the table, or a schema or database as a schema with no table. + fn target(&self) -> TableRef { + match self { + Self::TruncateTable { table, .. } | Self::DropTable { table, .. } => table.clone().into(), + Self::CreateSchema { name, .. } | Self::DropSchema { name, .. } | Self::DropDatabase { name, .. } => { + TableRef::new(Some(name.clone()), "") + } + Self::CreateDatabase { draft } => TableRef::new(Some(draft.name.clone()), ""), + } + } + + fn access(&self) -> StatementAccess { + match self { + Self::CreateSchema { .. } | Self::CreateDatabase { .. } => StatementAccess::Write, + Self::TruncateTable { .. } + | Self::DropTable { .. } + | Self::DropSchema { .. } + | Self::DropDatabase { .. } => StatementAccess::Destructive, + } + } +} + +fn catalog_gate(level: SafeModeLevel, access: StatementAccess, read_only: bool) -> SaveGate { + if read_only { + return SaveGate::Refuse; + } + match level.gate(access) { + WriteGate::Run => SaveGate::Run, + WriteGate::Confirm => SaveGate::Confirm, + WriteGate::Refuse => SaveGate::Refuse, + } +} + +fn abilities(dialect: &dyn SqlDialect) -> CatalogAbilities { + let catalog: CatalogCapabilities = ddl::catalog_capabilities(dialect); + let fields = catalog.database_fields; + let ddl = dialect.ddl(); + CatalogAbilities { + truncate_table: catalog.truncate_table, + truncate_cascade: catalog.truncate_cascade, + truncate_restart_identity: catalog.truncate_restart_identity, + truncate_refused_when_referenced: catalog.truncate_refused_when_referenced, + drop_table: ddl.is_some(), + drop_table_cascade: ddl.is_some_and(|ddl| ddl.capabilities().drop_table_cascade), + create_schema: catalog.create_schema, + drop_schema: catalog.drop_schema, + drop_schema_cascade: catalog.drop_schema_cascade, + schema_owner: catalog.schema_owner, + create_database: catalog.create_database, + drop_database: catalog.drop_database, + drop_database_force: catalog.drop_database_force, + database_fields: DatabaseFieldsInfo { + owner: fields.owner, + template: fields.template, + encoding: fields.encoding, + locale: fields.locale, + character_set: fields.character_set, + collation: fields.collation, + replication: fields.replication, + tablets: fields.tablets, + first_collection: fields.first_collection, + }, + keyspaces: catalog.database_noun == DatabaseNoun::Keyspace, + on_cluster: catalog.on_cluster, + } +} + +impl From for DatabaseDraft { + fn from(draft: DatabaseDraftArg) -> Self { + Self { + name: draft.name, + owner: draft.owner, + template: draft.template, + encoding: draft.encoding, + locale: draft.locale, + character_set: draft.character_set, + collation: draft.collation, + cluster: draft.cluster, + replication: draft.replication.map(|replication| match replication { + ReplicationArg::Simple { factor } => KeyspaceReplication::Simple { factor }, + ReplicationArg::NetworkTopology { factors } => KeyspaceReplication::NetworkTopology { + factors: factors + .into_iter() + .map(|factor| (factor.data_center, factor.replicas)) + .collect(), + }, + }), + durable_writes: draft.durable_writes, + tablets: draft.tablets, + first_collection: draft.first_collection, + } + } +} + +impl From for DatabaseChoicesInfo { + fn from(choices: DatabaseChoices) -> Self { + Self { + owners: choices.owners, + templates: choices.templates, + encodings: choices.encodings, + locales: choices.locales, + character_sets: choices + .character_sets + .into_iter() + .map(|set| CharacterSetInfo { + name: set.name, + default_collation: set.default_collation, + collations: set.collations, + }) + .collect(), + collations: choices.collations, + clusters: choices.clusters, + data_centers: choices.data_centers, + default_owner: choices.defaults.owner, + default_encoding: choices.defaults.encoding, + default_locale: choices.defaults.locale, + default_character_set: choices.defaults.character_set, + default_collation: choices.defaults.collation, + } + } +} + +// What the server holds now decides, as a row save reads it. +fn outcome( + written: Result, + resolution: Option>, +) -> CatalogChangeOutcome { + let error = match written { + Ok(_) => return CatalogChangeOutcome::Done, + Err(error) => error, + }; + let info = || CoreError::from(&error).into_info(); + match &error { + OperationError::Driver(DriverError::CommitOutcomeUnknown { .. }) => match resolution { + Some(Ok(CommitResolution::Applied)) => CatalogChangeOutcome::Done, + Some(Ok(CommitResolution::NotApplied)) => CatalogChangeOutcome::Failed { error: info() }, + _ => CatalogChangeOutcome::Unknown { error: info() }, + }, + OperationError::Driver(DriverError::PartiallyApplied { .. }) => CatalogChangeOutcome::Unknown { error: info() }, + OperationError::Driver(driver) + if matches!( + driver.category(), + ErrorCategory::Interrupted | ErrorCategory::Timeout | ErrorCategory::ConnectionLost + ) => + { + CatalogChangeOutcome::Unknown { error: info() } + } + OperationError::Cancelled | OperationError::Panicked(_) | OperationError::Closed => { + CatalogChangeOutcome::Unknown { error: info() } + } + _ => CatalogChangeOutcome::Failed { error: info() }, + } +} + +fn safe_mode_changed() -> CoreError { + CoreError::failed( + "catalog.safe_mode_changed", + "the connection's Safe Mode changed after the change was planned", + ) +} + +// The engine's refusal, before any SQL: an operation it has no syntax for, or a name or option it cannot take. +fn build_error(error: BuildDdlError) -> CoreError { + let code = match &error { + BuildDdlError::Unsupported(_) => "catalog.unsupported", + BuildDdlError::InvalidOption { + option: ddl::CatalogOption::Name, + .. + } => "catalog.invalid_name", + BuildDdlError::InvalidOption { .. } => "catalog.invalid_option", + _ => "catalog.refused", + }; + CoreError::failed(code, error.to_string()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn table(name: &str) -> TableName { + TableName { + schema: None, + name: name.to_owned(), + } + } + + #[test] + fn emptying_or_dropping_asks_at_every_level_but_read_only_and_creating_asks_only_when_writes_ask() { + let drop = CatalogOperation::DropTable { + table: table("t"), + cascade: false, + }; + let create = CatalogOperation::CreateSchema { + name: "s".to_owned(), + owner: None, + }; + let gates = |operation: &CatalogOperation| { + SafeModeLevel::ALL.map(|level| catalog_gate(level, operation.access(), false)) + }; + + assert_eq!(gates(&drop), [SaveGate::Confirm, SaveGate::Confirm, SaveGate::Refuse]); + assert_eq!(gates(&create), [SaveGate::Run, SaveGate::Confirm, SaveGate::Refuse]); + assert_eq!( + catalog_gate(SafeModeLevel::Off, create.access(), true), + SaveGate::Refuse + ); + } + + #[test] + fn a_database_names_itself_as_a_schema_with_no_table() { + let drop = CatalogOperation::DropDatabase { + name: "shop".to_owned(), + force: false, + cluster: None, + }; + + assert_eq!(drop.target(), TableRef::new(Some("shop".to_owned()), "")); + } + + #[test] + fn a_refusal_carries_the_code_the_app_words() { + assert_eq!( + build_error(BuildDdlError::Unsupported(ddl::DdlOperation::CreateSchema)).code(), + "catalog.unsupported" + ); + assert_eq!( + build_error(BuildDdlError::InvalidOption { + option: ddl::CatalogOption::Name, + value: " ".to_owned(), + }) + .code(), + "catalog.invalid_name" + ); + } +} diff --git a/core/crates/ffi/src/export.rs b/core/crates/ffi/src/export.rs index 63d2c8166c..cf46c7145a 100644 --- a/core/crates/ffi/src/export.rs +++ b/core/crates/ffi/src/export.rs @@ -1,13 +1,13 @@ use std::fs::File; use std::io::Write; use std::path::{Path, PathBuf}; -use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU64, 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::{ColumnKind, ColumnType, ResultColumn}; -use tablepro_core::dialect::SqlDialect; +use tablepro_core::dialect::{DatabaseLayout, SqlDialect}; use tablepro_core::edit::parse_filter_text; use tablepro_core::export::{ CsvEncoder, CsvOptions, EncodeError, HtmlEncoder, JsonArrayEncoder, MarkdownEncoder, RenderError, RowEncoder, @@ -35,6 +35,28 @@ const CHUNK_BYTES: usize = 64 * 1024; // Held rows encoded between two looks at the cancel. const ROWS_PER_BATCH: u64 = 1_024; +// The rows an export has written so far, which the app reads while it runs. +#[derive(Debug, Default, uniffi::Object)] +pub struct ExportProgress(AtomicU64); + +#[uniffi::export] +impl ExportProgress { + #[uniffi::constructor] + pub fn new() -> Arc { + Arc::new(Self::default()) + } + + pub fn rows(&self) -> u64 { + self.0.load(Ordering::Relaxed) + } +} + +impl ExportProgress { + fn add(&self, rows: u64) { + self.0.fetch_add(rows, Ordering::Relaxed); + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] pub enum ExportFormatTag { Csv, @@ -48,14 +70,22 @@ pub enum ExportFormatTag { #[uniffi::export] impl DatabaseSession { - // Only the first `visible_columns` columns, since a table page carries row addresses after them. + // Only the first `visible_columns` columns, since a table page carries row addresses after them. SQL INSERT with no + // table writes into the one table every column was read from, else into `query_results`. `database` is where the + // rows were read when that is not the session's own, as after a USE. + #[expect( + clippy::too_many_arguments, + reason = "the rows, their format, table, database and file, and the app's progress and cancel" + )] pub async fn export_rows( &self, rows: Arc, visible_columns: u32, format: ExportFormatTag, table: Option, + database: Option, path: String, + progress: Option>, cancel: Option>, ) -> Result { let session = self.session().clone(); @@ -74,26 +104,44 @@ impl DatabaseSession { if format == ExportFormatTag::Excel && rows.row_count() > XLSX_MAX_DATA_ROWS as u64 { return Err(sheet_full()); } + let held = rows.result_columns(); + let visible = usize::try_from(visible_columns).unwrap_or(usize::MAX).min(held.len()); + let mut columns: Arc<[ResultColumn]> = held.iter().take(visible).cloned().collect(); let insert = match (format, &table) { - (ExportFormatTag::SqlInsert, None) => return Err(needs_table()), (ExportFormatTag::SqlInsert, Some(table)) => { - let columns = tokio::select! { + let catalog = tokio::select! { biased; () = cancel.cancelled() => return Err(CoreError::Cancelled), columns = session.columns(table.clone()) => columns?, }; - Some(InsertTarget::of(table, &columns)) + Some(InsertTarget::of(table, &catalog)) } + (ExportFormatTag::SqlInsert, None) => match source_table(&columns) { + Some((source, named)) => { + // Read only to leave out what the server computes; a table that cannot be read leaves none out. + let layout = session.current_dialect().map(|dialect| dialect.database_layout()); + let elsewhere = !catalog_describes(database.as_deref(), layout); + let catalog = match elsewhere { + true => None, + false => tokio::select! { + biased; + () = cancel.cancelled() => return Err(CoreError::Cancelled), + columns = session.columns(source.clone()) => columns.ok(), + }, + }; + columns = named; + Some(InsertTarget::of(&source, catalog.as_deref().unwrap_or_default())) + } + None => Some(InsertTarget::of(&TableRef::unqualified(RESULT_TABLE), &[])), + }, _ => None, }; let recipe = Recipe { format, title: title(table.as_ref(), &target), insert, + progress, }; - 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( @@ -111,6 +159,10 @@ impl DatabaseSession { } // Every row the filter selects, in the sort's order, streamed to the file on a connection of the read's own. + #[expect( + clippy::too_many_arguments, + reason = "the table's sort and filter, the format and file, and the app's progress and cancel" + )] pub async fn export_table( &self, table: TableName, @@ -118,6 +170,7 @@ impl DatabaseSession { filter: Option, format: ExportFormatTag, path: String, + progress: Option>, cancel: Option>, ) -> Result { let session = self.session().clone(); @@ -143,6 +196,7 @@ impl DatabaseSession { format, title: title(Some(&table), &target), insert: (format == ExportFormatTag::SqlInsert).then(|| InsertTarget::of(&table, &columns)), + progress, }; let read = Arc::new(TableRead { table, @@ -199,11 +253,15 @@ impl TableRead { } } +// The table a query result's INSERT statements write into when its columns name no one table. +const RESULT_TABLE: &str = "query_results"; + #[derive(Clone)] struct Recipe { format: ExportFormatTag, title: String, insert: Option, + progress: Option>, } #[derive(Clone)] @@ -348,6 +406,7 @@ impl FileSink { .await?; self.writer = Some(Writer::Sheet(sheet)); self.rows += count; + self.written(count); Ok(()) } None => Err(CoreError::failed( @@ -358,16 +417,27 @@ impl FileSink { } async fn encode(&mut self, encoder: &mut dyn RowEncoder, rows: &[Vec]) -> Result<(), CoreError> { + let mut unreported = 0; for row in rows { encoder.row(&mut self.buffer, &mut row.iter()).map_err(encode_error)?; self.rows += 1; + unreported += 1; if self.buffer.len() >= CHUNK_BYTES { self.flush().await?; + self.written(std::mem::take(&mut unreported)); } } + self.written(unreported); Ok(()) } + // Counted as encoded, which is a chunk ahead of the disk at most. + fn written(&self, rows: u64) { + if let Some(progress) = &self.recipe.progress { + progress.add(rows); + } + } + async fn flush(&mut self) -> Result<(), CoreError> { if self.buffer.is_empty() { return Ok(()); @@ -436,6 +506,30 @@ impl ResultSink for FileSink { } } +// Where each database holds schemas of its own, an origin names no database, so the session's catalog describes the +// table only when the rows were read in the session's own database. `read_in` is None there. +fn catalog_describes(read_in: Option<&str>, layout: Option) -> bool { + read_in.is_none() || layout != Some(DatabaseLayout::WithSchemas) +} + +// The table every column was read from, as the driver traced it, with each column under the name the table gives it, +// so an alias is written as the column it reads. A column it could not trace, or one read twice, names none. +fn source_table(columns: &[ResultColumn]) -> Option<(TableRef, Arc<[ResultColumn]>)> { + let table = &columns.first()?.origin.as_ref()?.table; + let mut named: Vec = Vec::with_capacity(columns.len()); + for column in columns { + let origin = column.origin.as_ref().filter(|origin| origin.table == *table)?; + if named.iter().any(|seen| seen.name == origin.column) { + return None; + } + named.push(ResultColumn { + name: origin.column.clone(), + ..column.clone() + }); + } + Some((table.clone(), named.into())) +} + // 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, @@ -738,6 +832,54 @@ mod tests { }) } + #[test] + fn a_result_read_after_a_move_to_another_database_with_schemas_of_its_own_reads_no_catalog() { + assert!(catalog_describes(None, Some(DatabaseLayout::WithSchemas))); + assert!(!catalog_describes(Some("sales"), Some(DatabaseLayout::WithSchemas))); + assert!(catalog_describes(Some("sales"), Some(DatabaseLayout::AsSchemas))); + assert!(catalog_describes(Some("sales"), Some(DatabaseLayout::Single))); + } + + #[test] + fn a_result_writes_into_its_one_traced_table_under_the_tables_column_names() { + let traced = |name: &str, table: &str| { + array_of(name, bigint()).with_origin(Some(tablepro_core::column::ColumnOrigin::new( + TableRef::new(Some("public".to_owned()), table), + name, + ))) + }; + let orders = TableRef::new(Some("public".to_owned()), "orders"); + + let mut aliased = traced("total", "orders"); + aliased.name = "amount".to_owned(); + let names = |found: Option<(TableRef, Arc<[ResultColumn]>)>| { + found.map(|(table, columns)| { + ( + table, + columns.iter().map(|column| column.name.clone()).collect::>(), + ) + }) + }; + + assert_eq!( + names(source_table(&[traced("id", "orders"), aliased])), + Some((orders, vec!["id".to_owned(), "total".to_owned()])) + ); + assert_eq!( + names(source_table(&[traced("id", "orders"), traced("name", "people")])), + None + ); + assert_eq!( + names(source_table(&[traced("id", "orders"), array_of("sum", bigint())])), + None + ); + assert_eq!( + names(source_table(&[traced("id", "orders"), traced("id", "orders")])), + None + ); + assert_eq!(names(source_table(&[])), None); + } + #[test] fn an_array_comes_back_element_by_element_as_a_streamed_read_gives_it() { let columns: Arc<[ResultColumn]> = vec![ diff --git a/core/crates/ffi/src/lib.rs b/core/crates/ffi/src/lib.rs index 8b74ac7ab0..b92f1b7a40 100644 --- a/core/crates/ffi/src/lib.rs +++ b/core/crates/ffi/src/lib.rs @@ -3,6 +3,7 @@ mod account; mod cancel_token; mod catalog; +mod catalog_change; mod cloud; mod connection_url; mod core_handle; @@ -49,6 +50,10 @@ pub use account::{ }; pub use cancel_token::CancelToken; pub use catalog::{ColumnEntry, ObjectEntry, ObjectKindTag, ObjectScopeArg, SchemaEntry, TableName}; +pub use catalog_change::{ + CatalogAbilities, CatalogChange, CatalogChangeOutcome, CatalogOperation, CharacterSetInfo, DataCenterReplicas, + DatabaseChoicesInfo, DatabaseDraftArg, DatabaseFieldsInfo, ReplicationArg, +}; pub use cloud::{CloudConnectionFields, CloudGroupFields, CloudLibraryPull, CloudLibraryRecords}; pub use connection_url::{ParsedConnectionUrl, parse_connection_url}; pub use core_handle::{Core, FileOpenRequest}; @@ -56,7 +61,7 @@ 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 export::{ExportFormatTag, ExportProgress}; pub use favourites::{DatabaseEnvironmentTag, FavouriteDatabaseInfo, FavouriteStore, FavouriteTableInfo}; pub use filter::{ CombinatorTag, FilterChoices, FilterColumn, FilterOpTag, FilterRuleArg, FilterValueArg, TableFilterArg, diff --git a/core/crates/ffi/src/references.rs b/core/crates/ffi/src/references.rs index 963c24c841..a9892a695e 100644 --- a/core/crates/ffi/src/references.rs +++ b/core/crates/ffi/src/references.rs @@ -146,6 +146,21 @@ impl TableKeys { }) } + // The other tables whose foreign keys point at this one, each once; a key of the table on itself goes with it. + pub fn referencing_tables(&self) -> Vec { + let mut tables: Vec = Vec::new(); + for key in self.inbound.iter().filter(|key| key.table != self.table) { + let table = TableName { + schema: key.table.schema.clone(), + name: key.table.name.clone(), + }; + if !tables.contains(&table) { + tables.push(table); + } + } + tables + } + // The filter of one key `row_references` offered, built from the row's stored values. pub fn follow_reference( &self, diff --git a/core/crates/ffi/src/row_edits.rs b/core/crates/ffi/src/row_edits.rs index e6664657a0..a740e0cff3 100644 --- a/core/crates/ffi/src/row_edits.rs +++ b/core/crates/ffi/src/row_edits.rs @@ -688,7 +688,7 @@ fn same_plan(left: &SavePlan, right: &SavePlan) -> bool { } // Ask the server what became of a commit whose acknowledgement was lost, where the engine left a way to ask. -async fn resolve_commit( +pub(crate) async fn resolve_commit( session: &Session, written: &Result, ) -> Option> { diff --git a/core/crates/ffi/tests/catalog_change.rs b/core/crates/ffi/tests/catalog_change.rs new file mode 100644 index 0000000000..0b289da4a8 --- /dev/null +++ b/core/crates/ffi/tests/catalog_change.rs @@ -0,0 +1,268 @@ +#![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::{ + CatalogChangeOutcome, CatalogOperation, Core, DatabaseSession, FileOpenRequest, HostConfig, HostSecretError, + HostSecrets, ObjectScopeArg, ResultSet, RunListener, RunOutcome, RunSettings, SafeModeTag, SaveGate, SecretItemTag, + SecretKindTag, 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, 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 open(dir: &Path, file: &str, driver: Option<&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(file).display().to_string(), + driver_id: driver.map(str::to_owned), + read_only: false, + create: true, + }; + let session = core.open_file(request, None).await.unwrap(); + (core, session) +} + +async fn count(session: &DatabaseSession, table: &str) -> Option { + let set = run(session, &format!("SELECT count(*) FROM {table}")).await.unwrap(); + set.cell_text(0, 0).unwrap() +} + +async fn tables(session: &DatabaseSession) -> Vec { + session + .refresh_objects(ObjectScopeArg::DefaultSchema, None) + .await + .unwrap() + .into_iter() + .map(|object| object.name) + .collect() +} + +#[tokio::test] +async fn a_table_is_emptied_only_once_the_question_is_answered_and_only_once() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = open(dir.path(), "app.sqlite", None).await; + run( + &session, + "CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT); INSERT INTO items (name) VALUES ('pen'), ('ink')", + ) + .await; + let abilities = session.catalog_abilities(); + let truncate = CatalogOperation::TruncateTable { + table: table("items"), + cascade: false, + restart_identity: false, + cluster: None, + }; + + let change = session.plan_catalog_change(truncate).unwrap(); + let unanswered = change.run(false, None).await.unwrap_err(); + let kept = count(&session, "items").await; + let outcome = change.run(true, None).await.unwrap(); + let again = change.run(true, None).await.unwrap_err(); + + assert!(abilities.truncate_table && abilities.truncate_refused_when_referenced && abilities.drop_table); + assert!(!abilities.create_schema && !abilities.create_database); + assert_eq!(change.statements(), ["DELETE FROM \"items\""]); + assert_eq!(change.gate(), SaveGate::Confirm); + assert!(change.destructive()); + assert_eq!(unanswered.code(), "catalog.not_confirmed"); + assert_eq!(kept.as_deref(), Some("2")); + assert_eq!(outcome, CatalogChangeOutcome::Done); + assert_eq!(count(&session, "items").await.as_deref(), Some("0")); + assert_eq!(again.code(), "catalog.already_run"); +} + +#[tokio::test] +async fn a_dropped_table_leaves_the_catalog_and_the_tables_pointing_at_one_are_named() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = open(dir.path(), "app.sqlite", None).await; + run( + &session, + "CREATE TABLE authors (id INTEGER PRIMARY KEY, parent INTEGER REFERENCES authors(id)); \ + CREATE TABLE books (id INTEGER PRIMARY KEY, author INTEGER REFERENCES authors(id)); \ + CREATE TABLE reviews (id INTEGER PRIMARY KEY, author INTEGER REFERENCES authors(id), \ + book INTEGER REFERENCES books(id))", + ) + .await; + + let keys = session.table_keys(table("authors"), None).await.unwrap(); + let drop = session + .plan_catalog_change(CatalogOperation::DropTable { + table: table("reviews"), + cascade: false, + }) + .unwrap(); + let outcome = drop.run(true, None).await.unwrap(); + + assert_eq!(keys.referencing_tables(), [table("books"), table("reviews")]); + assert_eq!(drop.statements(), ["DROP TABLE \"reviews\""]); + assert_eq!(outcome, CatalogChangeOutcome::Done); + assert!(!tables(&session).await.contains(&"reviews".to_owned())); +} + +#[tokio::test] +async fn an_operation_the_engine_has_no_syntax_for_is_refused_before_anything_is_shown() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = open(dir.path(), "app.sqlite", None).await; + + let schema = session + .plan_catalog_change(CatalogOperation::CreateSchema { + name: "sales".to_owned(), + owner: None, + }) + .err() + .unwrap(); + let cascade = session + .plan_catalog_change(CatalogOperation::TruncateTable { + table: table("items"), + cascade: true, + restart_identity: false, + cluster: None, + }) + .err() + .unwrap(); + + assert_eq!(schema.code(), "catalog.unsupported"); + assert_eq!(cascade.code(), "catalog.unsupported"); +} + +#[tokio::test] +async fn a_schema_is_created_and_dropped_and_the_schemas_are_read_again() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = open(dir.path(), "app.duckdb", Some("duckdb")).await; + let before = session.schemas(None).await.unwrap(); + + let create = session + .plan_catalog_change(CatalogOperation::CreateSchema { + name: "sales".to_owned(), + owner: None, + }) + .unwrap(); + let created = create.run(false, None).await.unwrap(); + let after_create = session.refresh_schemas(None).await.unwrap(); + let drop = session + .plan_catalog_change(CatalogOperation::DropSchema { + name: "sales".to_owned(), + cascade: true, + }) + .unwrap(); + let dropped = drop.run(true, None).await.unwrap(); + let after_drop = session.refresh_schemas(None).await.unwrap(); + + assert!(session.catalog_abilities().create_schema); + assert!(session.protects_schema("main".to_owned())); + assert!(!session.protects_schema("sales".to_owned())); + assert_eq!(create.statements(), ["CREATE SCHEMA \"sales\""]); + assert_eq!(create.gate(), SaveGate::Run); + assert_eq!(drop.statements(), ["DROP SCHEMA \"sales\" CASCADE"]); + assert_eq!( + (created, dropped), + (CatalogChangeOutcome::Done, CatalogChangeOutcome::Done) + ); + assert!(!names_of(&before).contains(&"sales".to_owned())); + assert!(names_of(&after_create).contains(&"sales".to_owned())); + assert!(!names_of(&after_drop).contains(&"sales".to_owned())); +} + +#[tokio::test] +async fn a_level_saved_after_the_plan_refuses_it_before_anything_is_sent() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = open(dir.path(), "app.duckdb", Some("duckdb")).await; + + let create = session + .plan_catalog_change(CatalogOperation::CreateSchema { + name: "sales".to_owned(), + owner: None, + }) + .unwrap(); + assert!(session.set_safe_mode(SafeModeTag::ConfirmWrites)); + let refused = create.run(false, None).await.unwrap_err(); + let asked = session + .plan_catalog_change(CatalogOperation::CreateSchema { + name: "sales".to_owned(), + owner: None, + }) + .unwrap(); + + assert_eq!(create.gate(), SaveGate::Run); + assert_eq!(refused.code(), "catalog.safe_mode_changed"); + assert_eq!(asked.gate(), SaveGate::Confirm); + assert!( + !names_of(&session.refresh_schemas(None).await.unwrap()).contains(&"sales".to_owned()), + "nothing ran" + ); +} + +fn names_of(schemas: &[tablepro_ffi::SchemaEntry]) -> Vec { + schemas.iter().map(|schema| schema.name.clone()).collect() +} diff --git a/core/crates/ffi/tests/export.rs b/core/crates/ffi/tests/export.rs index 7aca63c134..1f08a49cc0 100644 --- a/core/crates/ffi/tests/export.rs +++ b/core/crates/ffi/tests/export.rs @@ -10,9 +10,9 @@ 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, + CancelToken, ColumnSort, CombinatorTag, Core, CoreError, DatabaseSession, ExportFormatTag, ExportProgress, + FileOpenRequest, FilterOpTag, FilterRuleArg, FilterValueArg, HostConfig, HostSecretError, HostSecrets, ResultSet, + RunListener, RunOutcome, RunSettings, SecretItemTag, SecretKindTag, TableFilterArg, TableName, }; struct NoSecrets; @@ -129,11 +129,20 @@ async fn csv_and_json_of_a_result_set_hold_exactly_its_rows() { std::fs::write(&csv, "an older export").unwrap(); let csv_rows = session - .export_rows(set.clone(), 4, ExportFormatTag::Csv, None, spelled(&csv), None) + .export_rows( + set.clone(), + 4, + ExportFormatTag::Csv, + None, + None, + spelled(&csv), + None, + None, + ) .await .unwrap(); let json_rows = session - .export_rows(set, 4, ExportFormatTag::Json, None, spelled(&json), None) + .export_rows(set, 4, ExportFormatTag::Json, None, None, spelled(&json), None, None) .await .unwrap(); @@ -165,7 +174,7 @@ async fn an_excel_export_is_a_workbook() { let path = dir.path().join("people.xlsx"); let rows = session - .export_rows(set, 4, ExportFormatTag::Excel, None, spelled(&path), None) + .export_rows(set, 4, ExportFormatTag::Excel, None, None, spelled(&path), None, None) .await .unwrap(); @@ -206,8 +215,10 @@ async fn a_page_exports_only_its_visible_columns() { visible, ExportFormatTag::Csv, Some(table("notes")), + None, spelled(&path), None, + None, ) .await .unwrap(); @@ -237,6 +248,7 @@ async fn sql_insert_of_a_table_names_it_and_leaves_out_what_the_server_computes( ExportFormatTag::SqlInsert, spelled(&path), None, + None, ) .await .unwrap(); @@ -250,19 +262,92 @@ async fn sql_insert_of_a_table_names_it_and_leaves_out_what_the_server_computes( } #[tokio::test] -async fn sql_insert_without_a_table_is_refused_before_any_file() { +async fn sql_insert_of_a_query_result_writes_into_the_table_its_columns_came_from() { let dir = tempfile::tempdir().unwrap(); let (_core, session) = fixture(dir.path()).await; - let set = run(&session, "SELECT id, name FROM people").await.unwrap(); + let set = run(&session, "SELECT id, name FROM people ORDER BY id").await.unwrap(); let path = dir.path().join("people.sql"); - let error = session - .export_rows(set, 2, ExportFormatTag::SqlInsert, None, spelled(&path), None) + let rows = session + .export_rows( + set, + 2, + ExportFormatTag::SqlInsert, + None, + None, + spelled(&path), + None, + None, + ) .await - .unwrap_err(); + .unwrap(); - assert_eq!(error.code(), "ffi.export_needs_table"); - assert_eq!(named(dir.path(), "people.sql"), Vec::::new()); + assert_eq!(rows, 2); + assert_eq!( + text(&path), + "INSERT INTO \"people\" (\"id\", \"name\") VALUES (1, 'Ada');\n\ + INSERT INTO \"people\" (\"id\", \"name\") VALUES (2, 'Grace, \"Amazing\"');\n" + ); +} + +#[tokio::test] +async fn sql_insert_of_an_aliased_result_names_the_tables_columns() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + let set = run(&session, "SELECT id, name AS label FROM people ORDER BY id") + .await + .unwrap(); + let path = dir.path().join("aliased.sql"); + + session + .export_rows( + set, + 2, + ExportFormatTag::SqlInsert, + None, + None, + spelled(&path), + None, + None, + ) + .await + .unwrap(); + + assert!( + text(&path).starts_with("INSERT INTO \"people\" (\"id\", \"name\") VALUES (1, 'Ada');"), + "{}", + text(&path) + ); +} + +#[tokio::test] +async fn sql_insert_of_a_computed_result_writes_into_query_results() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + let set = run(&session, "SELECT id, upper(name) AS loud FROM people ORDER BY id") + .await + .unwrap(); + let path = dir.path().join("loud.sql"); + + session + .export_rows( + set, + 2, + ExportFormatTag::SqlInsert, + None, + None, + spelled(&path), + None, + None, + ) + .await + .unwrap(); + + assert!( + text(&path).starts_with("INSERT INTO \"query_results\" (\"id\", \"loud\") VALUES (1, 'ADA');"), + "{}", + text(&path) + ); } #[tokio::test] @@ -292,6 +377,7 @@ async fn a_table_export_writes_every_row_the_filter_selects_in_the_sort_order() ascending: false, }; let path = dir.path().join("numbers.csv"); + let progress = ExportProgress::new(); let rows = session .export_table( @@ -300,6 +386,7 @@ async fn a_table_export_writes_every_row_the_filter_selects_in_the_sort_order() Some(filter), ExportFormatTag::Csv, spelled(&path), + Some(progress.clone()), None, ) .await @@ -308,6 +395,7 @@ async fn a_table_export_writes_every_row_the_filter_selects_in_the_sort_order() let written = text(&path); let lines: Vec<&str> = written.lines().collect(); assert_eq!(rows, 2_000); + assert_eq!(progress.rows(), 2_000); assert_eq!(lines.len(), 2_001); assert_eq!(lines[0], "n,label"); assert_eq!(lines[1], "3000,n3000"); @@ -331,6 +419,7 @@ async fn a_cancelled_export_leaves_no_file_and_no_part_file() { None, ExportFormatTag::Csv, path, + None, Some(cancel.clone()), ); // Cancelled once its part file is on disk, so the cancel lands mid-write. @@ -359,7 +448,7 @@ async fn a_failed_export_keeps_the_file_it_would_have_replaced() { std::fs::write(&path, "an older export").unwrap(); let error = session - .export_rows(set, 2, ExportFormatTag::Xml, None, spelled(&path), None) + .export_rows(set, 2, ExportFormatTag::Xml, None, None, spelled(&path), None, None) .await .unwrap_err(); @@ -394,7 +483,7 @@ async fn rows_still_arriving_are_not_exported() { let path = spelled(&dir.path().join("endless.csv")); let refused = session - .export_rows(set, 1, ExportFormatTag::Csv, None, path, None) + .export_rows(set, 1, ExportFormatTag::Csv, None, None, path, None, None) .await .unwrap_err(); run.cancel(); diff --git a/core/docs/decisions/0022-browsing-through-the-ffi.md b/core/docs/decisions/0022-browsing-through-the-ffi.md index f448107fb2..00b0ae3278 100644 --- a/core/docs/decisions/0022-browsing-through-the-ffi.md +++ b/core/docs/decisions/0022-browsing-through-the-ffi.md @@ -17,7 +17,7 @@ The FFI exposes what the Linux app calls, and does the work the Linux app does, - `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. +- `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 into the table, for a query result the one table its columns were read from or else `query_results`), count the rows written on an `ExportProgress` the app reads, write beside the target and rename once complete, so a failed or cancelled export leaves no file. - `DatabaseSession::table_keys` reads a table's outbound and inbound foreign keys in one Metadata operation, as the Linux browse tab reads them. `TableKeys::row_references` lists, without the server, the keys a row of a page of that table leads somewhere by: the table's keys holding a column, and the other tables' keys pointing at the row, at most 64 each way. It reads only whether each key's cells hold a value, so a menu copies no value however many keys a table declares; a key with a NULL or a column the page lacks is left out. `follow_reference` then builds the chosen key's filter through `browse::RowReference` from the row's stored values, or its refusal for the app to word, as for a value the store cannot name exactly or one past 8 KiB. - `ResultSet::cell_text_head` gives a value's start, line breaks kept, and `cell_bytes` a binary value's first bytes and its length, so an app's viewer copies no more of a large cell than it shows, where `cell_text` copies all of it. - `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. diff --git a/windows/AGENTS.md b/windows/AGENTS.md index 56a22116a3..d626c7d626 100644 --- a/windows/AGENTS.md +++ b/windows/AGENTS.md @@ -43,6 +43,7 @@ This is the guide for work under `windows/`. The root `AGENTS.md` holds the rule - `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: `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, or for an engine that pages on the server (`CanSeek` false, Cassandra) the `PageResume` the page before handed back through `NextPage()`, since an offset there reads through every row before it; `CountRowsAsync` counts the rows the filter picks, and `EstimateRowsAsync` gives the whole table's count from the engine's statistics, null where it keeps none. 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 and keeps the age and count `PruneAsync` is given. +- `DatabaseSession.CatalogAbilities` says what the engine does to whole tables, schemas and databases, and `PlanCatalogChange` gives a `CatalogChange` with its statements and Safe Mode gate, which `RunAsync` runs once; `RefreshSchemasAsync` reads the schemas again. `ExportRowsAsync` and `ExportTableAsync` report the rows written to an `IProgress` (`docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md`). - `QueryHistory.OpenSavedQueries` and `OpenFavourites` give the saved queries with their folders and the marked tables and databases, per saved connection, from the history's database; `SetPinnedAsync` pins a run and `ExportAsync` writes runs to a file. `DatabaseSession.OpenCsvImportAsync` gives a `CsvImport` that previews a file, suggests a mapping, plans it with a Safe Mode gate and runs it once; a field a column cannot read comes back as `CsvImportCheck.RowRefused`, not an exception. `ConnectionLibrary.DuplicateAsync` copies a connection with its secrets and `ExportAsync` writes the connections file without them (`core/docs/decisions/0030-storage-import-and-library-through-the-ffi.md`). - 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`). - `DatabaseSession.Layout` says how the server's databases stand to the catalog, `GetDatabasesAsync` lists them, and `OpenEditorAsync(database)` opens an editor that runs in one (opened there or moved there; one that cannot reach it throws). `QueryEditor.Database` follows a typed USE. `TableProCore.ConnectAsync(database:)` opens a saved connection in another of its databases, and `OpenKey` reads a key-value engine's key a window at a time (`KeyReader`). diff --git a/windows/docs/decisions/0007-browsing-a-connection.md b/windows/docs/decisions/0007-browsing-a-connection.md index 909aef5b41..ffd1a22dc6 100644 --- a/windows/docs/decisions/0007-browsing-a-connection.md +++ b/windows/docs/decisions/0007-browsing-a-connection.md @@ -15,13 +15,13 @@ Each one works as the macOS app's does, with its words in Windows sentence case, - 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. +- 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, and per table ([0012](0012-catalog-operations-table-filters-and-export-progress.md)). - 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. +- 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; 0012 adds the rest. - 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. ### Grid menus, values and foreign keys @@ -33,7 +33,7 @@ Each one works as the macOS app's does, with its words in Windows sentence case, ### 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. +- File > Export results (Ctrl+Shift+E) asks for a format, CSV, JSON, Excel, Markdown, HTML, XML or SQL INSERT (0012), 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 diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md new file mode 100644 index 0000000000..fbd0c58b8b --- /dev/null +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -0,0 +1,40 @@ +# 0012: Catalog operations, filters kept per table, filter by a cell and export progress + +- **Status**: Accepted +- **Date**: 2026-11-16 + +## Context + +A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)), so a table opened again started unfiltered, and no cell offered to filter by its value. Export results wrote SQL INSERT only from a table and said nothing while it ran. The object tree could not empty or drop a table, and nothing made or dropped a schema or database. The macOS app does all of it, and the core plans the catalog operations ([core 0013](../../../core/docs/decisions/0013-catalog-operations.md)), which the FFI now carries. + +## Decision + +### Filters + +- The filter a table's rows were last read with is kept per saved connection (or opened file), database, schema and table in `table-filters.json` beside the session file (`TableFilterMemory`), as macOS keys it. A table opened again reads with it and shows it in the bar. Clear forgets it; a filter the core or the server refused is not kept. A restored tab and a followed foreign key keep their own filter. +- A dropped table forgets its filter, and a dropped schema or database every filter under it. +- A table tab's cell menu has Filter, with `col = “value”` and `col != “value”`, `>` and `<` where the column orders, and `is NULL` and `is not NULL` for a NULL cell, each only where the engine offers the operator (`CellFilters`). None for a binary value, a value over 10,000 characters, one with a blank at either end or one that reads as NULL, a new row or an edited cell. The rule joins the rules under Match all and applies at once; under Match any of two clauses it runs alone; a rule already running does nothing. A query result has no Filter, as on macOS. + +### Export + +- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table or one column twice. Where each database holds schemas of its own, rows a query tab read after moving to another database leave nothing out, since the session's catalog describes its own database. +- The export bar counts the rows written, read from the core's counter (`ExportProgress`) four times a second, out of the rows held or the counted total where known. Stop ends it with no file left, so it asks nothing first. + +### Catalog operations + +- The tree's table menu adds Open in new tab, Show DDL, Truncate... and Drop..., the last two where the engine has them and the connection is not read-only. A schema row has New schema... and Drop schema, or New database... and Drop database where the server's databases are its schemas. The database switcher ends with New database... and a Drop database list, and Database > New database... and New schema... reach the same dialogs. The engine's own databases and schemas, and the one the window is in, are never offered for a drop. +- Each dialog shows the statements the core planned for its options (`PlanCatalogChange`), planned again as an option changes: Cascade and Restart identity for a truncate, Cascade for a drop, Close other connections first for a database, Run on for a cluster, and for a new database the options the engine names (`CatalogAbilities.DatabaseFields`) with the server's choices. +- A truncate where the server refuses a table other tables' keys point at is refused first, naming them; so is a drop where the engine has no Cascade. +- Safe Mode gates them as the core says (`CatalogChange.Gate`): emptying or dropping is asked at every level, in the dialog that shows the statements; creating is asked again where Confirm writes asks; Read only refuses. Each runs at once, as the Linux app's do. +- After a drop, the table's tabs close without asking, since their changes have nowhere to go, and are not kept to reopen. A truncate reads its table's tabs again, every table tab with Cascade. A change whose outcome is unknown reads the tree again and closes nothing. The tree reads the schemas again on every refresh. + +## Rationale + +- The core plans, gates and runs each statement, so Windows writes no DDL. +- Stock WinUI controls: MenuFlyout, ContentDialog, CheckBox, ComboBox, NumberBox and InfoBar. + +## Consequences + +- macOS stages a truncate or drop until Save; here it runs once answered, as on Linux. Staging waits on the pending structure model (roadmap C-05). +- Views, materialized views, foreign tables and sequences have no Drop: the core plans DROP TABLE only (roadmap C-05). +- No filter settings yet: a table always restores and applies its filter, macOS's default. diff --git a/windows/docs/decisions/README.md b/windows/docs/decisions/README.md index efc7dfcae8..888b5594c7 100644 --- a/windows/docs/decisions/README.md +++ b/windows/docs/decisions/README.md @@ -14,8 +14,9 @@ The format, the statuses and how a later ADR changes an earlier one follow [the | [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. | +| [0007](0007-browsing-a-connection.md) | Filtering, structure, definitions, export and query history | Accepted; amended by 0012 | 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. | | [0008](0008-tablepro-account.md) | Signing in to the TablePro account | Accepted | One account opened at launch through the core's `AccountService` (core 0026); Settings shows it with sign in, sign out, refresh, devices and the device limit; every `account.*` code has its sentence. | | [0009](0009-share-links.md) | Share links | Accepted | Share result, Share query and Share in the grids', editor's and File menus; a ContentDialog sheet over the plan's limits with preview, Hide SQL, Pro options, secret warnings and the cap's Revoke list; `tablepro://share` links open their SQL in a new query tab, never run (core 0027). | | [0010](0010-data-masking.md) | Masking a shared result | Accepted | The share sheet's Keep, Mask or Remove per column, detected columns masked by default, the core's preview and summary of what a link sends, and the Hide SQL suggestion; nothing else masks (core 0028). | | [0011](0011-saved-queries-csv-import-and-connection-copies.md) | Saved queries, history pins, CSV import and connection copies | Accepted | Query > Save query... and a Saved queries pane with folders where the history pane sits, Insert in editor as macOS; Pin on a history run; Import CSV... on a table with a field per column under the save's Safe Mode gate; Duplicate a connection with its passwords and Export the connections file without them. | +| [0012](0012-catalog-operations-table-filters-and-export-progress.md) | Catalog operations, filters kept per table, filter by a cell and export progress | Accepted | Truncate and Drop in the tree, New and Drop schema and database, each showing the core's statements under Safe Mode; a table's last filter comes back when it opens again; a cell's Filter menu; SQL INSERT of a query result and a running row count while an export writes. | diff --git a/windows/src/TablePro.App.Model/AppSettings.cs b/windows/src/TablePro.App.Model/AppSettings.cs index 94f4aae328..6eefc2f515 100644 --- a/windows/src/TablePro.App.Model/AppSettings.cs +++ b/windows/src/TablePro.App.Model/AppSettings.cs @@ -128,6 +128,7 @@ public void Save(AppSettings settings) UseStringEnumConverter = true)] [JsonSerializable(typeof(AppSettings))] [JsonSerializable(typeof(SessionState))] +[JsonSerializable(typeof(TableFilterFile))] internal sealed partial class ModelJson : JsonSerializerContext; internal static class JsonFile diff --git a/windows/src/TablePro.App.Model/CellFilters.cs b/windows/src/TablePro.App.Model/CellFilters.cs new file mode 100644 index 0000000000..0d42c15c89 --- /dev/null +++ b/windows/src/TablePro.App.Model/CellFilters.cs @@ -0,0 +1,73 @@ +using TablePro.Interop; + +namespace TablePro.App.Model; + +// The grid's Filter by a cell's value, as macOS offers it: equal or not, greater or less where the column orders, and +// the NULL tests for a NULL cell. Each rule is one of the operators the engine offers for the column. +public static class CellFilters +{ + // A longer value would make a menu title and a statement no one reads. + public const int MaximumValueLength = 10_000; + + private static readonly FilterOperator[] ForNull = [FilterOperator.IsNull, FilterOperator.IsNotNull]; + + private static readonly FilterOperator[] ForValue = + [FilterOperator.Equal, FilterOperator.NotEqual, FilterOperator.Greater, FilterOperator.Less]; + + // None for a binary value, a value cut short or too long, one that starts or ends with a blank, which a filter + // row reads trimmed, one that reads as the NULL literal, and a cell holding an edit not saved yet. + public static IReadOnlyList For( + string column, + IReadOnlyList offered, + CellValue? value, + bool pending) + { + ArgumentNullException.ThrowIfNull(column); + ArgumentNullException.ThrowIfNull(offered); + if (value is null || pending || value.IsDefault || value.Bytes is not null) + { + return []; + } + + if (value.IsNull) + { + return [.. ForNull.Where(offered.Contains).Select(op => new FilterRule(column, op, null))]; + } + + var text = value.Text ?? string.Empty; + if (value.TextCut + || text.Length == 0 + || text.Length > MaximumValueLength + || char.IsWhiteSpace(text[0]) + || char.IsWhiteSpace(text[^1]) + || string.Equals(text, "NULL", StringComparison.OrdinalIgnoreCase)) + { + return []; + } + + return [.. ForValue.Where(offered.Contains).Select(op => new FilterRule(column, op, new FilterValue.One(text)))]; + } + + // The rule joins the rules running under Match all; under Match any with two clauses running it runs alone, as + // macOS adds it. A rule already running leaves the filter as it is. + public static TableFilter With(TableFilter? current, FilterRule rule) + { + ArgumentNullException.ThrowIfNull(rule); + if (current is null) + { + return new TableFilter(FilterCombinator.And, [rule], null); + } + + var single = new TableFilter(FilterCombinator.And, [rule], null); + if (current.Rules.Any(running => TableFilters.Same( + new TableFilter(FilterCombinator.And, [running], null), single))) + { + return current; + } + + var clauses = current.Rules.Count + (string.IsNullOrWhiteSpace(current.RawSql) ? 0 : 1); + return current.Combinator == FilterCombinator.Or && clauses >= 2 + ? single + : new TableFilter(FilterCombinator.And, [.. current.Rules, rule], current.RawSql); + } +} diff --git a/windows/src/TablePro.App.Model/CellValue.cs b/windows/src/TablePro.App.Model/CellValue.cs index 2d0e7b391d..62054a0b00 100644 --- a/windows/src/TablePro.App.Model/CellValue.cs +++ b/windows/src/TablePro.App.Model/CellValue.cs @@ -24,8 +24,15 @@ public sealed record CellValue(string Column, string? Text, bool TextCut, ReadOn public bool IsWholeBinary => Bytes is { } bytes && bytes.Length == ByteLength; // The grid's text with its pending edit, and the server's value otherwise. A deleted row still shows what it - // holds; a new row and a changed cell show the text typed. - public static CellValue? Read(IGridData data, ResultSet? rows, int row, int column) + // holds; a new row and a changed cell show the text typed. A caller that only asks what kind of value it is reads + // less of it. + public static CellValue? Read( + IGridData data, + ResultSet? rows, + int row, + int column, + int maxCharacters = MaximumShownCharacters, + long maxBytes = MaximumPictureBytes) { ArgumentNullException.ThrowIfNull(data); if (row < 0 || row >= data.RowCount || column < 0 || column >= data.ColumnCount) @@ -55,12 +62,12 @@ public sealed record CellValue(string Column, string? Text, bool TextCut, ReadOn return Of(name, reader.ReadWhole(row, column)); } - if (rows.GetCellBytes(row, column, MaximumPictureBytes) is { } binary) + if (rows.GetCellBytes(row, column, maxBytes) is { } binary) { return new CellValue(name, null, false, binary.Bytes, binary.Length, IsDefault: false); } - return rows.GetCellTextHead(row, column, MaximumShownCharacters) is { } head + return rows.GetCellTextHead(row, column, maxCharacters) is { } head ? new CellValue(name, head.Text, head.Cut, null, 0, IsDefault: false) : new CellValue(name, null, false, null, 0, IsDefault: false); } diff --git a/windows/src/TablePro.App.Model/ExportPlan.cs b/windows/src/TablePro.App.Model/ExportPlan.cs index dbddfb2042..618dacfa5c 100644 --- a/windows/src/TablePro.App.Model/ExportPlan.cs +++ b/windows/src/TablePro.App.Model/ExportPlan.cs @@ -29,11 +29,11 @@ private ExportPlan(IReadOnlyList scopes, IReadOnlyList Tables { get; set; } = []; +} + +// The last filter applied to each table, per connection, database, schema and table, as macOS keeps it, so a table +// opened again reads with it. One file for every connection, written whole on each change. +public sealed class TableFilterMemory +{ + private readonly string _path; + private readonly Lock _lock = new(); + private Dictionary? _tables; + + public TableFilterMemory(string path) + { + ArgumentException.ThrowIfNullOrEmpty(path); + _path = path; + } + + // A saved connection by its id, a file opened without saving by its path; database is the one the session is in. + public static string ConnectionKey(Guid? connectionId, string? filePath) => + connectionId is { } id ? "id:" + id.ToString("D") : "file:" + (filePath ?? string.Empty); + + public ITableFilters For(string connection, string? database) => new Scope(this, connection, database); + + public TableFilter? Recall(string connection, string? database, TableName table) + { + ArgumentNullException.ThrowIfNull(table); + lock (_lock) + { + return Tables().GetValueOrDefault(Key(connection, database, table.Schema, table.Name))?.ToFilter(); + } + } + + public void Remember(string connection, string? database, TableName table, TableFilter? filter) + { + ArgumentNullException.ThrowIfNull(table); + var key = Key(connection, database, table.Schema, table.Name); + lock (_lock) + { + var tables = Tables(); + var kept = tables.GetValueOrDefault(key)?.ToFilter(); + if (filter is null ? !tables.ContainsKey(key) : TableFilters.Same(kept, filter)) + { + return; + } + + if (filter is null) + { + tables.Remove(key); + } + else + { + tables[key] = SavedFilter.From(filter); + } + + Write(tables); + } + } + + // A dropped table's filter goes with it, so a table made again under its name starts unfiltered. + public void ForgetTable(string connection, string? database, TableName table) + { + ArgumentNullException.ThrowIfNull(table); + Remember(connection, database, table, null); + } + + // Every table of a dropped schema, or of a dropped database where the server's databases are its schemas. + public void ForgetSchema(string connection, string? database, string schema) + { + ArgumentNullException.ThrowIfNull(schema); + ForgetPrefix(Part(connection) + "." + Part(database) + "." + Part(schema) + "."); + } + + // Every table of a dropped database whose schemas are its own. + public void ForgetDatabase(string connection, string database) + { + ArgumentNullException.ThrowIfNull(database); + ForgetPrefix(Part(connection) + "." + Part(database) + "."); + } + + private void ForgetPrefix(string prefix) + { + lock (_lock) + { + var tables = Tables(); + var gone = tables.Keys.Where(key => key.StartsWith(prefix, StringComparison.Ordinal)).ToList(); + if (gone.Count == 0) + { + return; + } + + foreach (var key in gone) + { + tables.Remove(key); + } + + Write(tables); + } + } + + private static string Key(string connection, string? database, string? schema, string table) => + $"{Part(connection)}.{Part(database)}.{Part(schema)}.{Part(table)}"; + + // Escaped dots included, which EscapeDataString leaves, so a dot in a name cannot make two tables one key. + private static string Part(string? part) => + Uri.EscapeDataString(part ?? string.Empty).Replace(".", "%2E", StringComparison.Ordinal); + + private Dictionary Tables() => + _tables ??= JsonFile.Read(_path, ModelJson.Default.TableFilterFile)?.Tables is { } read + ? new Dictionary(read, StringComparer.Ordinal) + : new Dictionary(StringComparer.Ordinal); + + // A file another program holds keeps the filter in memory until the next change writes it. + private void Write(Dictionary tables) + { + try + { + JsonFile.Write(_path, new TableFilterFile { Tables = tables }, ModelJson.Default.TableFilterFile); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + } + } + + private sealed class Scope(TableFilterMemory memory, string connection, string? database) : ITableFilters + { + public TableFilter? Recall(TableName table) => memory.Recall(connection, database, table); + + public void Remember(TableName table, TableFilter? filter) => memory.Remember(connection, database, table, filter); + } +} diff --git a/windows/src/TablePro.App.Model/TableTab.cs b/windows/src/TablePro.App.Model/TableTab.cs index 19edd7188e..66076a1500 100644 --- a/windows/src/TablePro.App.Model/TableTab.cs +++ b/windows/src/TablePro.App.Model/TableTab.cs @@ -148,6 +148,7 @@ public sealed class TableTab : IWorkspaceTab private readonly TabWork _work = new(); private readonly KeptRead _structure; private readonly KeptRead _choices; + private readonly ITableFilters? _filters; // Where each page of a server-paged table resumes, by its offset: a paging state only goes forward. private readonly Dictionary _resumes = []; @@ -200,7 +201,8 @@ public TableTab( TableName table, ObjectKind kind, Func settings, - SessionTab? restore = null) + SessionTab? restore = null, + ITableFilters? filters = null) { ArgumentNullException.ThrowIfNull(session); ArgumentNullException.ThrowIfNull(view); @@ -216,8 +218,10 @@ public TableTab( _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(); + _filters = filters; + // A restored filter stays until the user changes it, even when the first read refuses it. A table opened again + // reads with the filter it last had, as macOS restores and applies it. + Filter = restore is null ? filters?.Recall(table) : restore.Filter?.ToFilter(); _shownFilter = Filter; _recovered = restore?.Edits is { Changes: > 0 } recovered ? recovered : null; _structure = new KeptRead( @@ -249,6 +253,8 @@ public TableTab( public bool IsSaving { get; private set; } + public bool IsClosed => _work.IsClosed; + // A page is read on one of the session's pooled connections, and a save commits before it returns. public TransactionState Transaction => TransactionState.Idle; @@ -527,6 +533,20 @@ public async Task ApplyFilterAsync(TableFilter? filter) await LoadAsync(0, recount: true).ConfigureAwait(true); } + // A cell's rule joins the filter and the table reads again; false when it was already running. + public async Task FilterByAsync(FilterRule rule) + { + ArgumentNullException.ThrowIfNull(rule); + var filter = CellFilters.With(Filter, rule); + if (ReferenceEquals(filter, Filter)) + { + return false; + } + + await ApplyFilterAsync(filter).ConfigureAwait(true); + return true; + } + // Read once per tab, and again after a refresh, since the table's columns can change. public Task LoadFilterChoicesAsync() => _choices.LoadAsync(refresh: false); @@ -977,6 +997,7 @@ await _results.ShowRunAsync(_run, _sort, read.VisibleColumns, keptSets: 0, cance _shownSort = _sort; _shownFilter = filter; + _filters?.Remember(Table, filter); var next = read.NextPage?.Invoke(); KeepResume(page.NextOffset, next); _page = _page with { Rows = _results.RowCount, EndsTable = !read.CanSeek && next is null }; diff --git a/windows/src/TablePro.App.Model/WorkspaceTabs.cs b/windows/src/TablePro.App.Model/WorkspaceTabs.cs index 6f4593b5cb..69f54d3df8 100644 --- a/windows/src/TablePro.App.Model/WorkspaceTabs.cs +++ b/windows/src/TablePro.App.Model/WorkspaceTabs.cs @@ -107,6 +107,35 @@ public async Task CloseAsync(TItem item) } } + // The table the tab shows was dropped: nothing is asked, since its changes have nowhere to go, and the tab is not + // kept to reopen. + public async Task DiscardAsync(TItem item) + { + ArgumentNullException.ThrowIfNull(item); + if (!_closing.Add(item)) + { + return; + } + + try + { + if (_view.ContentOf(item) is { } content) + { + await content.CloseAsync().ConfigureAwait(true); + } + } + finally + { + _closing.Remove(item); + } + + _view.Remove(item); + if (_view.Items.Count == 0) + { + Emptied?.Invoke(this, EventArgs.Empty); + } + } + // A held transaction or unsaved changes not answered for yet. public bool HasUnconfirmed(ISet confirmed) { diff --git a/windows/src/TablePro.App/MainWindow.xaml b/windows/src/TablePro.App/MainWindow.xaml index c340199c06..c94d61cfb4 100644 --- a/windows/src/TablePro.App/MainWindow.xaml +++ b/windows/src/TablePro.App/MainWindow.xaml @@ -173,6 +173,10 @@ + + + + diff --git a/windows/src/TablePro.App/MainWindow.xaml.cs b/windows/src/TablePro.App/MainWindow.xaml.cs index 18a171ab5f..33eb485e7f 100644 --- a/windows/src/TablePro.App/MainWindow.xaml.cs +++ b/windows/src/TablePro.App/MainWindow.xaml.cs @@ -1538,6 +1538,8 @@ private void UpdateCommands() or KeyTabView { IsRunning: true }; var editor = tab?.Editor is not null; MenuSwitchDatabase.IsEnabled = workspace?.CanSwitchDatabase ?? false; + MenuNewDatabase.IsEnabled = workspace?.CanCreateDatabase ?? false; + MenuNewSchema.IsEnabled = workspace?.CanCreateSchema ?? false; foreach (var item in new[] { MenuUndo, MenuRedo, MenuCut, MenuPaste, MenuFind, MenuReplace }) { item.IsEnabled = editor; @@ -1636,6 +1638,22 @@ private void OnSelectAll(object sender, RoutedEventArgs e) private void OnSwitchDatabase(object sender, RoutedEventArgs e) => Workspace?.OpenDatabaseMenu(); + private async void OnNewDatabase(object sender, RoutedEventArgs e) + { + if (Workspace is { } workspace) + { + await workspace.NewDatabaseAsync().ConfigureAwait(true); + } + } + + private async void OnNewSchema(object sender, RoutedEventArgs e) + { + if (Workspace is { } workspace) + { + await workspace.NewSchemaAsync().ConfigureAwait(true); + } + } + // The menu sends the editor the key it already answers, so a menu command and its shortcut cannot differ. private void EditorKey(VirtualKey key) { diff --git a/windows/src/TablePro.App/Services/Workbench.cs b/windows/src/TablePro.App/Services/Workbench.cs index 34c145ebf7..ac409bfbd2 100644 --- a/windows/src/TablePro.App/Services/Workbench.cs +++ b/windows/src/TablePro.App/Services/Workbench.cs @@ -19,6 +19,7 @@ public Workbench(TableProCore core, string folder) Folder = folder; _settings = new SettingsStore(Path.Combine(folder, "settings.json")); Sessions = new SessionStore(Path.Combine(folder, "session.json")); + TableFilters = new TableFilterMemory(Path.Combine(folder, "table-filters.json")); Settings = _settings.Load(); DisplaySettings.Update(Settings); } @@ -44,6 +45,9 @@ public Workbench(TableProCore core, string folder) public SessionStore Sessions { get; } + // The last filter of each table, read when the table opens again. + public TableFilterMemory TableFilters { get; } + public AppSettings Settings { get; private set; } // Shared by every workspace and kept in the session file. diff --git a/windows/src/TablePro.App/Strings/en-US/Resources.resw b/windows/src/TablePro.App/Strings/en-US/Resources.resw index a5d404eba4..af1646c1ff 100644 --- a/windows/src/TablePro.App/Strings/en-US/Resources.resw +++ b/windows/src/TablePro.App/Strings/en-US/Resources.resw @@ -4014,4 +4014,271 @@ New fingerprint: {2} Unsaved changes + + New database... + + + New schema... + + + Open in new tab + + + Truncate... + + + Drop... + + + New schema... + + + New database... + + + New keyspace... + + + Drop schema “{0}”... + + + Drop database “{0}”... + + + Drop keyspace “{0}”... + + + Drop database + + + Drop keyspace + + + Truncate table '{0}' + + + Every row of the table will be deleted. This can't be undone. + + + Truncate + + + Cascade + + + Also empty the tables whose foreign keys point at this one + + + Restart identity + + + Start the table's numbering again + + + Cannot truncate table + + + Foreign keys on other tables point at {0}: {1}. Drop those foreign keys first, or delete the rows in a query. + + + Truncate failed + + + Drop table '{0}' + + + All rows and the table definition will be removed. This can't be undone. + + + Drop + + + Cascade + + + Drop all tables that depend on this table + + + Drop all tables that depend on this table: {0} + + + Cannot drop table + + + Foreign keys on other tables point at {0}: {1}. Drop those foreign keys first. + + + Drop failed + + + Drop schema “{0}”? + + + Drop everything the schema holds with it + + + Drop failed + + + Drop database “{0}”? + + + Drop keyspace “{0}”? + + + All tables and data will be permanently deleted. + + + Close other connections first + + + End every other session on the database before it is dropped + + + Drop failed + + + Drop failed + + + New schema + + + New database + + + New keyspace + + + Create + + + Could not create schema + + + Could not create database + + + Could not create keyspace + + + Name + + + Owner + + + Template + + + Encoding + + + Locale + + + Character set + + + Collation + + + Durable writes + + + Write each change to the commit log before it is acknowledged + + + Tablets + + + First collection + + + Run on + + + This server only + + + Default + + + Default ({0}) + + + Replication + + + Replication factor + + + Simple + + + Network topology + + + Replicas in {0} + + + It may have run in part, so the objects are read again. + + + On + + + Off + + + This database engine can't do that. + + + Enter a name the server can take. + + + The server can't take one of these options. + + + This change can't be written for this database. + + + Safe Mode asks before this change. Try again to answer it. + + + This change already ran. + + + The connection would run other statements than the ones shown. Try again. + + + The connection's Safe Mode changed. Try again to see what it asks. + + + Exporting to {0}... {1:N0} rows + + + Exporting to {0}... {1:N0} of {2:N0} rows + + + Filter + + + {0} = “{1}” + + + {0} != “{1}” + + + {0} > “{1}” + + + {0} < “{1}” + + + {0} is NULL + + + {0} is not NULL + diff --git a/windows/src/TablePro.App/Views/CatalogDialogs.cs b/windows/src/TablePro.App/Views/CatalogDialogs.cs new file mode 100644 index 0000000000..2df0cc64d0 --- /dev/null +++ b/windows/src/TablePro.App/Views/CatalogDialogs.cs @@ -0,0 +1,420 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Automation; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media; +using TablePro.App.Services; +using TablePro.Interop; + +namespace TablePro.App.Views; + +// The questions before a table is emptied or dropped and a schema or database is made or dropped. Each shows the +// statements its options plan, planned again as they change, and answers with the plan shown. +internal static class CatalogDialogs +{ + private const string MonoFont = "Cascadia Mono, Consolas, Courier New"; + + public static Task AskTruncateAsync( + FrameworkElement owner, + DatabaseSession session, + TableName table, + CatalogAbilities abilities, + IReadOnlyList clusters) + { + var form = new Form(Strings.Format("TruncateTitle", table.Name), Strings.Get("TruncateMessage"), "TruncateAction"); + var cascade = abilities.TruncateCascade ? form.Check("TruncateCascade", "TruncateCascadeDetail", "TruncateCascade") : null; + var restart = abilities.TruncateRestartIdentity + ? form.Check("TruncateRestartIdentity", "TruncateRestartIdentityDetail", "TruncateRestartIdentity") + : null; + var cluster = form.Cluster(clusters); + return form.AskAsync(owner, () => session.PlanCatalogChange(new CatalogOperation.TruncateTable( + table, cascade?.IsChecked == true, restart?.IsChecked == true, Form.Picked(cluster)))); + } + + public static Task AskDropTableAsync( + FrameworkElement owner, + DatabaseSession session, + TableName table, + CatalogAbilities abilities, + IReadOnlyList referencing) + { + var form = new Form(Strings.Format("DropTableTitle", table.Name), Strings.Get("DropTableMessage"), "DropAction"); + var cascade = abilities.DropTableCascade + ? referencing.Count > 0 + ? form.Check("DropCascade", "DropTableCascadeNamed", "DropCascade", Names(table, referencing)) + : form.Check("DropCascade", "DropTableCascadeDetail", "DropCascade") + : null; + return form.AskAsync(owner, () => session.PlanCatalogChange( + new CatalogOperation.DropTable(table, cascade?.IsChecked == true))); + } + + public static Task AskDropSchemaAsync( + FrameworkElement owner, + DatabaseSession session, + string schema, + CatalogAbilities abilities) + { + var form = new Form(Strings.Format("DropSchemaTitle", schema), Strings.Get("DropContainerMessage"), "DropAction"); + var cascade = abilities.DropSchemaCascade + ? form.Check("DropCascade", "DropSchemaCascadeDetail", "DropCascade") + : null; + return form.AskAsync(owner, () => session.PlanCatalogChange( + new CatalogOperation.DropSchema(schema, cascade?.IsChecked == true))); + } + + public static Task AskDropDatabaseAsync( + FrameworkElement owner, + DatabaseSession session, + string database, + CatalogAbilities abilities, + IReadOnlyList clusters) + { + var title = Strings.Format(abilities.Keyspaces ? "DropKeyspaceTitle" : "DropDatabaseTitle", database); + var form = new Form(title, Strings.Get("DropContainerMessage"), "DropAction"); + var force = abilities.DropDatabaseForce + ? form.Check("DropDatabaseForce", "DropDatabaseForceDetail", "DropDatabaseForce") + : null; + var cluster = form.Cluster(clusters); + return form.AskAsync(owner, () => session.PlanCatalogChange( + new CatalogOperation.DropDatabase(database, force?.IsChecked == true, Form.Picked(cluster)))); + } + + public static Task AskNewSchemaAsync( + FrameworkElement owner, + DatabaseSession session, + CatalogAbilities abilities, + DatabaseChoices choices) + { + var form = new Form(Strings.Get("NewSchemaTitle"), null, "CreateAction"); + var name = form.Name(); + var schemaOwner = abilities.SchemaOwner ? form.Pick("CatalogOwner", "CatalogOwner", choices.Owners, choices.DefaultOwner) : null; + return form.AskAsync( + owner, + () => session.PlanCatalogChange(new CatalogOperation.CreateSchema(name.Text.Trim(), Form.Picked(schemaOwner))), + () => name.Text.Trim().Length > 0); + } + + public static Task AskNewDatabaseAsync( + FrameworkElement owner, + DatabaseSession session, + CatalogAbilities abilities, + DatabaseChoices choices) + { + var fields = abilities.DatabaseFields; + var form = new Form(Strings.Get(abilities.Keyspaces ? "NewKeyspaceTitle" : "NewDatabaseTitle"), null, "CreateAction"); + var name = form.Name(); + var databaseOwner = fields.Owner ? form.Pick("CatalogOwner", "CatalogOwner", choices.Owners, choices.DefaultOwner) : null; + var template = fields.Template ? form.Pick("CatalogTemplate", "CatalogTemplate", choices.Templates, null) : null; + var encoding = fields.Encoding ? form.Pick("CatalogEncoding", "CatalogEncoding", choices.Encodings, choices.DefaultEncoding) : null; + var locale = fields.Locale ? form.Pick("CatalogLocale", "CatalogLocale", choices.Locales, choices.DefaultLocale) : null; + var characterSet = fields.CharacterSet + ? form.Pick( + "CatalogCharacterSet", + "CatalogCharacterSet", + [.. choices.CharacterSets.Select(set => set.Name)], + choices.DefaultCharacterSet) + : null; + var collation = fields.Collation + ? form.Pick("CatalogCollation", "CatalogCollation", choices.Collations, choices.DefaultCollation) + : null; + if (characterSet is not null && collation is not null) + { + // A collation belongs to a character set, so the list follows the one picked. + characterSet.SelectionChanged += (_, _) => + { + var picked = choices.CharacterSets.FirstOrDefault(set => set.Name == Form.Picked(characterSet)); + Form.Fill(collation, picked?.Collations ?? choices.Collations, picked?.DefaultCollation ?? choices.DefaultCollation); + }; + } + + var replication = fields.Replication ? form.Replication(choices.DataCenters) : null; + var durable = fields.Replication ? form.Check("CatalogDurableWrites", "CatalogDurableWritesDetail", "CatalogDurableWrites") : null; + if (durable is not null) + { + durable.IsChecked = true; + } + + var tablets = fields.Tablets ? form.Pick("CatalogTablets", "CatalogTablets", [Strings.Get("On"), Strings.Get("Off")], null) : null; + var collection = fields.FirstCollection ? form.Text("CatalogFirstCollection", "CatalogFirstCollection") : null; + var cluster = abilities.OnCluster ? form.Cluster(choices.Clusters) : null; + return form.AskAsync( + owner, + () => session.PlanCatalogChange(new CatalogOperation.CreateDatabase(new DatabaseDraft(name.Text.Trim()) + { + Owner = Form.Picked(databaseOwner), + Template = Form.Picked(template), + Encoding = Form.Picked(encoding), + Locale = Form.Picked(locale), + CharacterSet = Form.Picked(characterSet), + Collation = Form.Picked(collation), + Cluster = Form.Picked(cluster), + Replication = replication?.Chosen(), + DurableWrites = durable?.IsChecked != false, + Tablets = tablets?.SelectedIndex switch + { + 1 => true, + 2 => false, + _ => null, + }, + FirstCollection = collection?.Text.Trim(), + })), + () => name.Text.Trim().Length > 0 && (collection is null || collection.Text.Trim().Length > 0)); + } + + // A table some other table points at cannot be emptied there; its keys have to go first, as the Linux app says. + public static async Task ShowRefusedAsync(FrameworkElement owner, string title, string message) + { + var dialog = new ContentDialog + { + RequestedTheme = owner.ActualTheme, + Title = title, + Content = new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap, IsTextSelectionEnabled = true }, + CloseButtonText = Strings.Get("PreviewSqlDone"), + DefaultButton = ContentDialogButton.Close, + }; + await WindowDialogs.ShowAsync(dialog, owner).ConfigureAwait(true); + } + + // The other tables by name, qualified only where their schema is not the table's. + public static string Names(TableName table, IReadOnlyList others) => + string.Join(", ", others.Select(other => + other.Schema is { } schema && !string.Equals(schema, table.Schema, StringComparison.Ordinal) + ? $"{schema}.{other.Name}" + : other.Name)); + + private sealed class Form + { + private readonly StackPanel _content = new() { Spacing = 12 }; + private readonly TextBlock _statements = new() + { + FontFamily = new FontFamily(MonoFont), + TextWrapping = TextWrapping.Wrap, + IsTextSelectionEnabled = true, + }; + + private readonly ContentDialog _dialog; + private readonly List> _watches = []; + private CatalogChange? _planned; + + public Form(string title, string? message, string verb) + { + _dialog = new ContentDialog + { + Title = title, + Content = new ScrollViewer { Content = _content, MaxHeight = 480 }, + PrimaryButtonText = Strings.Get(verb), + CloseButtonText = Strings.Get("Cancel"), + DefaultButton = ContentDialogButton.Close, + }; + AutomationProperties.SetAutomationId(_statements, "CatalogStatements"); + if (message is not null) + { + _content.Children.Add(new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap }); + } + } + + public static string? Picked(ComboBox? box) => + box is { SelectedIndex: > 0, SelectedItem: string value } ? value : null; + + // The first item is the server's own choice, named with its default where the server said it. + public static void Fill(ComboBox box, IReadOnlyList values, string? fallback) + { + box.Items.Clear(); + box.Items.Add(fallback is null ? Strings.Get("CatalogDefault") : Strings.Format("CatalogDefaultNamed", fallback)); + foreach (var value in values) + { + box.Items.Add(value); + } + + box.SelectedIndex = 0; + } + + public TextBox Name() + { + var name = Text("CatalogName", "CatalogName"); + name.Loaded += (_, _) => name.Focus(FocusState.Programmatic); + return name; + } + + public TextBox Text(string header, string automationId) + { + var box = new TextBox { Header = Strings.Get(header) }; + AutomationProperties.SetAutomationId(box, automationId); + _content.Children.Add(box); + _watches.Add(refresh => box.TextChanged += (_, _) => refresh()); + return box; + } + + public CheckBox Check(string title, string detail, string automationId, string? detailValue = null) + { + var box = new CheckBox { Content = Strings.Get(title) }; + AutomationProperties.SetAutomationId(box, automationId); + var panel = new StackPanel { Spacing = 2 }; + panel.Children.Add(box); + panel.Children.Add(new TextBlock + { + Text = detailValue is null ? Strings.Get(detail) : Strings.Format(detail, detailValue), + TextWrapping = TextWrapping.Wrap, + Margin = new Thickness(28, 0, 0, 0), + Style = (Style)Application.Current.Resources["CaptionTextBlockStyle"], + Foreground = (Brush)Application.Current.Resources["TextFillColorSecondaryBrush"], + }); + _content.Children.Add(panel); + _watches.Add(refresh => + { + box.Checked += (_, _) => refresh(); + box.Unchecked += (_, _) => refresh(); + }); + return box; + } + + public ComboBox Pick(string header, string automationId, IReadOnlyList values, string? fallback) + { + var box = new ComboBox { Header = Strings.Get(header), HorizontalAlignment = HorizontalAlignment.Stretch }; + AutomationProperties.SetAutomationId(box, automationId); + Fill(box, values, fallback); + _content.Children.Add(box); + _watches.Add(refresh => box.SelectionChanged += (_, _) => refresh()); + return box; + } + + // Only a server in a cluster of more than itself offers one; the first item runs on this server only. + public ComboBox? Cluster(IReadOnlyList clusters) + { + if (clusters.Count == 0) + { + return null; + } + + var box = Pick("CatalogCluster", "CatalogCluster", clusters, null); + box.Items[0] = Strings.Get("CatalogClusterNone"); + box.SelectedIndex = 0; + return box; + } + + public ReplicationRows Replication(IReadOnlyList dataCenters) + { + var rows = new ReplicationRows(dataCenters); + _content.Children.Add(rows.Root); + _watches.Add(rows.Watch); + return rows; + } + + // Null when cancelled; ready says whether the fields typed so far can plan at all. + public async Task AskAsync(FrameworkElement owner, Func plan, Func? ready = null) + { + _content.Children.Add(_statements); + _dialog.RequestedTheme = owner.ActualTheme; + void Refresh() + { + _planned?.Dispose(); + _planned = null; + if (ready is not null && !ready()) + { + _statements.Text = string.Empty; + _dialog.IsPrimaryButtonEnabled = false; + return; + } + + try + { + var next = plan(); + _planned = next; + _statements.Text = string.Join("\n\n", next.Statements.Select(statement => statement + ";")); + _dialog.IsPrimaryButtonEnabled = next.Gate != SaveGate.Refuse; + } + catch (TableProException exception) + { + _statements.Text = Strings.Error(exception.Error); + _dialog.IsPrimaryButtonEnabled = false; + } + } + + foreach (var watch in _watches) + { + watch(Refresh); + } + + Refresh(); + var answer = await WindowDialogs.ShowAsync(_dialog, owner).ConfigureAwait(true); + var planned = _planned; + _planned = null; + if (answer == ContentDialogResult.Primary && planned is not null) + { + return planned; + } + + planned?.Dispose(); + return null; + } + } + + // A keyspace's replication: one factor for the whole cluster, or replicas per data centre. + private sealed class ReplicationRows + { + private readonly ComboBox _strategy = new() { Header = Strings.Get("CatalogReplication"), HorizontalAlignment = HorizontalAlignment.Stretch }; + private readonly NumberBox _factor = Replicas(Strings.Get("CatalogReplicationFactor"), 1, 1); + private readonly List<(string DataCenter, NumberBox Box)> _centres = []; + private readonly StackPanel _perCentre = new() { Spacing = 8 }; + + public ReplicationRows(IReadOnlyList dataCenters) + { + AutomationProperties.SetAutomationId(_strategy, "CatalogReplication"); + _strategy.Items.Add(Strings.Get("CatalogReplicationSimple")); + foreach (var centre in dataCenters) + { + var box = Replicas(Strings.Format("CatalogReplicasIn", centre), 0, 1); + _centres.Add((centre, box)); + _perCentre.Children.Add(box); + } + + if (_centres.Count > 0) + { + _strategy.Items.Add(Strings.Get("CatalogReplicationTopology")); + } + + _strategy.SelectedIndex = _centres.Count > 0 ? 1 : 0; + Root.Children.Add(_strategy); + Root.Children.Add(_factor); + Root.Children.Add(_perCentre); + Show(); + _strategy.SelectionChanged += (_, _) => Show(); + } + + public StackPanel Root { get; } = new() { Spacing = 8 }; + + private bool Simple => _strategy.SelectedIndex <= 0; + + public KeyspaceReplication Chosen() => Simple + ? new KeyspaceReplication.Simple(Count(_factor)) + : new KeyspaceReplication.NetworkTopology( + [.. _centres.Select(centre => new DataCenterReplicas(centre.DataCenter, Count(centre.Box)))]); + + public void Watch(Action refresh) + { + _strategy.SelectionChanged += (_, _) => refresh(); + _factor.ValueChanged += (_, _) => refresh(); + foreach (var (_, box) in _centres) + { + box.ValueChanged += (_, _) => refresh(); + } + } + + private static NumberBox Replicas(string header, double least, double value) => new() + { + Header = header, + Minimum = least, + Maximum = 100, + Value = value, + SmallChange = 1, + SpinButtonPlacementMode = NumberBoxSpinButtonPlacementMode.Inline, + }; + + private static int Count(NumberBox box) => double.IsNaN(box.Value) ? 0 : (int)Math.Round(box.Value); + + private void Show() + { + _factor.Visibility = Simple ? Visibility.Visible : Visibility.Collapsed; + _perCentre.Visibility = Simple ? Visibility.Collapsed : Visibility.Visible; + } + } +} diff --git a/windows/src/TablePro.App/Views/FilterBar.xaml.cs b/windows/src/TablePro.App/Views/FilterBar.xaml.cs index bb9e391b21..42cdebc456 100644 --- a/windows/src/TablePro.App/Views/FilterBar.xaml.cs +++ b/windows/src/TablePro.App/Views/FilterBar.xaml.cs @@ -93,6 +93,17 @@ internal async Task LoadAsync(bool focus) internal void ShowChoicesFailed(CoreError failure) => ShowMessage(Strings.Error(failure), retry: true); + // A filter applied from elsewhere, such as a cell's menu, replaces the rows typed; before the choices are read, + // LoadAsync shows it. + internal void ShowApplied() + { + if (_draft is { } draft) + { + MessageBar.IsOpen = false; + ShowDraft(Fresh(_tab.Filter, draft.Choices)); + } + } + // An empty filter shows one row to fill in. private static TableFilterDraft Fresh(TableFilter? filter, FilterChoices choices) { diff --git a/windows/src/TablePro.App/Views/GridMenus.cs b/windows/src/TablePro.App/Views/GridMenus.cs index e9ff3e09b6..1ca81b044c 100644 --- a/windows/src/TablePro.App/Views/GridMenus.cs +++ b/windows/src/TablePro.App/Views/GridMenus.cs @@ -28,15 +28,25 @@ public static MenuFlyoutItem ViewValue(ResultsPane results) => Item("GridMenuViewValue", results.CanViewValue, async () => await results.ViewValueAsync().ConfigureAwait(true)); // From the keyboard the menu opens at the cell it acts on, not over the editor; a pointer's opens where it was. - public static void Show(MenuFlyout menu, DataGridView grid, ContextRequestedEventArgs args) + public static void Show(MenuFlyout menu, DataGridView grid, ContextRequestedEventArgs args) => + Show(menu, grid, Place(grid, args)); + + // Read while the request is raised, for a menu built after an await. + public static FlyoutShowOptions? Place(DataGridView grid, ContextRequestedEventArgs args) { if (args.TryGetPosition(grid, out var point)) { - menu.ShowAt(grid, new FlyoutShowOptions { Position = point }); + return new FlyoutShowOptions { Position = point }; } - else if (grid.FocusedCellMenuPoint() is { } cell) + + return grid.FocusedCellMenuPoint() is { } cell ? new FlyoutShowOptions { Position = cell } : null; + } + + public static void Show(MenuFlyout menu, DataGridView grid, FlyoutShowOptions? options) + { + if (options is not null) { - menu.ShowAt(grid, new FlyoutShowOptions { Position = cell }); + menu.ShowAt(grid, options); } else { diff --git a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs index a7c116e02c..7915374867 100644 --- a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs @@ -110,6 +110,9 @@ private async void LoadRestoredFile(SqlFileStamp? stamp, bool unsaved) => // Set by the workspace that holds the tab: only a saved connection keeps queries. public bool CanSaveQuery { get; set; } + // Where the tab's editor runs, null for the connection's own; a typed USE moves it. + internal string? Database => _model.Database; + public string Title => _model.Title; public TabKind Kind => _model.Kind; diff --git a/windows/src/TablePro.App/Views/ResultsPane.xaml.cs b/windows/src/TablePro.App/Views/ResultsPane.xaml.cs index 68a37d27e5..b6a35c0a22 100644 --- a/windows/src/TablePro.App/Views/ResultsPane.xaml.cs +++ b/windows/src/TablePro.App/Views/ResultsPane.xaml.cs @@ -111,6 +111,26 @@ internal async Task ViewValueAsync() } } + // The focused cell as far as a filter by its value reads it: NULL or binary, and its text up to the longest a + // filter takes. Null when there is no cell or it cannot be read. + internal CellValue? FocusedCellForFilter() + { + if (!CanViewValue || ResultGrid.Data is not { } data) + { + return null; + } + + try + { + return CellValue.Read( + data, ShownSet, ResultGrid.FocusedRow, ResultGrid.FocusedColumn, CellFilters.MaximumValueLength, 1); + } + catch (TableProException) + { + return null; + } + } + // About the rows, not a failure of the run, so the status keeps what the run said. internal void ShowNotice(string message) { diff --git a/windows/src/TablePro.App/Views/StructureView.xaml.cs b/windows/src/TablePro.App/Views/StructureView.xaml.cs index c6f8442246..952f726ffc 100644 --- a/windows/src/TablePro.App/Views/StructureView.xaml.cs +++ b/windows/src/TablePro.App/Views/StructureView.xaml.cs @@ -106,6 +106,9 @@ internal async Task LoadAsync(bool refresh) } // What was shown before stays under the bar. + // The object menu's Show DDL opens the CREATE statement itself. + internal void ShowDdl() => DdlItem.IsSelected = true; + internal void ShowFailure(CoreError failure) { ShowLoading(false); diff --git a/windows/src/TablePro.App/Views/TableTabView.xaml.cs b/windows/src/TablePro.App/Views/TableTabView.xaml.cs index 5e2d933ae8..e7fe44b708 100644 --- a/windows/src/TablePro.App/Views/TableTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/TableTabView.xaml.cs @@ -27,7 +27,8 @@ internal TableTabView( string? schema, string table, ObjectKind kind, - SessionTab? restore = null) + SessionTab? restore = null, + ITableFilters? filters = null) { InitializeComponent(); Results.ShowsEdits = true; @@ -43,7 +44,8 @@ internal TableTabView( new TableName(schema, table), restore?.ObjectKindOr(kind) ?? kind, () => workbench.Settings, - restore); + restore, + filters); _model.StateChanged += (_, _) => StateChanged?.Invoke(this, EventArgs.Empty); Results.StatusChanged += (_, status) => StatusText.Text = status; Results.RowsStatus = (rows, milliseconds, truncated) => @@ -173,6 +175,12 @@ public void FocusContent() public void ShowData() => ShowView(structure: false); + public void ShowDdl() + { + ShowView(structure: true); + _structure?.ShowDdl(); + } + // The menu's Filter rows, as the command bar's toggle; from Structure it brings the rows back with the bar open. public void ToggleFilter() { @@ -618,7 +626,7 @@ private List SelectedRows() } // Built when asked for, since a menu keeps the window it first opened in and a tab can move to another. - private void OnGridContextRequested(UIElement sender, ContextRequestedEventArgs args) + private async void OnGridContextRequested(UIElement sender, ContextRequestedEventArgs args) { var grid = Results.GridView; // The editor's TextBox shows its own menu; a right-click on another cell keeps the edit first. @@ -628,7 +636,16 @@ private void OnGridContextRequested(UIElement sender, ContextRequestedEventArgs } args.Handled = true; + var place = GridMenus.Place(grid, args); var (row, column) = (grid.FocusedRow, grid.FocusedColumn); + var cellRules = grid.RowCount > 0 && !_model.IsDeleted(row) + ? await CellRulesAsync(row, column).ConfigureAwait(true) + : []; + if (XamlRoot is null || _model.IsClosed) + { + return; + } + var menu = new MenuFlyout(); // A row marked for delete offers only to keep it, as macOS does. if (grid.RowCount > 0 && _model.IsDeleted(row)) @@ -651,6 +668,11 @@ private void OnGridContextRequested(UIElement sender, ContextRequestedEventArgs menu.Items.Add(new MenuFlyoutSeparator()); menu.Items.Add(GridMenus.Copy(grid)); menu.Items.Add(GridMenus.ViewValue(Results)); + if (cellRules.Count > 0) + { + menu.Items.Add(FilterMenu(cellRules)); + } + AddReferences(menu, _model.ReferencesAt(row, column)); menu.Items.Add(new MenuFlyoutSeparator()); var finished = !IsRunning && Results.ShownSet is { IsFinished: true }; @@ -660,7 +682,64 @@ private void OnGridContextRequested(UIElement sender, ContextRequestedEventArgs finished && !_model.HasUnsavedChanges, () => ShareRequested?.Invoke(this, ShareKind.Result), Symbol.Share)); - GridMenus.Show(menu, grid, args); + GridMenus.Show(menu, grid, place); + } + + // The cell's value read before the choices, which can take a read the first time, so a later focus is not used. + private async Task> CellRulesAsync(int row, int column) + { + var value = Results.FocusedCellForFilter(); + var pending = _model.Edits is { } edits && (edits.IsAdded(row) || edits.GetCell(row, column) is not null); + if (value is null || pending) + { + return []; + } + + var choices = _model.FilterChoices ?? await _model.LoadFilterChoicesAsync().ConfigureAwait(true); + var offered = choices?.Columns + .FirstOrDefault(choice => string.Equals(choice.Name, value.Column, StringComparison.Ordinal))?.Operators ?? []; + return CellFilters.For(value.Column, offered, value, pending); + } + + // As macOS titles them: the column, the operator and the value the rule compares with. + private MenuFlyoutSubItem FilterMenu(IReadOnlyList rules) + { + var filter = new MenuFlyoutSubItem { Text = Strings.Get("GridMenuFilter"), Icon = new SymbolIcon(Symbol.Filter) }; + AutomationProperties.SetAutomationId(filter, "FilterByCell"); + foreach (var rule in rules) + { + var shown = rule.Value is FilterValue.One one ? MenuValue(one.Value) : string.Empty; + var item = new MenuFlyoutItem + { + Text = Strings.Format("CellFilter_" + Enum.GetName(rule.Operator), rule.Column, shown), + }; + AutomationProperties.SetAutomationId(item, "FilterByCell_" + Enum.GetName(rule.Operator)); + item.Click += async (_, _) => await FilterByAsync(rule).ConfigureAwait(true); + filter.Items.Add(item); + } + + return filter; + } + + // On one line and cut, since the rule itself carries the whole value. + private static string MenuValue(string value) + { + const int Shown = 40; + var line = value.ReplaceLineEndings(" "); + return line.Length <= Shown ? line : string.Concat(line.AsSpan(0, Shown), "…"); + } + + // The rule applies at once and shows in the bar, as one typed there would. + private async Task FilterByAsync(FilterRule rule) + { + if (!await _model.FilterByAsync(rule).ConfigureAwait(true)) + { + return; + } + + ShowView(structure: false); + ShowFilter(true, focus: false); + _filters?.ShowApplied(); } // One key the cell's column is in opens from the menu itself; several, and every key pointing at the row, are diff --git a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs new file mode 100644 index 0000000000..8dd5ffa321 --- /dev/null +++ b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs @@ -0,0 +1,317 @@ +using Microsoft.UI.Xaml.Controls; +using TablePro.App.Services; +using TablePro.Interop; + +namespace TablePro.App.Views; + +// Emptying and dropping a table, and making and dropping a schema or database: asked, planned by the core, run at once +// through Safe Mode, then the tree and the tabs follow (core/docs/decisions/0013-catalog-operations.md). +public sealed partial class WorkspaceView +{ + // Read-only, by the connection or its Safe Mode, offers none of them. + private CatalogAbilities Abilities => + _session is { IsReadOnly: false } session ? session.CatalogAbilities : CatalogAbilities.None; + + public bool CanCreateDatabase => Abilities.CreateDatabase; + + public bool CanCreateSchema => Abilities.CreateSchema; + + public bool CanTruncate(ObjectEntry entry) => IsTable(entry) && Abilities.TruncateTable; + + public bool CanDrop(ObjectEntry entry) => IsTable(entry) && Abilities.DropTable; + + public async Task NewSchemaAsync() + { + if (_session is not { } session || !CanCreateSchema) + { + return; + } + + var abilities = Abilities; + var choices = abilities.SchemaOwner ? await ChoicesAsync(session).ConfigureAwait(true) : DatabaseChoices.None; + using var change = await CatalogDialogs.AskNewSchemaAsync(this, session, abilities, choices).ConfigureAwait(true); + if (change is null || !await RunCatalogChangeAsync(session, change, "CreateSchemaFailed").ConfigureAwait(true)) + { + return; + } + + await RefreshAsync().ConfigureAwait(true); + if (CreatedName(change) is { } name) + { + RevealSchema(name); + } + } + + public async Task NewDatabaseAsync() + { + if (_session is not { } session || !CanCreateDatabase) + { + return; + } + + var abilities = Abilities; + var choices = await ChoicesAsync(session).ConfigureAwait(true); + using var change = await CatalogDialogs.AskNewDatabaseAsync(this, session, abilities, choices).ConfigureAwait(true); + if (change is null + || !await RunCatalogChangeAsync(session, change, abilities.Keyspaces ? "CreateKeyspaceFailed" : "CreateDatabaseFailed") + .ConfigureAwait(true)) + { + return; + } + + if (session.Layout == DatabaseLayout.AsSchemas) + { + await RefreshAsync().ConfigureAwait(true); + } + + // As macOS does, the window goes on in the new database. + if (CreatedName(change) is { } name && ReferenceEquals(_session, session)) + { + PickDatabase(session, name); + } + } + + private static bool IsTable(ObjectEntry entry) => entry.Kind is ObjectKind.Table or ObjectKind.PartitionedTable; + + private static string? CreatedName(CatalogChange change) => change.Operation switch + { + CatalogOperation.CreateSchema schema => schema.Name, + CatalogOperation.CreateDatabase database => database.Draft.Name, + _ => null, + }; + + private async Task TruncateAsync(ObjectEntry entry) + { + if (_session is not { } session || !CanTruncate(entry)) + { + return; + } + + var table = new TableName(entry.Schema, entry.Name); + var abilities = Abilities; + var referencing = await ReferencingAsync(session, table).ConfigureAwait(true); + if (referencing.Count > 0 && abilities.TruncateRefusedWhenReferenced) + { + await CatalogDialogs.ShowRefusedAsync( + this, + Strings.Get("TruncateRefusedTitle"), + Strings.Format("TruncateRefusedMessage", entry.Name, CatalogDialogs.Names(table, referencing))) + .ConfigureAwait(true); + return; + } + + var clusters = abilities.OnCluster ? (await ChoicesAsync(session).ConfigureAwait(true)).Clusters : []; + using var change = await CatalogDialogs.AskTruncateAsync(this, session, table, abilities, clusters).ConfigureAwait(true); + if (change is null || !await RunCatalogChangeAsync(session, change, "TruncateFailed").ConfigureAwait(true)) + { + return; + } + + // A cascade empties the tables pointing at this one too, so every table tab reads again. + var cascaded = change.Operation is CatalogOperation.TruncateTable { Cascade: true }; + var views = TableViews().Where(view => cascaded || view.Shows(entry.Schema, entry.Name)).ToList(); + foreach (var view in views) + { + if (TableViews().Contains(view)) + { + await view.RefreshRowsAsync().ConfigureAwait(true); + } + } + } + + private async Task DropTableAsync(ObjectEntry entry) + { + if (_session is not { } session || !CanDrop(entry)) + { + return; + } + + var table = new TableName(entry.Schema, entry.Name); + var abilities = Abilities; + var referencing = await ReferencingAsync(session, table).ConfigureAwait(true); + if (referencing.Count > 0 && !abilities.DropTableCascade) + { + await CatalogDialogs.ShowRefusedAsync( + this, + Strings.Get("DropTableRefusedTitle"), + Strings.Format("DropTableRefusedMessage", entry.Name, CatalogDialogs.Names(table, referencing))) + .ConfigureAwait(true); + return; + } + + using var change = await CatalogDialogs.AskDropTableAsync(this, session, table, abilities, referencing) + .ConfigureAwait(true); + if (change is null || !await RunCatalogChangeAsync(session, change, "DropTableFailed").ConfigureAwait(true)) + { + return; + } + + await DiscardTabsAsync(view => view.Shows(entry.Schema, entry.Name)).ConfigureAwait(true); + _workbench.TableFilters.ForgetTable(FilterConnection, session.Database, table); + await RefreshAsync().ConfigureAwait(true); + if (entry.Schema is { } schema && !_singleSchema) + { + RevealSchema(schema); + } + } + + private async Task DropSchemaAsync(string schema) + { + if (_session is not { } session || !Abilities.DropSchema) + { + return; + } + + using var change = await CatalogDialogs.AskDropSchemaAsync(this, session, schema, Abilities).ConfigureAwait(true); + if (change is null || !await RunCatalogChangeAsync(session, change, "DropSchemaFailed").ConfigureAwait(true)) + { + return; + } + + await DiscardTabsAsync(view => string.Equals(view.Model.Table.Schema, schema, StringComparison.Ordinal)) + .ConfigureAwait(true); + _workbench.TableFilters.ForgetSchema(FilterConnection, session.Database, schema); + await RefreshAsync().ConfigureAwait(true); + } + + private async Task DropDatabaseAsync(string database) + { + if (_session is not { } session || !Abilities.DropDatabase) + { + return; + } + + var abilities = Abilities; + var clusters = abilities.OnCluster ? (await ChoicesAsync(session).ConfigureAwait(true)).Clusters : []; + using var change = await CatalogDialogs.AskDropDatabaseAsync(this, session, database, abilities, clusters) + .ConfigureAwait(true); + if (change is null + || !await RunCatalogChangeAsync(session, change, abilities.Keyspaces ? "DropKeyspaceFailed" : "DropDatabaseFailed") + .ConfigureAwait(true)) + { + return; + } + + if (session.Layout == DatabaseLayout.AsSchemas) + { + // Each database is a schema of this connection's catalog, so its tabs and filters go with it. + await DiscardTabsAsync(view => string.Equals(view.Model.Table.Schema, database, StringComparison.Ordinal)) + .ConfigureAwait(true); + _workbench.TableFilters.ForgetSchema(FilterConnection, session.Database, database); + if (string.Equals(_defaultDatabase, database, StringComparison.Ordinal)) + { + _defaultDatabase = null; + SessionChanged?.Invoke(this, EventArgs.Empty); + } + + await RefreshAsync().ConfigureAwait(true); + ShowDatabase(); + } + else + { + _workbench.TableFilters.ForgetDatabase(FilterConnection, database); + } + } + + // A creation that Safe Mode asks before is asked once more with the statements, as a save is; emptying or dropping + // was asked in its own question, which showed them. True only once the change landed on this window's session. + private async Task RunCatalogChangeAsync(DatabaseSession session, CatalogChange change, string failedTitle) + { + if (change.Gate == SaveGate.Refuse) + { + await CatalogDialogs.ShowRefusedAsync(this, Strings.Get(failedTitle), Strings.Get("EditLockReadOnly")) + .ConfigureAwait(true); + return false; + } + + if (change.Gate == SaveGate.Confirm && !change.IsDestructive) + { + var statements = change.Statements.Select(statement => new PlannedStatement(statement, [])).ToList(); + if (!await RowEditDialogs.ConfirmSaveAsync(this, session.Name, statements).ConfigureAwait(true)) + { + return false; + } + } + + CatalogChangeOutcome outcome; + try + { + outcome = await change.RunAsync(confirmed: true).ConfigureAwait(true); + } + catch (TableProException exception) + { + await CatalogDialogs.ShowRefusedAsync(this, Strings.Get(failedTitle), Strings.Error(exception.Error)) + .ConfigureAwait(true); + return false; + } + catch (OperationCanceledException) + { + return false; + } + + switch (outcome) + { + case CatalogChangeOutcome.Failed failed: + await CatalogDialogs.ShowRefusedAsync(this, Strings.Get(failedTitle), Strings.Error(failed.Error)) + .ConfigureAwait(true); + return false; + case CatalogChangeOutcome.Unknown unknown: + // It may not have landed, so the tree is read again but no tab or kept filter goes with it. + await CatalogDialogs.ShowRefusedAsync( + this, + Strings.Get(failedTitle), + $"{Strings.Error(unknown.Error)} {Strings.Get("CatalogChangeUnknown")}") + .ConfigureAwait(true); + if (ReferenceEquals(_session, session)) + { + await RefreshAsync().ConfigureAwait(true); + } + + return false; + default: + return ReferenceEquals(_session, session); + } + } + + // The keys that cannot be read leave the refusal to the server. + private static async Task> ReferencingAsync(DatabaseSession session, TableName table) + { + try + { + using var keys = await session.ReadTableKeysAsync(table).ConfigureAwait(true); + return keys.ReferencingTables; + } + catch (TableProException) + { + return []; + } + } + + // A server that lists none leaves every picker with only its own default. + private static async Task ChoicesAsync(DatabaseSession session) + { + try + { + return await session.GetDatabaseChoicesAsync().ConfigureAwait(true); + } + catch (TableProException) + { + return DatabaseChoices.None; + } + } + + private List TableViews() => + [.. Tabs.TabItems.OfType().Select(item => item.Content).OfType()]; + + // The table is gone, so its tabs close without asking about their changes, and none is kept to reopen. + private async Task DiscardTabsAsync(Func shows) + { + var items = Tabs.TabItems.OfType() + .Where(item => item.Content is TableTabView view && shows(view)) + .ToList(); + foreach (var item in items) + { + await _tabs.DiscardAsync(item).ConfigureAwait(true); + } + } +} diff --git a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs index 2b739803d2..b164691372 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs @@ -481,6 +481,13 @@ public async Task OpenSqlFileAsync(string path) return true; } + // Per connection and the database it is in, as macOS keys a table's filter. + private ITableFilters TableFilters => + _workbench.TableFilters.For(FilterConnection, _session?.Database); + + private string FilterConnection => + TableFilterMemory.ConnectionKey(_connection.ConnectionId, _connection.Origin.FilePath); + // The open tab of the table is brought forward rather than a second one opened. private TableTabView? OpenTable(ObjectEntry entry) { @@ -504,7 +511,7 @@ public async Task OpenSqlFileAsync(string path) } // Its first page is read when the tab first shows, so a restored session reads only the tab it shows. - var table = new TableTabView(_workbench, _share, schema, name, kind, restore); + var table = new TableTabView(_workbench, _share, schema, name, kind, restore, TableFilters); AddTab(table, IconOf(kind), index); return table; } @@ -861,19 +868,32 @@ public async Task ExportAsync() // Read now, since the dialogs left the tab free to run again or page meanwhile. using var exporting = new CancellationTokenSource(); + var name = Path.GetFileName(request.Path); Task export; + long? total = null; + // Reported on this thread, so the bar counts the rows written as they go. + var progress = new Progress(rows => ShowExportRows(name, rows, total)); if (request.Scope == ExportScope.EntireTable && tab is TableTabView whole) { var sort = results.Sort is { Direction: not SortDirection.None } shown ? new ColumnSort(shown.Column, shown.Direction == SortDirection.Ascending) : null; + total = whole.Model.Page.Total; export = share.Session.ExportTableAsync( - whole.Model.Table, sort, whole.Model.Filter, request.Format, request.Path, exporting.Token); + whole.Model.Table, sort, whole.Model.Filter, request.Format, request.Path, progress, exporting.Token); } else if (results.ShownSet is { } rows) { + total = rows.RowCount; export = share.Session.ExportRowsAsync( - rows, results.Columns.Count, request.Format, table, request.Path, exporting.Token); + rows, + results.Columns.Count, + request.Format, + table, + request.Path, + (tab as QueryTabView)?.Database, + progress, + exporting.Token); } else { @@ -881,7 +901,6 @@ public async Task ExportAsync() return; } - var name = Path.GetFileName(request.Path); _exporting = exporting; ShowExport(InfoBarSeverity.Informational, Strings.Format("ExportRunning", name), running: true); CommandsChanged?.Invoke(this, EventArgs.Empty); @@ -972,6 +991,7 @@ private void ShowExport(InfoBarSeverity severity, string message, bool running) ExportBar.Message = message; ExportBar.IsClosable = !running; ExportProgress.Visibility = running ? Visibility.Visible : Visibility.Collapsed; + ExportProgress.IsIndeterminate = true; CancelExportButton.Visibility = running ? Visibility.Visible : Visibility.Collapsed; // A closed InfoBar keeps its margin, so the bar is collapsed while it is closed. ExportBar.Visibility = Visibility.Visible; @@ -980,6 +1000,27 @@ private void ShowExport(InfoBarSeverity severity, string message, bool running) ExportBar.IsOpen = true; } + // Only the text and the bar change, so Narrator is not read the bar again for every count. + private void ShowExportRows(string name, long rows, long? total) + { + if (_exporting is null || !ExportBar.IsOpen) + { + return; + } + + if (total is { } all && all > 0) + { + ExportBar.Message = Strings.Format("ExportRunningRowsOf", name, rows, all); + ExportProgress.IsIndeterminate = false; + ExportProgress.Maximum = all; + ExportProgress.Value = Math.Min(rows, all); + } + else + { + ExportBar.Message = Strings.Format("ExportRunningRows", name, rows); + } + } + private void OnExportBarClosed(InfoBar sender, InfoBarClosedEventArgs args) => ExportBar.Visibility = Visibility.Collapsed; @@ -1681,7 +1722,10 @@ private async Task LoadTreeAsync(bool refresh = false) _loadingTree = loading; try { - var schemas = await session.GetSchemasAsync(loading.Token).ConfigureAwait(true); + // A refresh reads the schemas again too, so one made or dropped in the editor shows. + var schemas = refresh + ? await session.RefreshSchemasAsync(loading.Token).ConfigureAwait(true) + : await session.GetSchemasAsync(loading.Token).ConfigureAwait(true); loading.Token.ThrowIfCancellationRequested(); var shown = schemas.Where(schema => !schema.IsSystem).ToList(); if (shown.Count == 0) @@ -1936,10 +1980,23 @@ private void Open(ObjectNode node) private void OnObjectRowContextRequested(UIElement sender, ContextRequestedEventArgs args) { - if (sender is TreeViewItem row && ObjectTree.ItemFromContainer(row) is ObjectNode { Entry: { } entry }) + if (sender is not TreeViewItem row) { - args.Handled = true; - ShowObjectMenu(row, entry, args); + return; + } + + switch (ObjectTree.ItemFromContainer(row)) + { + case ObjectNode { Entry: { } entry }: + args.Handled = true; + ShowObjectMenu(row, entry, args); + break; + case ObjectNode { Kind: ObjectNodeKind.Schema, Schema: { } schema }: + args.Handled = true; + ShowSchemaMenu(row, schema, args); + break; + default: + break; } } @@ -1972,8 +2029,11 @@ private void ShowObjectMenu(FrameworkElement row, ObjectEntry entry, ContextRequ if (ObjectFolders.HasRows(entry.Kind)) { menu.Items.Add(ObjectMenuItem("ObjectMenuOpen", Symbol.OpenFile, () => OpenTable(entry))); + menu.Items.Add(ObjectMenuItem( + "ObjectMenuOpenInNewTab", Symbol.NewWindow, () => AddTable(entry.Schema, entry.Name, entry.Kind, null, null))); menu.Items.Add(ObjectMenuItem( "ObjectMenuShowStructure", Symbol.List, () => OpenTable(entry)?.ShowStructure())); + menu.Items.Add(ObjectMenuItem("ObjectMenuShowDdl", Symbol.Preview, () => OpenTable(entry)?.ShowDdl())); if (entry.Kind is ObjectKind.Table or ObjectKind.PartitionedTable) { menu.Items.Add(ObjectMenuItem("ObjectMenuImportCsv", Symbol.Import, async () => await ImportCsvAsync(entry).ConfigureAwait(true))); @@ -1993,6 +2053,77 @@ private void ShowObjectMenu(FrameworkElement row, ObjectEntry entry, ContextRequ } menu.Items.Add(ObjectMenuItem("ObjectMenuCopyName", Symbol.Copy, () => CopyText(entry.Name))); + if (CanTruncate(entry) || CanDrop(entry)) + { + menu.Items.Add(new MenuFlyoutSeparator()); + if (CanTruncate(entry)) + { + menu.Items.Add(ObjectMenuItem("ObjectMenuTruncate", Symbol.Clear, async () => await TruncateAsync(entry).ConfigureAwait(true))); + } + + if (CanDrop(entry)) + { + menu.Items.Add(ObjectMenuItem("ObjectMenuDrop", Symbol.Delete, async () => await DropTableAsync(entry).ConfigureAwait(true))); + } + } + + ShowMenu(menu, row, args); + } + + // A schema row is a database where the server's databases are its schemas, as MySQL's are. + private void ShowSchemaMenu(FrameworkElement row, string schema, ContextRequestedEventArgs args) + { + var menu = new MenuFlyout(); + var abilities = Abilities; + if (_session is { Layout: DatabaseLayout.AsSchemas } session) + { + if (abilities.CreateDatabase) + { + menu.Items.Add(ObjectMenuItem( + abilities.Keyspaces ? "ObjectMenuNewKeyspace" : "ObjectMenuNewDatabase", + Symbol.Add, + async () => await NewDatabaseAsync().ConfigureAwait(true))); + } + + if (abilities.DropDatabase && Droppable(session, schema)) + { + menu.Items.Add(NamedMenuItem( + abilities.Keyspaces ? "ObjectMenuDropKeyspace" : "ObjectMenuDropDatabase", + schema, + async () => await DropDatabaseAsync(schema).ConfigureAwait(true))); + } + } + else if (_session is { } other) + { + if (abilities.CreateSchema) + { + menu.Items.Add(ObjectMenuItem("ObjectMenuNewSchema", Symbol.Add, async () => await NewSchemaAsync().ConfigureAwait(true))); + } + + if (abilities.DropSchema && !other.ProtectsSchema(schema)) + { + menu.Items.Add(NamedMenuItem( + "ObjectMenuDropSchema", schema, async () => await DropSchemaAsync(schema).ConfigureAwait(true))); + } + } + + if (menu.Items.Count > 0) + { + menu.Items.Add(new MenuFlyoutSeparator()); + } + + menu.Items.Add(ObjectMenuItem("ObjectMenuCopyName", Symbol.Copy, () => CopyText(schema))); + ShowMenu(menu, row, args); + } + + // Never the engine's own, nor the one this window is in, as macOS offers a drop. + private bool Droppable(DatabaseSession session, string database) => + !session.ProtectsDatabase(database) + && !string.Equals(database, CurrentDatabase, StringComparison.Ordinal) + && !string.Equals(database, session.Database ?? _listedOwnDatabase, StringComparison.Ordinal); + + private static void ShowMenu(MenuFlyout menu, FrameworkElement row, ContextRequestedEventArgs args) + { if (args.TryGetPosition(row, out var point)) { menu.ShowAt(row, new FlyoutShowOptions { Position = point }); @@ -2003,6 +2134,13 @@ private void ShowObjectMenu(FrameworkElement row, ObjectEntry entry, ContextRequ } } + private static MenuFlyoutItem NamedMenuItem(string text, string name, Action invoke) + { + var item = new MenuFlyoutItem { Text = Strings.Format(text, name), Icon = new SymbolIcon(Symbol.Delete) }; + item.Click += (_, _) => invoke(); + return item; + } + private static MenuFlyoutItem ObjectMenuItem(string text, Symbol icon, Action invoke) { var item = new MenuFlyoutItem { Text = Strings.Get(text), Icon = new SymbolIcon(icon) }; @@ -2276,9 +2414,50 @@ private async Task ShowDatabaseMenuAsync() menu.Items.Add(new MenuFlyoutItem { Text = Strings.Get("DatabaseNoneListed"), IsEnabled = false }); } + AddDatabaseCommands(menu, session, databases); menu.ShowAt(DatabaseButton, new FlyoutShowOptions { Placement = FlyoutPlacementMode.BottomEdgeAlignedLeft }); } + // New database at the foot of the list, as macOS has it, and a drop of any but the engine's own and this one. + private void AddDatabaseCommands(MenuFlyout menu, DatabaseSession session, IReadOnlyList databases) + { + var abilities = Abilities; + var droppable = abilities.DropDatabase + ? databases.Where(database => !database.IsSystem && Droppable(session, database.Name)).ToList() + : []; + if (!abilities.CreateDatabase && droppable.Count == 0) + { + return; + } + + menu.Items.Add(new MenuFlyoutSeparator()); + if (abilities.CreateDatabase) + { + menu.Items.Add(ObjectMenuItem( + abilities.Keyspaces ? "ObjectMenuNewKeyspace" : "ObjectMenuNewDatabase", + Symbol.Add, + async () => await NewDatabaseAsync().ConfigureAwait(true))); + } + + if (droppable.Count > 0) + { + var drop = new MenuFlyoutSubItem + { + Text = Strings.Get(abilities.Keyspaces ? "DatabaseMenuDropKeyspace" : "DatabaseMenuDropDatabase"), + Icon = new SymbolIcon(Symbol.Delete), + }; + foreach (var database in droppable) + { + var name = database.Name; + var item = new MenuFlyoutItem { Text = name }; + item.Click += async (_, _) => await DropDatabaseAsync(name).ConfigureAwait(true); + drop.Items.Add(item); + } + + menu.Items.Add(drop); + } + } + // Where each database holds schemas of its own, another one is another session, opened beside this one. private void PickDatabase(DatabaseSession session, string name) { diff --git a/windows/src/TablePro.Interop/CatalogChange.cs b/windows/src/TablePro.Interop/CatalogChange.cs new file mode 100644 index 0000000000..c99c9a7df5 --- /dev/null +++ b/windows/src/TablePro.Interop/CatalogChange.cs @@ -0,0 +1,305 @@ +using Native = TablePro.Interop.Native; + +namespace TablePro.Interop; + +// What the engine does to whole tables, schemas and databases, so a menu offers only those. +public sealed record CatalogAbilities( + bool TruncateTable, + bool TruncateCascade, + bool TruncateRestartIdentity, + bool TruncateRefusedWhenReferenced, + bool DropTable, + bool DropTableCascade, + bool CreateSchema, + bool DropSchema, + bool DropSchemaCascade, + bool SchemaOwner, + bool CreateDatabase, + bool DropDatabase, + bool DropDatabaseForce, + DatabaseFields DatabaseFields, + bool Keyspaces, + bool OnCluster) +{ + public static CatalogAbilities None { get; } = new( + false, false, false, false, false, false, false, false, false, false, false, false, false, + DatabaseFields.None, false, false); +} + +// The options a new database takes beside its name. +public sealed record DatabaseFields( + bool Owner, + bool Template, + bool Encoding, + bool Locale, + bool CharacterSet, + bool Collation, + bool Replication, + bool Tablets, + bool FirstCollection) +{ + public static DatabaseFields None { get; } = new(false, false, false, false, false, false, false, false, false); +} + +public sealed record CharacterSet(string Name, string DefaultCollation, IReadOnlyList Collations); + +// What the server offers for a new database's options; an empty list leaves only the server's default. +public sealed record DatabaseChoices( + IReadOnlyList Owners, + IReadOnlyList Templates, + IReadOnlyList Encodings, + IReadOnlyList Locales, + IReadOnlyList CharacterSets, + IReadOnlyList Collations, + IReadOnlyList Clusters, + IReadOnlyList DataCenters, + string? DefaultOwner, + string? DefaultEncoding, + string? DefaultLocale, + string? DefaultCharacterSet, + string? DefaultCollation) +{ + public static DatabaseChoices None { get; } = new([], [], [], [], [], [], [], [], null, null, null, null, null); +} + +public sealed record DataCenterReplicas(string DataCenter, int Replicas); + +public abstract record KeyspaceReplication +{ + private KeyspaceReplication() + { + } + + public sealed record Simple(int Factor) : KeyspaceReplication; + + public sealed record NetworkTopology(IReadOnlyList Factors) : KeyspaceReplication; +} + +// A new database or keyspace; null leaves an option to the server. +public sealed record DatabaseDraft(string Name) +{ + public string? Owner { get; init; } + + public string? Template { get; init; } + + public string? Encoding { get; init; } + + public string? Locale { get; init; } + + public string? CharacterSet { get; init; } + + public string? Collation { get; init; } + + public string? Cluster { get; init; } + + public KeyspaceReplication? Replication { get; init; } + + public bool DurableWrites { get; init; } = true; + + public bool? Tablets { get; init; } + + public string? FirstCollection { get; init; } +} + +public abstract record CatalogOperation +{ + private CatalogOperation() + { + } + + public sealed record TruncateTable(TableName Table, bool Cascade, bool RestartIdentity, string? Cluster) + : CatalogOperation; + + public sealed record DropTable(TableName Table, bool Cascade) : CatalogOperation; + + public sealed record CreateSchema(string Name, string? Owner) : CatalogOperation; + + public sealed record DropSchema(string Name, bool Cascade) : CatalogOperation; + + public sealed record CreateDatabase(DatabaseDraft Draft) : CatalogOperation; + + public sealed record DropDatabase(string Name, bool Force, string? Cluster) : CatalogOperation; +} + +public abstract record CatalogChangeOutcome +{ + private CatalogChangeOutcome() + { + } + + public sealed record Done : CatalogChangeOutcome + { + public static Done Instance { get; } = new(); + } + + // Nothing changed. + public sealed record Failed(CoreError Error) : CatalogChangeOutcome; + + // The change may have landed in part or in full. + public sealed record Unknown(CoreError Error) : CatalogChangeOutcome; +} + +// One catalog change, which runs only the statements it was planned with, once. +public sealed class CatalogChange : IDisposable +{ + private readonly Native.CatalogChange _change; + + internal CatalogChange(Native.CatalogChange change, CatalogOperation operation) + { + _change = change; + Operation = operation; + Statements = change.Statements(); + Gate = Mapping.Gate(change.Gate()); + IsDestructive = change.Destructive(); + } + + // What was planned, with the options it was planned with. + public CatalogOperation Operation { get; } + + public IReadOnlyList Statements { get; } + + // Emptying or dropping asks at every level but Read-only, which refuses; creating asks where writes ask. + public SaveGate Gate { get; } + + public bool IsDestructive { get; } + + // Confirmed says the gate's question was answered; nothing is sent when the level or the plan moved since. + public async Task RunAsync(bool confirmed, CancellationToken cancellationToken = default) + { + var outcome = await CoreCall.RunAsync(token => _change.Run(confirmed, token), cancellationToken) + .ConfigureAwait(false); + return outcome switch + { + Native.CatalogChangeOutcome.Done => CatalogChangeOutcome.Done.Instance, + Native.CatalogChangeOutcome.Failed failed => new CatalogChangeOutcome.Failed(Mapping.Error(failed.Error)), + Native.CatalogChangeOutcome.Unknown unknown => new CatalogChangeOutcome.Unknown(Mapping.Error(unknown.Error)), + _ => throw new InvalidOperationException("The core reported a catalog outcome this build does not know."), + }; + } + + public void Dispose() => _change.Dispose(); +} + +public sealed partial class DatabaseSession +{ + // Read again on each call, since a reconnect can reach another server version. + public CatalogAbilities CatalogAbilities => Catalog.Abilities(_session.CatalogAbilities()); + + public bool ProtectsDatabase(string name) + { + ArgumentNullException.ThrowIfNull(name); + return _session.ProtectsDatabase(name); + } + + public bool ProtectsSchema(string name) + { + ArgumentNullException.ThrowIfNull(name); + return _session.ProtectsSchema(name); + } + + public async Task GetDatabaseChoicesAsync(CancellationToken cancellationToken = default) + { + var choices = await CoreCall.RunAsync(token => _session.DatabaseChoices(token), cancellationToken) + .ConfigureAwait(false); + return Catalog.Choices(choices); + } + + // At the connection's Safe Mode level now; an option or name the engine cannot take throws a catalog.* code. + public CatalogChange PlanCatalogChange(CatalogOperation operation) + { + ArgumentNullException.ThrowIfNull(operation); + var native = Catalog.Operation(operation); + return new CatalogChange(CoreCall.Run(() => _session.PlanCatalogChange(native)), operation); + } + + // Read again from the server, as after a schema was created or dropped. + public async Task> RefreshSchemasAsync(CancellationToken cancellationToken = default) + { + var schemas = await CoreCall.RunAsync(token => _session.RefreshSchemas(token), cancellationToken) + .ConfigureAwait(false); + return [.. schemas.Select(Mapping.Schema)]; + } +} + +internal static class Catalog +{ + public static CatalogAbilities Abilities(Native.CatalogAbilities abilities) + { + var fields = abilities.DatabaseFields; + return new CatalogAbilities( + abilities.TruncateTable, + abilities.TruncateCascade, + abilities.TruncateRestartIdentity, + abilities.TruncateRefusedWhenReferenced, + abilities.DropTable, + abilities.DropTableCascade, + abilities.CreateSchema, + abilities.DropSchema, + abilities.DropSchemaCascade, + abilities.SchemaOwner, + abilities.CreateDatabase, + abilities.DropDatabase, + abilities.DropDatabaseForce, + new DatabaseFields( + fields.Owner, + fields.Template, + fields.Encoding, + fields.Locale, + fields.CharacterSet, + fields.Collation, + fields.Replication, + fields.Tablets, + fields.FirstCollection), + abilities.Keyspaces, + abilities.OnCluster); + } + + public static DatabaseChoices Choices(Native.DatabaseChoicesInfo choices) => new( + choices.Owners, + choices.Templates, + choices.Encodings, + choices.Locales, + [.. choices.CharacterSets.Select(set => new CharacterSet(set.Name, set.DefaultCollation, set.Collations))], + choices.Collations, + choices.Clusters, + choices.DataCenters, + choices.DefaultOwner, + choices.DefaultEncoding, + choices.DefaultLocale, + choices.DefaultCharacterSet, + choices.DefaultCollation); + + public static Native.CatalogOperation Operation(CatalogOperation operation) => operation switch + { + CatalogOperation.TruncateTable truncate => new Native.CatalogOperation.TruncateTable( + Mapping.Table(truncate.Table), truncate.Cascade, truncate.RestartIdentity, truncate.Cluster), + CatalogOperation.DropTable drop => new Native.CatalogOperation.DropTable(Mapping.Table(drop.Table), drop.Cascade), + CatalogOperation.CreateSchema create => new Native.CatalogOperation.CreateSchema(create.Name, create.Owner), + CatalogOperation.DropSchema drop => new Native.CatalogOperation.DropSchema(drop.Name, drop.Cascade), + CatalogOperation.CreateDatabase create => new Native.CatalogOperation.CreateDatabase(Draft(create.Draft)), + CatalogOperation.DropDatabase drop => new Native.CatalogOperation.DropDatabase(drop.Name, drop.Force, drop.Cluster), + _ => throw new ArgumentOutOfRangeException(nameof(operation), operation, "A catalog operation the core does not know."), + }; + + private static Native.DatabaseDraftArg Draft(DatabaseDraft draft) => new( + draft.Name, + draft.Owner, + draft.Template, + draft.Encoding, + draft.Locale, + draft.CharacterSet, + draft.Collation, + draft.Cluster, + draft.Replication switch + { + null => null, + KeyspaceReplication.Simple simple => new Native.ReplicationArg.Simple(checked((uint)simple.Factor)), + KeyspaceReplication.NetworkTopology topology => new Native.ReplicationArg.NetworkTopology( + [.. topology.Factors.Select(factor => + new Native.DataCenterReplicas(factor.DataCenter, checked((uint)factor.Replicas)))]), + _ => throw new ArgumentOutOfRangeException(nameof(draft), draft.Replication, "A replication the core does not know."), + }, + draft.DurableWrites, + draft.Tablets, + draft.FirstCollection); +} diff --git a/windows/src/TablePro.Interop/DatabaseSession.Export.cs b/windows/src/TablePro.Interop/DatabaseSession.Export.cs index 79d710689b..130c98de26 100644 --- a/windows/src/TablePro.Interop/DatabaseSession.Export.cs +++ b/windows/src/TablePro.Interop/DatabaseSession.Export.cs @@ -5,28 +5,40 @@ namespace TablePro.Interop; // A file appears whole or not at all: the core writes beside it and renames once the last row is in. public sealed partial class DatabaseSession { - // Only the first visibleColumns columns, since a table page carries row addresses after them. + private static readonly TimeSpan ProgressInterval = TimeSpan.FromMilliseconds(250); + + // Only the first visibleColumns columns, since a table page carries row addresses after them. SqlInsert with no + // table writes into the one table every column was read from, else into query_results; database names where the + // rows were read when that is not the session's own. public async Task ExportRowsAsync( ResultSet rows, int visibleColumns, ExportFormat format, TableName? table, string path, + string? database = null, + IProgress? progress = null, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(rows); ArgumentOutOfRangeException.ThrowIfNegative(visibleColumns); ArgumentException.ThrowIfNullOrEmpty(path); var target = table is null ? null : Mapping.Table(table); - var written = await CoreCall.RunAsync( - token => Handle.ExportRows( - rows.Handle, - checked((uint)visibleColumns), - NativeFormat(format), - target, - path, - token), - cancellationToken) + using var counter = new Native.ExportProgress(); + var written = await Reporting( + counter, + progress, + CoreCall.RunAsync( + token => Handle.ExportRows( + rows.Handle, + checked((uint)visibleColumns), + NativeFormat(format), + target, + database, + path, + counter, + token), + cancellationToken)) .ConfigureAwait(false); return Mapping.Count(written); } @@ -38,25 +50,56 @@ public async Task ExportTableAsync( TableFilter? filter, ExportFormat format, string path, + IProgress? progress = null, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(table); ArgumentException.ThrowIfNullOrEmpty(path); var nativeSort = sort is null ? null : new Native.ColumnSort(checked((uint)sort.Column), sort.Ascending); var nativeFilter = filter is null ? null : Mapping.Filter(filter); - var written = await CoreCall.RunAsync( - token => Handle.ExportTable( - Mapping.Table(table), - nativeSort, - nativeFilter, - NativeFormat(format), - path, - token), - cancellationToken) + using var counter = new Native.ExportProgress(); + var written = await Reporting( + counter, + progress, + CoreCall.RunAsync( + token => Handle.ExportTable( + Mapping.Table(table), + nativeSort, + nativeFilter, + NativeFormat(format), + path, + counter, + token), + cancellationToken)) .ConfigureAwait(false); return Mapping.Count(written); } + // The core counts the rows as it writes them, and the count is read a few times a second until the export ends. + private static async Task Reporting(Native.ExportProgress counter, IProgress? progress, Task export) + { + if (progress is null) + { + return await export.ConfigureAwait(false); + } + + using var timer = new PeriodicTimer(ProgressInterval); + var reported = -1L; + while (!export.IsCompleted) + { + var tick = timer.WaitForNextTickAsync().AsTask(); + await Task.WhenAny(export, tick).ConfigureAwait(false); + var rows = Mapping.Count(counter.Rows()); + if (rows != reported) + { + reported = rows; + progress.Report(rows); + } + } + + return await export.ConfigureAwait(false); + } + private static Native.ExportFormatTag NativeFormat(ExportFormat format) => format switch { ExportFormat.Csv => Native.ExportFormatTag.Csv, diff --git a/windows/src/TablePro.Interop/Export.cs b/windows/src/TablePro.Interop/Export.cs index b4ae3181ed..35be9326e9 100644 --- a/windows/src/TablePro.Interop/Export.cs +++ b/windows/src/TablePro.Interop/Export.cs @@ -1,6 +1,6 @@ namespace TablePro.Interop; -// SqlInsert writes the rows back into the table they came from, so it needs that table. +// SqlInsert writes the rows into the table they came from, or into query_results where no one table is known. public enum ExportFormat { Csv, diff --git a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs index 0ae8683d32..55f6613b7f 100644 --- a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs +++ b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs @@ -1245,6 +1245,36 @@ public struct UniffiVTableCallbackInterfaceRunListener + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1519,6 +1549,72 @@ public static extern sbyte uniffi_tablepro_ffi_fn_method_canceltoken_is_cancelled(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ulong uniffi_tablepro_ffi_fn_clone_catalogchange(ulong @handle,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + void uniffi_tablepro_ffi_fn_free_catalogchange(ulong @handle,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + sbyte uniffi_tablepro_ffi_fn_method_catalogchange_destructive(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + RustBuffer uniffi_tablepro_ffi_fn_method_catalogchange_gate(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ulong uniffi_tablepro_ffi_fn_method_catalogchange_run(ulong @ptr,sbyte @confirmed,RustBuffer @cancel + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + RustBuffer uniffi_tablepro_ffi_fn_method_catalogchange_statements(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -1728,6 +1824,50 @@ public static extern ulong uniffi_tablepro_ffi_fn_method_queryeditor_use_database(ulong @ptr,RustBuffer @database,RustBuffer @cancel ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ulong uniffi_tablepro_ffi_fn_clone_exportprogress(ulong @handle,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + void uniffi_tablepro_ffi_fn_free_exportprogress(ulong @handle,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ulong uniffi_tablepro_ffi_fn_constructor_exportprogress_new(ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ulong uniffi_tablepro_ffi_fn_method_exportprogress_rows(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -2355,6 +2495,17 @@ public static extern RustBuffer uniffi_tablepro_ffi_fn_method_tablekeys_follow_reference(ulong @ptr,ulong @rows,ulong @row,sbyte @referencing,uint @key,ref UniffiRustCallStatus _uniffi_out_err ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + RustBuffer uniffi_tablepro_ffi_fn_method_tablekeys_referencing_tables(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -2935,7 +3086,73 @@ public static partial [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] public static extern #endif - ulong uniffi_tablepro_ffi_fn_method_databasesession_export_rows(ulong @ptr,ulong @rows,uint @visibleColumns,RustBuffer @format,RustBuffer @table,RustBuffer @path,RustBuffer @cancel + RustBuffer uniffi_tablepro_ffi_fn_method_databasesession_catalog_abilities(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ulong uniffi_tablepro_ffi_fn_method_databasesession_database_choices(ulong @ptr,RustBuffer @cancel + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ulong uniffi_tablepro_ffi_fn_method_databasesession_plan_catalog_change(ulong @ptr,RustBuffer @operation,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + sbyte uniffi_tablepro_ffi_fn_method_databasesession_protects_database(ulong @ptr,RustBuffer @name,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + sbyte uniffi_tablepro_ffi_fn_method_databasesession_protects_schema(ulong @ptr,RustBuffer @name,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ulong uniffi_tablepro_ffi_fn_method_databasesession_refresh_schemas(ulong @ptr,RustBuffer @cancel + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ulong uniffi_tablepro_ffi_fn_method_databasesession_export_rows(ulong @ptr,ulong @rows,uint @visibleColumns,RustBuffer @format,RustBuffer @table,RustBuffer @database,RustBuffer @path,RustBuffer @progress,RustBuffer @cancel ); #if NET8_0_OR_GREATER @@ -2946,7 +3163,7 @@ public static partial [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] public static extern #endif - ulong uniffi_tablepro_ffi_fn_method_databasesession_export_table(ulong @ptr,RustBuffer @table,RustBuffer @sort,RustBuffer @filter,RustBuffer @format,RustBuffer @path,RustBuffer @cancel + ulong uniffi_tablepro_ffi_fn_method_databasesession_export_table(ulong @ptr,RustBuffer @table,RustBuffer @sort,RustBuffer @filter,RustBuffer @format,RustBuffer @path,RustBuffer @progress,RustBuffer @cancel ); #if NET8_0_OR_GREATER @@ -4302,6 +4519,50 @@ public static extern ushort uniffi_tablepro_ffi_checksum_method_canceltoken_is_cancelled( ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_catalogchange_destructive( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_catalogchange_gate( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_catalogchange_run( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_catalogchange_statements( + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -4445,6 +4706,17 @@ public static extern ushort uniffi_tablepro_ffi_checksum_method_queryeditor_use_database( ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_exportprogress_rows( + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -4948,7 +5220,7 @@ public static partial [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] public static extern #endif - ushort uniffi_tablepro_ffi_checksum_method_tablekeys_row_references( + ushort uniffi_tablepro_ffi_checksum_method_tablekeys_referencing_tables( ); #if NET8_0_OR_GREATER @@ -4959,7 +5231,7 @@ public static partial [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] public static extern #endif - ushort uniffi_tablepro_ffi_checksum_method_resultset_share_masking( + ushort uniffi_tablepro_ffi_checksum_method_tablekeys_row_references( ); #if NET8_0_OR_GREATER @@ -4970,7 +5242,7 @@ public static partial [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] public static extern #endif - ushort uniffi_tablepro_ffi_checksum_method_resultset_share_preview( + ushort uniffi_tablepro_ffi_checksum_method_resultset_share_masking( ); #if NET8_0_OR_GREATER @@ -4981,7 +5253,18 @@ public static partial [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] public static extern #endif - ushort uniffi_tablepro_ffi_checksum_method_resultset_share_summary( + ushort uniffi_tablepro_ffi_checksum_method_resultset_share_preview( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_resultset_share_summary( ); #if NET8_0_OR_GREATER @@ -5380,6 +5663,72 @@ public static extern ushort uniffi_tablepro_ffi_checksum_method_savedquerystore_snapshot( ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_databasesession_catalog_abilities( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_databasesession_database_choices( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_databasesession_plan_catalog_change( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_databasesession_protects_database( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_databasesession_protects_schema( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_databasesession_refresh_schemas( + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -5820,6 +6169,17 @@ public static extern ushort uniffi_tablepro_ffi_checksum_constructor_core_with_system_secrets( ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_constructor_exportprogress_new( + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -6104,6 +6464,30 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_canceltoken_is_cancelled` checksum `24287`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_catalogchange_destructive(); + if (checksum != 589) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_catalogchange_destructive` checksum `589`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_catalogchange_gate(); + if (checksum != 8992) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_catalogchange_gate` checksum `8992`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_catalogchange_run(); + if (checksum != 10012) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_catalogchange_run` checksum `10012`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_catalogchange_statements(); + if (checksum != 34813) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_catalogchange_statements` checksum `34813`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_core_open_account(); if (checksum != 36884) { @@ -6182,6 +6566,12 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_queryeditor_use_database` checksum `41238`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_exportprogress_rows(); + if (checksum != 43428) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_exportprogress_rows` checksum `43428`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_favouritestore_add_table(); if (checksum != 20993) { @@ -6452,6 +6842,12 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_tablekeys_follow_reference` checksum `56096`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_tablekeys_referencing_tables(); + if (checksum != 28654) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_tablekeys_referencing_tables` checksum `28654`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_tablekeys_row_references(); if (checksum != 17848) { @@ -6692,16 +7088,52 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_savedquerystore_snapshot` checksum `6061`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_catalog_abilities(); + if (checksum != 50555) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_catalog_abilities` checksum `50555`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_database_choices(); + if (checksum != 16359) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_database_choices` checksum `16359`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_plan_catalog_change(); + if (checksum != 9315) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_plan_catalog_change` checksum `9315`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_protects_database(); + if (checksum != 17181) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_protects_database` checksum `17181`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_protects_schema(); + if (checksum != 52310) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_protects_schema` checksum `52310`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_refresh_schemas(); + if (checksum != 26140) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_refresh_schemas` checksum `26140`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_export_rows(); - if (checksum != 61480) { - throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_export_rows` checksum `61480`, library returned `{checksum}`"); + if (checksum != 59377) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_export_rows` checksum `59377`, library returned `{checksum}`"); } } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_export_table(); - if (checksum != 50830) { - throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_export_table` checksum `50830`, library returned `{checksum}`"); + if (checksum != 3042) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_export_table` checksum `3042`, library returned `{checksum}`"); } } { @@ -6932,6 +7364,12 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_constructor_core_with_system_secrets` checksum `43052`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_constructor_exportprogress_new(); + if (checksum != 63262) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_constructor_exportprogress_new` checksum `63262`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_accountlistener_status_changed(); if (checksum != 24504) { @@ -7806,6 +8244,178 @@ public override void Write(CancelToken value, BigEndianStream stream) { +internal interface ICatalogChange { + bool Destructive(); + SaveGate Gate(); + /// + Task Run(bool @confirmed, CancelToken? @cancel); + string[] Statements(); +} +internal class CatalogChange : ICatalogChange, IDisposable { + protected ulong pointer; + private int _wasDestroyed = 0; + private long _callCounter = 1; + + public CatalogChange(ulong pointer) { + this.pointer = pointer; + } + + ~CatalogChange() { + Destroy(); + } + + protected void FreeRustArcPtr() { + _UniffiHelpers.RustCall((ref UniffiRustCallStatus status) => { + _UniFFILib.uniffi_tablepro_ffi_fn_free_catalogchange(this.pointer, ref status); + }); + } + + protected ulong CloneRustArcPtr() { + return _UniffiHelpers.RustCall((ref UniffiRustCallStatus status) => { + return _UniFFILib.uniffi_tablepro_ffi_fn_clone_catalogchange(this.pointer, ref status); + }); + } + + public void Destroy() + { + // Only allow a single call to this method. + if (Interlocked.CompareExchange(ref _wasDestroyed, 1, 0) == 0) + { + // This decrement always matches the initial count of 1 given at creation time. + if (Interlocked.Decrement(ref _callCounter) == 0) + { + FreeRustArcPtr(); + } + } + } + + public void Dispose() + { + Destroy(); + GC.SuppressFinalize(this); // Suppress finalization to avoid unnecessary GC overhead. + } + + private void IncrementCallCounter() + { + // Check and increment the call counter, to keep the object alive. + // This needs a compare-and-set retry loop in case of concurrent updates. + long count; + do + { + count = Interlocked.Read(ref _callCounter); + if (count == 0L) throw new System.ObjectDisposedException(String.Format("'{0}' object has already been destroyed", this.GetType().Name)); + if (count == long.MaxValue) throw new System.OverflowException(String.Format("'{0}' call counter would overflow", this.GetType().Name)); + + } while (Interlocked.CompareExchange(ref _callCounter, count + 1, count) != count); + } + + private void DecrementCallCounter() + { + // This decrement always matches the increment we performed above. + if (Interlocked.Decrement(ref _callCounter) == 0) { + FreeRustArcPtr(); + } + } + + internal void CallWithPointer(Action action) + { + IncrementCallCounter(); + try { + action(CloneRustArcPtr()); + } + finally { + DecrementCallCounter(); + } + } + + internal T CallWithPointer(Func func) + { + IncrementCallCounter(); + try { + return func(CloneRustArcPtr()); + } + finally { + DecrementCallCounter(); + } + } + + + public bool Destructive() { + return CallWithPointer(thisPtr => FfiConverterBoolean.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_catalogchange_destructive(thisPtr, ref _status) +))); + } + + + public SaveGate Gate() { + return CallWithPointer(thisPtr => FfiConverterTypeSaveGate.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_catalogchange_gate(thisPtr, ref _status) +))); + } + + + /// + public async Task Run(bool @confirmed, CancelToken? @cancel) { + return await _UniFFIAsync.UniffiRustCallAsync( + // Get rust future + CallWithPointer(thisPtr => { + return _UniFFILib.uniffi_tablepro_ffi_fn_method_catalogchange_run(thisPtr, FfiConverterBoolean.INSTANCE.Lower(@confirmed), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); + }), + // Poll + (ulong future, IntPtr continuation, ulong data) => _UniFFILib.ffi_tablepro_ffi_rust_future_poll_rust_buffer(future, continuation, data), + // Complete + (ulong future, ref UniffiRustCallStatus status) => { + return _UniFFILib.ffi_tablepro_ffi_rust_future_complete_rust_buffer(future, ref status); + }, + // Free + (ulong future) => _UniFFILib.ffi_tablepro_ffi_rust_future_free_rust_buffer(future), + // Lift + (result) => FfiConverterTypeCatalogChangeOutcome.INSTANCE.Lift(result), + // Error + FfiConverterTypeCoreError.INSTANCE + ); + } + + public string[] Statements() { + return CallWithPointer(thisPtr => FfiConverterSequenceString.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_catalogchange_statements(thisPtr, ref _status) +))); + } + + + + +} +class FfiConverterTypeCatalogChange: FfiConverter { + public static FfiConverterTypeCatalogChange INSTANCE = new FfiConverterTypeCatalogChange(); + + + public override ulong Lower(CatalogChange value) { + return value.CallWithPointer(thisPtr => thisPtr); + } + + public override CatalogChange Lift(ulong value) { + return new CatalogChange(value); + } + + public override CatalogChange Read(BigEndianStream stream) { + return Lift(stream.ReadULong()); + } + + public override int AllocationSize(CatalogChange value) { + return 8; + } + + public override void Write(CatalogChange value, BigEndianStream stream) { + stream.WriteULong(Lower(value)); + } +} + + + internal interface IConnectionLibrary { /// Task ApplyCloudPull(CloudLibraryPull @pull); @@ -8882,10 +9492,19 @@ public override void Write(CsvImport value, BigEndianStream stream) { internal interface IDatabaseSession { + CatalogAbilities CatalogAbilities(); + /// + Task DatabaseChoices(CancelToken? @cancel); /// - Task ExportRows(ResultSet @rows, uint @visibleColumns, ExportFormatTag @format, TableName? @table, string @path, CancelToken? @cancel); + CatalogChange PlanCatalogChange(CatalogOperation @operation); + bool ProtectsDatabase(string @name); + bool ProtectsSchema(string @name); /// - Task ExportTable(TableName @table, ColumnSort? @sort, TableFilterArg? @filter, ExportFormatTag @format, string @path, CancelToken? @cancel); + Task RefreshSchemas(CancelToken? @cancel); + /// + Task ExportRows(ResultSet @rows, uint @visibleColumns, ExportFormatTag @format, TableName? @table, string? @database, string @path, ExportProgress? @progress, CancelToken? @cancel); + /// + Task ExportTable(TableName @table, ColumnSort? @sort, TableFilterArg? @filter, ExportFormatTag @format, string @path, ExportProgress? @progress, CancelToken? @cancel); /// Task OpenCsvImport(TableName @table, CancelToken? @cancel); /// @@ -9024,12 +9643,89 @@ internal T CallWithPointer(Func func) } + public CatalogAbilities CatalogAbilities() { + return CallWithPointer(thisPtr => FfiConverterTypeCatalogAbilities.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_catalog_abilities(thisPtr, ref _status) +))); + } + + /// - public async Task ExportRows(ResultSet @rows, uint @visibleColumns, ExportFormatTag @format, TableName? @table, string @path, CancelToken? @cancel) { + public async Task DatabaseChoices(CancelToken? @cancel) { return await _UniFFIAsync.UniffiRustCallAsync( // Get rust future CallWithPointer(thisPtr => { - return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_export_rows(thisPtr, FfiConverterTypeResultSet.INSTANCE.Lower(@rows), FfiConverterUInt32.INSTANCE.Lower(@visibleColumns), FfiConverterTypeExportFormatTag.INSTANCE.Lower(@format), FfiConverterOptionalTypeTableName.INSTANCE.Lower(@table), FfiConverterString.INSTANCE.Lower(@path), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); + return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_database_choices(thisPtr, FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); + }), + // Poll + (ulong future, IntPtr continuation, ulong data) => _UniFFILib.ffi_tablepro_ffi_rust_future_poll_rust_buffer(future, continuation, data), + // Complete + (ulong future, ref UniffiRustCallStatus status) => { + return _UniFFILib.ffi_tablepro_ffi_rust_future_complete_rust_buffer(future, ref status); + }, + // Free + (ulong future) => _UniFFILib.ffi_tablepro_ffi_rust_future_free_rust_buffer(future), + // Lift + (result) => FfiConverterTypeDatabaseChoicesInfo.INSTANCE.Lift(result), + // Error + FfiConverterTypeCoreError.INSTANCE + ); + } + + /// + public CatalogChange PlanCatalogChange(CatalogOperation @operation) { + return CallWithPointer(thisPtr => FfiConverterTypeCatalogChange.INSTANCE.Lift( + _UniffiHelpers.RustCallWithError(FfiConverterTypeCoreError.INSTANCE, (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_plan_catalog_change(thisPtr, FfiConverterTypeCatalogOperation.INSTANCE.Lower(@operation), ref _status) +))); + } + + + public bool ProtectsDatabase(string @name) { + return CallWithPointer(thisPtr => FfiConverterBoolean.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_protects_database(thisPtr, FfiConverterString.INSTANCE.Lower(@name), ref _status) +))); + } + + + public bool ProtectsSchema(string @name) { + return CallWithPointer(thisPtr => FfiConverterBoolean.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_protects_schema(thisPtr, FfiConverterString.INSTANCE.Lower(@name), ref _status) +))); + } + + + /// + public async Task RefreshSchemas(CancelToken? @cancel) { + return await _UniFFIAsync.UniffiRustCallAsync( + // Get rust future + CallWithPointer(thisPtr => { + return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_refresh_schemas(thisPtr, FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); + }), + // Poll + (ulong future, IntPtr continuation, ulong data) => _UniFFILib.ffi_tablepro_ffi_rust_future_poll_rust_buffer(future, continuation, data), + // Complete + (ulong future, ref UniffiRustCallStatus status) => { + return _UniFFILib.ffi_tablepro_ffi_rust_future_complete_rust_buffer(future, ref status); + }, + // Free + (ulong future) => _UniFFILib.ffi_tablepro_ffi_rust_future_free_rust_buffer(future), + // Lift + (result) => FfiConverterSequenceTypeSchemaEntry.INSTANCE.Lift(result), + // Error + FfiConverterTypeCoreError.INSTANCE + ); + } + + /// + public async Task ExportRows(ResultSet @rows, uint @visibleColumns, ExportFormatTag @format, TableName? @table, string? @database, string @path, ExportProgress? @progress, CancelToken? @cancel) { + return await _UniFFIAsync.UniffiRustCallAsync( + // Get rust future + CallWithPointer(thisPtr => { + return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_export_rows(thisPtr, FfiConverterTypeResultSet.INSTANCE.Lower(@rows), FfiConverterUInt32.INSTANCE.Lower(@visibleColumns), FfiConverterTypeExportFormatTag.INSTANCE.Lower(@format), FfiConverterOptionalTypeTableName.INSTANCE.Lower(@table), FfiConverterOptionalString.INSTANCE.Lower(@database), FfiConverterString.INSTANCE.Lower(@path), FfiConverterOptionalTypeExportProgress.INSTANCE.Lower(@progress), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); }), // Poll (ulong future, IntPtr continuation, ulong data) => _UniFFILib.ffi_tablepro_ffi_rust_future_poll_u64(future, continuation, data), @@ -9047,11 +9743,11 @@ public async Task ExportRows(ResultSet @rows, uint @visibleColumns, Expor } /// - public async Task ExportTable(TableName @table, ColumnSort? @sort, TableFilterArg? @filter, ExportFormatTag @format, string @path, CancelToken? @cancel) { + public async Task ExportTable(TableName @table, ColumnSort? @sort, TableFilterArg? @filter, ExportFormatTag @format, string @path, ExportProgress? @progress, CancelToken? @cancel) { return await _UniFFIAsync.UniffiRustCallAsync( // Get rust future CallWithPointer(thisPtr => { - return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_export_table(thisPtr, FfiConverterTypeTableName.INSTANCE.Lower(@table), FfiConverterOptionalTypeColumnSort.INSTANCE.Lower(@sort), FfiConverterOptionalTypeTableFilterArg.INSTANCE.Lower(@filter), FfiConverterTypeExportFormatTag.INSTANCE.Lower(@format), FfiConverterString.INSTANCE.Lower(@path), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); + return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_export_table(thisPtr, FfiConverterTypeTableName.INSTANCE.Lower(@table), FfiConverterOptionalTypeColumnSort.INSTANCE.Lower(@sort), FfiConverterOptionalTypeTableFilterArg.INSTANCE.Lower(@filter), FfiConverterTypeExportFormatTag.INSTANCE.Lower(@format), FfiConverterString.INSTANCE.Lower(@path), FfiConverterOptionalTypeExportProgress.INSTANCE.Lower(@progress), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); }), // Poll (ulong future, IntPtr continuation, ulong data) => _UniFFILib.ffi_tablepro_ffi_rust_future_poll_u64(future, continuation, data), @@ -9499,75 +10195,210 @@ public async Task TableRowCount(TableName @table, TableFilterArg? @filter FfiConverterTypeCoreError.INSTANCE ); } - - /// - public async Task TableRowEstimate(TableName @table, CancelToken? @cancel) { - return await _UniFFIAsync.UniffiRustCallAsync( - // Get rust future - CallWithPointer(thisPtr => { - return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_table_row_estimate(thisPtr, FfiConverterTypeTableName.INSTANCE.Lower(@table), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); - }), - // Poll - (ulong future, IntPtr continuation, ulong data) => _UniFFILib.ffi_tablepro_ffi_rust_future_poll_rust_buffer(future, continuation, data), - // Complete - (ulong future, ref UniffiRustCallStatus status) => { - return _UniFFILib.ffi_tablepro_ffi_rust_future_complete_rust_buffer(future, ref status); - }, - // Free - (ulong future) => _UniFFILib.ffi_tablepro_ffi_rust_future_free_rust_buffer(future), - // Lift - (result) => FfiConverterTypeRowEstimate.INSTANCE.Lift(result), - // Error - FfiConverterTypeCoreError.INSTANCE - ); + + /// + public async Task TableRowEstimate(TableName @table, CancelToken? @cancel) { + return await _UniFFIAsync.UniffiRustCallAsync( + // Get rust future + CallWithPointer(thisPtr => { + return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_table_row_estimate(thisPtr, FfiConverterTypeTableName.INSTANCE.Lower(@table), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); + }), + // Poll + (ulong future, IntPtr continuation, ulong data) => _UniFFILib.ffi_tablepro_ffi_rust_future_poll_rust_buffer(future, continuation, data), + // Complete + (ulong future, ref UniffiRustCallStatus status) => { + return _UniFFILib.ffi_tablepro_ffi_rust_future_complete_rust_buffer(future, ref status); + }, + // Free + (ulong future) => _UniFFILib.ffi_tablepro_ffi_rust_future_free_rust_buffer(future), + // Lift + (result) => FfiConverterTypeRowEstimate.INSTANCE.Lift(result), + // Error + FfiConverterTypeCoreError.INSTANCE + ); + } + + /// + public async Task TableStructure(TableName @table, ObjectKindTag @kind, CancelToken? @cancel) { + return await _UniFFIAsync.UniffiRustCallAsync( + // Get rust future + CallWithPointer(thisPtr => { + return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_table_structure(thisPtr, FfiConverterTypeTableName.INSTANCE.Lower(@table), FfiConverterTypeObjectKindTag.INSTANCE.Lower(@kind), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); + }), + // Poll + (ulong future, IntPtr continuation, ulong data) => _UniFFILib.ffi_tablepro_ffi_rust_future_poll_rust_buffer(future, continuation, data), + // Complete + (ulong future, ref UniffiRustCallStatus status) => { + return _UniFFILib.ffi_tablepro_ffi_rust_future_complete_rust_buffer(future, ref status); + }, + // Free + (ulong future) => _UniFFILib.ffi_tablepro_ffi_rust_future_free_rust_buffer(future), + // Lift + (result) => FfiConverterTypeTableStructure.INSTANCE.Lift(result), + // Error + FfiConverterTypeCoreError.INSTANCE + ); + } + + + +} +class FfiConverterTypeDatabaseSession: FfiConverter { + public static FfiConverterTypeDatabaseSession INSTANCE = new FfiConverterTypeDatabaseSession(); + + + public override ulong Lower(DatabaseSession value) { + return value.CallWithPointer(thisPtr => thisPtr); + } + + public override DatabaseSession Lift(ulong value) { + return new DatabaseSession(value); + } + + public override DatabaseSession Read(BigEndianStream stream) { + return Lift(stream.ReadULong()); + } + + public override int AllocationSize(DatabaseSession value) { + return 8; + } + + public override void Write(DatabaseSession value, BigEndianStream stream) { + stream.WriteULong(Lower(value)); + } +} + + + +internal interface IExportProgress { + ulong Rows(); +} +internal class ExportProgress : IExportProgress, IDisposable { + protected ulong pointer; + private int _wasDestroyed = 0; + private long _callCounter = 1; + + public ExportProgress(ulong pointer) { + this.pointer = pointer; + } + + ~ExportProgress() { + Destroy(); + } + public ExportProgress() : + this( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_constructor_exportprogress_new( ref _status) +)) {} + + protected void FreeRustArcPtr() { + _UniffiHelpers.RustCall((ref UniffiRustCallStatus status) => { + _UniFFILib.uniffi_tablepro_ffi_fn_free_exportprogress(this.pointer, ref status); + }); + } + + protected ulong CloneRustArcPtr() { + return _UniffiHelpers.RustCall((ref UniffiRustCallStatus status) => { + return _UniFFILib.uniffi_tablepro_ffi_fn_clone_exportprogress(this.pointer, ref status); + }); + } + + public void Destroy() + { + // Only allow a single call to this method. + if (Interlocked.CompareExchange(ref _wasDestroyed, 1, 0) == 0) + { + // This decrement always matches the initial count of 1 given at creation time. + if (Interlocked.Decrement(ref _callCounter) == 0) + { + FreeRustArcPtr(); + } + } + } + + public void Dispose() + { + Destroy(); + GC.SuppressFinalize(this); // Suppress finalization to avoid unnecessary GC overhead. + } + + private void IncrementCallCounter() + { + // Check and increment the call counter, to keep the object alive. + // This needs a compare-and-set retry loop in case of concurrent updates. + long count; + do + { + count = Interlocked.Read(ref _callCounter); + if (count == 0L) throw new System.ObjectDisposedException(String.Format("'{0}' object has already been destroyed", this.GetType().Name)); + if (count == long.MaxValue) throw new System.OverflowException(String.Format("'{0}' call counter would overflow", this.GetType().Name)); + + } while (Interlocked.CompareExchange(ref _callCounter, count + 1, count) != count); + } + + private void DecrementCallCounter() + { + // This decrement always matches the increment we performed above. + if (Interlocked.Decrement(ref _callCounter) == 0) { + FreeRustArcPtr(); + } + } + + internal void CallWithPointer(Action action) + { + IncrementCallCounter(); + try { + action(CloneRustArcPtr()); + } + finally { + DecrementCallCounter(); + } + } + + internal T CallWithPointer(Func func) + { + IncrementCallCounter(); + try { + return func(CloneRustArcPtr()); + } + finally { + DecrementCallCounter(); + } } + - /// - public async Task TableStructure(TableName @table, ObjectKindTag @kind, CancelToken? @cancel) { - return await _UniFFIAsync.UniffiRustCallAsync( - // Get rust future - CallWithPointer(thisPtr => { - return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_table_structure(thisPtr, FfiConverterTypeTableName.INSTANCE.Lower(@table), FfiConverterTypeObjectKindTag.INSTANCE.Lower(@kind), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); - }), - // Poll - (ulong future, IntPtr continuation, ulong data) => _UniFFILib.ffi_tablepro_ffi_rust_future_poll_rust_buffer(future, continuation, data), - // Complete - (ulong future, ref UniffiRustCallStatus status) => { - return _UniFFILib.ffi_tablepro_ffi_rust_future_complete_rust_buffer(future, ref status); - }, - // Free - (ulong future) => _UniFFILib.ffi_tablepro_ffi_rust_future_free_rust_buffer(future), - // Lift - (result) => FfiConverterTypeTableStructure.INSTANCE.Lift(result), - // Error - FfiConverterTypeCoreError.INSTANCE - ); + public ulong Rows() { + return CallWithPointer(thisPtr => FfiConverterUInt64.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_exportprogress_rows(thisPtr, ref _status) +))); } + } -class FfiConverterTypeDatabaseSession: FfiConverter { - public static FfiConverterTypeDatabaseSession INSTANCE = new FfiConverterTypeDatabaseSession(); +class FfiConverterTypeExportProgress: FfiConverter { + public static FfiConverterTypeExportProgress INSTANCE = new FfiConverterTypeExportProgress(); - public override ulong Lower(DatabaseSession value) { + public override ulong Lower(ExportProgress value) { return value.CallWithPointer(thisPtr => thisPtr); } - public override DatabaseSession Lift(ulong value) { - return new DatabaseSession(value); + public override ExportProgress Lift(ulong value) { + return new ExportProgress(value); } - public override DatabaseSession Read(BigEndianStream stream) { + public override ExportProgress Read(BigEndianStream stream) { return Lift(stream.ReadULong()); } - public override int AllocationSize(DatabaseSession value) { + public override int AllocationSize(ExportProgress value) { return 8; } - public override void Write(DatabaseSession value, BigEndianStream stream) { + public override void Write(ExportProgress value, BigEndianStream stream) { stream.WriteULong(Lower(value)); } } @@ -11853,6 +12684,7 @@ public override void Write(TableEditor value, BigEndianStream stream) { internal interface ITableKeys { /// FollowedReference FollowReference(ResultSet @rows, ulong @row, bool @referencing, uint @key); + TableName[] ReferencingTables(); /// RowReferences RowReferences(ResultSet @rows, ulong @row, string? @column); } @@ -11954,6 +12786,14 @@ public FollowedReference FollowReference(ResultSet @rows, ulong @row, bool @refe } + public TableName[] ReferencingTables() { + return CallWithPointer(thisPtr => FfiConverterSequenceTypeTableName.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_tablekeys_referencing_tables(thisPtr, ref _status) +))); + } + + /// public RowReferences RowReferences(ResultSet @rows, ulong @row, string? @column) { return CallWithPointer(thisPtr => FfiConverterTypeRowReferences.INSTANCE.Lift( @@ -12279,6 +13119,92 @@ public override void Write(AccountStatusInfo value, BigEndianStream stream) { +internal record CatalogAbilities ( + bool TruncateTable, + bool TruncateCascade, + bool TruncateRestartIdentity, + bool TruncateRefusedWhenReferenced, + bool DropTable, + bool DropTableCascade, + bool CreateSchema, + bool DropSchema, + bool DropSchemaCascade, + bool SchemaOwner, + bool CreateDatabase, + bool DropDatabase, + bool DropDatabaseForce, + DatabaseFieldsInfo DatabaseFields, + bool Keyspaces, + bool OnCluster +) { +} + +class FfiConverterTypeCatalogAbilities: FfiConverterRustBuffer { + public static FfiConverterTypeCatalogAbilities INSTANCE = new FfiConverterTypeCatalogAbilities(); + + public override CatalogAbilities Read(BigEndianStream stream) { + return new CatalogAbilities( + TruncateTable: FfiConverterBoolean.INSTANCE.Read(stream), + TruncateCascade: FfiConverterBoolean.INSTANCE.Read(stream), + TruncateRestartIdentity: FfiConverterBoolean.INSTANCE.Read(stream), + TruncateRefusedWhenReferenced: FfiConverterBoolean.INSTANCE.Read(stream), + DropTable: FfiConverterBoolean.INSTANCE.Read(stream), + DropTableCascade: FfiConverterBoolean.INSTANCE.Read(stream), + CreateSchema: FfiConverterBoolean.INSTANCE.Read(stream), + DropSchema: FfiConverterBoolean.INSTANCE.Read(stream), + DropSchemaCascade: FfiConverterBoolean.INSTANCE.Read(stream), + SchemaOwner: FfiConverterBoolean.INSTANCE.Read(stream), + CreateDatabase: FfiConverterBoolean.INSTANCE.Read(stream), + DropDatabase: FfiConverterBoolean.INSTANCE.Read(stream), + DropDatabaseForce: FfiConverterBoolean.INSTANCE.Read(stream), + DatabaseFields: FfiConverterTypeDatabaseFieldsInfo.INSTANCE.Read(stream), + Keyspaces: FfiConverterBoolean.INSTANCE.Read(stream), + OnCluster: FfiConverterBoolean.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(CatalogAbilities value) { + return 0 + + FfiConverterBoolean.INSTANCE.AllocationSize(value.TruncateTable) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.TruncateCascade) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.TruncateRestartIdentity) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.TruncateRefusedWhenReferenced) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.DropTable) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.DropTableCascade) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.CreateSchema) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.DropSchema) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.DropSchemaCascade) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.SchemaOwner) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.CreateDatabase) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.DropDatabase) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.DropDatabaseForce) + + FfiConverterTypeDatabaseFieldsInfo.INSTANCE.AllocationSize(value.DatabaseFields) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.Keyspaces) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.OnCluster); + } + + public override void Write(CatalogAbilities value, BigEndianStream stream) { + FfiConverterBoolean.INSTANCE.Write(value.TruncateTable, stream); + FfiConverterBoolean.INSTANCE.Write(value.TruncateCascade, stream); + FfiConverterBoolean.INSTANCE.Write(value.TruncateRestartIdentity, stream); + FfiConverterBoolean.INSTANCE.Write(value.TruncateRefusedWhenReferenced, stream); + FfiConverterBoolean.INSTANCE.Write(value.DropTable, stream); + FfiConverterBoolean.INSTANCE.Write(value.DropTableCascade, stream); + FfiConverterBoolean.INSTANCE.Write(value.CreateSchema, stream); + FfiConverterBoolean.INSTANCE.Write(value.DropSchema, stream); + FfiConverterBoolean.INSTANCE.Write(value.DropSchemaCascade, stream); + FfiConverterBoolean.INSTANCE.Write(value.SchemaOwner, stream); + FfiConverterBoolean.INSTANCE.Write(value.CreateDatabase, stream); + FfiConverterBoolean.INSTANCE.Write(value.DropDatabase, stream); + FfiConverterBoolean.INSTANCE.Write(value.DropDatabaseForce, stream); + FfiConverterTypeDatabaseFieldsInfo.INSTANCE.Write(value.DatabaseFields, stream); + FfiConverterBoolean.INSTANCE.Write(value.Keyspaces, stream); + FfiConverterBoolean.INSTANCE.Write(value.OnCluster, stream); + } +} + + + internal record CellBytes ( byte[] Bytes, ulong Length @@ -12369,6 +13295,40 @@ public override void Write(CellTextHead value, BigEndianStream stream) { +internal record CharacterSetInfo ( + string Name, + string DefaultCollation, + string[] Collations +) { +} + +class FfiConverterTypeCharacterSetInfo: FfiConverterRustBuffer { + public static FfiConverterTypeCharacterSetInfo INSTANCE = new FfiConverterTypeCharacterSetInfo(); + + public override CharacterSetInfo Read(BigEndianStream stream) { + return new CharacterSetInfo( + Name: FfiConverterString.INSTANCE.Read(stream), + DefaultCollation: FfiConverterString.INSTANCE.Read(stream), + Collations: FfiConverterSequenceString.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(CharacterSetInfo value) { + return 0 + + FfiConverterString.INSTANCE.AllocationSize(value.Name) + + FfiConverterString.INSTANCE.AllocationSize(value.DefaultCollation) + + FfiConverterSequenceString.INSTANCE.AllocationSize(value.Collations); + } + + public override void Write(CharacterSetInfo value, BigEndianStream stream) { + FfiConverterString.INSTANCE.Write(value.Name, stream); + FfiConverterString.INSTANCE.Write(value.DefaultCollation, stream); + FfiConverterSequenceString.INSTANCE.Write(value.Collations, stream); + } +} + + + internal record CloudConnectionFields ( string ConnectionId, string? Name, @@ -12887,6 +13847,238 @@ public override void Write(CsvRowFault value, BigEndianStream stream) { +internal record DataCenterReplicas ( + string DataCenter, + uint Replicas +) { +} + +class FfiConverterTypeDataCenterReplicas: FfiConverterRustBuffer { + public static FfiConverterTypeDataCenterReplicas INSTANCE = new FfiConverterTypeDataCenterReplicas(); + + public override DataCenterReplicas Read(BigEndianStream stream) { + return new DataCenterReplicas( + DataCenter: FfiConverterString.INSTANCE.Read(stream), + Replicas: FfiConverterUInt32.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(DataCenterReplicas value) { + return 0 + + FfiConverterString.INSTANCE.AllocationSize(value.DataCenter) + + FfiConverterUInt32.INSTANCE.AllocationSize(value.Replicas); + } + + public override void Write(DataCenterReplicas value, BigEndianStream stream) { + FfiConverterString.INSTANCE.Write(value.DataCenter, stream); + FfiConverterUInt32.INSTANCE.Write(value.Replicas, stream); + } +} + + + +internal record DatabaseChoicesInfo ( + string[] Owners, + string[] Templates, + string[] Encodings, + string[] Locales, + CharacterSetInfo[] CharacterSets, + string[] Collations, + string[] Clusters, + string[] DataCenters, + string? DefaultOwner, + string? DefaultEncoding, + string? DefaultLocale, + string? DefaultCharacterSet, + string? DefaultCollation +) { +} + +class FfiConverterTypeDatabaseChoicesInfo: FfiConverterRustBuffer { + public static FfiConverterTypeDatabaseChoicesInfo INSTANCE = new FfiConverterTypeDatabaseChoicesInfo(); + + public override DatabaseChoicesInfo Read(BigEndianStream stream) { + return new DatabaseChoicesInfo( + Owners: FfiConverterSequenceString.INSTANCE.Read(stream), + Templates: FfiConverterSequenceString.INSTANCE.Read(stream), + Encodings: FfiConverterSequenceString.INSTANCE.Read(stream), + Locales: FfiConverterSequenceString.INSTANCE.Read(stream), + CharacterSets: FfiConverterSequenceTypeCharacterSetInfo.INSTANCE.Read(stream), + Collations: FfiConverterSequenceString.INSTANCE.Read(stream), + Clusters: FfiConverterSequenceString.INSTANCE.Read(stream), + DataCenters: FfiConverterSequenceString.INSTANCE.Read(stream), + DefaultOwner: FfiConverterOptionalString.INSTANCE.Read(stream), + DefaultEncoding: FfiConverterOptionalString.INSTANCE.Read(stream), + DefaultLocale: FfiConverterOptionalString.INSTANCE.Read(stream), + DefaultCharacterSet: FfiConverterOptionalString.INSTANCE.Read(stream), + DefaultCollation: FfiConverterOptionalString.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(DatabaseChoicesInfo value) { + return 0 + + FfiConverterSequenceString.INSTANCE.AllocationSize(value.Owners) + + FfiConverterSequenceString.INSTANCE.AllocationSize(value.Templates) + + FfiConverterSequenceString.INSTANCE.AllocationSize(value.Encodings) + + FfiConverterSequenceString.INSTANCE.AllocationSize(value.Locales) + + FfiConverterSequenceTypeCharacterSetInfo.INSTANCE.AllocationSize(value.CharacterSets) + + FfiConverterSequenceString.INSTANCE.AllocationSize(value.Collations) + + FfiConverterSequenceString.INSTANCE.AllocationSize(value.Clusters) + + FfiConverterSequenceString.INSTANCE.AllocationSize(value.DataCenters) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.DefaultOwner) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.DefaultEncoding) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.DefaultLocale) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.DefaultCharacterSet) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.DefaultCollation); + } + + public override void Write(DatabaseChoicesInfo value, BigEndianStream stream) { + FfiConverterSequenceString.INSTANCE.Write(value.Owners, stream); + FfiConverterSequenceString.INSTANCE.Write(value.Templates, stream); + FfiConverterSequenceString.INSTANCE.Write(value.Encodings, stream); + FfiConverterSequenceString.INSTANCE.Write(value.Locales, stream); + FfiConverterSequenceTypeCharacterSetInfo.INSTANCE.Write(value.CharacterSets, stream); + FfiConverterSequenceString.INSTANCE.Write(value.Collations, stream); + FfiConverterSequenceString.INSTANCE.Write(value.Clusters, stream); + FfiConverterSequenceString.INSTANCE.Write(value.DataCenters, stream); + FfiConverterOptionalString.INSTANCE.Write(value.DefaultOwner, stream); + FfiConverterOptionalString.INSTANCE.Write(value.DefaultEncoding, stream); + FfiConverterOptionalString.INSTANCE.Write(value.DefaultLocale, stream); + FfiConverterOptionalString.INSTANCE.Write(value.DefaultCharacterSet, stream); + FfiConverterOptionalString.INSTANCE.Write(value.DefaultCollation, stream); + } +} + + + +internal record DatabaseDraftArg ( + string Name, + string? Owner, + string? Template, + string? Encoding, + string? Locale, + string? CharacterSet, + string? Collation, + string? Cluster, + ReplicationArg? Replication, + bool DurableWrites, + bool? Tablets, + string? FirstCollection +) { +} + +class FfiConverterTypeDatabaseDraftArg: FfiConverterRustBuffer { + public static FfiConverterTypeDatabaseDraftArg INSTANCE = new FfiConverterTypeDatabaseDraftArg(); + + public override DatabaseDraftArg Read(BigEndianStream stream) { + return new DatabaseDraftArg( + Name: FfiConverterString.INSTANCE.Read(stream), + Owner: FfiConverterOptionalString.INSTANCE.Read(stream), + Template: FfiConverterOptionalString.INSTANCE.Read(stream), + Encoding: FfiConverterOptionalString.INSTANCE.Read(stream), + Locale: FfiConverterOptionalString.INSTANCE.Read(stream), + CharacterSet: FfiConverterOptionalString.INSTANCE.Read(stream), + Collation: FfiConverterOptionalString.INSTANCE.Read(stream), + Cluster: FfiConverterOptionalString.INSTANCE.Read(stream), + Replication: FfiConverterOptionalTypeReplicationArg.INSTANCE.Read(stream), + DurableWrites: FfiConverterBoolean.INSTANCE.Read(stream), + Tablets: FfiConverterOptionalBoolean.INSTANCE.Read(stream), + FirstCollection: FfiConverterOptionalString.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(DatabaseDraftArg value) { + return 0 + + FfiConverterString.INSTANCE.AllocationSize(value.Name) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.Owner) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.Template) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.Encoding) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.Locale) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.CharacterSet) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.Collation) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.Cluster) + + FfiConverterOptionalTypeReplicationArg.INSTANCE.AllocationSize(value.Replication) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.DurableWrites) + + FfiConverterOptionalBoolean.INSTANCE.AllocationSize(value.Tablets) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.FirstCollection); + } + + public override void Write(DatabaseDraftArg value, BigEndianStream stream) { + FfiConverterString.INSTANCE.Write(value.Name, stream); + FfiConverterOptionalString.INSTANCE.Write(value.Owner, stream); + FfiConverterOptionalString.INSTANCE.Write(value.Template, stream); + FfiConverterOptionalString.INSTANCE.Write(value.Encoding, stream); + FfiConverterOptionalString.INSTANCE.Write(value.Locale, stream); + FfiConverterOptionalString.INSTANCE.Write(value.CharacterSet, stream); + FfiConverterOptionalString.INSTANCE.Write(value.Collation, stream); + FfiConverterOptionalString.INSTANCE.Write(value.Cluster, stream); + FfiConverterOptionalTypeReplicationArg.INSTANCE.Write(value.Replication, stream); + FfiConverterBoolean.INSTANCE.Write(value.DurableWrites, stream); + FfiConverterOptionalBoolean.INSTANCE.Write(value.Tablets, stream); + FfiConverterOptionalString.INSTANCE.Write(value.FirstCollection, stream); + } +} + + + +internal record DatabaseFieldsInfo ( + bool Owner, + bool Template, + bool Encoding, + bool Locale, + bool CharacterSet, + bool Collation, + bool Replication, + bool Tablets, + bool FirstCollection +) { +} + +class FfiConverterTypeDatabaseFieldsInfo: FfiConverterRustBuffer { + public static FfiConverterTypeDatabaseFieldsInfo INSTANCE = new FfiConverterTypeDatabaseFieldsInfo(); + + public override DatabaseFieldsInfo Read(BigEndianStream stream) { + return new DatabaseFieldsInfo( + Owner: FfiConverterBoolean.INSTANCE.Read(stream), + Template: FfiConverterBoolean.INSTANCE.Read(stream), + Encoding: FfiConverterBoolean.INSTANCE.Read(stream), + Locale: FfiConverterBoolean.INSTANCE.Read(stream), + CharacterSet: FfiConverterBoolean.INSTANCE.Read(stream), + Collation: FfiConverterBoolean.INSTANCE.Read(stream), + Replication: FfiConverterBoolean.INSTANCE.Read(stream), + Tablets: FfiConverterBoolean.INSTANCE.Read(stream), + FirstCollection: FfiConverterBoolean.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(DatabaseFieldsInfo value) { + return 0 + + FfiConverterBoolean.INSTANCE.AllocationSize(value.Owner) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.Template) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.Encoding) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.Locale) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.CharacterSet) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.Collation) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.Replication) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.Tablets) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.FirstCollection); + } + + public override void Write(DatabaseFieldsInfo value, BigEndianStream stream) { + FfiConverterBoolean.INSTANCE.Write(value.Owner, stream); + FfiConverterBoolean.INSTANCE.Write(value.Template, stream); + FfiConverterBoolean.INSTANCE.Write(value.Encoding, stream); + FfiConverterBoolean.INSTANCE.Write(value.Locale, stream); + FfiConverterBoolean.INSTANCE.Write(value.CharacterSet, stream); + FfiConverterBoolean.INSTANCE.Write(value.Collation, stream); + FfiConverterBoolean.INSTANCE.Write(value.Replication, stream); + FfiConverterBoolean.INSTANCE.Write(value.Tablets, stream); + FfiConverterBoolean.INSTANCE.Write(value.FirstCollection, stream); + } +} + + + internal record DeviceInfo ( string Id, string Platform, @@ -15632,16 +16824,255 @@ public override AccountStateTag Read(BigEndianStream stream) { } } - public override int AllocationSize(AccountStateTag value) { - return 4; + public override int AllocationSize(AccountStateTag value) { + return 4; + } + + public override void Write(AccountStateTag value, BigEndianStream stream) { + switch (value) { + case AccountStateTag.SignedOut: stream.WriteInt(1); break; + case AccountStateTag.SignedIn: stream.WriteInt(2); break; + case AccountStateTag.Offline: stream.WriteInt(3); break; + default: throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeAccountStateTag.Write()", value)); + } + } +} + + + + + + + +internal record CatalogChangeOutcome { + + public record Done: CatalogChangeOutcome {} + + + public record Failed ( + BoxedErrorInfo Error + ) : CatalogChangeOutcome {} + + public record Unknown ( + BoxedErrorInfo Error + ) : CatalogChangeOutcome {} + + + +} + +class FfiConverterTypeCatalogChangeOutcome : FfiConverterRustBuffer{ + public static FfiConverterRustBuffer INSTANCE = new FfiConverterTypeCatalogChangeOutcome(); + + public override CatalogChangeOutcome Read(BigEndianStream stream) { + var value = stream.ReadInt(); + switch (value) { + case 1: + return new CatalogChangeOutcome.Done( + ); + case 2: + return new CatalogChangeOutcome.Failed( + FfiConverterTypeBoxedErrorInfo.INSTANCE.Read(stream) + ); + case 3: + return new CatalogChangeOutcome.Unknown( + FfiConverterTypeBoxedErrorInfo.INSTANCE.Read(stream) + ); + default: + throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeCatalogChangeOutcome.Read()", value)); + } + } + + public override int AllocationSize(CatalogChangeOutcome value) { + switch (value) { + case CatalogChangeOutcome.Done variant_value: + return 4; + case CatalogChangeOutcome.Failed variant_value: + return 4 + + FfiConverterTypeBoxedErrorInfo.INSTANCE.AllocationSize(variant_value.Error); + case CatalogChangeOutcome.Unknown variant_value: + return 4 + + FfiConverterTypeBoxedErrorInfo.INSTANCE.AllocationSize(variant_value.Error); + default: + throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeCatalogChangeOutcome.AllocationSize()", value)); + } + } + + public override void Write(CatalogChangeOutcome value, BigEndianStream stream) { + switch (value) { + case CatalogChangeOutcome.Done variant_value: + stream.WriteInt(1); + break; + case CatalogChangeOutcome.Failed variant_value: + stream.WriteInt(2); + FfiConverterTypeBoxedErrorInfo.INSTANCE.Write(variant_value.Error, stream); + break; + case CatalogChangeOutcome.Unknown variant_value: + stream.WriteInt(3); + FfiConverterTypeBoxedErrorInfo.INSTANCE.Write(variant_value.Error, stream); + break; + default: + throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeCatalogChangeOutcome.Write()", value)); + } + } +} + + + + + + + +internal record CatalogOperation { + + public record TruncateTable ( + TableName Table, + bool Cascade, + bool RestartIdentity, + string? Cluster + ) : CatalogOperation {} + + public record DropTable ( + TableName Table, + bool Cascade + ) : CatalogOperation {} + + public record CreateSchema ( + string Name, + string? Owner + ) : CatalogOperation {} + + public record DropSchema ( + string Name, + bool Cascade + ) : CatalogOperation {} + + public record CreateDatabase ( + DatabaseDraftArg Draft + ) : CatalogOperation {} + + public record DropDatabase ( + string Name, + bool Force, + string? Cluster + ) : CatalogOperation {} + + + +} + +class FfiConverterTypeCatalogOperation : FfiConverterRustBuffer{ + public static FfiConverterRustBuffer INSTANCE = new FfiConverterTypeCatalogOperation(); + + public override CatalogOperation Read(BigEndianStream stream) { + var value = stream.ReadInt(); + switch (value) { + case 1: + return new CatalogOperation.TruncateTable( + FfiConverterTypeTableName.INSTANCE.Read(stream), + FfiConverterBoolean.INSTANCE.Read(stream), + FfiConverterBoolean.INSTANCE.Read(stream), + FfiConverterOptionalString.INSTANCE.Read(stream) + ); + case 2: + return new CatalogOperation.DropTable( + FfiConverterTypeTableName.INSTANCE.Read(stream), + FfiConverterBoolean.INSTANCE.Read(stream) + ); + case 3: + return new CatalogOperation.CreateSchema( + FfiConverterString.INSTANCE.Read(stream), + FfiConverterOptionalString.INSTANCE.Read(stream) + ); + case 4: + return new CatalogOperation.DropSchema( + FfiConverterString.INSTANCE.Read(stream), + FfiConverterBoolean.INSTANCE.Read(stream) + ); + case 5: + return new CatalogOperation.CreateDatabase( + FfiConverterTypeDatabaseDraftArg.INSTANCE.Read(stream) + ); + case 6: + return new CatalogOperation.DropDatabase( + FfiConverterString.INSTANCE.Read(stream), + FfiConverterBoolean.INSTANCE.Read(stream), + FfiConverterOptionalString.INSTANCE.Read(stream) + ); + default: + throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeCatalogOperation.Read()", value)); + } + } + + public override int AllocationSize(CatalogOperation value) { + switch (value) { + case CatalogOperation.TruncateTable variant_value: + return 4 + + FfiConverterTypeTableName.INSTANCE.AllocationSize(variant_value.Table) + + FfiConverterBoolean.INSTANCE.AllocationSize(variant_value.Cascade) + + FfiConverterBoolean.INSTANCE.AllocationSize(variant_value.RestartIdentity) + + FfiConverterOptionalString.INSTANCE.AllocationSize(variant_value.Cluster); + case CatalogOperation.DropTable variant_value: + return 4 + + FfiConverterTypeTableName.INSTANCE.AllocationSize(variant_value.Table) + + FfiConverterBoolean.INSTANCE.AllocationSize(variant_value.Cascade); + case CatalogOperation.CreateSchema variant_value: + return 4 + + FfiConverterString.INSTANCE.AllocationSize(variant_value.Name) + + FfiConverterOptionalString.INSTANCE.AllocationSize(variant_value.Owner); + case CatalogOperation.DropSchema variant_value: + return 4 + + FfiConverterString.INSTANCE.AllocationSize(variant_value.Name) + + FfiConverterBoolean.INSTANCE.AllocationSize(variant_value.Cascade); + case CatalogOperation.CreateDatabase variant_value: + return 4 + + FfiConverterTypeDatabaseDraftArg.INSTANCE.AllocationSize(variant_value.Draft); + case CatalogOperation.DropDatabase variant_value: + return 4 + + FfiConverterString.INSTANCE.AllocationSize(variant_value.Name) + + FfiConverterBoolean.INSTANCE.AllocationSize(variant_value.Force) + + FfiConverterOptionalString.INSTANCE.AllocationSize(variant_value.Cluster); + default: + throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeCatalogOperation.AllocationSize()", value)); + } } - public override void Write(AccountStateTag value, BigEndianStream stream) { + public override void Write(CatalogOperation value, BigEndianStream stream) { switch (value) { - case AccountStateTag.SignedOut: stream.WriteInt(1); break; - case AccountStateTag.SignedIn: stream.WriteInt(2); break; - case AccountStateTag.Offline: stream.WriteInt(3); break; - default: throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeAccountStateTag.Write()", value)); + case CatalogOperation.TruncateTable variant_value: + stream.WriteInt(1); + FfiConverterTypeTableName.INSTANCE.Write(variant_value.Table, stream); + FfiConverterBoolean.INSTANCE.Write(variant_value.Cascade, stream); + FfiConverterBoolean.INSTANCE.Write(variant_value.RestartIdentity, stream); + FfiConverterOptionalString.INSTANCE.Write(variant_value.Cluster, stream); + break; + case CatalogOperation.DropTable variant_value: + stream.WriteInt(2); + FfiConverterTypeTableName.INSTANCE.Write(variant_value.Table, stream); + FfiConverterBoolean.INSTANCE.Write(variant_value.Cascade, stream); + break; + case CatalogOperation.CreateSchema variant_value: + stream.WriteInt(3); + FfiConverterString.INSTANCE.Write(variant_value.Name, stream); + FfiConverterOptionalString.INSTANCE.Write(variant_value.Owner, stream); + break; + case CatalogOperation.DropSchema variant_value: + stream.WriteInt(4); + FfiConverterString.INSTANCE.Write(variant_value.Name, stream); + FfiConverterBoolean.INSTANCE.Write(variant_value.Cascade, stream); + break; + case CatalogOperation.CreateDatabase variant_value: + stream.WriteInt(5); + FfiConverterTypeDatabaseDraftArg.INSTANCE.Write(variant_value.Draft, stream); + break; + case CatalogOperation.DropDatabase variant_value: + stream.WriteInt(6); + FfiConverterString.INSTANCE.Write(variant_value.Name, stream); + FfiConverterBoolean.INSTANCE.Write(variant_value.Force, stream); + FfiConverterOptionalString.INSTANCE.Write(variant_value.Cluster, stream); + break; + default: + throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeCatalogOperation.Write()", value)); } } } @@ -17998,6 +19429,74 @@ public override void Write(ReferenceRefusal value, BigEndianStream stream) { +internal record ReplicationArg { + + public record Simple ( + uint Factor + ) : ReplicationArg {} + + public record NetworkTopology ( + DataCenterReplicas[] Factors + ) : ReplicationArg {} + + + +} + +class FfiConverterTypeReplicationArg : FfiConverterRustBuffer{ + public static FfiConverterRustBuffer INSTANCE = new FfiConverterTypeReplicationArg(); + + public override ReplicationArg Read(BigEndianStream stream) { + var value = stream.ReadInt(); + switch (value) { + case 1: + return new ReplicationArg.Simple( + FfiConverterUInt32.INSTANCE.Read(stream) + ); + case 2: + return new ReplicationArg.NetworkTopology( + FfiConverterSequenceTypeDataCenterReplicas.INSTANCE.Read(stream) + ); + default: + throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeReplicationArg.Read()", value)); + } + } + + public override int AllocationSize(ReplicationArg value) { + switch (value) { + case ReplicationArg.Simple variant_value: + return 4 + + FfiConverterUInt32.INSTANCE.AllocationSize(variant_value.Factor); + case ReplicationArg.NetworkTopology variant_value: + return 4 + + FfiConverterSequenceTypeDataCenterReplicas.INSTANCE.AllocationSize(variant_value.Factors); + default: + throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeReplicationArg.AllocationSize()", value)); + } + } + + public override void Write(ReplicationArg value, BigEndianStream stream) { + switch (value) { + case ReplicationArg.Simple variant_value: + stream.WriteInt(1); + FfiConverterUInt32.INSTANCE.Write(variant_value.Factor, stream); + break; + case ReplicationArg.NetworkTopology variant_value: + stream.WriteInt(2); + FfiConverterSequenceTypeDataCenterReplicas.INSTANCE.Write(variant_value.Factors, stream); + break; + default: + throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeReplicationArg.Write()", value)); + } + } +} + + + + + + + internal record RowEdit { public record Update ( @@ -20717,6 +22216,37 @@ public override void Write(CancelToken? value, BigEndianStream stream) { +class FfiConverterOptionalTypeExportProgress: FfiConverterRustBuffer { + public static FfiConverterOptionalTypeExportProgress INSTANCE = new FfiConverterOptionalTypeExportProgress(); + + public override ExportProgress? Read(BigEndianStream stream) { + if (stream.ReadByte() == 0) { + return null; + } + return FfiConverterTypeExportProgress.INSTANCE.Read(stream); + } + + public override int AllocationSize(ExportProgress? value) { + if (value == null) { + return 1; + } else { + return 1 + FfiConverterTypeExportProgress.INSTANCE.AllocationSize((ExportProgress)value); + } + } + + public override void Write(ExportProgress? value, BigEndianStream stream) { + if (value == null) { + stream.WriteByte(0); + } else { + stream.WriteByte(1); + FfiConverterTypeExportProgress.INSTANCE.Write((ExportProgress)value, stream); + } + } +} + + + + class FfiConverterOptionalTypePageResume: FfiConverterRustBuffer { public static FfiConverterOptionalTypePageResume INSTANCE = new FfiConverterOptionalTypePageResume(); @@ -21337,6 +22867,37 @@ public override void Write(FilterValueArg? value, BigEndianStream stream) { +class FfiConverterOptionalTypeReplicationArg: FfiConverterRustBuffer { + public static FfiConverterOptionalTypeReplicationArg INSTANCE = new FfiConverterOptionalTypeReplicationArg(); + + public override ReplicationArg? Read(BigEndianStream stream) { + if (stream.ReadByte() == 0) { + return null; + } + return FfiConverterTypeReplicationArg.INSTANCE.Read(stream); + } + + public override int AllocationSize(ReplicationArg? value) { + if (value == null) { + return 1; + } else { + return 1 + FfiConverterTypeReplicationArg.INSTANCE.AllocationSize((ReplicationArg)value); + } + } + + public override void Write(ReplicationArg? value, BigEndianStream stream) { + if (value == null) { + stream.WriteByte(0); + } else { + stream.WriteByte(1); + FfiConverterTypeReplicationArg.INSTANCE.Write((ReplicationArg)value, stream); + } + } +} + + + + class FfiConverterOptionalTypeSecretKindTag: FfiConverterRustBuffer { public static FfiConverterOptionalTypeSecretKindTag INSTANCE = new FfiConverterOptionalTypeSecretKindTag(); @@ -21800,6 +23361,52 @@ public override void Write(CellChange[] value, BigEndianStream stream) { +class FfiConverterSequenceTypeCharacterSetInfo: FfiConverterRustBuffer { + public static FfiConverterSequenceTypeCharacterSetInfo INSTANCE = new FfiConverterSequenceTypeCharacterSetInfo(); + + public override CharacterSetInfo[] Read(BigEndianStream stream) { + var length = stream.ReadInt(); + if (length == 0) { + return []; + } + + var result = new CharacterSetInfo[length]; + var readFn = FfiConverterTypeCharacterSetInfo.INSTANCE.Read; + for (int i = 0; i < length; i++) { + result[i] = readFn(stream); + } + return result; + } + + public override int AllocationSize(CharacterSetInfo[] value) { + var sizeForLength = 4; + + // details/1-empty-list-as-default-method-parameter.md + if (value == null) { + return sizeForLength; + } + + var allocationSizeFn = FfiConverterTypeCharacterSetInfo.INSTANCE.AllocationSize; + var sizeForItems = value.Sum(item => allocationSizeFn(item)); + return sizeForLength + sizeForItems; + } + + public override void Write(CharacterSetInfo[] value, BigEndianStream stream) { + // details/1-empty-list-as-default-method-parameter.md + if (value == null) { + stream.WriteInt(0); + return; + } + + stream.WriteInt(value.Length); + var writerFn = FfiConverterTypeCharacterSetInfo.INSTANCE.Write; + value.ForEach(item => writerFn(item, stream)); + } +} + + + + class FfiConverterSequenceTypeCloudConnectionFields: FfiConverterRustBuffer { public static FfiConverterSequenceTypeCloudConnectionFields INSTANCE = new FfiConverterSequenceTypeCloudConnectionFields(); @@ -21984,6 +23591,52 @@ public override void Write(ConnectionRecord[] value, BigEndianStream stream) { +class FfiConverterSequenceTypeDataCenterReplicas: FfiConverterRustBuffer { + public static FfiConverterSequenceTypeDataCenterReplicas INSTANCE = new FfiConverterSequenceTypeDataCenterReplicas(); + + public override DataCenterReplicas[] Read(BigEndianStream stream) { + var length = stream.ReadInt(); + if (length == 0) { + return []; + } + + var result = new DataCenterReplicas[length]; + var readFn = FfiConverterTypeDataCenterReplicas.INSTANCE.Read; + for (int i = 0; i < length; i++) { + result[i] = readFn(stream); + } + return result; + } + + public override int AllocationSize(DataCenterReplicas[] value) { + var sizeForLength = 4; + + // details/1-empty-list-as-default-method-parameter.md + if (value == null) { + return sizeForLength; + } + + var allocationSizeFn = FfiConverterTypeDataCenterReplicas.INSTANCE.AllocationSize; + var sizeForItems = value.Sum(item => allocationSizeFn(item)); + return sizeForLength + sizeForItems; + } + + public override void Write(DataCenterReplicas[] value, BigEndianStream stream) { + // details/1-empty-list-as-default-method-parameter.md + if (value == null) { + stream.WriteInt(0); + return; + } + + stream.WriteInt(value.Length); + var writerFn = FfiConverterTypeDataCenterReplicas.INSTANCE.Write; + value.ForEach(item => writerFn(item, stream)); + } +} + + + + class FfiConverterSequenceTypeDeviceInfo: FfiConverterRustBuffer { public static FfiConverterSequenceTypeDeviceInfo INSTANCE = new FfiConverterSequenceTypeDeviceInfo(); @@ -23272,6 +24925,52 @@ public override void Write(StructureIndex[] value, BigEndianStream stream) { +class FfiConverterSequenceTypeTableName: FfiConverterRustBuffer { + public static FfiConverterSequenceTypeTableName INSTANCE = new FfiConverterSequenceTypeTableName(); + + public override TableName[] Read(BigEndianStream stream) { + var length = stream.ReadInt(); + if (length == 0) { + return []; + } + + var result = new TableName[length]; + var readFn = FfiConverterTypeTableName.INSTANCE.Read; + for (int i = 0; i < length; i++) { + result[i] = readFn(stream); + } + return result; + } + + public override int AllocationSize(TableName[] value) { + var sizeForLength = 4; + + // details/1-empty-list-as-default-method-parameter.md + if (value == null) { + return sizeForLength; + } + + var allocationSizeFn = FfiConverterTypeTableName.INSTANCE.AllocationSize; + var sizeForItems = value.Sum(item => allocationSizeFn(item)); + return sizeForLength + sizeForItems; + } + + public override void Write(TableName[] value, BigEndianStream stream) { + // details/1-empty-list-as-default-method-parameter.md + if (value == null) { + stream.WriteInt(0); + return; + } + + stream.WriteInt(value.Length); + var writerFn = FfiConverterTypeTableName.INSTANCE.Write; + value.ForEach(item => writerFn(item, stream)); + } +} + + + + class FfiConverterSequenceTypeTagRecord: FfiConverterRustBuffer { public static FfiConverterSequenceTypeTagRecord INSTANCE = new FfiConverterSequenceTypeTagRecord(); diff --git a/windows/src/TablePro.Interop/TableKeys.cs b/windows/src/TablePro.Interop/TableKeys.cs index b7e3025412..a3c4350197 100644 --- a/windows/src/TablePro.Interop/TableKeys.cs +++ b/windows/src/TablePro.Interop/TableKeys.cs @@ -34,5 +34,9 @@ public FollowedReference Follow(ResultSet rows, long row, RowReferenceChoice cho _keys.FollowReference(rows.Handle, checked((ulong)row), choice.Referencing, checked((uint)choice.Key)))); } + // The other tables whose foreign keys point at this one, each once. + public IReadOnlyList ReferencingTables => + [.. _keys.ReferencingTables().Select(table => new TableName(table.Schema, table.Name))]; + public void Dispose() => _keys.Dispose(); } diff --git a/windows/tests/TablePro.App.Model.Tests/CellFiltersTests.cs b/windows/tests/TablePro.App.Model.Tests/CellFiltersTests.cs new file mode 100644 index 0000000000..5a5515e709 --- /dev/null +++ b/windows/tests/TablePro.App.Model.Tests/CellFiltersTests.cs @@ -0,0 +1,91 @@ +using TablePro.Interop; + +namespace TablePro.App.Model.Tests; + +public sealed class CellFiltersTests +{ + private static readonly FilterOperator[] Ordered = + [ + FilterOperator.Equal, + FilterOperator.NotEqual, + FilterOperator.Less, + FilterOperator.Greater, + FilterOperator.IsNull, + FilterOperator.IsNotNull, + ]; + + private static readonly FilterOperator[] Textual = + [FilterOperator.Equal, FilterOperator.NotEqual, FilterOperator.Contains, FilterOperator.IsNull, FilterOperator.IsNotNull]; + + [Fact] + public void AValueOffersEqualAndNotAndAnOrderedColumnGreaterAndLessToo() + { + var number = CellFilters.For("Total", Ordered, CellValue.Of("Total", "9.99"), pending: false); + var text = CellFilters.For("Name", Textual, CellValue.Of("Name", "AC/DC"), pending: false); + + Assert.Equal( + [FilterOperator.Equal, FilterOperator.NotEqual, FilterOperator.Greater, FilterOperator.Less], + number.Select(rule => rule.Operator)); + Assert.All(number, rule => Assert.Equal(new FilterValue.One("9.99"), rule.Value)); + Assert.Equal([FilterOperator.Equal, FilterOperator.NotEqual], text.Select(rule => rule.Operator)); + } + + [Fact] + public void ANullCellOffersTheNullTests() + { + var rules = CellFilters.For("Composer", Textual, CellValue.Of("Composer", null), pending: false); + + Assert.Equal([FilterOperator.IsNull, FilterOperator.IsNotNull], rules.Select(rule => rule.Operator)); + Assert.All(rules, rule => Assert.Null(rule.Value)); + } + + [Theory] + [InlineData(" padded")] + [InlineData("padded ")] + [InlineData("null")] + [InlineData("")] + public void AValueAFilterRowWouldReadOtherwiseOffersNothing(string text) => + Assert.Empty(CellFilters.For("Name", Textual, CellValue.Of("Name", text), pending: false)); + + [Fact] + public void ABinaryValueALongValueAndAPendingEditOfferNothing() + { + var bytes = new CellValue("Data", null, false, new byte[] { 1, 2 }, 2, IsDefault: false); + var longText = CellValue.Of("Name", new string('x', CellFilters.MaximumValueLength + 1)); + + Assert.Empty(CellFilters.For("Data", Textual, bytes, pending: false)); + Assert.Empty(CellFilters.For("Name", Textual, longText, pending: false)); + Assert.Empty(CellFilters.For("Name", Textual, CellValue.Of("Name", "typed"), pending: true)); + Assert.Empty(CellFilters.For("Name", Textual, null, pending: false)); + } + + [Fact] + public void ARuleJoinsMatchAllKeepingTheSqlAndRunsAloneUnderMatchAnyOfTwo() + { + var name = new FilterRule("Name", FilterOperator.Contains, new FilterValue.One("AC")); + var id = new FilterRule("Id", FilterOperator.Greater, new FilterValue.One("3")); + var added = new FilterRule("Name", FilterOperator.NotEqual, new FilterValue.One("AC/DC")); + + var all = CellFilters.With(new TableFilter(FilterCombinator.And, [name], "Id < 9"), added); + var any = CellFilters.With(new TableFilter(FilterCombinator.Or, [name, id], null), added); + var anyOfOne = CellFilters.With(new TableFilter(FilterCombinator.Or, [name], null), added); + var none = CellFilters.With(null, added); + + Assert.Equal(new TableFilter(FilterCombinator.And, [name, added], "Id < 9").Rules, all.Rules); + Assert.Equal("Id < 9", all.RawSql); + Assert.Equal([added], any.Rules); + Assert.Equal(FilterCombinator.And, any.Combinator); + Assert.Equal([name, added], anyOfOne.Rules); + Assert.Equal(FilterCombinator.And, anyOfOne.Combinator); + Assert.Equal([added], none.Rules); + } + + [Fact] + public void ARuleAlreadyRunningLeavesTheFilterAsItIs() + { + var name = new FilterRule("Name", FilterOperator.Equal, new FilterValue.One("AC/DC")); + var running = new TableFilter(FilterCombinator.And, [name], null); + + Assert.Same(running, CellFilters.With(running, name with { })); + } +} diff --git a/windows/tests/TablePro.App.Model.Tests/ExportPlanTests.cs b/windows/tests/TablePro.App.Model.Tests/ExportPlanTests.cs index 6ae8d6a4e9..37ee49d210 100644 --- a/windows/tests/TablePro.App.Model.Tests/ExportPlanTests.cs +++ b/windows/tests/TablePro.App.Model.Tests/ExportPlanTests.cs @@ -5,7 +5,7 @@ namespace TablePro.App.Model.Tests; public sealed class ExportPlanTests { [Fact] - public void AQueryResultExportsTheRowsShownInEveryFormatButSqlInsert() + public void AQueryResultExportsTheRowsShownInEveryFormat() { var plan = ExportPlan.For(null, "Results"); @@ -18,6 +18,7 @@ public void AQueryResultExportsTheRowsShownInEveryFormatButSqlInsert() ExportFormat.Markdown, ExportFormat.Html, ExportFormat.Xml, + ExportFormat.SqlInsert, ], plan.Formats); Assert.Equal("Results", plan.FileName); diff --git a/windows/tests/TablePro.App.Model.Tests/TableFilterMemoryTests.cs b/windows/tests/TablePro.App.Model.Tests/TableFilterMemoryTests.cs new file mode 100644 index 0000000000..2385f08461 --- /dev/null +++ b/windows/tests/TablePro.App.Model.Tests/TableFilterMemoryTests.cs @@ -0,0 +1,67 @@ +using TablePro.Interop; + +namespace TablePro.App.Model.Tests; + +public sealed class TableFilterMemoryTests : IDisposable +{ + private static readonly TableFilter ByName = new( + FilterCombinator.Or, + [ + new FilterRule("Name", FilterOperator.Contains, new FilterValue.One("AC")), + new FilterRule("Id", FilterOperator.In, new FilterValue.List(["1", "2"])), + ], + "Id > 3"); + + private static readonly TableName Artist = new("public", "Artist"); + + private readonly string _directory = Directory.CreateTempSubdirectory("tablepro-filters-").FullName; + + public void Dispose() => Directory.Delete(_directory, recursive: true); + + private string Path => System.IO.Path.Combine(_directory, "table-filters.json"); + + [Fact] + public void AFilterComesBackForItsTableAfterARelaunchAndForNoOtherDatabaseOrConnection() + { + var connection = TableFilterMemory.ConnectionKey(Guid.NewGuid(), null); + new TableFilterMemory(Path).For(connection, "shop").Remember(Artist, ByName); + + var memory = new TableFilterMemory(Path); + + Assert.True(TableFilters.Same(ByName, memory.For(connection, "shop").Recall(Artist))); + Assert.Null(memory.For(connection, "other").Recall(Artist)); + Assert.Null(memory.For(TableFilterMemory.ConnectionKey(null, "C:\\a.sqlite"), "shop").Recall(Artist)); + Assert.Null(memory.For(connection, "shop").Recall(Artist with { Schema = "sales" })); + } + + [Fact] + public void ADotInANameCannotMakeTwoTablesOne() + { + var memory = new TableFilterMemory(Path); + var connection = TableFilterMemory.ConnectionKey(Guid.NewGuid(), null); + + memory.Remember(connection, "a.b", new TableName("c", "d"), ByName); + + Assert.Null(memory.Recall(connection, "a", new TableName("b.c", "d"))); + } + + [Fact] + public void ADroppedTableSchemaOrDatabaseTakesItsFiltersWithIt() + { + var memory = new TableFilterMemory(Path); + var connection = TableFilterMemory.ConnectionKey(Guid.NewGuid(), null); + var sales = new TableName("sales", "Orders"); + memory.Remember(connection, "shop", Artist, ByName); + memory.Remember(connection, "shop", sales, ByName); + memory.Remember(connection, "crm", Artist, ByName); + + memory.ForgetTable(connection, "shop", Artist); + Assert.Null(memory.Recall(connection, "shop", Artist)); + + memory.ForgetSchema(connection, "shop", "sales"); + Assert.Null(memory.Recall(connection, "shop", sales)); + + memory.ForgetDatabase(connection, "crm"); + Assert.Null(new TableFilterMemory(Path).Recall(connection, "crm", Artist)); + } +} diff --git a/windows/tests/TablePro.App.Model.Tests/TableTabFilterTests.cs b/windows/tests/TablePro.App.Model.Tests/TableTabFilterTests.cs index 45065b4e72..570351b145 100644 --- a/windows/tests/TablePro.App.Model.Tests/TableTabFilterTests.cs +++ b/windows/tests/TablePro.App.Model.Tests/TableTabFilterTests.cs @@ -350,7 +350,65 @@ public async Task FilterChoicesThatCannotBeReadSayWhy() Assert.Equal("catalog.columns_unavailable", Assert.Single(_view.ChoicesFailures).Code); } - private TableTab New(SessionTab? restore = null, ObjectKind kind = ObjectKind.Table) + [Fact] + public async Task ATableOpenedAgainReadsWithTheFilterItLastHadAndClearForgetsIt() + { + var memory = new MemoryFilters(); + var first = New(filters: memory); + await first.ApplyFilterAsync(ByName); + + var again = New(filters: memory); + await again.RunAsync(); + + Assert.Same(ByName, again.Filter); + Assert.Same(ByName, _session.PageFilters[^1]); + + await again.ApplyFilterAsync(null); + + Assert.Null(New(filters: memory).Filter); + } + + [Fact] + public async Task AFilterTheCoreRefusesIsNotKeptForTheTable() + { + var memory = new MemoryFilters(); + var tab = New(filters: memory); + await tab.RunAsync(); + _session.RefusesFilter = true; + + await tab.ApplyFilterAsync(ByName); + + Assert.Null(New(filters: memory).Filter); + } + + [Fact] + public void ARestoredTabKeepsItsOwnFilterOverTheTablesLast() + { + var memory = new MemoryFilters(); + memory.Remember(new TableName(null, "Artist"), ByName); + + var restored = New(new SessionTab { Kind = TabKind.Table, Table = "Artist" }, filters: memory); + + Assert.Null(restored.Filter); + } + + [Fact] + public async Task ACellsRuleJoinsTheRulesRunningAndOneRunningAlreadyReadsNothing() + { + await _tab.RunAsync(); + await _tab.ApplyFilterAsync(ByName); + var reads = _session.Queries.Count; + var notAc = new FilterRule("Name", FilterOperator.NotEqual, new FilterValue.One("AC/DC")); + + Assert.True(await _tab.FilterByAsync(notAc)); + Assert.False(await _tab.FilterByAsync(notAc)); + + Assert.Equal(FilterCombinator.And, _tab.Filter?.Combinator); + Assert.Equal([ByName.Rules[0], notAc], _tab.Filter!.Rules); + Assert.Equal(reads + 1, _session.Queries.Count); + } + + private TableTab New(SessionTab? restore = null, ObjectKind kind = ObjectKind.Table, ITableFilters? filters = null) { var tab = new TableTab( _session, @@ -359,13 +417,32 @@ private TableTab New(SessionTab? restore = null, ObjectKind kind = ObjectKind.Ta new TableName(null, "Artist"), kind, () => new AppSettings(), - restore); + restore, + filters); _view.Tab = tab; return tab; } + + private sealed class MemoryFilters : ITableFilters + { + private readonly Dictionary _kept = []; + + public TableFilter? Recall(TableName table) => _kept.GetValueOrDefault(table); + + public void Remember(TableName table, TableFilter? filter) + { + if (filter is null) + { + _kept.Remove(table); + } + else + { + _kept[table] = filter; + } + } + } } -// A filter as one line, since its records hold their lists by reference. internal static class FilterText { public static string Of(TableFilter? filter) diff --git a/windows/tests/TablePro.App.Model.Tests/WorkspaceTabsTests.cs b/windows/tests/TablePro.App.Model.Tests/WorkspaceTabsTests.cs index 7deec91d45..0ad742d00b 100644 --- a/windows/tests/TablePro.App.Model.Tests/WorkspaceTabsTests.cs +++ b/windows/tests/TablePro.App.Model.Tests/WorkspaceTabsTests.cs @@ -51,6 +51,23 @@ public async Task AClosedTabIsKeptAtItsPlaceToReopenAndTheLastOneEmptiesTheStrip Assert.Null(_tabs.TakeClosed()); } + [Fact] + public async Task ATabOfADroppedTableClosesWithoutAskingAndIsNotKeptToReopen() + { + var edited = new FakeTab("Artist", TabKind.Table) { HasUnsavedChanges = true, ClosesUnsaved = false }; + _strip.Tabs.Add(edited); + var emptied = 0; + _tabs.Emptied += (_, _) => emptied++; + + await _tabs.DiscardAsync(edited); + + Assert.Equal(1, edited.Closes); + Assert.Empty(_strip.Asked); + Assert.Empty(_strip.Tabs); + Assert.Equal(0, _tabs.ClosedCount); + Assert.Equal(1, emptied); + } + [Fact] public async Task ASecondCloseWhileTheFirstIsClosingTheTabDoesNothing() { diff --git a/windows/tests/TablePro.App.UITests/CatalogTests.cs b/windows/tests/TablePro.App.UITests/CatalogTests.cs new file mode 100644 index 0000000000..f277928ee7 --- /dev/null +++ b/windows/tests/TablePro.App.UITests/CatalogTests.cs @@ -0,0 +1,167 @@ +using FlaUI.Core.AutomationElements; +using FlaUI.Core.Definitions; +using FlaUI.Core.Input; +using FlaUI.Core.Tools; +using FlaUI.Core.WindowsAPI; + +namespace TablePro.App.UITests; + +public sealed partial class ShellTests +{ + [Theory] + [MemberData(nameof(Themes))] + public void ACellsValueFiltersTheTableAndTheTableOpensAgainWithIt(string theme) + { + using var app = AppSession.Launch(theme); + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + var tree = app.Find("ObjectTree"); + app.Click(app.FindByName("Genre", tree), () => app.TryFind("TableStatus") is not null); + Assert.True(ShowsRows(app.Find("TableStatus"), 25)); + var grid = app.Find("ResultsGrid"); + Assert.Equal("Rock", app.WaitForCell(grid, 0, 1, value => value == "Rock")); + + app.RightClick(CellOf(grid, 0, 1), () => app.TryFindMenuItem("Filter") is not null); + var equal = app.OpenSubmenu("Filter", "Name = “Rock”"); + Assert.NotNull(app.TryFindMenuItem("Name != “Rock”")); + app.Screenshot("98-filter-by-cell-menu"); + app.Click(equal); + Assert.True(ShowsRows(app.Find("TableStatus"), 1)); + Assert.Equal("Rock", app.Find("FilterValue0").AsTextBox().Text); + app.Screenshot("99-filter-by-cell"); + + var tabs = app.Find("Tabs"); + bool Open() => tabs.FindFirstDescendant(condition => + condition.ByControlType(ControlType.TabItem).And(condition.ByName("Genre"))) is not null; + app.Chord(() => !Open(), VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_W); + app.Click(app.FindByName("Genre", tree), () => app.TryFind("TableStatus") is not null); + + Assert.True(ShowsRows(app.Find("TableStatus"), 1)); + Assert.Equal("Rock", app.Find("FilterValue0").AsTextBox().Text); + app.Screenshot("100-filter-kept-for-table"); + } + + [Theory] + [MemberData(nameof(Themes))] + public void ATableIsEmptiedAndDroppedAfterAQuestionShowingItsStatement(string theme) + { + using var app = AppSession.Launch(theme); + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + var tree = app.Find("ObjectTree"); + + // Track's foreign key points at Genre, so emptying it is refused before anything is asked. + app.RightClick(app.FindByName("Genre", tree), () => app.TryFindMenuItem("Truncate...") is not null); + app.Click( + app.FindMenuItem("Truncate..."), + () => app.Window.FindFirstDescendant(condition => condition.ByName("Cannot truncate table")) is not null); + app.Screenshot("101-truncate-refused"); + app.CloseDialog(); + + app.RightClick(app.FindByName("PlaylistTrack", tree), () => app.TryFindMenuItem("Truncate...") is not null); + app.Click(app.FindMenuItem("Truncate..."), () => app.TryFind("CatalogStatements") is not null); + Assert.Equal("DELETE FROM \"PlaylistTrack\";", app.Find("CatalogStatements").Name); + app.Screenshot("102-truncate-question"); + app.PressDialogButton("Truncate"); + Assert.True(Retry.WhileTrue(() => app.TryFind("CatalogStatements") is not null, AppSession.Wait, throwOnTimeout: false).Success); + app.Click(app.FindByName("PlaylistTrack", tree), () => app.TryFind("TableStatus") is not null); + Assert.True(ShowsRows(app.Find("TableStatus"), 0)); + + app.RightClick(app.FindByName("PlaylistTrack", tree), () => app.TryFindMenuItem("Drop...") is not null); + app.Click(app.FindMenuItem("Drop..."), () => app.TryFind("CatalogStatements") is not null); + Assert.Equal("DROP TABLE \"PlaylistTrack\";", app.Find("CatalogStatements").Name); + app.Screenshot("103-drop-question"); + app.PressDialogButton("Drop"); + + Assert.True(Retry.WhileFalse( + () => tree.FindFirstDescendant(condition => condition.ByName("PlaylistTrack")) is null + && app.TryFind("TableStatus") is null, + AppSession.Wait, + throwOnTimeout: false).Success); + app.Screenshot("104-table-dropped"); + } + + [Theory] + [MemberData(nameof(Themes))] + public void AQueryResultExportsAsSqlInsert(string theme) + { + using var app = AppSession.Launch(theme); + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + app.FindByName("Track", app.Find("ObjectTree")); + app.TypeInto("Query 1", "SELECT GenreId, Name FROM Genre"); + app.Chord( + () => app.Find("QueryStatus").Name.StartsWith("Rows: ", StringComparison.Ordinal), + VirtualKeyShort.CONTROL, + VirtualKeyShort.ENTER); + var grid = app.Find("ResultsGrid"); + app.WaitForCell(grid, 0, 1, value => value == "Rock"); + + app.RightClick(CellOf(grid, 0, 1), () => app.TryFindMenuItem("Export results...") is not null); + Assert.Null(app.TryFindMenuItem("Filter")); + app.Click(app.FindMenuItem("Export results..."), () => app.TryFind("ExportFormats") is not null); + var sqlInsert = app.FindByName("SQL INSERT", app.Find("ExportFormats")); + app.Click(sqlInsert); + app.Screenshot("105-export-result-as-sql-insert"); + app.CloseDialog(); + } + + [Theory] + [MemberData(nameof(Themes))] + public void ASchemaIsCreatedAndDroppedFromTheDatabaseMenuAndItsRow(string theme) + { + using var app = AppSession.Launch(theme); + var file = Path.Combine(app.DataFolder, "catalog.duckdb"); + app.Click(app.Find("NewConnectionCard"), () => app.HasWindow("New connection")); + var form = app.WaitForWindow("New connection"); + app.Click(app.Find("Engine_duckdb", form), () => app.TryFind("FileBox", form) is not null, form); + app.Find("NameBox", form).AsTextBox().Text = "Catalog"; + app.Click(app.Find("NewFileButton", form), () => FileDialog(app) is not null, form); + SaveInFileDialog(app, file); + app.Find("ConnectButton", form).AsButton().Invoke(); + app.WaitForClose("New connection"); + var tree = app.Find("ObjectTree"); + + app.Click(app.Find("DatabaseMenu"), () => app.TryFindMenuItem("New schema...") is not null); + app.Click(app.FindMenuItem("New schema..."), () => app.TryFind("CatalogName") is not null); + app.Find("CatalogName").AsTextBox().Text = "sales"; + Assert.Equal( + "CREATE SCHEMA \"sales\";", + AppSession.WaitForText(app.Find("CatalogStatements"), text => text.Length > 0)); + app.Screenshot("106-new-schema"); + app.PressDialogButton("Create"); + var sales = Retry.WhileNull( + () => tree.FindFirstDescendant(condition => condition.ByName("sales")), + AppSession.Wait, + throwOnTimeout: false).Result + ?? throw new InvalidOperationException("The new schema did not show."); + + app.RightClick(sales, () => app.TryFindMenuItem("Drop schema “sales”...") is not null); + app.Click(app.FindMenuItem("Drop schema “sales”..."), () => app.TryFind("CatalogStatements") is not null); + Assert.Equal("DROP SCHEMA \"sales\";", app.Find("CatalogStatements").Name); + app.Screenshot("107-drop-schema"); + app.PressDialogButton("Drop"); + Assert.True(Retry.WhileFalse( + () => tree.FindFirstDescendant(condition => condition.ByName("sales")) is null, + AppSession.Wait, + throwOnTimeout: false).Success); + } + + // The Save dialog's File name box is control 1001, and Enter saves to the path typed there. + private static void SaveInFileDialog(AppSession app, string path) + { + var dialog = Retry.WhileNull(() => FileDialog(app), AppSession.Wait, throwOnTimeout: false).Result + ?? throw new InvalidOperationException("The Save dialog did not appear."); + var name = Retry.WhileNull( + () => dialog.FindFirstDescendant(condition => + condition.ByAutomationId("1001").And(condition.ByControlType(ControlType.Edit))), + AppSession.Wait, + throwOnTimeout: false).Result + ?? throw new InvalidOperationException("The Save dialog has no File name box."); + dialog.SetForeground(); + name.Focus(); + name.AsTextBox().Text = path; + Keyboard.Type(VirtualKeyShort.RETURN); + if (!Retry.WhileFalse(() => FileDialog(app) is null, AppSession.Wait, throwOnTimeout: false).Success) + { + throw new InvalidOperationException("The Save dialog did not take the path."); + } + } +} diff --git a/windows/tests/TablePro.Interop.Tests/CatalogChangeTests.cs b/windows/tests/TablePro.Interop.Tests/CatalogChangeTests.cs new file mode 100644 index 0000000000..9a99fbad11 --- /dev/null +++ b/windows/tests/TablePro.Interop.Tests/CatalogChangeTests.cs @@ -0,0 +1,67 @@ +namespace TablePro.Interop.Tests; + +public sealed class CatalogChangeTests +{ + private static readonly TableName People = new(null, "people"); + + [Fact] + public async Task ATruncateEmptiesTheTableOnceTheQuestionIsAnswered() + { + var cancellationToken = TestContext.Current.CancellationToken; + await using var database = await SqliteDatabase.CreateAsync(); + var abilities = database.Session.CatalogAbilities; + + using var change = database.Session.PlanCatalogChange( + new CatalogOperation.TruncateTable(People, Cascade: false, RestartIdentity: false, Cluster: null)); + var unanswered = await Assert.ThrowsAsync( + () => change.RunAsync(confirmed: false, cancellationToken)); + var outcome = await change.RunAsync(confirmed: true, cancellationToken); + + Assert.True(abilities.TruncateTable); + Assert.True(abilities.TruncateRefusedWhenReferenced); + Assert.False(abilities.CreateSchema); + Assert.Equal(["DELETE FROM \"people\""], change.Statements); + Assert.Equal(SaveGate.Confirm, change.Gate); + Assert.True(change.IsDestructive); + Assert.Equal("catalog.not_confirmed", unanswered.Code); + Assert.IsType(outcome); + Assert.Equal("0", await CountAsync(database, "people")); + } + + [Fact] + public async Task ADroppedTableLeavesTheCatalogAndTheTablesPointingAtOneAreNamed() + { + var cancellationToken = TestContext.Current.CancellationToken; + await using var database = await SqliteDatabase.CreateAsync(); + await database.ExecuteAsync("CREATE TABLE pets (id INTEGER PRIMARY KEY, owner INTEGER REFERENCES people(id))"); + + using var keys = await database.Session.ReadTableKeysAsync(People, cancellationToken); + using var change = database.Session.PlanCatalogChange(new CatalogOperation.DropTable(new(null, "pets"), false)); + var outcome = await change.RunAsync(confirmed: true, cancellationToken); + var objects = await database.Session.RefreshObjectsAsync(ObjectScope.DefaultSchema, cancellationToken); + + Assert.Equal([new TableName(null, "pets")], keys.ReferencingTables); + Assert.Equal(["DROP TABLE \"pets\""], change.Statements); + Assert.IsType(outcome); + Assert.DoesNotContain(objects, entry => entry.Name == "pets"); + } + + [Fact] + public async Task AnOperationTheEngineHasNoSyntaxForIsRefusedBeforeAnythingIsShown() + { + await using var database = await SqliteDatabase.CreateAsync(); + + var error = Assert.Throws( + () => database.Session.PlanCatalogChange(new CatalogOperation.CreateSchema("sales", null))); + + Assert.Equal("catalog.unsupported", error.Code); + } + + private static async Task CountAsync(SqliteDatabase database, string table) + { + using var run = database.Editor.Run($"SELECT count(*) FROM {table}"); + Assert.IsType(await run.Completion); + using var set = run.GetResultSet(0) ?? throw new InvalidOperationException("The count held no set."); + return set.GetCellText(0, 0); + } +} diff --git a/windows/tests/TablePro.Interop.Tests/ExportTests.cs b/windows/tests/TablePro.Interop.Tests/ExportTests.cs index cd5faac61f..d6f00764d4 100644 --- a/windows/tests/TablePro.Interop.Tests/ExportTests.cs +++ b/windows/tests/TablePro.Interop.Tests/ExportTests.cs @@ -18,7 +18,8 @@ public async Task CsvOfAResultHoldsExactlyItsRows() Assert.IsType(await run.Completion); using var set = run.GetResultSet(0) ?? throw new InvalidOperationException("The read held no set."); - var rows = await database.Session.ExportRowsAsync(set, 3, ExportFormat.Csv, null, path, cancellationToken); + var rows = await database.Session.ExportRowsAsync( + set, 3, ExportFormat.Csv, null, path, cancellationToken: cancellationToken); Assert.Equal(3, rows); Assert.Equal( @@ -43,27 +44,84 @@ public async Task APageExportsOnlyItsVisibleColumns() Assert.True(set.GetColumns().Count > page.VisibleColumns, "A table with no key pages by its row address."); var rows = await database.Session.ExportRowsAsync( - set, page.VisibleColumns, ExportFormat.Csv, notes, path, cancellationToken); + set, page.VisibleColumns, ExportFormat.Csv, notes, path, cancellationToken: cancellationToken); Assert.Equal(2, rows); Assert.Equal("body\nfirst\nsecond\n", await File.ReadAllTextAsync(path, cancellationToken)); } [Fact] - public async Task SqlInsertWithoutATableIsRefusedBeforeAnyFile() + public async Task SqlInsertOfAResultWritesIntoTheTableItsColumnsCameFrom() { var cancellationToken = TestContext.Current.CancellationToken; await using var database = await SqliteDatabase.CreateAsync(); var path = Beside(database, "people.sql"); - using var run = database.Editor.Run("SELECT id, name FROM people", cancellationToken: cancellationToken); + using var run = database.Editor.Run("SELECT id, name FROM people ORDER BY id", cancellationToken: cancellationToken); + Assert.IsType(await run.Completion); + using var set = run.GetResultSet(0) ?? throw new InvalidOperationException("The read held no set."); + + var rows = await database.Session.ExportRowsAsync( + set, 2, ExportFormat.SqlInsert, null, path, cancellationToken: cancellationToken); + + Assert.Equal(3, rows); + var first = (await File.ReadAllLinesAsync(path, cancellationToken))[0]; + Assert.Equal("INSERT INTO \"people\" (\"id\", \"name\") VALUES (1, 'Ada');", first); + } + + [Fact] + public async Task SqlInsertOfAComputedResultWritesIntoQueryResults() + { + var cancellationToken = TestContext.Current.CancellationToken; + await using var database = await SqliteDatabase.CreateAsync(); + var path = Beside(database, "loud.sql"); + using var run = database.Editor.Run( + "SELECT id, upper(name) AS loud FROM people ORDER BY id", cancellationToken: cancellationToken); Assert.IsType(await run.Completion); using var set = run.GetResultSet(0) ?? throw new InvalidOperationException("The read held no set."); - var error = await Assert.ThrowsAsync( - () => database.Session.ExportRowsAsync(set, 2, ExportFormat.SqlInsert, null, path, cancellationToken)); + await database.Session.ExportRowsAsync( + set, 2, ExportFormat.SqlInsert, null, path, cancellationToken: cancellationToken); - Assert.Equal("ffi.export_needs_table", error.Code); - Assert.Empty(Named(database, "people.sql")); + var first = (await File.ReadAllLinesAsync(path, cancellationToken))[0]; + Assert.Equal("INSERT INTO \"query_results\" (\"id\", \"loud\") VALUES (1, 'ADA');", first); + } + + [Fact] + public async Task AnExportReportsTheRowsWrittenWhileItRuns() + { + var cancellationToken = TestContext.Current.CancellationToken; + await using var database = await SqliteDatabase.CreateAsync(); + await database.ExecuteAsync( + "CREATE VIEW endless AS WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c) SELECT x FROM c"); + using var cancel = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + var progress = new LatestRows(); + var export = database.Session.ExportTableAsync( + new TableName(null, "endless"), + null, + null, + ExportFormat.Csv, + Beside(database, "endless.csv"), + progress, + cancel.Token); + + var waited = Stopwatch.StartNew(); + while (progress.Rows < 2) + { + Assert.False(export.IsCompleted, "The export ended before it reported."); + Assert.True(waited.Elapsed < TimeSpan.FromSeconds(10), "The export reported no rows."); + await Task.Delay(5, cancellationToken); + } + + var first = progress.Rows; + while (progress.Rows == first) + { + Assert.True(waited.Elapsed < TimeSpan.FromSeconds(10), "The count stayed where it was."); + await Task.Delay(5, cancellationToken); + } + + await cancel.CancelAsync(); + await Assert.ThrowsAnyAsync(() => export); + Assert.True(progress.Rows > first); } [Fact] @@ -74,7 +132,7 @@ public async Task SqlInsertOfATableNamesIt() var path = Beside(database, "people.sql"); var rows = await database.Session.ExportTableAsync( - People, null, null, ExportFormat.SqlInsert, path, cancellationToken); + People, null, null, ExportFormat.SqlInsert, path, cancellationToken: cancellationToken); Assert.Equal(3, rows); var first = (await File.ReadAllLinesAsync(path, cancellationToken))[0]; @@ -93,7 +151,7 @@ public async Task ATableExportWritesEveryRowTheFilterSelectsInTheSortOrder() null); var rows = await database.Session.ExportTableAsync( - People, new ColumnSort(0, false), filter, ExportFormat.Csv, path, cancellationToken); + People, new ColumnSort(0, false), filter, ExportFormat.Csv, path, cancellationToken: cancellationToken); Assert.Equal(2, rows); Assert.Equal( @@ -111,7 +169,7 @@ await database.ExecuteAsync( using var cancel = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); var path = Beside(database, "endless.csv"); var export = database.Session.ExportTableAsync( - new TableName(null, "endless"), null, null, ExportFormat.Csv, path, cancel.Token); + new TableName(null, "endless"), null, null, ExportFormat.Csv, path, cancellationToken: cancel.Token); var waited = Stopwatch.StartNew(); while (Named(database, "endless.csv").Count == 0) @@ -139,4 +197,14 @@ .. Directory.EnumerateFiles(Folder(database)) .Select(file => Path.GetFileName(file)) .Where(name => name.Contains(part, StringComparison.Ordinal)), ]; + + // Reported on the thread pool, which a test reads from another thread. + private sealed class LatestRows : IProgress + { + private long _rows; + + public long Rows => Interlocked.Read(ref _rows); + + public void Report(long value) => Interlocked.Exchange(ref _rows, value); + } } From d1ecc6a54db136fe00f82bfcf60510d611304b32 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 09:56:45 +0700 Subject: [PATCH 02/16] fix(windows): filter by the core's cell text, keep a result's database for export, refuse repeated INSERT columns --- core/crates/core/src/browse/mod.rs | 2 +- core/crates/core/src/browse/row_reference.rs | 2 +- core/crates/ffi/src/export.rs | 14 +++++++ core/crates/ffi/src/results/result_set.rs | 17 ++++++++ core/crates/ffi/tests/browse.rs | 22 ++++++++++ core/crates/ffi/tests/export.rs | 27 ++++++++++++ ...tions-table-filters-and-export-progress.md | 4 +- windows/src/TablePro.App.Model/CellFilters.cs | 8 ++-- windows/src/TablePro.App.Model/QueryTab.cs | 5 +++ .../TablePro.App/Strings/en-US/Resources.resw | 7 +++- .../TablePro.App/Views/QueryTabView.xaml.cs | 4 +- .../TablePro.App/Views/TableTabView.xaml.cs | 15 ++++++- .../TablePro.App/Views/WorkspaceView.xaml.cs | 2 +- .../Generated/tablepro_ffi.cs | 41 +++++++++++++++++++ windows/src/TablePro.Interop/ResultSet.cs | 9 ++++ .../CellFiltersTests.cs | 19 +++++---- .../QueryTabDatabaseTests.cs | 16 ++++++++ 17 files changed, 192 insertions(+), 22 deletions(-) diff --git a/core/crates/core/src/browse/mod.rs b/core/crates/core/src/browse/mod.rs index 053c99d78c..277897870a 100644 --- a/core/crates/core/src/browse/mod.rs +++ b/core/crates/core/src/browse/mod.rs @@ -36,6 +36,6 @@ pub use paging_mode::{PagingMode, paging_mode}; pub use read_page::{PageRead, read_page}; pub use reference_error::ReferenceError; pub use row_key_layout::RowKeyLayout; -pub use row_reference::RowReference; +pub use row_reference::{RowReference, filter_text}; pub use sort_key::SortKey; pub use split_page::split_page; diff --git a/core/crates/core/src/browse/row_reference.rs b/core/crates/core/src/browse/row_reference.rs index b27c0ad3d3..f66c9ee67e 100644 --- a/core/crates/core/src/browse/row_reference.rs +++ b/core/crates/core/src/browse/row_reference.rs @@ -112,7 +112,7 @@ fn key_filter(key: &str, cells: Vec<(&ColumnType, &Value)>, targets: &[String]) /// The other side reads it under a type that compares alike with this /// column's, so text this column's own type does not read back as the /// same value has no filter. -fn filter_text(value: &Value, column_type: &ColumnType) -> Option { +pub fn filter_text(value: &Value, column_type: &ColumnType) -> Option { let text = match value { Value::Bytes(bytes) if matches!(column_type.kind, ColumnKind::Bytes { .. }) => { format!("0x{}", crate::hex::encode_lower(bytes)) diff --git a/core/crates/ffi/src/export.rs b/core/crates/ffi/src/export.rs index cf46c7145a..e548a58d43 100644 --- a/core/crates/ffi/src/export.rs +++ b/core/crates/ffi/src/export.rs @@ -136,6 +136,13 @@ impl DatabaseSession { }, _ => None, }; + // An INSERT that names a column twice is refused by every server, so it is refused here before any file. + if insert.is_some() && repeats_a_name(&columns) { + return Err(CoreError::failed( + "ffi.export_duplicate_columns", + "SQL INSERT names each column once", + )); + } let recipe = Recipe { format, title: title(table.as_ref(), &target), @@ -506,6 +513,13 @@ impl ResultSink for FileSink { } } +fn repeats_a_name(columns: &[ResultColumn]) -> bool { + columns + .iter() + .enumerate() + .any(|(index, column)| columns[..index].iter().any(|before| before.name == column.name)) +} + // Where each database holds schemas of its own, an origin names no database, so the session's catalog describes the // table only when the rows were read in the session's own database. `read_in` is None there. fn catalog_describes(read_in: Option<&str>, layout: Option) -> bool { diff --git a/core/crates/ffi/src/results/result_set.rs b/core/crates/ffi/src/results/result_set.rs index e731b0862c..903e2f55e7 100644 --- a/core/crates/ffi/src/results/result_set.rs +++ b/core/crates/ffi/src/results/result_set.rs @@ -101,6 +101,23 @@ impl ResultSet { .ok_or_else(|| CoreError::failed("ffi.out_of_range", format!("no cell at row {row}, column {column}"))) } + // The text a filter on the cell's column reads back as the very value stored, as a followed foreign key builds its + // filter. `None` for null, and for a value no filter text binds again, such as 'true' in a SQLite BOOLEAN column. + pub fn filter_text(&self, row: u64, column: u32) -> Result, CoreError> { + let index = usize::try_from(column).unwrap_or(usize::MAX); + let read = self.result_columns(); + let Some(read) = read.get(index) else { + return Err(CoreError::failed( + "ffi.out_of_range", + format!("no cell at row {row}, column {column}"), + )); + }; + Ok(self + .stored_value(row, index) + .filter(|value| !matches!(value, tablepro_core::Value::Null)) + .and_then(|value| tablepro_core::browse::filter_text(&value, &read.column_type))) + } + // A value's start for a viewer, read without copying the rest: its text with line breaks kept, or a binary // value's hex. `None` for null. pub fn cell_text_head(&self, row: u64, column: u32, max_chars: u32) -> Result, CoreError> { diff --git a/core/crates/ffi/tests/browse.rs b/core/crates/ffi/tests/browse.rs index de039e2328..adfa1563b0 100644 --- a/core/crates/ffi/tests/browse.rs +++ b/core/crates/ffi/tests/browse.rs @@ -794,3 +794,25 @@ async fn a_cell_reads_its_start_and_a_binary_cell_its_first_bytes_with_its_lengt assert_eq!(rows.cell_text_head(1, 2, 100).unwrap(), None); session.close().await.unwrap(); } + +#[tokio::test] +async fn a_cells_filter_text_is_offered_only_where_it_binds_the_value_stored() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture( + dir.path(), + "CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT, active BOOLEAN); \ + INSERT INTO people VALUES (1, 'Ada', 'true'), (2, NULL, 1)", + ) + .await; + let page = read(&session, None, None, 10, 0).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(); + + assert_eq!(set.filter_text(0, 1).unwrap().as_deref(), Some("Ada")); + assert_eq!(set.filter_text(1, 1).unwrap(), None); + assert_eq!(set.filter_text(0, 2).unwrap(), None); + assert_eq!(set.filter_text(1, 2).unwrap().as_deref(), Some("1")); + assert!(set.filter_text(0, 9).is_err()); +} diff --git a/core/crates/ffi/tests/export.rs b/core/crates/ffi/tests/export.rs index 1f08a49cc0..8e59186ee9 100644 --- a/core/crates/ffi/tests/export.rs +++ b/core/crates/ffi/tests/export.rs @@ -320,6 +320,33 @@ async fn sql_insert_of_an_aliased_result_names_the_tables_columns() { ); } +#[tokio::test] +async fn sql_insert_of_a_result_naming_a_column_twice_is_refused_before_any_file() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path()).await; + let set = run(&session, "SELECT a.id, b.id FROM people a JOIN people b ON a.id = b.id") + .await + .unwrap(); + let path = dir.path().join("pairs.sql"); + + let error = session + .export_rows( + set, + 2, + ExportFormatTag::SqlInsert, + None, + None, + spelled(&path), + None, + None, + ) + .await + .unwrap_err(); + + assert_eq!(error.code(), "ffi.export_duplicate_columns"); + assert_eq!(named(dir.path(), "pairs.sql"), Vec::::new()); +} + #[tokio::test] async fn sql_insert_of_a_computed_result_writes_into_query_results() { let dir = tempfile::tempdir().unwrap(); diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md index fbd0c58b8b..a028299e44 100644 --- a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -13,11 +13,11 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) - The filter a table's rows were last read with is kept per saved connection (or opened file), database, schema and table in `table-filters.json` beside the session file (`TableFilterMemory`), as macOS keys it. A table opened again reads with it and shows it in the bar. Clear forgets it; a filter the core or the server refused is not kept. A restored tab and a followed foreign key keep their own filter. - A dropped table forgets its filter, and a dropped schema or database every filter under it. -- A table tab's cell menu has Filter, with `col = “value”` and `col != “value”`, `>` and `<` where the column orders, and `is NULL` and `is not NULL` for a NULL cell, each only where the engine offers the operator (`CellFilters`). None for a binary value, a value over 10,000 characters, one with a blank at either end or one that reads as NULL, a new row or an edited cell. The rule joins the rules under Match all and applies at once; under Match any of two clauses it runs alone; a rule already running does nothing. A query result has no Filter, as on macOS. +- A table tab's cell menu has Filter, with `col = “value”` and `col != “value”`, `>` and `<` where the column orders, and `is NULL` and `is not NULL` for a NULL cell, each only where the engine offers the operator (`CellFilters`). The value is the core's filter text for the cell (`ResultSet.GetFilterText`), the text that binds back to the value stored, as a followed foreign key's filter is built; a value no text binds back to, such as 'true' in a SQLite BOOLEAN column, offers none. None either for a binary value, a value over 10,000 characters, one with a blank at either end or one that reads as NULL, a new row or an edited cell. The rule joins the rules under Match all and applies at once; under Match any of two clauses it runs alone; a rule already running does nothing. A query result has no Filter, as on macOS. ### Export -- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table or one column twice. Where each database holds schemas of its own, rows a query tab read after moving to another database leave nothing out, since the session's catalog describes its own database. +- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table or one column twice. A result that names one column twice is refused before any file. Where each database holds schemas of its own, rows a query tab read in another database leave nothing out, since the session's catalog describes its own database; the tab keeps the database its rows were read in (`QueryTab.ResultsDatabase`). - The export bar counts the rows written, read from the core's counter (`ExportProgress`) four times a second, out of the rows held or the counted total where known. Stop ends it with no file left, so it asks nothing first. ### Catalog operations diff --git a/windows/src/TablePro.App.Model/CellFilters.cs b/windows/src/TablePro.App.Model/CellFilters.cs index 0d42c15c89..3085629109 100644 --- a/windows/src/TablePro.App.Model/CellFilters.cs +++ b/windows/src/TablePro.App.Model/CellFilters.cs @@ -14,12 +14,14 @@ public static class CellFilters private static readonly FilterOperator[] ForValue = [FilterOperator.Equal, FilterOperator.NotEqual, FilterOperator.Greater, FilterOperator.Less]; - // None for a binary value, a value cut short or too long, one that starts or ends with a blank, which a filter - // row reads trimmed, one that reads as the NULL literal, and a cell holding an edit not saved yet. + // filterText is the core's text that binds back to the value stored, null where none does. None for a binary + // value, a value cut short or too long, one that starts or ends with a blank, which a filter row reads trimmed, one + // that reads as the NULL literal, and a cell holding an edit not saved yet. public static IReadOnlyList For( string column, IReadOnlyList offered, CellValue? value, + string? filterText, bool pending) { ArgumentNullException.ThrowIfNull(column); @@ -34,7 +36,7 @@ public static IReadOnlyList For( return [.. ForNull.Where(offered.Contains).Select(op => new FilterRule(column, op, null))]; } - var text = value.Text ?? string.Empty; + var text = filterText ?? string.Empty; if (value.TextCut || text.Length == 0 || text.Length > MaximumValueLength diff --git a/windows/src/TablePro.App.Model/QueryTab.cs b/windows/src/TablePro.App.Model/QueryTab.cs index 4f744e7d6b..7f26d434e6 100644 --- a/windows/src/TablePro.App.Model/QueryTab.cs +++ b/windows/src/TablePro.App.Model/QueryTab.cs @@ -176,6 +176,9 @@ public QueryTab( // Where each editor connection the tab opens runs; null for the connection's own. public string? Database { get; private set; } + // Where the rows shown were read, which a database picked since leaves as it was; null for the connection's own. + public string? ResultsDatabase { get; private set; } + // The tab's own, else the connection's, else the one the server listed as the login's. public string? ShownDatabase => Database ?? _session.Database ?? _listedDefault; @@ -707,6 +710,8 @@ private async Task AfterRunAsync() SetDatabase(now); } + ResultsDatabase = Database; + var state = _results.Transaction == TransactionState.Unknown ? Transaction : _results.Transaction; ShowTransaction(WorkspaceNotices.Holds(state) ? state : TransactionState.Idle); } diff --git a/windows/src/TablePro.App/Strings/en-US/Resources.resw b/windows/src/TablePro.App/Strings/en-US/Resources.resw index af1646c1ff..76251e1463 100644 --- a/windows/src/TablePro.App/Strings/en-US/Resources.resw +++ b/windows/src/TablePro.App/Strings/en-US/Resources.resw @@ -4096,10 +4096,10 @@ New fingerprint: {2} Cascade - Drop all tables that depend on this table + Also drop the views and foreign keys that depend on this table - Drop all tables that depend on this table: {0} + Also drop the views that depend on this table, and the foreign keys of {0} that point at it. Those tables and their rows stay. Cannot drop table @@ -4281,4 +4281,7 @@ New fingerprint: {2} {0} is not NULL + + SQL INSERT names each column once. Name the columns apart with AS and run the query again. + diff --git a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs index 7915374867..03fcd84515 100644 --- a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs @@ -110,8 +110,8 @@ private async void LoadRestoredFile(SqlFileStamp? stamp, bool unsaved) => // Set by the workspace that holds the tab: only a saved connection keeps queries. public bool CanSaveQuery { get; set; } - // Where the tab's editor runs, null for the connection's own; a typed USE moves it. - internal string? Database => _model.Database; + // Where the rows shown were read, null for the connection's own. + internal string? ResultsDatabase => _model.ResultsDatabase; public string Title => _model.Title; diff --git a/windows/src/TablePro.App/Views/TableTabView.xaml.cs b/windows/src/TablePro.App/Views/TableTabView.xaml.cs index e7fe44b708..f601c04761 100644 --- a/windows/src/TablePro.App/Views/TableTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/TableTabView.xaml.cs @@ -695,10 +695,23 @@ private async Task> CellRulesAsync(int row, int column return []; } + var filterText = value.IsNull ? null : FilterTextAt(row, column); var choices = _model.FilterChoices ?? await _model.LoadFilterChoicesAsync().ConfigureAwait(true); var offered = choices?.Columns .FirstOrDefault(choice => string.Equals(choice.Name, value.Column, StringComparison.Ordinal))?.Operators ?? []; - return CellFilters.For(value.Column, offered, value, pending); + return CellFilters.For(value.Column, offered, value, filterText, pending); + } + + private string? FilterTextAt(int row, int column) + { + try + { + return Results.ShownSet is { } rows && row < rows.RowCount ? rows.GetFilterText(row, column) : null; + } + catch (TableProException) + { + return null; + } } // As macOS titles them: the column, the operator and the value the rule compares with. diff --git a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs index b164691372..3aadb143a9 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs @@ -891,7 +891,7 @@ public async Task ExportAsync() request.Format, table, request.Path, - (tab as QueryTabView)?.Database, + (tab as QueryTabView)?.ResultsDatabase, progress, exporting.Token); } diff --git a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs index 55f6613b7f..ab1d7e7721 100644 --- a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs +++ b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs @@ -1292,6 +1292,8 @@ public struct UniffiVTableCallbackInterfaceRunListener + + @@ -2616,6 +2618,17 @@ public static extern RustBuffer uniffi_tablepro_ffi_fn_method_resultset_columns(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + RustBuffer uniffi_tablepro_ffi_fn_method_resultset_filter_text(ulong @ptr,ulong @row,uint @column,ref UniffiRustCallStatus _uniffi_out_err + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -5311,6 +5324,17 @@ public static extern ushort uniffi_tablepro_ffi_checksum_method_resultset_columns( ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_resultset_filter_text( + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -6896,6 +6920,12 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_resultset_columns` checksum `43715`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_resultset_filter_text(); + if (checksum != 51473) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_resultset_filter_text` checksum `51473`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_resultset_index(); if (checksum != 33552) { @@ -11670,6 +11700,8 @@ internal interface IResultSet { /// CellTextHead? CellTextHead(ulong @row, uint @column, uint @maxChars); ResultColumnInfo[] Columns(); + /// + string? FilterText(ulong @row, uint @column); /// /// Its place among the run's result sets, from 0. /// @@ -11873,6 +11905,15 @@ public ResultColumnInfo[] Columns() { } + /// + public string? FilterText(ulong @row, uint @column) { + return CallWithPointer(thisPtr => FfiConverterOptionalString.INSTANCE.Lift( + _UniffiHelpers.RustCallWithError(FfiConverterTypeCoreError.INSTANCE, (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_resultset_filter_text(thisPtr, FfiConverterUInt64.INSTANCE.Lower(@row), FfiConverterUInt32.INSTANCE.Lower(@column), ref _status) +))); + } + + /// /// Its place among the run's result sets, from 0. /// diff --git a/windows/src/TablePro.Interop/ResultSet.cs b/windows/src/TablePro.Interop/ResultSet.cs index 5c13d69c4d..95932e240b 100644 --- a/windows/src/TablePro.Interop/ResultSet.cs +++ b/windows/src/TablePro.Interop/ResultSet.cs @@ -36,6 +36,15 @@ internal ResultSet(Native.ResultSet set, int index) return CoreCall.Run(() => _set.CellText(checked((ulong)row), checked((uint)column))); } + // The text a filter on the cell's column binds back to the very value stored; null for SQL NULL and for a value no + // filter text reads back as, such as 'true' in a SQLite BOOLEAN column. + public string? GetFilterText(long row, int column) + { + ArgumentOutOfRangeException.ThrowIfNegative(row); + ArgumentOutOfRangeException.ThrowIfNegative(column); + return CoreCall.Run(() => _set.FilterText(checked((ulong)row), checked((uint)column))); + } + // A value's start for a viewer, read without copying the rest: its text with line breaks kept, or a binary // value's hex. Null for SQL NULL. public CellTextHead? GetCellTextHead(long row, int column, int maxCharacters) diff --git a/windows/tests/TablePro.App.Model.Tests/CellFiltersTests.cs b/windows/tests/TablePro.App.Model.Tests/CellFiltersTests.cs index 5a5515e709..71c1658203 100644 --- a/windows/tests/TablePro.App.Model.Tests/CellFiltersTests.cs +++ b/windows/tests/TablePro.App.Model.Tests/CellFiltersTests.cs @@ -20,8 +20,8 @@ public sealed class CellFiltersTests [Fact] public void AValueOffersEqualAndNotAndAnOrderedColumnGreaterAndLessToo() { - var number = CellFilters.For("Total", Ordered, CellValue.Of("Total", "9.99"), pending: false); - var text = CellFilters.For("Name", Textual, CellValue.Of("Name", "AC/DC"), pending: false); + var number = CellFilters.For("Total", Ordered, CellValue.Of("Total", "9.99"), "9.99", pending: false); + var text = CellFilters.For("Name", Textual, CellValue.Of("Name", "AC/DC"), "AC/DC", pending: false); Assert.Equal( [FilterOperator.Equal, FilterOperator.NotEqual, FilterOperator.Greater, FilterOperator.Less], @@ -33,7 +33,7 @@ public void AValueOffersEqualAndNotAndAnOrderedColumnGreaterAndLessToo() [Fact] public void ANullCellOffersTheNullTests() { - var rules = CellFilters.For("Composer", Textual, CellValue.Of("Composer", null), pending: false); + var rules = CellFilters.For("Composer", Textual, CellValue.Of("Composer", null), null, pending: false); Assert.Equal([FilterOperator.IsNull, FilterOperator.IsNotNull], rules.Select(rule => rule.Operator)); Assert.All(rules, rule => Assert.Null(rule.Value)); @@ -45,18 +45,19 @@ public void ANullCellOffersTheNullTests() [InlineData("null")] [InlineData("")] public void AValueAFilterRowWouldReadOtherwiseOffersNothing(string text) => - Assert.Empty(CellFilters.For("Name", Textual, CellValue.Of("Name", text), pending: false)); + Assert.Empty(CellFilters.For("Name", Textual, CellValue.Of("Name", text), text, pending: false)); [Fact] - public void ABinaryValueALongValueAndAPendingEditOfferNothing() + public void ABinaryValueALongValueAPendingEditAndAValueNoFilterBindsOfferNothing() { var bytes = new CellValue("Data", null, false, new byte[] { 1, 2 }, 2, IsDefault: false); var longText = CellValue.Of("Name", new string('x', CellFilters.MaximumValueLength + 1)); - Assert.Empty(CellFilters.For("Data", Textual, bytes, pending: false)); - Assert.Empty(CellFilters.For("Name", Textual, longText, pending: false)); - Assert.Empty(CellFilters.For("Name", Textual, CellValue.Of("Name", "typed"), pending: true)); - Assert.Empty(CellFilters.For("Name", Textual, null, pending: false)); + Assert.Empty(CellFilters.For("Data", Textual, bytes, "0x0102", pending: false)); + Assert.Empty(CellFilters.For("Name", Textual, longText, longText.Text, pending: false)); + Assert.Empty(CellFilters.For("Name", Textual, CellValue.Of("Name", "typed"), "typed", pending: true)); + Assert.Empty(CellFilters.For("Name", Textual, null, null, pending: false)); + Assert.Empty(CellFilters.For("Active", Textual, CellValue.Of("Active", "true"), null, pending: false)); } [Fact] diff --git a/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs b/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs index 51afaae49e..4d5109cfe3 100644 --- a/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs +++ b/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs @@ -137,6 +137,22 @@ public async Task APickMovesTheOpenEditorSoItKeepsItsConnection() Assert.False(tab.IsRunning); } + [Fact] + public async Task TheRowsShownKeepTheDatabaseTheyWereReadInUntilTheNextRun() + { + var tab = Tab("shop"); + await tab.RunAsync(); + + await tab.PickDatabaseAsync("inventory"); + + Assert.Equal("inventory", tab.Database); + Assert.Equal("shop", tab.ResultsDatabase); + + await tab.RunAsync(); + + Assert.Equal("inventory", tab.ResultsDatabase); + } + // The rows came from the database before the switch, so running the statement again would read another's. [Fact] public async Task ASortAfterASwitchOrdersTheRowsHeldInsteadOfRunningAgain() From 5bc259833188197e34676a69bffe94cd66631ae0 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 10:07:50 +0700 Subject: [PATCH 03/16] fix(windows): name another database for rows a run read while moving between databases, read a cell's filter text only when it is offered --- ...tions-table-filters-and-export-progress.md | 2 +- windows/src/TablePro.App.Model/QueryTab.cs | 6 ++++- .../TablePro.App/Views/TableTabView.xaml.cs | 3 ++- .../QueryTabDatabaseTests.cs | 22 +++++++++++++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md index a028299e44..a92b6d8d74 100644 --- a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -17,7 +17,7 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) ### Export -- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table or one column twice. A result that names one column twice is refused before any file. Where each database holds schemas of its own, rows a query tab read in another database leave nothing out, since the session's catalog describes its own database; the tab keeps the database its rows were read in (`QueryTab.ResultsDatabase`). +- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table or one column twice. A result that names one column twice is refused before any file. Where each database holds schemas of its own, rows a query tab read in another database leave nothing out, since the session's catalog describes its own database; the tab keeps the database its rows were read in, or one that is not the connection's own where a run moved between databases (`QueryTab.ResultsDatabase`). - The export bar counts the rows written, read from the core's counter (`ExportProgress`) four times a second, out of the rows held or the counted total where known. Stop ends it with no file left, so it asks nothing first. ### Catalog operations diff --git a/windows/src/TablePro.App.Model/QueryTab.cs b/windows/src/TablePro.App.Model/QueryTab.cs index 7f26d434e6..80b4fbc5a4 100644 --- a/windows/src/TablePro.App.Model/QueryTab.cs +++ b/windows/src/TablePro.App.Model/QueryTab.cs @@ -177,6 +177,7 @@ public QueryTab( public string? Database { get; private set; } // Where the rows shown were read, which a database picked since leaves as it was; null for the connection's own. + // A run that moved between databases names one that is not the connection's own. public string? ResultsDatabase { get; private set; } // The tab's own, else the connection's, else the one the server listed as the login's. @@ -627,6 +628,7 @@ private async Task RunOnEditorAsync(string sql, bool record, CancellationToken c var startedAt = DateTimeOffset.Now; var started = Stopwatch.GetTimestamp(); var settings = _settings(); + var readIn = Database; _run = _editor.Run(sql, RunOptions(settings), cancellationToken); await _results.ShowRunAsync(_run, _sort, int.MaxValue, settings.ResultSetsKeptPerRun, cancellationToken) .ConfigureAwait(true); @@ -638,6 +640,9 @@ await _results.ShowRunAsync(_run, _sort, int.MaxValue, settings.ResultSetsKeptPe } await AfterRunAsync().ConfigureAwait(true); + // A typed USE can move the editor between the batches that read the rows, so another database than + // the connection's own is named whenever the run touched one. + ResultsDatabase = Database ?? readIn; } } catch (TableProException exception) @@ -710,7 +715,6 @@ private async Task AfterRunAsync() SetDatabase(now); } - ResultsDatabase = Database; var state = _results.Transaction == TransactionState.Unknown ? Transaction : _results.Transaction; ShowTransaction(WorkspaceNotices.Holds(state) ? state : TransactionState.Idle); diff --git a/windows/src/TablePro.App/Views/TableTabView.xaml.cs b/windows/src/TablePro.App/Views/TableTabView.xaml.cs index f601c04761..4c57559b3b 100644 --- a/windows/src/TablePro.App/Views/TableTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/TableTabView.xaml.cs @@ -695,7 +695,8 @@ private async Task> CellRulesAsync(int row, int column return []; } - var filterText = value.IsNull ? null : FilterTextAt(row, column); + // The core reads the whole stored value, so a value the menu would refuse anyway is never read whole. + var filterText = value is { IsNull: false, Bytes: null, TextCut: false } ? FilterTextAt(row, column) : null; var choices = _model.FilterChoices ?? await _model.LoadFilterChoicesAsync().ConfigureAwait(true); var offered = choices?.Columns .FirstOrDefault(choice => string.Equals(choice.Name, value.Column, StringComparison.Ordinal))?.Operators ?? []; diff --git a/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs b/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs index 4d5109cfe3..846a12e0ed 100644 --- a/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs +++ b/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs @@ -153,6 +153,28 @@ public async Task TheRowsShownKeepTheDatabaseTheyWereReadInUntilTheNextRun() Assert.Equal("inventory", tab.ResultsDatabase); } + [Fact] + public async Task ARunThatMovedBackToTheConnectionsOwnDatabaseNamesTheOneItLeft() + { + _session.Database = "shop"; + _session.Script = sql => + { + if (sql.EndsWith("use shop", StringComparison.Ordinal)) + { + _session.Editors[^1].Database = "shop"; + } + + return FakeRun.Rows(1); + }; + var tab = Tab("inventory"); + _view.WholeText = "select * from Artist; use shop"; + + await tab.RunAsync(); + + Assert.Null(tab.Database); + Assert.Equal("inventory", tab.ResultsDatabase); + } + // The rows came from the database before the switch, so running the statement again would read another's. [Fact] public async Task ASortAfterASwitchOrdersTheRowsHeldInsteadOfRunningAgain() From 286ce7bec64070cb1cc968aff527cced1095ff78 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 10:18:50 +0700 Subject: [PATCH 04/16] fix(windows): read no catalog for a result where databases hold their own schemas, close tabs of every dropped object, reload filter choices after a refresh --- core/crates/ffi/src/export.rs | 31 ++++----- core/crates/ffi/tests/export.rs | 64 +++---------------- ...tions-table-filters-and-export-progress.md | 4 +- windows/src/TablePro.App.Model/QueryTab.cs | 9 --- .../TablePro.App/Views/QueryTabView.xaml.cs | 3 - .../TablePro.App/Views/TableTabView.xaml.cs | 3 +- .../Views/WorkspaceView.Catalog.cs | 48 ++++++++++++-- .../TablePro.App/Views/WorkspaceView.xaml.cs | 9 +-- .../DatabaseSession.Export.cs | 5 +- .../Generated/tablepro_ffi.cs | 12 ++-- .../QueryTabDatabaseTests.cs | 38 ----------- 11 files changed, 79 insertions(+), 147 deletions(-) diff --git a/core/crates/ffi/src/export.rs b/core/crates/ffi/src/export.rs index e548a58d43..b3814bccb7 100644 --- a/core/crates/ffi/src/export.rs +++ b/core/crates/ffi/src/export.rs @@ -71,11 +71,10 @@ pub enum ExportFormatTag { #[uniffi::export] impl DatabaseSession { // Only the first `visible_columns` columns, since a table page carries row addresses after them. SQL INSERT with no - // table writes into the one table every column was read from, else into `query_results`. `database` is where the - // rows were read when that is not the session's own, as after a USE. + // table writes into the one table every column was read from, else into `query_results`. #[expect( clippy::too_many_arguments, - reason = "the rows, their format, table, database and file, and the app's progress and cancel" + reason = "the rows, their format, table and file, and the app's progress and cancel, as the app passes them" )] pub async fn export_rows( &self, @@ -83,7 +82,6 @@ impl DatabaseSession { visible_columns: u32, format: ExportFormatTag, table: Option, - database: Option, path: String, progress: Option>, cancel: Option>, @@ -120,10 +118,9 @@ impl DatabaseSession { Some((source, named)) => { // Read only to leave out what the server computes; a table that cannot be read leaves none out. let layout = session.current_dialect().map(|dialect| dialect.database_layout()); - let elsewhere = !catalog_describes(database.as_deref(), layout); - let catalog = match elsewhere { - true => None, - false => tokio::select! { + let catalog = match catalog_describes(layout) { + false => None, + true => tokio::select! { biased; () = cancel.cancelled() => return Err(CoreError::Cancelled), columns = session.columns(source.clone()) => columns.ok(), @@ -520,10 +517,10 @@ fn repeats_a_name(columns: &[ResultColumn]) -> bool { .any(|(index, column)| columns[..index].iter().any(|before| before.name == column.name)) } -// Where each database holds schemas of its own, an origin names no database, so the session's catalog describes the -// table only when the rows were read in the session's own database. `read_in` is None there. -fn catalog_describes(read_in: Option<&str>, layout: Option) -> bool { - read_in.is_none() || layout != Some(DatabaseLayout::WithSchemas) +// Where each database holds schemas of its own, an origin names no database, and an editor can read in another one +// than the session's, so the session's catalog may describe another table of the same name. +fn catalog_describes(layout: Option) -> bool { + layout != Some(DatabaseLayout::WithSchemas) } // The table every column was read from, as the driver traced it, with each column under the name the table gives it, @@ -847,11 +844,11 @@ mod tests { } #[test] - fn a_result_read_after_a_move_to_another_database_with_schemas_of_its_own_reads_no_catalog() { - assert!(catalog_describes(None, Some(DatabaseLayout::WithSchemas))); - assert!(!catalog_describes(Some("sales"), Some(DatabaseLayout::WithSchemas))); - assert!(catalog_describes(Some("sales"), Some(DatabaseLayout::AsSchemas))); - assert!(catalog_describes(Some("sales"), Some(DatabaseLayout::Single))); + fn a_result_where_each_database_holds_schemas_of_its_own_reads_no_catalog() { + assert!(!catalog_describes(Some(DatabaseLayout::WithSchemas))); + assert!(catalog_describes(Some(DatabaseLayout::AsSchemas))); + assert!(catalog_describes(Some(DatabaseLayout::Single))); + assert!(catalog_describes(None)); } #[test] diff --git a/core/crates/ffi/tests/export.rs b/core/crates/ffi/tests/export.rs index 8e59186ee9..9b0c398306 100644 --- a/core/crates/ffi/tests/export.rs +++ b/core/crates/ffi/tests/export.rs @@ -129,20 +129,11 @@ async fn csv_and_json_of_a_result_set_hold_exactly_its_rows() { std::fs::write(&csv, "an older export").unwrap(); let csv_rows = session - .export_rows( - set.clone(), - 4, - ExportFormatTag::Csv, - None, - None, - spelled(&csv), - None, - None, - ) + .export_rows(set.clone(), 4, ExportFormatTag::Csv, None, spelled(&csv), None, None) .await .unwrap(); let json_rows = session - .export_rows(set, 4, ExportFormatTag::Json, None, None, spelled(&json), None, None) + .export_rows(set, 4, ExportFormatTag::Json, None, spelled(&json), None, None) .await .unwrap(); @@ -174,7 +165,7 @@ async fn an_excel_export_is_a_workbook() { let path = dir.path().join("people.xlsx"); let rows = session - .export_rows(set, 4, ExportFormatTag::Excel, None, None, spelled(&path), None, None) + .export_rows(set, 4, ExportFormatTag::Excel, None, spelled(&path), None, None) .await .unwrap(); @@ -215,7 +206,6 @@ async fn a_page_exports_only_its_visible_columns() { visible, ExportFormatTag::Csv, Some(table("notes")), - None, spelled(&path), None, None, @@ -269,16 +259,7 @@ async fn sql_insert_of_a_query_result_writes_into_the_table_its_columns_came_fro let path = dir.path().join("people.sql"); let rows = session - .export_rows( - set, - 2, - ExportFormatTag::SqlInsert, - None, - None, - spelled(&path), - None, - None, - ) + .export_rows(set, 2, ExportFormatTag::SqlInsert, None, spelled(&path), None, None) .await .unwrap(); @@ -300,16 +281,7 @@ async fn sql_insert_of_an_aliased_result_names_the_tables_columns() { let path = dir.path().join("aliased.sql"); session - .export_rows( - set, - 2, - ExportFormatTag::SqlInsert, - None, - None, - spelled(&path), - None, - None, - ) + .export_rows(set, 2, ExportFormatTag::SqlInsert, None, spelled(&path), None, None) .await .unwrap(); @@ -330,16 +302,7 @@ async fn sql_insert_of_a_result_naming_a_column_twice_is_refused_before_any_file let path = dir.path().join("pairs.sql"); let error = session - .export_rows( - set, - 2, - ExportFormatTag::SqlInsert, - None, - None, - spelled(&path), - None, - None, - ) + .export_rows(set, 2, ExportFormatTag::SqlInsert, None, spelled(&path), None, None) .await .unwrap_err(); @@ -357,16 +320,7 @@ async fn sql_insert_of_a_computed_result_writes_into_query_results() { let path = dir.path().join("loud.sql"); session - .export_rows( - set, - 2, - ExportFormatTag::SqlInsert, - None, - None, - spelled(&path), - None, - None, - ) + .export_rows(set, 2, ExportFormatTag::SqlInsert, None, spelled(&path), None, None) .await .unwrap(); @@ -475,7 +429,7 @@ async fn a_failed_export_keeps_the_file_it_would_have_replaced() { std::fs::write(&path, "an older export").unwrap(); let error = session - .export_rows(set, 2, ExportFormatTag::Xml, None, None, spelled(&path), None, None) + .export_rows(set, 2, ExportFormatTag::Xml, None, spelled(&path), None, None) .await .unwrap_err(); @@ -510,7 +464,7 @@ async fn rows_still_arriving_are_not_exported() { let path = spelled(&dir.path().join("endless.csv")); let refused = session - .export_rows(set, 1, ExportFormatTag::Csv, None, None, path, None, None) + .export_rows(set, 1, ExportFormatTag::Csv, None, path, None, None) .await .unwrap_err(); run.cancel(); diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md index a92b6d8d74..b0809515ce 100644 --- a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -17,7 +17,7 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) ### Export -- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table or one column twice. A result that names one column twice is refused before any file. Where each database holds schemas of its own, rows a query tab read in another database leave nothing out, since the session's catalog describes its own database; the tab keeps the database its rows were read in, or one that is not the connection's own where a run moved between databases (`QueryTab.ResultsDatabase`). +- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table or one column twice. A result that names one column twice is refused before any file. Where each database holds schemas of its own, an origin names no database and an editor can read in another one, so the session's catalog is not read and nothing is left out: a computed column then makes the script fail where it runs rather than lose a value. - The export bar counts the rows written, read from the core's counter (`ExportProgress`) four times a second, out of the rows held or the counted total where known. Stop ends it with no file left, so it asks nothing first. ### Catalog operations @@ -26,7 +26,7 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) - Each dialog shows the statements the core planned for its options (`PlanCatalogChange`), planned again as an option changes: Cascade and Restart identity for a truncate, Cascade for a drop, Close other connections first for a database, Run on for a cluster, and for a new database the options the engine names (`CatalogAbilities.DatabaseFields`) with the server's choices. - A truncate where the server refuses a table other tables' keys point at is refused first, naming them; so is a drop where the engine has no Cascade. - Safe Mode gates them as the core says (`CatalogChange.Gate`): emptying or dropping is asked at every level, in the dialog that shows the statements; creating is asked again where Confirm writes asks; Read only refuses. Each runs at once, as the Linux app's do. -- After a drop, the table's tabs close without asking, since their changes have nowhere to go, and are not kept to reopen. A truncate reads its table's tabs again, every table tab with Cascade. A change whose outcome is unknown reads the tree again and closes nothing. The tree reads the schemas again on every refresh. +- After a drop, the table's tabs close without asking, since their changes have nowhere to go, and are not kept to reopen; so do the tabs of every object the catalog no longer lists, as a partitioned table's partitions and the views Cascade drops. A truncate reads every table tab again, since Cascade and a partitioned table empty others; a tab holding edits asks first. A change whose outcome is unknown reads the tree again and closes nothing. The tree reads the schemas again on every refresh. ## Rationale diff --git a/windows/src/TablePro.App.Model/QueryTab.cs b/windows/src/TablePro.App.Model/QueryTab.cs index 80b4fbc5a4..4f744e7d6b 100644 --- a/windows/src/TablePro.App.Model/QueryTab.cs +++ b/windows/src/TablePro.App.Model/QueryTab.cs @@ -176,10 +176,6 @@ public QueryTab( // Where each editor connection the tab opens runs; null for the connection's own. public string? Database { get; private set; } - // Where the rows shown were read, which a database picked since leaves as it was; null for the connection's own. - // A run that moved between databases names one that is not the connection's own. - public string? ResultsDatabase { get; private set; } - // The tab's own, else the connection's, else the one the server listed as the login's. public string? ShownDatabase => Database ?? _session.Database ?? _listedDefault; @@ -628,7 +624,6 @@ private async Task RunOnEditorAsync(string sql, bool record, CancellationToken c var startedAt = DateTimeOffset.Now; var started = Stopwatch.GetTimestamp(); var settings = _settings(); - var readIn = Database; _run = _editor.Run(sql, RunOptions(settings), cancellationToken); await _results.ShowRunAsync(_run, _sort, int.MaxValue, settings.ResultSetsKeptPerRun, cancellationToken) .ConfigureAwait(true); @@ -640,9 +635,6 @@ await _results.ShowRunAsync(_run, _sort, int.MaxValue, settings.ResultSetsKeptPe } await AfterRunAsync().ConfigureAwait(true); - // A typed USE can move the editor between the batches that read the rows, so another database than - // the connection's own is named whenever the run touched one. - ResultsDatabase = Database ?? readIn; } } catch (TableProException exception) @@ -715,7 +707,6 @@ private async Task AfterRunAsync() SetDatabase(now); } - var state = _results.Transaction == TransactionState.Unknown ? Transaction : _results.Transaction; ShowTransaction(WorkspaceNotices.Holds(state) ? state : TransactionState.Idle); } diff --git a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs index 03fcd84515..a7c116e02c 100644 --- a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs @@ -110,9 +110,6 @@ private async void LoadRestoredFile(SqlFileStamp? stamp, bool unsaved) => // Set by the workspace that holds the tab: only a saved connection keeps queries. public bool CanSaveQuery { get; set; } - // Where the rows shown were read, null for the connection's own. - internal string? ResultsDatabase => _model.ResultsDatabase; - public string Title => _model.Title; public TabKind Kind => _model.Kind; diff --git a/windows/src/TablePro.App/Views/TableTabView.xaml.cs b/windows/src/TablePro.App/Views/TableTabView.xaml.cs index 4c57559b3b..34ccc75905 100644 --- a/windows/src/TablePro.App/Views/TableTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/TableTabView.xaml.cs @@ -697,7 +697,8 @@ private async Task> CellRulesAsync(int row, int column // The core reads the whole stored value, so a value the menu would refuse anyway is never read whole. var filterText = value is { IsNull: false, Bytes: null, TextCut: false } ? FilterTextAt(row, column) : null; - var choices = _model.FilterChoices ?? await _model.LoadFilterChoicesAsync().ConfigureAwait(true); + // Kept until a refresh makes them stale, so this reads them only the first time and after a refresh. + var choices = await _model.LoadFilterChoicesAsync().ConfigureAwait(true); var offered = choices?.Columns .FirstOrDefault(choice => string.Equals(choice.Name, value.Column, StringComparison.Ordinal))?.Operators ?? []; return CellFilters.For(value.Column, offered, value, filterText, pending); diff --git a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs index 8dd5ffa321..82548143e5 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs @@ -107,10 +107,9 @@ await CatalogDialogs.ShowRefusedAsync( return; } - // A cascade empties the tables pointing at this one too, so every table tab reads again. - var cascaded = change.Operation is CatalogOperation.TruncateTable { Cascade: true }; - var views = TableViews().Where(view => cascaded || view.Shows(entry.Schema, entry.Name)).ToList(); - foreach (var view in views) + // A cascade empties the tables pointing at this one, and a partitioned table its partitions, so every table tab + // reads again; one holding edits asks first. + foreach (var view in TableViews()) { if (TableViews().Contains(view)) { @@ -148,6 +147,9 @@ await CatalogDialogs.ShowRefusedAsync( await DiscardTabsAsync(view => view.Shows(entry.Schema, entry.Name)).ConfigureAwait(true); _workbench.TableFilters.ForgetTable(FilterConnection, session.Database, table); + // A partitioned table takes its partitions with it, and Cascade the views on it, so every tab whose object the + // catalog no longer lists goes too. + await DiscardGoneTabsAsync(session).ConfigureAwait(true); await RefreshAsync().ConfigureAwait(true); if (entry.Schema is { } schema && !_singleSchema) { @@ -303,6 +305,44 @@ private static async Task ChoicesAsync(DatabaseSession session) private List TableViews() => [.. Tabs.TabItems.OfType().Select(item => item.Content).OfType()]; + // The objects of each schema a table tab shows, read again; a schema that cannot be read closes nothing. + private async Task DiscardGoneTabsAsync(DatabaseSession session) + { + HashSet<(string? Schema, string Name)> gone = []; + foreach (var schema in TableViews().Select(view => view.Model.Table.Schema).Distinct()) + { + IReadOnlyList objects; + try + { + var scope = schema is null ? ObjectScope.DefaultSchema : ObjectScope.Schema(schema); + objects = await session.RefreshObjectsAsync(scope).ConfigureAwait(true); + } + catch (TableProException) + { + continue; + } + + foreach (var view in TableViews().Where(view => view.Model.Table.Schema == schema)) + { + var table = view.Model.Table; + // A tab opened from the tree and one restored can spell the default schema apart, so a name with no + // schema on either side is the same object. + if (!objects.Any(entry => entry.Name == table.Name + && (entry.Schema is null || schema is null || entry.Schema == schema))) + { + gone.Add((table.Schema, table.Name)); + } + } + } + + foreach (var (schema, name) in gone) + { + _workbench.TableFilters.ForgetTable(FilterConnection, session.Database, new TableName(schema, name)); + } + + await DiscardTabsAsync(view => gone.Contains((view.Model.Table.Schema, view.Model.Table.Name))).ConfigureAwait(true); + } + // The table is gone, so its tabs close without asking about their changes, and none is kept to reopen. private async Task DiscardTabsAsync(Func shows) { diff --git a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs index 3aadb143a9..330f2f6a5a 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs @@ -886,14 +886,7 @@ public async Task ExportAsync() { total = rows.RowCount; export = share.Session.ExportRowsAsync( - rows, - results.Columns.Count, - request.Format, - table, - request.Path, - (tab as QueryTabView)?.ResultsDatabase, - progress, - exporting.Token); + rows, results.Columns.Count, request.Format, table, request.Path, progress, exporting.Token); } else { diff --git a/windows/src/TablePro.Interop/DatabaseSession.Export.cs b/windows/src/TablePro.Interop/DatabaseSession.Export.cs index 130c98de26..cb48227dd6 100644 --- a/windows/src/TablePro.Interop/DatabaseSession.Export.cs +++ b/windows/src/TablePro.Interop/DatabaseSession.Export.cs @@ -8,15 +8,13 @@ public sealed partial class DatabaseSession private static readonly TimeSpan ProgressInterval = TimeSpan.FromMilliseconds(250); // Only the first visibleColumns columns, since a table page carries row addresses after them. SqlInsert with no - // table writes into the one table every column was read from, else into query_results; database names where the - // rows were read when that is not the session's own. + // table writes into the one table every column was read from, else into query_results. public async Task ExportRowsAsync( ResultSet rows, int visibleColumns, ExportFormat format, TableName? table, string path, - string? database = null, IProgress? progress = null, CancellationToken cancellationToken = default) { @@ -34,7 +32,6 @@ public async Task ExportRowsAsync( checked((uint)visibleColumns), NativeFormat(format), target, - database, path, counter, token), diff --git a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs index ab1d7e7721..142c566955 100644 --- a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs +++ b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs @@ -3165,7 +3165,7 @@ public static partial [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] public static extern #endif - ulong uniffi_tablepro_ffi_fn_method_databasesession_export_rows(ulong @ptr,ulong @rows,uint @visibleColumns,RustBuffer @format,RustBuffer @table,RustBuffer @database,RustBuffer @path,RustBuffer @progress,RustBuffer @cancel + ulong uniffi_tablepro_ffi_fn_method_databasesession_export_rows(ulong @ptr,ulong @rows,uint @visibleColumns,RustBuffer @format,RustBuffer @table,RustBuffer @path,RustBuffer @progress,RustBuffer @cancel ); #if NET8_0_OR_GREATER @@ -7156,8 +7156,8 @@ static void uniffiCheckApiChecksums() { } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_export_rows(); - if (checksum != 59377) { - throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_export_rows` checksum `59377`, library returned `{checksum}`"); + if (checksum != 60707) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_export_rows` checksum `60707`, library returned `{checksum}`"); } } { @@ -9532,7 +9532,7 @@ internal interface IDatabaseSession { /// Task RefreshSchemas(CancelToken? @cancel); /// - Task ExportRows(ResultSet @rows, uint @visibleColumns, ExportFormatTag @format, TableName? @table, string? @database, string @path, ExportProgress? @progress, CancelToken? @cancel); + Task ExportRows(ResultSet @rows, uint @visibleColumns, ExportFormatTag @format, TableName? @table, string @path, ExportProgress? @progress, CancelToken? @cancel); /// Task ExportTable(TableName @table, ColumnSort? @sort, TableFilterArg? @filter, ExportFormatTag @format, string @path, ExportProgress? @progress, CancelToken? @cancel); /// @@ -9751,11 +9751,11 @@ public async Task RefreshSchemas(CancelToken? @cancel) { } /// - public async Task ExportRows(ResultSet @rows, uint @visibleColumns, ExportFormatTag @format, TableName? @table, string? @database, string @path, ExportProgress? @progress, CancelToken? @cancel) { + public async Task ExportRows(ResultSet @rows, uint @visibleColumns, ExportFormatTag @format, TableName? @table, string @path, ExportProgress? @progress, CancelToken? @cancel) { return await _UniFFIAsync.UniffiRustCallAsync( // Get rust future CallWithPointer(thisPtr => { - return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_export_rows(thisPtr, FfiConverterTypeResultSet.INSTANCE.Lower(@rows), FfiConverterUInt32.INSTANCE.Lower(@visibleColumns), FfiConverterTypeExportFormatTag.INSTANCE.Lower(@format), FfiConverterOptionalTypeTableName.INSTANCE.Lower(@table), FfiConverterOptionalString.INSTANCE.Lower(@database), FfiConverterString.INSTANCE.Lower(@path), FfiConverterOptionalTypeExportProgress.INSTANCE.Lower(@progress), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); + return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_export_rows(thisPtr, FfiConverterTypeResultSet.INSTANCE.Lower(@rows), FfiConverterUInt32.INSTANCE.Lower(@visibleColumns), FfiConverterTypeExportFormatTag.INSTANCE.Lower(@format), FfiConverterOptionalTypeTableName.INSTANCE.Lower(@table), FfiConverterString.INSTANCE.Lower(@path), FfiConverterOptionalTypeExportProgress.INSTANCE.Lower(@progress), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); }), // Poll (ulong future, IntPtr continuation, ulong data) => _UniFFILib.ffi_tablepro_ffi_rust_future_poll_u64(future, continuation, data), diff --git a/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs b/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs index 846a12e0ed..51afaae49e 100644 --- a/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs +++ b/windows/tests/TablePro.App.Model.Tests/QueryTabDatabaseTests.cs @@ -137,44 +137,6 @@ public async Task APickMovesTheOpenEditorSoItKeepsItsConnection() Assert.False(tab.IsRunning); } - [Fact] - public async Task TheRowsShownKeepTheDatabaseTheyWereReadInUntilTheNextRun() - { - var tab = Tab("shop"); - await tab.RunAsync(); - - await tab.PickDatabaseAsync("inventory"); - - Assert.Equal("inventory", tab.Database); - Assert.Equal("shop", tab.ResultsDatabase); - - await tab.RunAsync(); - - Assert.Equal("inventory", tab.ResultsDatabase); - } - - [Fact] - public async Task ARunThatMovedBackToTheConnectionsOwnDatabaseNamesTheOneItLeft() - { - _session.Database = "shop"; - _session.Script = sql => - { - if (sql.EndsWith("use shop", StringComparison.Ordinal)) - { - _session.Editors[^1].Database = "shop"; - } - - return FakeRun.Rows(1); - }; - var tab = Tab("inventory"); - _view.WholeText = "select * from Artist; use shop"; - - await tab.RunAsync(); - - Assert.Null(tab.Database); - Assert.Equal("inventory", tab.ResultsDatabase); - } - // The rows came from the database before the switch, so running the statement again would read another's. [Fact] public async Task ASortAfterASwitchOrdersTheRowsHeldInsteadOfRunningAgain() From 80323e8038d137511e0d7d6bb0bbe87fb4e50e26 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 10:25:58 +0700 Subject: [PATCH 05/16] fix(windows): hold a catalog action to the window's session, clean up tabs after a cascading schema drop, refuse INSERT names equal but for case --- core/crates/ffi/src/export.rs | 17 +++-- ...tions-table-filters-and-export-progress.md | 2 +- .../src/TablePro.App/Views/CatalogDialogs.cs | 6 ++ .../Views/WorkspaceView.Catalog.cs | 63 +++++++++++++++++-- 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/core/crates/ffi/src/export.rs b/core/crates/ffi/src/export.rs index b3814bccb7..d8a3ae3b27 100644 --- a/core/crates/ffi/src/export.rs +++ b/core/crates/ffi/src/export.rs @@ -510,11 +510,11 @@ impl ResultSink for FileSink { } } +// Ignoring case, since SQLite, MySQL and SQL Server read two names that differ only in case as one column, and the +// second value would be dropped without a word. fn repeats_a_name(columns: &[ResultColumn]) -> bool { - columns - .iter() - .enumerate() - .any(|(index, column)| columns[..index].iter().any(|before| before.name == column.name)) + let mut seen = std::collections::HashSet::with_capacity(columns.len()); + columns.iter().any(|column| !seen.insert(column.name.to_lowercase())) } // Where each database holds schemas of its own, an origin names no database, and an editor can read in another one @@ -843,6 +843,15 @@ mod tests { }) } + #[test] + fn two_names_that_differ_only_in_case_are_one_column() { + let named = |name: &str| array_of(name, bigint()); + + assert!(repeats_a_name(&[named("a"), named("A")])); + assert!(repeats_a_name(&[named("id"), named("name"), named("id")])); + assert!(!repeats_a_name(&[named("id"), named("name")])); + } + #[test] fn a_result_where_each_database_holds_schemas_of_its_own_reads_no_catalog() { assert!(!catalog_describes(Some(DatabaseLayout::WithSchemas))); diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md index b0809515ce..d9a23d0227 100644 --- a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -17,7 +17,7 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) ### Export -- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table or one column twice. A result that names one column twice is refused before any file. Where each database holds schemas of its own, an origin names no database and an editor can read in another one, so the session's catalog is not read and nothing is left out: a computed column then makes the script fail where it runs rather than lose a value. +- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table or one column twice. A result that names one column twice, case aside, is refused before any file. Where each database holds schemas of its own, an origin names no database and an editor can read in another one, so the session's catalog is not read and nothing is left out: a computed column then makes the script fail where it runs rather than lose a value. - The export bar counts the rows written, read from the core's counter (`ExportProgress`) four times a second, out of the rows held or the counted total where known. Stop ends it with no file left, so it asks nothing first. ### Catalog operations diff --git a/windows/src/TablePro.App/Views/CatalogDialogs.cs b/windows/src/TablePro.App/Views/CatalogDialogs.cs index 2df0cc64d0..d4cfcd7fee 100644 --- a/windows/src/TablePro.App/Views/CatalogDialogs.cs +++ b/windows/src/TablePro.App/Views/CatalogDialogs.cs @@ -327,6 +327,12 @@ void Refresh() _statements.Text = Strings.Error(exception.Error); _dialog.IsPrimaryButtonEnabled = false; } + catch (ObjectDisposedException) + { + // The window let go of its session while the question was open. + _statements.Text = string.Empty; + _dialog.IsPrimaryButtonEnabled = false; + } } foreach (var watch in _watches) diff --git a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs index 82548143e5..79b3476268 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs @@ -29,6 +29,11 @@ public async Task NewSchemaAsync() var abilities = Abilities; var choices = abilities.SchemaOwner ? await ChoicesAsync(session).ConfigureAwait(true) : DatabaseChoices.None; + if (!Holds(session)) + { + return; + } + using var change = await CatalogDialogs.AskNewSchemaAsync(this, session, abilities, choices).ConfigureAwait(true); if (change is null || !await RunCatalogChangeAsync(session, change, "CreateSchemaFailed").ConfigureAwait(true)) { @@ -51,6 +56,11 @@ public async Task NewDatabaseAsync() var abilities = Abilities; var choices = await ChoicesAsync(session).ConfigureAwait(true); + if (!Holds(session)) + { + return; + } + using var change = await CatalogDialogs.AskNewDatabaseAsync(this, session, abilities, choices).ConfigureAwait(true); if (change is null || !await RunCatalogChangeAsync(session, change, abilities.Keyspaces ? "CreateKeyspaceFailed" : "CreateDatabaseFailed") @@ -65,7 +75,7 @@ public async Task NewDatabaseAsync() } // As macOS does, the window goes on in the new database. - if (CreatedName(change) is { } name && ReferenceEquals(_session, session)) + if (CreatedName(change) is { } name && Holds(session)) { PickDatabase(session, name); } @@ -90,6 +100,11 @@ private async Task TruncateAsync(ObjectEntry entry) var table = new TableName(entry.Schema, entry.Name); var abilities = Abilities; var referencing = await ReferencingAsync(session, table).ConfigureAwait(true); + if (!Holds(session)) + { + return; + } + if (referencing.Count > 0 && abilities.TruncateRefusedWhenReferenced) { await CatalogDialogs.ShowRefusedAsync( @@ -101,6 +116,11 @@ await CatalogDialogs.ShowRefusedAsync( } var clusters = abilities.OnCluster ? (await ChoicesAsync(session).ConfigureAwait(true)).Clusters : []; + if (!Holds(session)) + { + return; + } + using var change = await CatalogDialogs.AskTruncateAsync(this, session, table, abilities, clusters).ConfigureAwait(true); if (change is null || !await RunCatalogChangeAsync(session, change, "TruncateFailed").ConfigureAwait(true)) { @@ -128,6 +148,11 @@ private async Task DropTableAsync(ObjectEntry entry) var table = new TableName(entry.Schema, entry.Name); var abilities = Abilities; var referencing = await ReferencingAsync(session, table).ConfigureAwait(true); + if (!Holds(session)) + { + return; + } + if (referencing.Count > 0 && !abilities.DropTableCascade) { await CatalogDialogs.ShowRefusedAsync( @@ -173,6 +198,8 @@ private async Task DropSchemaAsync(string schema) await DiscardTabsAsync(view => string.Equals(view.Model.Table.Schema, schema, StringComparison.Ordinal)) .ConfigureAwait(true); _workbench.TableFilters.ForgetSchema(FilterConnection, session.Database, schema); + // Cascade drops the views in other schemas that read this one's tables too. + await DiscardGoneTabsAsync(session).ConfigureAwait(true); await RefreshAsync().ConfigureAwait(true); } @@ -185,6 +212,11 @@ private async Task DropDatabaseAsync(string database) var abilities = Abilities; var clusters = abilities.OnCluster ? (await ChoicesAsync(session).ConfigureAwait(true)).Clusters : []; + if (!Holds(session)) + { + return; + } + using var change = await CatalogDialogs.AskDropDatabaseAsync(this, session, database, abilities, clusters) .ConfigureAwait(true); if (change is null @@ -200,6 +232,7 @@ private async Task DropDatabaseAsync(string database) await DiscardTabsAsync(view => string.Equals(view.Model.Table.Schema, database, StringComparison.Ordinal)) .ConfigureAwait(true); _workbench.TableFilters.ForgetSchema(FilterConnection, session.Database, database); + await DiscardGoneTabsAsync(session).ConfigureAwait(true); if (string.Equals(_defaultDatabase, database, StringComparison.Ordinal)) { _defaultDatabase = null; @@ -219,6 +252,11 @@ await DiscardTabsAsync(view => string.Equals(view.Model.Table.Schema, database, // was asked in its own question, which showed them. True only once the change landed on this window's session. private async Task RunCatalogChangeAsync(DatabaseSession session, CatalogChange change, string failedTitle) { + if (!Holds(session)) + { + return false; + } + if (change.Gate == SaveGate.Refuse) { await CatalogDialogs.ShowRefusedAsync(this, Strings.Get(failedTitle), Strings.Get("EditLockReadOnly")) @@ -246,8 +284,9 @@ await CatalogDialogs.ShowRefusedAsync(this, Strings.Get(failedTitle), Strings.Er .ConfigureAwait(true); return false; } - catch (OperationCanceledException) + catch (Exception exception) when (exception is OperationCanceledException or ObjectDisposedException) { + // Stopped, or the window let go of its session while the change waited. return false; } @@ -264,14 +303,14 @@ await CatalogDialogs.ShowRefusedAsync( Strings.Get(failedTitle), $"{Strings.Error(unknown.Error)} {Strings.Get("CatalogChangeUnknown")}") .ConfigureAwait(true); - if (ReferenceEquals(_session, session)) + if (Holds(session)) { await RefreshAsync().ConfigureAwait(true); } return false; default: - return ReferenceEquals(_session, session); + return Holds(session); } } @@ -283,7 +322,7 @@ private static async Task> ReferencingAsync(DatabaseSes using var keys = await session.ReadTableKeysAsync(table).ConfigureAwait(true); return keys.ReferencingTables; } - catch (TableProException) + catch (Exception exception) when (exception is TableProException or ObjectDisposedException) { return []; } @@ -296,7 +335,7 @@ private static async Task ChoicesAsync(DatabaseSession session) { return await session.GetDatabaseChoicesAsync().ConfigureAwait(true); } - catch (TableProException) + catch (Exception exception) when (exception is TableProException or ObjectDisposedException) { return DatabaseChoices.None; } @@ -305,6 +344,9 @@ private static async Task ChoicesAsync(DatabaseSession session) private List TableViews() => [.. Tabs.TabItems.OfType().Select(item => item.Content).OfType()]; + // A read can outlast the window's session: a disconnect replaces it, and the last holder disposes it. + private bool Holds(DatabaseSession session) => ReferenceEquals(_session, session); + // The objects of each schema a table tab shows, read again; a schema that cannot be read closes nothing. private async Task DiscardGoneTabsAsync(DatabaseSession session) { @@ -321,6 +363,15 @@ private async Task DiscardGoneTabsAsync(DatabaseSession session) { continue; } + catch (ObjectDisposedException) + { + return; + } + + if (!Holds(session)) + { + return; + } foreach (var view in TableViews().Where(view => view.Model.Table.Schema == schema)) { From 4c80c8c2357ef94a844a00cba6749153cc4eb46f Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 10:32:23 +0700 Subject: [PATCH 06/16] fix(core): compare INSERT column names by the engine's rule and keep a computed-only result's values --- core/crates/ffi/src/export.rs | 43 +++++++++++++------ core/crates/ffi/tests/export.rs | 25 +++++++++++ ...tions-table-filters-and-export-progress.md | 2 +- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/core/crates/ffi/src/export.rs b/core/crates/ffi/src/export.rs index d8a3ae3b27..20cd5b8e26 100644 --- a/core/crates/ffi/src/export.rs +++ b/core/crates/ffi/src/export.rs @@ -15,6 +15,7 @@ use tablepro_core::export::{ }; use tablepro_core::filter::FilterSet; use tablepro_core::meta::{RowIdentity, TableRef}; +use tablepro_core::sql_syntax::{SqlGrammar, same_name}; use tablepro_core::value::{JsonText, OtherValue, UndecodableReason, UndecodedValue}; use tablepro_core::{ColumnInfo, ReadStatement, ResultEvent, ResultSink, SinkFlow, Truncation, Value}; use tablepro_session::OperationKind; @@ -126,15 +127,23 @@ impl DatabaseSession { columns = session.columns(source.clone()) => columns.ok(), }, }; - columns = named; - Some(InsertTarget::of(&source, catalog.as_deref().unwrap_or_default())) + let into = InsertTarget::of(&source, catalog.as_deref().unwrap_or_default()); + // A result of only what the server computes would write nothing there, so its values go to + // query_results instead. + match named.iter().all(|column| into.generated.contains(&column.name)) { + true => Some(InsertTarget::of(&TableRef::unqualified(RESULT_TABLE), &[])), + false => { + columns = named; + Some(into) + } + } } None => Some(InsertTarget::of(&TableRef::unqualified(RESULT_TABLE), &[])), }, _ => None, }; - // An INSERT that names a column twice is refused by every server, so it is refused here before any file. - if insert.is_some() && repeats_a_name(&columns) { + // An INSERT that names a column twice is refused by the server, so it is refused here before any file. + if insert.is_some() && repeats_a_name(&columns, session.grammar()) { return Err(CoreError::failed( "ffi.export_duplicate_columns", "SQL INSERT names each column once", @@ -510,11 +519,15 @@ impl ResultSink for FileSink { } } -// Ignoring case, since SQLite, MySQL and SQL Server read two names that differ only in case as one column, and the -// second value would be dropped without a word. -fn repeats_a_name(columns: &[ResultColumn]) -> bool { - let mut seen = std::collections::HashSet::with_capacity(columns.len()); - columns.iter().any(|column| !seen.insert(column.name.to_lowercase())) +// By the engine's own rule, since SQLite, MySQL and SQL Server read two names that differ only in case as one column, +// and the second value would be dropped without a word. +fn repeats_a_name(columns: &[ResultColumn], grammar: SqlGrammar) -> bool { + columns.iter().enumerate().any(|(index, column)| { + columns + .iter() + .take(index) + .any(|before| same_name(&before.name, &column.name, grammar)) + }) } // Where each database holds schemas of its own, an origin names no database, and an editor can read in another one @@ -844,12 +857,16 @@ mod tests { } #[test] - fn two_names_that_differ_only_in_case_are_one_column() { + fn two_names_that_differ_only_in_case_are_one_column_where_the_engine_reads_them_so() { let named = |name: &str| array_of(name, bigint()); - assert!(repeats_a_name(&[named("a"), named("A")])); - assert!(repeats_a_name(&[named("id"), named("name"), named("id")])); - assert!(!repeats_a_name(&[named("id"), named("name")])); + assert!(repeats_a_name(&[named("a"), named("A")], SqlGrammar::Sqlite)); + assert!(!repeats_a_name(&[named("a"), named("A")], SqlGrammar::PostgreSql)); + assert!(repeats_a_name( + &[named("id"), named("name"), named("id")], + SqlGrammar::PostgreSql + )); + assert!(!repeats_a_name(&[named("id"), named("name")], SqlGrammar::MySql)); } #[test] diff --git a/core/crates/ffi/tests/export.rs b/core/crates/ffi/tests/export.rs index 9b0c398306..8c313b5e52 100644 --- a/core/crates/ffi/tests/export.rs +++ b/core/crates/ffi/tests/export.rs @@ -310,6 +310,31 @@ async fn sql_insert_of_a_result_naming_a_column_twice_is_refused_before_any_file assert_eq!(named(dir.path(), "pairs.sql"), Vec::::new()); } +#[tokio::test] +async fn sql_insert_of_only_what_the_server_computes_writes_into_query_results() { + 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')", + ) + .await; + let set = run(&session, "SELECT loud FROM items").await.unwrap(); + let path = dir.path().join("loud.sql"); + + session + .export_rows(set, 1, ExportFormatTag::SqlInsert, None, spelled(&path), None, None) + .await + .unwrap(); + + assert_eq!( + text(&path), + "INSERT INTO \"query_results\" (\"loud\") VALUES ('PEN');\n" + ); +} + #[tokio::test] async fn sql_insert_of_a_computed_result_writes_into_query_results() { let dir = tempfile::tempdir().unwrap(); diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md index d9a23d0227..412734dc19 100644 --- a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -17,7 +17,7 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) ### Export -- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table or one column twice. A result that names one column twice, case aside, is refused before any file. Where each database holds schemas of its own, an origin names no database and an editor can read in another one, so the session's catalog is not read and nothing is left out: a computed column then makes the script fail where it runs rather than lose a value. +- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table, name one column twice, or hold only what the server computes. A result whose names the engine reads as one column twice is refused before any file. Where each database holds schemas of its own, an origin names no database and an editor can read in another one, so the session's catalog is not read and nothing is left out: a computed column then makes the script fail where it runs rather than lose a value. - The export bar counts the rows written, read from the core's counter (`ExportProgress`) four times a second, out of the rows held or the counted total where known. Stop ends it with no file left, so it asks nothing first. ### Catalog operations From 3e0578caad79e47265d3935fdd8c009ed62e8aa8 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 10:40:32 +0700 Subject: [PATCH 07/16] fix(windows): let a catalog change be cancelled from the bar, drop a cell menu whose cell moved while it loaded --- ...tions-table-filters-and-export-progress.md | 2 +- .../TablePro.App/Strings/en-US/Resources.resw | 6 +++ .../TablePro.App/Views/TableTabView.xaml.cs | 8 +++- .../Views/WorkspaceView.Catalog.cs | 42 ++++++++++++++++--- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md index 412734dc19..5135a882bb 100644 --- a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -25,7 +25,7 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) - The tree's table menu adds Open in new tab, Show DDL, Truncate... and Drop..., the last two where the engine has them and the connection is not read-only. A schema row has New schema... and Drop schema, or New database... and Drop database where the server's databases are its schemas. The database switcher ends with New database... and a Drop database list, and Database > New database... and New schema... reach the same dialogs. The engine's own databases and schemas, and the one the window is in, are never offered for a drop. - Each dialog shows the statements the core planned for its options (`PlanCatalogChange`), planned again as an option changes: Cascade and Restart identity for a truncate, Cascade for a drop, Close other connections first for a database, Run on for a cluster, and for a new database the options the engine names (`CatalogAbilities.DatabaseFields`) with the server's choices. - A truncate where the server refuses a table other tables' keys point at is refused first, naming them; so is a drop where the engine has no Cascade. -- Safe Mode gates them as the core says (`CatalogChange.Gate`): emptying or dropping is asked at every level, in the dialog that shows the statements; creating is asked again where Confirm writes asks; Read only refuses. Each runs at once, as the Linux app's do. +- Safe Mode gates them as the core says (`CatalogChange.Gate`): emptying or dropping is asked at every level, in the dialog that shows the statements; creating is asked again where Confirm writes asks; Read only refuses. Each runs at once, as the Linux app's do, in the bar exports use, whose Cancel stops one waiting on a lock; leaving the window stops it too. A stopped change reads the tree again and closes nothing. The commands are hidden while the bar is busy. - After a drop, the table's tabs close without asking, since their changes have nowhere to go, and are not kept to reopen; so do the tabs of every object the catalog no longer lists, as a partitioned table's partitions and the views Cascade drops. A truncate reads every table tab again, since Cascade and a partitioned table empty others; a tab holding edits asks first. A change whose outcome is unknown reads the tree again and closes nothing. The tree reads the schemas again on every refresh. ## Rationale diff --git a/windows/src/TablePro.App/Strings/en-US/Resources.resw b/windows/src/TablePro.App/Strings/en-US/Resources.resw index 76251e1463..b48928f7a3 100644 --- a/windows/src/TablePro.App/Strings/en-US/Resources.resw +++ b/windows/src/TablePro.App/Strings/en-US/Resources.resw @@ -4284,4 +4284,10 @@ New fingerprint: {2} SQL INSERT names each column once. Name the columns apart with AS and run the query again. + + Running the change on {0}... + + + The change was stopped. It may have run in part, so the objects are read again. + diff --git a/windows/src/TablePro.App/Views/TableTabView.xaml.cs b/windows/src/TablePro.App/Views/TableTabView.xaml.cs index 34ccc75905..49726dfbd9 100644 --- a/windows/src/TablePro.App/Views/TableTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/TableTabView.xaml.cs @@ -638,10 +638,16 @@ private async void OnGridContextRequested(UIElement sender, ContextRequestedEven args.Handled = true; var place = GridMenus.Place(grid, args); var (row, column) = (grid.FocusedRow, grid.FocusedColumn); + var rows = Results.ShownSet; var cellRules = grid.RowCount > 0 && !_model.IsDeleted(row) ? await CellRulesAsync(row, column).ConfigureAwait(true) : []; - if (XamlRoot is null || _model.IsClosed) + // The first read of the choices can outlast a page change or another click, and the menu acts on its cell. + if (XamlRoot is null + || _model.IsClosed + || !ReferenceEquals(rows, Results.ShownSet) + || grid.FocusedRow != row + || grid.FocusedColumn != column) { return; } diff --git a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs index 79b3476268..6a4edc0293 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs @@ -8,9 +8,10 @@ namespace TablePro.App.Views; // through Safe Mode, then the tree and the tabs follow (core/docs/decisions/0013-catalog-operations.md). public sealed partial class WorkspaceView { - // Read-only, by the connection or its Safe Mode, offers none of them. + // Read-only, by the connection or its Safe Mode, offers none of them, nor does a window whose bar is busy with an + // export, an import or another change. private CatalogAbilities Abilities => - _session is { IsReadOnly: false } session ? session.CatalogAbilities : CatalogAbilities.None; + _session is { IsReadOnly: false } session && _exporting is null ? session.CatalogAbilities : CatalogAbilities.None; public bool CanCreateDatabase => Abilities.CreateDatabase; @@ -273,10 +274,20 @@ await CatalogDialogs.ShowRefusedAsync(this, Strings.Get(failedTitle), Strings.Ge } } + if (_exporting is not null) + { + return false; + } + + // The bar's Cancel stops a change waiting on a lock, and leaving the window stops it too. + using var running = new CancellationTokenSource(); + _exporting = running; + ShowExport(InfoBarSeverity.Informational, Strings.Format("CatalogChangeRunning", session.Name), running: true); + CommandsChanged?.Invoke(this, EventArgs.Empty); CatalogChangeOutcome outcome; try { - outcome = await change.RunAsync(confirmed: true).ConfigureAwait(true); + outcome = await change.RunAsync(confirmed: true, running.Token).ConfigureAwait(true); } catch (TableProException exception) { @@ -284,11 +295,32 @@ await CatalogDialogs.ShowRefusedAsync(this, Strings.Get(failedTitle), Strings.Er .ConfigureAwait(true); return false; } - catch (Exception exception) when (exception is OperationCanceledException or ObjectDisposedException) + catch (OperationCanceledException) { - // Stopped, or the window let go of its session while the change waited. + // A statement stopped once sent may have run, so the tree is read again and nothing closes. + if (Holds(session)) + { + ShowExport(InfoBarSeverity.Informational, Strings.Get("CatalogChangeStopped"), running: false); + await RefreshAsync().ConfigureAwait(true); + } + return false; } + catch (ObjectDisposedException) + { + // The window let go of its session while the change waited. + return false; + } + finally + { + _exporting = null; + if (ExportBar.Severity == InfoBarSeverity.Informational && !ExportBar.IsClosable) + { + ExportBar.IsOpen = false; + } + + CommandsChanged?.Invoke(this, EventArgs.Empty); + } switch (outcome) { From 99ff384bae925d6e34536fc9d60d8b2b04b0ab41 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 10:46:57 +0700 Subject: [PATCH 08/16] fix(windows): report a cell filter applied only when it was, give a lone schema's folders its commands --- ...tions-table-filters-and-export-progress.md | 2 +- windows/src/TablePro.App.Model/TableTab.cs | 5 ++-- .../TablePro.App/Views/WorkspaceView.xaml.cs | 25 ++++++++++++++----- .../TableTabFilterTests.cs | 14 +++++++++++ 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md index 5135a882bb..1396bb4dc9 100644 --- a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -22,7 +22,7 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) ### Catalog operations -- The tree's table menu adds Open in new tab, Show DDL, Truncate... and Drop..., the last two where the engine has them and the connection is not read-only. A schema row has New schema... and Drop schema, or New database... and Drop database where the server's databases are its schemas. The database switcher ends with New database... and a Drop database list, and Database > New database... and New schema... reach the same dialogs. The engine's own databases and schemas, and the one the window is in, are never offered for a drop. +- The tree's table menu adds Open in new tab, Show DDL, Truncate... and Drop..., the last two where the engine has them and the connection is not read-only. A schema row has New schema... and Drop schema, or New database... and Drop database where the server's databases are its schemas; with one schema, whose folders show with no row of their own, the folders carry them. The database switcher ends with New database... and a Drop database list, and Database > New database... and New schema... reach the same dialogs. The engine's own databases and schemas, and the one the window is in, are never offered for a drop. - Each dialog shows the statements the core planned for its options (`PlanCatalogChange`), planned again as an option changes: Cascade and Restart identity for a truncate, Cascade for a drop, Close other connections first for a database, Run on for a cluster, and for a new database the options the engine names (`CatalogAbilities.DatabaseFields`) with the server's choices. - A truncate where the server refuses a table other tables' keys point at is refused first, naming them; so is a drop where the engine has no Cascade. - Safe Mode gates them as the core says (`CatalogChange.Gate`): emptying or dropping is asked at every level, in the dialog that shows the statements; creating is asked again where Confirm writes asks; Read only refuses. Each runs at once, as the Linux app's do, in the bar exports use, whose Cancel stops one waiting on a lock; leaving the window stops it too. A stopped change reads the tree again and closes nothing. The commands are hidden while the bar is busy. diff --git a/windows/src/TablePro.App.Model/TableTab.cs b/windows/src/TablePro.App.Model/TableTab.cs index 66076a1500..36556f7aa6 100644 --- a/windows/src/TablePro.App.Model/TableTab.cs +++ b/windows/src/TablePro.App.Model/TableTab.cs @@ -533,7 +533,8 @@ public async Task ApplyFilterAsync(TableFilter? filter) await LoadAsync(0, recount: true).ConfigureAwait(true); } - // A cell's rule joins the filter and the table reads again; false when it was already running. + // A cell's rule joins the filter and the table reads again; false when it was already running, the user kept their + // edits instead, or the filter was refused. public async Task FilterByAsync(FilterRule rule) { ArgumentNullException.ThrowIfNull(rule); @@ -544,7 +545,7 @@ public async Task FilterByAsync(FilterRule rule) } await ApplyFilterAsync(filter).ConfigureAwait(true); - return true; + return ReferenceEquals(Filter, filter); } // Read once per tab, and again after a refresh, since the table's columns can change. diff --git a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs index 330f2f6a5a..461de869fe 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs @@ -1986,7 +1986,13 @@ private void OnObjectRowContextRequested(UIElement sender, ContextRequestedEvent break; case ObjectNode { Kind: ObjectNodeKind.Schema, Schema: { } schema }: args.Handled = true; - ShowSchemaMenu(row, schema, args); + ShowSchemaMenu(row, schema, args, copyName: true); + break; + + // With one schema the tree shows its folders with no row of its own, so they carry its commands. + case ObjectNode { Kind: ObjectNodeKind.Folder, Schema: { } schema } when _singleSchema: + args.Handled = true; + ShowSchemaMenu(row, schema, args, copyName: false); break; default: break; @@ -2064,7 +2070,7 @@ private void ShowObjectMenu(FrameworkElement row, ObjectEntry entry, ContextRequ } // A schema row is a database where the server's databases are its schemas, as MySQL's are. - private void ShowSchemaMenu(FrameworkElement row, string schema, ContextRequestedEventArgs args) + private void ShowSchemaMenu(FrameworkElement row, string schema, ContextRequestedEventArgs args, bool copyName) { var menu = new MenuFlyout(); var abilities = Abilities; @@ -2100,13 +2106,20 @@ private void ShowSchemaMenu(FrameworkElement row, string schema, ContextRequeste } } - if (menu.Items.Count > 0) + if (copyName) { - menu.Items.Add(new MenuFlyoutSeparator()); + if (menu.Items.Count > 0) + { + menu.Items.Add(new MenuFlyoutSeparator()); + } + + menu.Items.Add(ObjectMenuItem("ObjectMenuCopyName", Symbol.Copy, () => CopyText(schema))); } - menu.Items.Add(ObjectMenuItem("ObjectMenuCopyName", Symbol.Copy, () => CopyText(schema))); - ShowMenu(menu, row, args); + if (menu.Items.Count > 0) + { + ShowMenu(menu, row, args); + } } // Never the engine's own, nor the one this window is in, as macOS offers a drop. diff --git a/windows/tests/TablePro.App.Model.Tests/TableTabFilterTests.cs b/windows/tests/TablePro.App.Model.Tests/TableTabFilterTests.cs index 570351b145..cbafe99459 100644 --- a/windows/tests/TablePro.App.Model.Tests/TableTabFilterTests.cs +++ b/windows/tests/TablePro.App.Model.Tests/TableTabFilterTests.cs @@ -408,6 +408,20 @@ public async Task ACellsRuleJoinsTheRulesRunningAndOneRunningAlreadyReadsNothing Assert.Equal(reads + 1, _session.Queries.Count); } + [Fact] + public async Task ACellsRuleTheUserKeptTheirEditsOverIsNotReportedApplied() + { + await _tab.RunAsync(); + _tab.CommitEdit(0, 1, "changed"); + _view.DiscardsForReload = false; + + var applied = await _tab.FilterByAsync(new FilterRule("Name", FilterOperator.Equal, new FilterValue.One("AC/DC"))); + + Assert.False(applied); + Assert.Null(_tab.Filter); + Assert.True(_tab.HasUnsavedChanges); + } + private TableTab New(SessionTab? restore = null, ObjectKind kind = ObjectKind.Table, ITableFilters? filters = null) { var tab = new TableTab( From de24e5e31ef632f2da5bb471d2321d7038137855 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 10:56:43 +0700 Subject: [PATCH 09/16] fix(core): write a query result's SQL INSERT into query_results, as macOS does --- core/crates/ffi/src/export.rs | 111 ++---------------- core/crates/ffi/tests/export.rs | 56 +-------- .../0022-browsing-through-the-ffi.md | 2 +- ...tions-table-filters-and-export-progress.md | 2 +- windows/src/TablePro.App.Model/ExportPlan.cs | 2 +- .../DatabaseSession.Export.cs | 2 +- windows/src/TablePro.Interop/Export.cs | 2 +- .../TablePro.Interop.Tests/ExportTests.cs | 4 +- 8 files changed, 21 insertions(+), 160 deletions(-) diff --git a/core/crates/ffi/src/export.rs b/core/crates/ffi/src/export.rs index 20cd5b8e26..368d44e99f 100644 --- a/core/crates/ffi/src/export.rs +++ b/core/crates/ffi/src/export.rs @@ -7,7 +7,7 @@ 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::{ColumnKind, ColumnType, ResultColumn}; -use tablepro_core::dialect::{DatabaseLayout, SqlDialect}; +use tablepro_core::dialect::SqlDialect; use tablepro_core::edit::parse_filter_text; use tablepro_core::export::{ CsvEncoder, CsvOptions, EncodeError, HtmlEncoder, JsonArrayEncoder, MarkdownEncoder, RenderError, RowEncoder, @@ -72,7 +72,7 @@ pub enum ExportFormatTag { #[uniffi::export] impl DatabaseSession { // Only the first `visible_columns` columns, since a table page carries row addresses after them. SQL INSERT with no - // table writes into the one table every column was read from, else into `query_results`. + // table writes into `query_results`, as the macOS app names a query result's table. #[expect( clippy::too_many_arguments, reason = "the rows, their format, table and file, and the app's progress and cancel, as the app passes them" @@ -105,7 +105,7 @@ impl DatabaseSession { } let held = rows.result_columns(); let visible = usize::try_from(visible_columns).unwrap_or(usize::MAX).min(held.len()); - let mut columns: Arc<[ResultColumn]> = held.iter().take(visible).cloned().collect(); + let columns: Arc<[ResultColumn]> = held.iter().take(visible).cloned().collect(); let insert = match (format, &table) { (ExportFormatTag::SqlInsert, Some(table)) => { let catalog = tokio::select! { @@ -115,35 +115,12 @@ impl DatabaseSession { }; Some(InsertTarget::of(table, &catalog)) } - (ExportFormatTag::SqlInsert, None) => match source_table(&columns) { - Some((source, named)) => { - // Read only to leave out what the server computes; a table that cannot be read leaves none out. - let layout = session.current_dialect().map(|dialect| dialect.database_layout()); - let catalog = match catalog_describes(layout) { - false => None, - true => tokio::select! { - biased; - () = cancel.cancelled() => return Err(CoreError::Cancelled), - columns = session.columns(source.clone()) => columns.ok(), - }, - }; - let into = InsertTarget::of(&source, catalog.as_deref().unwrap_or_default()); - // A result of only what the server computes would write nothing there, so its values go to - // query_results instead. - match named.iter().all(|column| into.generated.contains(&column.name)) { - true => Some(InsertTarget::of(&TableRef::unqualified(RESULT_TABLE), &[])), - false => { - columns = named; - Some(into) - } - } - } - None => Some(InsertTarget::of(&TableRef::unqualified(RESULT_TABLE), &[])), - }, + (ExportFormatTag::SqlInsert, None) => Some(InsertTarget::of(&TableRef::unqualified(RESULT_TABLE), &[])), _ => None, }; - // An INSERT that names a column twice is refused by the server, so it is refused here before any file. - if insert.is_some() && repeats_a_name(&columns, session.grammar()) { + // A query result's INSERT that names a column twice is refused by the server, or keeps one of the two values + // where the engine reads the names as one column, so it is refused here before any file. + if format == ExportFormatTag::SqlInsert && table.is_none() && repeats_a_name(&columns, session.grammar()) { return Err(CoreError::failed( "ffi.export_duplicate_columns", "SQL INSERT names each column once", @@ -266,7 +243,7 @@ impl TableRead { } } -// The table a query result's INSERT statements write into when its columns name no one table. +// The table a query result's INSERT statements write into, as the macOS app names it. const RESULT_TABLE: &str = "query_results"; #[derive(Clone)] @@ -530,30 +507,6 @@ fn repeats_a_name(columns: &[ResultColumn], grammar: SqlGrammar) -> bool { }) } -// Where each database holds schemas of its own, an origin names no database, and an editor can read in another one -// than the session's, so the session's catalog may describe another table of the same name. -fn catalog_describes(layout: Option) -> bool { - layout != Some(DatabaseLayout::WithSchemas) -} - -// The table every column was read from, as the driver traced it, with each column under the name the table gives it, -// so an alias is written as the column it reads. A column it could not trace, or one read twice, names none. -fn source_table(columns: &[ResultColumn]) -> Option<(TableRef, Arc<[ResultColumn]>)> { - let table = &columns.first()?.origin.as_ref()?.table; - let mut named: Vec = Vec::with_capacity(columns.len()); - for column in columns { - let origin = column.origin.as_ref().filter(|origin| origin.table == *table)?; - if named.iter().any(|seen| seen.name == origin.column) { - return None; - } - named.push(ResultColumn { - name: origin.column.clone(), - ..column.clone() - }); - } - Some((table.clone(), named.into())) -} - // 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, @@ -869,54 +822,6 @@ mod tests { assert!(!repeats_a_name(&[named("id"), named("name")], SqlGrammar::MySql)); } - #[test] - fn a_result_where_each_database_holds_schemas_of_its_own_reads_no_catalog() { - assert!(!catalog_describes(Some(DatabaseLayout::WithSchemas))); - assert!(catalog_describes(Some(DatabaseLayout::AsSchemas))); - assert!(catalog_describes(Some(DatabaseLayout::Single))); - assert!(catalog_describes(None)); - } - - #[test] - fn a_result_writes_into_its_one_traced_table_under_the_tables_column_names() { - let traced = |name: &str, table: &str| { - array_of(name, bigint()).with_origin(Some(tablepro_core::column::ColumnOrigin::new( - TableRef::new(Some("public".to_owned()), table), - name, - ))) - }; - let orders = TableRef::new(Some("public".to_owned()), "orders"); - - let mut aliased = traced("total", "orders"); - aliased.name = "amount".to_owned(); - let names = |found: Option<(TableRef, Arc<[ResultColumn]>)>| { - found.map(|(table, columns)| { - ( - table, - columns.iter().map(|column| column.name.clone()).collect::>(), - ) - }) - }; - - assert_eq!( - names(source_table(&[traced("id", "orders"), aliased])), - Some((orders, vec!["id".to_owned(), "total".to_owned()])) - ); - assert_eq!( - names(source_table(&[traced("id", "orders"), traced("name", "people")])), - None - ); - assert_eq!( - names(source_table(&[traced("id", "orders"), array_of("sum", bigint())])), - None - ); - assert_eq!( - names(source_table(&[traced("id", "orders"), traced("id", "orders")])), - None - ); - assert_eq!(names(source_table(&[])), None); - } - #[test] fn an_array_comes_back_element_by_element_as_a_streamed_read_gives_it() { let columns: Arc<[ResultColumn]> = vec![ diff --git a/core/crates/ffi/tests/export.rs b/core/crates/ffi/tests/export.rs index 8c313b5e52..9dce2a439f 100644 --- a/core/crates/ffi/tests/export.rs +++ b/core/crates/ffi/tests/export.rs @@ -252,10 +252,12 @@ async fn sql_insert_of_a_table_names_it_and_leaves_out_what_the_server_computes( } #[tokio::test] -async fn sql_insert_of_a_query_result_writes_into_the_table_its_columns_came_from() { +async fn sql_insert_of_a_query_result_writes_into_query_results_under_its_own_column_names() { let dir = tempfile::tempdir().unwrap(); let (_core, session) = fixture(dir.path()).await; - let set = run(&session, "SELECT id, name FROM people ORDER BY id").await.unwrap(); + let set = run(&session, "SELECT id, name AS label FROM people ORDER BY id") + .await + .unwrap(); let path = dir.path().join("people.sql"); let rows = session @@ -266,29 +268,8 @@ async fn sql_insert_of_a_query_result_writes_into_the_table_its_columns_came_fro assert_eq!(rows, 2); assert_eq!( text(&path), - "INSERT INTO \"people\" (\"id\", \"name\") VALUES (1, 'Ada');\n\ - INSERT INTO \"people\" (\"id\", \"name\") VALUES (2, 'Grace, \"Amazing\"');\n" - ); -} - -#[tokio::test] -async fn sql_insert_of_an_aliased_result_names_the_tables_columns() { - let dir = tempfile::tempdir().unwrap(); - let (_core, session) = fixture(dir.path()).await; - let set = run(&session, "SELECT id, name AS label FROM people ORDER BY id") - .await - .unwrap(); - let path = dir.path().join("aliased.sql"); - - session - .export_rows(set, 2, ExportFormatTag::SqlInsert, None, spelled(&path), None, None) - .await - .unwrap(); - - assert!( - text(&path).starts_with("INSERT INTO \"people\" (\"id\", \"name\") VALUES (1, 'Ada');"), - "{}", - text(&path) + "INSERT INTO \"query_results\" (\"id\", \"label\") VALUES (1, 'Ada');\n\ + INSERT INTO \"query_results\" (\"id\", \"label\") VALUES (2, 'Grace, \"Amazing\"');\n" ); } @@ -310,31 +291,6 @@ async fn sql_insert_of_a_result_naming_a_column_twice_is_refused_before_any_file assert_eq!(named(dir.path(), "pairs.sql"), Vec::::new()); } -#[tokio::test] -async fn sql_insert_of_only_what_the_server_computes_writes_into_query_results() { - 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')", - ) - .await; - let set = run(&session, "SELECT loud FROM items").await.unwrap(); - let path = dir.path().join("loud.sql"); - - session - .export_rows(set, 1, ExportFormatTag::SqlInsert, None, spelled(&path), None, None) - .await - .unwrap(); - - assert_eq!( - text(&path), - "INSERT INTO \"query_results\" (\"loud\") VALUES ('PEN');\n" - ); -} - #[tokio::test] async fn sql_insert_of_a_computed_result_writes_into_query_results() { let dir = tempfile::tempdir().unwrap(); diff --git a/core/docs/decisions/0022-browsing-through-the-ffi.md b/core/docs/decisions/0022-browsing-through-the-ffi.md index 00b0ae3278..edd2a16e3c 100644 --- a/core/docs/decisions/0022-browsing-through-the-ffi.md +++ b/core/docs/decisions/0022-browsing-through-the-ffi.md @@ -17,7 +17,7 @@ The FFI exposes what the Linux app calls, and does the work the Linux app does, - `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 into the table, for a query result the one table its columns were read from or else `query_results`), count the rows written on an `ExportProgress` the app reads, write beside the target and rename once complete, so a failed or cancelled export leaves no file. +- `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 into the table, or into `query_results` for a query result), count the rows written on an `ExportProgress` the app reads, write beside the target and rename once complete, so a failed or cancelled export leaves no file. - `DatabaseSession::table_keys` reads a table's outbound and inbound foreign keys in one Metadata operation, as the Linux browse tab reads them. `TableKeys::row_references` lists, without the server, the keys a row of a page of that table leads somewhere by: the table's keys holding a column, and the other tables' keys pointing at the row, at most 64 each way. It reads only whether each key's cells hold a value, so a menu copies no value however many keys a table declares; a key with a NULL or a column the page lacks is left out. `follow_reference` then builds the chosen key's filter through `browse::RowReference` from the row's stored values, or its refusal for the app to word, as for a value the store cannot name exactly or one past 8 KiB. - `ResultSet::cell_text_head` gives a value's start, line breaks kept, and `cell_bytes` a binary value's first bytes and its length, so an app's viewer copies no more of a large cell than it shows, where `cell_text` copies all of it. - `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. diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md index 1396bb4dc9..53ed1a25d2 100644 --- a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -17,7 +17,7 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) ### Export -- SQL INSERT is offered for a query result too: the core writes into the one table every column was read from (`ColumnOrigin`), each under the table's own column name and leaving out what the server computes, and into `query_results` where the columns name no one table, name one column twice, or hold only what the server computes. A result whose names the engine reads as one column twice is refused before any file. Where each database holds schemas of its own, an origin names no database and an editor can read in another one, so the session's catalog is not read and nothing is left out: a computed column then makes the script fail where it runs rather than lose a value. +- SQL INSERT is offered for a query result too, into `query_results` under the result's own column names, as macOS writes it. A result whose names the engine reads as one column twice is refused before any file. - The export bar counts the rows written, read from the core's counter (`ExportProgress`) four times a second, out of the rows held or the counted total where known. Stop ends it with no file left, so it asks nothing first. ### Catalog operations diff --git a/windows/src/TablePro.App.Model/ExportPlan.cs b/windows/src/TablePro.App.Model/ExportPlan.cs index 618dacfa5c..f0ffc175b1 100644 --- a/windows/src/TablePro.App.Model/ExportPlan.cs +++ b/windows/src/TablePro.App.Model/ExportPlan.cs @@ -29,7 +29,7 @@ private ExportPlan(IReadOnlyList scopes, IReadOnlyList ExportRowsAsync( ResultSet rows, int visibleColumns, diff --git a/windows/src/TablePro.Interop/Export.cs b/windows/src/TablePro.Interop/Export.cs index 35be9326e9..0793b70c19 100644 --- a/windows/src/TablePro.Interop/Export.cs +++ b/windows/src/TablePro.Interop/Export.cs @@ -1,6 +1,6 @@ namespace TablePro.Interop; -// SqlInsert writes the rows into the table they came from, or into query_results where no one table is known. +// SqlInsert writes a table's rows into that table and a query result's into query_results. public enum ExportFormat { Csv, diff --git a/windows/tests/TablePro.Interop.Tests/ExportTests.cs b/windows/tests/TablePro.Interop.Tests/ExportTests.cs index d6f00764d4..001ca6d207 100644 --- a/windows/tests/TablePro.Interop.Tests/ExportTests.cs +++ b/windows/tests/TablePro.Interop.Tests/ExportTests.cs @@ -51,7 +51,7 @@ public async Task APageExportsOnlyItsVisibleColumns() } [Fact] - public async Task SqlInsertOfAResultWritesIntoTheTableItsColumnsCameFrom() + public async Task SqlInsertOfAResultWritesIntoQueryResults() { var cancellationToken = TestContext.Current.CancellationToken; await using var database = await SqliteDatabase.CreateAsync(); @@ -65,7 +65,7 @@ public async Task SqlInsertOfAResultWritesIntoTheTableItsColumnsCameFrom() Assert.Equal(3, rows); var first = (await File.ReadAllLinesAsync(path, cancellationToken))[0]; - Assert.Equal("INSERT INTO \"people\" (\"id\", \"name\") VALUES (1, 'Ada');", first); + Assert.Equal("INSERT INTO \"query_results\" (\"id\", \"name\") VALUES (1, 'Ada');", first); } [Fact] From 056e7de06ae092925873bb7a4b98a1b062e5000c Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 11:03:14 +0700 Subject: [PATCH 10/16] fix(windows): close definition tabs of dropped objects, leave out data centres with no replicas --- ...tions-table-filters-and-export-progress.md | 2 +- .../src/TablePro.App/Views/CatalogDialogs.cs | 4 +- .../Views/DefinitionTabView.xaml.cs | 2 + .../Views/WorkspaceView.Catalog.cs | 54 ++++++++++++++----- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md index 53ed1a25d2..4700cf21ca 100644 --- a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -26,7 +26,7 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) - Each dialog shows the statements the core planned for its options (`PlanCatalogChange`), planned again as an option changes: Cascade and Restart identity for a truncate, Cascade for a drop, Close other connections first for a database, Run on for a cluster, and for a new database the options the engine names (`CatalogAbilities.DatabaseFields`) with the server's choices. - A truncate where the server refuses a table other tables' keys point at is refused first, naming them; so is a drop where the engine has no Cascade. - Safe Mode gates them as the core says (`CatalogChange.Gate`): emptying or dropping is asked at every level, in the dialog that shows the statements; creating is asked again where Confirm writes asks; Read only refuses. Each runs at once, as the Linux app's do, in the bar exports use, whose Cancel stops one waiting on a lock; leaving the window stops it too. A stopped change reads the tree again and closes nothing. The commands are hidden while the bar is busy. -- After a drop, the table's tabs close without asking, since their changes have nowhere to go, and are not kept to reopen; so do the tabs of every object the catalog no longer lists, as a partitioned table's partitions and the views Cascade drops. A truncate reads every table tab again, since Cascade and a partitioned table empty others; a tab holding edits asks first. A change whose outcome is unknown reads the tree again and closes nothing. The tree reads the schemas again on every refresh. +- After a drop, the table's tabs close without asking, since their changes have nowhere to go, and are not kept to reopen; so do the table and definition tabs of every object the catalog no longer lists, as a partitioned table's partitions, the views Cascade drops and a dropped table's triggers. A truncate reads every table tab again, since Cascade and a partitioned table empty others; a tab holding edits asks first. A change whose outcome is unknown reads the tree again and closes nothing. The tree reads the schemas again on every refresh. ## Rationale diff --git a/windows/src/TablePro.App/Views/CatalogDialogs.cs b/windows/src/TablePro.App/Views/CatalogDialogs.cs index d4cfcd7fee..4cb38be89f 100644 --- a/windows/src/TablePro.App/Views/CatalogDialogs.cs +++ b/windows/src/TablePro.App/Views/CatalogDialogs.cs @@ -393,7 +393,9 @@ public ReplicationRows(IReadOnlyList dataCenters) public KeyspaceReplication Chosen() => Simple ? new KeyspaceReplication.Simple(Count(_factor)) : new KeyspaceReplication.NetworkTopology( - [.. _centres.Select(centre => new DataCenterReplicas(centre.DataCenter, Count(centre.Box)))]); + [.. _centres + .Select(centre => new DataCenterReplicas(centre.DataCenter, Count(centre.Box))) + .Where(centre => centre.Replicas > 0)]); public void Watch(Action refresh) { diff --git a/windows/src/TablePro.App/Views/DefinitionTabView.xaml.cs b/windows/src/TablePro.App/Views/DefinitionTabView.xaml.cs index 76ed75318f..2336a93c5f 100644 --- a/windows/src/TablePro.App/Views/DefinitionTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/DefinitionTabView.xaml.cs @@ -47,6 +47,8 @@ internal DefinitionTabView(SessionShare share, ObjectEntry entr public bool Shows(ObjectEntry entry) => _model.Entry == entry; + internal ObjectEntry Entry => _model.Entry; + public async void Load() => await _model.LoadAsync().ConfigureAwait(true); public void Run() diff --git a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs index 6a4edc0293..5c4348bc53 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs @@ -196,7 +196,7 @@ private async Task DropSchemaAsync(string schema) return; } - await DiscardTabsAsync(view => string.Equals(view.Model.Table.Schema, schema, StringComparison.Ordinal)) + await DiscardObjectTabsAsync(shown => string.Equals(shown.Schema, schema, StringComparison.Ordinal)) .ConfigureAwait(true); _workbench.TableFilters.ForgetSchema(FilterConnection, session.Database, schema); // Cascade drops the views in other schemas that read this one's tables too. @@ -230,7 +230,7 @@ private async Task DropDatabaseAsync(string database) if (session.Layout == DatabaseLayout.AsSchemas) { // Each database is a schema of this connection's catalog, so its tabs and filters go with it. - await DiscardTabsAsync(view => string.Equals(view.Model.Table.Schema, database, StringComparison.Ordinal)) + await DiscardObjectTabsAsync(shown => string.Equals(shown.Schema, database, StringComparison.Ordinal)) .ConfigureAwait(true); _workbench.TableFilters.ForgetSchema(FilterConnection, session.Database, database); await DiscardGoneTabsAsync(session).ConfigureAwait(true); @@ -379,11 +379,12 @@ private List TableViews() => // A read can outlast the window's session: a disconnect replaces it, and the last holder disposes it. private bool Holds(DatabaseSession session) => ReferenceEquals(_session, session); - // The objects of each schema a table tab shows, read again; a schema that cannot be read closes nothing. + // The objects of each schema a table or definition tab shows, read again; a schema that cannot be read closes + // nothing. private async Task DiscardGoneTabsAsync(DatabaseSession session) { - HashSet<(string? Schema, string Name)> gone = []; - foreach (var schema in TableViews().Select(view => view.Model.Table.Schema).Distinct()) + HashSet gone = []; + foreach (var schema in ObjectTabs().Select(shown => shown.Schema).Distinct()) { IReadOnlyList objects; try @@ -405,25 +406,50 @@ private async Task DiscardGoneTabsAsync(DatabaseSession session) return; } - foreach (var view in TableViews().Where(view => view.Model.Table.Schema == schema)) + // A tab opened from the tree and one restored can spell the default schema apart, so a name with no + // schema on either side is the same object. + foreach (var shown in ObjectTabs().Where(shown => shown.Schema == schema)) { - var table = view.Model.Table; - // A tab opened from the tree and one restored can spell the default schema apart, so a name with no - // schema on either side is the same object. - if (!objects.Any(entry => entry.Name == table.Name + if (!objects.Any(entry => entry.Name == shown.Name + && (shown.Kind is null || entry.Kind == shown.Kind) && (entry.Schema is null || schema is null || entry.Schema == schema))) { - gone.Add((table.Schema, table.Name)); + gone.Add(shown); } } } - foreach (var (schema, name) in gone) + foreach (var shown in gone.Where(shown => shown.Kind is null)) { - _workbench.TableFilters.ForgetTable(FilterConnection, session.Database, new TableName(schema, name)); + _workbench.TableFilters.ForgetTable(FilterConnection, session.Database, new TableName(shown.Schema, shown.Name)); } - await DiscardTabsAsync(view => gone.Contains((view.Model.Table.Schema, view.Model.Table.Name))).ConfigureAwait(true); + await DiscardObjectTabsAsync(gone.Contains).ConfigureAwait(true); + } + + // What a table or definition tab shows; a table tab's kind is left open, since a table and a view read alike. + private readonly record struct ShownObject(string? Schema, string Name, ObjectKind? Kind); + + private static ShownObject? ShownBy(object? content) => content switch + { + TableTabView table => new ShownObject(table.Model.Table.Schema, table.Model.Table.Name, null), + DefinitionTabView definition => new ShownObject(definition.Entry.Schema, definition.Entry.Name, definition.Entry.Kind), + _ => null, + }; + + private List ObjectTabs() => + [.. Tabs.TabItems.OfType().Select(item => ShownBy(item.Content)).OfType()]; + + // The object is gone, so its tabs close without asking, and none is kept to reopen. + private async Task DiscardObjectTabsAsync(Func shows) + { + var items = Tabs.TabItems.OfType() + .Where(item => ShownBy(item.Content) is { } shown && shows(shown)) + .ToList(); + foreach (var item in items) + { + await _tabs.DiscardAsync(item).ConfigureAwait(true); + } } // The table is gone, so its tabs close without asking about their changes, and none is kept to reopen. From 70b06b772af416caf22311003494f660b444d064 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 11:09:35 +0700 Subject: [PATCH 11/16] fix(windows): forget the filters of tables a drop took with it, match a trigger tab by its table, keep the server's own database from a drop --- ...tions-table-filters-and-export-progress.md | 2 +- .../TablePro.App.Model/TableFilterMemory.cs | 41 +++++++++++++++++++ .../Views/WorkspaceView.Catalog.cs | 38 +++++++++-------- .../TablePro.App/Views/WorkspaceView.xaml.cs | 8 ++-- .../TableFilterMemoryTests.cs | 20 +++++++++ 5 files changed, 88 insertions(+), 21 deletions(-) diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md index 4700cf21ca..df0c0b7cb5 100644 --- a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -12,7 +12,7 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) ### Filters - The filter a table's rows were last read with is kept per saved connection (or opened file), database, schema and table in `table-filters.json` beside the session file (`TableFilterMemory`), as macOS keys it. A table opened again reads with it and shows it in the bar. Clear forgets it; a filter the core or the server refused is not kept. A restored tab and a followed foreign key keep their own filter. -- A dropped table forgets its filter, and a dropped schema or database every filter under it. +- A dropped table forgets its filter, and a dropped schema or database every filter under it; after a drop, a kept filter of a table its schema no longer lists, as a partition, goes too. - A table tab's cell menu has Filter, with `col = “value”` and `col != “value”`, `>` and `<` where the column orders, and `is NULL` and `is not NULL` for a NULL cell, each only where the engine offers the operator (`CellFilters`). The value is the core's filter text for the cell (`ResultSet.GetFilterText`), the text that binds back to the value stored, as a followed foreign key's filter is built; a value no text binds back to, such as 'true' in a SQLite BOOLEAN column, offers none. None either for a binary value, a value over 10,000 characters, one with a blank at either end or one that reads as NULL, a new row or an edited cell. The rule joins the rules under Match all and applies at once; under Match any of two clauses it runs alone; a rule already running does nothing. A query result has no Filter, as on macOS. ### Export diff --git a/windows/src/TablePro.App.Model/TableFilterMemory.cs b/windows/src/TablePro.App.Model/TableFilterMemory.cs index 030d3642c1..f79466d5e4 100644 --- a/windows/src/TablePro.App.Model/TableFilterMemory.cs +++ b/windows/src/TablePro.App.Model/TableFilterMemory.cs @@ -92,6 +92,47 @@ public void ForgetDatabase(string connection, string database) ForgetPrefix(Part(connection) + "." + Part(database) + "."); } + // The schemas holding a kept filter under the connection and database; null for a table named with no schema. + public IReadOnlyList SchemasWithFilters(string connection, string? database) + { + var prefix = Part(connection) + "." + Part(database) + "."; + lock (_lock) + { + return [.. Tables().Keys + .Where(key => key.StartsWith(prefix, StringComparison.Ordinal)) + .Select(key => key[prefix.Length..].Split('.')[0]) + .Distinct(StringComparer.Ordinal) + .Select(part => part.Length == 0 ? null : Uri.UnescapeDataString(part))]; + } + } + + // A drop can take tables no tab showed, such as a partitioned table's partitions, so the schema's kept filters are + // matched against what it lists now. + public void ForgetMissing(string connection, string? database, string? schema, IReadOnlySet present) + { + ArgumentNullException.ThrowIfNull(present); + var prefix = Part(connection) + "." + Part(database) + "." + Part(schema) + "."; + lock (_lock) + { + var tables = Tables(); + var gone = tables.Keys + .Where(key => key.StartsWith(prefix, StringComparison.Ordinal) + && !present.Contains(Uri.UnescapeDataString(key[prefix.Length..]))) + .ToList(); + if (gone.Count == 0) + { + return; + } + + foreach (var key in gone) + { + tables.Remove(key); + } + + Write(tables); + } + } + private void ForgetPrefix(string prefix) { lock (_lock) diff --git a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs index 5c4348bc53..9ef277668b 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs @@ -175,7 +175,7 @@ await CatalogDialogs.ShowRefusedAsync( _workbench.TableFilters.ForgetTable(FilterConnection, session.Database, table); // A partitioned table takes its partitions with it, and Cascade the views on it, so every tab whose object the // catalog no longer lists goes too. - await DiscardGoneTabsAsync(session).ConfigureAwait(true); + await DiscardGoneTabsAsync(session, entry.Schema).ConfigureAwait(true); await RefreshAsync().ConfigureAwait(true); if (entry.Schema is { } schema && !_singleSchema) { @@ -200,7 +200,7 @@ await DiscardObjectTabsAsync(shown => string.Equals(shown.Schema, schema, String .ConfigureAwait(true); _workbench.TableFilters.ForgetSchema(FilterConnection, session.Database, schema); // Cascade drops the views in other schemas that read this one's tables too. - await DiscardGoneTabsAsync(session).ConfigureAwait(true); + await DiscardGoneTabsAsync(session, null).ConfigureAwait(true); await RefreshAsync().ConfigureAwait(true); } @@ -233,7 +233,7 @@ private async Task DropDatabaseAsync(string database) await DiscardObjectTabsAsync(shown => string.Equals(shown.Schema, database, StringComparison.Ordinal)) .ConfigureAwait(true); _workbench.TableFilters.ForgetSchema(FilterConnection, session.Database, database); - await DiscardGoneTabsAsync(session).ConfigureAwait(true); + await DiscardGoneTabsAsync(session, null).ConfigureAwait(true); if (string.Equals(_defaultDatabase, database, StringComparison.Ordinal)) { _defaultDatabase = null; @@ -379,12 +379,17 @@ private List TableViews() => // A read can outlast the window's session: a disconnect replaces it, and the last holder disposes it. private bool Holds(DatabaseSession session) => ReferenceEquals(_session, session); - // The objects of each schema a table or definition tab shows, read again; a schema that cannot be read closes - // nothing. - private async Task DiscardGoneTabsAsync(DatabaseSession session) + // The objects of each schema a table or definition tab shows or a kept filter names, read again: what the catalog + // no longer lists loses its tabs and its filter. A schema that cannot be read changes nothing. + private async Task DiscardGoneTabsAsync(DatabaseSession session, string? dropped) { HashSet gone = []; - foreach (var schema in ObjectTabs().Select(shown => shown.Schema).Distinct()) + var schemas = ObjectTabs().Select(shown => shown.Schema) + .Concat(_workbench.TableFilters.SchemasWithFilters(FilterConnection, session.Database)) + .Append(dropped) + .Distinct() + .ToList(); + foreach (var schema in schemas) { IReadOnlyList objects; try @@ -406,12 +411,15 @@ private async Task DiscardGoneTabsAsync(DatabaseSession session) return; } + _workbench.TableFilters.ForgetMissing( + FilterConnection, session.Database, schema, objects.Select(entry => entry.Name).ToHashSet(StringComparer.Ordinal)); + // A tab opened from the tree and one restored can spell the default schema apart, so a name with no - // schema on either side is the same object. + // schema on either side is the same object; a trigger is its table's. foreach (var shown in ObjectTabs().Where(shown => shown.Schema == schema)) { if (!objects.Any(entry => entry.Name == shown.Name - && (shown.Kind is null || entry.Kind == shown.Kind) + && (shown.Kind is null || (entry.Kind == shown.Kind && entry.ParentTable == shown.ParentTable)) && (entry.Schema is null || schema is null || entry.Schema == schema))) { gone.Add(shown); @@ -419,21 +427,17 @@ private async Task DiscardGoneTabsAsync(DatabaseSession session) } } - foreach (var shown in gone.Where(shown => shown.Kind is null)) - { - _workbench.TableFilters.ForgetTable(FilterConnection, session.Database, new TableName(shown.Schema, shown.Name)); - } - await DiscardObjectTabsAsync(gone.Contains).ConfigureAwait(true); } // What a table or definition tab shows; a table tab's kind is left open, since a table and a view read alike. - private readonly record struct ShownObject(string? Schema, string Name, ObjectKind? Kind); + private readonly record struct ShownObject(string? Schema, string Name, ObjectKind? Kind, string? ParentTable); private static ShownObject? ShownBy(object? content) => content switch { - TableTabView table => new ShownObject(table.Model.Table.Schema, table.Model.Table.Name, null), - DefinitionTabView definition => new ShownObject(definition.Entry.Schema, definition.Entry.Name, definition.Entry.Kind), + TableTabView table => new ShownObject(table.Model.Table.Schema, table.Model.Table.Name, null, null), + DefinitionTabView definition => + new ShownObject(definition.Entry.Schema, definition.Entry.Name, definition.Entry.Kind, definition.Entry.ParentTable), _ => null, }; diff --git a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs index 461de869fe..ac8bb52926 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.xaml.cs @@ -2122,11 +2122,13 @@ private void ShowSchemaMenu(FrameworkElement row, string schema, ContextRequeste } } - // Never the engine's own, nor the one this window is in, as macOS offers a drop. + // Never the engine's own, nor the one this window is in, as macOS offers a drop. A server can resolve the name the + // connection gave in another case, so case is ignored. private bool Droppable(DatabaseSession session, string database) => !session.ProtectsDatabase(database) - && !string.Equals(database, CurrentDatabase, StringComparison.Ordinal) - && !string.Equals(database, session.Database ?? _listedOwnDatabase, StringComparison.Ordinal); + && !string.Equals(database, CurrentDatabase, StringComparison.OrdinalIgnoreCase) + && !string.Equals(database, session.Database, StringComparison.OrdinalIgnoreCase) + && !string.Equals(database, _listedOwnDatabase, StringComparison.OrdinalIgnoreCase); private static void ShowMenu(MenuFlyout menu, FrameworkElement row, ContextRequestedEventArgs args) { diff --git a/windows/tests/TablePro.App.Model.Tests/TableFilterMemoryTests.cs b/windows/tests/TablePro.App.Model.Tests/TableFilterMemoryTests.cs index 2385f08461..b758342fd2 100644 --- a/windows/tests/TablePro.App.Model.Tests/TableFilterMemoryTests.cs +++ b/windows/tests/TablePro.App.Model.Tests/TableFilterMemoryTests.cs @@ -64,4 +64,24 @@ public void ADroppedTableSchemaOrDatabaseTakesItsFiltersWithIt() memory.ForgetDatabase(connection, "crm"); Assert.Null(new TableFilterMemory(Path).Recall(connection, "crm", Artist)); } + + [Fact] + public void AFilterOfATableTheSchemaNoLongerListsIsForgotten() + { + var memory = new TableFilterMemory(Path); + var connection = TableFilterMemory.ConnectionKey(Guid.NewGuid(), null); + var kept = new TableName("sales", "orders"); + var partition = new TableName("sales", "orders.2026"); + memory.Remember(connection, "shop", kept, ByName); + memory.Remember(connection, "shop", partition, ByName); + memory.Remember(connection, "shop", Artist, ByName); + + Assert.Equal(["public", "sales"], [.. memory.SchemasWithFilters(connection, "shop").Order()]); + + memory.ForgetMissing(connection, "shop", "sales", new HashSet(["orders"])); + + Assert.NotNull(memory.Recall(connection, "shop", kept)); + Assert.Null(memory.Recall(connection, "shop", partition)); + Assert.NotNull(memory.Recall(connection, "shop", Artist)); + } } From 5be6c99be21a7dc46ff29796027e33d0ee2b434a Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 11:15:41 +0700 Subject: [PATCH 12/16] fix(windows): read the keys of tables a drop left again, show a tab before asking to discard its edits --- ...ations-table-filters-and-export-progress.md | 2 +- windows/src/TablePro.App.Model/TableTab.cs | 17 +++++++++++++++++ .../TablePro.App/Views/TableTabView.xaml.cs | 7 +++++++ .../Views/WorkspaceView.Catalog.cs | 18 +++++++++++++++--- .../TableTabReferenceTests.cs | 16 ++++++++++++++++ 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md index df0c0b7cb5..1d1cb83934 100644 --- a/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md +++ b/windows/docs/decisions/0012-catalog-operations-table-filters-and-export-progress.md @@ -26,7 +26,7 @@ A table's filter lived only with its tab ([0007](0007-browsing-a-connection.md)) - Each dialog shows the statements the core planned for its options (`PlanCatalogChange`), planned again as an option changes: Cascade and Restart identity for a truncate, Cascade for a drop, Close other connections first for a database, Run on for a cluster, and for a new database the options the engine names (`CatalogAbilities.DatabaseFields`) with the server's choices. - A truncate where the server refuses a table other tables' keys point at is refused first, naming them; so is a drop where the engine has no Cascade. - Safe Mode gates them as the core says (`CatalogChange.Gate`): emptying or dropping is asked at every level, in the dialog that shows the statements; creating is asked again where Confirm writes asks; Read only refuses. Each runs at once, as the Linux app's do, in the bar exports use, whose Cancel stops one waiting on a lock; leaving the window stops it too. A stopped change reads the tree again and closes nothing. The commands are hidden while the bar is busy. -- After a drop, the table's tabs close without asking, since their changes have nowhere to go, and are not kept to reopen; so do the table and definition tabs of every object the catalog no longer lists, as a partitioned table's partitions, the views Cascade drops and a dropped table's triggers. A truncate reads every table tab again, since Cascade and a partitioned table empty others; a tab holding edits asks first. A change whose outcome is unknown reads the tree again and closes nothing. The tree reads the schemas again on every refresh. +- After a drop, the table's tabs close without asking, since their changes have nowhere to go, and are not kept to reopen; so do the table and definition tabs of every object the catalog no longer lists, as a partitioned table's partitions, the views Cascade drops and a dropped table's triggers. Every other table tab reads its keys, structure and filter choices again, since a cascade can take its foreign key, and keeps its rows and edits. A truncate reads every table tab again, since Cascade and a partitioned table empty others; a tab holding edits comes forward and asks first. A change whose outcome is unknown reads the tree again and closes nothing. The tree reads the schemas again on every refresh. ## Rationale diff --git a/windows/src/TablePro.App.Model/TableTab.cs b/windows/src/TablePro.App.Model/TableTab.cs index 36556f7aa6..e875749d4f 100644 --- a/windows/src/TablePro.App.Model/TableTab.cs +++ b/windows/src/TablePro.App.Model/TableTab.cs @@ -521,6 +521,23 @@ public async Task RefreshAsync() } } + // Another table's drop can take a foreign key of this one with it: its keys, structure and filter choices are read + // again, and the rows and their edits stay. + public void ForgetCatalog() + { + _reopenTableEditor = true; + _keysVersion++; + _keysStale = true; + _keys?.Dispose(); + _keys = null; + _structure.MarkStale(); + _choices.MarkStale(); + if (!IsRunning && !IsSaving && _shown) + { + ReadKeysIfNeeded(); + } + } + // 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) { diff --git a/windows/src/TablePro.App/Views/TableTabView.xaml.cs b/windows/src/TablePro.App/Views/TableTabView.xaml.cs index 49726dfbd9..c077211353 100644 --- a/windows/src/TablePro.App/Views/TableTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/TableTabView.xaml.cs @@ -934,6 +934,13 @@ private async void OnCountRows(object sender, RoutedEventArgs e) => private async void OnRefresh(object sender, RoutedEventArgs e) => await RefreshRowsAsync().ConfigureAwait(true); // As Refresh does, for rows written from outside the tab, such as an import. + // The rows and their edits stay; a structure or filter bar on screen reads again. + internal async Task ForgetCatalogAsync() + { + _model.ForgetCatalog(); + await ReloadShownAsync().ConfigureAwait(true); + } + internal async Task RefreshRowsAsync() { await _model.RefreshAsync().ConfigureAwait(true); diff --git a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs index 9ef277668b..5cf2aae96e 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs @@ -129,11 +129,17 @@ await CatalogDialogs.ShowRefusedAsync( } // A cascade empties the tables pointing at this one, and a partitioned table its partitions, so every table tab - // reads again; one holding edits asks first. - foreach (var view in TableViews()) + // reads again; one holding edits comes forward first, since the question names no table. + var items = Tabs.TabItems.OfType().Where(item => item.Content is TableTabView).ToList(); + foreach (var item in items) { - if (TableViews().Contains(view)) + if (item.Content is TableTabView view && Tabs.TabItems.Contains(item)) { + if (view.Model.HasUnsavedChanges) + { + Tabs.SelectedItem = item; + } + await view.RefreshRowsAsync().ConfigureAwait(true); } } @@ -428,6 +434,12 @@ private async Task DiscardGoneTabsAsync(DatabaseSession session, string? dropped } await DiscardObjectTabsAsync(gone.Contains).ConfigureAwait(true); + + // A table that stays can lose a foreign key into one that went, so every other table tab reads its keys again. + foreach (var view in TableViews()) + { + await view.ForgetCatalogAsync().ConfigureAwait(true); + } } // What a table or definition tab shows; a table tab's kind is left open, since a table and a view read alike. diff --git a/windows/tests/TablePro.App.Model.Tests/TableTabReferenceTests.cs b/windows/tests/TablePro.App.Model.Tests/TableTabReferenceTests.cs index e7b2ec3164..ac3cb9ccac 100644 --- a/windows/tests/TablePro.App.Model.Tests/TableTabReferenceTests.cs +++ b/windows/tests/TablePro.App.Model.Tests/TableTabReferenceTests.cs @@ -89,6 +89,22 @@ public async Task APendingChangeInAKeysColumnHidesThatKeyButNotTheRowsPointingAt Assert.True(_tab.ReferencesAt(0, 5).References.IsEmpty); } + [Fact] + public async Task AnotherTablesDropReadsTheKeysAgainAndLeavesTheRowsAndEdits() + { + await _tab.RunAsync(); + _tab.CommitEdit(0, 1, "changed"); + var first = Assert.Single(_session.KeyReads); + var reads = _session.Queries.Count; + + _tab.ForgetCatalog(); + + Assert.Equal(2, _session.KeyReads.Count); + Assert.True(first.IsDisposed); + Assert.Equal(reads, _session.Queries.Count); + Assert.True(_tab.HasUnsavedChanges); + } + [Fact] public async Task ARefreshWhoseKeyReadFailsOffersNoneOfTheKeysReadBefore() { From 14b346e7112147c03163bcb9be7d753752b899ce Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 11:45:06 +0700 Subject: [PATCH 13/16] fix(windows): name the new database choices apart from the query tab's database list --- windows/src/TablePro.App/Views/CatalogDialogs.cs | 4 ++-- windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs | 6 +++--- windows/src/TablePro.Interop/CatalogChange.cs | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/windows/src/TablePro.App/Views/CatalogDialogs.cs b/windows/src/TablePro.App/Views/CatalogDialogs.cs index 4cb38be89f..ff07ee394e 100644 --- a/windows/src/TablePro.App/Views/CatalogDialogs.cs +++ b/windows/src/TablePro.App/Views/CatalogDialogs.cs @@ -82,7 +82,7 @@ internal static class CatalogDialogs FrameworkElement owner, DatabaseSession session, CatalogAbilities abilities, - DatabaseChoices choices) + NewDatabaseChoices choices) { var form = new Form(Strings.Get("NewSchemaTitle"), null, "CreateAction"); var name = form.Name(); @@ -97,7 +97,7 @@ internal static class CatalogDialogs FrameworkElement owner, DatabaseSession session, CatalogAbilities abilities, - DatabaseChoices choices) + NewDatabaseChoices choices) { var fields = abilities.DatabaseFields; var form = new Form(Strings.Get(abilities.Keyspaces ? "NewKeyspaceTitle" : "NewDatabaseTitle"), null, "CreateAction"); diff --git a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs index 5cf2aae96e..dda8cf08e7 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs +++ b/windows/src/TablePro.App/Views/WorkspaceView.Catalog.cs @@ -29,7 +29,7 @@ public async Task NewSchemaAsync() } var abilities = Abilities; - var choices = abilities.SchemaOwner ? await ChoicesAsync(session).ConfigureAwait(true) : DatabaseChoices.None; + var choices = abilities.SchemaOwner ? await ChoicesAsync(session).ConfigureAwait(true) : NewDatabaseChoices.None; if (!Holds(session)) { return; @@ -367,7 +367,7 @@ private static async Task> ReferencingAsync(DatabaseSes } // A server that lists none leaves every picker with only its own default. - private static async Task ChoicesAsync(DatabaseSession session) + private static async Task ChoicesAsync(DatabaseSession session) { try { @@ -375,7 +375,7 @@ private static async Task ChoicesAsync(DatabaseSession session) } catch (Exception exception) when (exception is TableProException or ObjectDisposedException) { - return DatabaseChoices.None; + return NewDatabaseChoices.None; } } diff --git a/windows/src/TablePro.Interop/CatalogChange.cs b/windows/src/TablePro.Interop/CatalogChange.cs index c99c9a7df5..448d74c599 100644 --- a/windows/src/TablePro.Interop/CatalogChange.cs +++ b/windows/src/TablePro.Interop/CatalogChange.cs @@ -44,7 +44,7 @@ public sealed record DatabaseFields( public sealed record CharacterSet(string Name, string DefaultCollation, IReadOnlyList Collations); // What the server offers for a new database's options; an empty list leaves only the server's default. -public sealed record DatabaseChoices( +public sealed record NewDatabaseChoices( IReadOnlyList Owners, IReadOnlyList Templates, IReadOnlyList Encodings, @@ -59,7 +59,7 @@ public sealed record DatabaseChoices( string? DefaultCharacterSet, string? DefaultCollation) { - public static DatabaseChoices None { get; } = new([], [], [], [], [], [], [], [], null, null, null, null, null); + public static NewDatabaseChoices None { get; } = new([], [], [], [], [], [], [], [], null, null, null, null, null); } public sealed record DataCenterReplicas(string DataCenter, int Replicas); @@ -197,7 +197,7 @@ public bool ProtectsSchema(string name) return _session.ProtectsSchema(name); } - public async Task GetDatabaseChoicesAsync(CancellationToken cancellationToken = default) + public async Task GetDatabaseChoicesAsync(CancellationToken cancellationToken = default) { var choices = await CoreCall.RunAsync(token => _session.DatabaseChoices(token), cancellationToken) .ConfigureAwait(false); @@ -254,7 +254,7 @@ public static CatalogAbilities Abilities(Native.CatalogAbilities abilities) abilities.OnCluster); } - public static DatabaseChoices Choices(Native.DatabaseChoicesInfo choices) => new( + public static NewDatabaseChoices Choices(Native.DatabaseChoicesInfo choices) => new( choices.Owners, choices.Templates, choices.Encodings, From 9fcbd47dca64c5f2f4a86ea63785a2b2d616780d Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 12:10:01 +0700 Subject: [PATCH 14/16] test(windows): find the connection form's save picker under the form --- windows/tests/TablePro.App.UITests/CatalogTests.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/windows/tests/TablePro.App.UITests/CatalogTests.cs b/windows/tests/TablePro.App.UITests/CatalogTests.cs index f277928ee7..bbd513791c 100644 --- a/windows/tests/TablePro.App.UITests/CatalogTests.cs +++ b/windows/tests/TablePro.App.UITests/CatalogTests.cs @@ -113,8 +113,8 @@ public void ASchemaIsCreatedAndDroppedFromTheDatabaseMenuAndItsRow(string theme) var form = app.WaitForWindow("New connection"); app.Click(app.Find("Engine_duckdb", form), () => app.TryFind("FileBox", form) is not null, form); app.Find("NameBox", form).AsTextBox().Text = "Catalog"; - app.Click(app.Find("NewFileButton", form), () => FileDialog(app) is not null, form); - SaveInFileDialog(app, file); + app.Click(app.Find("NewFileButton", form), () => SaveDialog(app, form) is not null, form); + SaveInFileDialog(app, form, file); app.Find("ConnectButton", form).AsButton().Invoke(); app.WaitForClose("New connection"); var tree = app.Find("ObjectTree"); @@ -144,10 +144,14 @@ public void ASchemaIsCreatedAndDroppedFromTheDatabaseMenuAndItsRow(string theme) throwOnTimeout: false).Success); } + // The form owns its picker, so UI Automation lists it under the form, the app's window or the desktop. + private static Window? SaveDialog(AppSession app, Window form) => + form.FindFirstChild(condition => condition.ByClassName("#32770"))?.AsWindow() ?? FileDialog(app); + // The Save dialog's File name box is control 1001, and Enter saves to the path typed there. - private static void SaveInFileDialog(AppSession app, string path) + private static void SaveInFileDialog(AppSession app, Window form, string path) { - var dialog = Retry.WhileNull(() => FileDialog(app), AppSession.Wait, throwOnTimeout: false).Result + var dialog = Retry.WhileNull(() => SaveDialog(app, form), AppSession.Wait, throwOnTimeout: false).Result ?? throw new InvalidOperationException("The Save dialog did not appear."); var name = Retry.WhileNull( () => dialog.FindFirstDescendant(condition => @@ -159,7 +163,7 @@ private static void SaveInFileDialog(AppSession app, string path) name.Focus(); name.AsTextBox().Text = path; Keyboard.Type(VirtualKeyShort.RETURN); - if (!Retry.WhileFalse(() => FileDialog(app) is null, AppSession.Wait, throwOnTimeout: false).Success) + if (!Retry.WhileFalse(() => SaveDialog(app, form) is null, AppSession.Wait, throwOnTimeout: false).Success) { throw new InvalidOperationException("The Save dialog did not take the path."); } From 592600ee9e3abf4cccc8ea3ba3f4a759ada718ec Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 12:43:28 +0700 Subject: [PATCH 15/16] test(windows): type the new DuckDB file's path into the save picker --- windows/tests/TablePro.App.UITests/CatalogTests.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/windows/tests/TablePro.App.UITests/CatalogTests.cs b/windows/tests/TablePro.App.UITests/CatalogTests.cs index bbd513791c..1a3b6cf456 100644 --- a/windows/tests/TablePro.App.UITests/CatalogTests.cs +++ b/windows/tests/TablePro.App.UITests/CatalogTests.cs @@ -159,9 +159,16 @@ private static void SaveInFileDialog(AppSession app, Window form, string path) AppSession.Wait, throwOnTimeout: false).Result ?? throw new InvalidOperationException("The Save dialog has no File name box."); + // The Save dialog keeps its suggested name when the box's value is set without typing, so the path is typed. dialog.SetForeground(); name.Focus(); - name.AsTextBox().Text = path; + Keyboard.TypeSimultaneously(VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_A); + Keyboard.Type(path); + if (!Retry.WhileFalse(() => name.AsTextBox().Text == path, AppSession.Wait, throwOnTimeout: false).Success) + { + throw new InvalidOperationException("The Save dialog did not take the path typed."); + } + Keyboard.Type(VirtualKeyShort.RETURN); if (!Retry.WhileFalse(() => SaveDialog(app, form) is null, AppSession.Wait, throwOnTimeout: false).Success) { From e4d37371f4595c2d868b38fd16707938c7ded1d5 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 16 Nov 2026 13:11:28 +0700 Subject: [PATCH 16/16] test(windows): run the schema flow on the x64 runner's PostgreSQL fixture --- .../TablePro.App.UITests/CatalogTests.cs | 79 ++++++++----------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/windows/tests/TablePro.App.UITests/CatalogTests.cs b/windows/tests/TablePro.App.UITests/CatalogTests.cs index 1a3b6cf456..6ca1f531d8 100644 --- a/windows/tests/TablePro.App.UITests/CatalogTests.cs +++ b/windows/tests/TablePro.App.UITests/CatalogTests.cs @@ -1,8 +1,10 @@ +using System.Globalization; using FlaUI.Core.AutomationElements; using FlaUI.Core.Definitions; using FlaUI.Core.Input; using FlaUI.Core.Tools; using FlaUI.Core.WindowsAPI; +using TablePro.Testing; namespace TablePro.App.UITests; @@ -107,72 +109,57 @@ public void AQueryResultExportsAsSqlInsert(string theme) [MemberData(nameof(Themes))] public void ASchemaIsCreatedAndDroppedFromTheDatabaseMenuAndItsRow(string theme) { + Assert.SkipUnless(SshTestEnvironment.Available, "Requires the Windows PostgreSQL fixture."); using var app = AppSession.Launch(theme); - var file = Path.Combine(app.DataFolder, "catalog.duckdb"); app.Click(app.Find("NewConnectionCard"), () => app.HasWindow("New connection")); var form = app.WaitForWindow("New connection"); - app.Click(app.Find("Engine_duckdb", form), () => app.TryFind("FileBox", form) is not null, form); - app.Find("NameBox", form).AsTextBox().Text = "Catalog"; - app.Click(app.Find("NewFileButton", form), () => SaveDialog(app, form) is not null, form); - SaveInFileDialog(app, form, file); + app.Click(app.Find("Engine_postgres", form), () => app.TryFind("HostBox", form) is not null, form); + app.Find("NameBox", form).AsTextBox().Text = "PostgreSQL schemas"; + app.Find("HostBox", form).AsTextBox().Text = "127.0.0.1"; + app.Find("PortBox", form).AsTextBox().Text = SshTestEnvironment.DatabasePort.ToString(CultureInfo.InvariantCulture); + app.Find("UserBox", form).AsTextBox().Text = "tablepro_ssh"; + app.Find("DatabaseBox", form).AsTextBox().Text = "tablepro_ssh"; + app.Find("PasswordInput", form).Patterns.Value.Pattern.SetValue(SshTestEnvironment.DatabasePassword); app.Find("ConnectButton", form).AsButton().Invoke(); app.WaitForClose("New connection"); var tree = app.Find("ObjectTree"); + app.FindByName("ssh_proof", tree); + + // The dialog shows the options PostgreSQL names for a new database, each starting on the server's own. + app.Click(app.Find("DatabaseMenu"), () => app.TryFindMenuItem("New database...") is not null); + app.Click(app.FindMenuItem("New database..."), () => app.TryFind("CatalogName") is not null); + Assert.NotNull(app.TryFind("CatalogOwner")); + Assert.NotNull(app.TryFind("CatalogEncoding")); + app.Find("CatalogName").AsTextBox().Text = "tablepro_shop"; + Assert.Equal( + "CREATE DATABASE \"tablepro_shop\";", + AppSession.WaitForText(app.Find("CatalogStatements"), text => text.Length > 0)); + app.Screenshot("106-new-database"); + app.CloseDialog(); + Assert.True(Retry.WhileTrue(() => app.TryFind("CatalogName") is not null, AppSession.Wait, throwOnTimeout: false).Success); app.Click(app.Find("DatabaseMenu"), () => app.TryFindMenuItem("New schema...") is not null); app.Click(app.FindMenuItem("New schema..."), () => app.TryFind("CatalogName") is not null); - app.Find("CatalogName").AsTextBox().Text = "sales"; + app.Find("CatalogName").AsTextBox().Text = "tablepro_sales"; Assert.Equal( - "CREATE SCHEMA \"sales\";", + "CREATE SCHEMA \"tablepro_sales\";", AppSession.WaitForText(app.Find("CatalogStatements"), text => text.Length > 0)); - app.Screenshot("106-new-schema"); + app.Screenshot("107-new-schema"); app.PressDialogButton("Create"); var sales = Retry.WhileNull( - () => tree.FindFirstDescendant(condition => condition.ByName("sales")), + () => tree.FindFirstDescendant(condition => condition.ByName("tablepro_sales")), AppSession.Wait, throwOnTimeout: false).Result ?? throw new InvalidOperationException("The new schema did not show."); - app.RightClick(sales, () => app.TryFindMenuItem("Drop schema “sales”...") is not null); - app.Click(app.FindMenuItem("Drop schema “sales”..."), () => app.TryFind("CatalogStatements") is not null); - Assert.Equal("DROP SCHEMA \"sales\";", app.Find("CatalogStatements").Name); - app.Screenshot("107-drop-schema"); + app.RightClick(sales, () => app.TryFindMenuItem("Drop schema “tablepro_sales”...") is not null); + app.Click(app.FindMenuItem("Drop schema “tablepro_sales”..."), () => app.TryFind("CatalogStatements") is not null); + Assert.Equal("DROP SCHEMA \"tablepro_sales\";", app.Find("CatalogStatements").Name); + app.Screenshot("108-drop-schema"); app.PressDialogButton("Drop"); Assert.True(Retry.WhileFalse( - () => tree.FindFirstDescendant(condition => condition.ByName("sales")) is null, + () => tree.FindFirstDescendant(condition => condition.ByName("tablepro_sales")) is null, AppSession.Wait, throwOnTimeout: false).Success); } - - // The form owns its picker, so UI Automation lists it under the form, the app's window or the desktop. - private static Window? SaveDialog(AppSession app, Window form) => - form.FindFirstChild(condition => condition.ByClassName("#32770"))?.AsWindow() ?? FileDialog(app); - - // The Save dialog's File name box is control 1001, and Enter saves to the path typed there. - private static void SaveInFileDialog(AppSession app, Window form, string path) - { - var dialog = Retry.WhileNull(() => SaveDialog(app, form), AppSession.Wait, throwOnTimeout: false).Result - ?? throw new InvalidOperationException("The Save dialog did not appear."); - var name = Retry.WhileNull( - () => dialog.FindFirstDescendant(condition => - condition.ByAutomationId("1001").And(condition.ByControlType(ControlType.Edit))), - AppSession.Wait, - throwOnTimeout: false).Result - ?? throw new InvalidOperationException("The Save dialog has no File name box."); - // The Save dialog keeps its suggested name when the box's value is set without typing, so the path is typed. - dialog.SetForeground(); - name.Focus(); - Keyboard.TypeSimultaneously(VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_A); - Keyboard.Type(path); - if (!Retry.WhileFalse(() => name.AsTextBox().Text == path, AppSession.Wait, throwOnTimeout: false).Success) - { - throw new InvalidOperationException("The Save dialog did not take the path typed."); - } - - Keyboard.Type(VirtualKeyShort.RETURN); - if (!Retry.WhileFalse(() => SaveDialog(app, form) is null, AppSession.Wait, throwOnTimeout: false).Success) - { - throw new InvalidOperationException("The Save dialog did not take the path."); - } - } }