-
Notifications
You must be signed in to change notification settings - Fork 601
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
Implement sql.Scanner and driver.Valuer interfaces #5
Conversation
Any thoughts of this? Otherwise I might start migrating all my projects to use my fork. But I'd rather use your implementation (and give credit to you) |
@gust1n I'm sorry to have kept you waiting so long. Thank you for your pull request, I think SQL driver support is a great enhancement for go.uuid. |
Generally, UUID can be represented in at least two ways: as a 32 or 36 character ASCII Let's take a look at a Let's take a look at database world now. On the one side, PostgreSQL has builtin UUID datatype, represented as 16 byte array. PostgreSQL command-line client ( On the other side, MySQL and SQLite lack of builtin UUID datatype. If we are looking at SQLite, for example, there is at least two ways to store UUID: as a According to the information mentioned above, return u.MarshalBinary() As UUID should not be written to database as a string, there is no need in trying to scan it from data input, so return u.UnmarshalBinary(src.([]byte)) Is there anything I missed? |
Nice walk through! I'll be testing this tomorrow. The reason for my type switch was from earlier experiences when pg returned a string for the UUID type (yours). But that might have been since the type didn't implement valuer then. Let me test tomorrow. Skickat från min telefon
|
Oh, I see now, the problem is definitely in lib/pq#263 and lib/pq#209. Pure Go |
Yeah ok so it seems that using So my code actually works since the UUID inserted actually is a string (which postgres doesn't complain about), hence the Scan will take the path It actually seems the postgres uuid type (http://www.postgresql.org/docs/9.1/static/datatype-uuid.html) is a string, or am I reading it wrong? Should we instead use the approach you suggested for MySQL and SQlite and save as a bytea? |
@gust1n Documentation states only about human-readable representation but not about implementation. Actually UUID is implemented as /* uuid size in bytes */
#define UUID_LEN 16
/* pg_uuid_t is declared to be struct pg_uuid_t in uuid.h */
struct pg_uuid_t
{
unsigned char data[UUID_LEN];
}; I'll check if |
I didn't understand, do you want me to check and investigate further or will you do it? |
@gust1n I'll check it in the short term, a bit busy right now with my full-time duties. |
I've investigated on that problem a bit more. Both MySQL and SQLite feel just fine with Albeit PostgreSQL has builtin UUID datatype, driver implementations do not support binary wire protocol, only its text form. I've tested it against jbarham/gopgsqldriver and lib/pq. The first one is a simple cgo binding for native libpq library and it shouldn't be too difficult to make it use binary format instead of text one. Actually I was able to make it read and scan UUIDs from binary protocol in a just one-line fix. Write support is a bit harder and requires complete library refactoring. Anyway, this project seems to be a bit dead, last commit was pushed almost 3 years ago. Also I've discovered deafbybeheading/femebe – a library implementing PostgreSQL's FEBE protocol, unfortunately text version again. Looks like it is not too hard to add binary protocol support, it seems to me a bit cleaner protocol implementation than lib/pq has and can be used to reimplement lib/pq on top of it. Besides, femebe's author is one of active lib/pq committers. Anyway, in my opinion |
Here is an example code I've used during my tests. |
Oh my god, how did I miss this? I'm going to close my PR now haha! |
Can this be closed now that #10 is merged? |
This pull request also addresses driver.Valuer interface implementation. I'm reopening it as a reminder. |
To support mapping a struct to db with e.g. https://github.com/jmoiron/modl. But since they are interfaces they are not specific to that implementation.