Skip to content
Open
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
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
TBD
==============

Features
--------
* Add `--unbuffered` mode which fetches rows as needed, to save memory.


Bug Fixes
--------
* Fix CamelCase fuzzy matching.
Expand Down
8 changes: 8 additions & 0 deletions mycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ def connect(
ssh_password: str | None = "",
ssh_key_filename: str | None = "",
init_command: str | None = "",
unbuffered: bool | None = None,
password_file: str | None = "",
) -> None:
cnf = {
Expand Down Expand Up @@ -563,6 +564,7 @@ def _connect() -> None:
ssh_password,
ssh_key_filename,
init_command,
unbuffered,
)
except pymysql.OperationalError as e1:
if e1.args[0] == HANDSHAKE_ERROR and ssl is not None and ssl.get("mode", None) == "auto":
Expand All @@ -583,6 +585,7 @@ def _connect() -> None:
ssh_password,
ssh_key_filename,
init_command,
unbuffered,
)
except Exception as e2:
raise e2
Expand Down Expand Up @@ -1521,6 +1524,9 @@ def get_last_query(self) -> str | None:
@click.option("-g", "--login-path", type=str, help="Read this path from the login file.")
@click.option("-e", "--execute", type=str, help="Execute command and quit.")
@click.option("--init-command", type=str, help="SQL statement to execute after connecting.")
@click.option(
"--unbuffered", is_flag=True, help="Instead of copying every row of data into a buffer, fetch rows as needed, to save memory."
)
@click.option("--charset", type=str, help="Character set for MySQL session.")
@click.option(
"--password-file", type=click.Path(), help="File or FIFO path containing the password to connect to the db if not specified otherwise."
Expand Down Expand Up @@ -1570,6 +1576,7 @@ def cli(
ssh_config_path: str,
ssh_config_host: str | None,
init_command: str | None,
unbuffered: bool | None,
charset: str | None,
password_file: str | None,
) -> None:
Expand Down Expand Up @@ -1807,6 +1814,7 @@ def cli(
ssh_password=ssh_password,
ssh_key_filename=ssh_key_filename,
init_command=combined_init_cmd,
unbuffered=unbuffered,
charset=charset,
password_file=password_file,
)
Expand Down
10 changes: 9 additions & 1 deletion mycli/sqlexecute.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def __init__(
ssh_password: str | None,
ssh_key_filename: str | None,
init_command: str | None = None,
unbuffered: bool | None = None,
) -> None:
self.dbname = database
self.user = user
Expand All @@ -180,6 +181,7 @@ def __init__(
self.ssh_password = ssh_password
self.ssh_key_filename = ssh_key_filename
self.init_command = init_command
self.unbuffered = unbuffered
self.conn: Connection | None = None
self.connect()

Expand All @@ -200,6 +202,7 @@ def connect(
ssh_password: str | None = None,
ssh_key_filename: str | None = None,
init_command: str | None = None,
unbuffered: bool | None = None,
):
db = database if database is not None else self.dbname
user = user if user is not None else self.user
Expand All @@ -216,6 +219,7 @@ def connect(
ssh_password = ssh_password if ssh_password is not None else self.ssh_password
ssh_key_filename = ssh_key_filename if ssh_key_filename is not None else self.ssh_key_filename
init_command = init_command if init_command is not None else self.init_command
unbuffered = unbuffered if unbuffered is not None else self.unbuffered
_logger.debug(
"Connection DB Params: \n"
"\tdatabase: %r"
Expand All @@ -231,7 +235,8 @@ def connect(
"\tssh_port: %r"
"\tssh_password: %r"
"\tssh_key_filename: %r"
"\tinit_command: %r",
"\tinit_command: %r"
"\tunbuffered: %r",
db,
user,
host,
Expand All @@ -246,6 +251,7 @@ def connect(
ssh_password,
ssh_key_filename,
init_command,
unbuffered,
)
conv = conversions.copy()
conv.update({
Expand Down Expand Up @@ -285,6 +291,7 @@ def connect(
program_name="mycli",
defer_connect=defer_connect,
init_command=init_command or None,
cursorclass=pymysql.cursors.SSCursor if unbuffered else pymysql.cursors.Cursor,
) # type: ignore[misc]

if ssh_host:
Expand Down Expand Up @@ -324,6 +331,7 @@ def connect(
self.charset = charset
self.ssl = ssl
self.init_command = init_command
self.unbuffered = unbuffered
# retrieve connection id
self.reset_connection_id()
self.server_info = ServerInfo.from_version_string(conn.server_version) # type: ignore[attr-defined]
Expand Down