Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down
21 changes: 21 additions & 0 deletions test/everyday-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)', () => {
Expand Down
Loading