Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 16 additions & 7 deletions mycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,10 +816,12 @@ def show_suggestion_tip() -> bool:
# mutating if any one of the component statements is mutating
mutating = False

def output_res(res: Generator[SQLResult], start: float) -> None:
def output_res(results: Generator[SQLResult], start: float) -> None:
nonlocal mutating
result_count = 0
for title, cur, headers, status, command in res:
for result in results:
title, cur, headers, status = result.get_output()
command = result.command
logger.debug("title: %r", title)
logger.debug("headers: %r", headers)
logger.debug("rows: %r", cur)
Expand All @@ -828,8 +830,12 @@ def output_res(res: Generator[SQLResult], start: float) -> None:
# If this is a watch query, offset the start time on the 2nd+ iteration
# to account for the sleep duration
if command is not None and command["name"] == "watch":
watch_seconds = float(command["seconds"])
if result_count > 0:
try:
watch_seconds = float(command["seconds"])
except ValueError as e:
self.echo(f"Invalid watch sleep time provided ({e}).", err=True, fg="red")
sys.exit(1)
start += watch_seconds
if is_select(status) and cur and cur.rowcount > threshold:
self.echo(
Expand Down Expand Up @@ -880,7 +886,8 @@ def output_res(res: Generator[SQLResult], start: float) -> None:
# get and display warnings if enabled
if self.show_warnings and isinstance(cur, Cursor) and cur.warning_count > 0:
warnings = sqlexecute.run("SHOW WARNINGS")
for title, cur, headers, status, _command in warnings:
for warning in warnings:
title, cur, headers, status = warning.get_output()
formatted = self.format_output(
title,
cur,
Expand Down Expand Up @@ -1339,8 +1346,9 @@ def get_prompt(self, string: str) -> str:
def run_query(self, query: str, new_line: bool = True) -> None:
"""Runs *query*."""
assert self.sqlexecute is not None
res = self.sqlexecute.run(query)
for title, cur, headers, _status, _command in res:
results = self.sqlexecute.run(query)
for result in results:
title, cur, headers, _status = result.get_output()
self.main_formatter.query = query
self.redirect_formatter.query = query
output = self.format_output(
Expand All @@ -1357,7 +1365,8 @@ def run_query(self, query: str, new_line: bool = True) -> None:
# get and display warnings if enabled
if self.show_warnings and isinstance(cur, Cursor) and cur.warning_count > 0:
warnings = self.sqlexecute.run("SHOW WARNINGS")
for title, cur, headers, _status, _command in warnings:
for warning in warnings:
title, cur, headers, _status = warning.get_output()
output = self.format_output(
title,
cur,
Expand Down
2 changes: 1 addition & 1 deletion mycli/packages/special/iocommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def watch_query(arg: str, **kwargs) -> Generator[SQLResult, None, None]:
cur.execute(sql)
command = {
"name": "watch",
"seconds": seconds,
"seconds": str(seconds),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why str() here and float() later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the weeds on trying to make mypy happy. I reworked it a bit to be more explicit and found a combo mypy liked, so let me know if that's any better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better!

}
if cur.description:
headers = [x[0] for x in cur.description]
Expand Down
7 changes: 5 additions & 2 deletions mycli/packages/sqlresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ class SQLResult:
results: Cursor | list[tuple] | None = None
headers: list[str] | str | None = None
status: str | None = None
command: dict[str, object] | None = None
command: dict[str, str] | None = None

def get_output(self):
return self.title, self.results, self.headers, self.status

def __iter__(self):
return iter((self.title, self.results, self.headers, self.status, self.command))
return self

def __str__(self):
return f"{self.title}, {self.results}, {self.headers}, {self.status}, {self.command}"
5 changes: 3 additions & 2 deletions mycli/sqlexecute.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,9 @@ def reset_connection_id(self) -> None:
# Remember current connection id
_logger.debug("Get current connection id")
try:
res = self.run("select connection_id()")
for _title, cur, _headers, _status, _command in res:
results = self.run("select connection_id()")
for result in results:
_title, cur, _headers, _status = result.get_output()
self.connection_id = cur.fetchone()[0]
except Exception as e:
# See #1054
Expand Down
16 changes: 11 additions & 5 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ def create_db(dbname):

def run(executor, sql, rows_as_list=True):
"""Return string output for the sql to be run."""
result = []

for title, rows, headers, status, _command in executor.run(sql):
results = []

for result in executor.run(sql):
(
title,
rows,
headers,
status,
) = result.get_output()
rows = list(rows) if (rows_as_list and rows) else rows
result.append({"title": title, "rows": rows, "headers": headers, "status": status})
results.append({"title": title, "rows": rows, "headers": headers, "status": status})

return result
return results


def set_expanded_output(is_expanded):
Expand Down