Skip to content

End a statement at \G or \g in batch input - #2485

Merged
rolandwalker merged 2 commits into
dbcli:mainfrom
jayhemnani9910:batch-end-statement-at-backslash-g
Nov 30, 2026
Merged

rolandwalker merged 2 commits into
dbcli:mainfrom
jayhemnani9910:batch-end-statement-at-backslash-g

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

A \G line in a script isn't a statement end in --batch, stdin or source, so it's sent together with the next statement and the script stops:

$ printf 'select 1\\G\nselect 2;\n' > s.sql
$ mycli t --batch s.sql
(1064, "You have an error in your SQL syntax; ... near '\\G\nselect 2' at line 1")

mysql t < s.sql runs both. statements_from_filehandle only ended a statement at ; (or the DELIMITER). It now also ends one when the last tokens are \G or \g, and keeps the suffix so run() still picks the output mode. A \G inside a string or comment isn't a token there, so it's left alone.

After a \G, later statements in the same script also print vertically, because the expanded setting isn't reset between statements (the todo in sql_execute.py). I left that alone here.

Added a test_batch_utils.py case; it fails on main. pytest mycli_test/pytests has the same 62 failures as main (they need a server) plus the new pass. Against MySQL 8.4, the script above now prints both rows.

A \G line was not a statement end, so it was sent together with the next
statement and the script stopped with error 1064.
Comment thread mycli/packages/utils/batch_utils.py Outdated
statements = ''
line_counter = 0
elif len(tokens) > 1 and tokens[-2].text == '\\' and tokens[-1].text in ('G', 'g'):
# \G and \g end a statement too, as in the mysql client

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We prefer "vendor client" here for consistency.

@rolandwalker

Copy link
Copy Markdown
Contributor

Thanks! We probably do have to address the todo here.

@jayhemnani9910

Copy link
Copy Markdown
Contributor Author

Done in f5832c7:

  • "vendor client" in the comment.
  • run() now resets expanded, forced-horizontal and explorer output before each query, so a \G only applies to its own statement. That's the REPL behavior, which already reset them before each prompt; it now holds in --batch and source too.
  • test_run_falls_back_to_regular_sql_and_handles_output_flags records every setter call, so its expected lists now include the resets. New test_expanded_output_applies_to_one_query fails on main.

One side effect: the SHOW WARNINGS follow-up after a \G query now prints in table form, since it goes through run() as a separate query.

@rolandwalker
rolandwalker merged commit 7491aa6 into dbcli:main Nov 30, 2026
13 checks passed
@rolandwalker

Copy link
Copy Markdown
Contributor

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants