Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 30 additions & 6 deletions src/dictionary/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
str,
};

use log::debug;
use rusqlite::{Connection, Error as RusqliteError, OpenFlags, OptionalExtension, params};

use super::{
Expand Down Expand Up @@ -50,11 +51,26 @@ pub enum SqliteDictionaryError {

impl Display for SqliteDictionaryError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "sqlite error")
match self {
SqliteDictionaryError::SqliteError { source: _ } => {
write!(f, "failed to perform sqlite operation")
}
SqliteDictionaryError::MissingTable { table } => {
write!(f, "sqlite {table} does not exist")
}
SqliteDictionaryError::ReadOnly => write!(f, "sqlite file is readonly"),
}
}
}

impl Error for SqliteDictionaryError {}
impl Error for SqliteDictionaryError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
SqliteDictionaryError::SqliteError { source } => Some(source),
_ => None,
}
}
}

impl From<RusqliteError> for SqliteDictionaryError {
fn from(value: RusqliteError) -> Self {
Expand All @@ -75,11 +91,16 @@ impl SqliteDictionary {
/// TODO: doc
pub fn open<P: AsRef<Path>>(path: P) -> Result<SqliteDictionary, SqliteDictionaryError> {
let path = path.as_ref().to_path_buf();
debug!("open sqlite dictionary at {path:?}");
let mut conn = Connection::open(&path)?;
debug!("initialize dictionary tables");
Self::initialize_tables(&conn)?;
debug!("migrate from userphrase_v1");
Self::migrate_from_userphrase_v1(&mut conn)?;
debug!("ensure tables exist");
Self::ensure_tables(&conn)?;
let info = Self::read_info_v1(&conn)?;
debug!("read dictionary info {info:?}");

Ok(SqliteDictionary {
conn,
Expand Down Expand Up @@ -179,16 +200,19 @@ impl SqliteDictionary {
}

fn migrate_from_userphrase_v1(conn: &mut Connection) -> Result<(), SqliteDictionaryError> {
debug!("query has_userphrase_v1");
let has_userphrase_v1: bool = conn.query_row(
"SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE type='table' AND name='userphrase_v1')",
[],
|row| row.get(0)
)?;
debug!("query migrated");
let migrated: bool = conn.query_row(
"SELECT EXISTS (SELECT 1 FROM migration_v1 WHERE name='migrate_from_userphrase_v1')",
[],
|row| row.get(0),
)?;
debug!("has_userphrase_v1={has_userphrase_v1} migrated={migrated}");
if !has_userphrase_v1 || migrated {
// Don't need to migrate
conn.execute(
Expand Down Expand Up @@ -233,13 +257,13 @@ impl SqliteDictionary {
userphrases.push((
syllables,
row.get(0)?,
row.get(1)?,
row.get(2)?,
row.get(3)?,
row.get(1).unwrap_or(0),
row.get(2).unwrap_or(0),
row.get(3).unwrap_or(0),
));
}
}

debug!("{} phrases loaded", userphrases.len());
let tx = conn.transaction()?;
{
for item in userphrases {
Expand Down
1 change: 1 addition & 0 deletions tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2024"
anyhow = "1.0.0"
chewing = { version = "0.11.0", path = ".." }
clap = { version = "4.4.18", features = ["derive"] }
env_logger.workspace = true

[dependencies.clap_mangen]
version = "0.2.12"
Expand Down
1 change: 1 addition & 0 deletions tools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod info;
mod init_database;

fn main() -> Result<()> {
env_logger::init();
#[cfg(feature = "mangen")]
{
use clap::CommandFactory;
Expand Down
Loading