2023-09-27 08:43:46 +08:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-27 09:27:31 +08:00
|
|
|
"database/sql"
|
2023-09-27 08:43:46 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
func (d *DB) UpsertMemoRelation(ctx context.Context, create *store.MemoRelation) (*store.MemoRelation, error) {
|
2023-09-27 08:43:46 +08:00
|
|
|
stmt := `
|
|
|
|
INSERT INTO memo_relation (
|
|
|
|
memo_id,
|
|
|
|
related_memo_id,
|
|
|
|
type
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
RETURNING memo_id, related_memo_id, type
|
|
|
|
`
|
|
|
|
memoRelation := &store.MemoRelation{}
|
|
|
|
if err := d.db.QueryRowContext(
|
|
|
|
ctx,
|
|
|
|
stmt,
|
|
|
|
create.MemoID,
|
|
|
|
create.RelatedMemoID,
|
|
|
|
create.Type,
|
|
|
|
).Scan(
|
|
|
|
&memoRelation.MemoID,
|
|
|
|
&memoRelation.RelatedMemoID,
|
|
|
|
&memoRelation.Type,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return memoRelation, nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation) ([]*store.MemoRelation, error) {
|
2023-09-27 08:43:46 +08:00
|
|
|
where, args := []string{"TRUE"}, []any{}
|
|
|
|
if find.MemoID != nil {
|
|
|
|
where, args = append(where, "memo_id = ?"), append(args, find.MemoID)
|
|
|
|
}
|
|
|
|
if find.RelatedMemoID != nil {
|
|
|
|
where, args = append(where, "related_memo_id = ?"), append(args, find.RelatedMemoID)
|
|
|
|
}
|
|
|
|
if find.Type != nil {
|
|
|
|
where, args = append(where, "type = ?"), append(args, find.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := d.db.QueryContext(ctx, `
|
|
|
|
SELECT
|
|
|
|
memo_id,
|
|
|
|
related_memo_id,
|
|
|
|
type
|
|
|
|
FROM memo_relation
|
|
|
|
WHERE `+strings.Join(where, " AND "), args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
list := []*store.MemoRelation{}
|
|
|
|
for rows.Next() {
|
|
|
|
memoRelation := &store.MemoRelation{}
|
|
|
|
if err := rows.Scan(
|
|
|
|
&memoRelation.MemoID,
|
|
|
|
&memoRelation.RelatedMemoID,
|
|
|
|
&memoRelation.Type,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list = append(list, memoRelation)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
func (d *DB) DeleteMemoRelation(ctx context.Context, delete *store.DeleteMemoRelation) error {
|
2023-09-27 08:43:46 +08:00
|
|
|
where, args := []string{"TRUE"}, []any{}
|
|
|
|
if delete.MemoID != nil {
|
|
|
|
where, args = append(where, "memo_id = ?"), append(args, delete.MemoID)
|
|
|
|
}
|
|
|
|
if delete.RelatedMemoID != nil {
|
|
|
|
where, args = append(where, "related_memo_id = ?"), append(args, delete.RelatedMemoID)
|
|
|
|
}
|
|
|
|
if delete.Type != nil {
|
|
|
|
where, args = append(where, "type = ?"), append(args, delete.Type)
|
|
|
|
}
|
|
|
|
stmt := `
|
|
|
|
DELETE FROM memo_relation
|
|
|
|
WHERE ` + strings.Join(where, " AND ")
|
|
|
|
result, err := d.db.ExecContext(ctx, stmt, args...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err = result.RowsAffected(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-27 09:27:31 +08:00
|
|
|
|
|
|
|
func vacuumMemoRelations(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
if _, err := tx.ExecContext(ctx, `
|
|
|
|
DELETE FROM memo_relation
|
|
|
|
WHERE memo_id NOT IN (SELECT id FROM memo) OR related_memo_id NOT IN (SELECT id FROM memo)
|
|
|
|
`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|