use simple item factory with head fields only
This commit is contained in:
parent
40661d549d
commit
191b2a874a
4 changed files with 28 additions and 27 deletions
|
@ -14,11 +14,12 @@ func Message(tr tracking.Track) *message {
|
|||
return &message{tr}
|
||||
}
|
||||
|
||||
func MakeMessage(t *tracking.TrackTemplate, c *tracking.Container) tracking.Track {
|
||||
if t.Head["domain"] == "" {
|
||||
t.Head["domain"] = "scopes"
|
||||
func MakeMessage(c *tracking.Container, h ...string) tracking.Track {
|
||||
h = append(h, "") // make sure at least domain is there
|
||||
if h[0] == "" {
|
||||
h[0] = "scopes"
|
||||
}
|
||||
return tracking.MakeTrack(t, c)
|
||||
return Message(tracking.MakeTrack(c, h...))
|
||||
}
|
||||
|
||||
func (msg *message) Domain() string {
|
||||
|
@ -31,8 +32,8 @@ func Messages(db *sql.Storage) *tracking.Container {
|
|||
return &tracking.Container{container_definition, db}
|
||||
}
|
||||
|
||||
func New(cont *tracking.Container, headValues lib.StrSlice, data lib.Map) *message {
|
||||
return Message(cont.NewTrack(headValues, data))
|
||||
func New(cont *tracking.Container, h ...string) *message {
|
||||
return Message(cont.NewTrack(h, lib.Map{}, nil))
|
||||
}
|
||||
|
||||
// message store action handler
|
||||
|
|
|
@ -48,10 +48,11 @@ select {{ range $j, $c := .scols -}}
|
|||
where {{ range $j, $c := .qucols -}}
|
||||
{{- if ne $j 0 }} and {{ end }}{{ toLower $c }} = ${{ add1 $j }}{{- end }}
|
||||
{{ with .ordcols -}}
|
||||
ordered by {{ range $j, $c := . -}}
|
||||
order by {{ range $j, $c := . -}}
|
||||
{{- if ne $j 0 -}}, {{ end }}{{ toLower $c }}{{- end -}}
|
||||
{{- end }}
|
||||
{{ with .limit }}limit {{ . }}{{ end }}`
|
||||
{{ with .limit }}limit {{ . }}{{ end }}
|
||||
`
|
||||
)
|
||||
|
||||
var SqlCreate, SqlInsert, SqlSelect *template.Template
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
sql "git.sr.ht/~cco/go-scopes/storage"
|
||||
)
|
||||
|
||||
type ItemFactory func(*TrackTemplate, *Container) Track
|
||||
type ItemFactory func(*Container, ...string) Track
|
||||
|
||||
type Track interface {
|
||||
TrackId() lib.Ident
|
||||
|
@ -42,8 +42,15 @@ type track struct {
|
|||
container *Container
|
||||
}
|
||||
|
||||
func MakeTrack(t *TrackTemplate, cont *Container) Track {
|
||||
return &track{t.TrackId, t.Head, t.TimeStamp, t.Data, cont}
|
||||
func MakeTrack(cont *Container, h ...string) Track {
|
||||
head := lib.StrMap{}
|
||||
for i, k := range cont.HeadFields {
|
||||
if i >= len(h) {
|
||||
break
|
||||
}
|
||||
head[k] = h[i]
|
||||
}
|
||||
return &track{head: head}
|
||||
}
|
||||
|
||||
func (tr *track) TrackId() lib.Ident {
|
||||
|
@ -96,30 +103,22 @@ func Tracks(db *sql.Storage) *Container {
|
|||
}
|
||||
|
||||
func (cont *Container) Query(headValues lib.StrSlice) Track {
|
||||
head := lib.StrMap{}
|
||||
db := cont.Storage
|
||||
data := lib.Map{
|
||||
"schema": db.Schema,
|
||||
"tablename": cont.TableName,
|
||||
"scols": []string{"taskid", "data"},
|
||||
"qucols": []string{"taskid", "username"},
|
||||
"scols": lib.StrSlice{"taskid", "data"},
|
||||
"qucols": lib.StrSlice{"taskid", "username"},
|
||||
"ordcols": lib.StrSlice{"timestamp"},
|
||||
}
|
||||
sql := storage.BuildSql(SqlSelect, data)
|
||||
print(sql)
|
||||
tr := cont.ItemFactory(&TrackTemplate{Head: head}, cont)
|
||||
tr := cont.ItemFactory(cont)
|
||||
return tr
|
||||
}
|
||||
|
||||
func (cont *Container) NewTrack(headValues lib.StrSlice, data lib.Map) Track {
|
||||
head := lib.StrMap{}
|
||||
for i, k := range cont.HeadFields {
|
||||
if i >= len(headValues) {
|
||||
break
|
||||
}
|
||||
head[k] = headValues[i]
|
||||
}
|
||||
//tr := &track{head: head, data: data}
|
||||
tr := cont.ItemFactory(&TrackTemplate{Head: head, Data: data}, cont)
|
||||
func (cont *Container) NewTrack(h []string, data lib.Map, ts *time.Time) Track {
|
||||
tr := cont.ItemFactory(cont, h...)
|
||||
cont.insert(tr)
|
||||
return tr
|
||||
}
|
||||
|
|
|
@ -68,7 +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.Tracks(db)
|
||||
cont.CreateTable()
|
||||
track := cont.NewTrack(lib.StrSlice{"t01", "john"}, lib.Map{})
|
||||
track := cont.NewTrack(lib.StrSlice{"t01", "john"}, lib.Map{}, nil)
|
||||
t.AssertEqual(track.TrackId(), lib.Ident(1))
|
||||
t.AssertEqual(track.TimeStamp().Year(), time.Now().Year())
|
||||
//fmt.Printf("%+v\n", track.TimeStamp())
|
||||
|
@ -81,7 +81,7 @@ func MessageTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
|||
//msgstore.StoreDB(db, msg)
|
||||
cont := message.Messages(db)
|
||||
cont.CreateTable()
|
||||
msg := message.New(cont, lib.StrSlice{"", "data"}, lib.Map{})
|
||||
msg := message.New(cont, "", "data")
|
||||
t.AssertEqual(msg.TrackId(), lib.Ident(1))
|
||||
t.AssertEqual(msg.Domain(), "scopes")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue