Created
November 20, 2023 11:13
-
-
Save NielsLiisberg/9d0d384d95fe0dc6a1b3178095932b6e to your computer and use it in GitHub Desktop.
SQL generate GUID / UUID
This file contains hidden or 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
-- Use MI to generarte a RFC 4122 compiant UUID / GUID | |
-- | |
-- This also showcase how to integrate the MI api using | |
-- C code directly into your UDF | |
-- | |
-- Note: I am using library QUSRSYS. I suggest you put it into your own tool library | |
-- | |
-- Simply paste this gist into ACS SQL and step through the example. | |
-- | |
-- It is a cool example how far you can go with SQL: Have fun - | |
-- (C) Niels Liisberg 2023 | |
-- | |
-- This gist is distributed on an "as is" basis, without warranties | |
-- or conditions of any kind, either express or implied. | |
---------------------------------------------------------------------------------------------- | |
call qsys2.ifs_write( | |
path_name => '/tmp/main.c' , | |
file_ccsid => 1208, | |
overwrite => 'REPLACE', | |
line =>' | |
{ | |
#include "QSYSINC/MIH/GENUUID" | |
_UUID_Template_T ut; | |
memset (&ut , 0, sizeof(ut)); | |
ut.bytesProv = sizeof(ut); | |
ut.version = 4; | |
_GENUUID (&ut); | |
memcpy (MAIN.UUID , ut.uuid , sizeof(ut.uuid)); | |
} | |
'); | |
create or replace function qusrsys.uuid ( | |
) | |
returns char (36) | |
set option output=*print, commit=*none, DECMPT=*PERIOD ,dbgview = *list -- *source --list | |
main: | |
begin | |
declare uuid char(16) for bit data default ''; | |
declare uuidhex char(32); | |
include '/tmp/main.c'; | |
set uuidhex = hex(uuid); | |
return substr(uuidhex , 1 ,8) || '-' || substr(uuidhex , 9 ,4) || '-' || substr(uuidhex , 13 ,4) || '-' || substr(uuidhex , 17, 4) || '-' || substr(uuidhex , 21, 12); | |
end; | |
-- Test case: | |
values qusrsys.uuid (); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment