mirror of
https://github.com/usememos/memos.git
synced 2025-02-01 01:58:14 +08:00
chore: move SQL code of MemoOrganizer into Driver (#2291)
This commit is contained in:
parent
acbde4fb2d
commit
58b84f83d1
3 changed files with 89 additions and 67 deletions
|
@ -18,6 +18,10 @@ type Driver interface {
|
|||
ListMemoRelations(ctx context.Context, find *FindMemoRelation) ([]*MemoRelation, error)
|
||||
DeleteMemoRelation(ctx context.Context, delete *DeleteMemoRelation) error
|
||||
|
||||
UpsertMemoOrganizer(ctx context.Context, upsert *MemoOrganizer) (*MemoOrganizer, error)
|
||||
GetMemoOrganizer(ctx context.Context, find *FindMemoOrganizer) (*MemoOrganizer, error)
|
||||
DeleteMemoOrganizer(ctx context.Context, delete *DeleteMemoOrganizer) error
|
||||
|
||||
UpsertSystemSetting(ctx context.Context, upsert *SystemSetting) (*SystemSetting, error)
|
||||
ListSystemSettings(ctx context.Context, find *FindSystemSetting) ([]*SystemSetting, error)
|
||||
|
||||
|
|
|
@ -3,8 +3,6 @@ package store
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MemoOrganizer struct {
|
||||
|
@ -24,77 +22,15 @@ type DeleteMemoOrganizer struct {
|
|||
}
|
||||
|
||||
func (s *Store) UpsertMemoOrganizer(ctx context.Context, upsert *MemoOrganizer) (*MemoOrganizer, error) {
|
||||
stmt := `
|
||||
INSERT INTO memo_organizer (
|
||||
memo_id,
|
||||
user_id,
|
||||
pinned
|
||||
)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(memo_id, user_id) DO UPDATE
|
||||
SET
|
||||
pinned = EXCLUDED.pinned
|
||||
`
|
||||
if _, err := s.db.ExecContext(ctx, stmt, upsert.MemoID, upsert.UserID, upsert.Pinned); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
memoOrganizer := upsert
|
||||
return memoOrganizer, nil
|
||||
return s.driver.UpsertMemoOrganizer(ctx, upsert)
|
||||
}
|
||||
|
||||
func (s *Store) GetMemoOrganizer(ctx context.Context, find *FindMemoOrganizer) (*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 := s.db.QueryRowContext(ctx, query, args...)
|
||||
if err := row.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if row == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
memoOrganizer := &MemoOrganizer{}
|
||||
if err := row.Scan(
|
||||
&memoOrganizer.MemoID,
|
||||
&memoOrganizer.UserID,
|
||||
&memoOrganizer.Pinned,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return memoOrganizer, nil
|
||||
return s.driver.GetMemoOrganizer(ctx, find)
|
||||
}
|
||||
|
||||
func (s *Store) DeleteMemoOrganizer(ctx context.Context, delete *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 := s.db.ExecContext(ctx, stmt, args...); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return s.driver.DeleteMemoOrganizer(ctx, delete)
|
||||
}
|
||||
|
||||
func vacuumMemoOrganizer(ctx context.Context, tx *sql.Tx) error {
|
||||
|
|
82
store/sqlite/memo_organizer.go
Normal file
82
store/sqlite/memo_organizer.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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 CONFLICT(memo_id, user_id) DO UPDATE
|
||||
SET
|
||||
pinned = EXCLUDED.pinned
|
||||
`
|
||||
if _, err := d.db.ExecContext(ctx, stmt, upsert.MemoID, upsert.UserID, 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
|
||||
}
|
Loading…
Reference in a new issue