use crate::connection::ColumnInfo;
use super::pane_layout::{PaneTree, PaneType};
pub use super::pane_layout::{Pane, PaneDirection};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TableMode {
Normal,
VisualRow,
VisualColumn,
Insert,
}
#[derive(Debug, Clone)]
pub struct LoadedTable {
pub name: String,
pub schema: Vec<ColumnInfo>,
pub headers: Vec<String>,
pub rows: Vec<Vec<String>>,
}
impl LoadedTable {
pub fn new(
name: String,
schema: Vec<ColumnInfo>,
headers: Vec<String>,
rows: Vec<Vec<String>>,
) -> Self {
Self {
name,
schema,
headers,
rows,
}
}
}
#[derive(Debug, Clone)]
pub struct QueryResult {
pub sql: String,
pub headers: Vec<String>,
pub rows: Vec<Vec<String>>,
pub error: Option<String>,
}
#[derive(Debug, Clone)]
pub struct PendingQuery {
pub table: String,
pub filter: Option<String>,
pub sort_col: Option<String>,
pub sort_desc: bool,
pub selected_cols: Option<Vec<String>>,
}
#[derive(Debug, Clone)]
pub struct PendingInsert {
pub cols: Vec<String>,
pub vals: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct PendingCommit {
pub table: String,
pub pk_col: String,
pub updates: Vec<(String, String, String)>, pub deletes: Vec<String>, pub inserts: Vec<PendingInsert>,
}
#[derive(Debug, Clone)]
pub struct Tab {
pub tree: PaneTree,
pub pending_load: Option<PendingQuery>,
pub pending_commit: Option<PendingCommit>,
pub query_results: Vec<QueryResult>,
pub pending_query_exec: Option<String>,
pub query_history: Vec<String>,
pub loading: bool,
pub error: Option<String>,
}
impl Tab {
pub fn new() -> Self {
Self {
tree: PaneTree::new(PaneType::TableList),
pending_load: None,
pending_commit: None,
query_results: Vec::new(),
pending_query_exec: None,
query_history: Vec::new(),
loading: false,
error: None,
}
}
pub fn request_load(&mut self, tables: &[String]) {
if let Some(pane) = self.tree.active() {
if pane.kind == PaneType::TableList || pane.kind == PaneType::SchemaPicker {
if let Some(name) = tables.get(pane.nav_cursor) {
self.pending_load = Some(PendingQuery {
table: name.clone(),
filter: None,
sort_col: None,
sort_desc: false,
selected_cols: None,
});
self.loading = true;
self.error = None;
}
}
}
}
}