diff --git a/database/sql_helper.go b/database/sql_helper.go index cbb304751ba80580514ead1e2ecc488e482213af_ZGF0YWJhc2Uvc3FsX2hlbHBlci5nbw==..9f3fbd52bc119db9a377119c4a614f2b1d3e7843_ZGF0YWJhc2Uvc3FsX2hlbHBlci5nbw== 100644 --- a/database/sql_helper.go +++ b/database/sql_helper.go @@ -31,8 +31,8 @@ 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) } @@ -35,9 +35,15 @@ 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}) } @@ -40,7 +46,13 @@ 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)