storage: minor improvements, helper methods (DropTable, RunScript), Cfg: +Schema
This commit is contained in:
parent
e8a6696bcb
commit
064c442d4a
5 changed files with 32 additions and 18 deletions
|
@ -9,7 +9,6 @@ import (
|
|||
|
||||
func Store(db *sql.Storage, msg lib.Message) {
|
||||
q := fmt.Sprintf(sql_insert_msg, "messages")
|
||||
fmt.Println(q)
|
||||
db.Exec(q, msg.Domain(), msg.Action(), msg.Class(), msg.Item(), msg.Payload())
|
||||
}
|
||||
|
||||
|
|
|
@ -13,5 +13,4 @@ create table messages (
|
|||
|
||||
create index idx_msg on messages using btree (domain, action, class, item);
|
||||
create index idx_msg_item on messages using btree (domain, class, item);
|
||||
--create index idx_msg_action on messages using btree (action); -- obsolete
|
||||
|
||||
|
|
|
@ -13,5 +13,4 @@ create table messages (
|
|||
|
||||
create index idx_msg on messages (domain, action, class, item);
|
||||
create index idx_msg_item on messages (domain, class, item);
|
||||
--create index idx_msg_action on messages using btree (action); -- obsolete
|
||||
|
||||
|
|
|
@ -2,16 +2,19 @@ package sql
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.sr.ht/~cco/go-scopes/logging/log"
|
||||
)
|
||||
|
||||
type Cfg struct {
|
||||
Driver, Connstr string
|
||||
Driver, Connstr, Schema string
|
||||
}
|
||||
|
||||
type Storage struct {
|
||||
*sql.DB
|
||||
*Cfg
|
||||
Errors []error
|
||||
}
|
||||
|
||||
|
@ -31,7 +34,7 @@ func Open(cfg *Cfg) *Storage {
|
|||
log.Error(err).Msg("sql.Open")
|
||||
return nil
|
||||
}
|
||||
return &Storage{DB: db}
|
||||
return &Storage{DB: db, Cfg: cfg}
|
||||
}
|
||||
|
||||
func QueryData[T scn[T]](db *Storage, q string, args ...interface{}) []T {
|
||||
|
@ -100,8 +103,26 @@ func (db *Storage) Exec(q string, args ...interface{}) (int64, error) {
|
|||
return nrows, nil
|
||||
}
|
||||
|
||||
func (db *Storage) DropTable(tn string) error {
|
||||
_, err := db.Exec(fmt.Sprintf(sql_drop, tn))
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *Storage) RunScript(path string) error {
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return db.logErr(err, "sql.Storage.RunScript", path)
|
||||
}
|
||||
_, err = db.Exec(string(b))
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *Storage) logErr(err error, info, q string) error {
|
||||
log.Error(err).Str("query", q).Msg(info)
|
||||
db.Errors = append(db.Errors, err)
|
||||
return err
|
||||
}
|
||||
|
||||
const (
|
||||
sql_drop = `drop table %s`
|
||||
)
|
||||
|
|
|
@ -66,35 +66,31 @@ func MsgstoreTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
|||
}
|
||||
|
||||
func resetSqlite(db *sql.Storage) {
|
||||
db.Exec(drop_table)
|
||||
db.DropTable("test")
|
||||
db.Exec(sqlite_create_table)
|
||||
}
|
||||
|
||||
func resetPgsql(db *sql.Storage) {
|
||||
db.Exec(drop_table)
|
||||
db.DropTable("test")
|
||||
db.Exec(pgsql_create_table)
|
||||
}
|
||||
|
||||
// SQL statements
|
||||
|
||||
const (
|
||||
drop_table = `drop table test`
|
||||
|
||||
sql_insert = `insert into test (label) values
|
||||
('Hello World'),
|
||||
('Good Afternoon')`
|
||||
('Hello World'),
|
||||
('Good Afternoon')`
|
||||
|
||||
sql_query = `select id, label from test `
|
||||
sql_query = `select id, label from test ` //where id = $1`
|
||||
|
||||
//where id = $1`
|
||||
// database-specific SQL statements
|
||||
|
||||
// note: ... integer ... primary key ... automatically fills row sequentially
|
||||
sqlite_create_table = `create table test (
|
||||
id integer not null primary key,
|
||||
label text)`
|
||||
id integer not null primary key,
|
||||
label text)`
|
||||
|
||||
pgsql_create_table = `create table test (
|
||||
id serial not null primary key,
|
||||
label text)`
|
||||
id serial not null primary key,
|
||||
label text)`
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue