Remove duplicated database logic

This commit is contained in:
Jan-Lukas Else 2021-12-12 10:36:47 +01:00
parent f82aad4fbf
commit 787e5fb807
1 changed files with 10 additions and 34 deletions

View File

@ -147,7 +147,10 @@ func (db *database) close() error {
return db.db.Close() return db.db.Close()
} }
func (db *database) prepare(query string) (*sql.Stmt, error) { func (db *database) prepare(query string, args ...interface{}) (*sql.Stmt, []interface{}, error) {
if len(args) > 0 && args[0] == dbNoCache {
return nil, args[1:], nil
}
stmt, err, _ := db.sg.Do(query, func() (interface{}, error) { stmt, err, _ := db.sg.Do(query, func() (interface{}, error) {
// Look if statement already exists // Look if statement already exists
st, ok := db.ps.Load(query) st, ok := db.ps.Load(query)
@ -167,28 +170,19 @@ func (db *database) prepare(query string) (*sql.Stmt, error) {
if db.debug { if db.debug {
log.Printf(`Failed to prepare query "%s": %s`, query, err.Error()) log.Printf(`Failed to prepare query "%s": %s`, query, err.Error())
} }
return nil, err return nil, args, err
} }
return stmt.(*sql.Stmt), nil return stmt.(*sql.Stmt), args, nil
} }
const dbNoCache = "nocache" const dbNoCache = "nocache"
func (db *database) exec(query string, args ...interface{}) (sql.Result, error) { func (db *database) exec(query string, args ...interface{}) (sql.Result, error) {
// Maybe prepare
st, args, _ := db.prepare(query, args...)
// Lock execution // Lock execution
db.em.Lock() db.em.Lock()
defer db.em.Unlock() defer db.em.Unlock()
// Check if no cache arg set
cache := true
if len(args) > 0 && args[0] == dbNoCache {
cache = false
args = args[1:]
}
// Maybe prepare
var st *sql.Stmt
if cache {
st, _ = db.prepare(query)
}
// Prepare context, call hook // Prepare context, call hook
ctx := db.dbBefore(context.Background(), query, args...) ctx := db.dbBefore(context.Background(), query, args...)
defer db.dbAfter(ctx, query, args...) defer db.dbAfter(ctx, query, args...)
@ -200,17 +194,8 @@ func (db *database) exec(query string, args ...interface{}) (sql.Result, error)
} }
func (db *database) query(query string, args ...interface{}) (*sql.Rows, error) { func (db *database) query(query string, args ...interface{}) (*sql.Rows, error) {
// Check if no cache arg set
cache := true
if len(args) > 0 && args[0] == dbNoCache {
cache = false
args = args[1:]
}
// Maybe prepare // Maybe prepare
var st *sql.Stmt st, args, _ := db.prepare(query, args...)
if cache {
st, _ = db.prepare(query)
}
// Prepare context, call hook // Prepare context, call hook
ctx := db.dbBefore(context.Background(), query, args...) ctx := db.dbBefore(context.Background(), query, args...)
defer db.dbAfter(ctx, query, args...) defer db.dbAfter(ctx, query, args...)
@ -222,17 +207,8 @@ func (db *database) query(query string, args ...interface{}) (*sql.Rows, error)
} }
func (db *database) queryRow(query string, args ...interface{}) (*sql.Row, error) { func (db *database) queryRow(query string, args ...interface{}) (*sql.Row, error) {
// Check if no cache arg set
cache := true
if len(args) > 0 && args[0] == dbNoCache {
cache = false
args = args[1:]
}
// Maybe prepare // Maybe prepare
var st *sql.Stmt st, args, _ := db.prepare(query, args...)
if cache {
st, _ = db.prepare(query)
}
// Prepare context, call hook // Prepare context, call hook
ctx := db.dbBefore(context.Background(), query, args...) ctx := db.dbBefore(context.Background(), query, args...)
defer db.dbAfter(ctx, query, args...) defer db.dbAfter(ctx, query, args...)