Database query with time.Time
roihe opened this issue · comments
Hermann Roitinger commented
Hi, I have a question about the BinaryMarshaler with the datatype time.Time.
When I make a query with txn.WithValue I can not compare with the datatype time.Time.
The comparison is always false. I've already tried a few things to convert the value before comparing.
Here's a short example. The problem is in the line 'which datatyp should I use here?'
func main() {
table := column.NewCollection()
table.CreateColumn("name", column.ForString())
table.CreateColumn("country", column.ForString())
table.CreateColumn("birthdate", column.ForRecord(func() *time.Time { return new(time.Time) }))
table.Insert(func(r column.Row) error {
r.SetString("name", "Charlie")
r.SetString("country", "Großbritannien")
r.SetRecord("birthdate", time.Date(1987, 10, 25, 12, 0, 0, 0, time.Local))
return nil
})
srcDate := time.Date(1999, 3, 2, 12, 0, 0, 0, time.Local)
table.Insert(func(r column.Row) error {
r.SetString("name", "Alice")
r.SetString("country", "USA")
r.SetRecord("birthdate", srcDate)
return nil
})
table.Insert(func(r column.Row) error {
r.SetString("name", "Alice")
r.SetString("country", "Österreich")
r.SetRecord("birthdate", time.Date(1965, 1, 9, 12, 0, 0, 0, time.Local))
return nil
})
table.Insert(func(r column.Row) error {
r.SetString("name", "Bob")
r.SetString("country", "Kanada")
r.SetRecord("birthdate", time.Date(1965, 1, 9, 12, 0, 0, 0, time.Local))
return nil
})
table.Insert(func(r column.Row) error {
r.SetString("name", "David")
r.SetString("country", "Australien")
r.SetRecord("birthdate", time.Date(1978, 3, 4, 12, 0, 0, 0, time.Local))
return nil
})
table.Query(func(txn *column.Txn) error {
name := txn.String("name")
country := txn.String("country")
birthdate := txn.Record("birthdate")
return txn.WithValue("birthdate", func(v interface{}) bool {
return v == srcDate // which datatyp should I use here?
}).Range(func(idx uint32) {
valueName, _ := name.Get()
print(" Name: ", valueName)
valueCountry, _ := country.Get()
print(" Contry: ", valueCountry)
birthdate, _ := birthdate.Get()
str := fmt.Sprintf("%v", birthdate)
println(" Birthdate: ", str)
})
})
}
Roman Atachiants commented
Looks like there was an issue, the WithValue()
on record wasn't unmarshaled so it returned a marshaled string instead. Now, it will return *time.Time
in your case.