nushell/crates/nu_plugin_from_sqlite/src/from_sqlite.rs

129 lines
4.5 KiB
Rust
Raw Normal View History

use bigdecimal::FromPrimitive;
use nu_errors::ShellError;
use nu_protocol::{Primitive, ReturnSuccess, ReturnValue, TaggedDictBuilder, UntaggedValue, Value};
use nu_source::Tag;
2019-08-27 23:45:18 +02:00
use rusqlite::{types::ValueRef, Connection, Row, NO_PARAMS};
use std::io::Write;
use std::path::Path;
#[derive(Default)]
pub struct FromSqlite {
pub state: Vec<u8>,
pub name_tag: Tag,
}
2019-08-31 03:30:41 +02:00
impl FromSqlite {
pub fn new() -> FromSqlite {
FromSqlite {
state: vec![],
name_tag: Tag::unknown(),
}
}
2019-08-31 03:30:41 +02:00
}
2019-08-27 23:45:18 +02:00
pub fn convert_sqlite_file_to_nu_value(
path: &Path,
tag: impl Into<Tag> + Clone,
) -> Result<Value, rusqlite::Error> {
2019-08-27 23:45:18 +02: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)?;
2019-08-27 23:45:18 +02:00
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()? {
2021-02-12 11:13:14 +01:00
out.push(convert_sqlite_row_to_nu_value(table_row, tag.clone()))
2019-08-27 23:45:18 +02:00
}
meta_dict.insert_value(
2019-08-27 23:45:18 +02:00
"table_name".to_string(),
UntaggedValue::Primitive(Primitive::String(table_name)).into_value(tag.clone()),
2019-08-27 23:45:18 +02:00
);
meta_dict.insert_value(
"table_values",
UntaggedValue::Table(out).into_value(tag.clone()),
);
meta_out.push(meta_dict.into_value());
2019-08-27 23:45:18 +02:00
}
let tag = tag.into();
Ok(UntaggedValue::Table(meta_out).into_value(tag))
2019-08-27 23:45:18 +02:00
}
2021-02-12 11:13:14 +01:00
fn convert_sqlite_row_to_nu_value(row: &Row, tag: impl Into<Tag> + Clone) -> Value {
2019-08-27 23:45:18 +02:00
let mut collected = TaggedDictBuilder::new(tag.clone());
for (i, c) in row.column_names().iter().enumerate() {
collected.insert_value(
c.to_string(),
2019-08-27 23:45:18 +02:00
convert_sqlite_value_to_nu_value(row.get_raw(i), tag.clone()),
);
}
2021-02-12 11:13:14 +01:00
collected.into_value()
2019-08-27 23:45:18 +02:00
}
fn convert_sqlite_value_to_nu_value(value: ValueRef, tag: impl Into<Tag> + Clone) -> Value {
2019-08-27 23:45:18 +02:00
match value {
ValueRef::Null => {
UntaggedValue::Primitive(Primitive::String(String::from(""))).into_value(tag)
}
ValueRef::Integer(i) => UntaggedValue::int(i).into_value(tag),
ValueRef::Real(f) => {
let f = bigdecimal::BigDecimal::from_f64(f);
let tag = tag.into();
let span = tag.span;
match f {
Some(d) => UntaggedValue::decimal(d).into_value(tag),
None => UntaggedValue::Error(ShellError::labeled_error(
"Can not convert f64 to big decimal",
"can not convert to decimal",
span,
))
.into_value(tag),
}
}
ValueRef::Text(s) => {
2019-08-27 23:45:18 +02:00
// this unwrap is safe because we know the ValueRef is Text.
UntaggedValue::Primitive(Primitive::String(String::from_utf8_lossy(s).to_string()))
.into_value(tag)
2019-08-27 23:45:18 +02:00
}
ValueRef::Blob(u) => UntaggedValue::binary(u.to_owned()).into_value(tag),
2019-08-27 23:45:18 +02:00
}
}
pub fn from_sqlite_bytes_to_value(
mut bytes: Vec<u8>,
tag: impl Into<Tag> + Clone,
) -> Result<Value, std::io::Error> {
2019-08-27 23:45:18 +02: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)),
}
}
pub fn from_sqlite(bytes: Vec<u8>, name_tag: Tag) -> Result<Vec<ReturnValue>, ShellError> {
match from_sqlite_bytes_to_value(bytes, name_tag.clone()) {
Ok(x) => match x {
Value {
value: UntaggedValue::Table(list),
..
} => Ok(list.into_iter().map(ReturnSuccess::value).collect()),
_ => Ok(vec![ReturnSuccess::value(x)]),
},
Err(_) => Err(ShellError::labeled_error(
"Could not parse as SQLite",
"input cannot be parsed as SQLite",
&name_tag,
)),
}
}