chore: fix postgres driver

This commit is contained in:
Steven
2023-12-17 20:07:25 +08:00
parent a7d48e8059
commit 4c66edc170
18 changed files with 104 additions and 200 deletions

View File

@ -2,7 +2,6 @@ package postgres
import (
"context"
"database/sql"
"fmt"
"strings"
@ -17,9 +16,7 @@ func (d *DB) CreateMemo(ctx context.Context, create *storepb.Memo) (*storepb.Mem
args := []any{create.CreatorId, create.Name, create.Title, create.Content, create.Visibility.String(), strings.Join(create.Tags, " ")}
stmt := `
INSERT INTO memo (
` + strings.Join(set, ", ") + `
)
INSERT INTO memo (` + strings.Join(set, ", ") + `)
VALUES (` + placeholders(len(args)) + `)
RETURNING id, created_ts, updated_ts, row_status
`
@ -41,43 +38,34 @@ func (d *DB) CreateMemo(ctx context.Context, create *storepb.Memo) (*storepb.Mem
func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) (*storepb.Memo, error) {
set, args := []string{}, []any{}
if update.RowStatus != nil {
set = append(set, fmt.Sprintf("row_status = $%d", len(set)+1))
args = append(args, update.RowStatus.String())
set, args = append(set, "row_status = "+placeholder(len(args)+1)), append(args, update.RowStatus.String())
}
if update.Name != nil {
set = append(set, fmt.Sprintf("name = $%d", len(set)+1))
args = append(args, *update.Name)
set, args = append(set, "name = "+placeholder(len(args)+1)), append(args, *update.Name)
}
if update.Title != nil {
set = append(set, fmt.Sprintf("title = $%d", len(set)+1))
args = append(args, *update.Title)
set, args = append(set, "title = "+placeholder(len(args)+1)), append(args, *update.Title)
}
if update.Content != nil {
set = append(set, fmt.Sprintf("content = $%d", len(set)+1))
args = append(args, *update.Content)
set, args = append(set, "content = "+placeholder(len(args)+1)), append(args, *update.Content)
}
if update.Visibility != nil {
set = append(set, fmt.Sprintf("visibility = $%d", len(set)+1))
args = append(args, update.Visibility.String())
set, args = append(set, "visibility = "+placeholder(len(args)+1)), append(args, update.Visibility.String())
}
if update.Tag != nil {
set = append(set, fmt.Sprintf("tag = $%d", len(set)+1))
args = append(args, *update.Tag)
set, args = append(set, "tag = "+placeholder(len(args)+1)), append(args, *update.Tag)
}
if len(set) == 0 {
return nil, errors.New("no update specified")
}
args = append(args, update.ID)
stmt := `
UPDATE memo
SET
` + strings.Join(set, ", ") + `
WHERE
id = $` + fmt.Sprint(len(set)+1) + `
SET ` + strings.Join(set, ", ") + `
WHERE id = ` + placeholder(len(args)+1) + `
RETURNING id, creator_id, created_ts, updated_ts, row_status, name, title, content, visibility, tag
`
args = append(args, update.ID)
memo := &storepb.Memo{}
var rowStatus, visibility, tags string
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
@ -103,27 +91,26 @@ func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) (*storepb
func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*storepb.Memo, error) {
where, args := []string{"1 = 1"}, []any{}
if v := find.ID; v != nil {
where, args = append(where, "id = $1"), append(args, *v)
where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.CreatorID; v != nil {
where, args = append(where, "creator_id = $2"), append(args, *v)
where, args = append(where, "creator_id = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.RowStatus; v != nil {
where, args = append(where, "row_status = $3"), append(args, *v)
where, args = append(where, "row_status = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.Name; v != nil {
where, args = append(where, "name = $4"), append(args, *v)
where, args = append(where, "name = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.VisibilityList; len(v) != 0 {
list := []string{}
for i, visibility := range v {
list = append(list, fmt.Sprintf("$%d", len(args)+i+1))
args = append(args, visibility)
for _, visibility := range v {
list, args = append(list, placeholder(len(args)+1)), append(args, visibility)
}
where = append(where, fmt.Sprintf("visibility IN (%s)", strings.Join(list, ",")))
}
if v := find.Tag; v != nil {
where, args = append(where, "tag LIKE $"+fmt.Sprint(len(args)+1)), append(args, "%"+*v+"%")
where, args = append(where, "tag LIKE "+placeholder(len(args)+1)), append(args, "%"+*v+"%")
}
rows, err := d.db.QueryContext(ctx, `
@ -185,24 +172,10 @@ func (d *DB) DeleteMemo(ctx context.Context, delete *store.DeleteMemo) error {
return nil
}
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
}
func placeholders(n int) string {
placeholder := ""
list := []string{}
for i := 0; i < n; i++ {
if i == 0 {
placeholder = fmt.Sprintf("$%d", i+1)
} else {
placeholder = fmt.Sprintf("%s, $%d", placeholder, i+1)
}
list = append(list, fmt.Sprintf("$%d", i+1))
}
return placeholder
return strings.Join(list, ", ")
}