用于解析 SQL 参数的正则表达式 [英] Regex for parsing SQL parameters

查看:64
本文介绍了用于解析 SQL 参数的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个查询,例如 SELECT * from author where name = @name_param,是否有正则表达式来解析参数名称(特别是name_param")?

If I have a query such as SELECT * from authors where name = @name_param, is there a regex to parse out the parameter names (specifically the "name_param")?

谢谢

推荐答案

这很棘手,因为参数也可能出现在带引号的字符串中.

This is tricky because params can also occur inside quoted strings.

SELECT * FROM authors WHERE name = @name_param 
  AND string = 'don\'t use @name_param';

正则表达式如何知道使用第一个 @name_param 而不是第二个?

How would the regular expression know to use the first @name_param but not the second?

这是一个可以解决的问题,但是用一个正则表达式来做是不切实际的.我不得不在 Zend_Db 中处理这个问题,我所做的是首先去除所有带引号的字符串和分隔标识符,然后您可以在其余部分使用正则表达式.

It's a problem that can be solved, but it's not practical to do it in a single regular expression. I had to handle this in Zend_Db, and what I did was first strip out all quoted strings and delimited identifiers, and then you can use regular expressions on the remainder.

您可以看到代码,因为它是开源的.请参阅函数 _stripQuoted()_parseParameters().

You can see the code, because it's open-source. See functions _stripQuoted() and _parseParameters().

https://github.com/zendframework/zf1/blob/136735e776f520b081cd374012852cb88cef9a88/library/Zend/Db/Statement.php#L200https://github/com.zf1/blob/136735e776f520b081cd374012852cb88cef9a88/library/Zend/Db/Statement.php#L140

这篇关于用于解析 SQL 参数的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆