Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

fix: create memo without some attributes (#2355)

Athurg Gootheaac17a236cf
diff --git a/store/db/mysql/memo.go b/store/db/mysql/memo.go
index 29d2c6766..f387d64f3 100644
--- a/store/db/mysql/memo.go
+++ b/store/db/mysql/memo.go
@@ -13,14 +13,36 @@ import (
)
func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
- stmt := "INSERT INTO `memo` (`creator_id`, `content`, `visibility`) VALUES (?, ?, ?)"
- result, err := d.db.ExecContext(
- ctx,
- stmt,
- create.CreatorID,
- create.Content,
- create.Visibility,
- )
+ 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, "FROM_UNIXTIME(?)")
+ args = append(args, create.CreatedTs)
+ }
+
+ if create.UpdatedTs != 0 {
+ fields = append(fields, "`updated_ts`")
+ placeholder = append(placeholder, "FROM_UNIXTIME(?)")
+ args = append(args, create.UpdatedTs)
+ }
+
+ if create.RowStatus != "" {
+ fields = append(fields, "`row_status`")
+ placeholder = append(placeholder, "?")
+ args = append(args, create.RowStatus)
+ }
+
+ stmt := "INSERT INTO memo (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
+ result, err := d.db.ExecContext(ctx, stmt, args...)
if err != nil {
return nil, err
}
diff --git a/store/db/sqlite/memo.go b/store/db/sqlite/memo.go
index d02153916..1e315cbfe 100644
--- a/store/db/sqlite/memo.go
+++ b/store/db/sqlite/memo.go
@@ -5,7 +5,6 @@ import (
"database/sql"
"fmt"
"strings"
- "time"
"github.com/pkg/errors"
@@ -14,28 +13,36 @@ import (
)
func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
- if create.CreatedTs == 0 {
- create.CreatedTs = time.Now().Unix()
+ 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)
}
- stmt := `
- INSERT INTO memo (
- creator_id,
- created_ts,
- content,
- visibility
- )
- VALUES (?, ?, ?, ?)
- RETURNING id, created_ts, updated_ts, row_status
- `
- if err := d.db.QueryRowContext(
- ctx,
- stmt,
- create.CreatorID,
- create.CreatedTs,
- create.Content,
- create.Visibility,
- ).Scan(
+ 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)
+ }
+
+ 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(
&create.ID,
&create.CreatedTs,
&create.UpdatedTs,