2023-09-27 03:57:12 +03:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2023-10-26 04:02:50 +03:00
|
|
|
"github.com/usememos/memos/internal/util"
|
2023-09-27 03:57:12 +03:00
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
|
2023-10-08 13:29:12 +03:00
|
|
|
fields := []string{"`creator_id`", "`content`", "`visibility`"}
|
|
|
|
placeholder := []string{"?", "?", "?"}
|
|
|
|
args := []any{create.CreatorID, create.Content, create.Visibility}
|
|
|
|
|
|
|
|
if create.ID != 0 {
|
|
|
|
fields = append(fields, "`id`")
|
|
|
|
placeholder = append(placeholder, "?")
|
|
|
|
args = append(args, create.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if create.CreatedTs != 0 {
|
|
|
|
fields = append(fields, "`created_ts`")
|
|
|
|
placeholder = append(placeholder, "?")
|
|
|
|
args = append(args, create.CreatedTs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if create.UpdatedTs != 0 {
|
|
|
|
fields = append(fields, "`updated_ts`")
|
|
|
|
placeholder = append(placeholder, "?")
|
|
|
|
args = append(args, create.UpdatedTs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if create.RowStatus != "" {
|
|
|
|
fields = append(fields, "`row_status`")
|
|
|
|
placeholder = append(placeholder, "?")
|
|
|
|
args = append(args, create.RowStatus)
|
2023-09-27 03:57:12 +03:00
|
|
|
}
|
|
|
|
|
2023-10-08 13:29:12 +03:00
|
|
|
stmt := "INSERT INTO memo (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`, `row_status`"
|
|
|
|
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
2023-09-27 03:57:12 +03:00
|
|
|
&create.ID,
|
|
|
|
&create.CreatedTs,
|
|
|
|
&create.UpdatedTs,
|
|
|
|
&create.RowStatus,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return create, nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo, error) {
|
2023-09-27 03:57:12 +03:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
|
|
|
|
if v := find.ID; v != nil {
|
|
|
|
where, args = append(where, "memo.id = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := find.CreatorID; v != nil {
|
|
|
|
where, args = append(where, "memo.creator_id = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := find.RowStatus; v != nil {
|
|
|
|
where, args = append(where, "memo.row_status = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := find.CreatedTsBefore; v != nil {
|
|
|
|
where, args = append(where, "memo.created_ts < ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := find.CreatedTsAfter; v != nil {
|
|
|
|
where, args = append(where, "memo.created_ts > ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := find.ContentSearch; len(v) != 0 {
|
|
|
|
for _, s := range v {
|
|
|
|
where, args = append(where, "memo.content LIKE ?"), append(args, "%"+s+"%")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v := find.VisibilityList; len(v) != 0 {
|
|
|
|
list := []string{}
|
|
|
|
for _, visibility := range v {
|
|
|
|
list = append(list, fmt.Sprintf("$%d", len(args)+1))
|
|
|
|
args = append(args, visibility)
|
|
|
|
}
|
|
|
|
where = append(where, fmt.Sprintf("memo.visibility in (%s)", strings.Join(list, ",")))
|
|
|
|
}
|
2023-10-07 19:42:02 +03:00
|
|
|
if v := find.Pinned; v != nil {
|
|
|
|
where = append(where, "memo_organizer.pinned = 1")
|
|
|
|
}
|
|
|
|
if v := find.HasParent; v != nil {
|
|
|
|
if *v {
|
|
|
|
where = append(where, "parent_id IS NOT NULL")
|
|
|
|
} else {
|
|
|
|
where = append(where, "parent_id IS NULL")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-27 03:57:12 +03:00
|
|
|
orders := []string{"pinned DESC"}
|
|
|
|
if find.OrderByUpdatedTs {
|
|
|
|
orders = append(orders, "updated_ts DESC")
|
|
|
|
} else {
|
|
|
|
orders = append(orders, "created_ts DESC")
|
|
|
|
}
|
|
|
|
orders = append(orders, "id DESC")
|
|
|
|
|
2023-10-20 03:48:52 +03:00
|
|
|
fields := []string{
|
2023-10-20 03:52:16 +03:00
|
|
|
`memo.id AS id,`,
|
|
|
|
`memo.creator_id AS creator_id,`,
|
|
|
|
`memo.created_ts AS created_ts,`,
|
|
|
|
`memo.updated_ts AS updated_ts,`,
|
|
|
|
`memo.row_status AS row_status,`,
|
|
|
|
`memo.visibility AS visibility,`,
|
2023-10-20 03:48:52 +03:00
|
|
|
}
|
|
|
|
if !find.ExcludeContent {
|
2023-10-20 03:52:16 +03:00
|
|
|
fields = append(fields, `memo.content AS content,`)
|
2023-10-20 03:48:52 +03:00
|
|
|
}
|
|
|
|
|
2023-09-27 03:57:12 +03:00
|
|
|
query := `
|
|
|
|
SELECT
|
2023-10-20 03:52:16 +03:00
|
|
|
` + strings.Join(fields, "\n") + `
|
2023-10-20 03:48:52 +03:00
|
|
|
CASE WHEN mo.pinned = 1 THEN 1 ELSE 0 END AS pinned,
|
|
|
|
(
|
2023-10-07 19:42:02 +03:00
|
|
|
SELECT
|
|
|
|
related_memo_id
|
|
|
|
FROM
|
|
|
|
memo_relation
|
|
|
|
WHERE
|
|
|
|
memo_relation.memo_id = memo.id AND memo_relation.type = 'COMMENT'
|
|
|
|
LIMIT 1
|
2023-10-20 03:48:52 +03:00
|
|
|
) AS parent_id,
|
|
|
|
GROUP_CONCAT(resource.id) AS resource_id_list,
|
|
|
|
(
|
2023-10-01 11:27:40 +03:00
|
|
|
SELECT
|
2023-10-20 03:48:52 +03:00
|
|
|
GROUP_CONCAT(memo_relation.memo_id || ':' || memo_relation.related_memo_id || ':' || memo_relation.type)
|
2023-10-01 11:27:40 +03:00
|
|
|
FROM
|
|
|
|
memo_relation
|
|
|
|
WHERE
|
2023-10-01 16:35:17 +03:00
|
|
|
memo_relation.memo_id = memo.id OR memo_relation.related_memo_id = memo.id
|
2023-10-20 03:48:52 +03:00
|
|
|
) AS relation_list
|
|
|
|
FROM
|
|
|
|
memo
|
|
|
|
LEFT JOIN
|
|
|
|
memo_organizer mo ON memo.id = mo.memo_id
|
|
|
|
LEFT JOIN
|
|
|
|
resource ON memo.id = resource.memo_id
|
|
|
|
WHERE ` + strings.Join(where, " AND ") + `
|
|
|
|
GROUP BY memo.id
|
|
|
|
ORDER BY ` + strings.Join(orders, ", ")
|
2023-09-27 03:57:12 +03:00
|
|
|
if find.Limit != nil {
|
|
|
|
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
|
|
|
|
if find.Offset != nil {
|
|
|
|
query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
list := make([]*store.Memo, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var memo store.Memo
|
|
|
|
var memoResourceIDList sql.NullString
|
|
|
|
var memoRelationList sql.NullString
|
2023-10-20 03:48:52 +03:00
|
|
|
dests := []any{
|
2023-09-27 03:57:12 +03:00
|
|
|
&memo.ID,
|
|
|
|
&memo.CreatorID,
|
|
|
|
&memo.CreatedTs,
|
|
|
|
&memo.UpdatedTs,
|
|
|
|
&memo.RowStatus,
|
|
|
|
&memo.Visibility,
|
2023-10-20 03:48:52 +03:00
|
|
|
}
|
|
|
|
if !find.ExcludeContent {
|
|
|
|
dests = append(dests, &memo.Content)
|
|
|
|
}
|
|
|
|
dests = append(dests, &memo.Pinned, &memo.ParentID, &memoResourceIDList, &memoRelationList)
|
|
|
|
if err := rows.Scan(dests...); err != nil {
|
2023-09-27 03:57:12 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if memoResourceIDList.Valid {
|
|
|
|
idStringList := strings.Split(memoResourceIDList.String, ",")
|
|
|
|
memo.ResourceIDList = make([]int32, 0, len(idStringList))
|
|
|
|
for _, idString := range idStringList {
|
|
|
|
id, err := util.ConvertStringToInt32(idString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
memo.ResourceIDList = append(memo.ResourceIDList, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if memoRelationList.Valid {
|
|
|
|
memo.RelationList = make([]*store.MemoRelation, 0)
|
|
|
|
relatedMemoTypeList := strings.Split(memoRelationList.String, ",")
|
|
|
|
for _, relatedMemoType := range relatedMemoTypeList {
|
|
|
|
relatedMemoTypeList := strings.Split(relatedMemoType, ":")
|
2023-10-01 16:35:17 +03:00
|
|
|
if len(relatedMemoTypeList) != 3 {
|
2023-09-29 08:04:54 +03:00
|
|
|
return nil, errors.New("invalid relation format")
|
2023-09-27 03:57:12 +03:00
|
|
|
}
|
2023-10-01 16:35:17 +03:00
|
|
|
memoID, err := util.ConvertStringToInt32(relatedMemoTypeList[0])
|
2023-09-27 03:57:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-01 16:35:17 +03:00
|
|
|
relatedMemoID, err := util.ConvertStringToInt32(relatedMemoTypeList[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
relationType := store.MemoRelationType(relatedMemoTypeList[2])
|
2023-09-27 03:57:12 +03:00
|
|
|
memo.RelationList = append(memo.RelationList, &store.MemoRelation{
|
2023-10-01 16:35:17 +03:00
|
|
|
MemoID: memoID,
|
2023-09-27 03:57:12 +03:00
|
|
|
RelatedMemoID: relatedMemoID,
|
2023-10-01 11:27:40 +03:00
|
|
|
Type: relationType,
|
2023-09-27 03:57:12 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list = append(list, &memo)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
|
2023-09-27 03:57:12 +03:00
|
|
|
set, args := []string{}, []any{}
|
|
|
|
if v := update.CreatedTs; v != nil {
|
|
|
|
set, args = append(set, "created_ts = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := update.UpdatedTs; v != nil {
|
|
|
|
set, args = append(set, "updated_ts = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := update.RowStatus; v != nil {
|
|
|
|
set, args = append(set, "row_status = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := update.Content; v != nil {
|
|
|
|
set, args = append(set, "content = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := update.Visibility; v != nil {
|
|
|
|
set, args = append(set, "visibility = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
args = append(args, update.ID)
|
|
|
|
|
|
|
|
stmt := `
|
|
|
|
UPDATE memo
|
|
|
|
SET ` + strings.Join(set, ", ") + `
|
|
|
|
WHERE id = ?
|
|
|
|
`
|
|
|
|
if _, err := d.db.ExecContext(ctx, stmt, args...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
|
2023-09-27 03:57:12 +03:00
|
|
|
where, args := []string{"id = ?"}, []any{delete.ID}
|
|
|
|
stmt := `DELETE FROM memo 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
|
|
|
|
}
|
|
|
|
|
2023-09-27 04:27:31 +03:00
|
|
|
if err := d.Vacuum(ctx); err != nil {
|
|
|
|
// Prevent linter warning.
|
|
|
|
return err
|
|
|
|
}
|
2023-09-27 03:57:12 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
func (d *DB) FindMemosVisibilityList(ctx context.Context, memoIDs []int32) ([]store.Visibility, error) {
|
2023-09-27 03:57:12 +03:00
|
|
|
args := make([]any, 0, len(memoIDs))
|
|
|
|
list := make([]string, 0, len(memoIDs))
|
|
|
|
for _, memoID := range memoIDs {
|
|
|
|
args = append(args, memoID)
|
|
|
|
list = append(list, "?")
|
|
|
|
}
|
|
|
|
|
|
|
|
where := fmt.Sprintf("id in (%s)", strings.Join(list, ","))
|
|
|
|
query := `SELECT DISTINCT(visibility) FROM memo WHERE ` + where
|
|
|
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
visibilityList := make([]store.Visibility, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var visibility store.Visibility
|
|
|
|
if err := rows.Scan(&visibility); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
visibilityList = append(visibilityList, visibility)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return visibilityList, nil
|
|
|
|
}
|
2023-09-27 04:27:31 +03:00
|
|
|
|
|
|
|
func vacuumMemo(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
stmt := `
|
|
|
|
DELETE FROM
|
|
|
|
memo
|
|
|
|
WHERE
|
|
|
|
creator_id NOT IN (
|
|
|
|
SELECT
|
|
|
|
id
|
|
|
|
FROM
|
|
|
|
user
|
|
|
|
)`
|
|
|
|
_, err := tx.ExecContext(ctx, stmt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|