Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 64fb3354
authored
Mar 08, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
better schema quoting support
1 parent
9003aff1
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
79 additions
and
98 deletions
lib/dao-factory-manager.js
lib/dialects/abstract/index.js
lib/dialects/abstract/query-generator.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/index.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
test/associations/belongs-to.test.js
lib/dao-factory-manager.js
View file @
64fb335
...
...
@@ -44,6 +44,7 @@ module.exports = (function() {
var
daos
=
{}
,
sorter
=
new
Toposort
()
,
sorted
,
dep
options
=
_
.
defaults
(
options
||
{},
{
reverse
:
true
...
...
@@ -57,7 +58,9 @@ module.exports = (function() {
for
(
var
attrName
in
dao
.
rawAttributes
)
{
if
(
dao
.
rawAttributes
.
hasOwnProperty
(
attrName
))
{
if
(
dao
.
rawAttributes
[
attrName
].
references
)
{
deps
.
push
(
dao
.
rawAttributes
[
attrName
].
references
)
dep
=
dao
.
rawAttributes
[
attrName
].
references
dep
=
dep
.
tableName
||
dep
deps
.
push
(
dep
)
}
}
}
...
...
@@ -65,6 +68,7 @@ module.exports = (function() {
deps
=
deps
.
filter
(
function
(
dep
)
{
return
dao
.
tableName
!==
dep
})
sorter
.
add
(
dao
.
tableName
,
deps
)
})
...
...
lib/dialects/abstract/index.js
View file @
64fb335
...
...
@@ -6,7 +6,8 @@ AbstractDialect.prototype.supports = {
'RETURNING'
:
false
,
'DEFAULT'
:
true
,
'DEFAULT VALUES'
:
false
,
'VALUES ()'
:
false
'VALUES ()'
:
false
,
schemas
:
false
}
module
.
exports
=
AbstractDialect
\ No newline at end of file
lib/dialects/abstract/query-generator.js
View file @
64fb335
var
Utils
=
require
(
"../../utils"
)
,
SqlString
=
require
(
"../../sql-string"
)
,
daoFactory
=
require
(
"../../dao-factory"
)
,
_
=
require
(
'lodash'
)
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
addSchema
:
function
(
opts
)
{
throwMethodUndefined
(
'addSchema'
)
addSchema
:
function
(
param
)
{
var
schema
=
(
param
.
options
&&
param
.
options
.
schema
?
param
.
options
.
schema
:
undefined
)
,
schemaDelimiter
=
(
param
.
options
&&
param
.
options
.
schemaDelimiter
?
param
.
options
.
schemaDelimiter
:
undefined
)
return
{
tableName
:
param
.
tableName
||
param
,
schema
:
schema
,
delimiter
:
schemaDelimiter
||
'.'
}
},
/*
...
...
@@ -27,7 +35,13 @@ module.exports = (function() {
Returns a query for dropping a table.
*/
dropTableQuery
:
function
(
tableName
,
options
)
{
throwMethodUndefined
(
'dropTableQuery'
)
options
=
options
||
{}
var
query
=
"DROP TABLE IF EXISTS <%= table %>;"
return
Utils
.
_
.
template
(
query
)({
table
:
this
.
quoteTable
(
tableName
)
})
},
/*
...
...
@@ -329,6 +343,31 @@ module.exports = (function() {
throwMethodUndefined
(
'findAutoIncrementField'
)
},
quoteTable
:
function
(
param
)
{
if
(
_
.
isObject
(
param
))
{
var
table
=
''
;
if
(
this
.
_dialect
.
supports
.
schemas
)
{
if
(
param
.
schema
)
{
table
+=
this
.
quoteIdentifier
(
param
.
schema
)
+
param
.
delimiter
}
table
+=
this
.
quoteIdentifier
(
param
.
tableName
)
}
else
{
if
(
param
.
schema
)
{
table
+=
param
.
schema
+
param
.
delimiter
}
table
+=
param
.
tableName
table
=
this
.
quoteIdentifier
(
table
)
}
return
table
}
return
this
.
quoteIdentifier
(
param
)
},
/*
Quote an object based on its type. This is a more general version of quoteIdentifiers
Strings: should proxy to quoteIdentifiers
...
...
@@ -531,8 +570,8 @@ module.exports = (function() {
,
subJoinQueries
=
[]
// Escape table
options
.
table
=
table
=
!
Array
.
isArray
(
tableName
)
?
this
.
quote
Identifiers
(
tableName
)
:
tableName
.
map
(
function
(
t
)
{
return
this
.
quote
Identifiers
(
t
)
options
.
table
=
table
=
!
Array
.
isArray
(
tableName
)
?
this
.
quote
Table
(
tableName
)
:
tableName
.
map
(
function
(
t
)
{
return
this
.
quote
Table
(
t
)
}.
bind
(
this
)).
join
(
", "
)
if
(
subQuery
&&
mainAttributes
)
{
...
...
@@ -570,7 +609,7 @@ module.exports = (function() {
}
if
(
options
.
include
&&
attr
.
indexOf
(
'.'
)
===
-
1
&&
addTable
)
{
attr
=
this
.
quote
Identifier
(
options
.
table
)
+
'.'
+
attr
attr
=
this
.
quote
Table
(
options
.
table
)
+
'.'
+
attr
}
return
attr
...
...
lib/dialects/mysql/query-generator.js
View file @
64fb335
...
...
@@ -5,24 +5,6 @@ var Utils = require("../../utils")
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
dialect
:
'mysql'
,
addSchema
:
function
(
opts
)
{
var
tableName
var
schema
=
(
!!
opts
&&
!!
opts
.
options
&&
!!
opts
.
options
.
schema
?
opts
.
options
.
schema
:
undefined
)
var
schemaDelimiter
=
(
!!
opts
&&
!!
opts
.
options
&&
!!
opts
.
options
.
schemaDelimiter
?
opts
.
options
.
schemaDelimiter
:
undefined
)
if
(
!!
opts
&&
!!
opts
.
tableName
)
{
tableName
=
opts
.
tableName
}
else
if
(
typeof
opts
===
"string"
)
{
tableName
=
opts
}
if
(
!
schema
||
schema
.
toString
().
trim
()
===
""
)
{
return
tableName
}
return
this
.
quoteIdentifier
(
schema
+
(
!
schemaDelimiter
?
'.'
:
schemaDelimiter
)
+
tableName
,
false
)
},
createSchema
:
function
()
{
var
query
=
"SHOW TABLES"
...
...
@@ -103,16 +85,6 @@ module.exports = (function() {
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
},
dropTableQuery
:
function
(
tableName
,
options
)
{
options
=
options
||
{}
var
query
=
"DROP TABLE IF EXISTS <%= table %>;"
return
Utils
.
_
.
template
(
query
)({
table
:
this
.
quoteIdentifier
(
tableName
)
})
},
showTablesQuery
:
function
()
{
return
'SHOW TABLES;'
},
...
...
@@ -424,10 +396,6 @@ module.exports = (function() {
return
identifiers
.
split
(
'.'
).
map
(
function
(
v
)
{
return
this
.
quoteIdentifier
(
v
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
},
quoteTable
:
function
(
table
)
{
return
this
.
quoteIdentifier
(
table
)
},
/**
* Generates an SQL query that returns all foreign keys of a table.
*
...
...
lib/dialects/postgres/index.js
View file @
64fb335
...
...
@@ -7,7 +7,8 @@ var PostgresDialect = function(sequelize) {
PostgresDialect
.
prototype
.
supports
=
_
.
defaults
({
'RETURNING'
:
true
,
'DEFAULT VALUES'
:
true
'DEFAULT VALUES'
:
true
,
schemas
:
true
},
Abstract
.
prototype
.
supports
)
module
.
exports
=
PostgresDialect
lib/dialects/postgres/query-generator.js
View file @
64fb335
...
...
@@ -10,23 +10,15 @@ module.exports = (function() {
options
:
{},
dialect
:
'postgres'
,
addSchema
:
function
(
opts
)
{
var
tableName
=
undefined
var
schema
=
(
!!
opts
.
options
&&
!!
opts
.
options
.
schema
?
opts
.
options
.
schema
:
undefined
)
var
schemaDelimiter
=
(
!!
opts
.
options
&&
!!
opts
.
options
.
schemaDelimiter
?
opts
.
options
.
schemaDelimiter
:
undefined
)
addSchema
:
function
(
param
)
{
var
schema
=
(
param
.
options
&&
param
.
options
.
schema
?
param
.
options
.
schema
:
undefined
)
,
schemaDelimiter
=
(
param
.
options
&&
param
.
options
.
schemaDelimiter
?
param
.
options
.
schemaDelimiter
:
undefined
)
if
(
!!
opts
&&
!!
opts
.
tableName
)
{
tableName
=
opts
.
tableName
return
{
tableName
:
param
.
tableName
||
param
,
schema
:
schema
,
delimiter
:
schemaDelimiter
||
'.'
}
else
if
(
typeof
opts
===
"string"
)
{
tableName
=
opts
}
if
(
!
schema
||
schema
.
toString
().
trim
()
===
""
)
{
return
tableName
}
return
this
.
quoteIdentifiers
((
!!
schema
?
(
schema
+
'.'
+
tableName
)
:
tableName
));
},
createSchema
:
function
(
schema
)
{
...
...
@@ -97,10 +89,9 @@ module.exports = (function() {
dropTableQuery
:
function
(
tableName
,
options
)
{
options
=
options
||
{}
var
query
=
"DROP TABLE IF EXISTS <%=
schema %><%=
table %><%= cascade %>;"
var
query
=
"DROP TABLE IF EXISTS <%= table %><%= cascade %>;"
return
Utils
.
_
.
template
(
query
)({
schema
:
options
.
schema
?
this
.
quoteIdentifiers
(
options
.
schema
)
+
'.'
:
''
,
table
:
this
.
quoteIdentifiers
(
tableName
),
table
:
this
.
quoteTable
(
tableName
),
cascade
:
options
.
cascade
?
" CASCADE"
:
""
})
},
...
...
@@ -818,10 +809,6 @@ module.exports = (function() {
return
identifiers
.
split
(
'.'
).
map
(
function
(
t
)
{
return
this
.
quoteIdentifier
(
t
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
},
quoteTable
:
function
(
table
)
{
return
this
.
quoteIdentifiers
(
table
)
},
/**
* Generates an SQL query that returns all foreign keys of a table.
*
...
...
lib/dialects/sqlite/query-generator.js
View file @
64fb335
...
...
@@ -15,23 +15,15 @@ module.exports = (function() {
options
:
{},
dialect
:
'sqlite'
,
addSchema
:
function
(
opts
)
{
var
tableName
=
undefined
var
schema
=
(
!!
opts
&&
!!
opts
.
options
&&
!!
opts
.
options
.
schema
?
opts
.
options
.
schema
:
undefined
)
var
schemaDelimiter
=
(
!!
opts
&&
!!
opts
.
options
&&
!!
opts
.
options
.
schemaDelimiter
?
opts
.
options
.
schemaDelimiter
:
undefined
)
addSchema
:
function
(
param
)
{
var
schema
=
(
param
.
options
&&
param
.
options
.
schema
?
param
.
options
.
schema
:
undefined
)
,
schemaDelimiter
=
(
param
.
options
&&
param
.
options
.
schemaDelimiter
?
param
.
options
.
schemaDelimiter
:
undefined
)
if
(
!!
opts
&&
!!
opts
.
tableName
)
{
tableName
=
opts
.
tableName
return
{
tableName
:
param
.
tableName
||
param
,
schema
:
schema
,
delimiter
:
schemaDelimiter
||
'.'
}
else
if
(
typeof
opts
===
"string"
)
{
tableName
=
opts
}
if
(
!
schema
||
schema
.
toString
().
trim
()
===
""
)
{
return
tableName
}
return
this
.
quoteIdentifier
(
schema
)
+
(
!
schemaDelimiter
?
'.'
:
schemaDelimiter
)
+
this
.
quoteIdentifier
(
tableName
)
},
createSchema
:
function
()
{
...
...
@@ -109,7 +101,7 @@ module.exports = (function() {
}
var
values
=
{
table
:
this
.
quote
Identifier
(
tableName
),
table
:
this
.
quote
Table
(
tableName
),
attributes
:
attrStr
.
join
(
", "
),
charset
:
(
options
.
charset
?
"DEFAULT CHARSET="
+
options
.
charset
:
""
)
}
...
...
@@ -133,16 +125,6 @@ module.exports = (function() {
return
!!
value
?
1
:
0
;
},
dropTableQuery
:
function
(
tableName
,
options
)
{
options
=
options
||
{}
var
query
=
"DROP TABLE IF EXISTS <%= table %>;"
return
Utils
.
_
.
template
(
query
)({
table
:
this
.
quoteIdentifier
(
tableName
)
})
},
uniqueConstraintMapping
:
{
code
:
'SQLITE_CONSTRAINT'
,
map
:
function
(
str
)
{
...
...
@@ -199,7 +181,7 @@ module.exports = (function() {
var
replacements
=
{
ignoreDuplicates
:
options
&&
options
.
ignoreDuplicates
?
' OR IGNORE'
:
''
,
table
:
this
.
quote
Identifier
(
tableName
),
table
:
this
.
quote
Table
(
tableName
),
attributes
:
allAttributes
.
map
(
function
(
attr
){
return
this
.
quoteIdentifier
(
attr
)
}.
bind
(
this
)).
join
(
","
),
...
...
@@ -221,7 +203,7 @@ module.exports = (function() {
}
var
replacements
=
{
table
:
this
.
quote
Identifier
(
tableName
),
table
:
this
.
quote
Table
(
tableName
),
values
:
values
.
join
(
","
),
where
:
this
.
getWhereConditions
(
where
)
}
...
...
@@ -234,7 +216,7 @@ module.exports = (function() {
var
query
=
"DELETE FROM <%= table %> WHERE <%= where %>"
var
replacements
=
{
table
:
this
.
quote
Identifier
(
tableName
),
table
:
this
.
quote
Table
(
tableName
),
where
:
this
.
getWhereConditions
(
where
)
}
...
...
@@ -285,7 +267,7 @@ module.exports = (function() {
if
(
dataType
.
references
)
{
template
+=
" REFERENCES <%= referencesTable %> (<%= referencesKey %>)"
replacements
.
referencesTable
=
this
.
quote
Identifier
(
dataType
.
references
)
replacements
.
referencesTable
=
this
.
quote
Table
(
dataType
.
references
)
if
(
dataType
.
referencesKey
)
{
replacements
.
referencesKey
=
this
.
quoteIdentifier
(
dataType
.
referencesKey
)
...
...
@@ -434,10 +416,6 @@ module.exports = (function() {
return
identifiers
.
split
(
'.'
).
map
(
function
(
v
)
{
return
this
.
quoteIdentifier
(
v
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
},
quoteTable
:
function
(
table
)
{
return
this
.
quoteIdentifier
(
table
)
},
/**
* Generates an SQL query that returns all foreign keys of a table.
*
...
...
test/associations/belongs-to.test.js
View file @
64fb335
...
...
@@ -76,7 +76,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
})
})
it
(
'supports schemas'
,
function
(
done
)
{
it
.
only
(
'supports schemas'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
username
:
Sequelize
.
STRING
,
gender
:
Sequelize
.
STRING
}).
schema
(
'archive'
)
,
Task
=
this
.
sequelize
.
define
(
'TaskXYZ'
,
{
title
:
Sequelize
.
STRING
,
status
:
Sequelize
.
STRING
}).
schema
(
'archive'
)
,
self
=
this
...
...
@@ -92,6 +92,8 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
task
.
getUserXYZ
().
success
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
ok
done
()
}).
on
(
'sql'
,
function
(
sql
)
{
console
.
log
(
sql
)
})
})
})
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment