-->

Change Oracle SQL Query Text before submission [cl

2020-07-24 09:01发布

问题:

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 7 years ago.

Is there a advanced oracle feature to modify the SQL Query just before it is submitted to oracle? Or may be modify the result set? Based on certain condition? Like may be based on the name of the process that submits the query? Or based on the query text?

The scenario is that a process (written in C++) is running a query and we don't have a way to change the C++ code. A particular business validation within that process is failing and in order to make the validation not fail, I want a way to manipulate the returned data set or manipulate the select query just before it is submitted to oracle engine.

The C++ code is running this exact query

SELECT PL_ID FROM A_HDR WHERE ENT_NBR=''

I want to change it such that ORACLE always sees and executes the following query instead

SELECT NULL FROM A_HDR WHERE ENT_NBR=''

Please guide

回答1:

Oracle Virtual Private Database may be what you're looking for. From the manual:

Oracle Virtual Private Database (VPD) enables you to create security policies to control database access at the row and column level. Essentially, Oracle Virtual Private Database adds a dynamic WHERE clause to a SQL statement that is issued against the table, view, or synonym to which an Oracle Virtual Private Database security policy was applied.

You may be able to get the process and query text from SYS_CONTEXT. For example, sys_context('userenv', 'current_sql') and sys_context('userenv', 'module'). You may also need to use GV$SESSION to get more information.

However, I think you should try to avoid VPD if possible. It is extremely confusing to have all your SQL statements silently converted in the background. It can make development and troubleshooting very difficult.


Update

The SQL Translation Framework feature in Oracle 12c would probably be the best solution for this problem.