implement insert to db

This commit is contained in:
leca 2024-04-05 19:04:46 +03:00
parent 43531a1a12
commit f4cc2f492c
7 changed files with 52 additions and 20 deletions

View File

@ -9,7 +9,8 @@ edition = "2021"
gtk = { version = "0.8.1", package = "gtk4", features = ["v4_12"] }
rand = "0.8.5"
sqlite = "0.34.0"
rusqlite = "0.31.0"
sha256 = "1.5.0"
[build-dependencies]
dirs = "5.0.1"

View File

@ -31,10 +31,10 @@
</object>
</child>
<child>
<object class="GtkEntry" id="translation_entry">
<object class="GtkEntry" id="reading_entry">
<property name="halign">GTK_ALIGN_CENTER</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="placeholder-text">Translation</property>
<property name="placeholder-text">Reading</property>
</object>
</child>
<child>

View File

@ -1,5 +1,8 @@
use sqlite;
use std::{path::PathBuf, process::Command};
use rusqlite::Connection;
use std::{sync::RwLock, path::PathBuf, process::Command};
pub static PROGRAM_HOME_PATH: RwLock<std::string::String> = RwLock::new(String::new());
pub fn init() {
let program_home_path = if cfg!(target_os = "windows") {
@ -19,15 +22,21 @@ pub fn init() {
home
};
let mut p = PROGRAM_HOME_PATH.write().unwrap();
*p = program_home_path.as_path().to_str().unwrap().to_string();
println!("Program's home: {}", program_home_path.as_path().to_str().unwrap());
let connection = sqlite::open(program_home_path.join("db.sqlite")).unwrap();
let connection = Connection::open(program_home_path.join("db.sqlite")).unwrap();
connection.execute("CREATE TABLE IF NOT EXISTS cards (
id INTEGER PRIMARY KEY AUTOINCREMENT,
image_hash CHAR(64),
hieroglyph VARCHAR(2),
translation VARCHAR(128)
)").unwrap();
)",(),).unwrap();
}
pub fn get_program_home_path() -> &'static RwLock<String> {
&PROGRAM_HOME_PATH
}

View File

@ -1,15 +1,16 @@
use rusqlite::Connection;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use crate::ui::cards::new::*;
use crate::widgets::card_entry::CardEntry;
use glib::subclass::InitializingObject;
use gtk::glib::object::ObjectExt;
use gtk::glib::{clone, closure_local};
use gtk::subclass::prelude::*;
use gtk::{glib, Button, CompositeTemplate, ScrolledWindow, SearchEntry, Window};
use gtk::{prelude::*, FileDialog};
use sqlite;
use crate::ui::cards::new::*;
use crate::widgets::card_entry::CardEntry;
use sha256::try_digest;
#[derive(CompositeTemplate, Default)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/cards/edit/ui.xml")]
@ -66,13 +67,24 @@ async fn file_choose_dialog<W: IsA<gtk::Window>>(window: Rc<W>) {
let dialog: FileDialog = gtk::FileDialog::builder().build();
let answer = dialog.open_future(Some(&*window)).await;
let path = answer.unwrap().path().unwrap();
let path = path.as_path().to_str();
let w: &MemoryCardsNewScene = Into::<&Window>::into(window.upcast_ref()).downcast_ref().unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene
w.get_image_widget().set_file(path);
let path: &str = path.as_path().to_str().unwrap();
let w: &MemoryCardsNewScene = Into::<&Window>::into(window.upcast_ref())
.downcast_ref()
.unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene
w.get_image_widget().set_file(Some(path));
let binding = Path::new(path);
let hash = try_digest(binding).unwrap();
let conn = sqlite::open("test.db").unwrap();
w.get_done_button().connect_closure("clicked", false, closure_local!(@strong w => move |_b: &Button| {
let hieroglyph = w.get_hieroglyph_input();
let reading = w.get_reading_input();
let program_home_path = &*crate::db::get_program_home_path().read().unwrap().clone();
let conn = Connection::open(PathBuf::new().join(program_home_path).join("db.sqlite")).unwrap();
conn.execute("CREATE TABLE test(field_one TEXT, field_two INTEGER); INSERT INTO test VALUES ('Suka', 15);").unwrap();
let query = "INSERT INTO cards (image_hash, hieroglyph, translation) VALUES(?1, ?2, ?3)";
conn.execute(query, (&hash, &hieroglyph, &reading)).unwrap();
w.close();
}));
}
impl WidgetImpl for MemoryCardsEditScene {}

View File

@ -12,7 +12,7 @@ pub struct MemoryCardsNewScene {
#[template_child]
pub heiroglyph_entry: TemplateChild<Entry>,
#[template_child]
pub translation_entry: TemplateChild<Entry>,
pub reading_entry: TemplateChild<Entry>,
#[template_child]
pub done_button: TemplateChild<Button>,
}

View File

@ -2,7 +2,7 @@ mod imp;
use glib::Object;
use gtk::{
gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Application, Button, Image
gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Application, Button, Image, prelude::*
};
glib::wrapper! {
@ -24,4 +24,16 @@ impl MemoryCardsNewScene {
pub fn get_image_widget(&self) -> &Image {
self.imp().image.as_ref()
}
pub fn get_hieroglyph_input(&self) -> String {
self.imp().heiroglyph_entry.text().to_string()
}
pub fn get_reading_input(&self) -> String {
self.imp().reading_entry.text().to_string()
}
pub fn get_done_button(&self) -> &Button {
self.imp().done_button.as_ref()
}
}

View File

@ -43,8 +43,6 @@ impl ObjectImpl for GuessingSetupScene {
fn constructed(&self) {
self.parent_constructed();
//TODO: tie switches to settings and make "start" button
let settings: Settings = Settings::new(APP_ID);
let hiragana_enable_ref: &LabledSwitch = self.hiragana_enable.as_ref();