Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions stdlib/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ fn std_database_query(
})?;

let mut result_rows = Vec::new();
let mut prototype_map = IndexMap::with_capacity(column_names.len());
let mut col_to_map_idx = Vec::with_capacity(column_names.len());
for name in column_names.iter() {
let (map_idx, _) = prototype_map.insert_full(name.clone(), RuntimeValue::Null);
col_to_map_idx.push(map_idx);
}

while let Some(row) = rows.next().map_err(|e| {
RuntimeError::new(
Expand All @@ -144,8 +150,8 @@ fn std_database_query(
None,
)
})? {
let mut row_map = IndexMap::with_capacity(column_names.len());
for (idx, name) in column_names.iter().enumerate() {
let mut row_map = prototype_map.clone();
for idx in 0..column_names.len() {
let value = match row.get_ref(idx).map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!("Database column get error: {}", e)),
Expand All @@ -165,9 +171,10 @@ fn std_database_query(
RuntimeValue::Str(s)
}
};
// The clone here is strictly necessary because `IndexMap` requires an owned `String`
// key, and each row constructs a fresh map containing these keys.
row_map.insert(name.clone(), value);
let map_idx = col_to_map_idx[idx];
if let Some((_, v)) = row_map.get_index_mut(map_idx) {
*v = value;
}
}
result_rows.push(RuntimeValue::Map {
entries: Rc::new(RefCell::new(row_map)),
Expand Down
14 changes: 12 additions & 2 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,16 @@ impl StdlibRegistry {
.collect();

let mut rows = Vec::new();
let mut prototype_map = IndexMap::with_capacity(col_count);
let mut col_to_map_idx = Vec::with_capacity(col_count);
for name in col_names.iter() {
let (map_idx, _) = prototype_map.insert_full(name.clone(), RuntimeValue::Null);
col_to_map_idx.push(map_idx);
}

let row_iter = stmt
.query_map(rusqlite::params_from_iter(params), |row| {
let mut map = IndexMap::with_capacity(col_count);
let mut map = prototype_map.clone();
for i in 0..col_count {
let val = row.get_ref_unwrap(i);
let rt_val = match val {
Expand All @@ -207,7 +214,10 @@ impl StdlibRegistry {
)
}
};
map.insert(col_names[i].clone(), rt_val);
let map_idx = col_to_map_idx[i];
if let Some((_, v)) = map.get_index_mut(map_idx) {
*v = rt_val;
}
}
Ok(map)
})
Expand Down