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

Add support for sql null types #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add support for sql null types #66

wants to merge 2 commits into from

Conversation

adam-frisby
Copy link

This pull request adds Null* types from the database/sql package to the Hood SQL Types and adds support for the types in SetModelValue. I've tested the changes with both mysql and postgresql. Everything looks to be working as expected. This will allow hood to handle values which may be null.


Example:

type Test struct {
    Id       hood.Id
    Number   sql.NullInt64
}

// Connect and Start Transaction

a = Test{}
tx.Save(&a) //Number column is set to NULL in database (defaults to NULL)

b = Test{}
b.Number = sql.NullInt64{128, true} //Number column is set to 128 in database
tx.Save(&b)

c = Test{}
c.Number = sql.NullInt64{128, false} //Number column is set to NULL in database since valid bool is set to false
tx.Save(&c)

Retrieving the row which was inserted by tx.Save(&c) above will give you a sql.NullInt64{0, false} for Number


Question: Since normal types (int, string, etc) can't represent a null value, should the notnull keyword be removed and assumed to be not null unless the column type is of type NullBool, NullInt64, etc?

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.

1 participant