memos/store/db/postgres/memo.go

209 lines
6.2 KiB
Go
Raw Normal View History

package postgres
import (
"context"
"database/sql"
2024-01-05 21:27:16 +08:00
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/usememos/memos/store"
)
func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
2024-01-21 01:23:55 +08:00
fields := []string{"resource_name", "creator_id", "content", "visibility"}
args := []any{create.ResourceName, create.CreatorID, create.Content, create.Visibility}
2024-01-05 21:27:16 +08:00
stmt := "INSERT INTO memo (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts, updated_ts, row_status"
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&create.ID,
&create.CreatedTs,
&create.UpdatedTs,
&create.RowStatus,
); err != nil {
return nil, err
}
2024-01-05 21:27:16 +08:00
return create, nil
}
func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo, error) {
2024-01-05 21:27:16 +08:00
where, args := []string{"1 = 1"}, []any{}
if v := find.ID; v != nil {
2024-01-05 21:27:16 +08:00
where, args = append(where, "memo.id = "+placeholder(len(args)+1)), append(args, *v)
}
2024-01-21 01:23:55 +08:00
if v := find.ResourceName; v != nil {
where, args = append(where, "memo.resource_name = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.CreatorID; v != nil {
2024-01-05 21:27:16 +08:00
where, args = append(where, "memo.creator_id = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.RowStatus; v != nil {
2024-01-05 21:27:16 +08:00
where, args = append(where, "memo.row_status = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.CreatedTsBefore; v != nil {
2024-01-05 21:27:16 +08:00
where, args = append(where, "memo.created_ts < "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.CreatedTsAfter; v != nil {
2024-01-05 21:27:16 +08:00
where, args = append(where, "memo.created_ts > "+placeholder(len(args)+1)), append(args, *v)
}
2024-01-18 14:30:20 +08:00
if v := find.UpdatedTsBefore; v != nil {
where, args = append(where, "memo.updated_ts < "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.UpdatedTsAfter; v != nil {
where, args = append(where, "memo.updated_ts > "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.ContentSearch; len(v) != 0 {
for _, s := range v {
2024-01-05 21:27:16 +08:00
where, args = append(where, "memo.content LIKE "+placeholder(len(args)+1)), append(args, fmt.Sprintf("%%%s%%", s))
}
}
if v := find.VisibilityList; len(v) != 0 {
2024-01-05 21:27:16 +08:00
holders := []string{}
for _, visibility := range v {
holders = append(holders, placeholder(len(args)+1))
args = append(args, visibility.String())
}
2024-01-05 21:27:16 +08:00
where = append(where, fmt.Sprintf("memo.visibility in (%s)", strings.Join(holders, ", ")))
}
if find.ExcludeComments {
2024-01-06 13:22:02 +08:00
where = append(where, "memo_relation.related_memo_id IS NULL")
}
2024-01-05 21:27:16 +08:00
orders := []string{}
if find.OrderByPinned {
2024-01-05 21:27:16 +08:00
orders = append(orders, "pinned DESC")
}
if find.OrderByUpdatedTs {
2024-01-05 21:27:16 +08:00
orders = append(orders, "updated_ts DESC")
} else {
2024-01-05 21:27:16 +08:00
orders = append(orders, "created_ts DESC")
}
2024-01-05 21:27:16 +08:00
orders = append(orders, "id DESC")
2024-01-05 21:27:16 +08:00
fields := []string{
`memo.id AS id`,
2024-01-21 01:23:55 +08:00
`memo.resource_name AS resource_name`,
2024-01-05 21:27:16 +08:00
`memo.creator_id AS creator_id`,
`memo.created_ts AS created_ts`,
`memo.updated_ts AS updated_ts`,
`memo.row_status AS row_status`,
`memo.visibility AS visibility`,
2024-01-14 20:51:52 +08:00
`COALESCE(memo_organizer.pinned, 0) AS pinned`,
2024-01-06 13:22:02 +08:00
`memo_relation.related_memo_id AS parent_id`,
2024-01-05 21:27:16 +08:00
}
if !find.ExcludeContent {
fields = append(fields, `memo.content AS content`)
}
query := `SELECT ` + strings.Join(fields, ", ") + `
FROM memo
2024-01-28 08:38:29 +08:00
LEFT JOIN memo_organizer ON memo.id = memo_organizer.memo_id AND memo.creator_id = memo_organizer.user_id
LEFT JOIN memo_relation ON memo.id = memo_relation.memo_id AND memo_relation.type = 'COMMENT'
2024-01-05 21:27:16 +08:00
WHERE ` + strings.Join(where, " AND ") + `
ORDER BY ` + strings.Join(orders, ", ")
if find.Limit != nil {
2024-01-05 21:27:16 +08:00
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
if find.Offset != nil {
2024-01-05 21:27:16 +08:00
query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
}
}
rows, err := d.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
list := make([]*store.Memo, 0)
for rows.Next() {
var memo store.Memo
2024-01-05 21:27:16 +08:00
dests := []any{
&memo.ID,
2024-01-21 01:23:55 +08:00
&memo.ResourceName,
&memo.CreatorID,
2023-12-09 23:19:57 +08:00
&memo.CreatedTs,
&memo.UpdatedTs,
&memo.RowStatus,
&memo.Visibility,
2024-01-14 20:51:52 +08:00
&memo.Pinned,
&memo.ParentID,
2024-01-05 21:27:16 +08:00
}
if !find.ExcludeContent {
dests = append(dests, &memo.Content)
}
if err := rows.Scan(dests...); err != nil {
return nil, err
}
list = append(list, &memo)
}
if err := rows.Err(); err != nil {
return nil, err
}
return list, nil
}
func (d *DB) GetMemo(ctx context.Context, find *store.FindMemo) (*store.Memo, error) {
list, err := d.ListMemos(ctx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}
memo := list[0]
return memo, nil
}
func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
2024-01-05 21:27:16 +08:00
set, args := []string{}, []any{}
2024-01-21 01:23:55 +08:00
if v := update.ResourceName; v != nil {
set, args = append(set, "resource_name = "+placeholder(len(args)+1)), append(args, *v)
}
if v := update.CreatedTs; v != nil {
2024-01-05 21:27:16 +08:00
set, args = append(set, "created_ts = "+placeholder(len(args)+1)), append(args, *v)
}
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)
}
if v := update.RowStatus; v != nil {
2024-01-05 21:27:16 +08:00
set, args = append(set, "row_status = "+placeholder(len(args)+1)), append(args, *v)
}
if v := update.Content; v != nil {
2024-01-05 21:27:16 +08:00
set, args = append(set, "content = "+placeholder(len(args)+1)), append(args, *v)
}
if v := update.Visibility; v != nil {
2024-01-05 21:27:16 +08:00
set, args = append(set, "visibility = "+placeholder(len(args)+1)), append(args, *v)
}
2024-01-05 21:27:16 +08:00
stmt := `UPDATE memo SET ` + strings.Join(set, ", ") + ` WHERE id = ` + placeholder(len(args)+1)
args = append(args, update.ID)
if _, err := d.db.ExecContext(ctx, stmt, args...); err != nil {
return err
}
return nil
}
func (d *DB) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
2024-01-05 21:27:16 +08:00
where, args := []string{"id = " + placeholder(1)}, []any{delete.ID}
stmt := `DELETE FROM memo WHERE ` + strings.Join(where, " AND ")
result, err := d.db.ExecContext(ctx, stmt, args...)
if err != nil {
2024-01-05 21:27:16 +08:00
return errors.Wrap(err, "failed to delete memo")
}
if _, err := result.RowsAffected(); err != nil {
return err
}
2024-01-05 21:27:16 +08:00
return nil
}
func vacuumMemo(ctx context.Context, tx *sql.Tx) error {
2023-12-12 19:56:15 +08:00
stmt := `DELETE FROM memo WHERE creator_id NOT IN (SELECT id FROM "user")`
_, err := tx.ExecContext(ctx, stmt)
return err
}