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

proposal: database/sql: add NullInt16 support #40082

Open
kyleconroy opened this issue Jul 6, 2020 · 1 comment
Open

proposal: database/sql: add NullInt16 support #40082

kyleconroy opened this issue Jul 6, 2020 · 1 comment
Labels
Projects
Milestone

Comments

@kyleconroy
Copy link

@kyleconroy kyleconroy commented Jul 6, 2020

Hey Go team!

I maintain a database code generator. I'm having trouble correctly supporting null smallint columns because the database/sql package does not have a NullInt16 type. It would look almost identical to Nullint32 and NullInt64.

// NullInt16 represents an int32 that may be null.
// NullInt16 implements the Scanner interface so
// it can be used as a scan destination, similar to NullString.
type NullInt16 struct {
	Int16 int16
	Valid bool // Valid is true if Int16 is not NULL
}

// Scan implements the Scanner interface.
func (n *NullInt16) Scan(value interface{}) error {
	if value == nil {
		n.Int16, n.Valid = 0, false
		return nil
	}
	n.Valid = true
	return convertAssign(&n.Int16, value)
}

// Value implements the driver Valuer interface.
func (n NullInt16) Value() (driver.Value, error) {
	if !n.Valid {
		return nil, nil
	}
	return int64(n.Int16), nil
}
@gopherbot gopherbot added this to the Proposal milestone Jul 6, 2020
@gopherbot gopherbot added the Proposal label Jul 6, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 7, 2020

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals Aug 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Proposals
Incoming
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.