Skip to main content

nu_command/stor/
open.rs

1use crate::database::{MEMORY_DB, SQLiteDatabase};
2use nu_engine::command_prelude::*;
3use nu_protocol::Signals;
4
5#[derive(Clone)]
6pub struct StorOpen;
7
8impl Command for StorOpen {
9    fn name(&self) -> &str {
10        "stor open"
11    }
12
13    fn signature(&self) -> Signature {
14        Signature::build("stor open")
15            .input_output_types(vec![(
16                Type::Nothing,
17                Type::Custom("sqlite-in-memory".into()),
18            )])
19            .allow_variants_without_examples(true)
20            .category(Category::Database)
21    }
22
23    fn description(&self) -> &str {
24        "Opens the in-memory sqlite database."
25    }
26
27    fn search_terms(&self) -> Vec<&str> {
28        vec!["sqlite", "storing", "access"]
29    }
30
31    fn examples(&self) -> Vec<Example<'_>> {
32        vec![Example {
33            description: "Open the in-memory sqlite database",
34            example: "stor open",
35            result: None,
36        }]
37    }
38
39    fn run(
40        &self,
41        _engine_state: &EngineState,
42        _stack: &mut Stack,
43        call: &Call,
44        _input: PipelineData,
45    ) -> Result<PipelineData, ShellError> {
46        // eprintln!("Initializing nudb");
47        // eprintln!("Here's some things to try:");
48        // eprintln!("* stor open | schema | table -e");
49        // eprintln!("* stor open | query db 'insert into nudb (bool1,int1,float1,str1,datetime1) values (2,200,2.0,'str2','1969-04-17T06:00:00-05:00')'");
50        // eprintln!("* stor open | query db 'select * from nudb'");
51        // eprintln!("Now imagine all those examples happening as commands, without sql, in our normal nushell pipelines\n");
52
53        // TODO: Think about adding the following functionality
54        // * stor open --table-name my_table_name
55        //   It returns the output of `select * from my_table_name`
56
57        // Just create an empty database with MEMORY_DB and nothing else
58        let db = Box::new(SQLiteDatabase::new(
59            std::path::Path::new(MEMORY_DB),
60            Signals::empty(),
61        ));
62
63        // dbg!(db.clone());
64        Ok(db.into_value(call.head).into_pipeline_data())
65    }
66}
67
68#[cfg(test)]
69mod test {
70    use super::*;
71
72    #[test]
73    fn test_examples() -> nu_test_support::Result {
74        nu_test_support::test().examples(StorOpen)
75    }
76}