escape single quote to double quotes after formatting sql#74
escape single quote to double quotes after formatting sql#74kbarbounakis wants to merge 1 commit intothemost-framework:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #73 where creating MS SQL Server views with queries containing single quotes in string literals would fail with a syntax error. The fix applies proper escaping for single quotes when constructing dynamic SQL within an EXECUTE statement.
Changes:
- Added
.replace(/'/g, "''")to escape single quotes in formatted query strings before embedding them in EXECUTE() statements
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| try { | ||
| const formatter = new MSSqlFormatter(); | ||
| const sql = 'EXECUTE(\'' + sprintf('CREATE VIEW %s.%s AS ', formatter.escapeName(owner), formatter.escapeName(view)) + formatter.format(q) + '\')'; | ||
| const sql = 'EXECUTE(\'' + sprintf('CREATE VIEW %s.%s AS ', formatter.escapeName(owner), formatter.escapeName(view)) + formatter.format(q).replace(/'/g, '\'\'') + '\')'; |
There was a problem hiding this comment.
Consider adding a test case to verify that view creation works correctly with queries containing string literals with single quotes. For example, a query with a WHERE clause like WHERE name = 'O''Brien' or similar quoted strings to ensure the escaping logic works as expected.
| const sql = 'EXECUTE(\'' + sprintf('CREATE VIEW %s.%s AS ', formatter.escapeName(owner), formatter.escapeName(view)) + formatter.format(q).replace(/'/g, '\'\'') + '\')'; | |
| const createViewSql = sprintf('CREATE VIEW %s.%s AS ', formatter.escapeName(owner), formatter.escapeName(view)) + formatter.format(q); | |
| const escapedCreateViewSql = createViewSql.replace(/'/g, '\'\''); | |
| const sql = 'EXECUTE(\'' + escapedCreateViewSql + '\')'; |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This PR closes #73 and escapes single quotes with double quotes after formatting a query expression.