tcncloud/protoc-gen-persist

Have way to guarantee scan order in generated code

iamneal opened this issue · 1 comments

currently in the generated code, there is no way to guarantee that a row is scanned out of the database in the correct order.

given a proto message, and service that looks like this:

message TestMessage {
    string id = 1;
    string name = 2;
}
service TestService {
    rpc TestUniary(TestMessage) returns(TestMessage){
      option(ql.){
         query: "Select (id, name) FROM test_table WHERE id=$1",
         arguments: ["id"]
       };
    };
}

we generate code that looks like this:

var (
  id        string
  name  string
)
s.SqlDB.QueryRow("Select (id, name) FROM test_table WHERE id=$1", req.Id).
    Scan(&id, &name)// <---- problem here

The problem lies in that we can't guarantee that we create a scan that looks like above, vs a scan that looks like: Scan(&name, &id)

If the scan happens out of order, it is wrong, and can crash at runtime.

We either need to parse the query, or signal some way for persist to know which order to scan the variables.

So, I updated the templates file in the generator to return a TypeDesc array, and range over that. As long as their result proto has the exact fields, in the exact order as the fields that are selected in the query, this issue is solved on my branch. I can close this when it is merged in.