Re: int to inet conversion

Поиск
Список
Период
Сортировка
От Anton Nikiforov
Тема Re: int to inet conversion
Дата
Msg-id 4392CE81.9060609@nikiforov.ru
обсуждение исходный текст
Ответ на Re: int to inet conversion  (Tom Lane <tgl@sss.pgh.pa.us>)
Ответы Re: int to inet conversion  (Martijn van Oosterhout <kleptog@svana.org>)
Список pgsql-general
Tom Lane wrote:

> Anton Nikiforov <anton@nikiforov.ru> writes:
>
>>is there any function that can translate INT to INET type?
>
>
> Nothing built-in, and given the fact that "inet" no longer means "IPv4",
> it's unlikely we'd add one in the future.  But there's nothing stopping
> you from adding one of your own.  For example
>
> regression=# create or replace function int2inet(int) returns inet as $$
> regression$# declare oct1 int;
> regression$#   oct2 int;
> regression$#   oct3 int;
> regression$#   oct4 int;
> regression$# begin
> regression$#   oct1 := ((($1 >> 24) % 256) + 256) % 256;
> regression$#   oct2 := ((($1 >> 16) % 256) + 256) % 256;
> regression$#   oct3 := ((($1 >>  8) % 256) + 256) % 256;
> regression$#   oct4 := ((($1      ) % 256) + 256) % 256;
> regression$#   return oct1 || '.' || oct2 || '.' || oct3 || '.' || oct4;
> regression$# end$$ language plpgsql strict immutable;
> CREATE FUNCTION
> regression=# select int2inet(-1062726656);
>    int2inet
> --------------
>  192.168.20.0
> (1 row)
>
> There's probably a better way to do the shifting-and-masking, but that
> was the first thing that came to mind.  (Actually, if you are planning
> to push a whole lot of data through this, it might be worth your time
> to write something in C.  But for a one-shot data conversion task this
> is probably plenty good enough.)
>
>             regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
>        subscribe-nomail command to majordomo@postgresql.org so that your
>        message can get through to the mailing list cleanly
>
Sorry for my stupidity but, maybe there is a function that converts mask
stored in int format to a numer of bits? ;)
Your function easyly convert this mask to dot decimal notation, but how
to count the number of 1 in it?

Best regards,
Anton


Вложения

В списке pgsql-general по дате отправления:

Предыдущее
От: Martijn van Oosterhout
Дата:
Сообщение: Re: function in index expression and unnecessary function calls in select
Следующее
От: Martijn van Oosterhout
Дата:
Сообщение: Re: int to inet conversion