Skip to content

Commit

Permalink
db lab3
Browse files Browse the repository at this point in the history
  • Loading branch information
Tombleron committed Jan 8, 2023
1 parent ddbd24b commit 13d535a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 42 deletions.
6 changes: 5 additions & 1 deletion 7dProg/db/lab3/src/dbs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ pub trait Selectable {
fn select_all(conn: &mut PooledConn) -> Vec<Self::Item>;
}

pub enum TableError {
Dummy
}

pub trait IntoTable {
fn build(ui: &mut Ui) -> Table;
fn display_table(ui: &mut Ui, conn: &mut PooledConn);
fn insert_row(&mut self, ui: &mut Ui, conn: &mut PooledConn);
fn insert_row(&mut self, ui: &mut Ui, conn: &mut PooledConn) -> Result<(), TableError>;
}
10 changes: 7 additions & 3 deletions 7dProg/db/lab3/src/dbs/parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use egui_extras::{Size, Table, TableBuilder};
use mysql::{prelude::*, PooledConn};
// use mysql::*;

use super::{IntoTable, Selectable};
use super::{IntoTable, Selectable, TableError};

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Parts {
part_id: String,
part_name: String,
Expand Down Expand Up @@ -122,7 +122,7 @@ impl IntoTable for Parts {
});
}

fn insert_row(&mut self, ui: &mut Ui, conn: &mut PooledConn) {
fn insert_row(&mut self, ui: &mut Ui, conn: &mut PooledConn) -> Result<(), TableError>{
let mut part_builder = PartsBuilder::new();
ui.horizontal(|ui| {
ui.label("Part Id: ");
Expand Down Expand Up @@ -181,7 +181,11 @@ impl IntoTable for Parts {
value.city
)).unwrap();
*self = Self::default();

return Ok(())
}

Err(TableError::Dummy)
}
}

Expand Down
10 changes: 7 additions & 3 deletions 7dProg/db/lab3/src/dbs/products.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use eframe::{
use egui_extras::{Size, Table, TableBuilder};
use mysql::{prelude::*, PooledConn};

use super::{IntoTable, Selectable};
use super::{IntoTable, Selectable, TableError};

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Products {
product_id: String,
product_name: String,
Expand Down Expand Up @@ -95,7 +95,7 @@ impl IntoTable for Products {
});
}

fn insert_row(&mut self, ui: &mut Ui, conn: &mut PooledConn) {
fn insert_row(&mut self, ui: &mut Ui, conn: &mut PooledConn) -> Result<(), TableError> {
let mut product_builder = ProductsBuilder::new();
ui.horizontal(|ui| {
ui.label("Product Id: ");
Expand Down Expand Up @@ -141,7 +141,11 @@ impl IntoTable for Products {
))
.unwrap();
*self = Self::default();

return Ok(());
}

Err(TableError::Dummy)
}
}

Expand Down
10 changes: 7 additions & 3 deletions 7dProg/db/lab3/src/dbs/supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use eframe::{
use egui_extras::{Size, Table, TableBuilder};
use mysql::{prelude::*, PooledConn};

use super::{parts::Parts, products::Products, vendors::Vendors, IntoTable, Selectable};
use super::{parts::Parts, products::Products, vendors::Vendors, IntoTable, Selectable, TableError};

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Supply {
id: usize,
vendor_id: String,
Expand Down Expand Up @@ -108,7 +108,7 @@ impl IntoTable for Supply {
});
}

fn insert_row(&mut self, ui: &mut Ui, conn: &mut PooledConn) {
fn insert_row(&mut self, ui: &mut Ui, conn: &mut PooledConn) -> Result<(), TableError> {
ui.horizontal(|ui| {
ui.label("Vendor Id: ");
egui::ComboBox::from_id_source(120)
Expand Down Expand Up @@ -171,7 +171,11 @@ impl IntoTable for Supply {
value.count
)).unwrap();
*self = Self::default();

return Ok(())
}

Err(TableError::Dummy)
}
}

Expand Down
10 changes: 7 additions & 3 deletions 7dProg/db/lab3/src/dbs/vendors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use eframe::{
use egui_extras::{Size, Table, TableBuilder};
use mysql::{prelude::*, PooledConn};

use super::{IntoTable, Selectable};
use super::{IntoTable, Selectable, TableError};

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Vendors {
vendor_id: String,
surname: String,
Expand Down Expand Up @@ -104,7 +104,7 @@ impl IntoTable for Vendors {
});
}

fn insert_row(&mut self, ui: &mut Ui, conn: &mut PooledConn) {
fn insert_row(&mut self, ui: &mut Ui, conn: &mut PooledConn) -> Result<(), TableError>{
let mut vend_builder = VendorsBuilder::new();
ui.horizontal(|ui| {
ui.label("Vendor Id: ");
Expand Down Expand Up @@ -157,7 +157,11 @@ impl IntoTable for Vendors {
value.city
)).unwrap();
*self = Self::default();

return Ok(());
}

Err(TableError::Dummy)
}
}

Expand Down
87 changes: 58 additions & 29 deletions 7dProg/db/lab3/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use mysql::*;
use std::fmt::Display;

struct MyApp {
state: States,
state: TableExplorer,
conn: PooledConn,
}

Expand All @@ -31,17 +31,18 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
"lab3",
options,
Box::new(|_cc| {
Box::new(MyApp::new(
States::View(Tables::Supply(Supply::default())),
conn,
))
let a = TableExplorer {
state: States::View(Tables::Supply(Supply::default())),
dialog: false,
};
Box::new(MyApp::new(a, conn))
}),
);

Ok(())
}

#[derive(Debug)]
#[derive(Debug, Clone)]
enum Tables {
Supply(Supply),
Vendors(Vendors),
Expand All @@ -60,23 +61,33 @@ impl Display for Tables {
}
}

#[derive(Debug)]
struct TableExplorer {
state: States,
dialog: bool,
}

#[derive(Debug, Clone)]
enum States {
View(Tables),
Insert(Tables),
Search,
}

impl States {
impl TableExplorer {
pub fn update(&mut self, ctx: &egui::Context, conn: &mut PooledConn) {
// Generic code for all states
match self {
States::View(table) => States::view(ctx, conn, table),
States::Insert(table) => States::insert(ctx, conn, table),
match self.state {
States::View(_) => self.view(ctx, conn),
States::Insert(_) => self.insert(ctx, conn),
States::Search => self.search(ctx, conn),
}
}
fn view(ctx: &egui::Context, conn: &mut PooledConn, table: &Tables) {
fn view(&mut self, ctx: &egui::Context, conn: &mut PooledConn) {
let table = match &self.state {
States::View(table) => table,
_ => unreachable!(),
};

egui::CentralPanel::default().show(ctx, |ui| match table {
Tables::Supply(_) => {
Supply::display_table(ui, conn);
Expand All @@ -93,37 +104,55 @@ impl States {
});
}

fn insert(ctx: &egui::Context, conn: &mut PooledConn, table: &mut Tables) {
egui::CentralPanel::default().show(ctx, |ui| match table {
Tables::Supply(table) => {
table.insert_row(ui, conn);
fn insert(&mut self, ctx: &egui::Context, conn: &mut PooledConn) {
// FIXME: this is ugly
let dialog = match &mut self.state {
States::Insert(ref mut table) => {
egui::CentralPanel::default()
.show(ctx, |ui| match table {
Tables::Supply(ref mut table) => table.insert_row(ui, conn),
Tables::Parts(ref mut table) => table.insert_row(ui, conn),
Tables::Vendors(ref mut table) => table.insert_row(ui, conn),
Tables::Products(ref mut table) => table.insert_row(ui, conn),
})
.inner
}
Tables::Parts(table) => {
table.insert_row(ui, conn);
}
Tables::Vendors(table) => {
table.insert_row(ui, conn);
}
Tables::Products(table) => {
table.insert_row(ui, conn);
}
});
_ => unreachable!(),
};

self.dialog = match dialog {
Ok(_) => true,
Err(_) => false,
} || self.dialog;

if self.dialog {
egui::Window::new("Вставка успешно выполнена.")
.collapsible(false)
.resizable(false)
.show(ctx, |ui| {
ui.horizontal(|ui| {
if ui.button("Ok?").clicked() {
self.dialog = false;
};
});
});
}
}

fn search(&self, ctx: &egui::Context, _conn: &mut PooledConn) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("lab3");
ui.label(format!("State: {:?}", self));
ui.label(format!("State: {:?}", self.state));
});
}
}

impl MyApp {
fn new(state: States, conn: PooledConn) -> Self {
fn new(state: TableExplorer, conn: PooledConn) -> Self {
Self { state, conn }
}
fn change_state(&mut self, state: States) {
self.state = state
self.state.state = state
}
}

Expand Down

0 comments on commit 13d535a

Please sign in to comment.