diff --git a/src/index.ts b/src/index.ts index 7854bf0..82ec561 100644 --- a/src/index.ts +++ b/src/index.ts @@ -144,10 +144,10 @@ const findNextPlaceholder = (sql: string, start: number): number => { return -1; }; -const findSetKeyword = (sql: string): number => { +const findSetKeyword = (sql: string, startFrom = 0): number => { const length = sql.length; - for (let position = 0; position < length; position++) { + for (let position = startFrom; position < length; position++) { const code = sql.charCodeAt(position); const lower = code | 32; @@ -463,7 +463,7 @@ export const format = ( isRecord(currentValue) ) { escapedValue = objectToValues(currentValue, timezone); - setIndex = -1; + setIndex = findSetKeyword(sql, placeholderEnd); } else escapedValue = escape(currentValue, true, timezone); } else escapedValue = escape(currentValue, stringifyObjects, timezone); diff --git a/test/everyday-queries.test.ts b/test/everyday-queries.test.ts index 761d660..9c03a63 100644 --- a/test/everyday-queries.test.ts +++ b/test/everyday-queries.test.ts @@ -148,6 +148,27 @@ describe('Critical: ON DUPLICATE KEY UPDATE with raw() values', () => { "INSERT INTO t (id, name) VALUES (1, 'test') ON DUPLICATE KEY UPDATE `name` = 'updated', `modified` = CURRENT_TIMESTAMP" ); }); + + test('INSERT ... SET ? ON DUPLICATE KEY UPDATE ? with two objects', () => { + const dataInsert = { + aula: 'Math101', + fecha: '2024-01-15', + texto: 'Lesson content', + tipo: 'lecture', + imagen: 'image.png', + file: 'notes.pdf', + }; + const { aula, fecha, tipo, ...dataUpdate } = dataInsert; + const sql = format( + 'INSERT INTO column SET ? ON DUPLICATE KEY UPDATE ?', + [dataInsert, dataUpdate], + false + ); + assert.equal( + sql, + "INSERT INTO column SET `aula` = 'Math101', `fecha` = '2024-01-15', `texto` = 'Lesson content', `tipo` = 'lecture', `imagen` = 'image.png', `file` = 'notes.pdf' ON DUPLICATE KEY UPDATE `texto` = 'Lesson content', `imagen` = 'image.png', `file` = 'notes.pdf'" + ); + }); }); describe('Critical: stringifyObjects=undefined (mysqljs/mysql legacy mode)', () => {