Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix listobjects sql err #457

Merged
merged 1 commit into from
May 23, 2023
Merged
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
4 changes: 2 additions & 2 deletions store/bsdb/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bsdb

// ListObjectsResult represents the result of a List Objects operation.
type ListObjectsResult struct {
PathName string
ResultType string
PathName string `gorm:"path_name"`
ResultType string `gorm:"result_type"`
Object
}
25 changes: 14 additions & 11 deletions store/bsdb/object.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package bsdb

import "gorm.io/gorm"
import (
"fmt"

"gorm.io/gorm"
)

// ListObjectsByBucketName lists objects information by a bucket name.
// The function takes the following parameters:
Expand Down Expand Up @@ -29,28 +33,27 @@ func (b *BsDBImpl) ListObjectsByBucketName(bucketName, continuationToken, prefix
// 2. Find common prefixes based on the delimiter
// 3. Limit results
if delimiter != "" {
err = b.db.Raw(
`SELECT path_name, result_type, o.*
query := fmt.Sprintf(`SELECT path_name, result_type, o.*
FROM (
SELECT DISTINCT object_name as path_name, 'object' as result_type, id
FROM objects
WHERE bucket_name = ? AND object_name LIKE ? AND object_name >= IF(? = '', '', ?) AND LOCATE(?, SUBSTRING(object_name, LENGTH(?) + 1)) = 0
WHERE bucket_name = '%s' AND object_name LIKE '%s' AND object_name >= '%s' AND LOCATE('%s', SUBSTRING(object_name, LENGTH('%s') + 1)) = 0
UNION
SELECT CONCAT(SUBSTRING(object_name, 1, LENGTH(?) + LOCATE(?, SUBSTRING(object_name, LENGTH(?) + 1)) - 1), ?) as path_name, 'common_prefix' as result_type, MIN(id)
SELECT CONCAT(SUBSTRING(object_name, 1, LENGTH('%s') + LOCATE('%s', SUBSTRING(object_name, LENGTH('%s') + 1)) - 1), '%s') as path_name, 'common_prefix' as result_type, MIN(id)
FROM objects
WHERE bucket_name = ? AND object_name LIKE ? AND object_name >= IF(? = '', '', ?) AND LOCATE(?, SUBSTRING(object_name, LENGTH(?) + 1)) > 0
WHERE bucket_name = '%s' AND object_name LIKE '%s' AND object_name >= '%s' AND LOCATE('%s', SUBSTRING(object_name, LENGTH('%s') + 1)) > 0
GROUP BY path_name
) AS subquery
JOIN objects o ON subquery.id = o.id
ORDER BY path_name
LIMIT ?;`,
bucketName, prefix+"%", continuationToken, continuationToken, delimiter, prefix,
LIMIT %d;`,
bucketName, prefix+"%", continuationToken, delimiter, prefix,
prefix, delimiter, prefix, delimiter,
bucketName, prefix+"%", continuationToken, continuationToken, delimiter, prefix,
limit).Scan(&results).Error
bucketName, prefix+"%", continuationToken, delimiter, prefix,
limit)
err = b.db.Raw(query).Scan(&results).Error
} else {
// If delimiter is not specified, retrieve objects directly

if continuationToken != "" {
filters = append(filters, ContinuationTokenFilter(continuationToken))
}
Expand Down