Skip to content

Commit

Permalink
fix: fix query sql parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Asgmel03 committed Nov 19, 2024
1 parent 5a7ccc2 commit 7a764f3
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions tagreader/web_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,6 @@ def fetch(
txt = res.text.replace('"v":nan', '"v":NaN').replace('"v":-nan', '"v":NaN')
return json.loads(txt)

def fetch_text(
self, url, params: Optional[Union[str, Dict[str, str]]] = None
) -> str:
res = self.session.get(url, params=params)
res.raise_for_status()

return res.text

def connect(self):
try:
self.verify_connection(self.datasource)
Expand Down Expand Up @@ -667,10 +659,12 @@ def generate_sql_query(
'dso="CHARINT=N;CHARFLOAT=N;CHARTIME=N;CONVERTERRORS=N" '
f'm="{max_rows}" to="30" s="1">'
)
query = query.replace("\t"," ").replace('\n'," ") # Replace new lines and tabs that are typical in formatted SQL queries with spaces.

query = query.replace("\t", " ").replace(
"\n", " "
) # Replace new lines and tabs that are typical in formatted SQL queries with spaces.

# Need a solution to LIKE comments. These have a % symbol in the query that does not seem to pass through the request

connection_string += f"<![CDATA[{query}]]></SQL>"
return connection_string

Expand Down Expand Up @@ -709,21 +703,23 @@ def query_sql(self, query: str, parse: bool = True) -> Union[str, pd.DataFrame]:
max_rows=self._max_rows,
datasource=None,
)
res_text = self.fetch_text(url, params=params)
# For now just return result as text regardless of value of parse

res = self.session.get(url, params=params)
res.raise_for_status()

if parse:
dict = res.json()['data'][0]
parsed_dict = res.json()["data"][0]

cols = []
for i in dict['cols']:
cols.append(i['n'])
for i in parsed_dict["cols"]:
cols.append(i["n"])

rows = []
for i in dict['rows']:

for i in parsed_dict["rows"]:
element = []
for j in i['fld']:
element.append(j['v'])
for j in i["fld"]:
element.append(j["v"])
rows.append(element)
return pd.DataFrame(data=rows, columns=cols)
return res.text
Expand Down

0 comments on commit 7a764f3

Please sign in to comment.