mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: retire memo resource relation table
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/usememos/memos/store"
|
||||
@@ -45,35 +44,33 @@ func (d *Driver) ListResources(ctx context.Context, find *store.FindResource) ([
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if v := find.ID; v != nil {
|
||||
where, args = append(where, "resource.id = ?"), append(args, *v)
|
||||
where, args = append(where, "id = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.CreatorID; v != nil {
|
||||
where, args = append(where, "resource.creator_id = ?"), append(args, *v)
|
||||
where, args = append(where, "creator_id = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.Filename; v != nil {
|
||||
where, args = append(where, "resource.filename = ?"), append(args, *v)
|
||||
where, args = append(where, "filename = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.MemoID; v != nil {
|
||||
where, args = append(where, "resource.id in (SELECT resource_id FROM memo_resource WHERE memo_id = ?)"), append(args, *v)
|
||||
where, args = append(where, "memo_id = ?"), append(args, *v)
|
||||
}
|
||||
if find.HasRelatedMemo {
|
||||
where = append(where, "memo_resource.memo_id IS NOT NULL")
|
||||
where = append(where, "memo_id IS NOT NULL")
|
||||
}
|
||||
|
||||
fields := []string{"resource.id", "resource.filename", "resource.external_link", "resource.type", "resource.size", "resource.creator_id", "resource.created_ts", "resource.updated_ts", "internal_path"}
|
||||
fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts", "internal_path", "memo_id"}
|
||||
if find.GetBlob {
|
||||
fields = append(fields, "resource.blob")
|
||||
fields = append(fields, "blob")
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
SELECT
|
||||
GROUP_CONCAT(memo_resource.memo_id) as related_memo_ids,
|
||||
%s
|
||||
FROM resource
|
||||
LEFT JOIN memo_resource ON resource.id = memo_resource.resource_id
|
||||
WHERE %s
|
||||
GROUP BY resource.id
|
||||
ORDER BY resource.created_ts DESC
|
||||
GROUP BY id
|
||||
ORDER BY created_ts DESC
|
||||
`, strings.Join(fields, ", "), strings.Join(where, " AND "))
|
||||
if find.Limit != nil {
|
||||
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
|
||||
@@ -91,9 +88,8 @@ func (d *Driver) ListResources(ctx context.Context, find *store.FindResource) ([
|
||||
list := make([]*store.Resource, 0)
|
||||
for rows.Next() {
|
||||
resource := store.Resource{}
|
||||
var relatedMemoIDs sql.NullString
|
||||
var memoID sql.NullInt32
|
||||
dests := []any{
|
||||
&relatedMemoIDs,
|
||||
&resource.ID,
|
||||
&resource.Filename,
|
||||
&resource.ExternalLink,
|
||||
@@ -103,6 +99,7 @@ func (d *Driver) ListResources(ctx context.Context, find *store.FindResource) ([
|
||||
&resource.CreatedTs,
|
||||
&resource.UpdatedTs,
|
||||
&resource.InternalPath,
|
||||
&memoID,
|
||||
}
|
||||
if find.GetBlob {
|
||||
dests = append(dests, &resource.Blob)
|
||||
@@ -110,17 +107,8 @@ func (d *Driver) ListResources(ctx context.Context, find *store.FindResource) ([
|
||||
if err := rows.Scan(dests...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if relatedMemoIDs.Valid {
|
||||
relatedMemoIDList := strings.Split(relatedMemoIDs.String, ",")
|
||||
if len(relatedMemoIDList) > 0 {
|
||||
// Only take the first related memo ID.
|
||||
relatedMemoIDInt, err := strconv.ParseInt(relatedMemoIDList[0], 10, 32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
relatedMemoID := int32(relatedMemoIDInt)
|
||||
resource.RelatedMemoID = &relatedMemoID
|
||||
}
|
||||
if memoID.Valid {
|
||||
resource.MemoID = &memoID.Int32
|
||||
}
|
||||
list = append(list, &resource)
|
||||
}
|
||||
@@ -144,6 +132,12 @@ func (d *Driver) UpdateResource(ctx context.Context, update *store.UpdateResourc
|
||||
if v := update.InternalPath; v != nil {
|
||||
set, args = append(set, "internal_path = ?"), append(args, *v)
|
||||
}
|
||||
if v := update.MemoID; v != nil {
|
||||
set, args = append(set, "memo_id = ?"), append(args, *v)
|
||||
}
|
||||
if update.UnbindMemo {
|
||||
set = append(set, "memo_id = NULL")
|
||||
}
|
||||
if v := update.Blob; v != nil {
|
||||
set, args = append(set, "blob = ?"), append(args, v)
|
||||
}
|
||||
|
Reference in New Issue
Block a user