mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* add query builder to facilitate dynamic SQL construction * leverage query builder to refactor user store This is partially setup work for MM-13120, but merged to master to avoid further conflicts. * fix unrelated unit test breakage * documentation tweaks * Apply suggestions from code review Co-Authored-By: lieut-data <jesse.hallam@gmail.com> * prefer comma separated case options to fallthrough * vendor github.com/Masterminds/squirrel and deps * switch to using github.com/Masterminds/squirrel * rm querybuilder
75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
// +build go1.8
|
|
|
|
package squirrel
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
// PrepareerContext is the interface that wraps the Prepare and PrepareContext methods.
|
|
//
|
|
// Prepare executes the given query as implemented by database/sql.Prepare.
|
|
// PrepareContext executes the given query as implemented by database/sql.PrepareContext.
|
|
type PreparerContext interface {
|
|
Preparer
|
|
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
|
|
}
|
|
|
|
// DBProxyContext groups the Execer, Queryer, QueryRower and PreparerContext interfaces.
|
|
type DBProxyContext interface {
|
|
Execer
|
|
Queryer
|
|
QueryRower
|
|
PreparerContext
|
|
}
|
|
|
|
// NewStmtCacher returns a DBProxy wrapping prep that caches Prepared Stmts.
|
|
//
|
|
// Stmts are cached based on the string value of their queries.
|
|
func NewStmtCacher(prep PreparerContext) DBProxyContext {
|
|
return &stmtCacher{prep: prep, cache: make(map[string]*sql.Stmt)}
|
|
}
|
|
|
|
func (sc *stmtCacher) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
|
|
ctxPrep, ok := sc.prep.(PreparerContext)
|
|
if !ok {
|
|
return nil, NoContextSupport
|
|
}
|
|
sc.mu.Lock()
|
|
defer sc.mu.Unlock()
|
|
stmt, ok := sc.cache[query]
|
|
if ok {
|
|
return stmt, nil
|
|
}
|
|
stmt, err := ctxPrep.PrepareContext(ctx, query)
|
|
if err == nil {
|
|
sc.cache[query] = stmt
|
|
}
|
|
return stmt, err
|
|
}
|
|
|
|
func (sc *stmtCacher) ExecContext(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error) {
|
|
stmt, err := sc.PrepareContext(ctx, query)
|
|
if err != nil {
|
|
return
|
|
}
|
|
return stmt.ExecContext(ctx, args...)
|
|
}
|
|
|
|
func (sc *stmtCacher) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
|
|
stmt, err := sc.PrepareContext(ctx, query)
|
|
if err != nil {
|
|
return
|
|
}
|
|
return stmt.QueryContext(ctx, args...)
|
|
}
|
|
|
|
func (sc *stmtCacher) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
|
|
stmt, err := sc.PrepareContext(ctx, query)
|
|
if err != nil {
|
|
return &Row{err: err}
|
|
}
|
|
return stmt.QueryRowContext(ctx, args...)
|
|
}
|