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"
|
sql "git.sr.ht/~cco/go-scopes/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ItemFactory func() Track
|
||||||
|
|
||||||
type Track interface {
|
type Track interface {
|
||||||
TrackId() lib.Ident
|
TrackId() lib.Ident
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Container interface {
|
||||||
|
New(lib.StrSlice, lib.Map)
|
||||||
|
CreateTable()
|
||||||
|
}
|
||||||
|
|
||||||
type BaseTrack = track
|
type BaseTrack = track
|
||||||
type BaseContainer = container
|
type BaseContainer = container
|
||||||
|
|
||||||
|
@ -28,13 +35,6 @@ type track struct {
|
||||||
container *container
|
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 {
|
func (tr *track) TrackId() lib.Ident {
|
||||||
return tr.trackId
|
return tr.trackId
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ type container struct {
|
||||||
storage *sql.Storage
|
storage *sql.Storage
|
||||||
}
|
}
|
||||||
|
|
||||||
func SimpleContainer(db *sql.Storage) *container {
|
func Tracks(db *sql.Storage) *container {
|
||||||
return &container{
|
return &container{
|
||||||
tableName: "tracks",
|
tableName: "tracks",
|
||||||
headFields: []string{"taskId", "userName"},
|
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 columns []string
|
||||||
var values []any
|
var values []any
|
||||||
for _, k := range cont.headFields {
|
for _, k := range cont.headFields {
|
||||||
|
@ -74,7 +84,7 @@ func (cont *container) Insert(tr *track) lib.Ident {
|
||||||
"columns": columns,
|
"columns": columns,
|
||||||
}
|
}
|
||||||
sql := storage.BuildSql(SqlInsert, data)
|
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])
|
trid := lib.Ident(res[0])
|
||||||
tr.trackId = trid
|
tr.trackId = trid
|
||||||
tr.container = cont
|
tr.container = cont
|
||||||
|
@ -83,7 +93,7 @@ func (cont *container) Insert(tr *track) lib.Ident {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cont *container) CreateTable() *container {
|
func (cont *container) CreateTable() {
|
||||||
db := cont.storage
|
db := cont.storage
|
||||||
data := lib.Map{
|
data := lib.Map{
|
||||||
"schema": db.Schema,
|
"schema": db.Schema,
|
||||||
|
@ -93,8 +103,7 @@ func (cont *container) CreateTable() *container {
|
||||||
"params": db.Params,
|
"params": db.Params,
|
||||||
}
|
}
|
||||||
sql := storage.BuildSql(SqlCreate, data)
|
sql := storage.BuildSql(SqlCreate, data)
|
||||||
if _, err := db.Exec(sql); err == nil {
|
if _, err := db.Exec(sql); err != nil {
|
||||||
return cont
|
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) {
|
func TrackingTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
||||||
cont := tracking.SimpleContainer(db)
|
cont := tracking.SimpleContainer(db)
|
||||||
cont.CreateTable()
|
cont.CreateTable()
|
||||||
track := tracking.SimpleTrack("t01", "john", lib.Map{})
|
track := cont.New(lib.StrSlice{"t01", "john"}, lib.Map{})
|
||||||
cont.Insert(&track)
|
|
||||||
t.AssertEqual(track.TrackId(), lib.Ident(1))
|
t.AssertEqual(track.TrackId(), lib.Ident(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue