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

Native Driver support for Custom Time type in struct #742

Closed
bzon opened this issue Sep 7, 2022 · 3 comments · Fixed by #767
Closed

Native Driver support for Custom Time type in struct #742

bzon opened this issue Sep 7, 2022 · 3 comments · Fixed by #767
Assignees
Milestone

Comments

@bzon
Copy link

bzon commented Sep 7, 2022

Is your feature request related to a problem? Please describe.

I have a struct with a custom time type CustomDateTime. I use it over time.Time because I change the way how it is stringified in json.

type CustomDateTime time.Time

type Log struct {
  Timestamp CustomDateTime `json:"timestamp" ch:"timestamp"`
  Field1 string `json:"field1" ch:"field1"`
  Field2 string `json:"field2" ch:"field2"`
}

However, I get an error with this code:

var vv Log
_ = conn.Select(ctx, &vv, dest, args...)
query error: clickhouse [ScanRow]: (timestamp) converting DateTime to *CustomDateTime is unsupported

When the using sql driver, I have no problem using DateTime column type to a struct field with type CustomDateTime.

Describe the solution you'd like

I want the same usage when I was using the sql driver.

var vv Log
_ = conn.Select(ctx, &vv, dest)

I think #720 is a good reference for the solution.

Describe alternatives you've considered
My alternative solution is to create a temporary alias struct and use row scan.

type logTmp struct {
  Log
  Timestamp `ch:"timestamp"`
}

rows, _ := db.conn.Query(....)

var vv []Log
for rows.Next() {
		var res postbackLogs
		if err := rows.ScanStruct(&res); err != nil {
			return nil, errors.WrapT(span, err, "scan error")
		}
		v := res.PostbackResult

		// unsupported types by the native driver must be converted by hand.
		v.Timestsamp = parseTimeToCustomDateTimeFunc(res.Timestamp)
		vv = append(vv, v)
}

Additional context
Add any other context or screenshots about the feature request here.

@gingerwizard
Copy link
Collaborator

Makes sense to me. Will PR.

@gingerwizard
Copy link
Collaborator

support for sql.Scanner on DateTime should do it.

@gingerwizard
Copy link
Collaborator

@bzon addressed by #767

Requires your type to implement sql.Scanner
e.g.

type CustomDateTime time.Time

func (ct *CustomDateTime) Scan(src any) error {
	if t, ok := src.(time.Time); ok {
		*ct = CustomDateTime(t)
		return nil
	}
	return fmt.Errorf("cannot scan %T into CustomDateTime", src)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants