mirror of
https://github.com/tursodatabase/limbo.git
synced 2025-08-04 01:58:16 +00:00
Add several more rust tests for parameter binding
This commit is contained in:
parent
56f5f47e86
commit
50f2621c12
7 changed files with 288 additions and 111 deletions
|
@ -27,7 +27,12 @@ impl Parameter {
|
|||
pub struct Parameters {
|
||||
index: NonZero<usize>,
|
||||
pub list: Vec<Parameter>,
|
||||
remap: Vec<NonZero<usize>>,
|
||||
remap: Option<Vec<NonZero<usize>>>,
|
||||
// Indexes of the referenced insert values to maintain ordering of paramaters
|
||||
param_positions: Option<Vec<(usize, NonZero<usize>)>>,
|
||||
// For insert statements with multiple rows
|
||||
current_insert_row_idx: usize,
|
||||
current_col_value_idx: Option<usize>,
|
||||
}
|
||||
|
||||
impl Default for Parameters {
|
||||
|
@ -41,7 +46,10 @@ impl Parameters {
|
|||
Self {
|
||||
index: 1.try_into().unwrap(),
|
||||
list: vec![],
|
||||
remap: vec![],
|
||||
remap: None,
|
||||
param_positions: None,
|
||||
current_insert_row_idx: 0,
|
||||
current_col_value_idx: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,13 +59,43 @@ impl Parameters {
|
|||
params.len()
|
||||
}
|
||||
|
||||
pub fn set_parameter_remap(&mut self, remap: Vec<NonZero<usize>>) {
|
||||
tracing::debug!("remap: {:?}", remap);
|
||||
self.remap = remap;
|
||||
pub fn set_value_index(&mut self, idx: usize) {
|
||||
self.current_col_value_idx = Some(idx);
|
||||
}
|
||||
|
||||
pub fn get_remapped_value(&self, idx: NonZero<usize>) -> NonZero<usize> {
|
||||
let res = *self.remap.get(idx.get() - 1).unwrap_or(&idx);
|
||||
/// Add a parameter position to the array used to build the remap.
|
||||
pub fn push_parameter_position(&mut self, index: NonZero<usize>) {
|
||||
if let Some(cur) = self.current_col_value_idx {
|
||||
if let Some(positions) = self.param_positions.as_mut() {
|
||||
positions.push((cur, index));
|
||||
tracing::debug!("push parameter position: {:?}", positions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize the stored positions array at the start of an insert statement
|
||||
pub fn init_parameter_remap(&mut self, cols: usize) {
|
||||
self.param_positions = Some(Vec::with_capacity(cols));
|
||||
}
|
||||
|
||||
/// Sorts the stored value indexes and builds and sets the remap array.
|
||||
pub fn sort_and_build_remap(&mut self) {
|
||||
self.remap = self.param_positions.as_mut().map(|positions| {
|
||||
// sort by value_index
|
||||
positions.sort_by_key(|(idx, _)| *idx);
|
||||
tracing::debug!("param positions: {:?}", positions);
|
||||
// collect the parameter indexes
|
||||
positions.iter().map(|(_, idx)| *idx).collect::<Vec<_>>()
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the remapped index for a given parameter index or the original index if none is found
|
||||
pub fn get_remapped_index(&self, idx: NonZero<usize>) -> NonZero<usize> {
|
||||
let res = *self
|
||||
.remap
|
||||
.as_ref()
|
||||
.map(|p| p.get(idx.get() - 1).unwrap_or(&idx))
|
||||
.unwrap_or(&idx);
|
||||
tracing::debug!("get_remapped_value: {idx}, value: {res}");
|
||||
res
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue