Skip to content

Add ColumnTypeScanType to driver #199

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

Merged
merged 5 commits into from
Dec 11, 2024
Merged

Add ColumnTypeScanType to driver #199

merged 5 commits into from
Dec 11, 2024

Conversation

ncruces
Copy link
Owner

@ncruces ncruces commented Dec 10, 2024

Fix #198.

@ncruces
Copy link
Owner Author

ncruces commented Dec 10, 2024

@ngocphuongnb, can you test this please?

@ncruces ncruces marked this pull request as ready for review December 11, 2024 18:13
@ncruces
Copy link
Owner Author

ncruces commented Dec 11, 2024

I'm gonna merge this as:

  1. this passes your unit test
  2. like modernc (which you linked to) it supports the following pattern, which I've seen as a common usage
dest := make([]any, len(cols))
for rows.Next() {
	cols, err := rows.ColumnTypes()
	if err != nil {
		return err
	}

	for i, c := range cols {
		dest[i] = reflect.New(c.ScanType()).Interface()
	}

	err = rows.Scan(dest...)
	if err != nil {
		return err
	}
}

PR #197 would break this, be it because of "dynamic typing" or just NULLs in any column that's not []byte or any.

@ncruces
Copy link
Owner Author

ncruces commented Dec 11, 2024

With PR:

If you call rows.ColumnTypes() once, before calling rows.Next() you get types inferred based on the "declared type" of the column (if any). Here, I'm only committing to support STRICT tables, previously supported types used to tag time.Time, and BOOLEAN for bools. I don't go overboard with affinity rules: it's a straight "known names" list only. Other drivers seem to kinda agree.

Once you start iterating through rows, you get types based on the values. Here, I'm only committing to this rule: the scan type must be able to scan the value. I do take into account the column type, but I make no claim to pick the "best" type (whatever that means), or "most specific" type, or whatever, to scan the value. Just that it can scan the value, and is not too stupid.

If either these decisions turns out wrong, I'll tweak them. I'm pretty sure there's no right answer here, just heuristics.

@ncruces ncruces merged commit 36bbd67 into main Dec 11, 2024
13 checks passed
@ncruces ncruces deleted the scan branch December 13, 2024 16:18
@ngocphuongnb
Copy link

@ncruces Thank you for your great effort in making the decision and creating this PR.
The approach with ColumnTypeScanType is both creative and thoughtful, addressing the issue in a much more comprehensive and elegant way than my previous solution.
I really appreciate the time and consideration you put into crafting this improvement. Your work makes a significant difference. Thank you again!

@ncruces
Copy link
Owner Author

ncruces commented Dec 14, 2024

Thank you for your kind words!

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

Successfully merging this pull request may close these issues.

Add ColumnTypeScanType to driver
2 participants