work in progress: generalizing container and track behaviour
This commit is contained in:
parent
a7771672be
commit
1c44b135d8
2 changed files with 24 additions and 16 deletions
|
@ -11,10 +11,17 @@ import (
|
|||
sql "git.sr.ht/~cco/go-scopes/storage"
|
||||
)
|
||||
|
||||
type ItemFactory func() Track
|
||||
|
||||
type Track interface {
|
||||
TrackId() lib.Ident
|
||||
}
|
||||
|
||||
type Container interface {
|
||||
New(lib.StrSlice, lib.Map)
|
||||
CreateTable()
|
||||
}
|
||||
|
||||
type BaseTrack = track
|
||||
type BaseContainer = container
|
||||
|
||||
|
@ -28,13 +35,6 @@ type track struct {
|
|||
container *container
|
||||
}
|
||||
|
||||
func SimpleTrack(taskId, userName string, data lib.Map) track {
|
||||
return track{
|
||||
head: lib.StrMap{"taskId": taskId, "userName": userName},
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func (tr *track) TrackId() lib.Ident {
|
||||
return tr.trackId
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ type container struct {
|
|||
storage *sql.Storage
|
||||
}
|
||||
|
||||
func SimpleContainer(db *sql.Storage) *container {
|
||||
func Tracks(db *sql.Storage) *container {
|
||||
return &container{
|
||||
tableName: "tracks",
|
||||
headFields: []string{"taskId", "userName"},
|
||||
|
@ -57,7 +57,17 @@ func SimpleContainer(db *sql.Storage) *container {
|
|||
}
|
||||
}
|
||||
|
||||
func (cont *container) Insert(tr *track) lib.Ident {
|
||||
func (cont *container) New(headValues lib.StrSlice, data lib.Map) Track {
|
||||
head := lib.StrMap{}
|
||||
for i, k := range cont.headFields {
|
||||
head[k] = headValues[i]
|
||||
}
|
||||
tr := &track{head: head, data: data}
|
||||
cont.insert(tr)
|
||||
return tr
|
||||
}
|
||||
|
||||
func (cont *container) insert(tr *track) lib.Ident {
|
||||
var columns []string
|
||||
var values []any
|
||||
for _, k := range cont.headFields {
|
||||
|
@ -74,7 +84,7 @@ func (cont *container) Insert(tr *track) lib.Ident {
|
|||
"columns": columns,
|
||||
}
|
||||
sql := storage.BuildSql(SqlInsert, data)
|
||||
if res := storage.QueryCol[uint](db, sql, values...); res != nil {
|
||||
if res := storage.QueryCol[lib.Ident](db, sql, values...); res != nil {
|
||||
trid := lib.Ident(res[0])
|
||||
tr.trackId = trid
|
||||
tr.container = cont
|
||||
|
@ -83,7 +93,7 @@ func (cont *container) Insert(tr *track) lib.Ident {
|
|||
return 0
|
||||
}
|
||||
|
||||
func (cont *container) CreateTable() *container {
|
||||
func (cont *container) CreateTable() {
|
||||
db := cont.storage
|
||||
data := lib.Map{
|
||||
"schema": db.Schema,
|
||||
|
@ -93,8 +103,7 @@ func (cont *container) CreateTable() *container {
|
|||
"params": db.Params,
|
||||
}
|
||||
sql := storage.BuildSql(SqlCreate, data)
|
||||
if _, err := db.Exec(sql); err == nil {
|
||||
return cont
|
||||
if _, err := db.Exec(sql); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -68,8 +68,7 @@ func BaseTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
|||
func TrackingTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
||||
cont := tracking.SimpleContainer(db)
|
||||
cont.CreateTable()
|
||||
track := tracking.SimpleTrack("t01", "john", lib.Map{})
|
||||
cont.Insert(&track)
|
||||
track := cont.New(lib.StrSlice{"t01", "john"}, lib.Map{})
|
||||
t.AssertEqual(track.TrackId(), lib.Ident(1))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue