验证 Oracle 非 SELECT 查询
有没有什么方法可以检查 Oracle 的 SQL 查询是否会成功运行(成功是指查询在语法上是正确的,所有表/列名称都存在,用户具有适当的权限等),而无需实际运行它?该查询可能不是 SELECT,但如果它会修改任何数据,我不希望实际发生更改。
我想到了这样的事情:
$valid = false;
$stmt = oci_parse($db, $query);
if(!empty($stmt)) {
$res = oci_execute($stmt, OCI_DESCRIBE_ONLY|OCI_NO_AUTO_COMMIT);
if(!empty($res)) {
$name = oci_field_name($res, 1);
if(!empty($name)) {
$valid = true;
}
}
oci_rollback($db);
}
但是如果 $query 中有一些 DDL,我知道 Oracle 会立即提交它。那么有没有什么方法可以在不进行任何修改的情况下检查查询呢?
Is there any way to check if an SQL query for Oracle would run successfully (by successfully I mean that the query is syntactically correct, all the table/column names exist, the user has proper permissions, etc.) without actually running it? The query may be not SELECT, but I do not want the changes to actually happen if it would modify any data.
I thought about something like:
$valid = false;
$stmt = oci_parse($db, $query);
if(!empty($stmt)) {
$res = oci_execute($stmt, OCI_DESCRIBE_ONLY|OCI_NO_AUTO_COMMIT);
if(!empty($res)) {
$name = oci_field_name($res, 1);
if(!empty($name)) {
$valid = true;
}
}
oci_rollback($db);
}
But if $query has some DDL in it, I understand that Oracle would commit it immediately. So is there any way to check the query without any modifications happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在包中创建查询。如果创建包时没有错误,则查询一定是正确的。
基于角色的权限可能会导致任何语法检查出现问题。在 runtil 之前不会检查基于角色的权限,因此查询可能在设计时正确(因为开发人员已被授予对表的直接权限),但在运行时会失败(因为用户已被授予相同的权限)一个角色)。
You could create the query in a Package. If the package is created without error then the query must be correct.
Role based rights that can cause issues with any syntax check. Role based rights are not checked until runtil, so it is possible for a query to be correct at design time (because the developer has been granted direct rights to the tables) but fail at runtime (because the user has been granted the same rights in a role).
第 1 步:
在 PHP 代码上,创建一个包含文本的字符串:
第 2 步:
创建一个包含要验证的 SQL 查询的变量:
第 3 步:< /strong>
将字符串内容“{MY_QUERY}”替换为您要验证的SQL语句的内容:
STEP 4:
执行验证查询:
STEP 5:
验证如果最后一次调用 (DB.EXECUTE.....) 引发错误。
如果是,则查询无效
如果否,则查询有效
STEP 1:
on your PHP code, create a string containing the text:
STEP 2:
create a variable containing the SQLquery you want to validate:
STEP 3:
replace the string content "{MY_QUERY}" with the content of the SQL statement you want to validate:
STEP 4:
execute the validation query:
STEP 5:
verify if the last call (DB.EXECUTE.....) raise an error.
if yes, the query is invalid
if no , the query is valid