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

Commit 1b5385ac by Sascha Depold

Merge branch 'postgres_quotes' of git://github.com/joshm/sequelize into joshm-postgres_quotes

2 parents 0774f45a acf501d4
...@@ -10,7 +10,7 @@ function removeQuotes(s, quoteChar) { ...@@ -10,7 +10,7 @@ function removeQuotes(s, quoteChar) {
function addQuotes(s, quoteChar) { function addQuotes(s, quoteChar) {
quoteChar = quoteChar || '"' quoteChar = quoteChar || '"'
return quoteChar + removeQuotes(s) + quoteChar return s.split('.').map(function(e) { return quoteChar + String(e) + quoteChar }).join('.')
} }
function pgEscape(s) { function pgEscape(s) {
...@@ -343,9 +343,10 @@ module.exports = (function() { ...@@ -343,9 +343,10 @@ module.exports = (function() {
return (typeof attribute == 'string') ? attribute : attribute.attribute return (typeof attribute == 'string') ? attribute : attribute.attribute
}) })
var indexTable = tableName.split('.')
options = Utils._.extend({ options = Utils._.extend({
indicesType: null, indicesType: null,
indexName: Utils._.underscored(tableName + '_' + onlyAttributeNames.join('_')), indexName: Utils._.underscored(indexTable[indexTable.length-1] + '_' + onlyAttributeNames.join('_')),
parser: null parser: null
}, options || {}) }, options || {})
......
...@@ -19,12 +19,21 @@ describe('QueryGenerator', function() { ...@@ -19,12 +19,21 @@ describe('QueryGenerator', function() {
arguments: ['myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}], arguments: ['myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}],
expectation: "CREATE TABLE IF NOT EXISTS \"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));" expectation: "CREATE TABLE IF NOT EXISTS \"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));"
}, },
{
arguments: ['mySchema.myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}],
expectation: "CREATE TABLE IF NOT EXISTS \"mySchema\".\"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));"
},
], ],
dropTableQuery: [ dropTableQuery: [
{ {
arguments: ['myTable'], arguments: ['myTable'],
expectation: "DROP TABLE IF EXISTS \"myTable\";" expectation: "DROP TABLE IF EXISTS \"myTable\";"
},
{
arguments: ['mySchema.myTable'],
expectation: "DROP TABLE IF EXISTS \"mySchema\".\"myTable\";"
} }
], ],
...@@ -69,6 +78,12 @@ describe('QueryGenerator', function() { ...@@ -69,6 +78,12 @@ describe('QueryGenerator', function() {
title: 'uses offset even if no limit was passed', title: 'uses offset even if no limit was passed',
arguments: ['myTable', {offset: 2}], arguments: ['myTable', {offset: 2}],
expectation: "SELECT * FROM \"myTable\" OFFSET 2;" expectation: "SELECT * FROM \"myTable\" OFFSET 2;"
}, {
arguments: ['mySchema.myTable'],
expectation: "SELECT * FROM \"mySchema\".\"myTable\";"
}, {
arguments: ['mySchema.myTable', {where: {name: "foo';DROP TABLE mySchema.myTable;"}}],
expectation: "SELECT * FROM \"mySchema\".\"myTable\" WHERE \"name\"='foo'';DROP TABLE mySchema.myTable;';"
} }
], ],
...@@ -100,6 +115,12 @@ describe('QueryGenerator', function() { ...@@ -100,6 +115,12 @@ describe('QueryGenerator', function() {
arguments: ['myTable', {name: 'foo', nullValue: undefined}], arguments: ['myTable', {name: 'foo', nullValue: undefined}],
expectation: "INSERT INTO \"myTable\" (\"name\") VALUES ('foo') RETURNING *;", expectation: "INSERT INTO \"myTable\" (\"name\") VALUES ('foo') RETURNING *;",
context: {options: {omitNull: true}} context: {options: {omitNull: true}}
}, {
arguments: ['mySchema.myTable', {name: 'foo'}],
expectation: "INSERT INTO \"mySchema\".\"myTable\" (\"name\") VALUES ('foo') RETURNING *;"
}, {
arguments: ['mySchema.myTable', {name: "foo';DROP TABLE mySchema.myTable;"}],
expectation: "INSERT INTO \"mySchema\".\"myTable\" (\"name\") VALUES ('foo'';DROP TABLE mySchema.myTable;') RETURNING *;"
} }
], ],
...@@ -131,7 +152,13 @@ describe('QueryGenerator', function() { ...@@ -131,7 +152,13 @@ describe('QueryGenerator', function() {
arguments: ['myTable', {bar: 2, nullValue: undefined}, {name: 'foo'}], arguments: ['myTable', {bar: 2, nullValue: undefined}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"bar\"=2 WHERE \"name\"='foo'", expectation: "UPDATE \"myTable\" SET \"bar\"=2 WHERE \"name\"='foo'",
context: {options: {omitNull: true}} context: {options: {omitNull: true}}
}, }, {
arguments: ['mySchema.myTable', {name: 'foo', birthday: new Date(Date.UTC(2011, 2, 27, 10, 1, 55))}, {id: 2}],
expectation: "UPDATE \"mySchema\".\"myTable\" SET \"name\"='foo',\"birthday\"='2011-03-27 10:01:55.0' WHERE \"id\"=2"
}, {
arguments: ['mySchema.myTable', {name: "foo';DROP TABLE mySchema.myTable;"}, {name: 'foo'}],
expectation: "UPDATE \"mySchema\".\"myTable\" SET \"name\"='foo'';DROP TABLE mySchema.myTable;' WHERE \"name\"='foo'"
}
], ],
deleteQuery: [ deleteQuery: [
...@@ -147,6 +174,12 @@ describe('QueryGenerator', function() { ...@@ -147,6 +174,12 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', {name: "foo';DROP TABLE myTable;"}, {limit: 10}], arguments: ['myTable', {name: "foo';DROP TABLE myTable;"}, {limit: 10}],
expectation: "DELETE FROM \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"name\"='foo'';DROP TABLE myTable;' LIMIT 10)" expectation: "DELETE FROM \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"name\"='foo'';DROP TABLE myTable;' LIMIT 10)"
}, {
arguments: ['mySchema.myTable', {name: 'foo'}],
expectation: "DELETE FROM \"mySchema\".\"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"mySchema\".\"myTable\" WHERE \"name\"='foo' LIMIT 1)"
}, {
arguments: ['mySchema.myTable', {name: "foo';DROP TABLE mySchema.myTable;"}, {limit: 10}],
expectation: "DELETE FROM \"mySchema\".\"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"mySchema\".\"myTable\" WHERE \"name\"='foo'';DROP TABLE mySchema.myTable;' LIMIT 10)"
} }
], ],
...@@ -167,6 +200,9 @@ describe('QueryGenerator', function() { ...@@ -167,6 +200,9 @@ describe('QueryGenerator', function() {
'User', ['username', 'isAdmin'], { indicesType: 'FULLTEXT', indexName: 'bar'} 'User', ['username', 'isAdmin'], { indicesType: 'FULLTEXT', indexName: 'bar'}
], ],
expectation: "CREATE FULLTEXT INDEX \"bar\" ON \"User\" (\"username\", \"isAdmin\")" expectation: "CREATE FULLTEXT INDEX \"bar\" ON \"User\" (\"username\", \"isAdmin\")"
}, {
arguments: ['mySchema.User', ['username', 'isAdmin']],
expectation: 'CREATE INDEX \"user_username_is_admin\" ON \"mySchema\".\"User\" (\"username\", \"isAdmin\")'
} }
], ],
...@@ -188,6 +224,9 @@ describe('QueryGenerator', function() { ...@@ -188,6 +224,9 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['User', ['foo', 'bar']], arguments: ['User', ['foo', 'bar']],
expectation: "DROP INDEX IF EXISTS \"user_foo_bar\"" expectation: "DROP INDEX IF EXISTS \"user_foo_bar\""
}, {
arguments: ['User', 'mySchema.user_foo_bar'],
expectation: "DROP INDEX IF EXISTS \"mySchema\".\"user_foo_bar\""
} }
], ],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!