Write proper SQL literals when copying or dragging a table from DB Schema - #4199
Open
anandghegde wants to merge 1 commit into
Open
anandghegde wants to merge 1 commit into
anandghegde wants to merge 1 commit into
Conversation
…hema Copying a table from the DB Schema pane, or dragging it onto another database, builds INSERT statements by wrapping every value in single quotes without escaping it. As a result: - a value containing a quote, e.g. O'Brien, produces invalid SQL and the copy or drop fails - NULL values become empty strings - BLOB values are lost (written as '') - numbers are written as strings Write each value the way "Copy as SQL" in the table grid already does: NULL as NULL, BLOBs as X'..' literals, numbers unquoted and text escaped.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Copying a table from the DB Schema pane (right-click → Copy, or ⌘C/Ctrl+C), or dragging it onto another database, generates a
CREATE TABLEstatement plus oneINSERTper row.DbStructureModel::mimeData()builds those INSERTs by wrapping every value in single quotes without escaping it:insertStatement += QString("'%1',").arg(tableModel.data(...).toString());This PR writes each value as a proper SQL literal, the same way "Copy as SQL" in the table grid (
ExtendedTableWidget::copyMimeData) already does:NULLX'…'sqlb::escapeStringReproduction
DB Schema → right-click
people→ Copy:Before
near "Brien"), so the pasted SQL fails, and so does dropping the table onto another database, which runs the same SQL.''.After
Test plan
-DENABLE_TESTING=ON;ctestpasses (4/4).masterand in a build with this change, then ran each clipboard result withsqlite3 -bailagainst a new database:id, name, quote(nickname), quote(score), quote(avatar), typeof(id), typeof(score)is identical to the source table.dropMimeData(), so it gets the same fix.Separately: like the SQL export in #4198, these INSERTs also include generated columns, so a table that has them still can't be copied this way. I've left that out of this PR to keep it to the value literals.