Adding words by parts
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::fs;
|
||||
use crate::dictionary::app_data_dir;
|
||||
use crate::lang::WordOpenMode;
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::fs;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
@@ -12,7 +12,7 @@ pub struct ImportGroup {
|
||||
pub fields: Vec<String>,
|
||||
pub length: u64,
|
||||
pub mapping: HashMap<String, String>,
|
||||
pub imported: bool
|
||||
pub imported: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -68,13 +68,18 @@ pub fn get_words_of_group(connection: &Connection, group_id: u64) -> Vec<ImportN
|
||||
.prepare("select tags, flds from notes where mid == ?1;")
|
||||
.unwrap();
|
||||
|
||||
count_stmt.query_map((group_id as i64, ), |row| {
|
||||
Ok(ImportNote {
|
||||
tags: row.get(0)?,
|
||||
fields: row.get::<usize, String>(1)?
|
||||
.split('')
|
||||
.map(|x| x.to_string())
|
||||
.collect()
|
||||
count_stmt
|
||||
.query_map((group_id as i64,), |row| {
|
||||
Ok(ImportNote {
|
||||
tags: row.get(0)?,
|
||||
fields: row
|
||||
.get::<usize, String>(1)?
|
||||
.split('')
|
||||
.map(|x| x.to_string())
|
||||
.collect(),
|
||||
})
|
||||
})
|
||||
}).unwrap().map(|x| x.unwrap()).collect::<Vec<ImportNote>>()
|
||||
.unwrap()
|
||||
.map(|x| x.unwrap())
|
||||
.collect::<Vec<ImportNote>>()
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
pub(crate) mod card_sets;
|
||||
pub(crate) mod card_stats;
|
||||
pub(crate) mod history;
|
||||
pub(crate) mod import;
|
||||
pub(crate) mod settings;
|
||||
pub(crate) mod sqlite;
|
||||
pub(crate) mod voice;
|
||||
pub(crate) mod words;
|
||||
pub(crate) mod web_api;
|
||||
pub(crate) mod import;
|
||||
pub(crate) mod words;
|
||||
|
||||
@@ -6,7 +6,8 @@ pub fn get_setting(key: String, connection: &Connection) -> Option<String> {
|
||||
.unwrap();
|
||||
let mut iter = stmt.query_map((key,), |row| row.get(0)).unwrap();
|
||||
|
||||
if let Some(row) = iter.next() && let Ok(value) = row
|
||||
if let Some(row) = iter.next()
|
||||
&& let Ok(value) = row
|
||||
{
|
||||
return Some(value);
|
||||
}
|
||||
|
||||
@@ -62,4 +62,4 @@ pub fn default_connection() -> Connection {
|
||||
let connection = Connection::open(db_file).unwrap();
|
||||
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap();
|
||||
connection
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::dictionary::app_data_dir;
|
||||
use std::io;
|
||||
use tokio::fs::{File, OpenOptions};
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use zstd::{Decoder, Encoder, DEFAULT_COMPRESSION_LEVEL};
|
||||
use zstd::{DEFAULT_COMPRESSION_LEVEL, Decoder, Encoder};
|
||||
|
||||
// const API_URL: &str = "http://127.0.0.1:8089/";
|
||||
const API_URL: &str = "https://learning.micialware.ru/";
|
||||
@@ -17,7 +17,15 @@ pub async fn send_data(id: String) {
|
||||
let id_url = format!("{api_url}upload/stream/{id}");
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let new_version = client.post(&id_url).body(data).send().await.unwrap().text().await.unwrap();
|
||||
let new_version = client
|
||||
.post(&id_url)
|
||||
.body(data)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
set_local_version(new_version.parse::<u32>().unwrap()).await;
|
||||
}
|
||||
|
||||
@@ -49,7 +57,14 @@ pub async fn load_data(id: String, temp: bool) {
|
||||
tokio::fs::write(db_file, data).await.unwrap();
|
||||
|
||||
let version_url = format!("{API_URL}{id}/version");
|
||||
let version = client.get(&version_url).send().await.unwrap().text().await.unwrap();
|
||||
let version = client
|
||||
.get(&version_url)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
set_local_version(version.parse::<u32>().unwrap()).await;
|
||||
}
|
||||
|
||||
@@ -90,18 +105,36 @@ pub async fn get_local_version() -> u32 {
|
||||
let file = app_data_dir().join("data.meta");
|
||||
if file.exists() {
|
||||
let mut data = String::new();
|
||||
File::open(file.clone()).await.unwrap().read_to_string(&mut data).await.unwrap();
|
||||
File::open(file.clone())
|
||||
.await
|
||||
.unwrap()
|
||||
.read_to_string(&mut data)
|
||||
.await
|
||||
.unwrap();
|
||||
return data.parse::<u32>().unwrap();
|
||||
}
|
||||
|
||||
let mut file = OpenOptions::new().write(true).create(true).truncate(true).open(file).await.unwrap();
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(file)
|
||||
.await
|
||||
.unwrap();
|
||||
file.write_all("0".as_bytes()).await.unwrap();
|
||||
0
|
||||
}
|
||||
|
||||
pub async fn set_local_version(version: u32) {
|
||||
let file_path = app_data_dir().join("data.meta");
|
||||
let mut file = OpenOptions::new().write(true).create(true).truncate(true).open(file_path).await.unwrap();
|
||||
file.write_all(version.to_string().as_bytes()).await.unwrap();
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(file_path)
|
||||
.await
|
||||
.unwrap();
|
||||
file.write_all(version.to_string().as_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::lang::{WordData, WordGroup};
|
||||
use rusqlite::{params, Connection};
|
||||
use rusqlite::{Connection, params};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn add_word(word: &mut WordData, connection: &Connection) {
|
||||
@@ -23,18 +23,26 @@ pub fn add_word(word: &mut WordData, connection: &Connection) {
|
||||
word.id = index;
|
||||
}
|
||||
|
||||
pub fn add_words(words: &mut[WordData], connection: &mut Connection) {
|
||||
pub fn add_words(words: &mut [WordData], connection: &mut Connection) {
|
||||
let tx = connection.transaction().unwrap();
|
||||
let count = words.len();
|
||||
|
||||
{
|
||||
let mut stmt = tx.prepare(
|
||||
"INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
).unwrap();
|
||||
|
||||
let mut stmt = tx
|
||||
.prepare(
|
||||
"INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
for word in words.iter() {
|
||||
stmt.execute(params![word.key, word.value, word.tags, serde_json::to_string(&word.additional).unwrap(), word.group_id]).unwrap();
|
||||
stmt.execute(params![
|
||||
word.key,
|
||||
word.value,
|
||||
word.tags,
|
||||
serde_json::to_string(&word.additional).unwrap(),
|
||||
word.group_id
|
||||
])
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user