2022-04-24 11:29:21 +02:00
|
|
|
use super::super::SQLiteDatabase;
|
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::{
|
|
|
|
ast::Call,
|
|
|
|
engine::{Command, EngineState, Stack},
|
|
|
|
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Spanned, SyntaxShape,
|
2022-06-21 19:14:29 +02:00
|
|
|
Type,
|
2022-04-24 11:29:21 +02:00
|
|
|
};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct OpenDb;
|
|
|
|
|
|
|
|
impl Command for OpenDb {
|
|
|
|
fn name(&self) -> &str {
|
2022-06-21 19:14:29 +02:00
|
|
|
"open-db"
|
2022-04-24 11:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build(self.name())
|
|
|
|
.required("query", SyntaxShape::Filepath, "SQLite file to be opened")
|
2022-06-25 23:23:56 +02:00
|
|
|
.input_type(Type::Any)
|
|
|
|
.output_type(Type::Custom("database".into()))
|
2022-04-24 11:29:21 +02:00
|
|
|
.category(Category::Custom("database".into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Open a database"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
|
|
vec!["database", "open"]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
2022-06-21 19:14:29 +02:00
|
|
|
description: "Creates a connection to a sqlite database based on the file name",
|
|
|
|
example: r#"open-db file.sqlite"#,
|
2022-04-24 11:29:21 +02:00
|
|
|
result: None,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
_input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let path: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
|
|
|
|
|
|
|
SQLiteDatabase::try_from_path(path.item.as_path(), path.span)
|
|
|
|
.map(|db| db.into_value(call.head).into_pipeline_data())
|
|
|
|
}
|
|
|
|
}
|