下面的我的代码不是解决此问题的好方法。在使用该代码之前,请继续寻找更正式的解决方案。
似乎OCI确实具有通过功能OCIStmtGetBindInfo动态检索绑定名称的功能。但是,看起来default PHP functions中没有该功能。也许还有其他更高级的方法将PHP连接到Oracle,这些方法提供了必要的功能,但是我对OCI或PHP的了解还不足以找到它们。
如果您准备好一个不太好的解决方案,则可以使用我的开源程序plsql_lexer查找绑定变量名称。词法分析器将SQL语句分解为小标记,并处理诸如注释和字符串之类的语法难题。结果应该比使用一些正则表达式更加准确。
缺点是程序不是完整的解析器,因此您必须处理原始令牌。在这种情况下,使用单个SQL语句查找99.9999%的绑定变量相对容易。安装程序后,将SQL放入以下SELECT语句的中间:--Find bind variables.
--(Words or numerics that were immediately preceded (excluding whitespace) by a colon.)
select to_char(value) bind_variable_name
from
(
--Get previous token.
select type, value, first_char_position,
lag(to_char(type)) over (order by first_char_position) previous_type
from
(
--Convert to tokens, ignore whitespace.
select type, value, first_char_position
from table(plsql_lexer.lex(
q'[
--Here's the actual SQL statement you care about.
--/*:fake_bind1*/
select 1 a
from dual
where 1 = : real_bind_1 and :real_bind_2 = ':fake_bind_2'
]'))
where type not in ('whitespace')
order by first_char_position
)
)
where type in ('numeric', 'word')
and previous_type = ':'
order by first_char_position;
BIND_VARIABLE_NAME
------------------
real_bind_1
real_bind_2
[在某些情况下,此代码仍无法处理。例如,绑定变量可以是带引号的标识符,您可能需要处理双引号。上面的代码不处理indicators。另一方面,我实际上从未见过使用过的任何功能,因此对您而言可能并不重要。彻底测试。