Does Firebird support UUIDs or GUIDs?


Since Firebird 2.1 there is a built-in function GEN_UUID that creates a 128bit UUID. To retrieve it, you can use a statement like this one:

select GEN_UUID() from RDB$DATABASE;

The result is string of 16 bytes using character set OCTETS. In this character set, each character is one 8-bit byte. If you want to covert that to a hexadecimal representation, you could use a stored procedure like this one:

set term !! ;

create procedure get_hex_uuid
returns(real_uuid char(16) character set OCTETS, hex_uuid varchar(32))
AS
declare variable i integer;
declare variable c integer;
BEGIN
real_uuid = GEN_UUID();
hex_uuid = '';
i = 0;
while (i < 16) do
begin
c = ascii_val(substring(real_uuid from i+1 for 1));
if (c < 0) then c = 256 + c;
hex_uuid = hex_uuid
|| substring('0123456789abcdef' from bin_shr(c, 4) + 1 for 1)
|| substring('0123456789abcdef' from bin_and(c, 15) + 1 for 1);
i = i + 1;
end
suspend;
END !!


Earlier version of Firebird don't have this function, but you can use some UDF library like uuidlib or FreeAdhocUDF:

http://www.udf.adhoc-data.de/index_eng.html


To store such values, BIGINT is not enough as UUIDs are 128bit, while the biggest available integer is 64bit (8bytes), so you're best off using some CHAR(16) or bigger CHAR datatype. Please note that the shorter the storage type, the faster will the indexing work. It is recommended to use the OCTETS as character set for that column, as it means to store raw bytes and only wastes a single byte (8 bits) per character.

GUID is a Microsoft standard and implementations exists for various Windows development tools. UUID is Unix standard, and you can use it anywhere. Please note that codes are not always interchangeable (depending on the implementation you use).

http://www.ietf.org/rfc/rfc4122.txt


Here's an example of Delphi UDF you can use with older Firebird versions:

function gen_uuid: PChar; cdecl; export;
var
uid: TGUID;
begin
Result := ib_util_malloc(16);
CreateGUID(uid);
Move(uid, Result^, SizeOf(uid));
end;

Register it like this:

declare external function gen_uuid
returns char(16) character set octets free_it
entry_point 'gen_uuid' module_name 'udf.dll';


Do you find this FAQ incorrect or incomplete? Please e-mail us what needs to be changed. To ensure quality, each change is checked by our editors (and often tested on live Firebird databases), before it enters the main FAQ database. If you desire so, the changes will be credited to your name. To learn more, visit our add content page.



All contents are copyright © 2007-2024 FirebirdFAQ.org unless otherwise stated in the text.


Links   Firebird   News   FlameRobin   Powered by FB: Home Inventory   Euchre  
Add content   About  

Categories
 Newbies
 SQL
 Installation and setup
 Backup and restore
 Performance
 Security
 Connectivity and API
 HOWTOs
 Errors and error codes
 Miscellaneous