Skip to content

textRows.Next returns only []byte Values #366

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

Closed
binary132 opened this issue Aug 20, 2015 · 2 comments
Closed

textRows.Next returns only []byte Values #366

binary132 opened this issue Aug 20, 2015 · 2 comments

Comments

@binary132
Copy link

I'm using the database/sql driver implementation on a *DB obtained using sql.Open.

My code looks a bit like this.

var dataRows []map[string]interface{}
for rowNum := 0; rows.Next(); rowNum++ {
        newMap := make(map[string]interface{})
        err = rows.MapScan(newMap)
        // ...
}

json.Marshal(dataRows)
// [{"id":"MQ==","name":"b28="},{"id":"Mg==","name":"cmVhbCBuYW1l"}]
// expected:
// [{"id":1,"name":"oo"},{"id":2,"name":"real name"}]

All values unmarshaled into dataRows are of []byte type, including numbers, strings, etc. This is causing problems.

Would you be open to a pull request implementing the type switch from binaryRows on textRows?

@julienschmidt
Copy link
Member

No.
"textRows" is used when the text protocol is used. MySQL has two protocols: The default text protocol and the newer binary protocol which is used for prepared statements.
[]byte is returned since that is how the data from the MySQL server is received. All further type conversion is done by the database/sql package. Since you are using interface{} it is completely fine to put the []byte value there.

@binary132
Copy link
Author

I see. Thanks for clarifying.

Doesn't the typeswitch in the binary protocol also unmarshal VARCHAR and other string types as []byte? Perhaps there could be a switch within this block for VARCHAR, VARSTRING, and STRING to be stored in the result as string?

We're writing a user-facing feature for interfacing with the user's database, where the results are serialized as JSON. Because all of those cases are stored in the map as []byte, the type information between all of the types in that case is lost.

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

No branches or pull requests

2 participants