title: Parse sql select statement to fetch the where clause conditions in python
body: <p>I have a sql query and I want to fetch all the conditions in <code>where</code> clause into a Python dictionary.</p>
  
  <p>e.g.,</p>
  
  <pre><code>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 &lt;&gt; 'US') AND hire_date &lt; NOW() AND email_work IS NOT NULL AND LENGTH(email_work) &gt; 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):
      for i in where.tokens:
          try:
              name = i.get_real_name()
              if name and not isinstance(i, sqlparse.sql.Parenthesis):
                  # sql_tokens.append("{0} - {1} - {2}".format(str(i), str(name), i.value))
                  sql_tokens.append({
                      'key': str(name),
                      'value': i.value,
                  })
              else:
                  get_tokens(i)
          except Exception as e:
              pass
  
  
  get_tokens(where)
  for i in sql_tokens:
      print i
  
  </code></pre>
  
  <p>Following is the output</p>
  
  <pre><code>{'value': u"employee_type = 'Employee'", 'key': 'employee_type'}
  {'value': u"employment_status = 'Active'", 'key': 'employment_status'}
  {'value': u"employment_status = 'On Leave'", 'key': 'employment_status'}
  {'value': u"time_type='Full time'", 'key': 'time_type'}
  {'value': u"country_code &lt;&gt; 'US'", 'key': 'country_code'}
  {'value': u'hire_date &lt; NOW()', 'key': 'hire_date'}
  {'value': u'email_work', 'key': 'email_work'}
  {'value': u'LENGTH(email_work) &gt; 0', 'key': 'LENGTH'}
  {'value': u'job_profile_id', 'key': 'job_profile_id'}
  {'value': u"country_code = 'IE'", 'key': 'country_code'}
  {'value': u'job_profile_id', 'key': 'job_profile_id'}
  {'value': u'country_code', 'key': 'country_code'}
  
  </code></pre>
  
  <p>The problem here is with the <code>IN</code> operator.
  Check <code>job_profile_id</code>, it doesn't contain the list.</p>
  
  <p>On debugging, it doesn't show the list.</p>
  
  <p>I am unable to resolve this issue.</p>
  
  <p>Please help.</p>
  
  <p>Appreciate the help.</p>