container.insert: use Track interface instead of concrete implementation
This commit is contained in:
parent
5669e3f37d
commit
c49a8390d7
3 changed files with 44 additions and 49 deletions
|
@ -1,16 +0,0 @@
|
|||
create table messages (
|
||||
id bigserial not null primary key,
|
||||
domain text,
|
||||
action text,
|
||||
class text,
|
||||
item text,
|
||||
sender text,
|
||||
payload jsonb,
|
||||
tstamp timestamptz default current_timestamp
|
||||
);
|
||||
|
||||
-- indexes
|
||||
|
||||
create index idx_msg on messages using btree (domain, action, class, item);
|
||||
create index idx_msg_item on messages using btree (domain, class, item);
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
create table messages (
|
||||
id integer not null primary key,
|
||||
domain text,
|
||||
action text,
|
||||
class text,
|
||||
item text,
|
||||
sender text,
|
||||
payload jsonb,
|
||||
tstamp timestamptz default current_timestamp
|
||||
);
|
||||
|
||||
-- indexes
|
||||
|
||||
create index idx_msg on messages (domain, action, class, item);
|
||||
create index idx_msg_item on messages (domain, class, item);
|
||||
|
|
@ -13,14 +13,14 @@ import (
|
|||
sql "git.sr.ht/~cco/go-scopes/storage"
|
||||
)
|
||||
|
||||
type ItemFactory func(TrackTemplate) Track
|
||||
type ItemFactory func(*TrackTemplate) Track
|
||||
|
||||
type Track interface {
|
||||
TrackId() lib.Ident
|
||||
Head() lib.StrMap
|
||||
Data() lib.Map
|
||||
TimeStamp() *time.Time
|
||||
//Update(TrackTemplate)
|
||||
Update(*TrackTemplate)
|
||||
}
|
||||
|
||||
type BaseTrack = track
|
||||
|
@ -43,6 +43,10 @@ type track struct {
|
|||
container *Container
|
||||
}
|
||||
|
||||
func MakeTrack(t *TrackTemplate) Track {
|
||||
return &track{t.TrackId, t.Head, t.TimeStamp, t.Data, t.Container}
|
||||
}
|
||||
|
||||
func (tr *track) TrackId() lib.Ident {
|
||||
return tr.trackId
|
||||
}
|
||||
|
@ -59,21 +63,41 @@ func (tr *track) TimeStamp() *time.Time {
|
|||
return tr.timeStamp
|
||||
}
|
||||
|
||||
func (tr *track) Update(t *TrackTemplate) {
|
||||
if t.TrackId != 0 {
|
||||
tr.trackId = t.TrackId
|
||||
}
|
||||
if t.Head != nil {
|
||||
tr.head = t.Head // or update map with non-empty values?
|
||||
}
|
||||
if t.TimeStamp != nil {
|
||||
tr.timeStamp = t.TimeStamp
|
||||
}
|
||||
if t.Data != nil {
|
||||
tr.data = t.Data
|
||||
}
|
||||
if t.Container != nil {
|
||||
tr.container = t.Container
|
||||
}
|
||||
}
|
||||
|
||||
// basic container implementation
|
||||
|
||||
type Container struct {
|
||||
TableName string
|
||||
HeadFields []string
|
||||
Indexes [][]string
|
||||
Storage *sql.Storage
|
||||
ItemFactory ItemFactory
|
||||
TableName string
|
||||
HeadFields []string
|
||||
Indexes [][]string
|
||||
Storage *sql.Storage
|
||||
}
|
||||
|
||||
func Tracks(db *sql.Storage) *Container {
|
||||
return &Container{
|
||||
TableName: "tracks",
|
||||
HeadFields: []string{"taskId", "userName"},
|
||||
Indexes: [][]string{[]string{"taskId", "userName"}, []string{"userName"}},
|
||||
Storage: db,
|
||||
ItemFactory: MakeTrack,
|
||||
TableName: "tracks",
|
||||
HeadFields: []string{"taskId", "userName"},
|
||||
Indexes: [][]string{[]string{"taskId", "userName"}, []string{"userName"}},
|
||||
Storage: db,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,20 +106,22 @@ func (cont *Container) New(headValues lib.StrSlice, data lib.Map) Track {
|
|||
for i, k := range cont.HeadFields {
|
||||
head[k] = headValues[i]
|
||||
}
|
||||
tr := &track{head: head, data: data}
|
||||
//tr := &track{head: head, data: data}
|
||||
tr := cont.ItemFactory(&TrackTemplate{Head: head, Data: data})
|
||||
cont.insert(tr)
|
||||
return tr
|
||||
}
|
||||
|
||||
func (cont *Container) insert(tr *track) lib.Ident {
|
||||
func (cont *Container) insert(tr Track) lib.Ident {
|
||||
var columns []string
|
||||
var values []any
|
||||
head := tr.Head()
|
||||
for _, k := range cont.HeadFields {
|
||||
columns = append(columns, k)
|
||||
values = append(values, tr.head[k])
|
||||
values = append(values, head[k])
|
||||
}
|
||||
columns = append(columns, "Data")
|
||||
b, _ := json.Marshal(tr.data)
|
||||
b, _ := json.Marshal(tr.Data())
|
||||
values = append(values, b)
|
||||
db := cont.Storage
|
||||
data := lib.Map{
|
||||
|
@ -116,9 +142,10 @@ func (cont *Container) insert(tr *track) lib.Ident {
|
|||
return err
|
||||
}
|
||||
if err := db.Query(proc, sql, values...); err == nil {
|
||||
tr.trackId = trid
|
||||
tr.timeStamp = ts
|
||||
tr.container = cont
|
||||
tr.Update(&TrackTemplate{TrackId: trid, TimeStamp: ts, Container: cont})
|
||||
//tr.trackId = trid
|
||||
//tr.timeStamp = ts
|
||||
//tr.container = cont
|
||||
return trid
|
||||
}
|
||||
return 0
|
||||
|
|
Loading…
Add table
Reference in a new issue