2019-08-27 21:45:18 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::prelude::*;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 19:52:31 +00:00
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
2019-08-27 21:45:18 +00:00
|
|
|
use rusqlite::{types::ValueRef, Connection, Row, NO_PARAMS};
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
pub struct FromSQLite;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for FromSQLite {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"from-sqlite"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("from-sqlite")
|
|
|
|
}
|
|
|
|
|
2019-08-29 22:52:32 +00:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse binary data as sqlite .db and create table."
|
|
|
|
}
|
2019-08-31 01:30:41 +00:00
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
from_sqlite(args, registry)
|
|
|
|
}
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
2019-08-31 01:30:41 +00:00
|
|
|
|
2019-08-29 22:52:32 +00:00
|
|
|
pub struct FromDB;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for FromDB {
|
2019-08-31 01:30:41 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"from-db"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("from-db")
|
|
|
|
}
|
2019-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse binary data as db and create table."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
from_sqlite(args, registry)
|
|
|
|
}
|
2019-08-31 01:30:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:45:18 +00:00
|
|
|
pub fn convert_sqlite_file_to_nu_value(
|
|
|
|
path: &Path,
|
|
|
|
tag: impl Into<Tag> + Clone,
|
2019-11-21 14:33:14 +00:00
|
|
|
) -> Result<Value, rusqlite::Error> {
|
2019-08-27 21:45:18 +00:00
|
|
|
let conn = Connection::open(path)?;
|
|
|
|
|
|
|
|
let mut meta_out = Vec::new();
|
|
|
|
let mut meta_stmt = conn.prepare("select name from sqlite_master where type='table'")?;
|
|
|
|
let mut meta_rows = meta_stmt.query(NO_PARAMS)?;
|
|
|
|
while let Some(meta_row) = meta_rows.next()? {
|
|
|
|
let table_name: String = meta_row.get(0)?;
|
|
|
|
let mut meta_dict = TaggedDictBuilder::new(tag.clone());
|
|
|
|
let mut out = Vec::new();
|
|
|
|
let mut table_stmt = conn.prepare(&format!("select * from [{}]", table_name))?;
|
|
|
|
let mut table_rows = table_stmt.query(NO_PARAMS)?;
|
|
|
|
while let Some(table_row) = table_rows.next()? {
|
|
|
|
out.push(convert_sqlite_row_to_nu_value(table_row, tag.clone())?)
|
|
|
|
}
|
2019-11-21 14:33:14 +00:00
|
|
|
meta_dict.insert_value(
|
2019-08-27 21:45:18 +00:00
|
|
|
"table_name".to_string(),
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::String(table_name)).into_value(tag.clone()),
|
2019-08-27 21:45:18 +00:00
|
|
|
);
|
2019-11-21 14:33:14 +00:00
|
|
|
meta_dict.insert_value(
|
|
|
|
"table_values",
|
|
|
|
UntaggedValue::Table(out).into_value(tag.clone()),
|
|
|
|
);
|
|
|
|
meta_out.push(meta_dict.into_value());
|
2019-08-27 21:45:18 +00:00
|
|
|
}
|
|
|
|
let tag = tag.into();
|
2019-11-21 14:33:14 +00:00
|
|
|
Ok(UntaggedValue::Table(meta_out).into_value(tag))
|
2019-08-27 21:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn convert_sqlite_row_to_nu_value(
|
|
|
|
row: &Row,
|
|
|
|
tag: impl Into<Tag> + Clone,
|
2019-11-21 14:33:14 +00:00
|
|
|
) -> Result<Value, rusqlite::Error> {
|
2019-08-27 21:45:18 +00:00
|
|
|
let mut collected = TaggedDictBuilder::new(tag.clone());
|
|
|
|
for (i, c) in row.columns().iter().enumerate() {
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(
|
2019-08-27 21:45:18 +00:00
|
|
|
c.name().to_string(),
|
|
|
|
convert_sqlite_value_to_nu_value(row.get_raw(i), tag.clone()),
|
|
|
|
);
|
|
|
|
}
|
2019-12-06 15:28:26 +00:00
|
|
|
Ok(collected.into_value())
|
2019-08-27 21:45:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn convert_sqlite_value_to_nu_value(value: ValueRef, tag: impl Into<Tag> + Clone) -> Value {
|
2019-08-27 21:45:18 +00:00
|
|
|
match value {
|
2019-11-21 14:33:14 +00:00
|
|
|
ValueRef::Null => {
|
|
|
|
UntaggedValue::Primitive(Primitive::String(String::from(""))).into_value(tag)
|
|
|
|
}
|
2019-12-04 19:52:31 +00:00
|
|
|
ValueRef::Integer(i) => UntaggedValue::int(i).into_value(tag),
|
|
|
|
ValueRef::Real(f) => UntaggedValue::decimal(f).into_value(tag),
|
2020-01-02 04:02:46 +00:00
|
|
|
ValueRef::Text(s) => {
|
2019-08-27 21:45:18 +00:00
|
|
|
// this unwrap is safe because we know the ValueRef is Text.
|
2020-01-02 04:02:46 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::String(String::from_utf8_lossy(s).to_string()))
|
2019-11-21 14:33:14 +00:00
|
|
|
.into_value(tag)
|
2019-08-27 21:45:18 +00:00
|
|
|
}
|
2019-12-04 19:52:31 +00:00
|
|
|
ValueRef::Blob(u) => UntaggedValue::binary(u.to_owned()).into_value(tag),
|
2019-08-27 21:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_sqlite_bytes_to_value(
|
|
|
|
mut bytes: Vec<u8>,
|
|
|
|
tag: impl Into<Tag> + Clone,
|
2019-11-21 14:33:14 +00:00
|
|
|
) -> Result<Value, std::io::Error> {
|
2019-08-27 21:45:18 +00:00
|
|
|
// FIXME: should probably write a sqlite virtual filesystem
|
|
|
|
// that will allow us to use bytes as a file to avoid this
|
|
|
|
// write out, but this will require C code. Might be
|
|
|
|
// best done as a PR to rusqlite.
|
|
|
|
let mut tempfile = tempfile::NamedTempFile::new()?;
|
|
|
|
tempfile.write_all(bytes.as_mut_slice())?;
|
|
|
|
match convert_sqlite_file_to_nu_value(tempfile.path(), tag) {
|
|
|
|
Ok(value) => Ok(value),
|
|
|
|
Err(e) => Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_sqlite(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(registry)?;
|
2019-09-14 16:30:24 +00:00
|
|
|
let tag = args.name_tag();
|
2019-08-27 21:45:18 +00:00
|
|
|
let input = args.input;
|
|
|
|
|
2019-09-26 00:22:17 +00:00
|
|
|
let stream = async_stream! {
|
2020-03-06 16:06:39 +00:00
|
|
|
let bytes = input.collect_binary(tag.clone()).await?;
|
|
|
|
match from_sqlite_bytes_to_value(bytes.item, tag.clone()) {
|
|
|
|
Ok(x) => match x {
|
|
|
|
Value { value: UntaggedValue::Table(list), .. } => {
|
|
|
|
for l in list {
|
|
|
|
yield ReturnSuccess::value(l);
|
2019-08-27 21:45:18 +00:00
|
|
|
}
|
2020-03-06 16:06:39 +00:00
|
|
|
}
|
|
|
|
_ => yield ReturnSuccess::value(x),
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
println!("{:?}", err);
|
|
|
|
yield Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Could not parse as SQLite",
|
|
|
|
"input cannot be parsed as SQLite",
|
2019-10-13 04:12:43 +00:00
|
|
|
&tag,
|
2019-08-27 21:45:18 +00:00
|
|
|
"value originates from here",
|
2020-03-06 16:06:39 +00:00
|
|
|
bytes.tag,
|
|
|
|
))
|
2019-08-27 21:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|