Skip to content

Commit

Permalink
Add fn get_tables_with_condition
Browse files Browse the repository at this point in the history
- Update all fn with const
  • Loading branch information
TaQuangKhoi committed Aug 19, 2024
1 parent a607bd0 commit 747bcc4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
68 changes: 44 additions & 24 deletions src/core/get_knowledge.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use egui::TextBuffer;
use rusqlite::{Connection, MappedRows, params, Row};
use crate::core::table::{ExportComplexityType, Table, TableType};

const SQLITE_DATABASE_PATH: &str = "twodb.db";

/// Get all tables from the SQLite database
/// Issue: Long running query
pub fn get_tables() -> Vec<Table> {
let sqlite_conn = Connection::open(SQLITE_DATABASE_PATH).unwrap();
let mut stmt = sqlite_conn.prepare(
"SELECT
const SELECT_PART: &str = "SELECT
id,
name,
table_type,
Expand All @@ -17,8 +12,16 @@ pub fn get_tables() -> Vec<Table> {
export_order,
is_self_referencing,
self_referencing_column,
row_count
FROM tables"
row_count,
is_exported
FROM tables ";

/// Get all tables from the SQLite database
/// Issue: Long running query
pub fn get_tables() -> Vec<Table> {
let sqlite_conn = Connection::open(SQLITE_DATABASE_PATH).unwrap();
let mut stmt = sqlite_conn.prepare(
SELECT_PART
).unwrap();
let tables = stmt.query_map(params![], |row| {
Ok(Table {
Expand All @@ -37,7 +40,7 @@ pub fn get_tables() -> Vec<Table> {
let mut result = Vec::new();
for table in tables {
let mut inner_table = table.unwrap();
inner_table.update_row_count(); // Long running query
inner_table.update_row_count(); // Long-running query
result.push(inner_table);
}
result
Expand All @@ -46,21 +49,9 @@ pub fn get_tables() -> Vec<Table> {
pub fn get_tables_of_database(database_name: &String) -> Vec<Table>
{
let sqlite_conn = Connection::open(SQLITE_DATABASE_PATH).unwrap();
let query = String::from(SELECT_PART) + " WHERE database = ?1";
let mut stmt = sqlite_conn.prepare(
"SELECT
id,
name,
table_type,
export_complexity_type,
database,
export_order,
is_self_referencing,
self_referencing_column,
row_count,
is_exported
FROM tables
WHERE database = ?1
"
&*query
).unwrap();

let tables_iter = stmt.query_map([database_name], |row| {
Expand All @@ -82,4 +73,33 @@ pub fn get_tables_of_database(database_name: &String) -> Vec<Table>
result.push(table.unwrap());
}
result
}

pub fn get_tables_with_condition(database_name: &String, condition: &str) -> Vec<Table>
{
let sqlite_conn = Connection::open(SQLITE_DATABASE_PATH).unwrap();
let query = String::from(SELECT_PART) + condition;
let mut stmt = sqlite_conn.prepare(&*query).unwrap();

println!("Query: {}", query);
let tables_iter = stmt.query_map([], |row| {
println!("Row: {:?}", row);
Ok(Table {
id: row.get(0)?,
name: row.get(1)?,
table_type: TableType::BaseTable,
export_complexity_type: ExportComplexityType::SIMPLE,
database: row.get(4)?,
export_order: row.get(5)?,
is_self_referencing: false,
self_referencing_column: String::from(""),
row_count: row.get("row_count")?,
is_exported: false,
})
}).expect("Error in get_tables_with_condition");
let mut result = Vec::new();
for table in tables_iter {
result.push(table.unwrap());
}
result
}
8 changes: 5 additions & 3 deletions src/menu_bar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use egui::Align2;
use postgres::Row;
use crate::action::move_data::move_one_table;
use crate::action::working_database::get_rows;
use crate::core::get_knowledge::{get_tables, get_tables_of_database};
use crate::core::get_knowledge::{get_tables, get_tables_of_database, get_tables_with_condition};
/// Render the menu bar
use crate::TwoDBApp;
Expand All @@ -24,9 +24,11 @@ impl TwoDBApp {
thread::spawn(move || {
let source_database_name = var("POSTGRES_DB_SOURCE").unwrap_or(String::from(""));

let tables_from_sqlite = get_tables_of_database(&source_database_name);
let tables_from_sqlite = get_tables_with_condition(&source_database_name,
" WHERE database = 'mes' and is_exported = 0 and row_count > 0"
);
for table in tables_from_sqlite {
println!("Found table {:?}", table.name);
move_one_table(table.name);
}

let text = format!("Done Move Tables for {}", source_database_name);
Expand Down

0 comments on commit 747bcc4

Please sign in to comment.