mirror of
https://github.com/usememos/memos.git
synced 2024-12-25 04:13:07 +03:00
c72f221fc0
* Rename checkDSN to checkDataDir * Add option to set DSN and db driver * Add mysql driver skeleton * Add mysql container in compose for debug * Add basic function for mysql driver * Cleanup go mod with tidy * Cleanup go.sum with tidy * Add DeleteUser support for mysql driver * Fix UpdateUser of mysql driver * Add DeleteTag support for mysql driver * Add DeleteResource support for mysql driver * Add UpdateMemo and DeleteMemo support for mysql driver * Add MemoRelation support for mysql driver * Add MemoOrganizer support for mysql driver * Add Idp support for mysql driver * Add Storage support for mysql driver * Add FindMemosVisibilityList support for mysql driver * Add Vacuum support for mysql driver * Add Migration support for mysql driver * Add Migration support for mysql driver * Fix ListMemo failed with referece * Change Activity.CreateTs type in MySQL * Change User.CreateTs type in MySQL * Fix by golangci-lint * Change Resource.CreateTs type in MySQL * Change MigrationHistory.CreateTs type in MySQL * Change Memo.CreateTs type in MySQL
107 lines
2.1 KiB
Go
107 lines
2.1 KiB
Go
package mysql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/usememos/memos/store"
|
|
)
|
|
|
|
func (d *Driver) UpsertMemoOrganizer(ctx context.Context, upsert *store.MemoOrganizer) (*store.MemoOrganizer, error) {
|
|
stmt := `
|
|
INSERT INTO memo_organizer (
|
|
memo_id,
|
|
user_id,
|
|
pinned
|
|
)
|
|
VALUES (?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE pinned = ?
|
|
`
|
|
if _, err := d.db.ExecContext(ctx, stmt, upsert.MemoID, upsert.UserID, upsert.Pinned, upsert.Pinned); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return upsert, nil
|
|
}
|
|
|
|
func (d *Driver) GetMemoOrganizer(ctx context.Context, find *store.FindMemoOrganizer) (*store.MemoOrganizer, error) {
|
|
where, args := []string{}, []any{}
|
|
if find.MemoID != 0 {
|
|
where = append(where, "memo_id = ?")
|
|
args = append(args, find.MemoID)
|
|
}
|
|
if find.UserID != 0 {
|
|
where = append(where, "user_id = ?")
|
|
args = append(args, find.UserID)
|
|
}
|
|
|
|
query := fmt.Sprintf(`
|
|
SELECT
|
|
memo_id,
|
|
user_id,
|
|
pinned
|
|
FROM memo_organizer
|
|
WHERE %s
|
|
`, strings.Join(where, " AND "))
|
|
row := d.db.QueryRowContext(ctx, query, args...)
|
|
if err := row.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if row == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
memoOrganizer := &store.MemoOrganizer{}
|
|
if err := row.Scan(
|
|
&memoOrganizer.MemoID,
|
|
&memoOrganizer.UserID,
|
|
&memoOrganizer.Pinned,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return memoOrganizer, nil
|
|
}
|
|
|
|
func (d *Driver) DeleteMemoOrganizer(ctx context.Context, delete *store.DeleteMemoOrganizer) error {
|
|
where, args := []string{}, []any{}
|
|
if v := delete.MemoID; v != nil {
|
|
where, args = append(where, "memo_id = ?"), append(args, *v)
|
|
}
|
|
if v := delete.UserID; v != nil {
|
|
where, args = append(where, "user_id = ?"), append(args, *v)
|
|
}
|
|
stmt := `DELETE FROM memo_organizer WHERE ` + strings.Join(where, " AND ")
|
|
if _, err := d.db.ExecContext(ctx, stmt, args...); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func vacuumMemoOrganizer(ctx context.Context, tx *sql.Tx) error {
|
|
stmt := `
|
|
DELETE FROM
|
|
memo_organizer
|
|
WHERE
|
|
memo_id NOT IN (
|
|
SELECT
|
|
id
|
|
FROM
|
|
memo
|
|
)
|
|
OR user_id NOT IN (
|
|
SELECT
|
|
id
|
|
FROM
|
|
user
|
|
)`
|
|
_, err := tx.ExecContext(ctx, stmt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|