Skip to content

Commit

Permalink
Update web_handlers.py (#181)
Browse files Browse the repository at this point in the history
* Update web_handlers.py

Hi, was trying to find how to handle LIKE statements in the SQL query via the REST API and came across your great library. I notice that you don't current have some code to parse SQL queries into a dataframe so have suggested some code...

* Update web_handlers.py

Added processing of SQL query to repalce tabs and new lines with spaces that are typical of formatted SQL queries.  Note that LIKE statments do not seem to be compatible with the requests. I suspect this is to do with the % symbol not being passed. See my Stack Overflow question https://stackoverflow.com/questions/76063389/percent-symbol-in-python-requests-causing-query-to-not-work?noredirect=1#comment134147387_76063389

---------

Co-authored-by: Åsmund Våge Fannemel <34712686+asmfstatoil@users.noreply.github.com>
  • Loading branch information
RhymesWith0range and asmfstatoil authored Nov 18, 2024
1 parent f851de9 commit 48a0915
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions tagreader/web_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,10 @@ 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.

# 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 @@ -706,10 +709,21 @@ def query_sql(self, query: str, parse: bool = True) -> Union[str, pd.DataFrame]:
res_text = self.fetch_text(url, params=params)
# For now just return result as text regardless of value of parse
if parse:
raise NotImplementedError(
"Use parse=False to receive and handle text result instead"
)
return res_text
dict = res.json()['data'][0]

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

rows = []

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


class PIHandlerWeb(BaseHandlerWeb):
Expand Down

0 comments on commit 48a0915

Please sign in to comment.