Skip to content
Snippets Groups Projects
Commit 9f3fbd52bc11 authored by Christophe de Vienne's avatar Christophe de Vienne
Browse files

SQLHelper: add 'ForUpdate' versions of getter functions

parent cbb304751ba8
No related branches found
No related tags found
No related merge requests found
Pipeline #51859 failed
...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
return h.sqle.GetContext(h.ctx, obj, sql, args...) 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 { func (h *SQLHelper) GetByPKey(obj Mapped, value interface{}) error {
return h.GetBy(obj, obj.PKeyColumn(), value) return h.GetBy(obj, obj.PKeyColumn(), value)
} }
...@@ -35,9 +35,15 @@ ...@@ -35,9 +35,15 @@
func (h *SQLHelper) GetByPKey(obj Mapped, value interface{}) error { func (h *SQLHelper) GetByPKey(obj Mapped, value interface{}) error {
return h.GetBy(obj, obj.PKeyColumn(), value) 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 { func (h *SQLHelper) GetBy(obj Mapped, column string, value interface{}) error {
return h.GetWhere(obj, sq.Eq{column: value}) return h.GetWhere(obj, sq.Eq{column: value})
} }
...@@ -40,7 +46,13 @@ ...@@ -40,7 +46,13 @@
func (h *SQLHelper) GetBy(obj Mapped, column string, value interface{}) error { func (h *SQLHelper) GetBy(obj Mapped, column string, value interface{}) error {
return h.GetWhere(obj, sq.Eq{column: value}) 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 // GetWhere loads a mapped structure
func (h *SQLHelper) GetWhere(obj Mapped, pred interface{}, args ...interface{}) error { func (h *SQLHelper) GetWhere(obj Mapped, pred interface{}, args ...interface{}) error {
query := SQ. query := SQ.
...@@ -51,6 +63,18 @@ ...@@ -51,6 +63,18 @@
return h.Get(obj, query) 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 // Select loads a structure list
func (h *SQLHelper) Select(obj interface{}, query sq.Sqlizer) error { func (h *SQLHelper) Select(obj interface{}, query sq.Sqlizer) error {
return SelectContext(h.ctx, h.sqle, obj, query, &h.log) return SelectContext(h.ctx, h.sqle, obj, query, &h.log)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment