forked from arq5x/gemini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_utils.py
More file actions
46 lines (35 loc) · 1.28 KB
/
Copy pathsql_utils.py
File metadata and controls
46 lines (35 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
these are utilities to parse and transform SQL statements
"""
import re
def get_select_cols_and_rest(query):
"""
Separate the a list of selected columns from
the rest of the query
Returns:
1. a list of the selected columns
2. a string of the rest of the query after the SELECT
"""
from_loc = query.lower().find("from")
raw_select_clause = query[0:from_loc].rstrip()
rest_of_query = query[from_loc:len(query)]
# remove the SELECT keyword from the query
select_pattern = re.compile("select", re.IGNORECASE)
raw_select_clause = select_pattern.sub('', raw_select_clause)
# now create and iterate through a list of of the SELECT'ed columns
selected_columns = raw_select_clause.split(',')
selected_columns = [c.strip() for c in selected_columns]
return selected_columns, rest_of_query
def ensure_columns(query, cols):
"""
if a query is missing any of these list of columns, add them
and return the new query string
"""
sel_cols, rest = get_select_cols_and_rest(query)
sel_cols = [x.lower() for x in sel_cols]
for c in cols:
c = c.lower()
if c not in sel_cols:
sel_cols += [c]
sel_string = ", ".join(sel_cols)
return "select {sel_string} {rest}".format(**locals())