这是因为在关键字和比较中,树结构是不同的。例如,比较包括树中它下面的整个表达式。在
如果使用parsed[0]._pprint_tree(),则可以看到嵌套在比较节点下的所有内容:|- 2 Comparison 'employ...'
| |- 0 Identifier 'employ...'
| | `- 0 Name 'employ...'
| |- 1 Whitespace ' '
| |- 2 Comparison '='
| |- 3 Whitespace ' '
| `- 4 Single ''Emplo...'
但是,NOT IN子句是一系列顺序节点:
^{pr2}$
最好的办法是观察标识符,然后向前跳并保存下一个括号节点的值。虽然这并不能处理所有可能的情况,但它确实处理您的SQL语句并返回job_profile_id的值。在
这是我修改过的代码:import sqlparse
s = "select count(*) from users where employee_type = 'Employee' AND (employment_status = 'Active' OR employment_status = 'On Leave') AND (time_type='Full time' OR country_code <> 'US') AND hire_date < NOW() AND email_work IS NOT NULL AND LENGTH(email_work) > 0 AND NOT (job_profile_id IN ('8802 - Comm Ops - 1', '8801 - CityOps - 2', '10034', '10455', '21014', '21015', '21016', '21018', '21017', '21019') AND country_code = 'IE') AND job_profile_id NOT IN ('20992', '20993', '20994', '20995', '20996', '20997') AND country_code NOT IN ('CN', 'MO', 'SG', 'MY', 'TH', 'VN', 'MM', 'KH', 'PH', 'ID')"
parsed = sqlparse.parse(s)
where = parsed[0][-1]
sql_tokens = []
def get_tokens(where):
identifier = None
for i in where.tokens:
try:
name = i.get_real_name()
if name and isinstance(i, sqlparse.sql.Identifier):
identifier = i
elif identifier and isinstance(i, sqlparse.sql.Parenthesis):
sql_tokens.append({
'key': str(identifier),
'value': token.value
})
elif name:
identifier = None
# sql_tokens.append("{0} - {1} - {2}".format(str(i), str(name), i.value))
sql_tokens.append({
'key': str(name),
'value': u''.join(token.value for token in i.flatten()),
})
else:
get_tokens(i)
except Exception as e:
pass
get_tokens(where)
print sql_tokens