2023-12-03 13:31:29 +08:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2024-01-05 21:27:16 +08:00
|
|
|
"strings"
|
2023-12-03 13:31:29 +08:00
|
|
|
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *DB) CreateResource(ctx context.Context, create *store.Resource) (*store.Resource, error) {
|
2024-01-21 10:49:30 +08:00
|
|
|
fields := []string{"resource_name", "filename", "blob", "external_link", "type", "size", "creator_id", "internal_path", "memo_id"}
|
|
|
|
args := []any{create.ResourceName, create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath, create.MemoID}
|
2023-12-03 13:31:29 +08:00
|
|
|
|
2024-01-05 21:27:16 +08:00
|
|
|
stmt := "INSERT INTO resource (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts, updated_ts"
|
|
|
|
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(&create.ID, &create.CreatedTs, &create.UpdatedTs); err != nil {
|
2023-12-03 13:31:29 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-05 21:27:16 +08:00
|
|
|
return create, nil
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*store.Resource, error) {
|
2024-01-05 21:27:16 +08:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2023-12-03 13:31:29 +08:00
|
|
|
|
|
|
|
if v := find.ID; v != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *v)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
2024-01-21 10:49:30 +08:00
|
|
|
if v := find.ResourceName; v != nil {
|
|
|
|
where, args = append(where, "resource_name = "+placeholder(len(args)+1)), append(args, *v)
|
|
|
|
}
|
2023-12-03 13:31:29 +08:00
|
|
|
if v := find.CreatorID; v != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
where, args = append(where, "creator_id = "+placeholder(len(args)+1)), append(args, *v)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
if v := find.Filename; v != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
where, args = append(where, "filename = "+placeholder(len(args)+1)), append(args, *v)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
if v := find.MemoID; v != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
where, args = append(where, "memo_id = "+placeholder(len(args)+1)), append(args, *v)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
if find.HasRelatedMemo {
|
2024-01-05 21:27:16 +08:00
|
|
|
where = append(where, "memo_id IS NOT NULL")
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
2024-01-05 21:27:16 +08:00
|
|
|
|
2024-01-21 10:49:30 +08:00
|
|
|
fields := []string{"id", "resource_name", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts", "internal_path", "memo_id"}
|
2023-12-03 13:31:29 +08:00
|
|
|
if find.GetBlob {
|
2024-01-05 21:27:16 +08:00
|
|
|
fields = append(fields, "blob")
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
|
2024-01-05 21:27:16 +08:00
|
|
|
query := fmt.Sprintf(`
|
|
|
|
SELECT
|
|
|
|
%s
|
|
|
|
FROM resource
|
|
|
|
WHERE %s
|
2024-01-06 09:23:20 +08:00
|
|
|
ORDER BY updated_ts DESC, created_ts DESC
|
2024-01-05 21:27:16 +08:00
|
|
|
`, strings.Join(fields, ", "), strings.Join(where, " AND "))
|
2023-12-03 13:31:29 +08:00
|
|
|
if find.Limit != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
|
2023-12-03 13:31:29 +08:00
|
|
|
if find.Offset != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
list := make([]*store.Resource, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
resource := store.Resource{}
|
|
|
|
var memoID sql.NullInt32
|
|
|
|
dests := []any{
|
|
|
|
&resource.ID,
|
2024-01-21 10:49:30 +08:00
|
|
|
&resource.ResourceName,
|
2023-12-03 13:31:29 +08:00
|
|
|
&resource.Filename,
|
|
|
|
&resource.ExternalLink,
|
|
|
|
&resource.Type,
|
|
|
|
&resource.Size,
|
|
|
|
&resource.CreatorID,
|
2023-12-09 23:19:57 +08:00
|
|
|
&resource.CreatedTs,
|
|
|
|
&resource.UpdatedTs,
|
2023-12-03 13:31:29 +08:00
|
|
|
&resource.InternalPath,
|
|
|
|
&memoID,
|
|
|
|
}
|
|
|
|
if find.GetBlob {
|
|
|
|
dests = append(dests, &resource.Blob)
|
|
|
|
}
|
|
|
|
if err := rows.Scan(dests...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if memoID.Valid {
|
|
|
|
resource.MemoID = &memoID.Int32
|
|
|
|
}
|
|
|
|
list = append(list, &resource)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DB) UpdateResource(ctx context.Context, update *store.UpdateResource) (*store.Resource, error) {
|
2024-01-05 21:27:16 +08:00
|
|
|
set, args := []string{}, []any{}
|
2023-12-03 13:31:29 +08:00
|
|
|
|
2024-01-21 10:49:30 +08:00
|
|
|
if v := update.ResourceName; v != nil {
|
|
|
|
set, args = append(set, "resource_name = "+placeholder(len(args)+1)), append(args, *v)
|
|
|
|
}
|
2023-12-03 13:31:29 +08:00
|
|
|
if v := update.UpdatedTs; v != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
set, args = append(set, "updated_ts = "+placeholder(len(args)+1)), append(args, *v)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
if v := update.Filename; v != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
set, args = append(set, "filename = "+placeholder(len(args)+1)), append(args, *v)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
if v := update.InternalPath; v != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
set, args = append(set, "internal_path = "+placeholder(len(args)+1)), append(args, *v)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
if v := update.MemoID; v != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
set, args = append(set, "memo_id = "+placeholder(len(args)+1)), append(args, *v)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
if v := update.Blob; v != nil {
|
2024-01-05 21:27:16 +08:00
|
|
|
set, args = append(set, "blob = "+placeholder(len(args)+1)), append(args, v)
|
|
|
|
}
|
|
|
|
|
2024-01-21 10:49:30 +08:00
|
|
|
fields := []string{"id", "resource_name", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts", "internal_path"}
|
|
|
|
stmt := `UPDATE resource SET ` + strings.Join(set, ", ") + ` WHERE id = ` + placeholder(len(args)+1) + ` RETURNING ` + strings.Join(fields, ", ")
|
2024-01-05 21:27:16 +08:00
|
|
|
args = append(args, update.ID)
|
|
|
|
resource := store.Resource{}
|
|
|
|
dests := []any{
|
|
|
|
&resource.ID,
|
2024-01-21 10:49:30 +08:00
|
|
|
&resource.ResourceName,
|
2024-01-05 21:27:16 +08:00
|
|
|
&resource.Filename,
|
|
|
|
&resource.ExternalLink,
|
|
|
|
&resource.Type,
|
|
|
|
&resource.Size,
|
|
|
|
&resource.CreatorID,
|
|
|
|
&resource.CreatedTs,
|
|
|
|
&resource.UpdatedTs,
|
|
|
|
&resource.InternalPath,
|
|
|
|
}
|
|
|
|
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(dests...); err != nil {
|
2023-12-03 13:31:29 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-05 21:27:16 +08:00
|
|
|
return &resource, nil
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DB) DeleteResource(ctx context.Context, delete *store.DeleteResource) error {
|
2024-01-05 21:27:16 +08:00
|
|
|
stmt := `DELETE FROM resource WHERE id = $1`
|
|
|
|
result, err := d.db.ExecContext(ctx, stmt, delete.ID)
|
2023-12-03 13:31:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := result.RowsAffected(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func vacuumResource(ctx context.Context, tx *sql.Tx) error {
|
2024-01-21 10:49:30 +08:00
|
|
|
stmt := `DELETE FROM resource WHERE creator_id NOT IN (SELECT id FROM "user")`
|
2024-01-05 21:27:16 +08:00
|
|
|
_, err := tx.ExecContext(ctx, stmt)
|
2023-12-03 13:31:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-05 21:27:16 +08:00
|
|
|
return nil
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|