Change way types are inserted

This commit is contained in:
Augusto Gunsch 2022-01-10 13:24:55 -03:00
parent c2cb1f861d
commit 4e504997da
3 changed files with 43 additions and 43 deletions

View File

@ -1,6 +1,7 @@
use std::fs; use std::fs;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::process::exit; use std::process::exit;
use std::collections::HashSet;
use reqwest; use reqwest;
use rusqlite::{Connection, Transaction, ErrorCode}; use rusqlite::{Connection, Transaction, ErrorCode};
@ -72,14 +73,6 @@ impl WordDb {
name TINYTEXT UNIQUE NOT NULL name TINYTEXT UNIQUE NOT NULL
)", &lang.code), []).unwrap(); )", &lang.code), []).unwrap();
for type_ in &lang.types {
transaction.execute(&format!("
INSERT INTO {0}_types ( name )
VALUES (
?
)", &lang.code), [type_]).unwrap();
}
transaction.execute(&format!(" transaction.execute(&format!("
CREATE TABLE {0}_words ( CREATE TABLE {0}_words (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -95,11 +88,6 @@ impl WordDb {
ON {0}_words (word) ON {0}_words (word)
", &lang.code), []).unwrap(); ", &lang.code), []).unwrap();
transaction.execute("
INSERT INTO langs (code, name, major, minor, patch)
VALUES (?, ?, ?, ?, ?)
", params![&lang.code, &lang.name, MAJOR, MINOR, PATCH]).unwrap();
transaction.commit().unwrap(); transaction.commit().unwrap();
} }
@ -216,6 +204,37 @@ impl WordDb {
} }
} }
fn insert_types(&mut self, lang: &Language, entries: &WiktionaryEntries) {
let mut conn = self.connect();
let transaction = conn.transaction().unwrap();
let mut types = HashSet::new();
for entry in entries.iter() {
types.insert(&entry.type_);
}
for type_ in types {
transaction.execute(&format!("
INSERT INTO {0}_types ( name )
VALUES (?)", &lang.code), [type_]).unwrap();
}
transaction.commit().unwrap();
}
fn insert_version(&mut self, lang: &Language) {
let mut conn = self.connect();
let transaction = conn.transaction().unwrap();
transaction.execute("
INSERT INTO langs (code, name, major, minor, patch)
VALUES (?, ?, ?, ?, ?)
", params![&lang.code, &lang.name, MAJOR, MINOR, PATCH]).unwrap();
transaction.commit().unwrap();
}
pub async fn upgrade_lang(&mut self, lang: &Language) { pub async fn upgrade_lang(&mut self, lang: &Language) {
self.try_create_dir(DB_DIR); self.try_create_dir(DB_DIR);
@ -251,12 +270,18 @@ impl WordDb {
println!("Parsing data..."); println!("Parsing data...");
let entries = WiktionaryEntries::parse_data(data); let entries = WiktionaryEntries::parse_data(data);
println!("Inserting data..."); println!("Inserting types...");
self.insert_types(lang, &entries);
println!("Inserting entries...");
self.insert_entries(lang, &entries); self.insert_entries(lang, &entries);
println!("Generating \"form-of\" entries..."); println!("Generating \"form-of\" entries...");
self.generate_entries(lang, &entries); self.generate_entries(lang, &entries);
println!("Inserting version...");
self.insert_version(lang);
println!("Done"); println!("Done");
} }
} }

View File

@ -1,16 +1,14 @@
#[derive(Debug)] #[derive(Debug)]
pub struct Language { pub struct Language {
pub code: String, pub code: String,
pub name: String, pub name: String
pub types: Vec<String>
} }
impl Language { impl Language {
pub fn new(code: &str, name: &str, types: Vec<String>) -> Self { pub fn new(code: &str, name: &str) -> Self {
Self { Self {
code: String::from(code), code: String::from(code),
name: String::from(name), name: String::from(name)
types
} }
} }
} }

View File

@ -56,30 +56,7 @@ async fn main() {
let mut db = WordDb::new("inflectived.db"); let mut db = WordDb::new("inflectived.db");
let lang = Language::new("pl", let lang = Language::new("pl", "Polish");
"Polish",
vec![String::from("adj"),
String::from("noun"),
String::from("verb"),
String::from("character"),
String::from("suffix"),
String::from("prefix"),
String::from("conj"),
String::from("adv"),
String::from("infix"),
String::from("name"),
String::from("phrase"),
String::from("prep_phrase"),
String::from("intj"),
String::from("det"),
String::from("prep"),
String::from("proverb"),
String::from("abbrev"),
String::from("num"),
String::from("pron"),
String::from("punct"),
String::from("interfix"),
String::from("particle")]);
match matches.subcommand() { match matches.subcommand() {
("upgrade", _) => { db.upgrade_lang(&lang).await; }, ("upgrade", _) => { db.upgrade_lang(&lang).await; },