Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
74 changes: 67 additions & 7 deletions core/crates/core/src/import/csv_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,41 @@ pub struct CsvSheet {
/// mapping decides which fields matter and a file the user already has
/// is not improved by refusing to look at it.
pub fn read_csv(bytes: &[u8], options: &CsvImportOptions, limit: Option<usize>) -> Result<CsvSheet, ImportError> {
let CsvRecords {
headers,
mut rows,
width,
truncated,
} = read_csv_records(bytes, options, limit)?;
// A short row is padded so every row can be indexed by field
// number without a bounds check at every cell.
for row in &mut rows {
row.resize(width.max(row.len()), String::new());
}

Ok(CsvSheet {
headers,
rows,
truncated,
})
}

// A file's records as `read_csv` reads them, each as long as it was written, with the widest record's width. A
// whole-file import pads one row at a time with `padded_row`: one wide record padding every row of a file of
// narrow ones would take far more memory than the file.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CsvRecords {
pub headers: Vec<String>,
pub rows: Vec<Vec<String>>,
pub width: usize,
pub truncated: bool,
}

pub fn read_csv_records(
bytes: &[u8],
options: &CsvImportOptions,
limit: Option<usize>,
) -> Result<CsvRecords, ImportError> {
if let Err(error) = std::str::from_utf8(bytes) {
return Err(ImportError::NotUtf8 {
offset: error.valid_up_to(),
Expand Down Expand Up @@ -137,19 +172,20 @@ pub fn read_csv(bytes: &[u8], options: &CsvImportOptions, limit: Option<usize>)
true => vec![String::new(); width],
false => headers,
};
// A short row is padded so every row can be indexed by field
// number without a bounds check at every cell.
for row in &mut rows {
row.resize(width.max(row.len()), String::new());
}

Ok(CsvSheet {
Ok(CsvRecords {
headers,
rows,
width,
truncated,
})
}

// A row of `read_csv_records` as `read_csv` gives it: short rows padded to `width` with empty fields.
pub fn padded_row(mut row: Vec<String>, width: usize) -> Vec<String> {
row.resize(width.max(row.len()), String::new());
row
}

pub(super) fn malformed(error: csv::Error) -> ImportError {
ImportError::Malformed {
line: error.position().map(csv::Position::line),
Expand Down Expand Up @@ -342,6 +378,30 @@ mod tests {
assert_eq!(sheet.rows[0], vec!["1".to_owned(), String::new(), String::new()]);
}

#[test]
fn records_keep_each_row_as_written_and_padding_gives_what_read_csv_gives() {
let file = b"a,b\n1\n2,3,4\n";

let records = read_csv_records(file, &options(), None).expect("records");
let sheet = read_csv(file, &options(), None).expect("sheet");

assert_eq!(records.width, 3);
assert_eq!(
records.rows,
vec![
vec!["1".to_owned()],
vec!["2".to_owned(), "3".to_owned(), "4".to_owned()]
]
);
let padded: Vec<Vec<String>> = records
.rows
.into_iter()
.map(|row| padded_row(row, records.width))
.collect();
assert_eq!(padded, sheet.rows);
assert_eq!(records.headers, sheet.headers);
}

#[test]
fn a_long_row_keeps_the_fields_the_header_did_not_name() {
let sheet = read_csv(b"a,b\n1,2,3\n", &options(), None).expect("read");
Expand Down
4 changes: 2 additions & 2 deletions core/crates/core/src/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ mod sql_file;
mod sql_import;

pub use csv_import::{
CsvImportOptions, CsvRowError, CsvSheet, ImportError, MAX_CSV_FILE_BYTES, MAX_FIELDS, MAX_PREVIEW_ROWS, read_csv,
row_to_cells, suggest_mapping,
CsvImportOptions, CsvRecords, CsvRowError, CsvSheet, ImportError, MAX_CSV_FILE_BYTES, MAX_FIELDS, MAX_PREVIEW_ROWS,
padded_row, read_csv, read_csv_records, row_to_cells, suggest_mapping,
};
pub use import_column::ImportColumn;
pub use import_plan::{ImportPlan, plan_import};
Expand Down
11 changes: 11 additions & 0 deletions core/crates/ffi/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,17 @@ impl Drop for PartFile {
}
}

// A file a store's export writes whole, beside its place and renamed over it once synced. Blocking.
pub(crate) fn write_whole_file(path: &str, bytes: &[u8]) -> Result<(), CoreError> {
let part = PartFile::beside(destination(path)?)?;
let mut file = File::create(&part.path).map_err(|error| io_error(&part.target, &error))?;
file.write_all(bytes)
.and_then(|()| file.sync_all())
.map_err(|error| io_error(&part.target, &error))?;
drop(file);
part.place()
}

async fn blocking<T, F>(tasks: &Tasks, work: F) -> Result<T, CoreError>
where
T: Send + 'static,
Expand Down
173 changes: 173 additions & 0 deletions core/crates/ffi/src/favourites.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
use std::sync::Arc;

use tablepro_session::runtime::Tasks;
use tablepro_storage::{DatabaseEnvironment, FavouriteDatabase, FavouriteTable, Favourites};
use uuid::Uuid;

use crate::error::CoreError;
use crate::library::parse_id;
use crate::runtime::on_runtime;

// The tables and databases a user marked, per connection, in the query history's database (core ADR 0017).
#[derive(uniffi::Object)]
pub struct FavouriteStore {
favourites: Favourites,
tasks: Tasks,
}

// A table by its database and schema, None where the engine has neither.
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
pub struct FavouriteTableInfo {
pub database: Option<String>,
pub schema: Option<String>,
pub name: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
pub enum DatabaseEnvironmentTag {
Development,
Testing,
Production,
Unassigned,
}

#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
pub struct FavouriteDatabaseInfo {
pub database: String,
pub environment: DatabaseEnvironmentTag,
}

impl FavouriteStore {
pub(crate) fn new(favourites: Favourites, tasks: Tasks) -> Arc<Self> {
Arc::new(Self { favourites, tasks })
}
}

#[uniffi::export]
impl FavouriteStore {
// By database, schema and name.
pub async fn tables(&self, connection_id: String) -> Result<Vec<FavouriteTableInfo>, CoreError> {
let connection = parse_id(&connection_id)?;
let favourites = self.favourites.clone();
on_runtime(&self.tasks, async move {
Ok(favourites
.tables(connection)
.await?
.into_iter()
.map(FavouriteTableInfo::from)
.collect())
})
.await
}

// A table marked already stays as it is.
pub async fn add_table(&self, connection_id: String, table: FavouriteTableInfo) -> Result<(), CoreError> {
let table = stored(&connection_id, table)?;
let favourites = self.favourites.clone();
on_runtime(&self.tasks, async move { Ok(favourites.add_table(&table).await?) }).await
}

pub async fn remove_table(&self, connection_id: String, table: FavouriteTableInfo) -> Result<(), CoreError> {
let table = stored(&connection_id, table)?;
let favourites = self.favourites.clone();
on_runtime(&self.tasks, async move { Ok(favourites.remove_table(&table).await?) }).await
}

// By name, in any case.
pub async fn databases(&self, connection_id: String) -> Result<Vec<FavouriteDatabaseInfo>, CoreError> {
let connection = parse_id(&connection_id)?;
let favourites = self.favourites.clone();
on_runtime(&self.tasks, async move {
Ok(favourites
.databases(connection)
.await?
.into_iter()
.map(FavouriteDatabaseInfo::from)
.collect())
})
.await
}

// Marks the database, or files a marked one under another environment.
pub async fn set_database(
&self,
connection_id: String,
database: String,
environment: DatabaseEnvironmentTag,
) -> Result<(), CoreError> {
let connection = parse_id(&connection_id)?;
let favourites = self.favourites.clone();
on_runtime(&self.tasks, async move {
Ok(favourites
.set_database(connection, &database, environment.into())
.await?)
})
.await
}

pub async fn remove_database(&self, connection_id: String, database: String) -> Result<(), CoreError> {
let connection = parse_id(&connection_id)?;
let favourites = self.favourites.clone();
on_runtime(&self.tasks, async move {
Ok(favourites.remove_database(connection, &database).await?)
})
.await
}
}

fn stored(connection_id: &str, table: FavouriteTableInfo) -> Result<FavouriteTable, CoreError> {
let connection_id: Uuid = parse_id(connection_id)?;
Ok(FavouriteTable {
connection_id,
database: table.database.filter(|database| !database.is_empty()),
schema: table.schema.filter(|schema| !schema.is_empty()),
name: table.name,
})
}

impl From<FavouriteTable> for FavouriteTableInfo {
fn from(table: FavouriteTable) -> Self {
Self {
database: table.database,
schema: table.schema,
name: table.name,
}
}
}

impl From<FavouriteDatabase> for FavouriteDatabaseInfo {
fn from(database: FavouriteDatabase) -> Self {
Self {
database: database.database,
environment: database.environment.into(),
}
}
}

impl From<DatabaseEnvironment> for DatabaseEnvironmentTag {
fn from(environment: DatabaseEnvironment) -> Self {
match environment {
DatabaseEnvironment::Development => Self::Development,
DatabaseEnvironment::Testing => Self::Testing,
DatabaseEnvironment::Production => Self::Production,
DatabaseEnvironment::Unassigned => Self::Unassigned,
}
}
}

impl From<DatabaseEnvironmentTag> for DatabaseEnvironment {
fn from(environment: DatabaseEnvironmentTag) -> Self {
match environment {
DatabaseEnvironmentTag::Development => Self::Development,
DatabaseEnvironmentTag::Testing => Self::Testing,
DatabaseEnvironmentTag::Production => Self::Production,
DatabaseEnvironmentTag::Unassigned => Self::Unassigned,
}
}
}

impl std::fmt::Debug for FavouriteStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FavouriteStore").finish_non_exhaustive()
}
}
48 changes: 47 additions & 1 deletion core/crates/ffi/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ use tablepro_storage::{QueryHistory, StoragePaths};
use uuid::Uuid;

use crate::error::CoreError;
use crate::export::write_whole_file;
use crate::favourites::FavouriteStore;
use crate::library::parse_id;
use crate::runtime::on_runtime;
use crate::saved_queries::SavedQueryStore;
use crate::session::DatabaseSession;

#[derive(uniffi::Object)]
Expand Down Expand Up @@ -60,6 +63,13 @@ pub struct HistoryPageInfo {
pub next: Option<HistoryCursorArg>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
pub enum HistoryExportFormat {
// Each run as it ran, under comments that say where and when.
Sql,
Csv,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Record)]
pub struct HistoryCursorArg {
pub pinned: bool,
Expand Down Expand Up @@ -158,6 +168,42 @@ impl QueryHistoryStore {
})
.await
}

// A pinned run comes first in every search and stays through the store's pruning.
pub async fn set_pinned(&self, id: i64, pinned: bool) -> Result<(), CoreError> {
let history = self.history.clone();
on_runtime(&self.tasks, async move { Ok(history.set_pinned(id, pinned).await?) }).await
}

// The runs `ids` names, pinned first then newest, into the file at `path`, which appears only once whole.
pub async fn export(&self, ids: Vec<i64>, format: HistoryExportFormat, path: String) -> Result<(), CoreError> {
let history = self.history.clone();
let tasks = self.tasks.clone();
on_runtime(&self.tasks, async move {
let text = match format {
HistoryExportFormat::Sql => history.export_sql(&ids).await?,
HistoryExportFormat::Csv => history.export_csv(&ids).await?,
};
tasks
.spawn_blocking_task(move || write_whole_file(&path, text.as_bytes()))
.await
.map_err(|failure| CoreError::failed("ffi.panicked", failure.to_string()))?
})
.await
}

// The same database, so a store that opened serves both.
pub fn saved_queries(&self) -> Arc<SavedQueryStore> {
SavedQueryStore::new(
self.history.saved_queries(),
self.history.saved_query_folders(),
self.tasks.clone(),
)
}

pub fn favourites(&self) -> Arc<FavouriteStore> {
FavouriteStore::new(self.history.favourites(), self.tasks.clone())
}
}

// Without a dialect nobody can say which statements hold a secret, so nothing is kept.
Expand Down Expand Up @@ -268,7 +314,7 @@ fn from_epoch(span: Duration, before: bool) -> SystemTime {
time.unwrap_or(UNIX_EPOCH)
}

fn millis(time: SystemTime) -> i64 {
pub(crate) fn millis(time: SystemTime) -> i64 {
match time.duration_since(UNIX_EPOCH) {
Ok(after) => i64::try_from(after.as_millis()).unwrap_or(i64::MAX),
Err(before) => i64::try_from(before.duration().as_millis()).map_or(i64::MIN, |value| -value),
Expand Down
Loading
Loading