Skip to content
This repository has been archived by the owner on Aug 31, 2020. It is now read-only.

varchar not converted to string #8

Open
hblok opened this issue Jun 24, 2020 · 2 comments
Open

varchar not converted to string #8

hblok opened this issue Jun 24, 2020 · 2 comments

Comments

@hblok
Copy link

hblok commented Jun 24, 2020

Following the code under "examples" in this repository, I've brought up a MySQL DB and a simple server based on "server.go". I've created the "blog" table as per the "testdata/initdb/sql/init_db.sql" example and added some values. Finally, I've created a test client. Connections and mapping work for integer values and I get a correct BlogResponse. However, the varchar "title" field is blank.

After some debugging, I found that it was not correctly converted. The varchar was read as a []uint8 from the DB. I modified the function "MapResponse" in "mapper.go" to include a conversion to a string. Something like this:

func B2S(bs []uint8) string {
    b := make([]byte, len(bs))
    for i, v := range bs {
        b[i] = byte(v)
    }
    return string(b)
}


func (m *Mapper) MapResponse(respMap *ResponseMapping) error {
....

  arrayUint8, ok := sqlMapVals.ProtoValues[i].([]uint8)
  if ok == true {
     str := B2S(arrayUint8)
     setProto(respField, str)
  } else {
    setProto(respField, sqlMapVals.ProtoValues[i])
  }

With that fix, the full BlogResponse object is returned correctly.

Is this a known problem? I have configured the DB (MySql / MariaDB) wrong? Or maybe it's a driver or configuration problem?

@jackskj
Copy link
Owner

jackskj commented Jul 7, 2020

Which version are you using ?

@jackskj
Copy link
Owner

jackskj commented Jul 7, 2020

I predominately work with Postgres, but From what I remember, MySQL needs a prepare statement to return correct data type. When using query() directly, the MySQL will return your data in form of bytes.
This is not the case with Postgres.
The fix would involve adding a prepare statement in the generator.
Until I do that and thoroughly test it, feel free to use https://github.com/jackskj/carta
if you are using MySQL, just make sure to use prepare statement.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants