不要怂,就是干,撸起袖子干!

Commit 9d8af4e9 by Sushant Committed by GitHub

fix(postgres): reserved words (#9236)

1 parent a7899041
......@@ -14,6 +14,12 @@
const Utils = require('../../../../utils');
/**
* list of reserved words in PostgreSQL 10
* source: https://www.postgresql.org/docs/10/static/sql-keywords-appendix.html
*/
const postgresReservedWords = 'all,analyse,analyze,and,any,array,as,asc,asymmetric,authorization,binary,both,case,cast,check,collate,collation,column,concurrently,constraint,create,cross,current_catalog,current_date,current_role,current_schema,current_time,current_timestamp,current_user,default,deferrable,desc,distinct,do,else,end,except,false,fetch,for,foreign,freeze,from,full,grant,group,having,ilike,in,initially,inner,intersect,into,is,isnull,join,lateral,leading,left,like,limit,localtime,localtimestamp,natural,not,notnull,null,offset,on,only,or,order,outer,overlaps,placing,primary,references,returning,right,select,session_user,similar,some,symmetric,table,tablesample,then,to,trailing,true,union,unique,user,using,variadic,verbose,when,where,window,with'.split(',');
/**
*
* @param {String} dialect Dialect name
* @param {String} identifier Identifier to quote
......@@ -22,6 +28,7 @@ const Utils = require('../../../../utils');
* @param {Boolean} [options.quoteIdentifiers=true]
*
* @returns {String}
* @private
*/
function quoteIdentifier(dialect, identifier, options) {
if (identifier === '*') return identifier;
......@@ -39,20 +46,23 @@ function quoteIdentifier(dialect, identifier, options) {
return Utils.addTicks(Utils.removeTicks(identifier, '`'), '`');
case 'postgres':
const rawIdentifier = Utils.removeTicks(identifier, '"');
if (
options.force !== true &&
options.quoteIdentifiers === false &&
identifier.indexOf('.') === -1 &&
identifier.indexOf('->') === -1
identifier.indexOf('->') === -1 &&
postgresReservedWords.indexOf(rawIdentifier.toLowerCase()) === -1
) {
// In Postgres, if tables or attributes are created double-quoted,
// they are also case sensitive. If they contain any uppercase
// characters, they must always be double-quoted. This makes it
// impossible to write queries in portable SQL if tables are created in
// this way. Hence, we strip quotes if we don't want case sensitivity.
return Utils.removeTicks(identifier, '"');
return rawIdentifier;
} else {
return Utils.addTicks(Utils.removeTicks(identifier, '"'), '"');
return Utils.addTicks(rawIdentifier, '"');
}
case 'mssql':
......@@ -70,6 +80,7 @@ module.exports.quoteIdentifier = quoteIdentifier;
* @param {String} identifier
*
* @return Boolean
* @private
*/
function isIdentifierQuoted(identifier) {
return /^\s*(?:([`"'])(?:(?!\1).|\1{2})*\1\.?)+\s*$/i.test(identifier);
......
......@@ -569,7 +569,7 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
});
});
suite('queryIdentifiersFalse', () => {
suite('queryIdentifiers: false', () => {
suiteSetup(() => {
sql.options.quoteIdentifiers = false;
});
......@@ -580,7 +580,7 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
test('*', () => {
expectsql(sql.selectQuery('User'), {
default: 'SELECT * FROM [User];',
postgres: 'SELECT * FROM User;'
postgres: 'SELECT * FROM "User";'
});
});
......@@ -589,7 +589,7 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
attributes: ['name', 'age']
}), {
default: 'SELECT [name], [age] FROM [User];',
postgres: 'SELECT name, age FROM User;'
postgres: 'SELECT name, age FROM "User";'
});
});
......@@ -622,7 +622,7 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
model: User
}, User), {
default: 'SELECT [User].[name], [User].[age], [Posts].[id] AS [Posts.id], [Posts].[title] AS [Posts.title] FROM [User] AS [User] LEFT OUTER JOIN [Post] AS [Posts] ON [User].[id] = [Posts].[user_id];',
postgres: 'SELECT User.name, User.age, Posts.id AS "Posts.id", Posts.title AS "Posts.title" FROM User AS User LEFT OUTER JOIN Post AS Posts ON User.id = Posts.user_id;'
postgres: 'SELECT "User".name, "User".age, Posts.id AS "Posts.id", Posts.title AS "Posts.title" FROM "User" AS "User" LEFT OUTER JOIN Post AS Posts ON "User".id = Posts.user_id;'
});
});
......@@ -668,7 +668,7 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
model: User
}, User), {
default: 'SELECT [User].[name], [User].[age], [Posts].[id] AS [Posts.id], [Posts].[title] AS [Posts.title], [Posts->Comments].[id] AS [Posts.Comments.id], [Posts->Comments].[title] AS [Posts.Comments.title], [Posts->Comments].[createdAt] AS [Posts.Comments.createdAt], [Posts->Comments].[updatedAt] AS [Posts.Comments.updatedAt], [Posts->Comments].[post_id] AS [Posts.Comments.post_id] FROM [User] AS [User] LEFT OUTER JOIN [Post] AS [Posts] ON [User].[id] = [Posts].[user_id] LEFT OUTER JOIN [Comment] AS [Posts->Comments] ON [Posts].[id] = [Posts->Comments].[post_id];',
postgres: 'SELECT User.name, User.age, Posts.id AS "Posts.id", Posts.title AS "Posts.title", "Posts->Comments".id AS "Posts.Comments.id", "Posts->Comments".title AS "Posts.Comments.title", "Posts->Comments".createdAt AS "Posts.Comments.createdAt", "Posts->Comments".updatedAt AS "Posts.Comments.updatedAt", "Posts->Comments".post_id AS "Posts.Comments.post_id" FROM User AS User LEFT OUTER JOIN Post AS Posts ON User.id = Posts.user_id LEFT OUTER JOIN Comment AS "Posts->Comments" ON Posts.id = "Posts->Comments".post_id;'
postgres: 'SELECT "User".name, "User".age, Posts.id AS "Posts.id", Posts.title AS "Posts.title", "Posts->Comments".id AS "Posts.Comments.id", "Posts->Comments".title AS "Posts.Comments.title", "Posts->Comments".createdAt AS "Posts.Comments.createdAt", "Posts->Comments".updatedAt AS "Posts.Comments.updatedAt", "Posts->Comments".post_id AS "Posts.Comments.post_id" FROM "User" AS "User" LEFT OUTER JOIN Post AS Posts ON "User".id = Posts.user_id LEFT OUTER JOIN Comment AS "Posts->Comments" ON Posts.id = "Posts->Comments".post_id;'
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!