mirror of
https://github.com/knex/knex.git
synced 2025-12-29 16:08:45 +00:00
Fix comment escaping for MySQL and PostgreSQL (#4084)
This commit is contained in:
parent
4edd56f156
commit
20629af916
@ -4,6 +4,8 @@ const { inherits } = require('util');
|
||||
const ColumnCompiler = require('../../../schema/columncompiler');
|
||||
const { isObject } = require('../../../util/is');
|
||||
|
||||
const commentEscapeRegex = /(?<!\\)'/g;
|
||||
|
||||
function ColumnCompiler_MySQL() {
|
||||
ColumnCompiler.apply(this, arguments);
|
||||
this.modifiers = [
|
||||
@ -150,7 +152,7 @@ Object.assign(ColumnCompiler_MySQL.prototype, {
|
||||
'Your comment is longer than the max comment length for MySQL'
|
||||
);
|
||||
}
|
||||
return comment && `comment '${comment}'`;
|
||||
return comment && `comment '${comment.replace(commentEscapeRegex, "\\'")}'`;
|
||||
},
|
||||
|
||||
first() {
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
const { inherits } = require('util');
|
||||
const ColumnCompiler = require('../../../schema/columncompiler');
|
||||
const { isObject } = require('../../../util/is');
|
||||
const commentEscapeRegex = /(?<!')'(?!')/g;
|
||||
|
||||
function ColumnCompiler_PG() {
|
||||
ColumnCompiler.apply(this, arguments);
|
||||
@ -101,13 +102,15 @@ Object.assign(ColumnCompiler_PG.prototype, {
|
||||
// ------
|
||||
comment(comment) {
|
||||
const columnName = this.args[0] || this.defaults('columnName');
|
||||
const escapedComment = comment
|
||||
? `'${comment.replace(commentEscapeRegex, "''")}'`
|
||||
: 'NULL';
|
||||
|
||||
this.pushAdditional(function () {
|
||||
this.pushQuery(
|
||||
`comment on column ${this.tableCompiler.tableName()}.` +
|
||||
this.formatter.wrap(columnName) +
|
||||
' is ' +
|
||||
(comment ? `'${comment}'` : 'NULL')
|
||||
` is ${escapedComment}`
|
||||
);
|
||||
}, comment);
|
||||
},
|
||||
|
||||
@ -890,6 +890,38 @@ module.exports = function (dialect) {
|
||||
expect(tableSql[0].sql).to.equal("alter table `users` comment = ''");
|
||||
});
|
||||
|
||||
it('test column comment with quotes', function () {
|
||||
tableSql = client
|
||||
.schemaBuilder()
|
||||
.createTable('test', (t) => {
|
||||
t.text('column1').comment(
|
||||
"The table's first column and it's escaped"
|
||||
);
|
||||
})
|
||||
.toSQL();
|
||||
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal(
|
||||
"create table `test` (`column1` text comment 'The table\\'s first column and it\\'s escaped')"
|
||||
);
|
||||
});
|
||||
|
||||
it('test column comment with pre-escaped quotes', function () {
|
||||
tableSql = client
|
||||
.schemaBuilder()
|
||||
.createTable('test', (t) => {
|
||||
t.text('column1').comment(
|
||||
"The table\\'s first column and it\\'s escaped"
|
||||
);
|
||||
})
|
||||
.toSQL();
|
||||
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal(
|
||||
"create table `test` (`column1` text comment 'The table\\'s first column and it\\'s escaped')"
|
||||
);
|
||||
});
|
||||
|
||||
it('set comment to undefined', function () {
|
||||
expect(function () {
|
||||
client
|
||||
|
||||
@ -1190,6 +1190,36 @@ describe('PostgreSQL SchemaBuilder', function () {
|
||||
expect(tableSql[0].sql).to.equal('comment on table "user" is \'\'');
|
||||
});
|
||||
|
||||
it('test column comment with quotes', function () {
|
||||
tableSql = client
|
||||
.schemaBuilder()
|
||||
.createTable('test', (t) => {
|
||||
t.text('column1').comment("The table's first column and it's escaped");
|
||||
})
|
||||
.toSQL();
|
||||
|
||||
equal(tableSql.length, 2);
|
||||
expect(tableSql[1].sql).to.equal(
|
||||
"comment on column \"test\".\"column1\" is 'The table''s first column and it''s escaped'"
|
||||
);
|
||||
});
|
||||
|
||||
it('test column comment with pre-escaped quotes', function () {
|
||||
tableSql = client
|
||||
.schemaBuilder()
|
||||
.createTable('test', (t) => {
|
||||
t.text('column1').comment(
|
||||
"The table''s first column and it''s escaped"
|
||||
);
|
||||
})
|
||||
.toSQL();
|
||||
|
||||
equal(tableSql.length, 2);
|
||||
expect(tableSql[1].sql).to.equal(
|
||||
"comment on column \"test\".\"column1\" is 'The table''s first column and it''s escaped'"
|
||||
);
|
||||
});
|
||||
|
||||
it('set comment to undefined', function () {
|
||||
expect(function () {
|
||||
client
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user