Skip to content

Commit

Permalink
Add simple support for locking reads, FOR UPDATE clause
Browse files Browse the repository at this point in the history
  • Loading branch information
kolkov committed May 1, 2023
1 parent 5942e1e commit 99e91be
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
12 changes: 12 additions & 0 deletions query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type QueryBuilder interface {
BuildOrderByAndLimit(string, []string, int64, int64) string
// BuildUnion generates a UNION clause from the given union information.
BuildUnion([]UnionInfo, Params) string
// BuildFor generates a FOR UPDATE clause from the given expression
BuildFor(option string) string
}

// BaseQueryBuilder provides a basic implementation of QueryBuilder.
Expand Down Expand Up @@ -234,6 +236,16 @@ func (q *BaseQueryBuilder) BuildLimit(limit int64, offset int64) string {
return sql + fmt.Sprintf("OFFSET %v", offset)
}

func (q *BaseQueryBuilder) BuildFor(option string) string {
s := ""
if option != "" {
s += option
} else {
s += "UPDATE "
}
return "FOR " + s
}

func (q *BaseQueryBuilder) quoteTableNameAndAlias(table string) string {
matches := selectRegex.FindStringSubmatch(table)
if len(matches) == 0 {
Expand Down
44 changes: 26 additions & 18 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ type SelectQuery struct {
builder Builder
ctx context.Context

selects []string
distinct bool
selectOption string
from []string
where Expression
join []JoinInfo
orderBy []string
groupBy []string
having Expression
union []UnionInfo
limit int64
offset int64
params Params
selects []string
distinct bool
selectOption string
from []string
where Expression
join []JoinInfo
orderBy []string
groupBy []string
having Expression
union []UnionInfo
limit int64
offset int64
params Params
lockingOption string
}

// JoinInfo contains the specification for a JOIN clause.
Expand Down Expand Up @@ -68,14 +69,14 @@ func NewSelectQuery(builder Builder, db *DB) *SelectQuery {
}

// Context returns the context associated with the query.
func (q *SelectQuery) Context() context.Context {
return q.ctx
func (s *SelectQuery) Context() context.Context {
return s.ctx
}

// WithContext associates a context with the query.
func (q *SelectQuery) WithContext(ctx context.Context) *SelectQuery {
q.ctx = ctx
return q
func (s *SelectQuery) WithContext(ctx context.Context) *SelectQuery {
s.ctx = ctx
return s
}

// Select specifies the columns to be selected.
Expand Down Expand Up @@ -245,6 +246,12 @@ func (s *SelectQuery) AndBind(params Params) *SelectQuery {
return s
}

// For appends the FOR UPDATE or FOR SHARING clause
func (s *SelectQuery) For(option string) *SelectQuery {
s.lockingOption = option
return s
}

// Build builds the SELECT query and returns an executable Query object.
func (s *SelectQuery) Build() *Query {
params := Params{}
Expand All @@ -261,6 +268,7 @@ func (s *SelectQuery) Build() *Query {
qb.BuildWhere(s.where, params),
qb.BuildGroupBy(s.groupBy),
qb.BuildHaving(s.having, params),
qb.BuildFor(s.lockingOption),
}
sql := ""
for _, clause := range clauses {
Expand Down

0 comments on commit 99e91be

Please sign in to comment.