2019-08-27 21:45:18 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::prelude::*;
|
|
|
|
use hex::encode;
|
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-11-30 00:21:05 +00:00
|
|
|
use nu_protocol::{Dictionary, Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
|
2019-08-27 21:45:18 +00:00
|
|
|
use rusqlite::{Connection, NO_PARAMS};
|
|
|
|
use std::io::Read;
|
|
|
|
|
|
|
|
pub struct ToSQLite;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for ToSQLite {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"to-sqlite"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("to-sqlite")
|
|
|
|
}
|
|
|
|
|
2019-08-29 22:52:32 +00:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Convert table to sqlite .db binary data"
|
|
|
|
}
|
2019-08-31 01:30:41 +00:00
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
to_sqlite(args, registry)
|
|
|
|
}
|
2019-09-04 01:50:23 +00:00
|
|
|
|
|
|
|
fn is_binary(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
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 ToDB;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for ToDB {
|
2019-08-31 01:30:41 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"to-db"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("to-db")
|
|
|
|
}
|
2019-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Convert table to db data"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
to_sqlite(args, registry)
|
|
|
|
}
|
2019-09-04 01:50:23 +00:00
|
|
|
|
|
|
|
fn is_binary(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2019-08-31 01:30:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:45:18 +00:00
|
|
|
fn comma_concat(acc: String, current: String) -> String {
|
|
|
|
if acc == "" {
|
|
|
|
current
|
|
|
|
} else {
|
|
|
|
format!("{}, {}", acc, current)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:28:26 +00:00
|
|
|
fn get_columns(rows: &[Value]) -> Result<String, std::io::Error> {
|
2019-11-21 14:33:14 +00:00
|
|
|
match &rows[0].value {
|
|
|
|
UntaggedValue::Row(d) => Ok(d
|
2019-08-27 21:45:18 +00:00
|
|
|
.entries
|
|
|
|
.iter()
|
|
|
|
.map(|(k, _v)| k.clone())
|
|
|
|
.fold("".to_string(), comma_concat)),
|
|
|
|
_ => Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Could not find table column names",
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nu_value_to_sqlite_string(v: Value) -> String {
|
2019-11-21 14:33:14 +00:00
|
|
|
match &v.value {
|
|
|
|
UntaggedValue::Primitive(p) => match p {
|
2019-08-27 21:45:18 +00:00
|
|
|
Primitive::Nothing => "NULL".into(),
|
|
|
|
Primitive::Int(i) => format!("{}", i),
|
2019-11-17 05:48:48 +00:00
|
|
|
Primitive::Duration(u) => format!("{}", u),
|
2019-08-30 17:29:04 +00:00
|
|
|
Primitive::Decimal(f) => format!("{}", f),
|
2019-08-27 21:45:18 +00:00
|
|
|
Primitive::Bytes(u) => format!("{}", u),
|
2019-09-10 15:31:21 +00:00
|
|
|
Primitive::Pattern(s) => format!("'{}'", s.replace("'", "''")),
|
2019-08-27 21:45:18 +00:00
|
|
|
Primitive::String(s) => format!("'{}'", s.replace("'", "''")),
|
2019-12-03 06:44:59 +00:00
|
|
|
Primitive::Line(s) => format!("'{}'", s.replace("'", "''")),
|
2019-08-27 21:45:18 +00:00
|
|
|
Primitive::Boolean(true) => "1".into(),
|
|
|
|
Primitive::Boolean(_) => "0".into(),
|
|
|
|
Primitive::Date(d) => format!("'{}'", d),
|
|
|
|
Primitive::Path(p) => format!("'{}'", p.display().to_string().replace("'", "''")),
|
2019-09-12 18:29:16 +00:00
|
|
|
Primitive::Binary(u) => format!("x'{}'", encode(u)),
|
2019-11-04 15:47:03 +00:00
|
|
|
Primitive::BeginningOfStream | Primitive::EndOfStream | Primitive::ColumnPath(_) => {
|
|
|
|
"NULL".into()
|
|
|
|
}
|
2019-08-27 21:45:18 +00:00
|
|
|
},
|
|
|
|
_ => "NULL".into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn get_insert_values(rows: Vec<Value>) -> Result<String, std::io::Error> {
|
2019-08-27 21:45:18 +00:00
|
|
|
let values: Result<Vec<_>, _> = rows
|
|
|
|
.into_iter()
|
2019-11-21 14:33:14 +00:00
|
|
|
.map(|value| match value.value {
|
|
|
|
UntaggedValue::Row(d) => Ok(format!(
|
2019-08-27 21:45:18 +00:00
|
|
|
"({})",
|
|
|
|
d.entries
|
|
|
|
.iter()
|
2019-11-21 14:33:14 +00:00
|
|
|
.map(|(_k, v)| nu_value_to_sqlite_string(v.clone()))
|
2019-08-27 21:45:18 +00:00
|
|
|
.fold("".to_string(), comma_concat)
|
|
|
|
)),
|
|
|
|
_ => Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Could not find table column names",
|
|
|
|
)),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let values = values?;
|
|
|
|
Ok(values.into_iter().fold("".to_string(), comma_concat))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_statements(table: Dictionary) -> Result<(String, String), std::io::Error> {
|
|
|
|
let table_name = match table.entries.get("table_name") {
|
2019-11-21 14:33:14 +00:00
|
|
|
Some(Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String(table_name)),
|
2019-08-27 21:45:18 +00:00
|
|
|
..
|
|
|
|
}) => table_name,
|
|
|
|
_ => {
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Could not find table name",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let (columns, insert_values) = match table.entries.get("table_values") {
|
2019-11-21 14:33:14 +00:00
|
|
|
Some(Value {
|
|
|
|
value: UntaggedValue::Table(l),
|
2019-08-27 21:45:18 +00:00
|
|
|
..
|
|
|
|
}) => (get_columns(l), get_insert_values(l.to_vec())),
|
|
|
|
_ => {
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Could not find table values",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let create = format!("create table {}({})", table_name, columns?);
|
|
|
|
let insert = format!("insert into {} values {}", table_name, insert_values?);
|
|
|
|
Ok((create, insert))
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn sqlite_input_stream_to_bytes(values: Vec<Value>) -> 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()?;
|
|
|
|
let conn = match Connection::open(tempfile.path()) {
|
|
|
|
Ok(conn) => conn,
|
|
|
|
Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
|
|
|
|
};
|
|
|
|
let tag = values[0].tag.clone();
|
|
|
|
for value in values.into_iter() {
|
2019-11-21 14:33:14 +00:00
|
|
|
match &value.value {
|
|
|
|
UntaggedValue::Row(d) => {
|
2019-08-27 21:45:18 +00:00
|
|
|
let (create, insert) = generate_statements(d.to_owned())?;
|
|
|
|
match conn
|
|
|
|
.execute(&create, NO_PARAMS)
|
|
|
|
.and_then(|_| conn.execute(&insert, NO_PARAMS))
|
|
|
|
{
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
2019-11-04 15:47:03 +00:00
|
|
|
outln!("{}", create);
|
|
|
|
outln!("{}", insert);
|
|
|
|
outln!("{:?}", e);
|
2019-08-27 21:45:18 +00:00
|
|
|
return Err(std::io::Error::new(std::io::ErrorKind::Other, e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
other => {
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
2019-09-04 16:29:49 +00:00
|
|
|
format!("Expected row, found {:?}", other),
|
2019-08-27 21:45:18 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut out = Vec::new();
|
|
|
|
tempfile.read_to_end(&mut out)?;
|
2019-12-04 19:52:31 +00:00
|
|
|
Ok(UntaggedValue::binary(out).into_value(tag))
|
2019-08-27 21:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_sqlite(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(registry)?;
|
2019-09-14 16:30:24 +00:00
|
|
|
let name_tag = args.name_tag();
|
2019-09-26 00:22:17 +00:00
|
|
|
let stream = async_stream! {
|
2019-11-21 14:33:14 +00:00
|
|
|
let input: Vec<Value> = args.input.values.collect().await;
|
2019-09-04 06:48:40 +00:00
|
|
|
|
|
|
|
match sqlite_input_stream_to_bytes(input) {
|
|
|
|
Ok(out) => yield ReturnSuccess::value(out),
|
|
|
|
_ => {
|
2019-08-27 21:45:18 +00:00
|
|
|
yield Err(ShellError::labeled_error(
|
2019-09-14 16:30:24 +00:00
|
|
|
"Expected a table with SQLite-compatible structure.tag() from pipeline",
|
2019-08-27 21:45:18 +00:00
|
|
|
"requires SQLite-compatible input",
|
2019-09-14 16:30:24 +00:00
|
|
|
name_tag,
|
2019-09-04 06:48:40 +00:00
|
|
|
))
|
|
|
|
},
|
|
|
|
}
|
2019-08-27 21:45:18 +00:00
|
|
|
};
|
2019-09-04 06:48:40 +00:00
|
|
|
|
2019-08-27 21:45:18 +00:00
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|