update
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
"""Conservative projection audit of registered, fixed MyBatis SELECTs. Never generates SQL."""
|
||||
from pathlib import Path
|
||||
import re,xml.etree.ElementTree as ET
|
||||
from functools import lru_cache
|
||||
|
||||
@lru_cache(maxsize=None)
|
||||
def mapper(path):return ET.parse(path).getroot()
|
||||
|
||||
def returned_properties(root_path,steps,fields):
|
||||
known=set();reviewed=False
|
||||
for step in steps:
|
||||
root=mapper(str(Path(root_path)/step['path']));select=root.find("select[@id='"+step['id']+"']")
|
||||
if select is None:continue
|
||||
unresolved=[]
|
||||
def text(node,depth=0):
|
||||
if depth>15:unresolved.append(True);return ''
|
||||
parts=[node.text or '']
|
||||
for child in node:
|
||||
if child.tag=='include':
|
||||
ident=child.attrib.get('refid','');fragment=root.find("sql[@id='"+ident+"']")
|
||||
if fragment is None:unresolved.append(True)
|
||||
else:parts.append(text(fragment,depth+1))
|
||||
else:parts.append(text(child,depth+1))
|
||||
parts.append(child.tail or '')
|
||||
return ' '.join(parts)
|
||||
sql=text(select)
|
||||
if unresolved or re.match(r'\s*with\b',sql,re.I):continue
|
||||
sql=re.sub(r'/\*[\s\S]*?\*/|--[^\n]*',' ',sql)
|
||||
first=re.search(r'\bselect\b',sql,re.I)
|
||||
if not first:continue
|
||||
start=first.end();depth=0;quote=None;end=None
|
||||
for i in range(start,len(sql)):
|
||||
ch=sql[i]
|
||||
if quote:
|
||||
if ch==quote and (i==0 or sql[i-1]!='\\'):quote=None
|
||||
continue
|
||||
if ch in "'\"`":quote=ch;continue
|
||||
if ch=='(':depth+=1
|
||||
elif ch==')':depth-=1
|
||||
elif depth==0 and re.match(r'from\b',sql[i:],re.I):end=i;break
|
||||
if end is None:continue
|
||||
body=sql[start:end];segments=[];last=0;depth=0;quote=None
|
||||
for i,ch in enumerate(body):
|
||||
if quote:
|
||||
if ch==quote and (i==0 or body[i-1]!='\\'):quote=None
|
||||
elif ch in "'\"`":quote=ch
|
||||
elif ch=='(':depth+=1
|
||||
elif ch==')':depth-=1
|
||||
elif ch==',' and depth==0:segments.append(body[last:i]);last=i+1
|
||||
segments.append(body[last:]);columns=set();wildcard=False
|
||||
for segment in segments:
|
||||
segment=re.sub(r'^\s*distinct\s+','',segment.strip(),flags=re.I)
|
||||
if re.fullmatch(r'(?:\w+\.)?\*',segment):wildcard=True;continue
|
||||
alias=re.search(r'(?:\bas\s+|\s+)([\w]+)\s*$',segment,re.I)
|
||||
column=alias.group(1) if alias else segment.rsplit('.',1)[-1].strip(' `"')
|
||||
if re.fullmatch(r'\w+',column):columns.add(column.lower())
|
||||
maps={}
|
||||
def result_map(ident,seen=None):
|
||||
seen=set() if seen is None else seen
|
||||
if ident in seen:return
|
||||
seen.add(ident);node=root.find("resultMap[@id='"+ident+"']")
|
||||
if node is None:return
|
||||
if node.attrib.get('extends'):result_map(node.attrib['extends'],seen)
|
||||
for child in node:
|
||||
if child.tag in {'result','id'} and child.attrib.get('property'):maps[child.attrib['property']]=child.attrib.get('column','').lower()
|
||||
for ident in select.attrib.get('resultMap','').split(','):result_map(ident.strip())
|
||||
if wildcard and not maps:continue
|
||||
reviewed=True
|
||||
for field in fields.values():
|
||||
prop=field['property'];snake=re.sub(r'(?<!^)(?=[A-Z])','_',prop).lower()
|
||||
if wildcard and prop in maps or maps.get(prop) in columns or snake in columns or prop.lower() in columns:known.add(prop)
|
||||
return known if reviewed else None
|
||||
Reference in New Issue
Block a user