Skip to content

Commit

Permalink
1.0.203
Browse files Browse the repository at this point in the history
  • Loading branch information
yy0931 committed Feb 21, 2025
1 parent ab37cc4 commit 2adf816
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlite3-editor"
version = "1.0.202"
version = "1.0.203"
edition = "2021"

[features]
Expand Down
95 changes: 72 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{
fs::File,
io::{BufRead, Read, Seek, SeekFrom, Write},
path::PathBuf,
str::FromStr,
sync::{Arc, Mutex},
};
mod columnar_buffer;
Expand Down Expand Up @@ -80,27 +79,36 @@ struct Args {
command: Commands,
}

#[derive(clap::ValueEnum, Clone, Debug, PartialEq, Eq)]
#[derive(ts_rs::TS, clap::ValueEnum, Clone, Debug, PartialEq, Eq)]
#[ts(export)]
pub enum ImportFormat {
#[clap(name = "csv")]
#[ts(rename = "csv")]
CSV,
#[clap(name = "tsv")]
#[ts(rename = "tsv")]
TSV,
#[clap(name = "json")]
#[ts(rename = "json")]
JSON,
}

#[derive(clap::ValueEnum, Clone, Debug, PartialEq, Eq)]
#[derive(ts_rs::TS, clap::ValueEnum, Clone, Debug, PartialEq, Eq)]
#[ts(export)]
pub enum ExportFormat {
#[clap(name = "csv")]
#[ts(rename = "csv")]
CSV,
#[clap(name = "json")]
#[ts(rename = "json")]
JSON,
#[clap(name = "xlsx")]
#[ts(rename = "xlsx")]
XLSX,
}

#[derive(Subcommand)]
#[derive(ts_rs::TS, Subcommand)]
#[ts(export, rename_all = "kebab-case")]
enum Commands {
Version {},
FunctionList {},
Expand Down Expand Up @@ -158,6 +166,13 @@ enum Commands {
#[arg(long)]
sql_cipher_key: Option<String>,
},
CopyFile {
#[arg(long, required = true)]
src: PathBuf,

#[arg(long, required = true)]
dst: PathBuf,
},
}

/// Structure representing a database query
Expand Down Expand Up @@ -191,35 +206,22 @@ impl From<(String, i64, i64)> for CompletionQuery {
}
}

#[derive(Debug, PartialEq, Eq)]
#[derive(ts_rs::TS, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[ts(export, rename_all = "snake_case")]
enum ServerCommand {
Interrupt,
Close,
TryReconnect,
DisconnectTemporarily,
Resume,
Handle,
SemanticHighlight,
CodeLens,
CheckSyntax,
Completion,
}

impl FromStr for ServerCommand {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s.trim() {
"interrupt" => Self::Interrupt,
"close" => Self::Close,
"try_reconnect" => Self::TryReconnect,
"handle" => Self::Handle,
"semantic_highlight" => Self::SemanticHighlight,
"code_lens" => Self::CodeLens,
"check_syntax" => Self::CheckSyntax,
"completion" => Self::Completion,
_ => return Err(()),
})
}
}

fn cli<F, I, O, E>(args: Args, stdin: F, mut stdout: &mut O, mut stderr: &mut E) -> i32
where
F: Fn() -> I + std::marker::Send + 'static,
Expand Down Expand Up @@ -299,6 +301,7 @@ where
};

let (command_sender, command_receiver) = std::sync::mpsc::channel::<ServerCommand>();
let (resume_command_sender, resume_command_receiver) = std::sync::mpsc::channel::<()>();
let interrupt_handle = Arc::new(Mutex::new(db.get_interrupt_handle()));
let _thread = {
let interrupt_handle = Arc::clone(&interrupt_handle);
Expand All @@ -311,10 +314,16 @@ where
Ok(0) => return,
_ => {}
}
match command_str.parse::<ServerCommand>() {

match ServerCommand::deserialize(
serde::de::value::StrDeserializer::<serde::de::value::Error>::new(command_str.trim()),
) {
Ok(ServerCommand::Interrupt) => {
interrupt_handle.lock().unwrap().interrupt();
}
Ok(ServerCommand::Resume) => {
resume_command_sender.send(()).unwrap();
}
Ok(command) => {
if command_sender.send(command).is_err() {
return;
Expand Down Expand Up @@ -388,6 +397,30 @@ where
}
}

ServerCommand::DisconnectTemporarily => {
drop(db);

if resume_command_receiver.recv().is_err() {
return 0;
}

match sqlite3::SQLite3::connect(&database_filepath, READ_ONLY, &sql_cipher_key) {
Ok(new_db) => {
db = new_db;

*interrupt_handle.lock().unwrap() = db.get_interrupt_handle();
write_named(&mut w, &None::<&i64>).expect("Failed to write the result.");
finish(&mut stdout, &mut w, error::ErrorCode::Success);
}
Err(err) => {
w.truncate_all();
write!(w, "{err}").expect("Failed to write an error message.");
finish(&mut stdout, &mut w, err.code());
return 1;
}
}
}

// Handle the request
ServerCommand::Handle => {
// Deserialize the request
Expand Down Expand Up @@ -569,6 +602,22 @@ where
return 1;
}
}
Commands::CopyFile { src, dst } => {
// Read
let data = match std::fs::read(&src) {
Err(err) => {
writeln!(&mut stderr, "Failed to read {src:?}: {err}").unwrap();
return 1;
}
Ok(data) => data,
};

// Write
if let Err(err) = std::fs::write(&dst, data) {
writeln!(&mut stderr, "Failed to write {src:?}: {err}").unwrap();
return 1;
}
}
}

0
Expand Down

0 comments on commit 2adf816

Please sign in to comment.