Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database query with time.Time #87

Closed
roihe opened this issue Mar 19, 2023 · 1 comment
Closed

Database query with time.Time #87

roihe opened this issue Mar 19, 2023 · 1 comment

Comments

@roihe
Copy link

roihe commented Mar 19, 2023

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)
		})
	})
}
kelindar added a commit that referenced this issue Jul 3, 2023
This PR fixes issue #87 where `WithValue()` wasn't unmarshaling the
record.
@kelindar
Copy link
Owner

kelindar commented Jul 3, 2023

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.

@kelindar kelindar closed this as completed Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants