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/sql: add NullInt16 support #40082

Open
kyleconroy opened this issue Jul 6, 2020 · 9 comments
Open

database/sql: add NullInt16 support #40082

kyleconroy opened this issue Jul 6, 2020 · 9 comments

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
@kardianos
Copy link
Contributor

@kardianos kardianos commented Aug 11, 2020

@kyleconroy This looks reasonable. Can you send a CL?

On a side note, these nullable wrappers would probably be a great use for generics...

@kyleconroy
Copy link
Author

@kyleconroy kyleconroy commented Aug 11, 2020

Yeah, can do. A bit swamped right now, but should have it done by the end of the week

@kyleconroy
Copy link
Author

@kyleconroy kyleconroy commented Aug 11, 2020

On a side note, these nullable wrappers would probably be a great use for generics...

I know! Here's a four-line replacement for the various sql.Null* types in the standard library.

    type Null(type T) struct {
        Val   T
        Valid bool // Valid is true if Val is not NULL
    }

Here's what it looks like in practice: https://go2goplay.golang.org/p/Qj8MqYWWAc3

@rsc
Copy link
Contributor

@rsc rsc commented Aug 12, 2020

Do we need NullByte for tinyint too?
(No luck for 24-bit mediumint)

@kardianos
Copy link
Contributor

@kardianos kardianos commented Aug 12, 2020

NullByte and NullInt16 both sound good. Might as well complete the family.

@rsc rsc moved this from Incoming to Active in Proposals Aug 14, 2020
@rsc
Copy link
Contributor

@rsc rsc commented Aug 26, 2020

Based on the discussion above, this seems like a likely accept.

@rsc rsc moved this from Active to Likely Accept in Proposals Aug 26, 2020
@rsc
Copy link
Contributor

@rsc rsc commented Sep 2, 2020

No change in consensus, so accepted.

@rsc rsc moved this from Likely Accept to Accepted in Proposals Sep 2, 2020
@rsc rsc modified the milestones: Proposal, Backlog Sep 2, 2020
@rsc rsc changed the title proposal: database/sql: add NullInt16 support database/sql: add NullInt16 support Sep 2, 2020
@muhlemmer
Copy link
Contributor

@muhlemmer muhlemmer commented Sep 9, 2020

I have some spare time and like to contribute more to the Go project. May I volunteer for a CL @kyleconroy, or do you prefer to do it yourself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Proposals
Accepted
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants