Skip to content
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

Uuid support for MySQL #536

Merged
merged 11 commits into from
Jul 27, 2020
Merged

Uuid support for MySQL #536

merged 11 commits into from
Jul 27, 2020

Conversation

jkoudys
Copy link
Contributor

@jkoudys jkoudys commented Jul 20, 2020

While there's no native UUID type in mysql, we can use either the standard hex-encoded string, or a raw BYTES(16) to represent a UUID.

I think this may still need some more type data setup. The Encode also assumes a binary encoding, but mysql aggregate functions exist to support both the 128-bit encoding, and the hex-encoded string with the dashes, so we ought to be able to encode to both somehow: https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_uuid .

@mehcode
Copy link
Member

mehcode commented Jul 20, 2020

This is a good start.

Each Rust type needs to pick one SQL type if the format we encode to is different. The mysql crate picks BINARY(16). I believe that's probably fine for the uuid::Uuid type.

Why don't we implement Type as CHAR(36) for https://docs.rs/uuid/0.8.1/uuid/adapter/struct.Hyphenated.html ? That way it's simple for users to do some_uuid.to_hyphenated() and have it sent to MySQL as CHAR(36).

One more thing, Decode does not allow switching between textual and binary types but the encoding format in the protocol. In MySQL, if you make a parameterized query, the returned value is encoded in a binary encoding. If you make a simple query, the returned value is in a textual encoding.

Here is an example of using trait delegation to re-use the impl for &[u8]:

impl Decode<'_, MySql> for Uuid {
    fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
        // delegate to the &[u8] type to decode from MySQL
        let bytes = <&[u8] as Decode<MySql>>::decode(value)?;

        // construct a Uuid from the returned bytes
        Uuid::from_slice(bytes).map_err(Into::into)
    }
}

@jkoudys
Copy link
Contributor Author

jkoudys commented Jul 20, 2020

@mehcode so that sounds right to me -- where do those BINARY and CHAR types get defined?

@jkoudys
Copy link
Contributor Author

jkoudys commented Jul 21, 2020

@mehcode Alright we've put in support for uuid::adapter::Hyphenated as well. Anything else?

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

Successfully merging this pull request may close these issues.

3 participants