Skip to content

Commit

Permalink
replace with sqlParams
Browse files Browse the repository at this point in the history
  • Loading branch information
noisersup committed Jul 27, 2022
1 parent 8027719 commit 40befc6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/handlers/pg/msg_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (h *Handler) MsgUpdate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg,
func (h *Handler) update(ctx context.Context, sp pgdb.SQLParam, doc *types.Document) (int64, error) {
id := must.NotFail(doc.Get("_id"))

rowsUpdated, err := h.pgPool.SetDocumentByID(ctx, sp.DB, sp.Collection, sp.Comment, id, doc)
rowsUpdated, err := h.pgPool.SetDocumentByID(ctx, sp, id, doc)
if err != nil {
return 0, err
}
Expand Down
14 changes: 7 additions & 7 deletions internal/handlers/pg/pgdb/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,23 +263,23 @@ func (pgPool *Pool) SchemaStats(ctx context.Context, schema, collection string)

// SetDocumentByID sets a document by its ID.
func (pgPool *Pool) SetDocumentByID(
ctx context.Context, db, collection, comment string, id any, doc *types.Document,
ctx context.Context, sp SQLParam, id any, doc *types.Document,
) (int64, error) {
var tag pgconn.CommandTag
err := pgPool.InTransaction(ctx, func(tx pgx.Tx) error {
table, err := getTableName(ctx, tx, db, collection)
table, err := getTableName(ctx, tx, sp.DB, sp.Collection)
if err != nil {
return err
}

sql := "UPDATE "
if comment != "" {
comment = strings.ReplaceAll(comment, "/*", "/ *")
comment = strings.ReplaceAll(comment, "*/", "* /")
if sp.Comment != "" {
sp.Comment = strings.ReplaceAll(sp.Comment, "/*", "/ *")
sp.Comment = strings.ReplaceAll(sp.Comment, "*/", "* /")

sql += `/* ` + comment + ` */ `
sql += `/* ` + sp.Comment + ` */ `
}
sql += pgx.Identifier{db, table}.Sanitize() +
sql += pgx.Identifier{sp.DB, table}.Sanitize() +
" SET _jsonb = $1 WHERE _jsonb->'_id' = $2"

tag, err = tx.Exec(ctx, sql, must.NotFail(fjson.Marshal(doc)), must.NotFail(fjson.Marshal(id)))
Expand Down

0 comments on commit 40befc6

Please sign in to comment.