2023-09-28 22:09:52 +08:00
package mysql
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/usememos/memos/store"
)
2023-10-05 23:11:29 +08:00
func ( d * DB ) CreateMemo ( ctx context . Context , create * store . Memo ) ( * store . Memo , error ) {
2023-10-08 18:29:12 +08:00
fields := [ ] string { "`creator_id`" , "`content`" , "`visibility`" }
placeholder := [ ] string { "?" , "?" , "?" }
args := [ ] any { create . CreatorID , create . Content , create . Visibility }
stmt := "INSERT INTO memo (" + strings . Join ( fields , ", " ) + ") VALUES (" + strings . Join ( placeholder , ", " ) + ")"
result , err := d . db . ExecContext ( ctx , stmt , args ... )
2023-09-28 22:09:52 +08:00
if err != nil {
return nil , err
}
2023-09-29 09:15:54 +08:00
rawID , err := result . LastInsertId ( )
2023-09-28 22:09:52 +08:00
if err != nil {
return nil , err
}
2023-09-29 09:15:54 +08:00
id := int32 ( rawID )
memo , err := d . GetMemo ( ctx , & store . FindMemo { ID : & id } )
if err != nil {
2023-09-28 22:09:52 +08:00
return nil , err
}
2023-09-29 09:15:54 +08:00
if memo == nil {
return nil , errors . Errorf ( "failed to create memo" )
}
return memo , nil
2023-09-28 22:09:52 +08:00
}
2023-10-05 23:11:29 +08:00
func ( d * DB ) ListMemos ( ctx context . Context , find * store . FindMemo ) ( [ ] * store . Memo , error ) {
2023-09-28 22:09:52 +08:00
where , args := [ ] string { "1 = 1" } , [ ] any { }
if v := find . ID ; v != nil {
2023-10-07 22:56:12 +08:00
where , args = append ( where , "`memo`.`id` = ?" ) , append ( args , * v )
2023-09-28 22:09:52 +08:00
}
if v := find . CreatorID ; v != nil {
2023-10-07 22:56:12 +08:00
where , args = append ( where , "`memo`.`creator_id` = ?" ) , append ( args , * v )
2023-09-28 22:09:52 +08:00
}
if v := find . RowStatus ; v != nil {
2023-10-07 22:56:12 +08:00
where , args = append ( where , "`memo`.`row_status` = ?" ) , append ( args , * v )
2023-09-28 22:09:52 +08:00
}
if v := find . CreatedTsBefore ; v != nil {
2023-10-07 22:56:12 +08:00
where , args = append ( where , "UNIX_TIMESTAMP(`memo`.`created_ts`) < ?" ) , append ( args , * v )
2023-09-28 22:09:52 +08:00
}
if v := find . CreatedTsAfter ; v != nil {
2023-10-07 22:56:12 +08:00
where , args = append ( where , "UNIX_TIMESTAMP(`memo`.`created_ts`) > ?" ) , append ( args , * v )
2023-09-28 22:09:52 +08:00
}
if v := find . Pinned ; v != nil {
2023-10-07 22:56:12 +08:00
where = append ( where , "`memo_organizer`.`pinned` = 1" )
2023-09-28 22:09:52 +08:00
}
if v := find . ContentSearch ; len ( v ) != 0 {
for _ , s := range v {
2023-10-07 22:56:12 +08:00
where , args = append ( where , "`memo`.`content` LIKE ?" ) , append ( args , "%" + s + "%" )
2023-09-28 22:09:52 +08:00
}
}
if v := find . VisibilityList ; len ( v ) != 0 {
2023-12-18 20:47:29 +08:00
placeholder := [ ] string { }
2023-09-28 22:09:52 +08:00
for _ , visibility := range v {
2023-12-18 20:47:29 +08:00
placeholder = append ( placeholder , "?" )
args = append ( args , visibility . String ( ) )
2023-09-28 22:09:52 +08:00
}
2023-12-18 20:47:29 +08:00
where = append ( where , fmt . Sprintf ( "`memo`.`visibility` in (%s)" , strings . Join ( placeholder , "," ) ) )
2023-09-28 22:09:52 +08:00
}
2023-11-19 09:42:59 +08:00
orders := [ ] string { }
if find . OrderByPinned {
orders = append ( orders , "`pinned` DESC" )
}
2023-09-28 22:09:52 +08:00
if find . OrderByUpdatedTs {
2023-10-07 22:56:12 +08:00
orders = append ( orders , "`updated_ts` DESC" )
2023-09-28 22:09:52 +08:00
} else {
2023-10-07 22:56:12 +08:00
orders = append ( orders , "`created_ts` DESC" )
2023-09-28 22:09:52 +08:00
}
2023-10-07 22:56:12 +08:00
orders = append ( orders , "`id` DESC" )
2023-09-28 22:09:52 +08:00
2023-12-06 22:44:49 +08:00
fields := [ ] string {
"`memo`.`id` AS `id`" ,
"`memo`.`creator_id` AS `creator_id`" ,
"UNIX_TIMESTAMP(`memo`.`created_ts`) AS `created_ts`" ,
"UNIX_TIMESTAMP(`memo`.`updated_ts`) AS `updated_ts`" ,
"`memo`.`row_status` AS `row_status`" ,
"`memo`.`content` AS `content`" ,
"`memo`.`visibility` AS `visibility`" ,
"MAX(CASE WHEN `memo_organizer`.`pinned` = 1 THEN 1 ELSE 0 END) AS `pinned`" ,
}
query := "SELECT " + strings . Join ( fields , ",\n" ) + " FROM `memo` LEFT JOIN `memo_organizer` ON `memo`.`id` = `memo_organizer`.`memo_id` WHERE " + strings . Join ( where , " AND " ) + " GROUP BY `memo`.`id` ORDER BY " + strings . Join ( orders , ", " )
2023-09-28 22:09:52 +08:00
if find . Limit != nil {
query = fmt . Sprintf ( "%s LIMIT %d" , query , * find . Limit )
if find . Offset != nil {
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
if err := rows . Scan (
& memo . ID ,
& memo . CreatorID ,
& memo . CreatedTs ,
& memo . UpdatedTs ,
& memo . RowStatus ,
& memo . Content ,
& memo . Visibility ,
& memo . Pinned ,
) ; err != nil {
return nil , err
}
list = append ( list , & memo )
}
if err := rows . Err ( ) ; err != nil {
return nil , err
}
return list , nil
}
2023-10-05 23:11:29 +08:00
func ( d * DB ) GetMemo ( ctx context . Context , find * store . FindMemo ) ( * store . Memo , error ) {
2023-09-29 09:15:54 +08:00
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
}
2023-10-05 23:11:29 +08:00
func ( d * DB ) UpdateMemo ( ctx context . Context , update * store . UpdateMemo ) error {
2023-09-28 22:09:52 +08:00
set , args := [ ] string { } , [ ] any { }
if v := update . CreatedTs ; v != nil {
2023-10-07 22:56:12 +08:00
set , args = append ( set , "`created_ts` = FROM_UNIXTIME(?)" ) , append ( args , * v )
2023-09-28 22:09:52 +08:00
}
if v := update . UpdatedTs ; v != nil {
2023-10-07 22:56:12 +08:00
set , args = append ( set , "`updated_ts` = FROM_UNIXTIME(?)" ) , append ( args , * v )
2023-09-28 22:09:52 +08:00
}
if v := update . RowStatus ; v != nil {
2023-10-07 22:56:12 +08:00
set , args = append ( set , "`row_status` = ?" ) , append ( args , * v )
2023-09-28 22:09:52 +08:00
}
if v := update . Content ; v != nil {
2023-10-07 22:56:12 +08:00
set , args = append ( set , "`content` = ?" ) , append ( args , * v )
2023-09-28 22:09:52 +08:00
}
if v := update . Visibility ; v != nil {
2023-10-07 22:56:12 +08:00
set , args = append ( set , "`visibility` = ?" ) , append ( args , * v )
2023-09-28 22:09:52 +08:00
}
args = append ( args , update . ID )
2023-10-07 22:56:12 +08:00
stmt := "UPDATE `memo` SET " + strings . Join ( set , ", " ) + " WHERE `id` = ?"
2023-09-28 22:09:52 +08:00
if _ , err := d . db . ExecContext ( ctx , stmt , args ... ) ; err != nil {
return err
}
return nil
}
2023-10-05 23:11:29 +08:00
func ( d * DB ) DeleteMemo ( ctx context . Context , delete * store . DeleteMemo ) error {
2023-10-07 22:56:12 +08:00
where , args := [ ] string { "`id` = ?" } , [ ] any { delete . ID }
stmt := "DELETE FROM `memo` WHERE " + strings . Join ( where , " AND " )
2023-09-28 22:09:52 +08:00
result , err := d . db . ExecContext ( ctx , stmt , args ... )
if err != nil {
return err
}
if _ , err := result . RowsAffected ( ) ; err != nil {
return err
}
if err := d . Vacuum ( ctx ) ; err != nil {
// Prevent linter warning.
return err
}
return nil
}
func vacuumMemo ( ctx context . Context , tx * sql . Tx ) error {
2023-10-07 22:56:12 +08:00
stmt := "DELETE FROM `memo` WHERE `creator_id` NOT IN (SELECT `id` FROM `user`)"
2023-09-28 22:09:52 +08:00
_ , err := tx . ExecContext ( ctx , stmt )
if err != nil {
return err
}
return nil
}