2023-04-25 23:27:38 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MemoRelationType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
MemoRelationReference MemoRelationType = "REFERENCE"
|
|
|
|
MemoRelationAdditional MemoRelationType = "ADDITIONAL"
|
|
|
|
)
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
type MemoRelation struct {
|
2023-04-25 23:27:38 +08:00
|
|
|
MemoID int
|
|
|
|
RelatedMemoID int
|
|
|
|
Type MemoRelationType
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
type FindMemoRelation struct {
|
2023-04-25 23:27:38 +08:00
|
|
|
MemoID *int
|
|
|
|
RelatedMemoID *int
|
|
|
|
Type *MemoRelationType
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
type DeleteMemoRelation struct {
|
2023-04-25 23:27:38 +08:00
|
|
|
MemoID *int
|
|
|
|
RelatedMemoID *int
|
|
|
|
Type *MemoRelationType
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
func (s *Store) UpsertMemoRelation(ctx context.Context, create *MemoRelation) (*MemoRelation, error) {
|
2023-07-20 23:15:56 +08:00
|
|
|
stmt := `
|
2023-04-25 23:27:38 +08:00
|
|
|
INSERT INTO memo_relation (
|
|
|
|
memo_id,
|
|
|
|
related_memo_id,
|
|
|
|
type
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
ON CONFLICT (memo_id, related_memo_id, type) DO UPDATE SET
|
|
|
|
type = EXCLUDED.type
|
|
|
|
RETURNING memo_id, related_memo_id, type
|
|
|
|
`
|
2023-07-06 22:53:38 +08:00
|
|
|
memoRelation := &MemoRelation{}
|
2023-07-20 23:15:56 +08:00
|
|
|
if err := s.db.QueryRowContext(
|
2023-04-25 23:27:38 +08:00
|
|
|
ctx,
|
2023-07-20 23:15:56 +08:00
|
|
|
stmt,
|
2023-04-25 23:27:38 +08:00
|
|
|
create.MemoID,
|
|
|
|
create.RelatedMemoID,
|
|
|
|
create.Type,
|
|
|
|
).Scan(
|
2023-07-06 22:53:38 +08:00
|
|
|
&memoRelation.MemoID,
|
|
|
|
&memoRelation.RelatedMemoID,
|
|
|
|
&memoRelation.Type,
|
2023-04-25 23:27:38 +08:00
|
|
|
); err != nil {
|
2023-07-06 21:56:42 +08:00
|
|
|
return nil, err
|
2023-04-25 23:27:38 +08:00
|
|
|
}
|
2023-07-06 22:53:38 +08:00
|
|
|
|
|
|
|
return memoRelation, nil
|
2023-04-25 23:27:38 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
func (s *Store) ListMemoRelations(ctx context.Context, find *FindMemoRelation) ([]*MemoRelation, error) {
|
2023-07-20 23:15:56 +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)
|
2023-04-25 23:27:38 +08:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:15:56 +08:00
|
|
|
rows, err := s.db.QueryContext(ctx, `
|
|
|
|
SELECT
|
|
|
|
memo_id,
|
|
|
|
related_memo_id,
|
|
|
|
type
|
|
|
|
FROM memo_relation
|
|
|
|
WHERE `+strings.Join(where, " AND "), args...)
|
2023-04-25 23:27:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-20 23:15:56 +08:00
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
list := []*MemoRelation{}
|
|
|
|
for rows.Next() {
|
|
|
|
memoRelation := &MemoRelation{}
|
|
|
|
if err := rows.Scan(
|
|
|
|
&memoRelation.MemoID,
|
|
|
|
&memoRelation.RelatedMemoID,
|
|
|
|
&memoRelation.Type,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list = append(list, memoRelation)
|
|
|
|
}
|
2023-04-25 23:27:38 +08:00
|
|
|
|
2023-07-20 23:15:56 +08:00
|
|
|
if err := rows.Err(); err != nil {
|
2023-07-06 21:56:42 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:27:38 +08:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
func (s *Store) GetMemoRelation(ctx context.Context, find *FindMemoRelation) (*MemoRelation, error) {
|
2023-07-20 23:15:56 +08:00
|
|
|
list, err := s.ListMemoRelations(ctx, find)
|
2023-04-25 23:27:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
2023-07-06 21:56:42 +08:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:27:38 +08:00
|
|
|
return list[0], nil
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
func (s *Store) DeleteMemoRelation(ctx context.Context, delete *DeleteMemoRelation) error {
|
2023-04-25 23:27:38 +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)
|
|
|
|
}
|
2023-07-20 23:15:56 +08:00
|
|
|
stmt := `
|
2023-04-25 23:27:38 +08:00
|
|
|
DELETE FROM memo_relation
|
|
|
|
WHERE ` + strings.Join(where, " AND ")
|
2023-07-20 23:15:56 +08:00
|
|
|
result, err := s.db.ExecContext(ctx, stmt, args...)
|
|
|
|
if err != nil {
|
2023-07-06 21:56:42 +08:00
|
|
|
return err
|
2023-04-25 23:27:38 +08:00
|
|
|
}
|
2023-07-20 23:15:56 +08:00
|
|
|
if _, err = result.RowsAffected(); err != nil {
|
2023-07-06 21:56:42 +08:00
|
|
|
return err
|
2023-04-25 23:27:38 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-07-06 21:56:42 +08:00
|
|
|
return err
|
2023-04-25 23:27:38 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|