开发者

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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜