diff --git a/core/crates/core/src/connection.rs b/core/crates/core/src/connection.rs index 028142ba6d..b8212bec31 100644 --- a/core/crates/core/src/connection.rs +++ b/core/crates/core/src/connection.rs @@ -15,7 +15,7 @@ use crate::foreign_key_info::ForeignKeyInfo; use crate::keyspace::{KeyListing, KeyPage, KeyScan, KeyWindow}; use crate::link_watch::LinkState; use crate::meta::{ - IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableRef, + IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, }; use crate::query_result::QueryResult; use crate::read_statement::ReadStatement; @@ -148,6 +148,13 @@ pub trait Connection: Send + Sync { async fn table_estimate(&self, table: &TableRef, options: CallOptions) -> Result; + /// The table's comment and size, as far as the engine keeps them. An + /// engine that keeps neither says nothing rather than failing, so a + /// caller shows what there is. + async fn table_info(&self, _table: &TableRef, _options: CallOptions) -> Result { + Ok(TableInfo::default()) + } + /// Run a generated read and keep at most `limit` rows, marking the /// result truncated when there were more. async fn query_bound( diff --git a/core/crates/core/src/meta/mod.rs b/core/crates/core/src/meta/mod.rs index 3dbbe1d0c1..3198dc25b9 100644 --- a/core/crates/core/src/meta/mod.rs +++ b/core/crates/core/src/meta/mod.rs @@ -8,6 +8,7 @@ mod page; mod row_identity; mod schema_info; mod table_estimate; +mod table_info; mod table_ref; pub use index_info::{IndexInfo, IndexKey}; @@ -17,4 +18,5 @@ pub use page::{CtidWindow, HeapAddress, PageCost, PagePosition, PageStability, P pub use row_identity::{EngineRowId, EngineRowIdPart, RowIdentity}; pub use schema_info::{ObjectScope, SchemaInfo}; pub use table_estimate::{RowCountEstimate, TableEstimate}; +pub use table_info::TableInfo; pub use table_ref::TableRef; diff --git a/core/crates/core/src/meta/table_info.rs b/core/crates/core/src/meta/table_info.rs new file mode 100644 index 0000000000..b0a50fd51e --- /dev/null +++ b/core/crates/core/src/meta/table_info.rs @@ -0,0 +1,39 @@ +/// What the engine keeps about a table beyond its columns: its comment +/// and the space it takes. Each figure is `None` where the engine keeps +/// none or the login may not read it. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct TableInfo { + pub comment: Option, + /// The rows themselves, with the out-of-line pages an engine keeps + /// apart for long values (PostgreSQL's TOAST, SQL Server's LOB pages). + pub data_bytes: Option, + pub index_bytes: Option, + /// Everything the table holds on disk, which can be more than its + /// data and indexes together. + pub total_bytes: Option, +} + +impl TableInfo { + /// A blank comment is no comment. + pub fn with_comment(mut self, comment: Option) -> Self { + self.comment = comment.filter(|text| !text.trim().is_empty()); + self + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_blank_comment_is_none() { + assert_eq!(TableInfo::default().with_comment(Some(" ".to_owned())).comment, None); + assert_eq!( + TableInfo::default() + .with_comment(Some("Orders".to_owned())) + .comment + .as_deref(), + Some("Orders") + ); + } +} diff --git a/core/crates/drivers/cassandra/src/catalog.rs b/core/crates/drivers/cassandra/src/catalog.rs index 2f1e534306..119b58b475 100644 --- a/core/crates/drivers/cassandra/src/catalog.rs +++ b/core/crates/drivers/cassandra/src/catalog.rs @@ -9,7 +9,7 @@ use tablepro_core::ddl::{ TableDefinition, TableSource, }; use tablepro_core::meta::{ - IndexInfo, IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowIdentity, SchemaInfo, TableRef, + IndexInfo, IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowIdentity, SchemaInfo, TableInfo, TableRef, }; use scylla::value::{CqlValue, Row}; @@ -388,6 +388,24 @@ async fn view_row(link: &CqlLink, table: &CatalogTable) -> Result Result { + for sql in [ + "SELECT comment FROM system_schema.tables WHERE keyspace_name = ? AND table_name = ?", + "SELECT comment FROM system_schema.views WHERE keyspace_name = ? AND view_name = ?", + ] { + let rows = link + .catalogue(sql, (table.keyspace.as_str(), table.name.as_str())) + .await? + .into_rows_result() + .map_err(rows_error)?; + if let Some((comment,)) = rows.maybe_first_row::<(Option,)>().map_err(rows_error)? { + return Ok(TableInfo::default().with_comment(comment)); + } + } + Ok(TableInfo::default()) +} + /// The options a CREATE statement sets, in the order cqlsh's DESCRIBE /// writes them. A release keeps only some of them; the rest are left /// out, and so are the read repair chances Cassandra 4.0 dropped from diff --git a/core/crates/drivers/cassandra/src/connection.rs b/core/crates/drivers/cassandra/src/connection.rs index 607ee1f4f3..ba10c05888 100644 --- a/core/crates/drivers/cassandra/src/connection.rs +++ b/core/crates/drivers/cassandra/src/connection.rs @@ -8,7 +8,7 @@ use tablepro_core::column::ColumnInfo; use tablepro_core::ddl::{DatabaseChoices, InboundForeignKey, OutboundForeignKey, TableDefinition}; use tablepro_core::dialect::SqlDialect; use tablepro_core::meta::{ - IndexInfo, ObjectInfo, ObjectScope, RowCountEstimate, RowIdentity, SchemaInfo, TableEstimate, TableRef, + IndexInfo, ObjectInfo, ObjectScope, RowCountEstimate, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, }; use tablepro_core::statement::Statement; use tablepro_core::{ @@ -354,6 +354,16 @@ impl Connection for CqlConnection { }) } + /// CQL keeps a comment. A table's size is each node's own, kept in + /// its metrics rather than in a table a client reads. + async fn table_info(&self, table: &TableRef, options: CallOptions) -> Result { + let table = self.catalog_table(table)?; + self.read(options, move |link, _| async move { + catalog::table_info(&link, &table).await + }) + .await + } + async fn query_bound( &self, statement: &Statement, diff --git a/core/crates/drivers/cassandra/tests/integration/metadata.rs b/core/crates/drivers/cassandra/tests/integration/metadata.rs index 5c659146b5..5d0277780d 100644 --- a/core/crates/drivers/cassandra/tests/integration/metadata.rs +++ b/core/crates/drivers/cassandra/tests/integration/metadata.rs @@ -2,7 +2,7 @@ //! views, a table's key, and the CREATE statements the server's schema //! tables describe. -use tablepro_core::meta::{ObjectKind, ObjectScope, RowIdentity}; +use tablepro_core::meta::{ObjectKind, ObjectScope, RowIdentity, TableInfo}; use tablepro_core::{Connection, TlsConfig, Value, WorkspaceSettings}; use tablepro_test_fixtures::CassandraFlavour; use tablepro_test_support::conformance::test_liveness; @@ -101,6 +101,48 @@ async fn a_table_is_keyed_by_its_primary_key_and_a_view_is_read_only( assert_eq!(view_identity, RowIdentity::Unordered); } +async fn a_table_and_a_view_report_their_comment_alone() { + let fixture = start::().await; + let connection = connect(&fixture, false).await; + setup( + connection.as_ref(), + &[ + ORDERS, + NOTE_INDEX, + "ALTER TABLE tablepro.orders WITH comment = 'the orders'", + "INSERT INTO tablepro.orders (customer, placed, total) VALUES (1, '2024-01-01', 1.5)", + "CREATE MATERIALIZED VIEW tablepro.orders_by_total AS SELECT * FROM tablepro.orders \ + WHERE customer IS NOT NULL AND placed IS NOT NULL AND total IS NOT NULL \ + PRIMARY KEY (total, customer, placed) WITH comment = 'by total'", + "CREATE TABLE tablepro.plain (id int PRIMARY KEY)", + ], + ) + .await; + + let orders = connection + .table_info(&table("orders"), within(CALL)) + .await + .expect("the table's info"); + let view = connection + .table_info(&table("orders_by_total"), within(CALL)) + .await + .expect("the view's info"); + let plain = connection + .table_info(&table("plain"), within(CALL)) + .await + .expect("the plain table's info"); + + assert_eq!( + orders, + TableInfo { + comment: Some("the orders".to_owned()), + ..TableInfo::default() + } + ); + assert_eq!(view.comment.as_deref(), Some("by total")); + assert_eq!(plain, TableInfo::default()); +} + async fn the_create_script_rebuilds_the_table_it_was_read_from() { let fixture = start::().await; let connection = connect(&fixture, false).await; @@ -263,3 +305,15 @@ async fn cassandra_create_script_rebuilds_the_table() { async fn scylla_create_script_rebuilds_the_table() { the_create_script_rebuilds_the_table_it_was_read_from::().await; } + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn cassandra_table_info_is_the_comment() { + a_table_and_a_view_report_their_comment_alone::().await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn scylla_table_info_is_the_comment() { + a_table_and_a_view_report_their_comment_alone::().await; +} diff --git a/core/crates/drivers/clickhouse/src/catalog/mod.rs b/core/crates/drivers/clickhouse/src/catalog/mod.rs index a88669b2e0..9b6def6b6b 100644 --- a/core/crates/drivers/clickhouse/src/catalog/mod.rs +++ b/core/crates/drivers/clickhouse/src/catalog/mod.rs @@ -6,7 +6,7 @@ mod table_definition; use clickhouse::query::Query; use tablepro_core::DriverError; use tablepro_core::ddl::ddl_sql::parenthesized; -use tablepro_core::meta::{IndexInfo, RowIdentity, TableEstimate}; +use tablepro_core::meta::{IndexInfo, RowIdentity, TableEstimate, TableInfo}; pub(crate) use clickhouse_column_row::ClickHouseColumnRow; pub(crate) use table_definition::fetch_table_definition; @@ -138,3 +138,50 @@ pub(crate) async fn table_estimate(calls: &CallClient, table: &CatalogTable) -> heap_blocks: None, }) } + +/// `system.tables` keeps one figure: a MergeTree table's bytes on disk, +/// NULL for an engine that keeps none, with no split into data and +/// indexes. +pub(crate) async fn table_info(calls: &CallClient, table: &CatalogTable) -> Result { + let sql = "SELECT comment, total_bytes FROM system.tables \ + WHERE database = {db:String} AND name = {tbl:String}"; + let result = read_table(table.bind(calls.raw(sql)?), None).await?; + match result.rows.first() { + Some(row) => table_info_of(&CatalogRow(row)), + None => Ok(TableInfo::default()), + } +} + +fn table_info_of(row: &CatalogRow<'_>) -> Result { + Ok(TableInfo { + total_bytes: row.optional_u64(1)?, + ..TableInfo::default() + } + .with_comment(Some(row.text(0)?))) +} + +#[cfg(test)] +mod tests { + use tablepro_core::Value; + + use super::*; + + #[test] + fn a_row_gives_the_total_and_a_blank_comment_is_none() { + let sized = [Value::Text("orders".to_owned()), Value::UInt(4096)]; + let view = [Value::Text(String::new()), Value::Null]; + + assert_eq!( + table_info_of(&CatalogRow(&sized)).expect("the info"), + TableInfo { + comment: Some("orders".to_owned()), + total_bytes: Some(4096), + ..TableInfo::default() + } + ); + assert_eq!( + table_info_of(&CatalogRow(&view)).expect("the info"), + TableInfo::default() + ); + } +} diff --git a/core/crates/drivers/clickhouse/src/connection.rs b/core/crates/drivers/clickhouse/src/connection.rs index 277bd2c97f..60cec22d04 100644 --- a/core/crates/drivers/clickhouse/src/connection.rs +++ b/core/crates/drivers/clickhouse/src/connection.rs @@ -9,7 +9,7 @@ use tablepro_core::ddl::{DatabaseChoices, InboundForeignKey, OutboundForeignKey, use tablepro_core::dialect::SqlDialect; use tablepro_core::dump::{DumpCatalog, DumpScope}; use tablepro_core::meta::{ - IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableRef, + IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, }; use tablepro_core::statement::Statement; use tablepro_core::{ @@ -417,6 +417,14 @@ impl Connection for ClickHouseConnection { .await } + async fn table_info(&self, table: &TableRef, options: CallOptions) -> Result { + let table = self.catalog_table(table); + self.workspace_read(options, move |calls| async move { + catalog::table_info(&calls, &table).await + }) + .await + } + async fn query_bound( &self, statement: &Statement, diff --git a/core/crates/drivers/clickhouse/tests/integration/metadata.rs b/core/crates/drivers/clickhouse/tests/integration/metadata.rs index 6d22afd085..a2c23553cd 100644 --- a/core/crates/drivers/clickhouse/tests/integration/metadata.rs +++ b/core/crates/drivers/clickhouse/tests/integration/metadata.rs @@ -1,7 +1,7 @@ //! What the catalogue says, and SQL the dialect builds against it. use tablepro_core::column::{ColumnKind, TemporalZone, TextLength}; -use tablepro_core::meta::{IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowCountEstimate, RowIdentity}; +use tablepro_core::meta::{IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowCountEstimate, RowIdentity, TableInfo}; use tablepro_core::statement::StatementBuilder; use tablepro_core::{ ConnectTarget, Connection, DatabaseDriver, DriverError, FilterOp, FilterRule, FilterSet, FilterValue, RowLimit, @@ -208,6 +208,43 @@ async fn fetch_table_definition_reports_keys_comment_and_source() { connection.close().await.expect("a close"); } +/// MergeTree keeps its bytes on disk as one figure, a view none. +#[tokio::test] +#[ignore = "requires docker"] +async fn table_info_reports_comment_and_total_bytes() { + let fixture = start().await; + let connection = connect(&fixture, false).await; + let (sized, view) = (unique("sized"), unique("sized_view")); + setup( + connection.as_ref(), + &[ + &format!( + "CREATE TABLE {} (id UInt64, label String, INDEX by_label label TYPE bloom_filter GRANULARITY 1) \ + ENGINE = MergeTree ORDER BY id COMMENT 'the sized one'", + quoted(&sized) + ), + &format!("INSERT INTO {} VALUES (1, 'a'), (2, 'b'), (3, 'c')", quoted(&sized)), + &format!("CREATE VIEW {} AS SELECT id FROM {}", quoted(&view), quoted(&sized)), + ], + ) + .await; + + let table_info = connection + .table_info(&table(&sized), within(CALL)) + .await + .expect("the table's info"); + let view_info = connection + .table_info(&table(&view), within(CALL)) + .await + .expect("the view's info"); + + assert_eq!(table_info.comment.as_deref(), Some("the sized one")); + assert!(table_info.total_bytes.is_some_and(|total| total > 0), "{table_info:?}"); + assert_eq!((table_info.data_bytes, table_info.index_bytes), (None, None)); + assert_eq!(view_info, TableInfo::default()); + connection.close().await.expect("a close"); +} + #[tokio::test] #[ignore = "requires docker"] async fn objects_and_schemas_are_listed_by_kind() { diff --git a/core/crates/drivers/duckdb/src/catalog/mod.rs b/core/crates/drivers/duckdb/src/catalog/mod.rs index e8b6679d34..0d68c77c0b 100644 --- a/core/crates/drivers/duckdb/src/catalog/mod.rs +++ b/core/crates/drivers/duckdb/src/catalog/mod.rs @@ -17,7 +17,7 @@ use tablepro_core::ddl::{ use tablepro_core::edit::parse_literal_text; use tablepro_core::meta::{ IndexInfo, IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowCountEstimate, RowIdentity, SchemaInfo, TableEstimate, - TableRef, + TableInfo, TableRef, }; use tablepro_core::sql_syntax::{LiteralToken, MAX_EXPRESSION_LEN, SqlGrammar, parse_expression}; use tablepro_core::value::Value; @@ -696,6 +696,22 @@ pub(crate) fn table_estimate(link: &Link, catalog: &str, table: &TableRef) -> Re }) } +/// DuckDB keeps a comment on a table or a view, and no figure for the +/// bytes either takes. +pub(crate) fn table_info(link: &Link, catalog: &str, table: &TableRef) -> Result { + let schema = schema_of(table); + let rows = text_rows( + link, + "SELECT comment FROM duckdb_tables() WHERE database_name = ? AND schema_name = ? AND table_name = ? \ + UNION ALL \ + SELECT comment FROM duckdb_views() WHERE database_name = ? AND schema_name = ? AND view_name = ?", + &[catalog, schema, &table.name, catalog, schema, &table.name], + &mut CatalogBudget::default(), + )?; + let comment = rows.into_iter().next().and_then(|row| row.into_iter().next().flatten()); + Ok(TableInfo::default().with_comment(comment)) +} + /// The table as structure changes plan against it. pub(crate) fn fetch_table_definition( link: &Link, diff --git a/core/crates/drivers/duckdb/src/connection.rs b/core/crates/drivers/duckdb/src/connection.rs index ac7387de23..a85b0b7d7d 100644 --- a/core/crates/drivers/duckdb/src/connection.rs +++ b/core/crates/drivers/duckdb/src/connection.rs @@ -5,7 +5,9 @@ use async_trait::async_trait; use tablepro_core::ddl::{InboundForeignKey, OutboundForeignKey, TableDefinition}; use tablepro_core::dialect::SqlDialect; use tablepro_core::dump::{DumpCatalog, DumpScope}; -use tablepro_core::meta::{IndexInfo, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableRef}; +use tablepro_core::meta::{ + IndexInfo, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, +}; use tablepro_core::statement::Statement; use tablepro_core::{ CallOptions, CatalogBudget, ColumnInfo, CommitResolution, CommitToken, Connection, DriverError, EditorSession, @@ -393,6 +395,12 @@ impl Connection for DuckDbConnection { .await } + async fn table_info(&self, table: &TableRef, options: CallOptions) -> Result { + let table = table.clone(); + self.read(options, move |link, catalog| catalog::table_info(link, catalog, &table)) + .await + } + async fn query_bound( &self, statement: &Statement, diff --git a/core/crates/drivers/duckdb/tests/integration/catalog.rs b/core/crates/drivers/duckdb/tests/integration/catalog.rs index b6ac813fd4..e282076abd 100644 --- a/core/crates/drivers/duckdb/tests/integration/catalog.rs +++ b/core/crates/drivers/duckdb/tests/integration/catalog.rs @@ -2,7 +2,7 @@ use tablepro_core::column::ColumnDefault; use tablepro_core::dump::DumpScope; -use tablepro_core::meta::{IndexKey, ObjectKind, ObjectScope, RowCountEstimate, RowIdentity, TableRef}; +use tablepro_core::meta::{IndexKey, ObjectKind, ObjectScope, RowCountEstimate, RowIdentity, TableInfo, TableRef}; use tablepro_core::{RunEnd, Value}; use crate::support::{TestFile, apply, call, query, run, session}; @@ -388,6 +388,55 @@ async fn the_estimate_is_the_storage_count() { connection.close().await.unwrap(); } +/// DuckDB keeps a table's or a view's comment and no sizes. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn table_info_is_the_comment_alone() { + let file = TestFile::new(); + let connection = file.open().await; + apply( + connection.as_ref(), + &[ + "CREATE TABLE t (a INTEGER)", + "CREATE INDEX t_a ON t (a)", + "INSERT INTO t SELECT range FROM range(100)", + "COMMENT ON TABLE t IS 'the table'", + "CREATE VIEW v AS SELECT a FROM t", + "COMMENT ON VIEW v IS 'the view'", + "CREATE TABLE plain (a INTEGER)", + ], + ) + .await + .unwrap(); + + let table = connection + .table_info(&TableRef::unqualified("t"), call()) + .await + .unwrap(); + let view = connection + .table_info(&TableRef::unqualified("v"), call()) + .await + .unwrap(); + let plain = connection + .table_info(&TableRef::unqualified("plain"), call()) + .await + .unwrap(); + + assert_eq!( + table, + TableInfo { + comment: Some("the table".to_owned()), + ..TableInfo::default() + } + ); + assert_eq!(view.comment.as_deref(), Some("the view")); + assert_eq!( + (view.data_bytes, view.index_bytes, view.total_bytes), + (None, None, None) + ); + assert_eq!(plain, TableInfo::default()); + connection.close().await.unwrap(); +} + /// A schema can share its name with the file's catalogue, and DuckDB /// refuses `"test"."t"` as ambiguous then, so the app names a table with /// the catalogue as well, and its structure changes run. diff --git a/core/crates/drivers/mongodb/src/catalog.rs b/core/crates/drivers/mongodb/src/catalog.rs index 4b1e80a7bf..e61b6b9450 100644 --- a/core/crates/drivers/mongodb/src/catalog.rs +++ b/core/crates/drivers/mongodb/src/catalog.rs @@ -6,12 +6,15 @@ use bson::{Binary, Bson, Document, doc}; use mongodb::{Client, ClientSession}; use tablepro_core::column::ColumnKind; use tablepro_core::document::NUMBER_TYPE; -use tablepro_core::meta::{IndexInfo, IndexKey, ObjectInfo, ObjectKind, RowCountEstimate, SchemaInfo, TableEstimate}; +use tablepro_core::meta::{ + IndexInfo, IndexKey, ObjectInfo, ObjectKind, RowCountEstimate, SchemaInfo, TableEstimate, TableInfo, +}; use tablepro_core::{ColumnInfo, DriverError, LossPhase}; use crate::call::ReadStop; use crate::cursor_rows::Read; use crate::endpoint::MongoEndpoint; +use crate::errors::{UNAUTHORIZED, server_code}; use crate::field_types::{field_column, type_alias}; use crate::shell_command::Plan; @@ -190,6 +193,63 @@ impl Reader<'_> { heap_blocks: None, }) } + + /// The collection's sizes from `$collStats`. A view and a missing + /// collection store nothing, and a login may read a collection + /// without the `collStats` action. + pub(crate) async fn table_info(&mut self, database: &str, collection: &str) -> Result { + let listed = self + .documents( + database, + doc! { "listCollections": 1_i32, "filter": { "name": collection }, "nameOnly": true, "authorizedCollections": true }, + ) + .await?; + match listed.first() { + Some(entry) if entry.get_str("type").ok() != Some("view") => {} + _ => return Ok(TableInfo::default()), + } + let pipeline = vec![doc! { "$collStats": { "storageStats": {} } }]; + let command = doc! { "aggregate": collection, "pipeline": pipeline, "cursor": {} }; + match self.documents(database, command).await { + Ok(stats) => Ok(collection_sizes(&stats)), + Err(error) if server_code(&error) == Some(UNAUTHORIZED) => Ok(TableInfo::default()), + Err(error) => Err(error), + } + } +} + +/// `$collStats` answers once for each shard that holds the collection. +/// Where a shard's answer has no `totalSize`, its data and indexes +/// together are its total. +fn collection_sizes(stats: &[Document]) -> TableInfo { + let field = |storage: &Document, name: &str| bytes(storage.get(name)); + TableInfo { + comment: None, + data_bytes: summed(stats, |storage| field(storage, "storageSize")), + index_bytes: summed(stats, |storage| field(storage, "totalIndexSize")), + total_bytes: summed(stats, |storage| { + field(storage, "totalSize") + .or_else(|| field(storage, "storageSize")?.checked_add(field(storage, "totalIndexSize")?)) + }), + } +} + +fn summed(stats: &[Document], read: impl Fn(&Document) -> Option) -> Option { + if stats.is_empty() { + return None; + } + stats.iter().try_fold(0_u64, |sum, shard| { + sum.checked_add(read(shard.get_document("storageStats").ok()?)?) + }) +} + +fn bytes(value: Option<&Bson>) -> Option { + match value? { + Bson::Int32(number) => u64::try_from(*number).ok(), + Bson::Int64(number) => u64::try_from(*number).ok(), + Bson::Double(number) if number.is_finite() && *number >= 0.0 => Some(*number as u64), + _ => None, + } } /// Each collection's columns as this connection last read them, so a @@ -417,6 +477,25 @@ fn index_info(entry: &Document) -> Option { mod tests { use super::*; + #[test] + fn sizes_sum_over_shards_and_a_total_falls_back_to_data_and_indexes() { + let stats = [ + doc! { "storageStats": { "storageSize": 4096_i32, "totalIndexSize": 8192_i64, "totalSize": 12288_i64 } }, + doc! { "storageStats": { "storageSize": 1000.0, "totalIndexSize": 24_i32 } }, + ]; + + assert_eq!( + collection_sizes(&stats), + TableInfo { + comment: None, + data_bytes: Some(5096), + index_bytes: Some(8216), + total_bytes: Some(13312), + } + ); + assert_eq!(collection_sizes(&[]), TableInfo::default()); + } + fn tally(name: &str, kind: &str, position: i64) -> Document { doc! { "_id": { "name": name, "type": kind }, "position": position } } diff --git a/core/crates/drivers/mongodb/src/connection.rs b/core/crates/drivers/mongodb/src/connection.rs index 26b2f01cd6..8a05b48140 100644 --- a/core/crates/drivers/mongodb/src/connection.rs +++ b/core/crates/drivers/mongodb/src/connection.rs @@ -12,7 +12,9 @@ use tablepro_core::ddl::{ ColumnAttributes, ColumnDefinition, InboundForeignKey, OutboundForeignKey, PrimaryKeyConstraint, TableDefinition, }; use tablepro_core::dialect::SqlDialect; -use tablepro_core::meta::{IndexInfo, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableRef}; +use tablepro_core::meta::{ + IndexInfo, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, +}; use tablepro_core::sql_syntax::StatementAccess; use tablepro_core::sql_syntax::mongo::access_of; use tablepro_core::statement::Statement; @@ -443,6 +445,14 @@ impl Connection for MongoConnection { .await } + async fn table_info(&self, table: &TableRef, options: CallOptions) -> Result { + let (database, collection) = (self.database_of(table), table.name.clone()); + self.read(options, move |context, session| { + Box::pin(async move { reader(&context, session).table_info(&database, &collection).await }) + }) + .await + } + async fn query_bound( &self, statement: &Statement, diff --git a/core/crates/drivers/mongodb/src/errors.rs b/core/crates/drivers/mongodb/src/errors.rs index 5507201759..2bd82d1af2 100644 --- a/core/crates/drivers/mongodb/src/errors.rs +++ b/core/crates/drivers/mongodb/src/errors.rs @@ -23,6 +23,8 @@ pub(crate) fn closed_unanswered() -> DriverError { /// `AuthenticationFailed`: the user or the password is wrong. pub(crate) const AUTHENTICATION_FAILED: i32 = 18; +/// `Unauthorized`: the login lacks the action the command needs. +pub(crate) const UNAUTHORIZED: i32 = 13; /// `MaxTimeMSExpired`: the server stopped the operation at its time limit. pub(crate) const MAX_TIME_MS_EXPIRED: i32 = 50; /// `Interrupted`, `InterruptedAtShutdown`, `CursorKilled` and diff --git a/core/crates/drivers/mongodb/tests/integration/catalog.rs b/core/crates/drivers/mongodb/tests/integration/catalog.rs index 215b21842d..652a2521a1 100644 --- a/core/crates/drivers/mongodb/tests/integration/catalog.rs +++ b/core/crates/drivers/mongodb/tests/integration/catalog.rs @@ -10,7 +10,7 @@ use tablepro_core::ddl::{ plan_truncate_table, }; use tablepro_core::filter::FilterSet; -use tablepro_core::meta::{ObjectScope, PagePosition, SchemaInfo, TableRef}; +use tablepro_core::meta::{ObjectScope, PagePosition, SchemaInfo, TableInfo, TableRef}; use tablepro_core::{ColumnInfo, Connection, RunEnd, Value}; use crate::support::{CALL, DATABASE, connect, editor, rows, run, start, unique, within}; @@ -124,6 +124,52 @@ async fn deleting_every_document_keeps_the_collection_and_its_indexes() { assert!(objects.iter().any(|object| object.name == collection), "{objects:?}"); } +/// A collection's sizes come from `$collStats`, and a view and a missing +/// collection have none. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn table_info_reports_a_collections_sizes_and_none_for_a_view() { + let fixture = start().await; + let connection = connect(&fixture).await; + let session = editor(connection.as_ref()).await; + let (collection, view) = (unique("sized"), unique("sized_view")); + let (filled, _) = run( + session.as_ref(), + &format!( + "db.getCollection('{collection}').insertMany([{{_id: 1, label: 'a'}}, {{_id: 2, label: 'b'}}])\n\ + db.getCollection('{collection}').createIndex({{label: 1}})\n\ + db.createView('{view}', '{collection}', [])" + ), + ) + .await; + assert_eq!(filled.end, RunEnd::Completed); + let in_database = |name: &str| TableRef::new(Some(DATABASE.to_owned()), name.to_owned()); + + let sized = connection + .table_info(&in_database(&collection), within(CALL)) + .await + .expect("the collection's info"); + let viewed = connection + .table_info(&in_database(&view), within(CALL)) + .await + .expect("the view's info"); + let missing = connection + .table_info(&in_database(&unique("missing")), within(CALL)) + .await + .expect("no info"); + + assert_eq!(sized.comment, None); + let (data, index, total) = ( + sized.data_bytes.expect("data"), + sized.index_bytes.expect("indexes"), + sized.total_bytes.expect("total"), + ); + assert!(data > 0 && index > 0, "{sized:?}"); + assert!(total >= data + index, "{sized:?}"); + assert_eq!(viewed, TableInfo::default()); + assert_eq!(missing, TableInfo::default()); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker"] async fn a_new_database_is_listed_with_its_first_collection() { diff --git a/core/crates/drivers/mssql/src/connection.rs b/core/crates/drivers/mssql/src/connection.rs index f80cade819..5691b38ca8 100644 --- a/core/crates/drivers/mssql/src/connection.rs +++ b/core/crates/drivers/mssql/src/connection.rs @@ -9,7 +9,7 @@ use tablepro_core::ddl::{DatabaseChoices, InboundForeignKey, OutboundForeignKey, use tablepro_core::dialect::SqlDialect; use tablepro_core::dump::{DumpCatalog, DumpScope}; use tablepro_core::meta::{ - IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableRef, + IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, }; use tablepro_core::statement::Statement; use tablepro_core::{ @@ -422,6 +422,15 @@ impl Connection for MssqlConnection { .await } + async fn table_info(&self, table: &TableRef, options: CallOptions) -> Result { + let relation = self.relation(table); + self.read( + options, + read_op(move |client, watch| Box::pin(async move { metadata::table_info(client, watch, &relation).await })), + ) + .await + } + async fn query_bound( &self, statement: &Statement, diff --git a/core/crates/drivers/mssql/src/errors.rs b/core/crates/drivers/mssql/src/errors.rs index cbf62cfe2c..de9f365d83 100644 --- a/core/crates/drivers/mssql/src/errors.rs +++ b/core/crates/drivers/mssql/src/errors.rs @@ -68,6 +68,15 @@ pub(crate) fn tds_number(error: &DriverError) -> Option { } } +/// Permission refused on an object, in a database, or for an action +/// such as reading a DMV without VIEW DATABASE STATE. +const PERMISSION_DENIED: [u32; 4] = [229, 262, 297, 300]; + +/// Whether the server refused for want of a permission. +pub(crate) fn permission_denied(error: &DriverError) -> bool { + tds_number(error).is_some_and(|number| PERMISSION_DENIED.contains(&number)) +} + /// Whether the connection the error came from is gone or out of step /// with the server, so it must not run anything else. pub(crate) fn ends_connection(error: &DriverError) -> bool { @@ -166,6 +175,17 @@ mod tests { } } + #[test] + fn a_refused_permission_is_told_apart() { + let refused = server_error(&message( + 297, + "The user does not have permission to perform this action.", + )); + let missing = server_error(&message(208, "Invalid object name 't'.")); + assert!(permission_denied(&refused)); + assert!(!permission_denied(&missing)); + } + #[test] fn diagnostics_carry_the_tds_triple_line_and_procedure() { let mut sent = message(547, "The INSERT statement conflicted with the FOREIGN KEY constraint"); diff --git a/core/crates/drivers/mssql/src/metadata.rs b/core/crates/drivers/mssql/src/metadata.rs index e986f6e06a..19b26d6387 100644 --- a/core/crates/drivers/mssql/src/metadata.rs +++ b/core/crates/drivers/mssql/src/metadata.rs @@ -7,7 +7,7 @@ use tablepro_core::ddl::{DatabaseChoices, DatabaseDefaults, InboundForeignKey, R use tablepro_core::dialect::SqlDialect; use tablepro_core::meta::{ EditorRefusal, IndexInfo, IndexKey, ObjectDefinition, ObjectInfo, ObjectKind, ObjectScope, RowCountEstimate, - RowIdentity, SchemaInfo, TableEstimate, TableRef, + RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, }; use tablepro_core::sql_syntax::{SqlGrammar, holds_client_directive, qualify_created_name}; use tablepro_core::{ColumnInfo, DriverError, ForeignKeyInfo, UnsupportedFeature, Value}; @@ -18,6 +18,7 @@ use crate::catalog::{ inbound_foreign_keys, primary_key, }; use crate::dialect::MssqlDialect; +use crate::errors::permission_denied; use crate::in_band_request::{Interrupt, Watch}; use crate::index_statements::fetch_index_statements; use crate::physical::MssqlClient; @@ -145,6 +146,20 @@ const IDENTITY_SQL: &str = "SELECT i.index_id, c.name \ const ESTIMATE_SQL: &str = "SELECT CAST(SUM(p.rows) AS bigint) \ FROM sys.partitions p WHERE p.object_id = OBJECT_ID(@P1) AND p.index_id IN (0, 1)"; +const TABLE_COMMENT_SQL: &str = "SELECT CAST(p.value AS nvarchar(max)) FROM sys.extended_properties p \ + WHERE p.class = 1 AND p.major_id = OBJECT_ID(@P1) AND p.minor_id = 0 AND p.name = N'MS_Description'"; + +/// The pages `sp_spaceused` counts: every reserved one, the data pages +/// of the heap or clustered index with their LOB and row-overflow pages, +/// and every used one. +const TABLE_PAGES_SQL: &str = "SELECT SUM(s.reserved_page_count), \ + SUM(CASE WHEN s.index_id < 2 \ + THEN s.in_row_data_page_count + s.lob_used_page_count + s.row_overflow_used_page_count ELSE 0 END), \ + SUM(s.used_page_count) \ + FROM sys.dm_db_partition_stats s WHERE s.object_id = OBJECT_ID(@P1)"; + +const PAGE_BYTES: u64 = 8192; + pub(crate) async fn list_schemas(client: &mut MssqlClient, watch: &Watch) -> Result, Interrupt> { let rows = catalogue_rows(client, watch, SCHEMAS_SQL, &[]).await?; rows.iter() @@ -544,6 +559,50 @@ pub(crate) async fn table_estimate( }) } +/// The comment and the sizes, the sizes in a statement of their own: +/// their DMV needs VIEW DATABASE STATE, which a login that reads the +/// table may lack. +pub(crate) async fn table_info( + client: &mut MssqlClient, + watch: &Watch, + relation: &str, +) -> Result { + let comment = catalogue_rows(client, watch, TABLE_COMMENT_SQL, &[text_param(relation)]) + .await? + .first() + .and_then(|row| Cells::new(row, "sys.extended_properties").text(0)); + let pages = match catalogue_rows(client, watch, TABLE_PAGES_SQL, &[text_param(relation)]).await { + Ok(rows) => rows, + Err(Interrupt::Failed(error)) if permission_denied(&error) => Vec::new(), + Err(interrupt) => return Err(interrupt), + }; + let info = match pages.first() { + Some(row) => { + let cells = Cells::new(row, "sys.dm_db_partition_stats"); + sizes(cells.int(0), cells.int(1), cells.int(2)) + } + None => TableInfo::default(), + }; + Ok(info.with_comment(comment)) +} + +// sp_spaceused's arithmetic: the indexes are the used pages that are not +// data. An object with no partitions, such as a view, sums to NULL. +fn sizes(reserved: Option, data: Option, used: Option) -> TableInfo { + let bytes = |pages: Option| { + pages + .and_then(|pages| u64::try_from(pages).ok()) + .and_then(|pages| pages.checked_mul(PAGE_BYTES)) + }; + let data = bytes(data); + TableInfo { + comment: None, + data_bytes: data, + index_bytes: bytes(used).zip(data).map(|(used, data)| used.saturating_sub(data)), + total_bytes: bytes(reserved), + } +} + /// The table as a structure change plans against it, read in one call /// on one connection. pub(crate) async fn fetch_table_definition( @@ -759,4 +818,19 @@ mod tests { assert_eq!(keys[0].on_delete, Some(ReferentialAction::Cascade)); assert!(keys[0].references(Some("dbo"), "orders")); } + + #[test] + fn sizes_follow_sp_spaceused() { + assert_eq!( + sizes(Some(10), Some(4), Some(7)), + TableInfo { + comment: None, + data_bytes: Some(4 * 8192), + index_bytes: Some(3 * 8192), + total_bytes: Some(10 * 8192), + } + ); + assert_eq!(sizes(Some(2), Some(3), Some(1)).index_bytes, Some(0)); + assert_eq!(sizes(None, None, None), TableInfo::default()); + } } diff --git a/core/crates/drivers/mssql/tests/integration/metadata.rs b/core/crates/drivers/mssql/tests/integration/metadata.rs index 0eff871419..1b9529f0b3 100644 --- a/core/crates/drivers/mssql/tests/integration/metadata.rs +++ b/core/crates/drivers/mssql/tests/integration/metadata.rs @@ -1,14 +1,19 @@ //! The catalogue as browse and the structure tab read it. +use secrecy::SecretString; use tablepro_core::column::{ColumnDefault, ColumnKind, ReadForm, TextLength}; use tablepro_core::ddl::{DdlDialect, IdentityStyle, TableDraft, diff_table}; use tablepro_core::dialect::{BindTarget, LikeForm}; -use tablepro_core::meta::{EditorRefusal, ObjectInfo, ObjectKind, ObjectScope, RowIdentity, TableRef}; +use tablepro_core::meta::{EditorRefusal, ObjectInfo, ObjectKind, ObjectScope, RowIdentity, TableInfo, TableRef}; use tablepro_core::statement::StatementBuilder; -use tablepro_core::{DriverError, RowLimit, UnsupportedFeature, Value}; +use tablepro_core::{Credentials, DriverError, RowLimit, UnsupportedFeature, Value, WorkspaceSettings}; use tablepro_driver_mssql::MssqlDialect; +use tablepro_test_support::conformance::test_liveness; -use crate::support::{CALL, columns, connect, dbo, rows, run_ok, scalar, session, setup, start, statement, within}; +use crate::support::{ + CALL, columns, connect, dbo, dial, direct, options_over, require, rows, run_ok, scalar, session, setup, start, + statement, within, +}; #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker"] @@ -138,6 +143,84 @@ async fn the_servers_databases_are_listed_for_a_query_tab() { assert!(!own.is_system || own.name == "master", "{own:?}"); } +/// A login that may read the table but not the DMV its sizes come from +/// still gets its comment. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn table_info_reports_comment_and_sizes() { + let fixture = start().await; + let connection = connect(&fixture, false).await; + setup( + connection.as_ref(), + &[ + "CREATE TABLE dbo.sized (id int PRIMARY KEY, label nvarchar(100))", + "CREATE INDEX sized_label ON dbo.sized (label)", + "INSERT INTO dbo.sized VALUES (1, N'a'), (2, N'b'), (3, N'c')", + "EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'the sized one', \ + @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'sized'", + "CREATE VIEW dbo.sized_view AS SELECT id FROM dbo.sized", + ], + ) + .await; + let admin = session(connection.as_ref()).await; + for sql in [ + "CREATE LOGIN sizer WITH PASSWORD = N'Sizer-Pass-2026!', CHECK_POLICY = OFF", + "CREATE USER sizer FOR LOGIN sizer", + "GRANT SELECT ON dbo.sized TO sizer", + ] { + run_ok(admin.as_ref(), sql).await; + } + let liveness = test_liveness(); + let sizer = dial(options_over( + direct(fixture.host(), fixture.port(), &liveness), + Credentials::Password { + username: "sizer".to_owned(), + password: SecretString::from("Sizer-Pass-2026!".to_owned()), + }, + require(), + WorkspaceSettings::workspace(false), + liveness, + )) + .await + .expect("the sizer's connection"); + + let sized = connection + .table_info(&dbo("sized"), within(CALL)) + .await + .expect("the table's info"); + let view = connection + .table_info(&dbo("sized_view"), within(CALL)) + .await + .expect("the view's info"); + let missing = connection + .table_info(&dbo("missing"), within(CALL)) + .await + .expect("no info"); + let refused = sizer + .table_info(&dbo("sized"), within(CALL)) + .await + .expect("the comment without the sizes"); + + assert_eq!(sized.comment.as_deref(), Some("the sized one")); + let (data, index, total) = ( + sized.data_bytes.expect("data"), + sized.index_bytes.expect("indexes"), + sized.total_bytes.expect("total"), + ); + assert!(data > 0 && index > 0, "{sized:?}"); + assert!(total >= data + index, "{sized:?}"); + assert_eq!(view, TableInfo::default()); + assert_eq!(missing, TableInfo::default()); + assert_eq!( + refused, + TableInfo { + comment: Some("the sized one".to_owned()), + ..TableInfo::default() + } + ); + sizer.close().await.expect("a close"); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker"] async fn fetch_table_definition_reports_pk_name_collation_comment() { diff --git a/core/crates/drivers/mysql/src/connection.rs b/core/crates/drivers/mysql/src/connection.rs index 912a929359..4d58b7db58 100644 --- a/core/crates/drivers/mysql/src/connection.rs +++ b/core/crates/drivers/mysql/src/connection.rs @@ -9,7 +9,7 @@ use tablepro_core::ddl::{CharacterSet, DatabaseChoices, InboundForeignKey, Outbo use tablepro_core::dialect::SqlDialect; use tablepro_core::dump::{DumpCatalog, DumpScope}; use tablepro_core::meta::{ - IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableRef, + IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, }; use tablepro_core::statement::Statement; use tablepro_core::{ @@ -474,6 +474,17 @@ impl Connection for MysqlConnection { .await } + async fn table_info(&self, table: &TableRef, options: CallOptions) -> Result { + let table = table.clone(); + self.read( + options, + read_op(move |connection, flavour| { + Box::pin(async move { metadata::table_info(connection, flavour, &table).await }) + }), + ) + .await + } + async fn query_bound( &self, statement: &Statement, diff --git a/core/crates/drivers/mysql/src/metadata.rs b/core/crates/drivers/mysql/src/metadata.rs index fc376180ef..d59ac49141 100644 --- a/core/crates/drivers/mysql/src/metadata.rs +++ b/core/crates/drivers/mysql/src/metadata.rs @@ -16,7 +16,7 @@ use tablepro_core::ddl::{ use tablepro_core::dialect::SqlDialect; use tablepro_core::meta::{ EditorRefusal, IndexInfo, IndexKey, ObjectDefinition, ObjectInfo, ObjectKind, ObjectScope, RowCountEstimate, - RowIdentity, SchemaInfo, TableEstimate, TableRef, + RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, }; use tablepro_core::sql_syntax::{mysql_program_script, terminated}; use tablepro_core::{DriverError, ForeignKeyInfo, UnsupportedFeature, Value}; @@ -556,6 +556,42 @@ pub(crate) async fn table_estimate( }) } +const TABLE_INFO_SQL: &str = "\ +SELECT TABLE_TYPE, TABLE_COMMENT, DATA_LENGTH, INDEX_LENGTH FROM information_schema.TABLES +WHERE TABLE_SCHEMA = COALESCE(?, DATABASE()) AND TABLE_NAME = ?"; + +pub(crate) async fn table_info( + connection: &mut MySqlConnection, + flavour: MySqlFlavour, + table: &TableRef, +) -> Result { + let rows = catalog_rows(connection, flavour, TABLE_INFO_SQL, "TABLES", &table_binds(table)).await?; + let Some(row) = rows.first() else { + return Ok(TableInfo::default()); + }; + Ok(table_info_of( + row.text(0)?.as_deref(), + row.text(1)?, + row.number(2)?, + row.number(3)?, + )) +} + +// A view's TABLE_COMMENT is the word VIEW, not a comment, and it stores +// nothing of its own. +fn table_info_of(kind: Option<&str>, comment: Option, data: Option, index: Option) -> TableInfo { + if kind.is_some_and(|kind| kind.ends_with("VIEW")) { + return TableInfo::default(); + } + TableInfo { + data_bytes: data, + index_bytes: index, + total_bytes: data.zip(index).and_then(|(data, index)| data.checked_add(index)), + ..TableInfo::default() + } + .with_comment(comment) +} + pub(crate) async fn fetch_table_definition( connection: &mut MySqlConnection, dialect: &MySqlDialect, @@ -970,4 +1006,28 @@ mod tests { assert_eq!(referential_action("RESTRICT"), Some(ReferentialAction::Restrict)); assert_eq!(referential_action("SOMETHING NEW"), None); } + + #[test] + fn a_table_sums_data_and_indexes_and_a_view_reports_nothing() { + assert_eq!( + table_info_of(Some("BASE TABLE"), Some("orders".to_owned()), Some(16384), Some(32768)), + TableInfo { + comment: Some("orders".to_owned()), + data_bytes: Some(16384), + index_bytes: Some(32768), + total_bytes: Some(49152), + } + ); + assert_eq!( + table_info_of(Some("BASE TABLE"), Some(String::new()), Some(16384), None), + TableInfo { + data_bytes: Some(16384), + ..TableInfo::default() + } + ); + assert_eq!( + table_info_of(Some("VIEW"), Some("VIEW".to_owned()), None, None), + TableInfo::default() + ); + } } diff --git a/core/crates/drivers/mysql/tests/integration/metadata.rs b/core/crates/drivers/mysql/tests/integration/metadata.rs index 6684bef64b..c22a97fe99 100644 --- a/core/crates/drivers/mysql/tests/integration/metadata.rs +++ b/core/crates/drivers/mysql/tests/integration/metadata.rs @@ -3,7 +3,7 @@ use tablepro_core::column::{BoolStorage, ColumnKind, IntegerKind, IntegerWidth}; use tablepro_core::ddl::{ColumnVisibility, GeneratedStorage, IdentityStyle, TableOptions}; use tablepro_core::dialect::{BindTarget, LikeForm, SqlDialect}; -use tablepro_core::meta::{EditorRefusal, ObjectInfo, ObjectKind, ObjectScope}; +use tablepro_core::meta::{EditorRefusal, ObjectInfo, ObjectKind, ObjectScope, TableInfo}; use tablepro_core::statement::StatementBuilder; use tablepro_core::{Connection, DatabaseDriver, DriverError, RowLimit, TlsConfig, Value, WorkspaceSettings}; use tablepro_driver_mysql::MysqlDriver; @@ -406,6 +406,50 @@ async fn objects_and_estimates_are_listed() { } } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn table_info_reports_comment_and_sizes() { + for flavour in FLAVOURS { + let fixture = start(flavour).await; + let connection = connect(&fixture, false).await; + setup( + connection.as_ref(), + &[ + "CREATE TABLE sized (id int PRIMARY KEY, label varchar(100), KEY sized_label (label)) \ + COMMENT 'the sized one'", + "INSERT INTO sized VALUES (1, 'a'), (2, 'b'), (3, 'c')", + "CREATE VIEW sized_view AS SELECT id FROM sized", + ], + ) + .await; + + let sized = connection + .table_info(&table("sized"), within(CALL)) + .await + .expect("the table's info"); + let view = connection + .table_info(&table("sized_view"), within(CALL)) + .await + .expect("the view's info"); + let missing = connection + .table_info(&table("missing"), within(CALL)) + .await + .expect("no info"); + + assert_eq!(sized.comment.as_deref(), Some("the sized one"), "{flavour:?}"); + let (data, index, total) = ( + sized.data_bytes.expect("data"), + sized.index_bytes.expect("indexes"), + sized.total_bytes.expect("total"), + ); + assert!(data > 0 && index > 0, "{flavour:?}: {sized:?}"); + assert_eq!(total, data + index, "{flavour:?}"); + assert_eq!(view, TableInfo::default(), "{flavour:?}"); + assert_eq!(missing, TableInfo::default(), "{flavour:?}"); + connection.close().await.expect("a close"); + } +} + async fn names_matching( connection: &dyn Connection, dialect: &dyn SqlDialect, diff --git a/core/crates/drivers/postgres/src/connection.rs b/core/crates/drivers/postgres/src/connection.rs index 10e8a4144d..716c551fcd 100644 --- a/core/crates/drivers/postgres/src/connection.rs +++ b/core/crates/drivers/postgres/src/connection.rs @@ -8,7 +8,7 @@ use tablepro_core::ddl::{DatabaseChoices, InboundForeignKey, OutboundForeignKey, use tablepro_core::dialect::SqlDialect; use tablepro_core::dump::{DumpCatalog, DumpScope}; use tablepro_core::meta::{ - IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableRef, + IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, }; use tablepro_core::statement::Statement; use tablepro_core::{ @@ -481,6 +481,15 @@ impl Connection for PgConnection { .await } + async fn table_info(&self, table: &TableRef, options: CallOptions) -> Result { + let relation = self.relation(table); + let partition_tree = self.server.at_least(12, 0, 0); + self.catalog_relation_read(options, relation, move |client, relation| { + Box::pin(async move { metadata::table_info(client, &relation, partition_tree).await }) + }) + .await + } + async fn query_bound( &self, statement: &Statement, diff --git a/core/crates/drivers/postgres/src/metadata/mod.rs b/core/crates/drivers/postgres/src/metadata/mod.rs index 8695785407..2e2c43f475 100644 --- a/core/crates/drivers/postgres/src/metadata/mod.rs +++ b/core/crates/drivers/postgres/src/metadata/mod.rs @@ -9,6 +9,7 @@ mod foreign_keys; mod identity; mod indexes; mod objects; +mod table_info; pub(crate) use database_choices::database_choices; pub(crate) use definitions::fetch_object_definition; @@ -17,6 +18,7 @@ pub(crate) use foreign_keys::{fetch_foreign_keys, fetch_outbound_foreign_keys}; pub(crate) use identity::row_identity; pub(crate) use indexes::{fetch_indexes, in_tablespace, without_only}; pub(crate) use objects::{list_databases, list_objects, list_schemas}; +pub(crate) use table_info::table_info; use futures_util::TryStreamExt; use postgres_types::{ToSql, Type}; @@ -194,6 +196,8 @@ mod tests { ("indexes", indexes::INDEXES_SQL.to_owned()), ("index statements", indexes::INDEX_STATEMENTS_SQL.to_owned()), ("estimate", estimate::ESTIMATE_SQL.to_owned()), + ("table info", table_info::TABLE_INFO_SQL.to_owned()), + ("table info before 12", table_info::TABLE_INFO_SQL_BEFORE_12.to_owned()), ("foreign keys", foreign_keys::foreign_keys_sql()), ("inbound foreign keys", inbound_foreign_keys_sql()), ("primary key", PRIMARY_KEY_NAME_SQL.to_owned()), diff --git a/core/crates/drivers/postgres/src/metadata/table_info.rs b/core/crates/drivers/postgres/src/metadata/table_info.rs new file mode 100644 index 0000000000..d751223df2 --- /dev/null +++ b/core/crates/drivers/postgres/src/metadata/table_info.rs @@ -0,0 +1,113 @@ +use tablepro_core::DriverError; +use tablepro_core::meta::TableInfo; +use tokio_postgres::Client; + +use super::{decode_error, text_rows}; + +/// The relation's kind, its comment, and the sizes of what it stores: +/// its own for a table or materialized view, its leaf partitions' for a +/// partitioned table, which stores nothing itself. +pub(super) const TABLE_INFO_SQL: &str = "\ +SELECT c.relkind::text, + pg_catalog.obj_description(c.oid, 'pg_class'), + sizes.data_bytes, + sizes.index_bytes, + sizes.total_bytes +FROM pg_catalog.pg_class c +CROSS JOIN LATERAL ( + SELECT COALESCE(pg_catalog.sum(pg_catalog.pg_table_size(part.relid)), 0)::int8, + COALESCE(pg_catalog.sum(pg_catalog.pg_indexes_size(part.relid)), 0)::int8, + COALESCE(pg_catalog.sum(pg_catalog.pg_total_relation_size(part.relid)), 0)::int8 + FROM (SELECT c.oid WHERE c.relkind IN ('r', 'm') + UNION ALL + SELECT leaf.oid + FROM pg_catalog.pg_partition_tree(c.oid) AS tree + JOIN pg_catalog.pg_class leaf ON leaf.oid = tree.relid::pg_catalog.oid + WHERE c.relkind = 'p' AND leaf.relkind = 'r') AS part(relid) +) AS sizes(data_bytes, index_bytes, total_bytes) +WHERE c.oid = $1::text::oid"; + +/// `pg_partition_tree` came in PostgreSQL 12, so before it a partitioned +/// table reports its own sizes. +pub(super) const TABLE_INFO_SQL_BEFORE_12: &str = "\ +SELECT c.relkind::text, + pg_catalog.obj_description(c.oid, 'pg_class'), + sizes.data_bytes, + sizes.index_bytes, + sizes.total_bytes +FROM pg_catalog.pg_class c +CROSS JOIN LATERAL ( + SELECT COALESCE(pg_catalog.sum(pg_catalog.pg_table_size(part.relid)), 0)::int8, + COALESCE(pg_catalog.sum(pg_catalog.pg_indexes_size(part.relid)), 0)::int8, + COALESCE(pg_catalog.sum(pg_catalog.pg_total_relation_size(part.relid)), 0)::int8 + FROM (SELECT c.oid WHERE c.relkind IN ('r', 'm', 'p')) AS part(relid) +) AS sizes(data_bytes, index_bytes, total_bytes) +WHERE c.oid = $1::text::oid"; + +/// `partition_tree` says the server has `pg_partition_tree`. +pub(crate) async fn table_info( + client: &Client, + relation: &str, + partition_tree: bool, +) -> Result { + let sql = match partition_tree { + true => TABLE_INFO_SQL, + false => TABLE_INFO_SQL_BEFORE_12, + }; + let field = decode_error("pg_class"); + let rows = text_rows(client, sql, &[relation]).await?; + let Some(row) = rows.first() else { + return Ok(TableInfo::default()); + }; + let relkind: String = row.try_get(0).map_err(&field)?; + let comment: Option = row.try_get(1).map_err(&field)?; + let data: i64 = row.try_get(2).map_err(&field)?; + let index: i64 = row.try_get(3).map_err(&field)?; + let total: i64 = row.try_get(4).map_err(&field)?; + Ok(info(&relkind, comment, [data, index, total])) +} + +// A view or a foreign table stores nothing here, so its sizes are no +// figure at all rather than zero. +fn info(relkind: &str, comment: Option, [data, index, total]: [i64; 3]) -> TableInfo { + let info = TableInfo::default().with_comment(comment); + if !matches!(relkind, "r" | "m" | "p") { + return info; + } + TableInfo { + data_bytes: u64::try_from(data).ok(), + index_bytes: u64::try_from(index).ok(), + total_bytes: u64::try_from(total).ok(), + ..info + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_view_keeps_its_comment_and_has_no_sizes() { + let view = info("v", Some("seen".to_owned()), [0, 0, 0]); + assert_eq!(view.comment.as_deref(), Some("seen")); + assert_eq!( + (view.data_bytes, view.index_bytes, view.total_bytes), + (None, None, None) + ); + assert_eq!(info("f", None, [0, 0, 0]), TableInfo::default()); + } + + #[test] + fn a_table_reports_its_sizes_and_a_blank_comment_is_none() { + let table = info("p", Some(" ".to_owned()), [8192, 16384, 24576]); + assert_eq!( + table, + TableInfo { + comment: None, + data_bytes: Some(8192), + index_bytes: Some(16384), + total_bytes: Some(24576), + } + ); + } +} diff --git a/core/crates/drivers/postgres/tests/integration/metadata.rs b/core/crates/drivers/postgres/tests/integration/metadata.rs index 9fef08b532..edd8b66854 100644 --- a/core/crates/drivers/postgres/tests/integration/metadata.rs +++ b/core/crates/drivers/postgres/tests/integration/metadata.rs @@ -536,6 +536,64 @@ async fn estimate_heap_blocks_matches_relation_size() { ); } +/// A table's comment and sizes, a partitioned table's summed over its +/// partitions, and a view's comment without sizes. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn table_info_reports_comment_and_sizes() { + let fixture = start().await; + let connection = connect(&fixture, false).await; + setup( + connection.as_ref(), + &[ + "CREATE TABLE sized (id int PRIMARY KEY, label text)", + "CREATE INDEX sized_label ON sized (label)", + "INSERT INTO sized SELECT n, repeat('x', 100) FROM generate_series(1, 1000) AS n", + "COMMENT ON TABLE sized IS 'the sized one'", + "CREATE TABLE parted (id int, label text) PARTITION BY RANGE (id)", + "CREATE TABLE parted_low PARTITION OF parted FOR VALUES FROM (0) TO (500)", + "CREATE TABLE parted_high PARTITION OF parted FOR VALUES FROM (500) TO (1000)", + "CREATE INDEX parted_label ON parted (label)", + "INSERT INTO parted SELECT n, 'x' FROM generate_series(0, 999) AS n", + "CREATE VIEW seen AS SELECT id FROM sized", + "COMMENT ON VIEW seen IS 'a view'", + ], + ) + .await; + + let sized = connection + .table_info(&table("sized"), within(CALL)) + .await + .expect("the table's info"); + let parted = connection + .table_info(&table("parted"), within(CALL)) + .await + .expect("the partitioned table's info"); + let seen = connection + .table_info(&table("seen"), within(CALL)) + .await + .expect("the view's info"); + let missing = connection.table_info(&table("missing"), within(CALL)).await; + + assert_eq!(sized.comment.as_deref(), Some("the sized one")); + assert_eq!(parted.comment, None); + for info in [&sized, &parted] { + let (data, index, total) = ( + info.data_bytes.expect("data"), + info.index_bytes.expect("indexes"), + info.total_bytes.expect("total"), + ); + assert!(data > 0 && index > 0, "{info:?}"); + assert!(total >= data + index, "{info:?}"); + } + assert_eq!(seen.comment.as_deref(), Some("a view")); + assert_eq!( + (seen.data_bytes, seen.index_bytes, seen.total_bytes), + (None, None, None) + ); + assert!(missing.is_err(), "{missing:?}"); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker"] async fn fetch_table_definition_reports_pk_name_collation_comment_and_inbound_keys() { @@ -1083,6 +1141,7 @@ async fn lookalikes_in_public_never_run_in_a_catalog_read() { .unwrap(); connection.row_identity(&table(name), within(CALL)).await.unwrap(); connection.table_estimate(&table(name), within(CALL)).await.unwrap(); + connection.table_info(&table(name), within(CALL)).await.unwrap(); } let unqualified = TableRef::new(None, "orders"); let columns = connection.fetch_columns(&unqualified, within(CALL)).await.unwrap(); @@ -1153,6 +1212,7 @@ async fn read_table_catalog(connection: &dyn Connection, target: &TableRef) { .table_estimate(target, within(CALL)) .await .expect("row estimate"); + connection.table_info(target, within(CALL)).await.expect("table info"); } /// The login's own search_path puts exact copies of built-ins ahead of diff --git a/core/crates/drivers/sqlite/src/catalog/mod.rs b/core/crates/drivers/sqlite/src/catalog/mod.rs index 7bca991c64..a3bd14bbc3 100644 --- a/core/crates/drivers/sqlite/src/catalog/mod.rs +++ b/core/crates/drivers/sqlite/src/catalog/mod.rs @@ -14,6 +14,7 @@ use tablepro_core::column::{ BoolStorage, CatalogType, ColumnDefault, ColumnKind, ColumnType, FloatWidth, IntegerKind, IntegerWidth, ReadForm, SqlExpression, SqlTypeExpr, TemporalZone, TextLength, TimeRange, classify_type_name, }; +use tablepro_core::meta::TableInfo; use tablepro_core::sql_syntax::{LiteralToken, MAX_EXPRESSION_LEN, SqlGrammar, parse_expression}; use tablepro_core::value::{SqlDecimal, Value}; @@ -147,6 +148,37 @@ pub(crate) fn has_table_sql(schema_sql: &str) -> String { format!("SELECT count(*) FROM {schema_sql}.sqlite_schema WHERE type = 'table' AND name = ?1") } +/// The bytes `dbstat` counts for the table's own b-tree and for its +/// indexes together, automatic ones included, by `sqlite_schema` type. +pub(crate) fn table_pages_sql(schema_sql: &str) -> String { + format!( + "SELECT s.type, sum(d.pgsize) FROM {schema_sql}.sqlite_schema s \ + JOIN dbstat d ON d.schema = ?1 AND d.name = s.name \ + WHERE s.tbl_name = ?2 AND s.type IN ('table', 'index') GROUP BY s.type" + ) +} + +/// The sizes from what `table_pages_sql` read. Without a b-tree of its +/// own the object is a view or a virtual table, which has none. +pub(crate) fn table_sizes(bytes: &[(String, i64)]) -> TableInfo { + let of = |kind: &str| { + bytes + .iter() + .find(|(found, _)| found == kind) + .and_then(|(_, bytes)| u64::try_from(*bytes).ok()) + }; + let Some(data) = of("table") else { + return TableInfo::default(); + }; + let index = of("index").unwrap_or(0); + TableInfo { + comment: None, + data_bytes: Some(data), + index_bytes: Some(index), + total_bytes: data.checked_add(index), + } +} + /// The first number of a `sqlite_stat1` row is the row count ANALYZE /// saw. A table without indexes has one row whose `idx` is NULL. pub(crate) fn statistics_sql(schema_sql: &str) -> String { @@ -382,6 +414,22 @@ fn number_value(number: &SqlDecimal) -> Option { mod tests { use super::*; + #[test] + fn a_table_adds_its_indexes_and_a_view_has_no_sizes() { + let bytes = [("index".to_owned(), 8192), ("table".to_owned(), 4096)]; + assert_eq!( + table_sizes(&bytes), + TableInfo { + comment: None, + data_bytes: Some(4096), + index_bytes: Some(8192), + total_bytes: Some(12288), + } + ); + assert_eq!(table_sizes(&[("table".to_owned(), 4096)]).index_bytes, Some(0)); + assert_eq!(table_sizes(&[]), TableInfo::default()); + } + #[test] fn sqlite_affinity_rules_and_strict() { let cases = [ diff --git a/core/crates/drivers/sqlite/src/connection.rs b/core/crates/drivers/sqlite/src/connection.rs index 2ae404648a..eb36192cd5 100644 --- a/core/crates/drivers/sqlite/src/connection.rs +++ b/core/crates/drivers/sqlite/src/connection.rs @@ -10,7 +10,7 @@ use tablepro_core::ddl::{InboundForeignKey, OutboundForeignKey, TableDefinition} use tablepro_core::dialect::SqlDialect; use tablepro_core::dump::{DumpCatalog, DumpScope}; use tablepro_core::meta::{ - IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableRef, + IndexInfo, ObjectDefinition, ObjectInfo, ObjectScope, RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, }; use tablepro_core::statement::Statement; use tablepro_core::{ @@ -484,6 +484,14 @@ impl Connection for SqliteConnection { .await } + async fn table_info(&self, table: &TableRef, options: CallOptions) -> Result { + let table = table.clone(); + self.read_catalog(options, move |connection| { + Box::pin(async move { metadata::table_info(connection, &table).await }) + }) + .await + } + async fn query_bound( &self, statement: &Statement, diff --git a/core/crates/drivers/sqlite/src/metadata.rs b/core/crates/drivers/sqlite/src/metadata.rs index f775f754b4..f5fd62201c 100644 --- a/core/crates/drivers/sqlite/src/metadata.rs +++ b/core/crates/drivers/sqlite/src/metadata.rs @@ -15,7 +15,7 @@ use tablepro_core::ddl::{ }; use tablepro_core::meta::{ EngineRowId, IndexInfo, IndexKey, ObjectDefinition, ObjectInfo, ObjectKind, ObjectScope, RowCountEstimate, - RowIdentity, SchemaInfo, TableEstimate, TableRef, + RowIdentity, SchemaInfo, TableEstimate, TableInfo, TableRef, }; use tablepro_core::sql_syntax::{SqlGrammar, qualify_created_name, terminated}; use tablepro_core::{ @@ -750,6 +750,30 @@ pub(crate) async fn table_estimate( }) } +/// The bytes `dbstat` counts for the table and its indexes. SQLite keeps +/// no comment. +pub(crate) async fn table_info(connection: &mut SqlxConnection, table: &TableRef) -> Result { + let schema = schema_of(table); + let read: Result, sqlx::Error> = + sqlx::query_as(AssertSqlSafe(catalog::table_pages_sql("e(schema)))) + .bind(schema) + .bind(table.name.as_str()) + .fetch_all(&mut *connection) + .await; + match read { + Ok(bytes) => Ok(catalog::table_sizes(&bytes)), + Err(error) if cannot_count_pages(&error) => Ok(TableInfo::default()), + Err(error) => Err(map_error(error)), + } +} + +// dbstat is missing from a system SQLite built without SQLITE_ENABLE_DBSTAT_VTAB, and a table or view the user named +// dbstat hides it and lacks its columns. An interrupt, a lock or an I/O error still fails the read. +fn cannot_count_pages(error: &sqlx::Error) -> bool { + matches!(error, sqlx::Error::Database(database) + if database.message() == "no such table: dbstat" || database.message().starts_with("no such column: d.")) +} + pub(crate) async fn fetch_table_definition( connection: &mut SqlxConnection, table: &TableRef, diff --git a/core/crates/drivers/sqlite/tests/integration/metadata.rs b/core/crates/drivers/sqlite/tests/integration/metadata.rs index 21265f2d15..3e664da982 100644 --- a/core/crates/drivers/sqlite/tests/integration/metadata.rs +++ b/core/crates/drivers/sqlite/tests/integration/metadata.rs @@ -2,7 +2,8 @@ use tablepro_core::ddl::{IdentityStyle, ReferentialAction}; use tablepro_core::meta::{ - EngineRowId, IndexInfo, IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowCountEstimate, RowIdentity, TableRef, + EngineRowId, IndexInfo, IndexKey, ObjectInfo, ObjectKind, ObjectScope, RowCountEstimate, RowIdentity, TableInfo, + TableRef, }; use tablepro_core::{Connection, DriverError, RowLimit, Value}; @@ -457,6 +458,84 @@ async fn table_estimate_reads_sqlite_stat1() { connection.close().await.unwrap(); } +/// `dbstat` counts the table's own b-tree and every index on it, a +/// UNIQUE constraint's automatic one too, in the schema that holds it. +#[tokio::test] +async fn table_info_counts_the_table_and_its_indexes() { + let file = TestFile::new().await; + let other = file.directory().join("other.sqlite3"); + create_database(&other).await.unwrap(); + let connection = file.open().await; + let names: Vec = (1..=2000).map(|n| n.to_string()).collect(); + for statement in [ + format!("ATTACH DATABASE '{}' AS archive", other.display()), + "CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT)".to_owned(), + "CREATE VIEW people_view AS SELECT id FROM people".to_owned(), + "CREATE TABLE archive.people (id INTEGER PRIMARY KEY, name TEXT UNIQUE)".to_owned(), + "CREATE INDEX archive.by_name ON people (name)".to_owned(), + format!( + "INSERT INTO archive.people (name) SELECT 'person ' || value FROM json_each('[{}]')", + names.join(",") + ), + ] { + query(connection.as_ref(), &statement).await; + } + let compiled = query( + connection.as_ref(), + "SELECT count(*) FROM pragma_compile_options WHERE compile_options = 'ENABLE_DBSTAT_VTAB'", + ) + .await; + + let archived = connection + .table_info(&TableRef::new(Some("archive".to_owned()), "people"), call()) + .await + .unwrap(); + let main = connection.table_info(&table("people"), call()).await.unwrap(); + let view = connection.table_info(&table("people_view"), call()).await.unwrap(); + let missing = connection.table_info(&table("missing"), call()).await.unwrap(); + + assert_eq!(view, TableInfo::default()); + assert_eq!(missing, TableInfo::default()); + if compiled.rows != [vec![Value::Int(1)]] { + // A system SQLite built without dbstat has no sizes to give. + assert_eq!(archived, TableInfo::default()); + assert_eq!(main, TableInfo::default()); + connection.close().await.unwrap(); + return; + } + // The empty table is its root page alone, and its INTEGER PRIMARY KEY + // is the rowid, not an index. + let page = main.data_bytes.expect("the root page"); + assert_eq!((main.index_bytes, main.total_bytes), (Some(0), Some(page))); + let (data, index, total) = ( + archived.data_bytes.expect("data"), + archived.index_bytes.expect("indexes"), + archived.total_bytes.expect("total"), + ); + assert!(data > page && index > 2 * page, "{archived:?}"); + assert_eq!(total, data + index); + connection.close().await.unwrap(); +} + +/// A table the user named dbstat hides SQLite's own, so sizes are not known +/// rather than every table's info failing. +#[tokio::test] +async fn a_table_named_dbstat_leaves_the_sizes_unknown() { + let file = TestFile::new().await; + let connection = file.open().await; + for statement in [ + "CREATE TABLE dbstat (id INTEGER)", + "CREATE TABLE people (id INTEGER PRIMARY KEY)", + ] { + query(connection.as_ref(), statement).await; + } + + let info = connection.table_info(&table("people"), call()).await.unwrap(); + + assert_eq!(info, TableInfo::default()); + connection.close().await.unwrap(); +} + #[tokio::test] async fn a_missing_table_is_an_error_not_an_empty_list() { let file = TestFile::new().await; diff --git a/core/crates/ffi/src/lib.rs b/core/crates/ffi/src/lib.rs index b92f1b7a40..ac16e653c4 100644 --- a/core/crates/ffi/src/lib.rs +++ b/core/crates/ffi/src/lib.rs @@ -78,7 +78,7 @@ pub use import::{ pub use key_read::{KeyKindTag, KeyReader, KeyTtlTag, KeyWindowInfo}; pub use library::{ ConnectionLibrary, ConnectionRecord, ConnectionSecrets, GroupRecord, ImportSummary, LibrarySnapshot, SafeModeTag, - SshAuthTag, SshSettings, TagRecord, TlsModeTag, TlsSettings, + SshAuthTag, SshSettings, TagRecord, TlsModeTag, TlsSettings, check_jump_hosts, }; pub use masking::{ MaskReason, MaskStyleTag, MaskSuggestionInfo, PersonalDataHitInfo, SensitiveKindTag, ShareColumnChoice, @@ -96,7 +96,7 @@ pub use safe_mode_gate::RunGate; pub use saved_queries::{SavedQueriesInfo, SavedQueryDraft, SavedQueryFolderInfo, SavedQueryInfo, SavedQueryStore}; pub use session::{ ColumnSort, DatabaseLayoutTag, DatabaseSession, FailureCauseTag, PageResume, RowEstimate, SessionHealthTag, - TablePageRead, + TableInfoRecord, TablePageRead, }; pub use statement_at::StatementSpan; pub use structure::{StructureColumn, StructureForeignKey, StructureIndex, TableStructure}; diff --git a/core/crates/ffi/src/library.rs b/core/crates/ffi/src/library.rs index 1a17a06296..4068787aba 100644 --- a/core/crates/ffi/src/library.rs +++ b/core/crates/ffi/src/library.rs @@ -635,6 +635,17 @@ impl ConnectionLibrary { .await } + // None takes the colour off; the id is one ConnectionColor names, as a tag's is. + pub async fn set_group_color(&self, id: String, color: Option) -> Result<(), CoreError> { + let id = parse_id(&id)?; + let color = color + .filter(|color| !color.trim().is_empty()) + .map(|color| tag_color(&color)) + .transpose()?; + self.blocking(move |store| Ok(store.set_group_color_blocking(id, color)?)) + .await + } + pub async fn delete_group(&self, id: String) -> Result<(), CoreError> { let id = parse_id(&id)?; self.blocking(move |store| { @@ -885,6 +896,14 @@ pub(crate) fn tls_config(settings: &TlsSettings) -> Result Ok(config) } +// The form's jump hosts as OpenSSH's -J takes them, checked as a connect would read them; the list is saved as typed. +#[uniffi::export] +pub fn check_jump_hosts(list: String) -> Result<(), CoreError> { + tablepro_ssh::SshDestination::parse_jump_list(&list) + .map(|_| ()) + .map_err(|problem| DriverError::from(tablepro_core::TransportError::Ssh(problem.into())).into()) +} + pub(crate) fn saved_ssh(settings: &SshSettings) -> SavedSshConfig { SavedSshConfig { host: settings.host.trim().to_owned(), diff --git a/core/crates/ffi/src/session.rs b/core/crates/ffi/src/session.rs index a79be7b982..4d03ee8317 100644 --- a/core/crates/ffi/src/session.rs +++ b/core/crates/ffi/src/session.rs @@ -128,6 +128,16 @@ pub enum RowEstimate { Unknown, } +// What the engine keeps about a table besides its rows: each figure None where it keeps none or the login cannot read it. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct TableInfoRecord { + pub rows: RowEstimate, + pub comment: Option, + pub data_bytes: Option, + pub index_bytes: Option, + pub total_bytes: Option, +} + impl From for RowEstimate { fn from(estimate: RowCountEstimate) -> Self { match estimate { @@ -367,6 +377,42 @@ impl DatabaseSession { .await } + // The row estimate and the engine's figures for the table, read in one metadata operation. + pub async fn table_info( + &self, + table: TableName, + cancel: Option>, + ) -> Result { + let session = self.session.clone(); + let cancel = token(cancel); + on_runtime(&self.tasks, async move { + let table: TableRef = table.into(); + let (estimate, info) = session + .run_cancellable(OperationKind::Metadata, &cancel, |lease, options| { + let table = table.clone(); + async move { + // An estimate the engine refuses, as MongoDB's count on a view, is unknown; a lost link or + // a cancel still ends the read at table_info, which runs after it on the same lease. + let estimate = lease + .table_estimate(&table, options.clone()) + .await + .map_or(RowCountEstimate::Unknown, |estimate| estimate.rows); + let info = lease.table_info(&table, options).await?; + Ok((estimate, info)) + } + }) + .await?; + Ok(TableInfoRecord { + rows: RowEstimate::from(estimate), + comment: info.comment, + data_bytes: info.data_bytes, + index_bytes: info.index_bytes, + total_bytes: info.total_bytes, + }) + }) + .await + } + // The table as the server defines it now. `kind` decides whether a CREATE script belongs: a view gets none. pub async fn table_structure( &self, diff --git a/core/crates/ffi/tests/browse.rs b/core/crates/ffi/tests/browse.rs index adfa1563b0..325c7696bc 100644 --- a/core/crates/ffi/tests/browse.rs +++ b/core/crates/ffi/tests/browse.rs @@ -291,6 +291,27 @@ async fn a_tables_estimate_is_unknown_until_analyze_writes_its_statistics() { assert_eq!(after, RowEstimate::Approximate { rows: 7 }); } +#[tokio::test] +async fn a_tables_info_has_its_estimate_and_its_size_but_sqlite_keeps_no_comment() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session) = fixture(dir.path(), PEOPLE).await; + execute(&session, "CREATE INDEX people_by_age ON people (age); ANALYZE").await; + + let info = session.table_info(table("people"), None).await.unwrap(); + session.close().await.unwrap(); + + assert_eq!(info.rows, RowEstimate::Approximate { rows: 7 }); + assert_eq!(info.comment, None); + // A system SQLite may be built without dbstat, so only the bundled one is sure to report sizes. + if cfg!(not(target_os = "linux")) { + assert!(info.data_bytes.is_some_and(|bytes| bytes > 0), "{info:?}"); + assert!(info.index_bytes.is_some_and(|bytes| bytes > 0), "{info:?}"); + } + if let (Some(data), Some(index), Some(total)) = (info.data_bytes, info.index_bytes, info.total_bytes) { + assert_eq!(total, data + index); + } +} + #[tokio::test] async fn a_filter_on_a_column_the_table_lacks_is_refused_before_any_read() { let dir = tempfile::tempdir().unwrap(); diff --git a/core/crates/ffi/tests/library.rs b/core/crates/ffi/tests/library.rs index d1c73167ab..f35eeb6237 100644 --- a/core/crates/ffi/tests/library.rs +++ b/core/crates/ffi/tests/library.rs @@ -245,6 +245,42 @@ async fn a_saved_connection_comes_back_in_its_group_and_among_the_favourites() { assert_eq!(snapshot.favorites, [saved.id]); } +#[tokio::test] +async fn a_groups_colour_is_set_taken_off_and_an_unknown_one_refused() { + let dir = tempfile::tempdir().unwrap(); + let core = core(dir.path(), MemorySecrets::default()); + let library = core.library().unwrap(); + let group = library.create_group("Work".to_owned(), None).await.unwrap(); + + library + .set_group_color(group.id.clone(), Some("red".to_owned())) + .await + .unwrap(); + let red = library.snapshot().await.unwrap().groups[0].color.clone(); + let refused = library + .set_group_color(group.id.clone(), Some("mauve".to_owned())) + .await + .unwrap_err(); + library.set_group_color(group.id.clone(), None).await.unwrap(); + let none = library.snapshot().await.unwrap().groups[0].color.clone(); + + assert_eq!(red.as_deref(), Some("red")); + assert_eq!(refused.code(), "library.unknown_color"); + assert_eq!(none, None); +} + +#[test] +fn jump_hosts_are_checked_as_a_connect_reads_them() { + assert!(tablepro_ffi::check_jump_hosts(String::new()).is_ok()); + assert!(tablepro_ffi::check_jump_hosts("ops@jump1:2200, [fd00::1], bastion".to_owned()).is_ok()); + + let empty_hop = tablepro_ffi::check_jump_hosts("jump1,,jump2".to_owned()).unwrap_err(); + let bad_port = tablepro_ffi::check_jump_hosts("jump1:ssh".to_owned()).unwrap_err(); + + assert_eq!(empty_hop.code(), "ssh.invalid_destination"); + assert_eq!(bad_port.code(), "ssh.invalid_destination"); +} + #[tokio::test] async fn saving_again_keeps_the_id_and_takes_the_new_fields() { 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 edd2a16e3c..64b392778b 100644 --- a/core/docs/decisions/0022-browsing-through-the-ffi.md +++ b/core/docs/decisions/0022-browsing-through-the-ffi.md @@ -12,7 +12,7 @@ The Windows app reaches the core only through `crates/ffi`. A table tab read its The FFI exposes what the Linux app calls, and does the work the Linux app does, so an app on it builds no SQL and keeps no store of its own. - `DatabaseSession::read_table_page` reads the catalog's columns and the row identity, builds the page once with the filter so a value the column cannot take or a column the table lacks fails the call before anything is sent, then reads it with `browse::read_page` on a pooled connection (`OperationKind::Page`). The rows arrive as the run events an editor run gives (`ResultSetStarted`, `RowsAppended`, `ResultSetFinished`, `Finished` with an idle transaction), so a host shows a page and a query result the same way. `table_query` is gone, and a table tab no longer holds an editor connection. -- `table_row_count` takes the same filter. `table_row_estimate` gives the whole table's rows from `Connection::table_estimate` as `RowEstimate` (exact, approximate or unknown), on a pooled connection (`OperationKind::Metadata`), so a host decides whether to count as the Linux pager does. `filter_choices` gives each column's operators from `SqlDialect::filter_operators`, the engine's combinators, and whether it takes a raw SQL predicate, which `validate_raw_predicate` reads as exactly one expression. MongoDB's raw filter is a query document, not SQL, so it is not offered there yet, and raw text for an engine that is not SQL is refused (`filter.raw_unsupported`). +- `table_row_count` takes the same filter. `table_row_estimate` gives the whole table's rows from `Connection::table_estimate` as `RowEstimate` (exact, approximate or unknown), on a pooled connection (`OperationKind::Metadata`), so a host decides whether to count as the Linux pager does. `table_info` adds `Connection::table_info` to that estimate in one metadata operation: the table's comment and its data, index and total size, each `None` where the engine keeps none or the login cannot read it; the trait's default keeps none, so an engine without them answers rather than fails. `filter_choices` gives each column's operators from `SqlDialect::filter_operators`, the engine's combinators, and whether it takes a raw SQL predicate, which `validate_raw_predicate` reads as exactly one expression. MongoDB's raw filter is a query document, not SQL, so it is not offered there yet, and raw text for an engine that is not SQL is refused (`filter.raw_unsupported`). - A refused filter carries the codes of each layer: `operation.browse`, `browse.filter`, the rule's `filter.*`, and for a value the column cannot take, the parse's `edit.*` (`BrowseSqlError::codes`, `BuildFilterError::codes`). - `table_structure` reads `fetch_table_definition` and gives its columns, indexes and foreign keys as display text, and the CREATE script from `DdlDialect::create_table_script` only for a table or a partitioned table, by the kind the catalog lists it under where it has one: on some engines the script for a view would be a CREATE TABLE made from its columns. - `QueryEditor::use_database` moves an editor's connection to a database, for a definition whose text names none (MySQL's), as the Linux app opens such a definition. diff --git a/windows/AGENTS.md b/windows/AGENTS.md index d626c7d626..9a20d58802 100644 --- a/windows/AGENTS.md +++ b/windows/AGENTS.md @@ -40,8 +40,9 @@ This is the guide for work under `windows/`. The root `AGENTS.md` holds the rule - A `CancellationToken` fires the core's own cancel token, since UniFFI cannot cancel a call. A cancelled call throws `OperationCanceledException`; a failed one throws `TableProException`, whose `Code` the app words. A statement the server refused is not an exception: the run ends `QueryOutcome.Failed`. - `QueryEditor.Run` returns at once. Read the run's events with `ReadEventsAsync` on the UI thread; rows that arrive while it is busy come as one event. - A grid reads a `ResultSet` through `ResultCursor.Fetch`, one window per visible range. A cursor belongs to one thread, and a window cannot be read after the cursor's next fetch. -- `DataGridView.SetData(new ResultSetData(set, columns))` shows a set. Call the grid's `RowsChanged` after each `RowsAppended`. Sorting is the host's: the grid raises `SortRequested` and shows `Sort`. +- `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`. `EditedGridData.Arrange` sets the columns the grid shows and their order, and `RearrangeColumns` tells the grid; the grid's columns are then display positions, which `DataColumn` maps to the set's. - 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`). +- `GetTableInfoAsync` gives a table's row estimate, comment and data, index and total size, each null where the engine keeps none. `ConnectionLibrary.SetGroupColorAsync` sets or clears a group's colour, and `TableProCore.CheckJumpHosts` checks the SSH jump hosts as a connect reads them (`docs/decisions/0013-row-details-column-layout-and-selection-summary.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`). diff --git a/windows/docs/decisions/0003-data-grid-control.md b/windows/docs/decisions/0003-data-grid-control.md index a5c9653ffd..1452293cd2 100644 --- a/windows/docs/decisions/0003-data-grid-control.md +++ b/windows/docs/decisions/0003-data-grid-control.md @@ -1,6 +1,6 @@ # 0003: TablePro's own data grid control -- **Status**: Accepted +- **Status**: Accepted; column order amended by [0013](0013-row-details-column-layout-and-selection-summary.md) - **Date**: 2026-11-03 ## Context @@ -122,7 +122,8 @@ - The grid raises `SortRequested` and the host sorts: on the server, or with the core's sort of the rows held. - A UI in a right-to-left language would need the grid mirrored; only values read right to left today. - The gate runs WARP on hosted runners; no machine with a GPU is measured yet. -- Not yet: column reorder and find. +- The host owns the column order: a dragged header raises `ColumnMoveRequested`, the host arranges its data and calls `RearrangeColumns`, which keeps each column's width ([0013](0013-row-details-column-layout-and-selection-summary.md)). +- Not yet: find. ## Alternatives considered diff --git a/windows/docs/decisions/0013-row-details-column-layout-and-selection-summary.md b/windows/docs/decisions/0013-row-details-column-layout-and-selection-summary.md new file mode 100644 index 0000000000..8721e0eeb0 --- /dev/null +++ b/windows/docs/decisions/0013-row-details-column-layout-and-selection-summary.md @@ -0,0 +1,40 @@ +# 0013: Row details, table info, column layout, selection summary and connection URLs + +- **Status**: Accepted +- **Date**: 2026-11-18 + +## Context + +A table tab showed its rows and nothing about the row or the table beside them, its columns stayed in the table's order, and no selection said what it held. The Connections page could not start a connection from a URL, the SSH section could not show the jump hosts the core already stores, and a group could not take the colour the library keeps for it. The macOS app does all of it. + +## Decision + +### Table tab + +- Details on the table bar opens a pane beside the rows, as macOS's inspector. Row lists each column of the focused row in the table's own order with its type, in a text box that edits the cell as the grid's editor does (`TableTab.StartEdit`, `CommitEdit`): kept when the box loses the focus or on Ctrl+Enter, put back with Escape, and with the line break style the value had. Its menu sets Empty, NULL or DEFAULT where the column takes it, and copies the value. Read only, or too long to show whole, a field cannot be edited there. +- Table shows the row estimate, the data, index and total size, and the comment, from the core's `table_info` (core `Connection::table_info`): each figure the engine keeps, and Not reported for the rest. It is read once and again after Refresh. +- Each table keeps its column layout per saved connection (or opened file), database, schema and table in `table-columns.json` (`ColumnLayoutMemory`), as filters are kept ([0012](0012-catalog-operations-table-filters-and-export-progress.md)): the order, the hidden columns and how many are frozen, by column name, so a column added since goes last and a dropped one is passed over. A dropped table, schema or database forgets it. +- A header's menu has Hide column, Show all columns, Move column left and right (Alt+Left, Alt+Right), Freeze up to this column (refused when the frozen columns would leave less than a third of the grid, as macOS refuses it; a smaller window or wider columns freeze only those that fit, as macOS shows them), Unfreeze columns and Columns; a header drag moves a column. Columns on the bar lists every column with a check, Show all columns and Reset columns. The last shown column cannot be hidden. +- The table tab's grid data (`EditedGridData`) carries the order, so the grid works in display positions and the tab maps them to the set's columns where it calls the model: edits, the cell menu, the value viewer, Filter, a followed foreign key and the sort. + +### Selection summary + +- Two or more selected cells show Sum, Average and Count under the grid, as a spreadsheet's status bar, with every figure and a Copy for each in its flyout: the count, how many hold numbers, the empty ones (NULL or DEFAULT), the ones a numeric column cannot read, sum, average, minimum and maximum. Integer, decimal and float columns are summed; a column the engine could not type is summed where its text reads as a number; the rest are counted. The sum is exact (`ExactDecimal`), the average to 10 more decimals. Deleted rows are left out and pending edits counted. It is worked out off the UI thread after the selection rests, and a new selection stops the last. + +### Connections + +- New from URL on the Connections page opens a box filled from a connection URL on the clipboard, as macOS's Import from URL; Continue opens the connection form on what the URL names. Ctrl+V while the page shows, outside a text box, a dialog or a menu, does the same with the clipboard's URL. It is the page's keyboard accelerator, not a KeyDown handler, so it also works with the focus on the rail, on the title bar or nowhere after the library rebuilds. Both read it as the engine chooser's From connection URL does (`ConnectionDraft.FromUrl`). +- The SSH section has Jump hosts, one list as OpenSSH's `-J` takes it and the Linux app shows it: `user@host:port` separated by commas, saved as one entry. The core checks it as a connect would (`check_jump_hosts`) before a save or a test. +- A group's menu has Color, None and the nine connection colours; the group's folder takes its colour (`set_group_color`). + +## Rationale + +- The core reads the figures, checks the jump hosts and stores the colour, so the app only words and lays out. +- Stock WinUI controls: SelectorBar, TextBox, DropDownButton, Flyout, MenuFlyout with toggle and radio items, and KeyboardAccelerator. +- One order in the grid data keeps the grid control's API in display space, where its selection rectangle lives. + +## Consequences + +- A query result keeps its own order: a layout belongs to a table. +- The connect timeout field (roadmap W-05) waits: it needs a field on `SavedConnection` and on the connect request, both of which `linux/crates/app` builds with full struct literals, after Linux 0.1.0. +- Engines that keep no size or comment for a table (Redis, and sizes on Cassandra and DuckDB) show Not reported. diff --git a/windows/docs/decisions/README.md b/windows/docs/decisions/README.md index 888b5594c7..1786efcae4 100644 --- a/windows/docs/decisions/README.md +++ b/windows/docs/decisions/README.md @@ -10,7 +10,7 @@ The format, the statuses and how a later ADR changes an earlier one follow [the |---|---|---|---| | [0001](0001-toolchain-baseline.md) | Windows toolchain and build baseline | Accepted; amended by 0005 | Build Tools for Visual Studio 2026 with MSVC for x64 and Arm64, Windows SDK 10.0.26100, .NET 10 and Rust with both MSVC targets; cargo runs from the csproj; Native AOT; Windows 10 1809 as the floor, with runtime checks for Windows 11 behavior. | | [0002](0002-sql-editor-control.md) | TablePro's own SQL editor control | Accepted | A rope with its own undo in a plain .NET model, drawn with Direct2D and DirectWrite into a SwapChainPanel, TSF as Windows Terminal does it, a UIA text provider, and the core's lexer for colors. | -| [0003](0003-data-grid-control.md) | TablePro's own data grid control | Accepted | A WinUI 3 control that edits cells in place: ItemsRepeater rows that draw their visible cells with DirectWrite into composition surfaces through the editor's drawing layer, cells as UIA peers with no element, data read from the core in windows of cells with the host's pending changes laid over them. | +| [0003](0003-data-grid-control.md) | TablePro's own data grid control | Accepted; column order amended by 0013 | A WinUI 3 control that edits cells in place: ItemsRepeater rows that draw their visible cells with DirectWrite into composition surfaces through the editor's drawing layer, cells as UIA peers with no element, data read from the core in windows of cells with the host's pending changes laid over them. | | [0004](0004-app-shell.md) | The app shell | Accepted; packaging amended by 0005, 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). | @@ -20,3 +20,4 @@ The format, the statuses and how a later ADR changes an earlier one follow [the | [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. | +| [0013](0013-row-details-column-layout-and-selection-summary.md) | Row details, table info, column layout, selection summary and connection URLs | Accepted | A Details pane with the focused row's fields edited in place and the table's size and comment; column order, hidden and frozen columns kept per table; Sum, Average and Count of the selected cells; New from URL and Ctrl+V on the Connections page; jump hosts in the SSH section; a group's colour. | diff --git a/windows/src/TablePro.App.Model/AppSettings.cs b/windows/src/TablePro.App.Model/AppSettings.cs index 6eefc2f515..3eced073ff 100644 --- a/windows/src/TablePro.App.Model/AppSettings.cs +++ b/windows/src/TablePro.App.Model/AppSettings.cs @@ -129,6 +129,7 @@ public void Save(AppSettings settings) [JsonSerializable(typeof(AppSettings))] [JsonSerializable(typeof(SessionState))] [JsonSerializable(typeof(TableFilterFile))] +[JsonSerializable(typeof(ColumnLayoutFile))] internal sealed partial class ModelJson : JsonSerializerContext; internal static class JsonFile diff --git a/windows/src/TablePro.App.Model/ByteSize.cs b/windows/src/TablePro.App.Model/ByteSize.cs new file mode 100644 index 0000000000..8b0f98099f --- /dev/null +++ b/windows/src/TablePro.App.Model/ByteSize.cs @@ -0,0 +1,50 @@ +using System.Globalization; + +namespace TablePro.App.Model; + +// A size as File Explorer words one: bytes below 1 KB, then KB, MB, GB and TB of 1,024 each, to one decimal. +public static class ByteSize +{ + private static readonly string[] Units = ["KB", "MB", "GB", "TB", "PB"]; + + public static string Format(long bytes, string bytesPattern, IFormatProvider? culture = null) + { + ArgumentNullException.ThrowIfNull(bytesPattern); + culture ??= CultureInfo.CurrentCulture; + if (bytes < 1024) + { + return string.Format(culture, bytesPattern, bytes); + } + + var value = (double)bytes; + var unit = -1; + do + { + value /= 1024; + unit++; + } + while (value >= 1024 && unit < Units.Length - 1); + + return string.Create(culture, $"{value:0.#} {Units[unit]}"); + } +} + +// A TextBox keeps each line break as CR, so a value edited there gets back the line break style it opened with; one +// that had none takes LF, the break a database's own clients write. +public static class LineBreaks +{ + public static string Of(string? original) => + original is null ? "\n" + : original.Contains("\r\n", StringComparison.Ordinal) ? "\r\n" + : original.Contains('\n', StringComparison.Ordinal) ? "\n" + : original.Contains('\r', StringComparison.Ordinal) ? "\r" + : "\n"; + + public static string Restore(string boxText, string newline) + { + ArgumentNullException.ThrowIfNull(boxText); + ArgumentNullException.ThrowIfNull(newline); + return boxText.Replace("\r\n", "\r", StringComparison.Ordinal).Replace('\n', '\r') + .Replace("\r", newline, StringComparison.Ordinal); + } +} diff --git a/windows/src/TablePro.App.Model/CellValue.cs b/windows/src/TablePro.App.Model/CellValue.cs index 62054a0b00..f4ae37c19c 100644 --- a/windows/src/TablePro.App.Model/CellValue.cs +++ b/windows/src/TablePro.App.Model/CellValue.cs @@ -62,12 +62,14 @@ public sealed record CellValue(string Column, string? Text, bool TextCut, ReadOn return Of(name, reader.ReadWhole(row, column)); } - if (rows.GetCellBytes(row, column, maxBytes) is { } binary) + // The grid's column is a display position where a table tab's layout moved or hid columns; the set's is not. + var source = data is EditedGridData edited ? edited.DataColumn(column) : column; + if (rows.GetCellBytes(row, source, maxBytes) is { } binary) { return new CellValue(name, null, false, binary.Bytes, binary.Length, IsDefault: false); } - return rows.GetCellTextHead(row, column, maxCharacters) is { } head + return rows.GetCellTextHead(row, source, 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/ColumnLayout.cs b/windows/src/TablePro.App.Model/ColumnLayout.cs new file mode 100644 index 0000000000..81d7a00f46 --- /dev/null +++ b/windows/src/TablePro.App.Model/ColumnLayout.cs @@ -0,0 +1,140 @@ +using System.Text.Json.Serialization; + +namespace TablePro.App.Model; + +// The grid's columns for one set of names: the data column at each display position, and how many from the left +// are frozen. +public sealed record ArrangedColumns(IReadOnlyList Order, int Frozen) +{ + public static ArrangedColumns Identity(int count) => new([.. Enumerable.Range(0, Math.Max(0, count))], 0); + + public bool IsIdentity(int count) => + Frozen == 0 && Order.Count == count && Order.Select((column, index) => column == index).All(same => same); +} + +// A table's columns as the user arranged them, by name, so a column added since goes last and a dropped one is passed +// over. Order lists hidden columns too, so one shown again comes back where it was. +public sealed record ColumnLayout +{ + public static ColumnLayout Default { get; } = new(); + + public IReadOnlyList Order { get; init; } = []; + + public IReadOnlyList Hidden { get; init; } = []; + + // Shown columns from the left that stay in view as the rest scroll. + public int Frozen { get; init; } + + [JsonIgnore] + public bool IsDefault => Order.Count == 0 && Hidden.Count == 0 && Frozen == 0; + + // At least one column always shows, so a layout that would hide them all shows the first. + public ArrangedColumns Resolve(IReadOnlyList columns) + { + ArgumentNullException.ThrowIfNull(columns); + var shown = Sequence(columns).Where(index => !IsHidden(columns[index])).ToList(); + if (shown.Count == 0 && columns.Count > 0) + { + shown.Add(0); + } + + return new ArrangedColumns(shown, Math.Clamp(Frozen, 0, shown.Count)); + } + + public bool IsHidden(string column) => Hidden.Contains(column, StringComparer.Ordinal); + + // The last shown column stays. + public ColumnLayout Hide(IReadOnlyList columns, int dataColumn) + { + ArgumentNullException.ThrowIfNull(columns); + var arranged = Resolve(columns); + var position = IndexOf(arranged.Order, dataColumn); + if (position < 0 || arranged.Order.Count <= 1) + { + return this; + } + + return this with + { + Order = Names(columns), + Hidden = [.. Hidden.Append(columns[dataColumn]).Distinct(StringComparer.Ordinal)], + Frozen = position < arranged.Frozen ? arranged.Frozen - 1 : arranged.Frozen, + }; + } + + public ColumnLayout Show(IReadOnlyList columns, int dataColumn) + { + ArgumentNullException.ThrowIfNull(columns); + var name = columns[dataColumn]; + return IsHidden(name) + ? this with { Order = Names(columns), Hidden = [.. Hidden.Where(hidden => !string.Equals(hidden, name, StringComparison.Ordinal))] } + : this; + } + + public ColumnLayout ShowAll() => this with { Hidden = [] }; + + // From and to are display positions among the shown columns; to is where the column ends up. + public ColumnLayout Move(IReadOnlyList columns, int from, int to) + { + ArgumentNullException.ThrowIfNull(columns); + var shown = Resolve(columns).Order; + if (from < 0 || from >= shown.Count || to < 0 || to >= shown.Count || from == to) + { + return this; + } + + var full = Sequence(columns).ToList(); + var moving = shown[from]; + var reordered = shown.ToList(); + reordered.RemoveAt(from); + reordered.Insert(to, moving); + full.Remove(moving); + // Placed before the shown column it lands in front of, or after the last shown one; hidden ones keep their places. + var anchor = to + 1 < reordered.Count ? full.IndexOf(reordered[to + 1]) : full.IndexOf(reordered[to - 1]) + 1; + full.Insert(anchor, moving); + return this with { Order = [.. full.Select(index => columns[index])] }; + } + + public ColumnLayout Freeze(int count) => this with { Frozen = Math.Max(0, count) }; + + private IEnumerable Sequence(IReadOnlyList columns) + { + var byName = new Dictionary(StringComparer.Ordinal); + for (var index = 0; index < columns.Count; index++) + { + byName.TryAdd(columns[index], index); + } + + var placed = new HashSet(); + foreach (var name in Order) + { + if (byName.TryGetValue(name, out var index) && placed.Add(index)) + { + yield return index; + } + } + + for (var index = 0; index < columns.Count; index++) + { + if (placed.Add(index)) + { + yield return index; + } + } + } + + private List Names(IReadOnlyList columns) => [.. Sequence(columns).Select(index => columns[index])]; + + private static int IndexOf(IReadOnlyList order, int column) + { + for (var index = 0; index < order.Count; index++) + { + if (order[index] == column) + { + return index; + } + } + + return -1; + } +} diff --git a/windows/src/TablePro.App.Model/ColumnLayoutMemory.cs b/windows/src/TablePro.App.Model/ColumnLayoutMemory.cs new file mode 100644 index 0000000000..6cf7773a2d --- /dev/null +++ b/windows/src/TablePro.App.Model/ColumnLayoutMemory.cs @@ -0,0 +1,182 @@ +using TablePro.Interop; + +namespace TablePro.App.Model; + +// The column layouts a table tab reads and keeps, scoped to one connection and database. +public interface ITableColumns +{ + ColumnLayout Recall(TableName table); + + void Remember(TableName table, ColumnLayout layout); +} + +public sealed record ColumnLayoutFile +{ + public Dictionary Tables { get; set; } = []; +} + +// Each table's column order, hidden columns and frozen count, as macOS keeps a table's column layout, so a table opened +// again shows its columns as they were left. One file for every connection, written whole on each change. +public sealed class ColumnLayoutMemory +{ + private readonly string _path; + private readonly Lock _lock = new(); + private Dictionary? _tables; + + public ColumnLayoutMemory(string path) + { + ArgumentException.ThrowIfNullOrEmpty(path); + _path = path; + } + + public ITableColumns For(string connection, string? database) => new Scope(this, connection, database); + + public ColumnLayout Recall(string connection, string? database, TableName table) + { + ArgumentNullException.ThrowIfNull(table); + lock (_lock) + { + return Tables().GetValueOrDefault(TableMemoryKey.Of(connection, database, table.Schema, table.Name)) + ?? ColumnLayout.Default; + } + } + + // The default layout forgets the table, as Reset Columns does. + public void Remember(string connection, string? database, TableName table, ColumnLayout layout) + { + ArgumentNullException.ThrowIfNull(table); + ArgumentNullException.ThrowIfNull(layout); + var key = TableMemoryKey.Of(connection, database, table.Schema, table.Name); + lock (_lock) + { + var tables = Tables(); + var kept = tables.GetValueOrDefault(key); + if (layout.IsDefault ? kept is null : kept is not null && Same(kept, layout)) + { + return; + } + + if (layout.IsDefault) + { + tables.Remove(key); + } + else + { + tables[key] = layout; + } + + Write(tables); + } + } + + // A dropped table's layout goes with it, so a table made again under its name starts with its own columns. + public void ForgetTable(string connection, string? database, TableName table) + { + ArgumentNullException.ThrowIfNull(table); + Remember(connection, database, table, ColumnLayout.Default); + } + + public void ForgetSchema(string connection, string? database, string schema) + { + ArgumentNullException.ThrowIfNull(schema); + ForgetPrefix(TableMemoryKey.Part(connection) + "." + TableMemoryKey.Part(database) + "." + + TableMemoryKey.Part(schema) + "."); + } + + public void ForgetDatabase(string connection, string database) + { + ArgumentNullException.ThrowIfNull(database); + ForgetPrefix(TableMemoryKey.Part(connection) + "." + TableMemoryKey.Part(database) + "."); + } + + // The schemas holding a kept layout under the connection and database; null for a table named with no schema. + public IReadOnlyList SchemasWithLayouts(string connection, string? database) + { + var prefix = TableMemoryKey.Part(connection) + "." + TableMemoryKey.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, as a partitioned table's partitions, so the schema's kept layouts are + // matched against what it lists now. + public void ForgetMissing(string connection, string? database, string? schema, IReadOnlySet present) + { + ArgumentNullException.ThrowIfNull(present); + var prefix = TableMemoryKey.Part(connection) + "." + TableMemoryKey.Part(database) + "." + + TableMemoryKey.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 static bool Same(ColumnLayout left, ColumnLayout right) => + left.Frozen == right.Frozen + && left.Order.SequenceEqual(right.Order, StringComparer.Ordinal) + && left.Hidden.SequenceEqual(right.Hidden, StringComparer.Ordinal); + + 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 Dictionary Tables() => + _tables ??= JsonFile.Read(_path, ModelJson.Default.ColumnLayoutFile)?.Tables is { } read + ? new Dictionary(read, StringComparer.Ordinal) + : new Dictionary(StringComparer.Ordinal); + + // A file another program holds keeps the layout in memory until the next change writes it. + private void Write(Dictionary tables) + { + try + { + JsonFile.Write(_path, new ColumnLayoutFile { Tables = tables }, ModelJson.Default.ColumnLayoutFile); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException) + { + } + } + + private sealed class Scope(ColumnLayoutMemory memory, string connection, string? database) : ITableColumns + { + public ColumnLayout Recall(TableName table) => memory.Recall(connection, database, table); + + public void Remember(TableName table, ColumnLayout layout) => memory.Remember(connection, database, table, layout); + } +} diff --git a/windows/src/TablePro.App.Model/ConnectionDraft.cs b/windows/src/TablePro.App.Model/ConnectionDraft.cs index 6610b07a4c..dd88c7906a 100644 --- a/windows/src/TablePro.App.Model/ConnectionDraft.cs +++ b/windows/src/TablePro.App.Model/ConnectionDraft.cs @@ -11,6 +11,7 @@ public enum FormField SshHost, SshPort, SshKey, + JumpHosts, ClientKey, Timeout, } @@ -21,6 +22,9 @@ public enum FieldProblem NotANumber, OutOfRange, NotAbsolute, + + // The core refused the text, as a connect would. + Invalid, } public sealed record FieldError(FormField Field, FieldProblem Problem); @@ -69,6 +73,9 @@ public sealed class ConnectionDraft public string SshPassword { get; set; } = string.Empty; + // As OpenSSH's -J takes them, user@host:port separated by commas, and saved as one entry the way Linux saves it. + public string JumpHosts { get; set; } = string.Empty; + public TlsMode TlsMode { get; set; } = TlsMode.Disable; // A saved connection's mode was picked when it was saved; a new one follows the engine's default until the user picks. @@ -121,6 +128,14 @@ public static ConnectionDraft New(DatabaseEngine engine) }; } + // A new connection with what the URL names, the way the engine chooser's From connection URL fills one. + public static ConnectionDraft FromUrl(ParsedConnectionUrl url, DatabaseEngine engine) + { + var draft = New(engine); + draft.Apply(url, engine); + return draft; + } + public static ConnectionDraft From(SavedConnection connection, DatabaseEngine engine) { ArgumentNullException.ThrowIfNull(connection); @@ -145,6 +160,7 @@ public static ConnectionDraft From(SavedConnection connection, DatabaseEngine en SshUserName = connection.Ssh?.UserName ?? string.Empty, SshAuth = connection.Ssh?.Auth ?? SshAuthMethod.Agent, SshKeyPath = connection.Ssh?.KeyPath ?? string.Empty, + JumpHosts = string.Join(", ", connection.Ssh?.JumpHosts ?? []), TlsMode = connection.Tls.Mode, TlsModeChosen = true, CaFile = connection.Tls.CaFile ?? string.Empty, @@ -302,7 +318,7 @@ public SavedConnection ToConnection() SshHost.Trim(), ParsePort(SshPort), Blank(SshUserName), - _savedSsh?.JumpHosts ?? [], + JumpHostList(), SshAuth, SshAuth == SshAuthMethod.PrivateKey ? Blank(SshKeyPath) : null, SshAuth == SshAuthMethod.PrivateKey && _savedSsh is { KeyHasPassphrase: true } @@ -351,6 +367,18 @@ public ConnectionSecrets Secrets() return new ConnectionSecrets(password, sshPassword); } + // A list left as it was shown keeps its stored entries, so an import's several entries are not counted as an edit. + private IReadOnlyList JumpHostList() + { + if (_savedSsh is { } saved + && string.Equals(string.Join(", ", saved.JumpHosts), JumpHosts, StringComparison.Ordinal)) + { + return saved.JumpHosts; + } + + return Blank(JumpHosts) is { } list ? [list] : []; + } + private static void CheckPort(List errors, FormField field, string text, bool required) { if (string.IsNullOrWhiteSpace(text)) diff --git a/windows/src/TablePro.App.Model/PastedUrl.cs b/windows/src/TablePro.App.Model/PastedUrl.cs new file mode 100644 index 0000000000..9e36676646 --- /dev/null +++ b/windows/src/TablePro.App.Model/PastedUrl.cs @@ -0,0 +1,18 @@ +namespace TablePro.App.Model; + +// The connection URL in pasted text: its first line, when that names a scheme, as macOS's Import from URL takes it. +public static class PastedUrl +{ + public static string? In(string? text) + { + if (string.IsNullOrWhiteSpace(text)) + { + return null; + } + + var line = text.AsSpan().TrimStart(); + var end = line.IndexOfAny('\r', '\n'); + var first = (end < 0 ? line : line[..end]).Trim().ToString(); + return first.Contains("://", StringComparison.Ordinal) && !first.Contains(' ', StringComparison.Ordinal) ? first : null; + } +} diff --git a/windows/src/TablePro.App.Model/TabSession.cs b/windows/src/TablePro.App.Model/TabSession.cs index 0704303809..073ec7eb65 100644 --- a/windows/src/TablePro.App.Model/TabSession.cs +++ b/windows/src/TablePro.App.Model/TabSession.cs @@ -43,6 +43,9 @@ Task ReadTablePageAsync( Task GetStructureAsync(TableName table, ObjectKind kind, CancellationToken cancellationToken); + // The row estimate with the table's comment and sizes, each null where the engine keeps none. + Task GetTableInfoAsync(TableName table, CancellationToken cancellationToken); + // Null when the object is gone or the engine cannot read its definition. Task GetObjectDefinitionAsync(ObjectEntry entry, CancellationToken cancellationToken); @@ -198,6 +201,9 @@ public Task CountRowsAsync(TableName table, TableFilter? filter, Cancellat public Task GetFilterChoicesAsync(TableName table, CancellationToken cancellationToken) => _share.Session.GetFilterChoicesAsync(table, cancellationToken); + public Task GetTableInfoAsync(TableName table, CancellationToken cancellationToken) => + _share.Session.GetTableInfoAsync(table, cancellationToken); + public Task GetStructureAsync( TableName table, ObjectKind kind, diff --git a/windows/src/TablePro.App.Model/TableFilterMemory.cs b/windows/src/TablePro.App.Model/TableFilterMemory.cs index f79466d5e4..47d4e4619f 100644 --- a/windows/src/TablePro.App.Model/TableFilterMemory.cs +++ b/windows/src/TablePro.App.Model/TableFilterMemory.cs @@ -154,11 +154,9 @@ private void ForgetPrefix(string prefix) } private static string Key(string connection, string? database, string? schema, string table) => - $"{Part(connection)}.{Part(database)}.{Part(schema)}.{Part(table)}"; + TableMemoryKey.Of(connection, database, schema, 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 static string Part(string? part) => TableMemoryKey.Part(part); private Dictionary Tables() => _tables ??= JsonFile.Read(_path, ModelJson.Default.TableFilterFile)?.Tables is { } read @@ -184,3 +182,14 @@ private sealed class Scope(TableFilterMemory memory, string connection, string? public void Remember(TableName table, TableFilter? filter) => memory.Remember(connection, database, table, filter); } } + +// Per connection, database, schema and table, as macOS keys what it keeps of a table. +internal static class TableMemoryKey +{ + public static string Of(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. + public static string Part(string? part) => + Uri.EscapeDataString(part ?? string.Empty).Replace(".", "%2E", StringComparison.Ordinal); +} diff --git a/windows/src/TablePro.App.Model/TableTab.cs b/windows/src/TablePro.App.Model/TableTab.cs index e875749d4f..b741fae61b 100644 --- a/windows/src/TablePro.App.Model/TableTab.cs +++ b/windows/src/TablePro.App.Model/TableTab.cs @@ -30,6 +30,21 @@ public enum CellValueKind // The open editor's cell and text, which a commit would keep; Row counts the page's rows, then the new ones. public sealed record CellDraft(int Row, int Column, string Text); +// One column of a row as the details pane shows it: what the cell holds now, its pending edit included. Text is null +// for NULL and for DEFAULT; a value too long to show whole is cut, and then not edited here. +public sealed record RowField( + int Column, + string Name, + string TypeName, + string? Text, + bool TextCut, + bool IsDefault, + bool IsEdited, + bool CanEdit) +{ + public bool IsNull => Text is null && !IsDefault; +} + // The run that read the page shown, which the status words with the page. public readonly record struct PageRun(long Milliseconds, bool Truncated); @@ -99,6 +114,9 @@ public interface ITableTabView // The grid's open editor keeps its text first; false when the text was refused and the editor stays open. bool CommitOpenEdit(); + // A confirmed Discard drops what is still typed and not kept, a refused value included. + void DiscardOpenEdit(); + void FocusRow(int row); Task ConfirmDiscardForReloadAsync(ReloadReason reason); @@ -123,6 +141,9 @@ public interface ITableTabView // Without the columns and their operators the filter bar has nothing to offer. void ShowFilterChoicesFailed(CoreError failure); + // The details pane's table figures could not be read; the rows stay as they are. + void ShowTableInfoFailed(CoreError failure); + // Recovered changes back and waiting, and whether Save waits for a reload or a discard; all empty hides it. void ShowRecovered(int restored, int waiting, bool saveBlocked); } @@ -148,6 +169,7 @@ public sealed class TableTab : IWorkspaceTab private readonly TabWork _work = new(); private readonly KeptRead _structure; private readonly KeptRead _choices; + private readonly KeptRead _info; private readonly ITableFilters? _filters; // Where each page of a server-paged table resumes, by its offset: a paging state only goes forward. @@ -232,6 +254,10 @@ public TableTab( _work, token => _session.GetFilterChoicesAsync(Table, token), _view.ShowFilterChoicesFailed); + _info = new KeptRead( + _work, + token => _session.GetTableInfoAsync(Table, token), + _view.ShowTableInfoFailed); _results.ShownSetChanged += OnShownSetChanged; } @@ -267,6 +293,9 @@ public TableTab( // Null until LoadStructureAsync has read it. public TableStructure? Structure => _structure.Value; + // The details pane's figures, read when it first shows them and again after a refresh. + public TableInfo? Info => _info.Value; + public TablePage Page => _page; // Null until a run has read the page; a count that arrives later adds the total, as on macOS. @@ -517,6 +546,7 @@ public async Task RefreshAsync() _keys = null; _structure.MarkStale(); _choices.MarkStale(); + _info.MarkStale(); await LoadAsync(_page.Offset, recount: true).ConfigureAwait(true); } } @@ -571,6 +601,8 @@ public async Task FilterByAsync(FilterRule rule) // Read beside the page, never in its way, and kept until a refresh of the tab or of the structure. public Task LoadStructureAsync(bool refresh) => _structure.LoadAsync(refresh); + public Task LoadInfoAsync(bool refresh) => _info.LoadAsync(refresh); + public async Task SortAsync(GridSort sort) { if (IsRunning || !await MayReloadNowAsync(ReloadReason.Sort).ConfigureAwait(true)) @@ -652,6 +684,89 @@ public void PageRead(long rows, long milliseconds, bool truncated) return null; } + // Each column of the row in the table's own order; a binary value shows as the grid shows it, 0x and hex. A stored + // value past maxCharacters is cut and not edited here, so a large one is never read whole to show a row. + public IReadOnlyList FieldsAt(int row, int maxCharacters = CellValue.MaximumShownCharacters) + { + var columns = _results.Columns; + var rows = ShownRows; + var edits = Edits; + if (row < 0 || (edits is null ? rows is null || row >= rows.RowCount : row >= edits.RowCount)) + { + return []; + } + + var fields = new List(columns.Count); + var count = edits is null ? columns.Count : Math.Min(columns.Count, edits.Columns.Count); + for (var column = 0; column < count; column++) + { + var info = columns[column]; + var editable = CanEdit && edits is not null && CellRefusal(edits, row, column) is null; + if (edits?.GetCell(row, column) is { } pending) + { + var edited = !edits.IsAdded(row); + (string? Text, bool IsDefault) shown = pending switch + { + CellEdit.Text typed => (typed.Value, false), + CellEdit.ServerDefault => (null, true), + _ => (null, false), + }; + // A duplicated row's copy of a value is pending text: one with a NUL, or past maxCharacters, is shown + // as the stored value would be, read only. + var cut = shown.Text is { } whole && whole.Length > maxCharacters; + var text = cut ? Head(shown.Text!, maxCharacters) : shown.Text; + var canEdit = editable && !cut && text?.Contains('\0', StringComparison.Ordinal) != true; + fields.Add(new RowField( + column, info.Name, info.TypeName, text, cut, shown.IsDefault, edited, canEdit)); + continue; + } + + var held = StoredField(rows, row, column, info, maxCharacters); + // An editable cell opens with its whole stored text, as the grid's editor does, once it fits. + if (editable && !held.TextCut && ReadStored(row, column, out var stored) is null) + { + // A TextBox ends its text at a NUL, so such a value is shown and never edited here, as in the grid. + var editableText = stored?.Contains('\0', StringComparison.Ordinal) != true; + fields.Add(new RowField(column, info.Name, info.TypeName, stored, false, false, false, editableText)); + continue; + } + + fields.Add(held); + } + + return fields; + } + + // Never between the halves of a surrogate pair. + private static string Head(string text, int length) => + text[..(length > 0 && char.IsHighSurrogate(text[length - 1]) ? length - 1 : length)]; + + private static RowField StoredField(ResultSet? rows, int row, int column, ResultColumn info, int maxCharacters) + { + if (rows is null || row >= rows.RowCount) + { + return new RowField(column, info.Name, info.TypeName, null, false, false, false, false); + } + + try + { + if (rows.GetCellBytes(row, column, Math.Clamp(maxCharacters / 2, 1, CellValue.MaximumHexBytes)) is { } binary) + { + var hex = "0x" + Convert.ToHexStringLower(binary.Bytes.Span); + var cut = binary.Bytes.Length < binary.Length; + return new RowField(column, info.Name, info.TypeName, hex, cut, false, false, false); + } + + return rows.GetCellTextHead(row, column, maxCharacters) is { } head + ? new RowField(column, info.Name, info.TypeName, head.Text, head.Cut, false, false, false) + : new RowField(column, info.Name, info.TypeName, null, false, false, false, false); + } + catch (TableProException) + { + return new RowField(column, info.Name, info.TypeName, null, false, false, false, false); + } + } + // Read as the save will read it, so a cell the user leaves says why it would not save. public EditRefusal? CommitEdit(int row, int column, string text) { @@ -764,6 +879,7 @@ public async Task DiscardAsync() return; } + _view.DiscardOpenEdit(); Unblock(); edits.Discard(); } diff --git a/windows/src/TablePro.App/App.xaml b/windows/src/TablePro.App/App.xaml index 5adb9e63bb..22f180a535 100644 --- a/windows/src/TablePro.App/App.xaml +++ b/windows/src/TablePro.App/App.xaml @@ -39,6 +39,21 @@ + + + + + diff --git a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml index f6e3a2fc5e..ee63e087db 100644 --- a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml +++ b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml @@ -177,6 +177,9 @@ + + + diff --git a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs index bbaef01de6..98a3d4cfc0 100644 --- a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs +++ b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs @@ -35,7 +35,8 @@ public sealed partial class ConnectionFormWindow : Window private bool _fillingTls; private TlsRemedy? _remedy; - internal ConnectionFormWindow(Workbench workbench, Window owner, SavedConnection? existing) + internal ConnectionFormWindow( + Workbench workbench, Window owner, SavedConnection? existing, ConnectionDraft? draft = null) { _workbench = workbench; _isNew = existing is null; @@ -64,6 +65,10 @@ internal ConnectionFormWindow(Workbench workbench, Window owner, SavedConnection { ShowDetails(engine, ConnectionDraft.From(existing, engine)); } + else if (draft is not null && workbench.Engine(draft.EngineId) is { } drafted) + { + ShowDetails(drafted, draft); + } else { ShowChooser(); @@ -74,6 +79,15 @@ internal ConnectionFormWindow(Workbench workbench, Window owner, SavedConnection internal event EventHandler? Completed; + // A URL pasted on the Connections page while this new connection's form is open replaces what it holds. + internal void ShowDraft(ConnectionDraft draft) + { + if (_isNew && !_busy && _workbench.Engine(draft.EngineId) is { } engine) + { + ShowDetails(engine, draft); + } + } + internal string Key { get; } internal static string Keys(Guid? id) => id?.ToString() ?? "new"; @@ -193,6 +207,7 @@ private void ShowDetails(DatabaseEngine engine, ConnectionDraft draft) SshAuthBox.SelectedIndex = (int)draft.SshAuth; SshKeyBox.Text = draft.SshKeyPath; SshPasswordInput.Password = string.Empty; + SshJumpHostsBox.Text = draft.JumpHosts; AuthBox.Visibility = engine.SupportsIntegratedAuth ? Visibility.Visible : Visibility.Collapsed; AuthBox.SelectedIndex = draft.IntegratedAuth ? 1 : 0; FillTlsModes(engine, draft.TlsMode); @@ -236,6 +251,7 @@ private ConnectionDraft ReadDraft() draft.SshAuth = (SshAuthMethod)Math.Max(0, SshAuthBox.SelectedIndex); draft.SshKeyPath = SshKeyBox.Text; draft.SshPassword = SshPasswordInput.Password; + draft.JumpHosts = SshJumpHostsBox.Text; draft.TlsMode = SelectedTlsMode; draft.CaFile = CaFileBox.Text; draft.ServerName = ServerNameBox.Text; @@ -254,7 +270,19 @@ private bool Validate() { ClearErrors(); var draft = ReadDraft(); - var errors = draft.Validate(); + var errors = draft.Validate().ToList(); + if (draft is { IsFileEngine: false, UseSsh: true } && !string.IsNullOrWhiteSpace(draft.JumpHosts)) + { + try + { + TableProCore.CheckJumpHosts(draft.JumpHosts); + } + catch (TableProException) + { + errors.Add(new FieldError(FormField.JumpHosts, FieldProblem.Invalid)); + } + } + if (errors.Count == 0) { return true; @@ -266,7 +294,7 @@ private bool Validate() var field = Field(error.Field); SetError(field, Message(error)); first ??= field; - if (error.Field is FormField.SshHost or FormField.SshPort or FormField.SshKey) + if (error.Field is FormField.SshHost or FormField.SshPort or FormField.SshKey or FormField.JumpHosts) { SshExpander.IsExpanded = true; } @@ -293,6 +321,7 @@ private bool Validate() FormField.SshHost => SshHostBox, FormField.SshPort => SshPortBox, FormField.SshKey => SshKeyBox, + FormField.JumpHosts => SshJumpHostsBox, FormField.ClientKey => ClientKeyBox, _ => TimeoutBox, }; @@ -300,6 +329,7 @@ private bool Validate() private static string Message(FieldError error) => (error.Field, error.Problem) switch { (FormField.ClientKey, _) => Strings.Get("FieldClientPair"), + (FormField.JumpHosts, _) => Strings.Get("FieldJumpHosts"), (_, FieldProblem.Required) => Strings.Get("FieldRequired"), (_, FieldProblem.NotANumber) => Strings.Get("FieldNotANumber"), (_, FieldProblem.NotAbsolute) => Strings.Get("FieldNotAbsolute"), @@ -333,7 +363,8 @@ private void SetError(Control field, string message) private void ClearErrors() { - foreach (var box in new[] { HostBox, PortBox, FileBox, SshHostBox, SshPortBox, SshKeyBox, ClientKeyBox, TimeoutBox }) + TextBox[] boxes = [HostBox, PortBox, FileBox, SshHostBox, SshPortBox, SshKeyBox, SshJumpHostsBox, ClientKeyBox, TimeoutBox]; + foreach (var box in boxes) { box.Description = null; Microsoft.UI.Xaml.Automation.AutomationProperties.SetFullDescription(box, string.Empty); @@ -482,8 +513,12 @@ private void UpdateSsh() // Each section says what it holds while folded. private void UpdateSummaries() { + var server = string.IsNullOrWhiteSpace(SshUserBox.Text) + ? SshHostBox.Text.Trim() + : $"{SshUserBox.Text.Trim()}@{SshHostBox.Text.Trim()}"; + var jumps = SshJumpHostsBox.Text.Trim(); SshExpander.Description = SshSwitch.IsOn && !string.IsNullOrWhiteSpace(SshHostBox.Text) - ? string.IsNullOrWhiteSpace(SshUserBox.Text) ? SshHostBox.Text.Trim() : $"{SshUserBox.Text.Trim()}@{SshHostBox.Text.Trim()}" + ? jumps.Length == 0 ? server : Strings.Format("SshThroughSummary", server, jumps) : Strings.Get(SshSwitch.IsOn ? "SshOnSummary" : "SshOffSummary"); TlsExpander.Description = (TlsModeBox.SelectedItem as ComboBoxItem)?.Content as string ?? string.Empty; var safeMode = (SafeModeBox.SelectedItem as ComboBoxItem)?.Content as string ?? string.Empty; @@ -515,6 +550,14 @@ private void OnSshToggled(object sender, RoutedEventArgs e) private void OnSshAuthChanged(object sender, SelectionChangedEventArgs e) => UpdateSsh(); + private void OnSshSummaryChanged(object sender, TextChangedEventArgs e) + { + if (_draft is not null) + { + UpdateSummaries(); + } + } + private void OnEngineSearchChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) => FillEngines(sender.Text); @@ -565,15 +608,8 @@ private void ContinueFromUrl() return; } - var draft = ConnectionDraft.New(engine); - draft.Apply(parsed, engine); - if (draft.IsFileEngine) - { - draft.Database = parsed.Database; - } - UrlFlyout.Hide(); - ShowDetails(engine, draft); + ShowDetails(engine, ConnectionDraft.FromUrl(parsed, engine)); } catch (TableProException exception) { diff --git a/windows/src/TablePro.App/MainWindow.xaml.cs b/windows/src/TablePro.App/MainWindow.xaml.cs index 33eb485e7f..2b3cc94b8e 100644 --- a/windows/src/TablePro.App/MainWindow.xaml.cs +++ b/windows/src/TablePro.App/MainWindow.xaml.cs @@ -170,6 +170,18 @@ public void NewConnection() ShowForm(null); } + public void NewConnection(ConnectionDraft draft) + { + ArgumentNullException.ThrowIfNull(draft); + if (!IsPrimary) + { + _primary.NewConnection(draft); + return; + } + + ShowForm(null, draft); + } + public void EditConnection(SavedConnection connection) { ArgumentNullException.ThrowIfNull(connection); @@ -876,22 +888,28 @@ private async Task EditAsync(OpenConnection open) } } - private void ShowForm(SavedConnection? existing) + // A draft from a URL goes to the new connection's form, open or not, which then shows what the URL names. + private void ShowForm(SavedConnection? existing, ConnectionDraft? draft = null) { if (!IsPrimary) { - _primary.ShowForm(existing); + _primary.ShowForm(existing, draft); return; } var key = ConnectionFormWindow.Keys(existing?.Id); if (_forms.TryGetValue(key, out var shown)) { + if (draft is not null) + { + shown.ShowDraft(draft); + } + shown.Activate(); return; } - var form = new ConnectionFormWindow(_workbench, this, existing); + var form = new ConnectionFormWindow(_workbench, this, existing, draft); _forms[key] = form; form.Closed += (_, _) => _forms.Remove(key); form.Completed += async (_, result) => await OnFormCompletedAsync(result); diff --git a/windows/src/TablePro.App/Services/Workbench.cs b/windows/src/TablePro.App/Services/Workbench.cs index ac409bfbd2..27c887a492 100644 --- a/windows/src/TablePro.App/Services/Workbench.cs +++ b/windows/src/TablePro.App/Services/Workbench.cs @@ -20,6 +20,7 @@ public Workbench(TableProCore core, string 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")); + ColumnLayouts = new ColumnLayoutMemory(Path.Combine(folder, "table-columns.json")); Settings = _settings.Load(); DisplaySettings.Update(Settings); } @@ -48,6 +49,9 @@ public Workbench(TableProCore core, string folder) // The last filter of each table, read when the table opens again. public TableFilterMemory TableFilters { get; } + // Each table's column order, hidden columns and frozen count, read when the table opens again. + public ColumnLayoutMemory ColumnLayouts { 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 aa7a62db66..32411e9d93 100644 --- a/windows/src/TablePro.App/Strings/en-US/Resources.resw +++ b/windows/src/TablePro.App/Strings/en-US/Resources.resw @@ -4293,4 +4293,169 @@ New fingerprint: {2} The change was stopped. It may have run in part, so the objects are read again. + + New from URL + + + Jump hosts + + + Servers to pass through first, as user@host:port, separated by commas + + + user@bastion:22 + + + Jump hosts + + + Enter each jump host as user@host:port, separated by commas + + + {0} through {1} + + + Color + + + Columns + + + Details + + + Hide column + + + Show all columns + + + Move column left + + + Alt+Left + + + Move column right + + + Alt+Right + + + Freeze up to this column + + + These columns would leave too little room to scroll the rest. + + + Unfreeze columns + + + Columns + + + Reset columns + + + Details + + + Row + + + Table + + + No row is selected. + + + New row + + + Row marked for delete + + + Row {0:N0} + + + {0}, edited + + + NULL + + + Too long to edit here. Open it from the cell's View value. + + + More for {0} + + + Copy value + + + Reading the table's details... + + + The table's details could not be read. + + + Not reported + + + {0:N0} bytes + + + {0:N0} + + + About {0:N0} + + + Rows + + + Data size + + + Index size + + + Total size + + + Comment + + + Sum: {0} Average: {1} Count: {2} + + + Count: {0} + + + Count + + + Numbers + + + Empty + + + Not a number + + + Sum + + + Average + + + Minimum + + + Maximum + + + Copy {0} + diff --git a/windows/src/TablePro.App/Views/DetailsPane.cs b/windows/src/TablePro.App/Views/DetailsPane.cs new file mode 100644 index 0000000000..980a12a777 --- /dev/null +++ b/windows/src/TablePro.App/Views/DetailsPane.cs @@ -0,0 +1,493 @@ +using System.Globalization; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Automation; +using Microsoft.UI.Xaml.Controls; +using TablePro.App.Model; +using TablePro.App.Services; +using TablePro.DataGrid; +using TablePro.Interop; +using Windows.ApplicationModel.DataTransfer; +using Windows.System; + +namespace TablePro.App.Views; + +// A table tab's side pane, as macOS's inspector: the focused row's fields, each edited as its cell in the grid +// would be, and the table's own figures. +internal sealed partial class DetailsPane : UserControl +{ + private readonly TableTab _model; + private readonly DataGridView _grid; + private readonly Func _refusalText; + private readonly SelectorBar _views = new() { Margin = new Thickness(4, 0, 4, 0) }; + private readonly SelectorBarItem _rowItem = new() { IsSelected = true }; + private readonly SelectorBarItem _tableItem = new(); + private readonly StackPanel _rowPanel = new() { Spacing = 12, Padding = new Thickness(12, 4, 12, 12) }; + private readonly StackPanel _tablePanel = new() { Spacing = 12, Padding = new Thickness(12, 4, 12, 12) }; + private readonly ScrollViewer _scroller = new() { VerticalScrollBarVisibility = ScrollBarVisibility.Auto }; + private readonly List _entries = []; + private int _shownRow = -1; + private bool _refreshQueued; + private int _infoReads; + private TextBlock? _rowStatus; + + // Longer values open read only and whole in View value: a wrapping TextBox lays out every character it holds. + private const int FieldCharacters = 10_000; + + public DetailsPane(TableTab model, DataGridView grid, Func refusalText) + { + _model = model; + _grid = grid; + _refusalText = refusalText; + Width = 320; + AutomationProperties.SetAutomationId(this, "DetailsPane"); + AutomationProperties.SetName(this, Strings.Get("DetailsPaneName")); + _rowItem.Text = Strings.Get("DetailsRowView"); + _tableItem.Text = Strings.Get("DetailsTableView"); + AutomationProperties.SetAutomationId(_rowItem, "DetailsRowView"); + AutomationProperties.SetAutomationId(_tableItem, "DetailsTableView"); + AutomationProperties.SetAutomationId(_views, "DetailsViews"); + _views.Items.Add(_rowItem); + _views.Items.Add(_tableItem); + _views.SelectionChanged += (_, _) => ShowView(); + _scroller.Content = _rowPanel; + var root = new Grid { Style = (Style)Application.Current.Resources["PaneDividerStyle"] }; + root.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); + root.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); + Grid.SetRow(_scroller, 1); + root.Children.Add(_views); + root.Children.Add(_scroller); + Content = root; + grid.FocusedCellChanged += (_, _) => QueueRefresh(); + model.StateChanged += (_, _) => QueueRefresh(); + model.ShownRowsChanged += (_, _) => QueueRefresh(rowsChanged: true); + } + + // A field's text typed and not kept yet, which the tab counts as an open edit as it counts the grid's editor. + public event EventHandler? DraftChanged; + + // A field took the focus, so Copy and Select All in the Edit menu reach it rather than the grid. + public event EventHandler? FieldFocused; + + public bool HasDraft => _entries.Any(entry => entry.Changed); + + public CellDraft? Draft => + _entries.FirstOrDefault(entry => entry.Changed) is { } entry + ? new CellDraft(entry.Row, entry.Field.Column, entry.Text) + : null; + + private bool ShowsTable => ReferenceEquals(_views.SelectedItem, _tableItem); + + // Keeps what each field holds, as a save, a reload or a close keeps the grid's open editor first. + public bool CommitDraft() => _entries.Where(entry => entry.Changed).ToList().All(Commit); + + // Discard was confirmed: what each field holds goes back to the value, a refused one too. + public void CancelDraft() + { + foreach (var entry in _entries.Where(entry => entry.Changed)) + { + entry.Box.Text = entry.Field.Text ?? string.Empty; + entry.Box.Description = null; + } + + _shownRow = -1; + QueueRefresh(); + } + + public void Show() + { + _shownRow = -1; + ShowView(); + } + + // After a refresh, the figures are stale with the rows. + public void Reload() + { + if (ShowsTable && Visibility == Visibility.Visible) + { + LoadInfo(refresh: false); + } + } + + private void ShowView() + { + if (ShowsTable) + { + _scroller.Content = _tablePanel; + LoadInfo(refresh: false); + } + else + { + _scroller.Content = _rowPanel; + _shownRow = -1; + RefreshRow(); + } + } + + // Edits and focus moves come in bursts, so the fields are built once after them. + private void QueueRefresh(bool rowsChanged = false) + { + if (rowsChanged) + { + _shownRow = -1; + } + + if (_refreshQueued || Visibility != Visibility.Visible || ShowsTable) + { + return; + } + + _refreshQueued = true; + DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Low, () => + { + _refreshQueued = false; + RefreshRow(); + }); + } + + // The same row's fields are updated in place, so the focus and a field being typed in stay; another row's are + // built anew. + private void RefreshRow() + { + if (Visibility != Visibility.Visible || ShowsTable || _model.IsClosed) + { + return; + } + + var row = _grid.RowCount > 0 ? _grid.FocusedRow : -1; + var fields = row >= 0 ? _model.FieldsAt(row, FieldCharacters) : []; + if (row == _shownRow && _rowStatus is { } shownStatus && fields.Count == _entries.Count + && fields.Select(field => field.Column).SequenceEqual(_entries.Select(entry => entry.Field.Column))) + { + shownStatus.Text = RowStatus(row); + for (var index = 0; index < fields.Count; index++) + { + var entry = _entries[index]; + if (!entry.Changed) + { + entry.Show(fields[index]); + } + + entry.Box.IsReadOnly = !Editable(entry); + ShowMark(entry); + } + + return; + } + + _shownRow = row; + _rowPanel.Children.Clear(); + _entries.Clear(); + _rowStatus = null; + if (fields.Count == 0) + { + _rowPanel.Children.Add(Note(Strings.Get("DetailsNoRow"))); + return; + } + + var status = new TextBlock + { + Text = RowStatus(row), + Style = (Style)Application.Current.Resources["BodyStrongTextBlockStyle"], + }; + AutomationProperties.SetAutomationId(status, "DetailsRowStatus"); + AutomationProperties.SetHeadingLevel(status, Microsoft.UI.Xaml.Automation.Peers.AutomationHeadingLevel.Level2); + _rowStatus = status; + _rowPanel.Children.Add(status); + foreach (var field in fields) + { + _rowPanel.Children.Add(FieldView(row, field)); + } + } + + private string RowStatus(int row) => _model.Edits switch + { + { } edits when edits.IsAdded(row) => Strings.Get("DetailsNewRow"), + { } edits when edits.IsDeleted(row) => Strings.Get("DetailsDeletedRow"), + _ => Strings.Format("DetailsRowNumber", _model.Page.Offset + row + 1), + }; + + private StackPanel FieldView(int row, RowField field) + { + var name = new TextBlock + { + Text = field.Name, + Style = (Style)Application.Current.Resources["BodyStrongTextBlockStyle"], + TextTrimming = TextTrimming.CharacterEllipsis, + }; + var type = new TextBlock { VerticalAlignment = VerticalAlignment.Center }; + var title = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 8 }; + title.Children.Add(name); + title.Children.Add(type); + + // Multi-line before the text: a single-line TextBox keeps only a value's first line. + var box = new TextBox + { + AcceptsReturn = true, + TextWrapping = TextWrapping.Wrap, + MaxHeight = 200, + IsSpellCheckEnabled = false, + }; + var entry = new FieldEntry(row, box, type); + entry.Show(field); + _entries.Add(entry); + box.IsReadOnly = !Editable(entry); + ShowMark(entry); + AutomationProperties.SetName(box, field.Name); + var id = field.Column.ToString(CultureInfo.InvariantCulture); + AutomationProperties.SetAutomationId(box, "DetailsField_" + id); + + box.TextChanged += (_, _) => DraftChanged?.Invoke(this, EventArgs.Empty); + box.GotFocus += (_, _) => FieldFocused?.Invoke(this, box); + // As the grid's editor on a focus loss: a refused value goes back, with the reason left under the field and in + // the status, so at most the focused field holds text not kept. + box.LostFocus += (_, _) => + { + // The field's own menus take the focus while they show; the text stays the field's. + if (box.ContextFlyout is { IsOpen: true } || box.SelectionFlyout is { IsOpen: true }) + { + return; + } + + if (!Commit(entry)) + { + entry.Show(entry.Field); + } + }; + box.KeyDown += (_, e) => + { + if (e.Key == VirtualKey.Escape && !box.IsReadOnly && entry.Changed) + { + e.Handled = true; + box.Text = entry.Field.Text ?? string.Empty; + box.Description = null; + } + else if (e.Key == VirtualKey.Enter && Microsoft.UI.Input.InputKeyboardSource + .GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down)) + { + e.Handled = true; + Commit(entry); + } + }; + + var more = new Button + { + Content = new SymbolIcon(Symbol.More), + VerticalAlignment = VerticalAlignment.Top, + Flyout = FieldMenu(entry), + }; + AutomationProperties.SetName(more, Strings.Format("DetailsFieldMenu", field.Name)); + AutomationProperties.SetAutomationId(more, "DetailsFieldMenu_" + id); + var line = new Grid { ColumnSpacing = 4 }; + line.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); + line.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); + Grid.SetColumn(more, 1); + line.Children.Add(box); + line.Children.Add(more); + + var view = new StackPanel { Spacing = 4 }; + view.Children.Add(title); + view.Children.Add(line); + return view; + } + + // A data row's field with a pending value says so; a new row's are all new. + private void ShowMark(FieldEntry entry) + { + var edited = _model.Edits is { } edits && !edits.IsAdded(entry.Row) + && edits.GetCell(entry.Row, entry.Field.Column) is not null; + entry.Type.Text = edited ? Strings.Format("DetailsTypeEdited", entry.Field.TypeName) : entry.Field.TypeName; + entry.Type.Style = (Style)Application.Current.Resources[ + edited ? "AccentCaptionTextBlockStyle" : "SecondaryCaptionTextBlockStyle"]; + } + + // Kept in place, so the field the focus moved to, or this one after Ctrl+Enter, stays as it is. False when the + // value was refused, which the field then says. + private bool Commit(FieldEntry entry) + { + var box = entry.Box; + if (!entry.Changed) + { + box.Description = null; + return true; + } + + // The grid's own editor keeps its text first, so two edits of one cell cannot cross. + if (!_grid.CommitEdit()) + { + return false; + } + + var text = entry.Text; + if (_model.CommitEdit(entry.Row, entry.Field.Column, text) is { } refusal) + { + box.Description = _refusalText(refusal) ?? Strings.Get("EditCellReadOnly"); + return false; + } + + box.Description = null; + entry.Keep(text); + ShowMark(entry); + DraftChanged?.Invoke(this, EventArgs.Empty); + return true; + } + + // Read as it opens, so it acts on what the field holds now, a value just kept included. + private MenuFlyout FieldMenu(FieldEntry entry) + { + var menu = new MenuFlyout(); + var values = new[] + { + ValueItem("GridMenuEmpty", entry, CellValueKind.Empty), + ValueItem("GridMenuNull", entry, CellValueKind.Null), + ValueItem("GridMenuDefault", entry, CellValueKind.Default), + }; + foreach (var (item, _) in values) + { + menu.Items.Add(item); + } + + menu.Items.Add(new MenuFlyoutSeparator()); + var copy = new MenuFlyoutItem { Text = Strings.Get("DetailsCopyValue"), Icon = new SymbolIcon(Symbol.Copy) }; + copy.Click += (_, _) => + { + var package = new DataPackage(); + package.SetText(entry.Shown ?? string.Empty); + Clipboard.SetContent(package); + Clipboard.Flush(); + }; + menu.Items.Add(copy); + menu.Opening += (_, _) => + { + foreach (var (item, kind) in values) + { + item.IsEnabled = Editable(entry) && _model.CanSetValue(entry.Row, entry.Field.Column, kind); + } + + copy.IsEnabled = entry.Shown is not null && !entry.Field.TextCut; + }; + return menu; + } + + private (MenuFlyoutItem Item, CellValueKind Kind) ValueItem(string text, FieldEntry entry, CellValueKind kind) + { + var item = new MenuFlyoutItem { Text = Strings.Get(text) }; + item.Click += (_, _) => + { + if (_grid.CommitEdit() && _model.SetValue(entry.Row, entry.Field.Column, kind) is null) + { + _shownRow = -1; + QueueRefresh(); + } + }; + return (item, kind); + } + + // As the tab allows now: a save or a reload under way takes edits from every field, the focused one too. + private bool Editable(FieldEntry entry) => entry.Field.CanEdit && !entry.Field.TextCut && _model.CanEdit; + + // A read a refresh overtook answers last with the older figures, so only the newest read shows. + private async void LoadInfo(bool refresh) + { + var reading = ++_infoReads; + ShowInfo(_model.Info, loading: true); + var info = await _model.LoadInfoAsync(refresh).ConfigureAwait(true); + if (reading == _infoReads) + { + ShowInfo(info, loading: false); + } + } + + // A figure the engine keeps none of says so, rather than leaving a gap. + private void ShowInfo(TableInfo? info, bool loading) + { + _tablePanel.Children.Clear(); + if (info is null) + { + _tablePanel.Children.Add(Note(Strings.Get(loading ? "DetailsTableLoading" : "DetailsTableUnavailable"))); + return; + } + + var none = Strings.Get("DetailsNotKept"); + string Size(long? bytes) => bytes is { } value ? ByteSize.Format(value, Strings.Get("DetailsBytes"), CultureInfo.CurrentCulture) : none; + var rows = info.Rows is { } estimate + ? Strings.Format(estimate.IsExact ? "DetailsRowsExact" : "DetailsRowsAbout", estimate.Rows) + : none; + _tablePanel.Children.Add(Figure("DetailsRows", rows, "DetailsRowsValue")); + _tablePanel.Children.Add(Figure("DetailsDataSize", Size(info.DataBytes), "DetailsDataSizeValue")); + _tablePanel.Children.Add(Figure("DetailsIndexSize", Size(info.IndexBytes), "DetailsIndexSizeValue")); + _tablePanel.Children.Add(Figure("DetailsTotalSize", Size(info.TotalBytes), "DetailsTotalSizeValue")); + _tablePanel.Children.Add(Figure("DetailsComment", info.Comment ?? none, "DetailsCommentValue")); + } + + private static StackPanel Figure(string label, string value, string automationId) + { + var title = new TextBlock + { + Text = Strings.Get(label), + Style = (Style)Application.Current.Resources["SecondaryCaptionTextBlockStyle"], + }; + var text = new TextBlock { Text = value, TextWrapping = TextWrapping.Wrap, IsTextSelectionEnabled = true }; + AutomationProperties.SetAutomationId(text, automationId); + AutomationProperties.SetName(text, $"{title.Text}: {value}"); + var figure = new StackPanel { Spacing = 2 }; + figure.Children.Add(title); + figure.Children.Add(text); + return figure; + } + + private static TextBlock Note(string text) => new() + { + Text = text, + TextWrapping = TextWrapping.Wrap, + Style = (Style)Application.Current.Resources["SecondaryTextBlockStyle"], + }; + + private sealed class FieldEntry(int row, TextBox box, TextBlock type) + { + private string _newline = "\n"; + private string _kept = string.Empty; + + public int Row => row; + + public RowField Field { get; private set; } = null!; + + public TextBox Box => box; + + public TextBlock Type => type; + + // With the line breaks the value had, which a TextBox turns into CR. + public string Text => LineBreaks.Restore(box.Text, _newline); + + // An empty box over NULL or DEFAULT is no edit: Set to Empty makes one. + public bool Changed => !box.IsReadOnly && !string.Equals(Text, _kept, StringComparison.Ordinal) + && !(Field.Text is null && box.Text.Length == 0); + + // What Copy takes: the text typed, else the value. + public string? Shown => Changed ? Text : Field.Text; + + // A NUL would end the TextBox's text, so it shows as the symbol for one, in a field that is read only. + public void Show(RowField field) + { + Field = field; + _newline = LineBreaks.Of(field.Text); + var text = (field.Text ?? string.Empty).Replace('\0', '\u2400'); + // Set only when it differs, so a field holding the focus keeps its caret. + if (!string.Equals(LineBreaks.Restore(box.Text, _newline), text, StringComparison.Ordinal)) + { + box.Text = text; + } + + _kept = LineBreaks.Restore(box.Text, _newline); + box.PlaceholderText = field.IsDefault ? Strings.Get("GridDefaultText") + : field.IsNull ? Strings.Get("DetailsNullText") + : string.Empty; + ToolTipService.SetToolTip(box, field.TextCut ? Strings.Get("DetailsTextCut") : null); + } + + public void Keep(string text) + { + _kept = text; + Field = Field with { Text = text, IsDefault = false }; + } + } +} diff --git a/windows/src/TablePro.App/Views/HomeView.xaml b/windows/src/TablePro.App/Views/HomeView.xaml index dd112ffbdf..ff27f7f67a 100644 --- a/windows/src/TablePro.App/Views/HomeView.xaml +++ b/windows/src/TablePro.App/Views/HomeView.xaml @@ -4,7 +4,13 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TablePro.App.Views" - xmlns:toolkit="using:CommunityToolkit.WinUI.Controls"> + xmlns:toolkit="using:CommunityToolkit.WinUI.Controls" + KeyboardAcceleratorPlacementMode="Hidden"> + + + + + @@ -33,6 +39,17 @@ DefaultLabelPosition="Right" IsDynamicOverflowEnabled="True"> + + + + + + +