Parse sql select statement to fetch the where clause conditions in pythonReplacements for switch statement in Python?Styling multi-line conditions in 'if' statements?How do I parse a string to a float or int?Does Python have a ternary conditional operator?INNER JOIN ON vs WHERE clauseHow do I parse XML in Python?Why can't Python parse this JSON data?Else clause on Python while statement'IF' in 'SELECT' statement - choose output value based on column valuesSQL select only rows with max value on a column
Test to know when to use GLM over Linear Regression?
How to convert Mn2O3 to Mn3O4?
Statistical tests for benchmark comparison
Is my sink P-trap too low?
Are there objective criteria for classifying consonance v. dissonance?
Hobby function generators
What is the source of "You can achieve a lot with hate, but even more with love" (Shakespeare?)
How to ensure that neurotic or annoying characters don't get tiring on the long run
Why is the return value of the fun function 8 instead of 7?
'Overwrote' files, space still occupied, are they lost?
Other than good shoes and a stick, what are some ways to preserve your knees on long hikes?
What does the Free Recovery sign (UK) actually mean?
Extra initial Aeneid lines in 1662 M. de Marolles version
Why does an orbit become hyperbolic when total orbital energy is positive?
What are the typical trumpet parts in classical music?
Is there a generally agreed upon solution to Bradley's Infinite Regress without appeal to Paraconsistent Logic?
How to give my students a straightedge instead of a ruler
How to make classical firearms effective on space habitats despite the coriolis effect?
Updating without Composer
How to generate short fixed length cryptographic hashs?
Does Forgotten Realms setting count as “High magic”?
What was the ultimate objective of The Party in 1984?
Wrong Schengen Visa exit stamp on my passport, who can I complain to?
Compiling Blender with Optix on Ubuntu
Parse sql select statement to fetch the where clause conditions in python
Replacements for switch statement in Python?Styling multi-line conditions in 'if' statements?How do I parse a string to a float or int?Does Python have a ternary conditional operator?INNER JOIN ON vs WHERE clauseHow do I parse XML in Python?Why can't Python parse this JSON data?Else clause on Python while statement'IF' in 'SELECT' statement - choose output value based on column valuesSQL select only rows with max value on a column
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a sql query and I want to fetch all the conditions in where clause into a Python dictionary.
e.g.,
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):
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
Following is the output
'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 <> 'US'", 'key': 'country_code'
'value': u'hire_date < NOW()', 'key': 'hire_date'
'value': u'email_work', 'key': 'email_work'
'value': u'LENGTH(email_work) > 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'
The problem here is with the IN operator.
Check job_profile_id, it doesn't contain the list.
On debugging, it doesn't show the list.
I am unable to resolve this issue.
Please help.
Appreciate the help.
python mysql parsing dictionary sql-parser
add a comment
|
I have a sql query and I want to fetch all the conditions in where clause into a Python dictionary.
e.g.,
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):
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
Following is the output
'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 <> 'US'", 'key': 'country_code'
'value': u'hire_date < NOW()', 'key': 'hire_date'
'value': u'email_work', 'key': 'email_work'
'value': u'LENGTH(email_work) > 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'
The problem here is with the IN operator.
Check job_profile_id, it doesn't contain the list.
On debugging, it doesn't show the list.
I am unable to resolve this issue.
Please help.
Appreciate the help.
python mysql parsing dictionary sql-parser
add a comment
|
I have a sql query and I want to fetch all the conditions in where clause into a Python dictionary.
e.g.,
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):
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
Following is the output
'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 <> 'US'", 'key': 'country_code'
'value': u'hire_date < NOW()', 'key': 'hire_date'
'value': u'email_work', 'key': 'email_work'
'value': u'LENGTH(email_work) > 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'
The problem here is with the IN operator.
Check job_profile_id, it doesn't contain the list.
On debugging, it doesn't show the list.
I am unable to resolve this issue.
Please help.
Appreciate the help.
python mysql parsing dictionary sql-parser
I have a sql query and I want to fetch all the conditions in where clause into a Python dictionary.
e.g.,
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):
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
Following is the output
'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 <> 'US'", 'key': 'country_code'
'value': u'hire_date < NOW()', 'key': 'hire_date'
'value': u'email_work', 'key': 'email_work'
'value': u'LENGTH(email_work) > 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'
The problem here is with the IN operator.
Check job_profile_id, it doesn't contain the list.
On debugging, it doesn't show the list.
I am unable to resolve this issue.
Please help.
Appreciate the help.
python mysql parsing dictionary sql-parser
python mysql parsing dictionary sql-parser
edited Mar 28 at 13:03
Krishnachandra Sharma
asked Mar 28 at 12:54
Krishnachandra SharmaKrishnachandra Sharma
1,0721 gold badge17 silver badges38 bronze badges
1,0721 gold badge17 silver badges38 bronze badges
add a comment
|
add a comment
|
1 Answer
1
active
oldest
votes
This is because the tree structure is different for IN keywords and comparisons. For example, a comparison includes the entire expression underneath it in the tree.
If you use parsed[0]._pprint_tree() you can see everything nested under a Comparison node:
|- 2 Comparison 'employ...'
| |- 0 Identifier 'employ...'
| | `- 0 Name 'employ...'
| |- 1 Whitespace ' '
| |- 2 Comparison '='
| |- 3 Whitespace ' '
| `- 4 Single ''Emplo...'
However, the NOT IN clause is a series of sequential nodes:
|- 36 Identifier 'job_pr...'
| `- 0 Name 'job_pr...'
|- 37 Whitespace ' '
|- 38 Keyword 'NOT'
|- 39 Whitespace ' '
|- 40 Keyword 'IN'
|- 41 Whitespace ' '
|- 42 Parenthesis '('2099...'
| |- 0 Punctuation '('
| |- 1 IdentifierList ''20992...'
| | |- 0 Single "'20992'"
| | |- 1 Punctuation ','
| | |- 2 Whitespace ' '
| | |- 3 Single "'20993'"
| | |- 4 Punctuation ','
| | |- 5 Whitespace ' '
| | |- 6 Single "'20994'"
| | |- 7 Punctuation ','
| | |- 8 Whitespace ' '
| | |- 9 Single "'20995'"
| | |- 10 Punctuation ','
| | |- 11 Whitespace ' '
| | |- 12 Single "'20996'"
| | |- 13 Punctuation ','
| | |- 14 Whitespace ' '
| | `- 15 Single "'20997'"
| `- 2 Punctuation ')'
Your best bet is to watch for identifiers, then jump ahead and save the value of the next parenthesis node. While this doesn't handle every possible situation, it does handle your SQL statement and returns the value of job_profile_id.
Here's my modified 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 <> '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
Awesome! Works!
– Krishnachandra Sharma
Mar 28 at 13:38
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55398189%2fparse-sql-select-statement-to-fetch-the-where-clause-conditions-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is because the tree structure is different for IN keywords and comparisons. For example, a comparison includes the entire expression underneath it in the tree.
If you use parsed[0]._pprint_tree() you can see everything nested under a Comparison node:
|- 2 Comparison 'employ...'
| |- 0 Identifier 'employ...'
| | `- 0 Name 'employ...'
| |- 1 Whitespace ' '
| |- 2 Comparison '='
| |- 3 Whitespace ' '
| `- 4 Single ''Emplo...'
However, the NOT IN clause is a series of sequential nodes:
|- 36 Identifier 'job_pr...'
| `- 0 Name 'job_pr...'
|- 37 Whitespace ' '
|- 38 Keyword 'NOT'
|- 39 Whitespace ' '
|- 40 Keyword 'IN'
|- 41 Whitespace ' '
|- 42 Parenthesis '('2099...'
| |- 0 Punctuation '('
| |- 1 IdentifierList ''20992...'
| | |- 0 Single "'20992'"
| | |- 1 Punctuation ','
| | |- 2 Whitespace ' '
| | |- 3 Single "'20993'"
| | |- 4 Punctuation ','
| | |- 5 Whitespace ' '
| | |- 6 Single "'20994'"
| | |- 7 Punctuation ','
| | |- 8 Whitespace ' '
| | |- 9 Single "'20995'"
| | |- 10 Punctuation ','
| | |- 11 Whitespace ' '
| | |- 12 Single "'20996'"
| | |- 13 Punctuation ','
| | |- 14 Whitespace ' '
| | `- 15 Single "'20997'"
| `- 2 Punctuation ')'
Your best bet is to watch for identifiers, then jump ahead and save the value of the next parenthesis node. While this doesn't handle every possible situation, it does handle your SQL statement and returns the value of job_profile_id.
Here's my modified 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 <> '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
Awesome! Works!
– Krishnachandra Sharma
Mar 28 at 13:38
add a comment
|
This is because the tree structure is different for IN keywords and comparisons. For example, a comparison includes the entire expression underneath it in the tree.
If you use parsed[0]._pprint_tree() you can see everything nested under a Comparison node:
|- 2 Comparison 'employ...'
| |- 0 Identifier 'employ...'
| | `- 0 Name 'employ...'
| |- 1 Whitespace ' '
| |- 2 Comparison '='
| |- 3 Whitespace ' '
| `- 4 Single ''Emplo...'
However, the NOT IN clause is a series of sequential nodes:
|- 36 Identifier 'job_pr...'
| `- 0 Name 'job_pr...'
|- 37 Whitespace ' '
|- 38 Keyword 'NOT'
|- 39 Whitespace ' '
|- 40 Keyword 'IN'
|- 41 Whitespace ' '
|- 42 Parenthesis '('2099...'
| |- 0 Punctuation '('
| |- 1 IdentifierList ''20992...'
| | |- 0 Single "'20992'"
| | |- 1 Punctuation ','
| | |- 2 Whitespace ' '
| | |- 3 Single "'20993'"
| | |- 4 Punctuation ','
| | |- 5 Whitespace ' '
| | |- 6 Single "'20994'"
| | |- 7 Punctuation ','
| | |- 8 Whitespace ' '
| | |- 9 Single "'20995'"
| | |- 10 Punctuation ','
| | |- 11 Whitespace ' '
| | |- 12 Single "'20996'"
| | |- 13 Punctuation ','
| | |- 14 Whitespace ' '
| | `- 15 Single "'20997'"
| `- 2 Punctuation ')'
Your best bet is to watch for identifiers, then jump ahead and save the value of the next parenthesis node. While this doesn't handle every possible situation, it does handle your SQL statement and returns the value of job_profile_id.
Here's my modified 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 <> '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
Awesome! Works!
– Krishnachandra Sharma
Mar 28 at 13:38
add a comment
|
This is because the tree structure is different for IN keywords and comparisons. For example, a comparison includes the entire expression underneath it in the tree.
If you use parsed[0]._pprint_tree() you can see everything nested under a Comparison node:
|- 2 Comparison 'employ...'
| |- 0 Identifier 'employ...'
| | `- 0 Name 'employ...'
| |- 1 Whitespace ' '
| |- 2 Comparison '='
| |- 3 Whitespace ' '
| `- 4 Single ''Emplo...'
However, the NOT IN clause is a series of sequential nodes:
|- 36 Identifier 'job_pr...'
| `- 0 Name 'job_pr...'
|- 37 Whitespace ' '
|- 38 Keyword 'NOT'
|- 39 Whitespace ' '
|- 40 Keyword 'IN'
|- 41 Whitespace ' '
|- 42 Parenthesis '('2099...'
| |- 0 Punctuation '('
| |- 1 IdentifierList ''20992...'
| | |- 0 Single "'20992'"
| | |- 1 Punctuation ','
| | |- 2 Whitespace ' '
| | |- 3 Single "'20993'"
| | |- 4 Punctuation ','
| | |- 5 Whitespace ' '
| | |- 6 Single "'20994'"
| | |- 7 Punctuation ','
| | |- 8 Whitespace ' '
| | |- 9 Single "'20995'"
| | |- 10 Punctuation ','
| | |- 11 Whitespace ' '
| | |- 12 Single "'20996'"
| | |- 13 Punctuation ','
| | |- 14 Whitespace ' '
| | `- 15 Single "'20997'"
| `- 2 Punctuation ')'
Your best bet is to watch for identifiers, then jump ahead and save the value of the next parenthesis node. While this doesn't handle every possible situation, it does handle your SQL statement and returns the value of job_profile_id.
Here's my modified 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 <> '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
This is because the tree structure is different for IN keywords and comparisons. For example, a comparison includes the entire expression underneath it in the tree.
If you use parsed[0]._pprint_tree() you can see everything nested under a Comparison node:
|- 2 Comparison 'employ...'
| |- 0 Identifier 'employ...'
| | `- 0 Name 'employ...'
| |- 1 Whitespace ' '
| |- 2 Comparison '='
| |- 3 Whitespace ' '
| `- 4 Single ''Emplo...'
However, the NOT IN clause is a series of sequential nodes:
|- 36 Identifier 'job_pr...'
| `- 0 Name 'job_pr...'
|- 37 Whitespace ' '
|- 38 Keyword 'NOT'
|- 39 Whitespace ' '
|- 40 Keyword 'IN'
|- 41 Whitespace ' '
|- 42 Parenthesis '('2099...'
| |- 0 Punctuation '('
| |- 1 IdentifierList ''20992...'
| | |- 0 Single "'20992'"
| | |- 1 Punctuation ','
| | |- 2 Whitespace ' '
| | |- 3 Single "'20993'"
| | |- 4 Punctuation ','
| | |- 5 Whitespace ' '
| | |- 6 Single "'20994'"
| | |- 7 Punctuation ','
| | |- 8 Whitespace ' '
| | |- 9 Single "'20995'"
| | |- 10 Punctuation ','
| | |- 11 Whitespace ' '
| | |- 12 Single "'20996'"
| | |- 13 Punctuation ','
| | |- 14 Whitespace ' '
| | `- 15 Single "'20997'"
| `- 2 Punctuation ')'
Your best bet is to watch for identifiers, then jump ahead and save the value of the next parenthesis node. While this doesn't handle every possible situation, it does handle your SQL statement and returns the value of job_profile_id.
Here's my modified 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 <> '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
answered Mar 28 at 13:27
user2340724user2340724
1262 bronze badges
1262 bronze badges
Awesome! Works!
– Krishnachandra Sharma
Mar 28 at 13:38
add a comment
|
Awesome! Works!
– Krishnachandra Sharma
Mar 28 at 13:38
Awesome! Works!
– Krishnachandra Sharma
Mar 28 at 13:38
Awesome! Works!
– Krishnachandra Sharma
Mar 28 at 13:38
add a comment
|
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55398189%2fparse-sql-select-statement-to-fetch-the-where-clause-conditions-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown