PostgreSQL customize inet_aton, inet_ntoa function in text type like MySQL.
来源:互联网 发布:2017网络最美情歌对唱 编辑:程序博客网 时间:2024/05/21 08:48
Postgres2015全国用户大会将于11月20至21日在北京丽亭华苑酒店召开。本次大会嘉宾阵容强大,国内顶级PostgreSQL数据库专家将悉数到场,并特邀欧洲、俄罗斯、日本、美国等国家和地区的数据库方面专家助阵:
- Postgres-XC项目的发起人铃木市一(SUZUKI Koichi)
- Postgres-XL的项目发起人Mason Sharp
- pgpool的作者石井达夫(Tatsuo Ishii)
- PG-Strom的作者海外浩平(Kaigai Kohei)
- Greenplum研发总监姚延栋
- 周正中(德哥), PostgreSQL中国用户会创始人之一
- 汪洋,平安科技数据库技术部经理
- ……
digoal=# select inet '192.168.1/24' >> inet '192.168.2.1'digoal-# ;?column?----------f(1 row)digoal=# select inet '192.168.1/24' >> inet '192.168.2.1';?column?----------f(1 row)digoal=# select inet '192.168.1/24' >> inet '192.168.1.100';?column?----------t(1 row)digoal=# select inet '192.168.1.0/24' >> inet '192.168.1.100';?column?----------t(1 row)digoal=# select inet '192.168.1.128/25' >> inet '192.168.1.100';?column?----------f(1 row)digoal=# select inet '192.168.1.128/25' >> inet '192.168.1.129';?column?----------t(1 row)
INET_ATON(
expr
)Given the dotted-quad representation of an IPv4 network address as a string, returns an integer that represents the numeric value of the address in network byte order (big endian).
INET_ATON()
returnsNULL
if it does not understand its argument.mysql>
SELECT INET_ATON('10.0.5.9');
-> 167773449For this example, the return value is calculated as 10×2563 + 0×2562 + 5×256 + 9.
INET_ATON()
may or may not return a non-NULL
result for short-form IP addresses (such as'127.1'
as a representation of'127.0.0.1'
). Because of this,INET_ATON()
a should not be used for such addresses.NoteTo store values generated by
INET_ATON()
, use anINT UNSIGNED
column rather thanINT
, which is signed. If you use a signed column, values corresponding to IP addresses for which the first octet is greater than 127 cannot be stored correctly. See Section 11.2.6, “Out-of-Range and Overflow Handling”.INET_NTOA(
expr
)Given a numeric IPv4 network address in network byte order, returns the dotted-quad string representation of the address as a nonbinary string in the connection character set.
INET_NTOA()
returnsNULL
if it does not understand its argument.mysql>
SELECT INET_NTOA(167773449);
-> '10.0.5.9'
create or replace function inet_aton(ip text) returns int8 as $$declarev int;res int8 := 0;i int := 3;beginforeach v in array string_to_array(ip, '.') loopres := res+v*(256^i);i := i-1;end loop;return res;end;$$ language plpgsql;create or replace function inet_ntoa(ip int8) returns text as $$declareres text := '';beginres := res || ((ip >> 24) & (2^8-1)::int);res := res || '.' || ((ip >> 16) & (2^8-1)::int);res := res || '.' || ((ip >> 8) & (2^8-1)::int);res := res || '.' || (ip & (2^8-1)::int);return res;end;$$ language plpgsql;
digoal=# select inet_aton('192.168.9.100');inet_aton------------3232237924(1 row)digoal=# select inet_ntoa(3232237924);inet_ntoa---------------192.168.9.100(1 row)
192.168.9.0/24192.168.9.128/25192.168.9.0/25192.168.0.0/16
192.168.9.0, 192.168.9.255192.168.9.128, 192.168.9.255192.168.9.0, 192.168.9.127192.168.0.0, 192.168.255.255
digoal=# select substring('192.168.9.0/24','(.*)/\d*');substring-------------192.168.9.0(1 row)
digoal=# select inet_ntoa((inet_aton(substring('192.168.9.0/24','(.*)/\d*')) + 2^(32-substring('192.168.9.0/24','.*/(\d*)')::int) -1)::int8);inet_ntoa---------------192.168.9.255(1 row)
digoal=# create table t1 (ip text, inet text);CREATE TABLEdigoal=# insert into t1 values ('192.168.1.1','192.168.1.128/25');INSERT 0 1digoal=# insert into t1 values ('192.168.1.10','192.168.1.0/25');INSERT 0 1digoal=# insert into t1 values ('192.168.1.129','192.168.1.128/25');INSERT 0 1digoal=# select inet, substring(inet,'(.*)/\d*') as net, inet_ntoa((inet_aton(substring(inet,'(.*)/\d*')) + 2^(32-substring(inet,'.*/(\d*)')::int) -1)::int8) as broadcast from t1;inet | net | broadcast------------------+---------------+---------------192.168.1.128/25 | 192.168.1.128 | 192.168.1.255192.168.1.0/25 | 192.168.1.0 | 192.168.1.127192.168.1.128/25 | 192.168.1.128 | 192.168.1.255(3 rows)
digoal=# select * from (select ip, inet, substring(inet,'(.*)/\d*') as net, inet_ntoa((inet_aton(substring(inet,'(.*)/\d*')) + 2^(32-substring(inet,'.*/(\d*)')::int) -1)::int8) as broadcast from t1) t where inet_aton(ip) > inet_aton(net) and inet_aton(ip) < inet_aton(broadcast);ip | inet | net | broadcast---------------+------------------+---------------+---------------192.168.1.10 | 192.168.1.0/25 | 192.168.1.0 | 192.168.1.127192.168.1.129 | 192.168.1.128/25 | 192.168.1.128 | 192.168.1.255(2 rows)
- PostgreSQL customize inet_aton, inet_ntoa function in text type like MySQL.
- PostgreSQL aggregate function customize
- mysql inet_aton和inet_ntoa存储ip地址
- MySQL的IP存储 INET_ATON INET_NTOA
- mysql利用INET_ATON或INET_NTOA比较ip
- [MySQL]IP处理函数inet_aton()和inet_ntoa()
- Mysql的IP处理函数inet_aton()和inet_ntoa()
- 利用mysql的inet_aton()和inet_ntoa()函数存储IP地址
- 利用mysql的inet_aton()和inet_ntoa()函数存储IP地址
- 利用mysql的inet_aton()和inet_ntoa()函数存储IP地址
- Mysql的IP处理函数inet_aton()和inet_ntoa()
- Mysql的IP处理函数inet_aton()和inet_ntoa()
- 利用mysql的inet_aton()和inet_ntoa()函数存储IP地址
- mysql 使用inet_aton和inet_ntoa处理ip地址数据
- mysql 使用inet_aton和inet_ntoa处理ip地址数据
- mysql 使用inet_aton和inet_ntoa处理ip地址数据
- inet_ntoa、 inet_aton、inet_addr
- Java的inet_aton inet_ntoa
- PostgreSQL json jsonb 支持的value数据类型,如何构造一个jsonb
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-04-目标函数
- 九度OJ 1002:Grading
- System 类和日期类
- 关于字符串的判空问题
- PostgreSQL customize inet_aton, inet_ntoa function in text type like MySQL.
- springMVC配置文件解释(2)
- 【SQL】删除表中重复数据
- 【集训队作业】DGCD
- 为Firefox安装样式
- c语言学习笔记(13)pragma详解,#和##运算符及编译指示字总结
- MFC(一) 程序运行机制编故事
- 针对不等式的定积分计算
- linux shell — 3.linux的档案属性和目录配置(2)