# HG changeset patch
# User Christophe de Vienne <christophe@cdevienne.info>
# Date 1678281366 -3600
#      Wed Mar 08 14:16:06 2023 +0100
# Node ID 9f3fbd52bc119db9a377119c4a614f2b1d3e7843
# Parent  cbb304751ba80580514ead1e2ecc488e482213af
SQLHelper: add 'ForUpdate' versions of getter functions

diff --git a/database/sql_helper.go b/database/sql_helper.go
--- a/database/sql_helper.go
+++ b/database/sql_helper.go
@@ -31,16 +31,28 @@
 	return h.sqle.GetContext(h.ctx, obj, sql, args...)
 }
 
-// GetByPKey loads a mapped structure
+// GetByPKey loads a mapped structure by primary key
 func (h *SQLHelper) GetByPKey(obj Mapped, value interface{}) error {
 	return h.GetBy(obj, obj.PKeyColumn(), value)
 }
 
-// GetBy ...
+// GetByPKeyForUpdate loads a mapped structure by primary key and get a lock
+// (see https://www.postgresql.org/docs/current/sql-select.html#SQL-FOR-UPDATE-SHARE)
+func (h *SQLHelper) GetByPKeyForUpdate(obj Mapped, value interface{}) error {
+	return h.GetByForUpdate(obj, obj.PKeyColumn(), value)
+}
+
+// GetBy loads a mapped structure by a given column
 func (h *SQLHelper) GetBy(obj Mapped, column string, value interface{}) error {
 	return h.GetWhere(obj, sq.Eq{column: value})
 }
 
+// GetBy loads a mapped structure by a given column and get a lock
+// (see https://www.postgresql.org/docs/current/sql-select.html#SQL-FOR-UPDATE-SHARE)
+func (h *SQLHelper) GetByForUpdate(obj Mapped, column string, value interface{}) error {
+	return h.GetWhereForUpdate(obj, sq.Eq{column: value})
+}
+
 // GetWhere loads a mapped structure
 func (h *SQLHelper) GetWhere(obj Mapped, pred interface{}, args ...interface{}) error {
 	query := SQ.
@@ -51,6 +63,18 @@
 	return h.Get(obj, query)
 }
 
+// GetWhere loads a mapped structure and get a lock
+// (see https://www.postgresql.org/docs/current/sql-select.html#SQL-FOR-UPDATE-SHARE)
+func (h *SQLHelper) GetWhereForUpdate(obj Mapped, pred interface{}, args ...interface{}) error {
+	query := SQ.
+		Select(obj.Columns(true)...).
+		From(obj.Table()).
+		Where(pred, args...).
+		Suffix("FOR UPDATE")
+
+	return h.Get(obj, query)
+}
+
 // Select loads a structure list
 func (h *SQLHelper) Select(obj interface{}, query sq.Sqlizer) error {
 	return SelectContext(h.ctx, h.sqle, obj, query, &h.log)