Skip to content

Double single quotes in sql-insert and sql-update values - #1668

Open
jayhemnani9910 wants to merge 1 commit into
dbcli:mainfrom
jayhemnani9910:sql-output-escape-quotes
Open

jayhemnani9910 wants to merge 1 commit into
dbcli:mainfrom
jayhemnani9910:sql-output-escape-quotes

Conversation

@jayhemnani9910

Copy link
Copy Markdown

Description

\T sql-insert and sql-update write text values without escaping ', so the output doesn't run as soon as a value has an apostrophe:

postgres> \T sql-insert
postgres> select id, name from people order by id;
INSERT INTO "people" ("id", "name") VALUES
  ('1', 'O'Brien')
, ('2', 'Ann')
;

psql: ERROR: syntax error at or near "Brien". A value like x'); drop table people; -- would also turn into extra statements when the script is replayed. escape_for_sql_statement now doubles single quotes, giving 'O''Brien'.

Tested: new test_escape_for_sql_statement_str_with_quote fails without the change; pytest tests (without features and the ssh tunnel test, which needs sshtunnel) 2674 passed, 2673 on main. Against PostgreSQL 18, the \T sql-insert output for the table above now loads into a copy of the table with psql and gives back O'Brien.

Schema-qualified queries have a separate problem (INSERT INTO "public.people"), which I left out of this one.

Checklist

  • I searched the existing issues and pull requests on this topic and linked them above (see "Before Opening a Pull Request" in CONTRIBUTING.rst).
  • I've added this contribution to the changelog.rst.
  • I've added my name to the AUTHORS file (or it's already there). It's added in Show arrays in Postgres format with the default data formats #1664.
  • I installed pre-commit hooks (pip install pre-commit && pre-commit install).
  • I verified that my changes work as expected (this may include manually testing them in your local environment, or in other available environments). Cross this out if not relevant (for example, if you're making a documentation change).
  • Please squash merge this pull request (uncheck if you'd like us to merge as multiple commits)

A value with an apostrophe, like O'Brien, was written as 'O'Brien', so the
generated statements did not run.

This branch has not been deployed

No deployments
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.

1 participant