Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the DELETE and UPDATE clause support #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions pandasql/sqldf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,25 @@ def __call__(self, query, env=None):
continue
self.loaded_tables.add(table_name)
write_table(env[table_name], table_name, conn)
result = self.__actually_execute_query(query,conn)
# But if result is None, this might also mean that an UDPATE or DELETE clause has been executed!
if not result:
table_to_query = re.findall(r'(?:FROM|JOIN|UPDATE)\s+(\w+(?:\s*,\s*\w+)*)', query, re.IGNORECASE)[0]
result = self.__actually_execute_query(f"SELECT * FROM {table_to_query}", conn)
return result

try:
result = read_sql(query, conn)
except DatabaseError as ex:
raise PandaSQLException(ex)
except ResourceClosedError:
# query returns nothing
result = None

def __actually_execute_query(self, query, conn):
"""
Actually executes the SQL query
:return the query result
"""
try:
result = read_sql(query, conn)
except DatabaseError as ex:
raise PandaSQLException(ex)
except ResourceClosedError:
# query returns nothing.
result = None
return result

@property
Expand Down Expand Up @@ -110,7 +120,7 @@ def get_outer_frame_variables():
def extract_table_names(query):
""" Extract table names from an SQL query. """
# a good old fashioned regex. turns out this worked better than actually parsing the code
tables_blocks = re.findall(r'(?:FROM|JOIN)\s+(\w+(?:\s*,\s*\w+)*)', query, re.IGNORECASE)
tables_blocks = re.findall(r'(?:FROM|JOIN|UPDATE)\s+(\w+(?:\s*,\s*\w+)*)', query, re.IGNORECASE)
tables = [tbl
for block in tables_blocks
for tbl in re.findall(r'\w+', block)]
Expand Down