Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions mycli/main_modes/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
prepare_polars_transform,
run_polars_transform,
)
from mycli.packages.execution.background_runner import QueryCancelled, QueryRunner, runner_for
from mycli.packages.execution.background_runner import BackgroundRunner, QueryCancelled, runner_for
from mycli.packages.execution.sql_execute import SQLExecute
from mycli.packages.prompt_toolkit.history import FRECENCY_HISTORY_ENTRIES, FRECENCY_REFRESH_INTERVAL, FileHistoryWithTimestamp
from mycli.packages.prompt_toolkit.key_bindings import mycli_bindings
Expand Down Expand Up @@ -1179,9 +1179,9 @@ def main_repl(mycli: 'MyCli') -> None:

config = mycli.config['main']
show_state_interval = config.as_float('show_query_state_interval')
runner = QueryRunner(show_state_interval)
runner = BackgroundRunner(show_state_interval)
if isinstance(sql_execute, SQLExecute):
sql_execute.set_query_runner(runner)
sql_execute.set_background_runner(runner)
try:
while True:
_one_iteration(mycli, state)
Expand All @@ -1192,5 +1192,5 @@ def main_repl(mycli: 'MyCli') -> None:
mycli.echo('Goodbye!')
finally:
if isinstance(sql_execute, SQLExecute):
sql_execute.set_query_runner(None)
sql_execute.set_background_runner(None)
runner.close()
14 changes: 7 additions & 7 deletions mycli/packages/execution/background_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
logger = logging.getLogger(__name__)


def runner_for(client: Any) -> QueryRunner | None:
runner = getattr(getattr(client, 'sql_execute', None), 'query_runner', None)
return runner if isinstance(runner, QueryRunner) else None
def runner_for(client: Any) -> BackgroundRunner | None:
runner = getattr(getattr(client, 'sql_execute', None), 'background_runner', None)
return runner if isinstance(runner, BackgroundRunner) else None


def rendering_output(method: Callable[..., T]) -> Callable[..., T]:
Expand All @@ -50,7 +50,7 @@ def __init__(self, disconnected: bool) -> None:
def background(method: Callable[..., Any]) -> Callable[..., Any]:
@wraps(method)
def call(cursor: Cursor, *args: Any, **kwargs: Any) -> Any:
runner = getattr(cursor.connection, '_mycli_query_runner', None)
runner = getattr(cursor.connection, '_mycli_background_runner', None)
if runner is None:
return method(cursor, *args, **kwargs)
return runner.call(lambda: method(cursor, *args, **kwargs), new_statement=method.__name__ == 'execute')
Expand All @@ -74,7 +74,7 @@ class BackgroundSSCursor(SSCursor):
__del__ = close


class QueryRunner:
class BackgroundRunner:
def __init__(self, show_state_interval: float = 0.5) -> None:
self.show_state = math.isfinite(show_state_interval) and show_state_interval > 0 and sys.stderr.isatty()
self.interval = show_state_interval if self.show_state else 0.5
Expand Down Expand Up @@ -168,13 +168,13 @@ def attach(self, connection: Connection, connect_control: Callable[[], Connectio
self.connect_control = connect_control
self.cursorclass = connection.cursorclass
connection.cursorclass = BackgroundSSCursor if issubclass(self.cursorclass, SSCursor) else BackgroundCursor
connection._mycli_query_runner = self # type: ignore[attr-defined]
connection._mycli_background_runner = self # type: ignore[attr-defined]

def detach(self) -> None:
self.reset_progress()
if self.connection is not None:
self.connection.cursorclass = self.cursorclass
del self.connection._mycli_query_runner # type: ignore[attr-defined]
del self.connection._mycli_background_runner # type: ignore[attr-defined]
self.connection = None
self.monitor.submit(self._close_control).result()

Expand Down
14 changes: 7 additions & 7 deletions mycli/packages/execution/sql_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pymysql.cursors import Cursor, SSCursor

from mycli.constants import ER_MUST_CHANGE_PASSWORD
from mycli.packages.execution.background_runner import QueryRunner
from mycli.packages.execution.background_runner import BackgroundRunner
from mycli.packages.special_commands import io_commands
from mycli.packages.special_commands.main import CommandNotFound, execute
from mycli.packages.sql_result.sql_result import SQLResult
Expand Down Expand Up @@ -193,7 +193,7 @@ def __init__(
self.init_command = init_command
self.unbuffered = unbuffered
self.conn: Connection | None = None
self.query_runner: QueryRunner | None = None
self.background_runner: BackgroundRunner | None = None
self.connect()

def connect(
Expand Down Expand Up @@ -351,16 +351,16 @@ def connect(
write_timeout=2,
)
self._control_kwargs = control_kwargs
if runner := getattr(self, 'query_runner', None):
if runner := getattr(self, 'background_runner', None):
runner.attach(conn, self.connect_query_monitor)

def connect_query_monitor(self) -> Connection:
return pymysql.connect(**self._control_kwargs) # type: ignore[misc]

def set_query_runner(self, runner: QueryRunner | None) -> None:
if previous := getattr(self, 'query_runner', None):
def set_background_runner(self, runner: BackgroundRunner | None) -> None:
if previous := getattr(self, 'background_runner', None):
previous.detach()
self.query_runner = runner
self.background_runner = runner
if runner is not None and self.conn is not None:
runner.attach(self.conn, self.connect_query_monitor)

Expand Down Expand Up @@ -648,7 +648,7 @@ def reset_connection_id(self) -> None:

def change_db(self, db: str) -> None:
assert isinstance(self.conn, Connection)
if runner := getattr(self, 'query_runner', None):
if runner := getattr(self, 'background_runner', None):
runner.call(lambda: self.conn.select_db(db))
else:
self.conn.select_db(db)
Expand Down
Loading
Loading