Last active
September 26, 2024 23:36
-
-
Save jamesgmarks/56502e46e29a9576b0f5afea3a0f595c to your computer and use it in GitHub Desktop.
MySQL/MariaDB BIN_TO_UUID and UUID_TO_BIN Polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELIMITER // | |
CREATE FUNCTION BIN_TO_UUID(b BINARY(16)) | |
RETURNS CHAR(36) | |
BEGIN | |
DECLARE hexStr CHAR(32); | |
SET hexStr = HEX(b); | |
RETURN LOWER(CONCAT( | |
SUBSTR(hexStr, 1, 8), '-', | |
SUBSTR(hexStr, 9, 4), '-', | |
SUBSTR(hexStr, 13, 4), '-', | |
SUBSTR(hexStr, 17, 4), '-', | |
SUBSTR(hexStr, 21) | |
)); | |
END// | |
CREATE FUNCTION UUID_TO_BIN(uuid CHAR(36)) | |
RETURNS BINARY(16) | |
BEGIN | |
RETURN UNHEX(REPLACE(uuid, '-', '')); | |
END// | |
DELIMITER ; |
@theking2 - yes, this is more or less what I use. I came here looking for an implementation and after I found it lacking, I just wanted to warn people away (assuming they are looking for a correct implementation). If you continue looking elsewhere, you'd find the implementation that you posted, posted in many other places - hopefully with a good explanation of what it does and why it is good, which I saw no need to duplicate again.
excellent. I just happened to stumble on your gist first. Hence my two cents.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In response to @guss77