Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

sql.NullString and friends #380

Unanswered
zfLQ2qx2 asked this question in Issue Triage
Discussion options

I'm not doing anything too hardcore with Sqlite, I was able to generate compatible code with sqlc by manually changing datetime to timestamp in the table definitions I fed to sqlc.

The structures I read in from the database are being shared with several packages (json, html/template, etc) which are not aware of sql.NullString and friends, so my two cents is that it would be nice to have an option to emit *string (etc) instead of sql.NullString, sql.NullInt, etc. since the database/sql module already works with them seamlessly.

Otherwise I'm really impressed with sqlc and the code it generated, thank you for a great tool!

You must be logged in to vote

Replies: 5 comments

Comment options

You can easily create a type that embeds sql.Null* and make that type implement all the Marshallers and Unmarshallers that you need. Or, if you only need json and don't want to write any additional code, you can the null package to handle this for you.

You must be logged in to vote
0 replies
Comment options

Hi @euller88, what do you suggest in case I do not want interface to have sql specific types. As there might be other nosql implementation

You must be logged in to vote
0 replies
Comment options

@dharmjit I do not know what to do, but I have a suggestion. Try to override every type you expect to receive to either interface{} or map[string]interface{}. But remember that this is a really bad suggestion. If you really want to use the data between a relational and a nosql databases, you should write some conversion code.

You must be logged in to vote
0 replies
Comment options

I think a perl one-liner to replace sql.NullString with *string is the way I'm going to go, no intermediate steps or extra code needed.

But thats just me, I'm still really impressed with the utility of sqlc and wish I had something like this twenty years ago.

You must be logged in to vote
0 replies
Comment options

I would very much like for this to be a feature as well. Happy with sqlc in many ways, good work.

For anyone reading this thread, I added the following to my Makefile:

sql:
	sqlc generate
	sed -i -e 's/sql.NullString/*string/g' pkg/queries/*.go
	sed -i -e 's/sql.NullInt32/*int32/g' pkg/queries/*.go
	sed -i -e 's/sql.NullTime/*time.Time/g' pkg/queries/*.go
	goimports -w ./pkg/queries/

Will probably be solved by #1571.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Converted from issue

This discussion was converted from issue #380 on August 23, 2020 20:03.

AltStyle によって変換されたページ (->オリジナル) /