Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show total row counts #55

Merged
merged 1 commit into from
Jun 17, 2024
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
22 changes: 22 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,23 @@ impl App {
orders,
)
.await?;
let total_row_count = self
.pool
.as_ref()
.unwrap()
.get_total_row_count(
&database,
&table,
if self.record_table.filter.input_str().is_empty() {
None
} else {
Some(self.record_table.filter.input_str())
},
)
.await?;
self.record_table.update(
records,
Some(total_row_count),
self.concat_headers(headers, header_icons),
database.clone(),
table.clone(),
Expand Down Expand Up @@ -246,8 +261,15 @@ impl App {
.unwrap()
.get_records(&database, &table, 0, None, None)
.await?;
let total_row_count = self
.pool
.as_ref()
.unwrap()
.get_total_row_count(&database, &table, None)
.await?;
self.record_table.update(
records,
Some(total_row_count),
headers,
database.clone(),
table.clone(),
Expand Down
4 changes: 4 additions & 0 deletions src/components/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl PropertiesComponent {
.iter()
.map(|c| c.columns())
.collect::<Vec<Vec<String>>>(),
None,
columns.first().unwrap().fields(),
database.clone(),
table.clone(),
Expand All @@ -87,6 +88,7 @@ impl PropertiesComponent {
.iter()
.map(|c| c.columns())
.collect::<Vec<Vec<String>>>(),
None,
constraints.first().unwrap().fields(),
database.clone(),
table.clone(),
Expand All @@ -101,6 +103,7 @@ impl PropertiesComponent {
.iter()
.map(|c| c.columns())
.collect::<Vec<Vec<String>>>(),
None,
foreign_keys.first().unwrap().fields(),
database.clone(),
table.clone(),
Expand All @@ -115,6 +118,7 @@ impl PropertiesComponent {
.iter()
.map(|c| c.columns())
.collect::<Vec<Vec<String>>>(),
None,
indexes.first().unwrap().fields(),
database.clone(),
table.clone(),
Expand Down
11 changes: 9 additions & 2 deletions src/components/record_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ impl RecordTableComponent {
pub fn update(
&mut self,
rows: Vec<Vec<String>>,
total_row_count: Option<usize>,
headers: Vec<String>,
database: Database,
table: DTable,
hold_cursor_position: bool,
) {
self.table
.update(rows, headers, database, table.clone(), hold_cursor_position);
self.table.update(
rows,
total_row_count,
headers,
database,
table.clone(),
hold_cursor_position,
);
self.filter.table = Some(table);
}

Expand Down
4 changes: 3 additions & 1 deletion src/components/sql_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ impl Component for SqlEditorComponent {
database,
table,
} => {
self.table.update(rows, headers, database, table, false);
let count = Some(rows.len());
self.table
.update(rows, count, headers, database, table, false);
self.focus = Focus::Table;
self.query_result = None;
}
Expand Down
5 changes: 5 additions & 0 deletions src/components/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl OrderManager {
pub struct TableComponent {
pub headers: Vec<String>,
pub rows: Vec<Vec<String>>,
pub total_row_count: Option<usize>,
pub eod: bool,
pub selected_row: TableState,
orders: OrderManager,
Expand All @@ -117,6 +118,7 @@ impl TableComponent {
selected_row: TableState::default(),
headers: vec![],
rows: vec![],
total_row_count: None,
orders: OrderManager::new(),
table: None,
selected_column: 0,
Expand All @@ -137,6 +139,7 @@ impl TableComponent {
pub fn update(
&mut self,
rows: Vec<Vec<String>>,
total_row_count: Option<usize>,
headers: Vec<String>,
database: Database,
table: DTable,
Expand All @@ -148,6 +151,7 @@ impl TableComponent {
}
self.headers = headers;
self.rows = rows;
self.total_row_count = total_row_count;
self.selected_column = if hold_cusor_position {
self.selected_column
} else {
Expand Down Expand Up @@ -627,6 +631,7 @@ impl StatefulDrawableComponent for TableComponent {
} else {
Some(self.rows.len())
},
self.total_row_count,
if self.headers.is_empty() {
None
} else {
Expand Down
10 changes: 8 additions & 2 deletions src/components/table_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ use ratatui::{
pub struct TableStatusComponent {
column_count: Option<usize>,
row_count: Option<usize>,
total_row_count: Option<usize>,
table: Option<Table>,
}

impl Default for TableStatusComponent {
fn default() -> Self {
Self {
row_count: None,
total_row_count: None,
column_count: None,
table: None,
}
Expand All @@ -30,11 +32,13 @@ impl Default for TableStatusComponent {
impl TableStatusComponent {
pub fn new(
row_count: Option<usize>,
total_row_count: Option<usize>,
column_count: Option<usize>,
table: Option<Table>,
) -> Self {
Self {
row_count,
total_row_count,
column_count,
table,
}
Expand All @@ -45,8 +49,10 @@ impl DrawableComponent for TableStatusComponent {
fn draw(&self, f: &mut Frame, area: Rect, focused: bool) -> Result<()> {
let status = Paragraph::new(Line::from(vec![
Span::from(format!(
"rows: {}, ",
self.row_count.map_or("-".to_string(), |c| c.to_string())
"rows: {} / {}, ",
self.row_count.map_or("-".to_string(), |c| c.to_string()),
self.total_row_count
.map_or("-".to_string(), |c| c.to_string()),
)),
Span::from(format!(
"columns: {}, ",
Expand Down
6 changes: 6 additions & 0 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ pub trait Pool: Send + Sync {
database: &Database,
table: &Table,
) -> anyhow::Result<Vec<Box<dyn TableRow>>>;
async fn get_total_row_count(
&self,
database: &Database,
table: &Table,
filter: Option<String>,
) -> anyhow::Result<usize>;
async fn get_constraints(
&self,
database: &Database,
Expand Down
24 changes: 24 additions & 0 deletions src/database/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,30 @@ impl Pool for MySqlPool {
Ok((headers, records))
}

async fn get_total_row_count(
&self,
database: &Database,
table: &Table,
filter: Option<String>,
) -> anyhow::Result<usize> {
let query = if let Some(filter) = &filter {
format!(
"SELECT COUNT(*) FROM `{database}`.`{table}` WHERE {filter}",
database = database.name,
table = table.name,
filter = filter
)
} else {
format!(
"SELECT COUNT(*) FROM `{database}`.`{table}`",
database = database.name,
table = table.name,
)
};
let res = sqlx::query(query.as_str()).fetch_one(&self.pool).await?;
Ok(res.get::<i64, usize>(0) as usize)
}

async fn get_columns(
&self,
database: &Database,
Expand Down
26 changes: 26 additions & 0 deletions src/database/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,32 @@ impl Pool for PostgresPool {
Ok((headers, records))
}

async fn get_total_row_count(
&self,
database: &Database,
table: &Table,
filter: Option<String>,
) -> anyhow::Result<usize> {
let query = if let Some(filter) = &filter {
format!(
r#"SELECT COUNT(*) FROM "{database}"."{table_schema}"."{table}" WHERE {filter}"#,
database = database.name,
table = table.name,
filter = filter,
table_schema = table.schema.clone().unwrap_or_else(|| "public".to_string()),
)
} else {
format!(
r#"SELECT COUNT(*) FROM "{database}"."{table_schema}"."{table}"#,
database = database.name,
table = table.name,
table_schema = table.schema.clone().unwrap_or_else(|| "public".to_string()),
)
};
let res = sqlx::query(query.as_str()).fetch_one(&self.pool).await?;
Ok(res.get::<i64, usize>(0) as usize)
}

async fn get_columns(
&self,
database: &Database,
Expand Down
19 changes: 19 additions & 0 deletions src/database/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,25 @@ impl Pool for SqlitePool {
Ok((headers, records))
}

async fn get_total_row_count(
&self,
_database: &Database,
table: &Table,
filter: Option<String>,
) -> anyhow::Result<usize> {
let query = if let Some(filter) = &filter {
format!(
"SELECT COUNT(*) FROM `{table}` WHERE {filter}",
table = table.name,
filter = filter,
)
} else {
format!("SELECT COUNT(*) FROM `{table}`", table = table.name,)
};
let res = sqlx::query(query.as_str()).fetch_one(&self.pool).await?;
Ok(res.get::<i64, usize>(0) as usize)
}

async fn get_columns(
&self,
_database: &Database,
Expand Down
Loading