also return timestamp from insert track and store in track
This commit is contained in:
parent
1c44b135d8
commit
9f17a14ea9
3 changed files with 36 additions and 4 deletions
|
@ -38,7 +38,7 @@ insert into {{ $tablename }} (
|
|||
{{- range $j, $c := .columns -}}
|
||||
{{- if ne $j 0 -}}, {{ end }}${{ add1 $j }}
|
||||
{{- end -}})
|
||||
returning trackid`
|
||||
returning trackid, timestamp`
|
||||
)
|
||||
|
||||
var SqlCreate, SqlInsert *template.Template
|
||||
|
|
|
@ -3,10 +3,13 @@
|
|||
package tracking
|
||||
|
||||
import (
|
||||
sqllib "database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/logging/log"
|
||||
"git.sr.ht/~cco/go-scopes/storage"
|
||||
sql "git.sr.ht/~cco/go-scopes/storage"
|
||||
)
|
||||
|
@ -84,15 +87,44 @@ func (cont *container) insert(tr *track) lib.Ident {
|
|||
"columns": columns,
|
||||
}
|
||||
sql := storage.BuildSql(SqlInsert, data)
|
||||
if res := storage.QueryCol[lib.Ident](db, sql, values...); res != nil {
|
||||
trid := lib.Ident(res[0])
|
||||
type rt struct {
|
||||
trid lib.Ident
|
||||
ts string
|
||||
//ts *time.Time
|
||||
}
|
||||
var res []rt
|
||||
proc := func(r *sqllib.Rows) error {
|
||||
var rec rt
|
||||
err := r.Scan(&rec.trid, &rec.ts)
|
||||
res = append(res, rec)
|
||||
return err
|
||||
}
|
||||
if err := db.Query(proc, sql, values...); err == nil {
|
||||
row := res[0]
|
||||
trid := row.trid
|
||||
tr.trackId = trid
|
||||
if ts := ParseDateTime(row.ts); ts != nil {
|
||||
fmt.Printf("%+v\n", ts)
|
||||
tr.timeStamp = ts
|
||||
}
|
||||
tr.container = cont
|
||||
return trid
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func ParseDateTime(inp string) *time.Time {
|
||||
if ts, err := time.Parse(time.RFC3339, inp); err == nil {
|
||||
return &ts
|
||||
}
|
||||
ts, err := time.Parse("2006-01-02 15:04:05", inp)
|
||||
if err == nil {
|
||||
return &ts
|
||||
}
|
||||
log.Error(err).Msg("storage.tracking.ParseDateTime")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cont *container) CreateTable() {
|
||||
db := cont.storage
|
||||
data := lib.Map{
|
||||
|
|
|
@ -66,7 +66,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 := tracking.Tracks(db)
|
||||
cont.CreateTable()
|
||||
track := cont.New(lib.StrSlice{"t01", "john"}, lib.Map{})
|
||||
t.AssertEqual(track.TrackId(), lib.Ident(1))
|
||||
|
|
Loading…
Add table
Reference in a new issue