mirror of
https://github.com/usememos/memos.git
synced 2024-12-19 00:51:30 +03:00
9c18960f47
* skeleton of postgres skeleton * Adding Postgres specific db schema sql * user test passed * memo store test passed * tag is working * update user setting test done * activity test done * idp test passed * inbox test done * memo_organizer, UNTESTED * memo relation test passed * webhook test passed * system setting test passed * passed storage test * pass resource test * migration_history done * fix memo_relation_test * fixing server memo_relation test * passes memo relation server test * paess memo test * final manual testing done * final fixes * final fixes cleanup * sync schema * lint * lint * lint * lint * lint
124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
|
|
"github.com/usememos/memos/store"
|
|
)
|
|
|
|
func (d *DB) UpsertMemoOrganizer(ctx context.Context, upsert *store.MemoOrganizer) (*store.MemoOrganizer, error) {
|
|
pinnedValue := 0
|
|
if upsert.Pinned {
|
|
pinnedValue = 1
|
|
}
|
|
qb := squirrel.Insert("memo_organizer").
|
|
Columns("memo_id", "user_id", "pinned").
|
|
Values(upsert.MemoID, upsert.UserID, pinnedValue).
|
|
PlaceholderFormat(squirrel.Dollar)
|
|
|
|
stmt, args, err := qb.ToSql()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if _, err = d.db.ExecContext(ctx, stmt, args...); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return upsert, nil
|
|
}
|
|
|
|
func (d *DB) ListMemoOrganizer(ctx context.Context, find *store.FindMemoOrganizer) ([]*store.MemoOrganizer, error) {
|
|
qb := squirrel.Select("memo_id", "user_id", "pinned").
|
|
From("memo_organizer").
|
|
Where("1 = 1").
|
|
PlaceholderFormat(squirrel.Dollar)
|
|
|
|
if find.MemoID != 0 {
|
|
qb = qb.Where(squirrel.Eq{"memo_id": find.MemoID})
|
|
}
|
|
if find.UserID != 0 {
|
|
qb = qb.Where(squirrel.Eq{"user_id": find.UserID})
|
|
}
|
|
|
|
query, args, err := qb.ToSql()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var list []*store.MemoOrganizer
|
|
for rows.Next() {
|
|
memoOrganizer := &store.MemoOrganizer{}
|
|
if err := rows.Scan(&memoOrganizer.MemoID, &memoOrganizer.UserID, &memoOrganizer.Pinned); err != nil {
|
|
return nil, err
|
|
}
|
|
list = append(list, memoOrganizer)
|
|
}
|
|
|
|
return list, rows.Err()
|
|
}
|
|
|
|
func (d *DB) DeleteMemoOrganizer(ctx context.Context, delete *store.DeleteMemoOrganizer) error {
|
|
qb := squirrel.Delete("memo_organizer").
|
|
PlaceholderFormat(squirrel.Dollar)
|
|
|
|
if v := delete.MemoID; v != nil {
|
|
qb = qb.Where(squirrel.Eq{"memo_id": *v})
|
|
}
|
|
if v := delete.UserID; v != nil {
|
|
qb = qb.Where(squirrel.Eq{"user_id": *v})
|
|
}
|
|
|
|
stmt, args, err := qb.ToSql()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err = d.db.ExecContext(ctx, stmt, args...); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func vacuumMemoOrganizer(ctx context.Context, tx *sql.Tx) error {
|
|
// First, build the subquery for memo_id
|
|
subQueryMemo, subArgsMemo, err := squirrel.Select("id").From("memo").PlaceholderFormat(squirrel.Dollar).ToSql()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Build the subquery for user_id
|
|
subQueryUser, subArgsUser, err := squirrel.Select("id").From("\"user\"").PlaceholderFormat(squirrel.Dollar).ToSql()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Now, build the main delete query using the subqueries
|
|
query, args, err := squirrel.Delete("memo_organizer").
|
|
Where(fmt.Sprintf("memo_id NOT IN (%s)", subQueryMemo), subArgsMemo...).
|
|
Where(fmt.Sprintf("user_id NOT IN (%s)", subQueryUser), subArgsUser...).
|
|
PlaceholderFormat(squirrel.Dollar).
|
|
ToSql()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Combine the arguments from both subqueries
|
|
args = append(args, subArgsUser...)
|
|
|
|
// Execute the query
|
|
_, err = tx.ExecContext(ctx, query, args...)
|
|
return err
|
|
}
|