2023-09-28 17:09:52 +03:00
package mysql
import (
"context"
"database/sql"
"strings"
"github.com/usememos/memos/store"
)
2023-10-05 18:11:29 +03:00
func ( d * DB ) UpsertMemoRelation ( ctx context . Context , create * store . MemoRelation ) ( * store . MemoRelation , error ) {
2023-10-07 17:56:12 +03:00
stmt := "INSERT INTO `memo_relation` (`memo_id`, `related_memo_id`, `type`) VALUES (?, ?, ?)"
2023-09-28 17:09:52 +03:00
_ , err := d . db . ExecContext (
ctx ,
stmt ,
create . MemoID ,
create . RelatedMemoID ,
create . Type ,
)
if err != nil {
return nil , err
}
memoRelation := store . MemoRelation {
MemoID : create . MemoID ,
RelatedMemoID : create . RelatedMemoID ,
Type : create . Type ,
}
return & memoRelation , nil
}
2023-10-05 18:11:29 +03:00
func ( d * DB ) ListMemoRelations ( ctx context . Context , find * store . FindMemoRelation ) ( [ ] * store . MemoRelation , error ) {
2023-09-28 17:09:52 +03:00
where , args := [ ] string { "TRUE" } , [ ] any { }
if find . MemoID != nil {
2023-10-07 17:56:12 +03:00
where , args = append ( where , "`memo_id` = ?" ) , append ( args , find . MemoID )
2023-09-28 17:09:52 +03:00
}
if find . RelatedMemoID != nil {
2023-10-07 17:56:12 +03:00
where , args = append ( where , "`related_memo_id` = ?" ) , append ( args , find . RelatedMemoID )
2023-09-28 17:09:52 +03:00
}
if find . Type != nil {
2023-10-07 17:56:12 +03:00
where , args = append ( where , "`type` = ?" ) , append ( args , find . Type )
2023-09-28 17:09:52 +03:00
}
2023-10-07 17:56:12 +03:00
rows , err := d . db . QueryContext ( ctx , "SELECT `memo_id`, `related_memo_id`, `type` FROM `memo_relation` WHERE " + strings . Join ( where , " AND " ) , args ... )
2023-09-28 17:09:52 +03:00
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 18:11:29 +03:00
func ( d * DB ) DeleteMemoRelation ( ctx context . Context , delete * store . DeleteMemoRelation ) error {
2023-09-28 17:09:52 +03:00
where , args := [ ] string { "TRUE" } , [ ] any { }
if delete . MemoID != nil {
2023-10-07 17:56:12 +03:00
where , args = append ( where , "`memo_id` = ?" ) , append ( args , delete . MemoID )
2023-09-28 17:09:52 +03:00
}
if delete . RelatedMemoID != nil {
2023-10-07 17:56:12 +03:00
where , args = append ( where , "`related_memo_id` = ?" ) , append ( args , delete . RelatedMemoID )
2023-09-28 17:09:52 +03:00
}
if delete . Type != nil {
2023-10-07 17:56:12 +03:00
where , args = append ( where , "`type` = ?" ) , append ( args , delete . Type )
2023-09-28 17:09:52 +03:00
}
2023-10-07 17:56:12 +03:00
stmt := "DELETE FROM `memo_relation` WHERE " + strings . Join ( where , " AND " )
2023-09-28 17:09:52 +03:00
result , err := d . db . ExecContext ( ctx , stmt , args ... )
if err != nil {
return err
}
if _ , err = result . RowsAffected ( ) ; err != nil {
return err
}
return nil
}
func vacuumMemoRelations ( ctx context . Context , tx * sql . Tx ) error {
2023-10-07 17:56:12 +03:00
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-09-28 17:09:52 +03:00
return err
}
return nil
}