storage: sql.Exec() OK

This commit is contained in:
Helmut Merz 2023-08-15 16:45:54 +02:00
parent f1ce07fd7e
commit d7096500ce
3 changed files with 25 additions and 5 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
bin* bin*
*.log *.log
*.sqlite

View file

@ -23,5 +23,14 @@ func Open(cfg *Cfg) *Storage {
return &Storage{db} return &Storage{db}
} }
func Exec(db *Storage, s string) { func (db *Storage) Exec(q string, args ...interface{}) (int64, error) {
info := "sql.Exec"
//log.Debug().Str("query", q).Msg(info)
res, err := db.DB.Exec(q, args...)
if err != nil {
log.Error(err).Str("query", q).Msg(info)
return 0, err
}
nrows, _ := res.RowsAffected()
return nrows, nil
} }

View file

@ -1,7 +1,6 @@
package scopes package scopes
import ( import (
"fmt"
tbase "testing" tbase "testing"
"git.sr.ht/~cco/go-scopes/common/testing" "git.sr.ht/~cco/go-scopes/common/testing"
@ -15,7 +14,7 @@ func TestSqlite(tb *tbase.T) {
t := testing.SetUp(tb) t := testing.SetUp(tb)
cfg := etc.ConfigSqlite() cfg := etc.ConfigSqlite()
db := sql.Open(cfg) db := sql.Open(cfg)
// resetSqlite(db) resetSqlite(db)
DoTests(t, cfg, db) DoTests(t, cfg, db)
} }
@ -23,7 +22,7 @@ func TestPgsql(tb *tbase.T) {
t := testing.SetUp(tb) t := testing.SetUp(tb)
cfg := etc.ConfigPgsql() cfg := etc.ConfigPgsql()
db := sql.Open(cfg) db := sql.Open(cfg)
// resetPgsql(db) resetPgsql(db)
DoTests(t, cfg, db) DoTests(t, cfg, db)
} }
@ -32,7 +31,18 @@ func DoTests(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
} }
func BaseTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) { func BaseTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
fmt.Println(cfg.Connstr) //fmt.Println(cfg.Connstr)
db.Exec(insert)
}
func resetSqlite(db *sql.Storage) {
db.Exec(drop_table)
db.Exec(sqlite_create_table)
}
func resetPgsql(db *sql.Storage) {
db.Exec(drop_table)
db.Exec(pgsql_create_table)
} }
// SQL statements // SQL statements