Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Each boundary error has a stable `code()`, such as `driver.timeout` or `ssh.host

`ffi` is the library an app outside Rust loads: the Windows app as a DLL, the Apple apps as a static library in an XCFramework, built without the `engines` feature by `apple/scripts/build-core.sh`. Core ADRs 0002, on the bindings, 0003, on result windows, and 0009, on the macOS slices, hold the reasons.

- One UniFFI API. `Core` lists the engines with the family each is listed under, opens a file, connects a saved connection or tests one from a form, and parses a connection URL with `tablepro_core::connection_url`, refusing one that needs a setting its record has no field for. Its `ConnectionLibrary` reads and changes the saved connections, groups, favourites and tags in `connections.json` and stores their secrets, and gives the Apple apps the library's CloudKit records and merges a pull into it (`tablepro_storage::cloudkit`). A `DatabaseSession` reads the catalog one level at a time, writes a table's sorted page at an offset and counts the table, writes a query re-run with a sort (`docs/decisions/0007-sorting-a-result.md`), picks the statement at a caret with the session's grammar (`statement_at`), says what Safe Mode lets an editor run do at the session's level and takes a level saved while it is open (`gate_run`, over `SafeModeLevel::gate`, and `set_safe_mode`), reports and waits on the session's health, and opens a `QueryEditor`. `QueryEditor::run` returns a `QueryRun` at once; the app drains its events after a `RunListener` call, which comes only when no earlier one is waiting to be drained, and the last says which transaction the editor is left in. Each result set is a `ResultSet`, whose `sorted` gives the rows it holds in a column's order.
- One UniFFI API. `Core` lists the engines with the family each is listed under, opens a file, connects a saved connection or tests one from a form, and parses a connection URL with `tablepro_core::connection_url`, refusing one that needs a setting its record has no field for. Its `ConnectionLibrary` reads and changes the saved connections, groups, favourites and tags in `connections.json` and stores their secrets, and gives the Apple apps the library's CloudKit records and merges a pull into it (`tablepro_storage::cloudkit`). A `DatabaseSession` reads the catalog one level at a time, writes a table's sorted page at an offset and counts the table, writes a query re-run with a sort (`docs/decisions/0007-sorting-a-result.md`), picks the statement at a caret with the session's grammar (`statement_at`), says what Safe Mode lets an editor run do at the session's level and takes a level saved while it is open (`gate_run`, over `SafeModeLevel::gate`, and `set_safe_mode`), reports and waits on the session's health, opens a `QueryEditor`, and opens a `TableEditor`, which plans a table tab's typed edits into a `RowSave` and runs it (`docs/decisions/0019-row-edits-through-the-ffi.md`). `QueryEditor::run` returns a `QueryRun` at once; the app drains its events after a `RunListener` call, which comes only when no earlier one is waiting to be drained, and the last says which transaction the editor is left in. Each result set is a `ResultSet`, whose `sorted` gives the rows it holds in a column's order.
- The grid reads a `ResultSet` through the C ABI in `src/window_abi.rs`: `tablepro_cursor_fetch` packs the display text of one visible range, and `tablepro_cursor_free` ends the cursor.
- The library owns one tokio runtime. Each async export runs on it and is aborted when the foreign side drops the call; a `CancelToken` stops it sooner, since UniFFI cannot cancel.
- A result set takes rows as the driver reads them. Each column keeps a kind byte, a raw `i64` and the exact text `tablepro_core::export::value_text` writes, or a binary value's bytes, in chunks of 65,536 rows. A window only cuts and copies.
Expand Down
32 changes: 32 additions & 0 deletions core/crates/core/src/dml/build_sql_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,35 @@ pub enum BuildSqlError {
#[error(transparent)]
Bind(#[from] BindError),
}

impl BuildSqlError {
// Stable across releases, for an app outside Rust that words the error itself.
pub fn code(&self) -> &'static str {
match self {
Self::NoRowIdentity => "build.no_row_identity",
Self::ReadOnlyRows => "build.read_only_rows",
Self::UnknownKeyColumn { .. } => "build.unknown_key_column",
Self::NothingToUpdate => "build.nothing_to_update",
Self::NoInsertValues => "build.no_insert_values",
Self::LengthMismatch { .. } => "build.length_mismatch",
Self::UndecodableKey => "build.undecodable_key",
Self::UnaddressableField { .. } => "build.unaddressable_field",
Self::ServerComputedValue { .. } => "build.server_computed_value",
Self::Bind(_) => "build.bind",
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn each_code_names_its_variant_under_build() {
assert_eq!(BuildSqlError::NoRowIdentity.code(), "build.no_row_identity");
assert_eq!(
BuildSqlError::ServerComputedValue { name: "id".to_owned() }.code(),
"build.server_computed_value"
);
}
}
62 changes: 62 additions & 0 deletions core/crates/core/src/edit/edit_parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,65 @@ pub enum ReadOnlyReason {
Composite,
Variant,
}

impl EditParseError {
// Stable across releases, for an app outside Rust that words the error itself.
pub fn code(&self) -> &'static str {
match self {
Self::Required => "edit.required",
Self::InvalidInteger => "edit.invalid_integer",
Self::IntegerOutOfRange { .. } => "edit.integer_out_of_range",
Self::InvalidFloat => "edit.invalid_float",
Self::InvalidDecimal(_) => "edit.invalid_decimal",
Self::PrecisionExceeded { .. } => "edit.precision_exceeded",
Self::ScaleExceeded { .. } => "edit.scale_exceeded",
Self::InvalidBoolean => "edit.invalid_boolean",
Self::InvalidUuid => "edit.invalid_uuid",
Self::InvalidJson(_) => "edit.invalid_json",
Self::InvalidDate => "edit.invalid_date",
Self::InvalidTime => "edit.invalid_time",
Self::TimeOutOfRange => "edit.time_out_of_range",
Self::InvalidTimestamp => "edit.invalid_timestamp",
Self::OffsetRequired => "edit.offset_required",
Self::InvalidInterval => "edit.invalid_interval",
Self::InvalidBits => "edit.invalid_bits",
Self::InvalidBytes => "edit.invalid_bytes",
Self::BitLengthMismatch { .. } => "edit.bit_length_mismatch",
Self::UnknownEnumLabel => "edit.unknown_enum_label",
Self::NotEditable(_) => "edit.not_editable",
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn each_code_names_its_variant_under_edit() {
let codes = [
EditParseError::Required.code(),
EditParseError::InvalidInteger.code(),
EditParseError::IntegerOutOfRange {
kind: IntegerKind {
width: crate::column::IntegerWidth::W32,
signed: true,
},
}
.code(),
EditParseError::UnknownEnumLabel.code(),
EditParseError::NotEditable(ReadOnlyReason::Bytes).code(),
];

assert_eq!(
codes,
[
"edit.required",
"edit.invalid_integer",
"edit.integer_out_of_range",
"edit.unknown_enum_label",
"edit.not_editable"
]
);
}
}
8 changes: 8 additions & 0 deletions core/crates/ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ impl CoreError {
Self::Failed { info } => &info.code,
}
}

// As a record, a cancel included, for an outcome that reports the error instead of failing the call.
pub(crate) fn into_info(self) -> BoxedErrorInfo {
match self {
Self::Failed { info } => info,
Self::Cancelled => Box::new(ErrorInfo::plain(&["cancelled"], "cancelled".to_owned())),
}
}
}

impl ErrorInfo {
Expand Down
5 changes: 5 additions & 0 deletions core/crates/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod host_key;
mod library;
mod prompter;
mod results;
mod row_edits;
mod run;
mod runtime;
mod safe_mode_gate;
Expand All @@ -38,6 +39,10 @@ pub use library::{
};
pub use prompter::{HostPrompter, PromptAnswer, PromptFieldInfo, PromptPurposeTag, PromptReasonTag, PromptRequest};
pub use results::{ResultColumnInfo, ResultSet, ValueKind};
pub use row_edits::{
CellChange, CellEdit, EditColumnInfo, EditLock, EditStart, PlannedStatement, RowEdit, RowSave, RowSaveOutcome,
SaveGate, TableEditor,
};
pub use run::{QueryRun, RunEvent, RunListener, RunOutcome, TransactionStateTag, TruncationTag};
pub use safe_mode_gate::RunGate;
pub use session::{ColumnSort, DatabaseSession, FailureCauseTag, SessionHealthTag, TableQuery};
Expand Down
79 changes: 79 additions & 0 deletions core/crates/ffi/src/results/column_cells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,46 @@ pub(super) fn cell_value(cell: Cell<'_>) -> Value {
})
}

// The value a row was read with, for a key or an unchanged edit: what `push` kept exactly, or the text it wrote read back
// as the column it was read as, which `value_text` writes for that. `None` where neither names the stored value.
pub(super) fn stored_value(cell: Cell<'_>, column: &ColumnType) -> Option<Value> {
let Some(kind) = cell.kind else {
return Some(Value::Null);
};
match kind {
ValueKind::Bool
| ValueKind::Integer
| ValueKind::UnsignedInteger
| ValueKind::WideInteger
| ValueKind::Decimal
| ValueKind::Text
| ValueKind::Bytes
| ValueKind::Uuid
| ValueKind::Date => match cell_value(cell) {
Value::Other(_) => None,
value => Some(value),
},
ValueKind::Time
| ValueKind::TimeWithOffset
| ValueKind::Timestamp
| ValueKind::TimestampWithOffset
| ValueKind::Interval
| ValueKind::Bits => {
let text = std::str::from_utf8(cell.bytes).ok()?;
tablepro_core::edit::parse_literal_text(text, column).ok()
}
// A float widened to a double binds digits the column may not hold.
ValueKind::Float
| ValueKind::Json
| ValueKind::Xml
| ValueKind::Array
| ValueKind::Network
| ValueKind::Spatial
| ValueKind::Other
| ValueKind::Undecodable => None,
}
}

fn from_raw<T>(raw: i64, finite: impl FnOnce(i64) -> Option<T>) -> Temporal<T> {
match raw {
i64::MAX => Temporal::Infinity,
Expand Down Expand Up @@ -409,4 +449,43 @@ mod tests {
assert_eq!(target.cell(3).bytes, [0xde, 0xad]);
assert_eq!(target.cell(3).raw, 2);
}

#[test]
fn a_stored_value_is_exact_or_absent() {
use tablepro_core::column::{ColumnKind, ReadForm, TemporalZone};
use tablepro_core::value::Temporal;

let stamp_type = ColumnType {
name: "datetime2".to_owned(),
kind: ColumnKind::Timestamp {
fractional_digits: Some(7),
zone: TemporalZone::Naive,
},
catalog: None,
dynamic_storage: false,
read_form: ReadForm::Native,
};
let stamp = Value::Timestamp(Temporal::Finite(
NaiveDate::from_ymd_opt(2024, 6, 15)
.and_then(|date| date.and_hms_nano_opt(12, 0, 0, 123_456_700))
.expect("a timestamp"),
));
let mut timestamps = ColumnCells::default();
timestamps.push(&stamp, &stamp_type);
let held = cells(&[
Value::Int(-7),
Value::Text(" 2024-01-01 ".to_owned()),
Value::Float64(0.1),
Value::Null,
]);

assert_eq!(stored_value(held.cell(0), &column()), Some(Value::Int(-7)));
assert_eq!(
stored_value(held.cell(1), &column()),
Some(Value::Text(" 2024-01-01 ".to_owned()))
);
assert_eq!(stored_value(held.cell(2), &column()), None);
assert_eq!(stored_value(held.cell(3), &column()), Some(Value::Null));
assert_eq!(stored_value(timestamps.cell(0), &stamp_type), Some(stamp));
}
}
16 changes: 16 additions & 0 deletions core/crates/ffi/src/results/result_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ impl ResultSet {
}
}

pub(crate) fn result_columns(&self) -> Arc<[tablepro_core::column::ResultColumn]> {
self.store.columns()
}

pub(crate) fn stored_value(&self, row: u64, column: usize) -> Option<tablepro_core::Value> {
self.store.stored_value(self.stored_row(row), column)
}

pub(crate) fn cell_kind(&self, row: u64, column: usize) -> Option<Option<ValueKind>> {
self.store.cell_kind(self.stored_row(row), column)
}

pub(crate) fn full_text(&self, row: u64, column: usize) -> Option<Option<String>> {
self.store.full_text(self.stored_row(row), column)
}

fn stored_row(&self, row: u64) -> u64 {
self.order
.as_ref()
Expand Down
16 changes: 15 additions & 1 deletion core/crates/ffi/src/results/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tablepro_core::Value;
use tablepro_core::column::ResultColumn;
use tablepro_core::value::order::{row_order, sorts_as_text};

use super::column_cells::{Cell, ColumnCells, cell_value};
use super::column_cells::{Cell, ColumnCells, cell_value, stored_value};
use super::{ValueKind, cell_text};

// A full chunk is never written again, so an append copies at most one chunk's arrays when they grow.
Expand Down Expand Up @@ -130,6 +130,20 @@ impl ResultStore {
}
}

impl ResultStore {
// `None` when the cell is not there or its value cannot be named exactly (`stored_value`).
pub(crate) fn stored_value(&self, row: u64, column: usize) -> Option<Value> {
let state = self.read();
let read = &state.columns.get(column)?.column_type;
stored_value(state.cell(row, column)?, read)
}

// The kind of the value in a cell, `None` for null; a dynamically typed column can hold any.
pub(crate) fn cell_kind(&self, row: u64, column: usize) -> Option<Option<ValueKind>> {
Some(self.read().cell(row, column)?.kind)
}
}

impl ResultStore {
// The rows held now, in a column's order. Rows that arrive later follow them as they came.
pub(crate) fn row_order(&self, column: usize, ascending: bool) -> Option<Vec<u64>> {
Expand Down
Loading
Loading