From bb52e3e84f3204cdde5f1d84d76c35c843e4b50b Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sun, 5 Mar 2023 13:25:23 +0200 Subject: [PATCH] finish generate button --- index.html | 4 +-- src/app.rs | 42 +++++++++++++++++++++++------- src/components/generator_picker.rs | 1 - src/generate_sql.rs | 19 +++++++++----- src/main.rs | 9 +++---- 5 files changed, 50 insertions(+), 25 deletions(-) diff --git a/index.html b/index.html index 40e540d..e114bc7 100644 --- a/index.html +++ b/index.html @@ -12,8 +12,6 @@ - + diff --git a/src/app.rs b/src/app.rs index 0e6f606..082140a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,7 +2,7 @@ use std::cell::RefCell; use std::io::Cursor; use std::collections::{HashMap, self}; use std::rc::Rc; -use gloo::console::console_dbg; +use gloo::console::{console_dbg, console}; use gloo::file::callbacks::FileReader; use gloo::file::File; use gloo::storage::{LocalStorage, Storage}; @@ -10,7 +10,7 @@ use web_sys::{DragEvent, Event, FileList, HtmlInputElement, MouseEvent}; use yew::html::TargetCast; use yew::{html, Callback, Component, Context, Html}; -use crate::generate_sql::{SQLValueGuess, generate_table_guessess}; +use crate::generate_sql::{SQLValueGuess, generate_table_guessess, generate_fake_entries}; use crate::magicdraw_parser::{parse_project, SQLTableCollection, SQLTable}; use crate::components::sql_column_info::SQLTableColumnInfo; @@ -27,6 +27,7 @@ pub enum Msg { ShowPrevTable, AllGoodConfirmation, GenerateSQL, + UpdateRowsPerTable(u32), } pub struct App { @@ -35,7 +36,8 @@ pub struct App { current_guessess: Vec>>>, currently_shown_table: usize, all_good_confirmed: bool, - generated_sql: Option + generated_sql: Option, + rows_per_table: u32 } impl Component for App { @@ -60,7 +62,8 @@ impl Component for App { currently_shown_table: 0, all_good_confirmed: true, // TODO: make this false, by default generated_sql: None, - current_guessess + current_guessess, + rows_per_table: DEFAULT_ROWS_PER_TABLE } } @@ -75,6 +78,7 @@ impl Component for App { let msg = Self::update_current_collection(Some(collections.remove(0))); ctx.link().send_message(msg); } + // TODO: show error message } self.active_readers.remove(&file_name); @@ -88,6 +92,7 @@ impl Component for App { let file_name = file_name.clone(); gloo::file::callbacks::read_as_bytes(&file, move |res| { + // TODO: show error message link.send_message(Msg::Loaded( file_name, res.expect("failed to read file"), @@ -137,15 +142,26 @@ impl Component for App { true }, Msg::UpdateGenarator(column, generator) => { - console_dbg!(column, generator); let mut guessess = self.current_guessess[self.currently_shown_table].borrow_mut(); let entry = guessess.get_mut(&column).unwrap(); *entry = generator; true }, Msg::GenerateSQL => { - false + let tables = self.current_collection.as_ref().unwrap(); + let guessess = self.current_guessess.iter().map(|v| v.borrow()).collect(); + // TODO: show error message + if let Ok(result) = generate_fake_entries(tables, &guessess, self.rows_per_table) { + self.generated_sql = Some(result) + } else { + self.generated_sql = None + } + true }, + Msg::UpdateRowsPerTable(rows_per_table) => { + self.rows_per_table = rows_per_table; + false + } } } @@ -249,6 +265,12 @@ impl App { } fn show_step3(&self, ctx: &Context) -> Html { + let on_rows_changed = ctx.link().callback(|e: Event| { + let value_str = e.target_unchecked_into::().value(); + let value = value_str.parse().unwrap_or(DEFAULT_ROWS_PER_TABLE); + Msg::UpdateRowsPerTable(value) + }); + html! {

{ "3. Final settings" }

@@ -258,12 +280,14 @@ impl App { @@ -276,9 +300,9 @@ impl App { html! {

{ "4. Copy & Paste" }

- +
 					{ sql }
-				
+				
} } diff --git a/src/components/generator_picker.rs b/src/components/generator_picker.rs index 322c2a1..1e64f7f 100644 --- a/src/components/generator_picker.rs +++ b/src/components/generator_picker.rs @@ -1,6 +1,5 @@ use std::{collections::HashMap, str::FromStr}; -use gloo::console::console_dbg; use yew::{Html, html, Callback, TargetCast, AttrValue}; use web_sys::{Event, HtmlInputElement}; diff --git a/src/generate_sql.rs b/src/generate_sql.rs index da3f5c8..d9dabd8 100644 --- a/src/generate_sql.rs +++ b/src/generate_sql.rs @@ -1,6 +1,7 @@ -use std::{rc::Rc, collections::{HashSet, HashMap}}; +use std::{rc::Rc, collections::{HashSet, HashMap}, cell::Ref}; use anyhow::{Result, bail}; +use gloo::console::console_dbg; use rand::{seq::SliceRandom, Rng, rngs::ThreadRng}; use chrono::{Local, NaiveDateTime, Days}; use fake::{faker::{lorem::en::*, name::en::{FirstName, LastName, Name}, phone_number::en::PhoneNumber, internet::en::{DomainSuffix, FreeEmail}, company::en::BsNoun, address::{en::{CityName, StreetName}}}, Fake}; @@ -58,7 +59,7 @@ pub enum SQLValueGuess { // TODO: Check primary key constraint pub fn generate_fake_entries( tables: &[Rc], - value_guessess: &Vec>, + value_guessess: &Vec>>, rows_per_table: u32 ) -> Result { let mut lines = vec![]; @@ -97,13 +98,17 @@ pub fn generate_fake_entries( let entries = &mut all_entries[table_idx]; for column in &table.columns { - let mut auto_increment_counter = 0; - let value_guess = value_guessess[table_idx].get(column.name.as_str()).expect("Failed to get column guess"); - for entry_idx in 0..(rows_per_table as usize) { - if let Some(_) = &column.foreign_key { + if column.foreign_key.is_some() { + for entry_idx in 0..(rows_per_table as usize) { entries_with_foreign_keys.insert((table_idx, entry_idx)); entries[entry_idx].push("".into()); - } else { + } + } else { + let mut auto_increment_counter = 0; + let value_guess = value_guessess[table_idx] + .get(column.name.as_str()) + .expect("Failed to get column guess"); + for entry_idx in 0..(rows_per_table as usize) { entries[entry_idx].push(generate_value(&mut rng, &value_guess, &mut auto_increment_counter)); } } diff --git a/src/main.rs b/src/main.rs index 09b6b63..55a9758 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use anyhow::Result; + use app::App; mod magicdraw_parser; @@ -10,10 +12,7 @@ mod generate_sql; // TODO: Fix double rebuilding when on "trunk server". uno css triggers second build. // TODO: Add simple versioning in frontend for data -fn main() { - // let f = File::open("example.mdzip").unwrap(); - // let collections = parse_project(f)?; - // dbg!(collections); - +fn main() -> Result<()> { yew::Renderer::::new().render(); + Ok(()) }