mirror of
https://github.com/nushell/nushell.git
synced 2025-01-11 16:58:41 +01:00
to sqlite: Fix panic caused by empty tables (#3522)
This commit is contained in:
parent
9dbb3e80fe
commit
01f1208ad1
@ -80,7 +80,7 @@ fn get_insert_values(rows: Vec<Value>) -> Result<String, std::io::Error> {
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let values = values?;
|
let values = values?;
|
||||||
Ok(values.into_iter().fold("".to_string(), comma_concat))
|
Ok(values.join(", "))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_statements(table: Dictionary) -> Result<(String, String), std::io::Error> {
|
fn generate_statements(table: Dictionary) -> Result<(String, String), std::io::Error> {
|
||||||
@ -100,7 +100,12 @@ fn generate_statements(table: Dictionary) -> Result<(String, String), std::io::E
|
|||||||
Some(Value {
|
Some(Value {
|
||||||
value: UntaggedValue::Table(l),
|
value: UntaggedValue::Table(l),
|
||||||
..
|
..
|
||||||
}) => (get_columns(l), get_insert_values(l.to_vec())),
|
}) => {
|
||||||
|
if l.is_empty() {
|
||||||
|
return Ok((String::new(), String::new()));
|
||||||
|
}
|
||||||
|
(get_columns(l), get_insert_values(l.to_vec()))
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(std::io::Error::new(
|
return Err(std::io::Error::new(
|
||||||
std::io::ErrorKind::Other,
|
std::io::ErrorKind::Other,
|
||||||
@ -108,6 +113,7 @@ fn generate_statements(table: Dictionary) -> Result<(String, String), std::io::E
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let create = format!("create table {}({})", table_name, columns?);
|
let create = format!("create table {}({})", table_name, columns?);
|
||||||
let insert = format!("insert into {} values {}", table_name, insert_values?);
|
let insert = format!("insert into {} values {}", table_name, insert_values?);
|
||||||
Ok((create, insert))
|
Ok((create, insert))
|
||||||
@ -128,6 +134,9 @@ fn sqlite_input_stream_to_bytes(values: Vec<Value>) -> Result<Value, std::io::Er
|
|||||||
match &value.value {
|
match &value.value {
|
||||||
UntaggedValue::Row(d) => {
|
UntaggedValue::Row(d) => {
|
||||||
let (create, insert) = generate_statements(d.to_owned())?;
|
let (create, insert) = generate_statements(d.to_owned())?;
|
||||||
|
if create.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
match conn
|
match conn
|
||||||
.execute(&create, NO_PARAMS)
|
.execute(&create, NO_PARAMS)
|
||||||
.and_then(|_| conn.execute(&insert, NO_PARAMS))
|
.and_then(|_| conn.execute(&insert, NO_PARAMS))
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# to sqlite
|
# to sqlite
|
||||||
Convert table into sqlite binary
|
Convert table into sqlite binary. Empty tables are skipped.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
```shell
|
```shell
|
||||||
|
Loading…
Reference in New Issue
Block a user