From aa02f9fc48ee5a44586816997030028e050a5a3f Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 27 Apr 2021 20:24:04 +1000 Subject: [PATCH] Add Count() Add Createf() Fix inqbuild2() loop. --- common/common.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/common/common.go b/common/common.go index 97d38b71..950c1be4 100644 --- a/common/common.go +++ b/common/common.go @@ -208,13 +208,28 @@ func Logf(str string, args ...interface{}) { log.Printf(str, args...) } -func Countf(stmt *sql.Stmt, args ...interface{}) (count int) { - err := stmt.QueryRow(args...).Scan(&count) - if err != nil { - LogError(err) +func Count(stmt *sql.Stmt) (count int) { + e := stmt.QueryRow().Scan(&count) + if e != nil { + LogError(e) } return count } +func Countf(stmt *sql.Stmt, args ...interface{}) (count int) { + e := stmt.QueryRow(args...).Scan(&count) + if e != nil { + LogError(e) + } + return count +} +func Createf(stmt *sql.Stmt, args ...interface{}) (id int, e error) { + res, e := stmt.Exec(args...) + if e != nil { + return 0, e + } + id64, e := res.LastInsertId() + return int(id64), e +} func eachall(stmt *sql.Stmt, f func(r *sql.Rows) error) error { rows, e := stmt.Query() @@ -261,7 +276,7 @@ func inqbuild2(count int) string { } var sb strings.Builder sb.Grow((count * 2) - 1) - for i := 0; i <= count; i++ { + for i := 0; i < count; i++ { if i == 0 { sb.WriteRune('?') } else {