datamapper / do

DataObjects
147 stars 73 forks source link

[do_postgres] Returning ID when bigint fails at `atoi` #98

Closed tpitale closed 8 years ago

tpitale commented 8 years ago

The call to parse the string returned inserted_id inside execute_non_query in do_postgres.c using atoi fails to parse ids returned that are big integers.

I changed: https://github.com/datamapper/do/blob/master/do_postgres/ext/do_postgres/do_postgres.c#L546

To:

char *returned_str = PQgetvalue(response, 0, 0);
returned_id = atoi(returned_str);
printf("String value = %s, Int value = %d\n", returned_str, returned_id);

to see what was up, and got:

String value = 20002471289, Int value = -1472365191

I'm going to try changing it to atoll locally, to see what happens.

tpitale commented 8 years ago

Oddly, atoll seems to return the same negative int value.

To be clear … the reason I went digging was because any time I tried to save a new record the save would fail and the returned id would be some negative integer of this sort.

@dbussink have you dealt with this before?

dbussink commented 8 years ago

Looks like a real issue indeed. Best way to handle it is probably to let Ruby do the heavy lifting here and call the Ruby C-API methods to do the string -> number conversion. This would then also overflow to a Bignum when necessary.

tpitale commented 8 years ago

I don't see a macro for converting a STR2NUM or something like that. Do you have something in mind? Or just calling to_i in the adapter or somewhere else higher up?

dbussink commented 8 years ago

rb_cstr_to_inum would be usable for this I think.

tpitale commented 8 years ago

Oh, sweet!