简体   繁体   English

从Oracle查询中提取表引用

[英]Extract Table References from Oracle Queries

What I am Doing: 我在做什么:

I am not very experienced with Linux but am using it for an internal api for my company. 我对Linux不是很有经验,但我正在将它用于我公司的内部api。 Through the API I have an array of 79 (expecting a total of 550) PL/SQL statements for which I need to get each table referenced in each statement. 通过API,我有一个79(期望总共550个)PL / SQL语句的数组,我需要在每个语句中引用每个表。 I have tried reducing each statement to the text between the FROM and WHERE clause, however some statements have from in quotes in their SELECT statement and there are a large number of sub-queries so not every item following FROM is a table. 我已经尝试将每个语句减少到FROM和WHERE子句之间的文本,但是有些语句来自SELECT语句中的引号,并且存在大量子查询,因此不是FROM之后的每个项都是表。

Question: 题:

Is there a parser or a known series of regex patterns I could use to parse out the table references? 是否有解析器或已知的一系列正则表达式模式我可以用来解析表引用? I know this pattern would be vast and am hoping someone has done this work already and if so, does anyone know where it is documented? 我知道这种模式会非常广泛,我希望有人已经完成了这项工作,如果有的话,是否有人知道它在哪里被记录?

Here is what I have tried so far: 这是我到目前为止所尝试的:

#1 This returns the IDs for each profile I need to reference
IDs=(`echo $(APICommand_GetIDs) | jq '.[] | select(.schedule.type!="NOT_SCHEDULED") | .profileId' |sort | uniq`)

#2 Itterate through the IDs and get the sql statement for each ID. Also has a percentage complete counter
profiles=(); i=0; for ID in "${IDs[@]}"; do profiles+=("`echo $(APICommand_GetProfiles) | jq '.sql'`"); let i+=1; status=`expr $i \* 100 / ${#IDs[@]}`; printf "$status \r"; done; printf "$complete \n";

#3 Itterate through each profile to get line between "FROM" and "WHERE"
echo ${profiles[@]} | sed 's/\\r\\n/ /g' | sed 's/\\t/ /g' | sed 's/%//g' | sed 's/  / /g' | sed 's/.*FROM //' | sed 's/WHERE.*//'

However #3 returns the PL/SQL statement in multiple lines which makes it difficult to parse. 但是#3以多行返回PL / SQL语句,这使得难以解析。 I'm also having difficulty coming up with the regex patterns to only get proper table references. 我也很难提出正则表达式模式只能获得正确的表引用。

It appears as if you are trying to use an OS script to find these dependencies. 看起来好像您正在尝试使用OS脚本来查找这些依赖项。 If these are pl/sql statements, and they are in a procedure, package, or function, you can use the table PUBLIC_DEPENDENCY to find all dependencies to the objects. 如果这些是pl / sql语句,并且它们位于过程,包或函数中,则可以使用表PUBLIC_DEPENDENCY来查找对象的所有依赖项。 Link the object_id's in this table to DBA_OBJECTS. 将此表中的object_id链接到DBA_OBJECTS。

Here is a SQL example showing the dependencies on my package 'ROUTINE_PKG'. 这是一个SQL示例,显示了我的包'ROUTINE_PKG'的依赖关系。

WITH cte (owner, object_type, object_name
        , object_id, lvl)
     AS (SELECT owner
              , object_type
              , object_name
              , object_id
              , 1 lvl
           FROM dba_objects
          WHERE owner = 'BRIANL'
            AND object_name = 'ROUTINE_PKG'
         UNION ALL
         SELECT do.owner
              , do.object_type
              , do.object_name
              , do.object_id
              , lvl + 1
           FROM public_dependency pd
                INNER JOIN cte
                    ON pd.referenced_object_id = cte.object_id
                inner join dba_objects  do on do.object_id = pd.object_id where cte.lvl <= 5)
SELECT distinct *
  FROM cte order by lvl, owner, object_name;


OWNER  OBJECT_TYPE  OBJECT_NAME OBJECT_ID LVL  
BRIANL PACKAGE      ROUTINE_PKG 148593     1  
BRIANL PACKAGE BODY ROUTINE_PKG 148594     1  
BRIANL PROCEDURE    ROUTINE3    148590     2  
BRIANL PACKAGE BODY ROUTINE_PKG 148594     2  
BRIANL PROCEDURE    ROUTINE2    148591     3  
BRIANL PROCEDURE    ROUTINE1    148592     4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM