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
12 changes: 11 additions & 1 deletion core/services/mysql/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use super::MYSQL_SCHEME;
use super::config::MysqlConfig;
use super::core::*;
use super::deleter::MysqlDeleter;
use super::lister::MysqlLister;
use super::writer::MysqlWriter;
use opendal_core::raw::oio;
use opendal_core::raw::*;
Expand Down Expand Up @@ -164,6 +165,8 @@ impl MysqlBackend {
info.set_root("/");
info.set_native_capability(Capability {
read: true,
list: true,
list_with_recursive: true,
stat: true,
write: true,
write_can_empty: true,
Expand All @@ -189,7 +192,7 @@ impl MysqlBackend {
impl Access for MysqlBackend {
type Reader = Buffer;
type Writer = MysqlWriter;
type Lister = ();
type Lister = oio::HierarchyLister<MysqlLister>;
type Deleter = oio::OneShotDeleter<MysqlDeleter>;

fn info(&self) -> Arc<AccessorInfo> {
Expand Down Expand Up @@ -232,4 +235,11 @@ impl Access for MysqlBackend {
oio::OneShotDeleter::new(MysqlDeleter::new(self.core.clone(), self.root.clone())),
))
}

async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
let lister =
MysqlLister::new(self.core.clone(), self.root.clone(), path.to_string()).await?;
let lister = oio::HierarchyLister::new(lister, path, args.recursive());
Ok((RpList::default(), lister))
}
}
68 changes: 68 additions & 0 deletions core/services/mysql/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,76 @@ impl MysqlCore {

Ok(())
}

pub async fn list(&self, path: &str) -> Result<Vec<String>> {
let pool = self.get_client().await?;

let mut sql = format!(
"SELECT `{}` FROM `{}` WHERE `{}` LIKE ?",
self.key_field, self.table, self.key_field
);
sql.push_str(&format!(" ORDER BY `{}`", self.key_field));

let escaped = escape_like(path);
sqlx::query_scalar(&sql)
.bind(format!("{escaped}%"))
.fetch_all(pool)
.await
.map_err(parse_mysql_error)
}
}

fn escape_like(s: &str) -> String {
const ESCAPE_CHAR: char = '\\';
let mut out = String::with_capacity(s.len());
for ch in s.chars() {
match ch {
c if c == ESCAPE_CHAR => {
out.push(ESCAPE_CHAR);
out.push(ESCAPE_CHAR);
}
'%' | '_' => {
out.push(ESCAPE_CHAR);
out.push(ch);
}
_ => out.push(ch),
}
}
out
}

fn parse_mysql_error(err: sqlx::Error) -> Error {
Error::new(ErrorKind::Unexpected, "unhandled error from mysql").set_source(err)
}

#[cfg(test)]
mod tests {
use crate::core::escape_like;

#[test]
fn test_escape_like_basic() {
assert_eq!(escape_like("abc"), "abc");
assert_eq!(escape_like("foo"), "foo");
}

#[test]
fn test_escape_like_wildcards() {
assert_eq!(escape_like("%"), r"\%");
assert_eq!(escape_like("_"), r"\_");
assert_eq!(escape_like("a%b_c"), r"a\%b\_c");
}

#[test]
fn test_escape_like_escape_char() {
assert_eq!(escape_like(r"\"), r"\\");
assert_eq!(escape_like(r"\%"), r"\\\%");
}

#[test]
fn test_escape_like_mixed() {
let input = r"foo\%bar_baz%";
let expected = r"foo\\\%bar\_baz\%";

assert_eq!(escape_like(input), expected);
}
}
2 changes: 1 addition & 1 deletion core/services/mysql/src/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This service can be used to:
- [x] read
- [x] write
- [x] delete
- [ ] list
- [x] list
- [ ] copy
- [ ] rename
- [ ] ~~presign~~
Expand Down
1 change: 1 addition & 0 deletions core/services/mysql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod backend;
mod config;
mod core;
mod deleter;
mod lister;
mod writer;

pub use backend::MysqlBuilder as Mysql;
Expand Down
53 changes: 53 additions & 0 deletions core/services/mysql/src/lister.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use std::sync::Arc;
use std::vec::IntoIter;

use opendal_core::Result;
use opendal_core::raw::oio::{Entry, List};
use opendal_core::raw::{build_abs_path, build_rel_path};
use opendal_core::{EntryMode, Metadata};

use super::core::MysqlCore;

pub struct MysqlLister {
root: String,
entries: IntoIter<String>,
}

impl MysqlLister {
pub async fn new(core: Arc<MysqlCore>, root: String, path: String) -> Result<Self> {
let entries = core.list(&build_abs_path(&root, &path)).await?.into_iter();
Ok(Self { root, entries })
}
}

impl List for MysqlLister {
async fn next(&mut self) -> Result<Option<Entry>> {
let Some(key) = self.entries.next() else {
return Ok(None);
};

let mut path = build_rel_path(&self.root, &key);
if path.is_empty() {
path = "/".to_string();
}
let meta = Metadata::new(EntryMode::from_path(&path));
Ok(Some(Entry::new(&path, meta)))
}
}
Loading