Skip to content

Commit

Permalink
feat(services/sqlite): Add list capability supported for sqlite (#4180)
Browse files Browse the repository at this point in the history
* add sqlite scan function

* update

* update

* format
  • Loading branch information
jihuayu authored Feb 14, 2024
1 parent 688fde2 commit c27e35f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
34 changes: 34 additions & 0 deletions core/src/services/sqlite/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ impl kv::Adapter for Adapter {
write: true,
delete: true,
blocking: true,
list: true,
..Default::default()
},
)
Expand Down Expand Up @@ -335,6 +336,39 @@ impl kv::Adapter for Adapter {
statement.execute([path]).map_err(parse_rusqlite_error)?;
Ok(())
}

async fn scan(&self, path: &str) -> Result<Vec<String>> {
let this = self.clone();
let path = path.to_string();

task::spawn_blocking(move || this.blocking_scan(&path))
.await
.map_err(new_task_join_error)?
}

fn blocking_scan(&self, path: &str) -> Result<Vec<String>> {
let conn = self.pool.get().map_err(parse_r2d2_error)?;
let query = format!(
"SELECT {} FROM {} WHERE `{}` LIKE $1 and `{}` <> $2",
self.key_field, self.table, self.key_field, self.key_field
);
let mut statement = conn.prepare(&query).map_err(parse_rusqlite_error)?;
let like_param = format!("{}%", path);
let result = statement.query(params![like_param, path]);

match result {
Ok(mut rows) => {
let mut keys: Vec<String> = Vec::new();
while let Some(row) = rows.next().map_err(parse_rusqlite_error)? {
let item = row.get(0).map_err(parse_rusqlite_error)?;
keys.push(item);
}
Ok(keys)
}
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(vec![]),
Err(err) => Err(parse_rusqlite_error(err)),
}
}
}

fn parse_rusqlite_error(err: rusqlite::Error) -> Error {
Expand Down
4 changes: 1 addition & 3 deletions core/src/services/sqlite/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ This service can be used to:
- [x] delete
- [ ] copy
- [ ] rename
- [ ] ~~list~~
- [ ] scan
- [ ] ~~presign~~
- [x] list
- [ ] blocking

## Configuration
Expand Down

0 comments on commit c27e35f

Please sign in to comment.