SQL ::抽象类型铸件列

发布于 2025-01-31 22:47:39 字数 494 浏览 5 评论 0原文

使用sql :: Abstract我需要将IP列键入text,以便能够使用喜欢

我只找到了一种实现目标的“骇人听闻”的方法:

where( { ip => { '::TEXT LIKE' => $myParameter } } )

生成

WHERE ( "ip" ::TEXT LIKE ? )

问题是:实现这一目标的技巧较少或官方方法吗?

问题不是:

  • 表演会很差吗?
  • 我应该使用文本列而不是ip列吗?
  • 使用CIDR是更好的选择吗

Using SQL::Abstract I need to type cast an IP column to TEXT in order to be able to search using LIKE.

I only found a "hacky" way to achieve it with:

where( { ip => { '::TEXT LIKE' => $myParameter } } )

Which generates

WHERE ( "ip" ::TEXT LIKE ? )

Question is: Is there a less hacky or official way to achieve this?

Questions are not:

  • Will the performance be poor?
  • Should I use a TEXT column instead of an IP column?
  • Are searches using CIDR a better alternative

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再浓的妆也掩不了殇 2025-02-07 22:47:39

问题是在mojo :: pg中添加QUOTE_CHAR “ ”到SQL :: Abstract Object。

当我将其设置为空时字符串,这将按预期工作:

where( { 'ip::TEXT' => { 'LIKE' => $myParameter } } )

但是,要完成,我必须使用,

where( { 'host(ip)' => { 'LIKE' => $myParameter } } )

因为:: text将为IP提供附加的/32

The issue was in Mojo::Pg which adds a quote_char of " to the SQL::Abstract object.

When I set this to the empty string, this will work as expected:

where( { 'ip::TEXT' => { 'LIKE' => $myParameter } } )

But, to be complete, I had to use

where( { 'host(ip)' => { 'LIKE' => $myParameter } } )

because ::TEXT will give the IP with an appended /32.

紅太極 2025-02-07 22:47:39

我认为您在问题中混合了很多东西。您听起来像是sql :: Abstract问题,当您的实际问题与基础SQL本身有关。

首先,我个人会避免使用sql :: Abstract在大多数情况下(有时创建复杂的查询非常慢,您无法确定其输出),并且在这样的情况下肯定您想要一些非标准的地方。

因此,继续前进,我不确定您的意思是ip类型,从您的postgres标签中以及提及cidr我怀疑您的意思是您的意思是inet 类型?如果是这样,则相当于喜欢是使用子网掩码,这基本上是使用inet而不是文本/varchar字段的全部原因。对于192.168

SELECT * FROM users WHERE ip << inet '192.168.0.0/16'

。像文本一样对待它们(例如,您要搜索192.16%,生产192.168。*结果,但与192.16。使用子网掩码完成),然后您首先使用文本类型,或者正如您所说的那样,可以直接使用like直接使用:

SELECT * FROM users WHERE TEXT(ip) LIKE '192.168.%'

使用子网掩码的性能罚款,但是当然,这是否可能是问题取决于您的数据。

注意cidr工作类似于inet,它不会为您提供之类的

I think you mix a lot of things in your question. You make it sound like it is an SQL::Abstract issue, when your real issue is with the underlying SQL itself.

First of all, I would personally avoid using SQL::Abstract in most cases (it is sometimes VERY slow to create complex queries and you can't be sure of its output) and definitely in cases like this where you want something non-standard.

So, moving on, I am not sure what you mean by IP type, from your postgres tag and the mention of CIDR I suspect you mean the inet type? If so, the equivalent of LIKE is to use subnet masks, which is basically the whole reason to use inet instead of a text/varchar field. For 192.168.* for example you would do something like below using the subnet slash notation:

SELECT * FROM users WHERE ip << inet '192.168.0.0/16'

If you don't want to treat IPs as actual IPs and take advantage of things like above, but instead you want to treat them like text (e.g. you want to search 192.16% with that producing 192.168.* results but along with 192.16.* etc which is not done with subnet masks), then you either use a text type in the first place, or as you said convert on the fly to use LIKE directly:

SELECT * FROM users WHERE TEXT(ip) LIKE '192.168.%'

There is a performance penalty over using the subnet masks, but whether that may be an issue depends on your data of course.

Note cidr works similar to inet, it won't help you with a LIKE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文