forked from extern/nushell
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();
|
||||
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> {
|
||||
@ -100,7 +100,12 @@ fn generate_statements(table: Dictionary) -> Result<(String, String), std::io::E
|
||||
Some(Value {
|
||||
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(
|
||||
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 insert = format!("insert into {} values {}", table_name, insert_values?);
|
||||
Ok((create, insert))
|
||||
@ -128,6 +134,9 @@ fn sqlite_input_stream_to_bytes(values: Vec<Value>) -> Result<Value, std::io::Er
|
||||
match &value.value {
|
||||
UntaggedValue::Row(d) => {
|
||||
let (create, insert) = generate_statements(d.to_owned())?;
|
||||
if create.is_empty() {
|
||||
continue;
|
||||
}
|
||||
match conn
|
||||
.execute(&create, NO_PARAMS)
|
||||
.and_then(|_| conn.execute(&insert, NO_PARAMS))
|
||||
|
@ -1,5 +1,5 @@
|
||||
# to sqlite
|
||||
Convert table into sqlite binary
|
||||
Convert table into sqlite binary. Empty tables are skipped.
|
||||
|
||||
## Usage
|
||||
```shell
|
||||
|
Loading…
Reference in New Issue
Block a user