From c27e35fa5856b7cc79e72ea1fcebf5c16fdc3431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BA=AA=E5=8D=8E=E8=A3=95?= <8042833@qq.com> Date: Wed, 14 Feb 2024 22:13:45 +0800 Subject: [PATCH] feat(services/sqlite): Add list capability supported for sqlite (#4180) * add sqlite scan function * update * update * format --- core/src/services/sqlite/backend.rs | 34 +++++++++++++++++++++++++++++ core/src/services/sqlite/docs.md | 4 +--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/core/src/services/sqlite/backend.rs b/core/src/services/sqlite/backend.rs index f108aa9680af..5e83651b1742 100644 --- a/core/src/services/sqlite/backend.rs +++ b/core/src/services/sqlite/backend.rs @@ -263,6 +263,7 @@ impl kv::Adapter for Adapter { write: true, delete: true, blocking: true, + list: true, ..Default::default() }, ) @@ -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> { + 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> { + 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 = 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 { diff --git a/core/src/services/sqlite/docs.md b/core/src/services/sqlite/docs.md index ac349ace8c19..e8dad8185312 100644 --- a/core/src/services/sqlite/docs.md +++ b/core/src/services/sqlite/docs.md @@ -9,9 +9,7 @@ This service can be used to: - [x] delete - [ ] copy - [ ] rename -- [ ] ~~list~~ -- [ ] scan -- [ ] ~~presign~~ +- [x] list - [ ] blocking ## Configuration