2022-09-30 20:20:00 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-07-06 00:01:40 +08:00
|
|
|
type MemoResource struct {
|
|
|
|
MemoID int
|
|
|
|
ResourceID int
|
|
|
|
CreatedTs int64
|
|
|
|
UpdatedTs int64
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
type UpsertMemoResource struct {
|
|
|
|
MemoID int
|
|
|
|
ResourceID int
|
|
|
|
CreatedTs int64
|
|
|
|
UpdatedTs *int64
|
|
|
|
}
|
|
|
|
|
2023-07-06 00:01:40 +08:00
|
|
|
type FindMemoResource struct {
|
|
|
|
MemoID *int
|
|
|
|
ResourceID *int
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
type DeleteMemoResource struct {
|
|
|
|
MemoID *int
|
|
|
|
ResourceID *int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) UpsertMemoResource(ctx context.Context, upsert *UpsertMemoResource) (*MemoResource, error) {
|
2023-07-06 00:01:40 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-06 22:53:38 +08:00
|
|
|
return nil, err
|
2023-07-06 00:01:40 +08:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
set := []string{"memo_id", "resource_id"}
|
|
|
|
args := []any{upsert.MemoID, upsert.ResourceID}
|
|
|
|
placeholder := []string{"?", "?"}
|
2023-07-06 00:01:40 +08:00
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
if v := upsert.UpdatedTs; v != nil {
|
|
|
|
set, args, placeholder = append(set, "updated_ts"), append(args, v), append(placeholder, "?")
|
2023-07-06 00:01:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
query := `
|
2023-07-06 21:56:42 +08:00
|
|
|
INSERT INTO memo_resource (
|
|
|
|
` + strings.Join(set, ", ") + `
|
|
|
|
)
|
|
|
|
VALUES (` + strings.Join(placeholder, ",") + `)
|
|
|
|
ON CONFLICT(memo_id, resource_id) DO UPDATE
|
|
|
|
SET
|
|
|
|
updated_ts = EXCLUDED.updated_ts
|
|
|
|
RETURNING memo_id, resource_id, created_ts, updated_ts
|
2023-07-06 00:01:40 +08:00
|
|
|
`
|
2023-07-06 21:56:42 +08:00
|
|
|
memoResource := &MemoResource{}
|
|
|
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
|
|
|
&memoResource.MemoID,
|
|
|
|
&memoResource.ResourceID,
|
|
|
|
&memoResource.CreatedTs,
|
|
|
|
&memoResource.UpdatedTs,
|
|
|
|
); err != nil {
|
2023-07-06 22:53:38 +08:00
|
|
|
return nil, err
|
2023-07-06 00:01:40 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
if err := tx.Commit(); err != nil {
|
2023-07-06 22:53:38 +08:00
|
|
|
return nil, err
|
2023-07-06 00:01:40 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
return memoResource, nil
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
func (s *Store) ListMemoResources(ctx context.Context, find *FindMemoResource) ([]*MemoResource, error) {
|
2022-09-30 20:20:00 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-06 21:56:42 +08:00
|
|
|
return nil, err
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
list, err := listMemoResources(ctx, tx, find)
|
2022-09-30 20:20:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
func (s *Store) GetMemoResource(ctx context.Context, find *FindMemoResource) (*MemoResource, error) {
|
2022-10-14 23:14:08 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-06 21:56:42 +08:00
|
|
|
return nil, err
|
2022-10-14 23:14:08 +08:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
list, err := listMemoResources(ctx, tx, find)
|
2022-10-14 23:14:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(list) == 0 {
|
2023-07-06 21:56:42 +08:00
|
|
|
return nil, nil
|
2022-10-14 23:14:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-14 23:14:08 +08:00
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
memoResource := list[0]
|
|
|
|
return memoResource, nil
|
2022-10-14 23:14:08 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
func (s *Store) DeleteMemoResource(ctx context.Context, delete *DeleteMemoResource) error {
|
2022-09-30 20:20:00 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-06 22:53:38 +08:00
|
|
|
return err
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
where, args := []string{}, []any{}
|
2022-09-30 20:20:00 +08:00
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
if v := delete.MemoID; v != nil {
|
|
|
|
where, args = append(where, "memo_id = ?"), append(args, *v)
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
2023-07-06 21:56:42 +08:00
|
|
|
if v := delete.ResourceID; v != nil {
|
|
|
|
where, args = append(where, "resource_id = ?"), append(args, *v)
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
stmt := `DELETE FROM memo_resource WHERE ` + strings.Join(where, " AND ")
|
|
|
|
_, err = tx.ExecContext(ctx, stmt, args...)
|
|
|
|
if err != nil {
|
2023-07-06 22:53:38 +08:00
|
|
|
return err
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2023-07-06 22:53:38 +08:00
|
|
|
// Prevent linter warning.
|
|
|
|
return err
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
func listMemoResources(ctx context.Context, tx *sql.Tx, find *FindMemoResource) ([]*MemoResource, error) {
|
2023-03-18 22:34:22 +08:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2022-09-30 20:20:00 +08:00
|
|
|
|
|
|
|
if v := find.MemoID; v != nil {
|
|
|
|
where, args = append(where, "memo_id = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := find.ResourceID; v != nil {
|
|
|
|
where, args = append(where, "resource_id = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
memo_id,
|
|
|
|
resource_id,
|
|
|
|
created_ts,
|
|
|
|
updated_ts
|
|
|
|
FROM memo_resource
|
|
|
|
WHERE ` + strings.Join(where, " AND ") + `
|
|
|
|
ORDER BY updated_ts DESC
|
|
|
|
`
|
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
2023-07-06 22:53:38 +08:00
|
|
|
return nil, err
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
list := make([]*MemoResource, 0)
|
2022-09-30 20:20:00 +08:00
|
|
|
for rows.Next() {
|
2023-07-06 21:56:42 +08:00
|
|
|
var memoResource MemoResource
|
2022-09-30 20:20:00 +08:00
|
|
|
if err := rows.Scan(
|
2023-07-06 21:56:42 +08:00
|
|
|
&memoResource.MemoID,
|
|
|
|
&memoResource.ResourceID,
|
|
|
|
&memoResource.CreatedTs,
|
|
|
|
&memoResource.UpdatedTs,
|
2022-09-30 20:20:00 +08:00
|
|
|
); err != nil {
|
2023-07-06 22:53:38 +08:00
|
|
|
return nil, err
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
list = append(list, &memoResource)
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
return list, nil
|
2022-09-30 20:20:00 +08:00
|
|
|
}
|
2022-11-06 12:21:58 +08:00
|
|
|
|
|
|
|
func vacuumMemoResource(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
stmt := `
|
|
|
|
DELETE FROM
|
|
|
|
memo_resource
|
|
|
|
WHERE
|
|
|
|
memo_id NOT IN (
|
|
|
|
SELECT
|
|
|
|
id
|
|
|
|
FROM
|
|
|
|
memo
|
|
|
|
)
|
|
|
|
OR resource_id NOT IN (
|
|
|
|
SELECT
|
|
|
|
id
|
|
|
|
FROM
|
|
|
|
resource
|
|
|
|
)`
|
|
|
|
_, err := tx.ExecContext(ctx, stmt)
|
|
|
|
if err != nil {
|
2023-07-06 22:53:38 +08:00
|
|
|
return err
|
2022-11-06 12:21:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|