Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a 128 bit integer with nHibernate?

Question:

How can I store a 128 bit integer (IPv6) with nHibernate ?

Especially on Firebird, where the BigInteger MAX size is 2^64 .

I want to do calculations like :

SELECT * FROM T_Country_IP_Range 
WHERE 
(block_from <= @in_IP) 
AND (block_to >= @in_ip)

So storing it as text/varchar/char is not an option.

like image 425
Stefan Steiger Avatar asked Jan 22 '26 18:01

Stefan Steiger


1 Answers

It seems indeed that there is no firebird support for INT128.

What you can do is represent the 128bits as two 64 bit fields, the upper 64bit in one field (IP_upper) and the lower 64bits in the other (IP_Lower).

All comparison operation will have to compare both fields. If the upper field is smaller/larger or if it's equal and then check the lower field:

SELECT * FROM T_Country_IP_Range 
WHERE 
(
    block_from_upper < @in_IP_upper 
    OR 
    (block_from_upper = @in_IP_upper AND block_from_lower <= @in_IP_lower)
) 
AND 
(
    block_to_upper > @in_IP_upper 
    OR 
    (block_to_upper = @in_IP_upper AND block_to_lower >= @in_IP_lower)
)

Kinda awkward, no doubt

like image 150
Variant Avatar answered Jan 24 '26 09:01

Variant