Skip to main content

nu_command/stor/
open.rs

1use crate::database::{MEMORY_DB, SQLiteDatabase};
2use nu_engine::command_prelude::*;
3
4#[derive(Clone)]
5pub struct StorOpen;
6
7impl Command for StorOpen {
8    fn name(&self) -> &str {
9        "stor open"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build("stor open")
14            .input_output_types(vec![(Type::Nothing, Type::Custom("SQLiteDatabase".into()))])
15            .allow_variants_without_examples(true)
16            .category(Category::Database)
17    }
18
19    fn description(&self) -> &str {
20        "Opens the in-memory sqlite database."
21    }
22
23    fn search_terms(&self) -> Vec<&str> {
24        vec!["sqlite", "storing", "access"]
25    }
26
27    fn examples(&self) -> Vec<Example<'_>> {
28        vec![Example {
29            description: "Open the in-memory sqlite database",
30            example: "stor open",
31            result: None,
32        }]
33    }
34
35    fn run(
36        &self,
37        engine_state: &EngineState,
38        _stack: &mut Stack,
39        call: &Call,
40        _input: PipelineData,
41    ) -> Result<PipelineData, ShellError> {
42        // TODO: Think about adding the following functionality
43        // * stor open --table-name my_table_name
44        //   It returns the output of `select * from my_table_name`
45
46        // Just create an empty database with MEMORY_DB and nothing else
47        let db = Box::new(SQLiteDatabase::new(
48            std::path::Path::new(MEMORY_DB),
49            engine_state.signals().clone(),
50        ));
51
52        Ok(db.into_value(call.head).into_pipeline_data())
53    }
54}
55
56#[cfg(test)]
57mod test {
58    use super::*;
59    use nu_test_support::Result;
60    use nu_test_support::prelude::*;
61
62    #[test]
63    fn test_examples() -> Result {
64        test().examples(StorOpen)
65    }
66
67    #[test]
68    #[exp(nu_experimental::ENFORCE_RUNTIME_ANNOTATIONS)]
69    fn correct_return_ty() -> Result {
70        let () = test().run("let db = stor open")?;
71        Ok(())
72    }
73}