Just refactor a bit
Some checks reported errors
continuous-integration/drone/push Build was killed
continuous-integration/drone/pr Build was killed

This commit is contained in:
Alex 2022-06-02 23:46:28 +02:00
parent 0e4f336887
commit 6ebb54cddb
Signed by: lx
GPG key ID: 0E496D15096376BE

View file

@ -1,6 +1,5 @@
use core::ops::Bound; use core::ops::Bound;
use std::cell::Cell;
use std::marker::PhantomPinned; use std::marker::PhantomPinned;
use std::pin::Pin; use std::pin::Pin;
use std::ptr::NonNull; use std::ptr::NonNull;
@ -133,41 +132,8 @@ impl IDb for SqliteDb {
) -> Result<ValueIter<'a>> { ) -> Result<ValueIter<'a>> {
let tree = self.get_tree(tree)?; let tree = self.get_tree(tree)?;
let mut sql = format!("SELECT k, v FROM {}", tree); let (bounds_sql, params) = bounds_sql(low, high);
let mut params: Vec<Vec<u8>> = vec![]; let sql = format!("SELECT k, v FROM {} {} ORDER BY k ASC", tree, bounds_sql);
match low {
Bound::Included(b) => {
sql.push_str(" WHERE k >= ?1");
params.push(b.to_vec());
}
Bound::Excluded(b) => {
sql.push_str(" WHERE k > ?1");
params.push(b.to_vec());
}
Bound::Unbounded => (),
};
match high {
Bound::Included(b) => {
if !params.is_empty() {
sql.push_str(" AND k <= ?2");
} else {
sql.push_str(" WHERE k <= ?1");
}
params.push(b.to_vec());
}
Bound::Excluded(b) => {
if !params.is_empty() {
sql.push_str(" AND k < ?2");
} else {
sql.push_str(" WHERE k < ?1");
}
params.push(b.to_vec());
}
Bound::Unbounded => (),
}
sql.push_str(" ORDER BY k ASC");
let params = params let params = params
.iter() .iter()
@ -187,41 +153,8 @@ impl IDb for SqliteDb {
) -> Result<ValueIter<'a>> { ) -> Result<ValueIter<'a>> {
let tree = self.get_tree(tree)?; let tree = self.get_tree(tree)?;
let mut sql = format!("SELECT k, v FROM {}", tree); let (bounds_sql, params) = bounds_sql(low, high);
let mut params: Vec<Vec<u8>> = vec![]; let sql = format!("SELECT k, v FROM {} {} ORDER BY k DESC", tree, bounds_sql);
match low {
Bound::Included(b) => {
sql.push_str(" WHERE k >= ?1");
params.push(b.to_vec());
}
Bound::Excluded(b) => {
sql.push_str(" WHERE k > ?1");
params.push(b.to_vec());
}
Bound::Unbounded => (),
};
match high {
Bound::Included(b) => {
if !params.is_empty() {
sql.push_str(" AND k <= ?2");
} else {
sql.push_str(" WHERE k <= ?1");
}
params.push(b.to_vec());
}
Bound::Excluded(b) => {
if !params.is_empty() {
sql.push_str(" AND k < ?2");
} else {
sql.push_str(" WHERE k < ?1");
}
params.push(b.to_vec());
}
Bound::Unbounded => (),
}
sql.push_str(" ORDER BY k DESC");
let params = params let params = params
.iter() .iter()
@ -429,3 +362,44 @@ impl<'a> Iterator for DbValueIteratorPin<'a> {
Some(Ok((k.into(), v.into()))) Some(Ok((k.into(), v.into())))
} }
} }
// ----
fn bounds_sql<'r>(low: Bound<&'r [u8]>, high: Bound<&'r [u8]>) -> (String, Vec<Vec<u8>>) {
let mut sql = String::new();
let mut params: Vec<Vec<u8>> = vec![];
match low {
Bound::Included(b) => {
sql.push_str(" WHERE k >= ?1");
params.push(b.to_vec());
}
Bound::Excluded(b) => {
sql.push_str(" WHERE k > ?1");
params.push(b.to_vec());
}
Bound::Unbounded => (),
};
match high {
Bound::Included(b) => {
if !params.is_empty() {
sql.push_str(" AND k <= ?2");
} else {
sql.push_str(" WHERE k <= ?1");
}
params.push(b.to_vec());
}
Bound::Excluded(b) => {
if !params.is_empty() {
sql.push_str(" AND k < ?2");
} else {
sql.push_str(" WHERE k < ?1");
}
params.push(b.to_vec());
}
Bound::Unbounded => (),
}
(sql, params)
}