db info tweaks (#5338)

* Rename db info to db schema

* Change db schema to take db as input
This commit is contained in:
Reilly Wood 2022-04-26 16:16:46 -07:00 committed by GitHub
parent 5319544481
commit cd5199de31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 24 deletions

View file

@ -2,9 +2,9 @@ mod collect;
mod command; mod command;
mod describe; mod describe;
mod from; mod from;
mod info;
mod open; mod open;
mod query; mod query;
mod schema;
mod select; mod select;
mod utils; mod utils;
@ -12,10 +12,10 @@ use collect::CollectDb;
use command::Database; use command::Database;
use describe::DescribeDb; use describe::DescribeDb;
use from::FromDb; use from::FromDb;
use info::InfoDb;
use nu_protocol::engine::StateWorkingSet; use nu_protocol::engine::StateWorkingSet;
use open::OpenDb; use open::OpenDb;
use query::QueryDb; use query::QueryDb;
use schema::SchemaDb;
use select::SelectDb; use select::SelectDb;
pub fn add_database_decls(working_set: &mut StateWorkingSet) { pub fn add_database_decls(working_set: &mut StateWorkingSet) {
@ -29,5 +29,5 @@ pub fn add_database_decls(working_set: &mut StateWorkingSet) {
} }
// Series commands // Series commands
bind_command!(CollectDb, Database, DescribeDb, FromDb, QueryDb, SelectDb, OpenDb, InfoDb); bind_command!(CollectDb, Database, DescribeDb, FromDb, QueryDb, SelectDb, OpenDb, SchemaDb);
} }

View file

@ -1,56 +1,51 @@
use super::super::SQLiteDatabase; use super::super::SQLiteDatabase;
use crate::database::values::db_row::DbRow; use crate::database::values::db_row::DbRow;
use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Value, Category, Example, PipelineData, ShellError, Signature, Value,
}; };
use std::path::PathBuf;
#[derive(Clone)] #[derive(Clone)]
pub struct InfoDb; pub struct SchemaDb;
impl Command for InfoDb { impl Command for SchemaDb {
fn name(&self) -> &str { fn name(&self) -> &str {
"db info" "db schema"
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build(self.name()) Signature::build(self.name()).category(Category::Custom("database".into()))
.required("db", SyntaxShape::Filepath, "sqlite database file name")
.category(Category::Custom("database".into()))
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"Show database information." "Show database information, including its schema."
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Show information of a SQLite database", description: "Show the schema of a SQLite database",
example: r#"db info foo.db"#, example: r#"open foo.db | db schema"#,
result: None, result: None,
}] }]
} }
fn search_terms(&self) -> Vec<&str> { fn search_terms(&self) -> Vec<&str> {
vec!["database", "info", "SQLite"] vec!["database", "info", "SQLite", "schema"]
} }
fn run( fn run(
&self, &self,
engine_state: &EngineState, _engine_state: &EngineState,
stack: &mut Stack, _stack: &mut Stack,
call: &Call, call: &Call,
_input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let db_file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let span = db_file.span;
let mut cols = vec![]; let mut cols = vec![];
let mut vals = vec![]; let mut vals = vec![];
let span = call.head;
let sqlite_db = SQLiteDatabase::try_from_path(db_file.item.as_path(), db_file.span)?; let sqlite_db = SQLiteDatabase::try_from_pipeline(input, span)?;
let conn = sqlite_db.open_connection().map_err(|e| { let conn = sqlite_db.open_connection().map_err(|e| {
ShellError::GenericError( ShellError::GenericError(
"Error opening file".into(), "Error opening file".into(),
@ -73,7 +68,7 @@ impl Command for InfoDb {
cols.push("db_filename".into()); cols.push("db_filename".into());
vals.push(Value::String { vals.push(Value::String {
val: db_file.item.to_string_lossy().to_string(), val: sqlite_db.path.to_string_lossy().to_string(),
span, span,
}); });

View file

@ -19,7 +19,7 @@ pub struct SQLiteDatabase {
// I considered storing a SQLite connection here, but decided against it because // I considered storing a SQLite connection here, but decided against it because
// 1) YAGNI, 2) it's not obvious how cloning a connection could work, 3) state // 1) YAGNI, 2) it's not obvious how cloning a connection could work, 3) state
// management gets tricky quick. Revisit this approach if we find a compelling use case. // management gets tricky quick. Revisit this approach if we find a compelling use case.
path: PathBuf, pub path: PathBuf,
pub query: Option<Query>, pub query: Option<Query>,
} }