nu_command/stor/
export.rs1use crate::database::{MEMORY_DB, SQLiteDatabase, get_shared_mem_conn};
2use nu_engine::command_prelude::*;
3use nu_protocol::shell_error::generic::GenericError;
4
5#[derive(Clone)]
6pub struct StorExport;
7
8impl Command for StorExport {
9 fn name(&self) -> &str {
10 "stor export"
11 }
12
13 fn signature(&self) -> Signature {
14 Signature::build("stor export")
15 .input_output_types(vec![(Type::Nothing, Type::table())])
16 .required_named(
17 "file-name",
18 SyntaxShape::String,
19 "File name to export the sqlite in-memory database to.",
20 Some('f'),
21 )
22 .allow_variants_without_examples(true)
23 .category(Category::Database)
24 }
25
26 fn description(&self) -> &str {
27 "Export the in-memory sqlite database to a sqlite database file."
28 }
29
30 fn search_terms(&self) -> Vec<&str> {
31 vec!["sqlite", "save", "database", "saving", "file"]
32 }
33
34 fn examples(&self) -> Vec<Example<'_>> {
35 vec![Example {
36 description: "Export the in-memory sqlite database",
37 example: "stor export --file-name nudb.sqlite",
38 result: None,
39 }]
40 }
41
42 fn run(
43 &self,
44 engine_state: &EngineState,
45 stack: &mut Stack,
46 call: &Call,
47 _input: PipelineData,
48 ) -> Result<PipelineData, ShellError> {
49 let span = call.head;
50 let file_name_opt: Option<String> = call.get_flag(engine_state, stack, "file-name")?;
51 let file_name = match file_name_opt {
52 Some(file_name) => file_name,
53 None => {
54 return Err(ShellError::MissingParameter {
55 param_name: "please supply a file name with the --file-name parameter".into(),
56 span,
57 });
58 }
59 };
60
61 let conn = get_shared_mem_conn()?;
62 let db = Box::new(SQLiteDatabase::new(
63 std::path::Path::new(MEMORY_DB),
64 engine_state.signals().clone(),
65 ));
66 db.export_in_memory_database_to_file(&conn, file_name)
69 .map_err(|err| {
70 ShellError::Generic(GenericError::new_internal(
71 "Failed to open SQLite connection to the in-memory database from export",
72 err.to_string(),
73 ))
74 })?;
75
76 Ok(Value::custom(db, span).into_pipeline_data())
77 }
78}
79
80#[cfg(test)]
81mod test {
82 use super::*;
83
84 #[test]
85 fn test_examples() -> nu_test_support::Result {
86 nu_test_support::test().examples(StorExport)
87 }
88}