Subject | Re: [firebird-support] GUID generator |
---|---|
Author | Fabiano Bonin |
Post date | 2009-03-10T21:21:50Z |
For now, if you are using FB 2.1, you can convert a UUID to a hex
string using this stored procedure:
set term !! ;
create or alter procedure sp_uuid2hex (
uuid char(16) character set octets)
returns(
result varchar(32))
as
declare variable i integer;
declare variable c integer;
begin
result = '';
i = 1;
while (i <= octet_length(uuid)) do
begin
c = ascii_val(substring(uuid from i for 1));
result = result || 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 !!
set term ; !!
And you can also convert an hex string to an UUID using this stored procedure:
set term !! ;
create or alter procedure sp_hex2uuid (
hex varchar(32))
returns(
result char(16) character set octets)
as
declare variable i integer;
declare variable temp varchar(16) character set octets;
begin
hex = upper(hex);
temp = '';
i = 1;
while (i <= char_length(hex)) do
begin
temp = temp || ascii_char((position(substring(hex from i for 1),
'0123456789ABCDEF') - 1) * 16 + (position(substring(hex from i + 1 for
1), '0123456789ABCDEF') - 1));
i = i + 2;
end
result = temp;
suspend;
end !!
set term ; !!
Regards,
Fabiano
string using this stored procedure:
set term !! ;
create or alter procedure sp_uuid2hex (
uuid char(16) character set octets)
returns(
result varchar(32))
as
declare variable i integer;
declare variable c integer;
begin
result = '';
i = 1;
while (i <= octet_length(uuid)) do
begin
c = ascii_val(substring(uuid from i for 1));
result = result || 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 !!
set term ; !!
And you can also convert an hex string to an UUID using this stored procedure:
set term !! ;
create or alter procedure sp_hex2uuid (
hex varchar(32))
returns(
result char(16) character set octets)
as
declare variable i integer;
declare variable temp varchar(16) character set octets;
begin
hex = upper(hex);
temp = '';
i = 1;
while (i <= char_length(hex)) do
begin
temp = temp || ascii_char((position(substring(hex from i for 1),
'0123456789ABCDEF') - 1) * 16 + (position(substring(hex from i + 1 for
1), '0123456789ABCDEF') - 1));
i = i + 2;
end
result = temp;
suspend;
end !!
set term ; !!
Regards,
Fabiano