我正在使用 moz_sql_parser 来比较两个 SQL 查询。和 DeepDiff 来查看两个查询之间的差异。
我想要一种不同类型的字典,如下所示,以进行更好的比较。
from moz_sql_parser import parse
import json
from jsondiff import diff
from deepdiff import DeepDiff
import pprint
sqlr = "SELECT distinct c.nome FROM compositor c, faixas f, artista ar, album a, fx_comp fx WHERE fx.compositor = c.compositor_id AND fx.faixa_id = f.faixa_id AND f.album = a.album_id AND (a.titulo = 'Help!' OR a.titulo = 'Abbey Road') AND ar.nome = 'The Beatles' AND f.faixa_numero < 6 Order By c.nome asc;"
sqlr1 = "SELECT c.nome FROM artista ar, album a, faixas f, compositor c, fx_comp fx WHERE fx.compositor = c.compositor_id AND fx.faixa_id = f.faixa_id AND f.album = a.album_id AND (a.titulo = 'Help!' OR a.titulo = 'Abbey Road') AND ar.nome = 'The Beatles' AND f.faixa_numero < 6 Order By c.nome desc;"
sqlr2 = "SELECT * FROM (SELECT * FROM ALBUM as x ,ARTISTA as pto WHERE ARTISTA.ARTISTA_ID = ALBUM.ARTISTA AND ARTISTA.ARTISTA_ID = 4) AS B;"
#print(json.dumps(parse(sql8), sort_keys=True, indent=4))
data1 = json.loads(json.dumps(parse(sqlr.lower()),sort_keys=True))
data2 = json.loads(json.dumps(parse(sqlr1.lower()),sort_keys=True))
pprint.pprint(data1)
print('###################################')
#data3 = diff(data2, data1)
ddiff = DeepDiff(data1,data2, ignore_order=True)
pprint.pprint((ddiff))
print(ddiff['type_changes']["root['select']['value']"])
#print(type(data1['select']['value']))
在当前版本中,data1 和 data2 是这样的: 并解析字典的数据,使其更清晰和可用。
DATA1
{'from': [{'name': 'c', 'value': 'compositor'},
{'name': 'f', 'value': 'faixas'},
{'name': 'ar', 'value': 'artista'},
{'name': 'a', 'value': 'album'},
{'name': 'fx', 'value': 'fx_comp'}],
'orderby': {'sort': 'asc', 'value': 'c.nome'},
'select': {'value': {'distinct': 'c.nome'}},
'where': {'and': [{'eq': ['fx.compositor', 'c.compositor_id']},
{'eq': ['fx.faixa_id', 'f.faixa_id']},
{'eq': ['f.album', 'a.album_id']},
{'or': [{'eq': ['a.titulo', {'literal': 'help!'}]},
{'eq': ['a.titulo', {'literal': 'abbey road'}]}]},
{'eq': ['ar.nome', {'literal': 'the beatles'}]},
{'lt': ['f.faixa_numero', 6]}]}}
DATA2
{'from': [{'name': 'ar', 'value': 'artista'},
{'name': 'a', 'value': 'album'},
{'name': 'f', 'value': 'faixas'},
{'name': 'c', 'value': 'compositor'},
{'name': 'fx', 'value': 'fx_comp'}],
'orderby': {'sort': 'desc', 'value': 'c.nome'},
'select': {'value': 'c.nome'},
'where': {'and': [{'eq': ['fx.compositor', 'c.compositor_id']},
{'eq': ['fx.faixa_id', 'f.faixa_id']},
{'eq': ['f.album', 'a.album_id']},
{'or': [{'eq': ['a.titulo', {'literal': 'help!'}]},
{'eq': ['a.titulo', {'literal': 'abbey road'}]}]},
{'eq': ['ar.nome', {'literal': 'the beatles'}]},
{'lt': ['f.faixa_numero', 6]}]}}
但它一直在变化。所以字典的结构从来都不一样,巫婆让解析变得一团糟。
我想将 data1 和 data2 转换为:
[['Table1','Alias','variable1associatedwithtable1','variable1associatedwithtable12',],
['Table2name','Alias','variable1associatedwithtable2name','variable1associatedwithtable2name']]
对于表和它们自己的变量,包括使用的别名
[['Table2name','function','variable_associated_with_table_name'],