IP 地址存储为十进制 - PL/SQL 显示为点线四边形
我们有一个 Oracle 数据库,其中包含以十进制整数存储的 IP 地址 - 当手动操作数据而不是通过 Web 界面时,这是非常痛苦的,但手动操作确实很方便,因为网络人员不断要求我们做奇怪的事情Web 界面的作者没有预料到。
有人可以向我提供 PL/SQL 或其他方法来将这些十进制 IP 显示为点分十进制,即 123.123.123.123 格式吗?
即我希望能够运行诸如 : 之类的查询,
select hostname, inttoip(ip_address) from host;
并让 inttoip()
过程将 ip_address 显示为 203.30.237.2 而不是 3407801602。
理想情况下,我想要一个提供以下功能的过程:反函数也是如此,例如
insert into host (hostname,ip_address) values ('some-hostname', iptoint('203.30.237.2'));
我有 perl 来做到这一点,但我的 PL/SQL/Oracle 知识不足以将其移植到 PL/SQL 中。
或者一种在 oracle 上下文中运行 perl 作为过程语言的方法,类似于 postgres 中的以下内容:
CREATE FUNCTION perl_func (integer) RETURNS integer AS $$
<some perl>
$$ LANGUAGE plperl;
会很棒 - 如果可能的话 - 可能甚至更好然后,我可以用我熟悉的语言在 Oracle 中完成许多程序性工作。
We have an Oracle database that contains IP addresses stored as decimal integers - this is incredibly painful when manipulating the data by hand instead of via the web interface, yet hand manipulation is really handy as the network guys continually ask us to do strange things that the web interface authors did not anticipate.
Could someone provide me with the PL/SQL or other method to display these decimal IPs as dotted decimal i.e. 123.123.123.123 format?
I.e. I'd like to be able to run a query such as :
select hostname, inttoip(ip_address) from host;
and have the inttoip()
procedure display ip_address as 203.30.237.2 instead of as 3407801602.
Ideally I'd like a procedure which provides the inverse function too, e.g.
insert into host (hostname,ip_address) values ('some-hostname', iptoint('203.30.237.2'));
I have perl to do this, but my PL/SQL/Oracle knowledge is not good enough to port it into PL/SQL.
Alternatively a way to run the perl as the procedural language within the oracle context analogous to the following in postgres:
CREATE FUNCTION perl_func (integer) RETURNS integer AS $
<some perl>
$ LANGUAGE plperl;
Would be great - if possible - probably even better as I could then do lots of procedural stuff within Oracle in a language I am familiar with.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是您需要的函数:(
关于使函数确定性和使用 to_char 的评论 - 谢谢)。
在 Oracle 11G 中,您可以将格式化的 IP 地址设置为主机表上的虚拟列:
如果需要,可以对该列建立索引以供查询。
您的查询变为:
This is the function you need:
(Comments about making function deterministic and using to_char taken on board - thanks).
In Oracle 11G you could make the formatted IP address a virtual column on the host table:
This column could then be indexed for queries if required.
Your query becomes: