Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stringify json value in update #5063

Merged
merged 1 commit into from Mar 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/query/querybuilder.js
Expand Up @@ -1236,7 +1236,11 @@ class Builder extends EventEmitter {
const obj = this._single.update || {};
this._method = 'update';
if (isString(values)) {
obj[values] = returning;
if (isPlainObject(returning)) {
obj[values] = JSON.stringify(returning);
} else {
obj[values] = returning;
}
if (arguments.length > 2) {
ret = arguments[2];
}
Expand Down
41 changes: 41 additions & 0 deletions test/integration2/query/update/updates.spec.js
Expand Up @@ -423,6 +423,47 @@ describe('Updates', function () {
.where('email', '=', 'test1@example.com');
expect(results[0].last_name).to.equal('olivier');
});

it('should escaped json objects when update value #5059', async function () {
await knex.schema.dropTableIfExists('testing');
await knex.schema.createTable('testing', (t) => {
t.increments('id');
t.string('one');
t.integer('two');
});
await knex('testing')
.update('one', { one: 123, two: 456 })
.where('id', 1)
.testSql(function (tester) {
tester('mysql', 'update `testing` set `one` = ? where `id` = ?', [
'{"one":123,"two":456}',
1,
]);
tester('pg', 'update "testing" set "one" = ? where "id" = ?', [
'{"one":123,"two":456}',
1,
]);
tester(
'pg-redshift',
'update "testing" set "one" = ? where "id" = ?',
['{"one":123,"two":456}', 1]
);
tester('sqlite3', 'update `testing` set `one` = ? where `id` = ?', [
'{"one":123,"two":456}',
1,
]);
tester('mysql', 'update `testing` set `one` = ? where `id` = ?', [
'{"one":123,"two":456}',
1,
]);
tester(
'mssql',
'update [testing] set [one] = ? where [id] = ?;select @@rowcount',
['{"one":123,"two":456}', 1]
);
});
await knex.schema.dropTable('testing');
});
});
});
});