mirror of
https://github.com/usememos/memos.git
synced 2024-12-21 02:01:55 +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
119 lines
2.7 KiB
Go
119 lines
2.7 KiB
Go
package mysql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"strings"
|
|
|
|
"github.com/usememos/memos/store"
|
|
)
|
|
|
|
func (d *Driver) UpsertMemoRelation(ctx context.Context, create *store.MemoRelation) (*store.MemoRelation, error) {
|
|
stmt := `
|
|
INSERT INTO memo_relation (
|
|
memo_id,
|
|
related_memo_id,
|
|
type
|
|
)
|
|
VALUES (?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE type = ?
|
|
`
|
|
_, err := d.db.ExecContext(
|
|
ctx,
|
|
stmt,
|
|
create.MemoID,
|
|
create.RelatedMemoID,
|
|
create.Type,
|
|
create.Type,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
memoRelation := store.MemoRelation{
|
|
MemoID: create.MemoID,
|
|
RelatedMemoID: create.RelatedMemoID,
|
|
Type: create.Type,
|
|
}
|
|
|
|
return &memoRelation, nil
|
|
}
|
|
|
|
func (d *Driver) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation) ([]*store.MemoRelation, error) {
|
|
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
|
|
}
|
|
|
|
func (d *Driver) DeleteMemoRelation(ctx context.Context, delete *store.DeleteMemoRelation) error {
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|