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
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
80 additions
and
99 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() {
...
@@ -44,6 +44,7 @@ module.exports = (function() {
var
daos
=
{}
var
daos
=
{}
,
sorter
=
new
Toposort
()
,
sorter
=
new
Toposort
()
,
sorted
,
sorted
,
dep
options
=
_
.
defaults
(
options
||
{},
{
options
=
_
.
defaults
(
options
||
{},
{
reverse
:
true
reverse
:
true
...
@@ -57,7 +58,9 @@ module.exports = (function() {
...
@@ -57,7 +58,9 @@ module.exports = (function() {
for
(
var
attrName
in
dao
.
rawAttributes
)
{
for
(
var
attrName
in
dao
.
rawAttributes
)
{
if
(
dao
.
rawAttributes
.
hasOwnProperty
(
attrName
))
{
if
(
dao
.
rawAttributes
.
hasOwnProperty
(
attrName
))
{
if
(
dao
.
rawAttributes
[
attrName
].
references
)
{
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() {
...
@@ -65,6 +68,7 @@ module.exports = (function() {
deps
=
deps
.
filter
(
function
(
dep
)
{
deps
=
deps
.
filter
(
function
(
dep
)
{
return
dao
.
tableName
!==
dep
return
dao
.
tableName
!==
dep
})
})
sorter
.
add
(
dao
.
tableName
,
deps
)
sorter
.
add
(
dao
.
tableName
,
deps
)
})
})
...
...
lib/dialects/abstract/index.js
View file @
64fb335
...
@@ -6,7 +6,8 @@ AbstractDialect.prototype.supports = {
...
@@ -6,7 +6,8 @@ AbstractDialect.prototype.supports = {
'RETURNING'
:
false
,
'RETURNING'
:
false
,
'DEFAULT'
:
true
,
'DEFAULT'
:
true
,
'DEFAULT VALUES'
:
false
,
'DEFAULT VALUES'
:
false
,
'VALUES ()'
:
false
'VALUES ()'
:
false
,
schemas
:
false
}
}
module
.
exports
=
AbstractDialect
module
.
exports
=
AbstractDialect
\ No newline at end of file
lib/dialects/abstract/query-generator.js
View file @
64fb335
var
Utils
=
require
(
"../../utils"
)
var
Utils
=
require
(
"../../utils"
)
,
SqlString
=
require
(
"../../sql-string"
)
,
SqlString
=
require
(
"../../sql-string"
)
,
daoFactory
=
require
(
"../../dao-factory"
)
,
daoFactory
=
require
(
"../../dao-factory"
)
,
_
=
require
(
'lodash'
)
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
var
QueryGenerator
=
{
addSchema
:
function
(
opts
)
{
addSchema
:
function
(
param
)
{
throwMethodUndefined
(
'addSchema'
)
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() {
...
@@ -27,7 +35,13 @@ module.exports = (function() {
Returns a query for dropping a table.
Returns a query for dropping a table.
*/
*/
dropTableQuery
:
function
(
tableName
,
options
)
{
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() {
...
@@ -329,6 +343,31 @@ module.exports = (function() {
throwMethodUndefined
(
'findAutoIncrementField'
)
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
Quote an object based on its type. This is a more general version of quoteIdentifiers
Strings: should proxy to quoteIdentifiers
Strings: should proxy to quoteIdentifiers
...
@@ -531,8 +570,8 @@ module.exports = (function() {
...
@@ -531,8 +570,8 @@ module.exports = (function() {
,
subJoinQueries
=
[]
,
subJoinQueries
=
[]
// Escape table
// Escape table
options
.
table
=
table
=
!
Array
.
isArray
(
tableName
)
?
this
.
quote
Identifiers
(
tableName
)
:
tableName
.
map
(
function
(
t
)
{
options
.
table
=
table
=
!
Array
.
isArray
(
tableName
)
?
this
.
quote
Table
(
tableName
)
:
tableName
.
map
(
function
(
t
)
{
return
this
.
quote
Identifiers
(
t
)
return
this
.
quote
Table
(
t
)
}.
bind
(
this
)).
join
(
", "
)
}.
bind
(
this
)).
join
(
", "
)
if
(
subQuery
&&
mainAttributes
)
{
if
(
subQuery
&&
mainAttributes
)
{
...
@@ -570,7 +609,7 @@ module.exports = (function() {
...
@@ -570,7 +609,7 @@ module.exports = (function() {
}
}
if
(
options
.
include
&&
attr
.
indexOf
(
'.'
)
===
-
1
&&
addTable
)
{
if
(
options
.
include
&&
attr
.
indexOf
(
'.'
)
===
-
1
&&
addTable
)
{
attr
=
this
.
quote
Identifier
(
options
.
table
)
+
'.'
+
attr
attr
=
this
.
quote
Table
(
options
.
table
)
+
'.'
+
attr
}
}
return
attr
return
attr
...
...
lib/dialects/mysql/query-generator.js
View file @
64fb335
...
@@ -5,24 +5,6 @@ var Utils = require("../../utils")
...
@@ -5,24 +5,6 @@ var Utils = require("../../utils")
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
var
QueryGenerator
=
{
dialect
:
'mysql'
,
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
()
{
createSchema
:
function
()
{
var
query
=
"SHOW TABLES"
var
query
=
"SHOW TABLES"
...
@@ -103,16 +85,6 @@ module.exports = (function() {
...
@@ -103,16 +85,6 @@ module.exports = (function() {
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
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
()
{
showTablesQuery
:
function
()
{
return
'SHOW TABLES;'
return
'SHOW TABLES;'
},
},
...
@@ -424,10 +396,6 @@ module.exports = (function() {
...
@@ -424,10 +396,6 @@ module.exports = (function() {
return
identifiers
.
split
(
'.'
).
map
(
function
(
v
)
{
return
this
.
quoteIdentifier
(
v
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
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.
* 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) {
...
@@ -7,7 +7,8 @@ var PostgresDialect = function(sequelize) {
PostgresDialect
.
prototype
.
supports
=
_
.
defaults
({
PostgresDialect
.
prototype
.
supports
=
_
.
defaults
({
'RETURNING'
:
true
,
'RETURNING'
:
true
,
'DEFAULT VALUES'
:
true
'DEFAULT VALUES'
:
true
,
schemas
:
true
},
Abstract
.
prototype
.
supports
)
},
Abstract
.
prototype
.
supports
)
module
.
exports
=
PostgresDialect
module
.
exports
=
PostgresDialect
lib/dialects/postgres/query-generator.js
View file @
64fb335
...
@@ -10,23 +10,15 @@ module.exports = (function() {
...
@@ -10,23 +10,15 @@ module.exports = (function() {
options
:
{},
options
:
{},
dialect
:
'postgres'
,
dialect
:
'postgres'
,
addSchema
:
function
(
opts
)
{
addSchema
:
function
(
param
)
{
var
tableName
=
undefined
var
schema
=
(
param
.
options
&&
param
.
options
.
schema
?
param
.
options
.
schema
:
undefined
)
var
schema
=
(
!!
opts
.
options
&&
!!
opts
.
options
.
schema
?
opts
.
options
.
schema
:
undefined
)
,
schemaDelimiter
=
(
param
.
options
&&
param
.
options
.
schemaDelimiter
?
param
.
options
.
schemaDelimiter
:
undefined
)
var
schemaDelimiter
=
(
!!
opts
.
options
&&
!!
opts
.
options
.
schemaDelimiter
?
opts
.
options
.
schemaDelimiter
:
undefined
)
if
(
!!
opts
&&
!!
opts
.
tableName
)
{
return
{
tableName
=
opts
.
tableName
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
)
{
createSchema
:
function
(
schema
)
{
...
@@ -97,10 +89,9 @@ module.exports = (function() {
...
@@ -97,10 +89,9 @@ module.exports = (function() {
dropTableQuery
:
function
(
tableName
,
options
)
{
dropTableQuery
:
function
(
tableName
,
options
)
{
options
=
options
||
{}
options
=
options
||
{}
var
query
=
"DROP TABLE IF EXISTS <%=
schema %><%=
table %><%= cascade %>;"
var
query
=
"DROP TABLE IF EXISTS <%= table %><%= cascade %>;"
return
Utils
.
_
.
template
(
query
)({
return
Utils
.
_
.
template
(
query
)({
schema
:
options
.
schema
?
this
.
quoteIdentifiers
(
options
.
schema
)
+
'.'
:
''
,
table
:
this
.
quoteTable
(
tableName
),
table
:
this
.
quoteIdentifiers
(
tableName
),
cascade
:
options
.
cascade
?
" CASCADE"
:
""
cascade
:
options
.
cascade
?
" CASCADE"
:
""
})
})
},
},
...
@@ -818,10 +809,6 @@ module.exports = (function() {
...
@@ -818,10 +809,6 @@ module.exports = (function() {
return
identifiers
.
split
(
'.'
).
map
(
function
(
t
)
{
return
this
.
quoteIdentifier
(
t
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
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.
* 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() {
...
@@ -15,23 +15,15 @@ module.exports = (function() {
options
:
{},
options
:
{},
dialect
:
'sqlite'
,
dialect
:
'sqlite'
,
addSchema
:
function
(
opts
)
{
addSchema
:
function
(
param
)
{
var
tableName
=
undefined
var
schema
=
(
param
.
options
&&
param
.
options
.
schema
?
param
.
options
.
schema
:
undefined
)
var
schema
=
(
!!
opts
&&
!!
opts
.
options
&&
!!
opts
.
options
.
schema
?
opts
.
options
.
schema
:
undefined
)
,
schemaDelimiter
=
(
param
.
options
&&
param
.
options
.
schemaDelimiter
?
param
.
options
.
schemaDelimiter
:
undefined
)
var
schemaDelimiter
=
(
!!
opts
&&
!!
opts
.
options
&&
!!
opts
.
options
.
schemaDelimiter
?
opts
.
options
.
schemaDelimiter
:
undefined
)
return
{
if
(
!!
opts
&&
!!
opts
.
tableName
)
{
tableName
:
param
.
tableName
||
param
,
tableName
=
opts
.
tableName
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
()
{
createSchema
:
function
()
{
...
@@ -109,7 +101,7 @@ module.exports = (function() {
...
@@ -109,7 +101,7 @@ module.exports = (function() {
}
}
var
values
=
{
var
values
=
{
table
:
this
.
quote
Identifier
(
tableName
),
table
:
this
.
quote
Table
(
tableName
),
attributes
:
attrStr
.
join
(
", "
),
attributes
:
attrStr
.
join
(
", "
),
charset
:
(
options
.
charset
?
"DEFAULT CHARSET="
+
options
.
charset
:
""
)
charset
:
(
options
.
charset
?
"DEFAULT CHARSET="
+
options
.
charset
:
""
)
}
}
...
@@ -133,16 +125,6 @@ module.exports = (function() {
...
@@ -133,16 +125,6 @@ module.exports = (function() {
return
!!
value
?
1
:
0
;
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
:
{
uniqueConstraintMapping
:
{
code
:
'SQLITE_CONSTRAINT'
,
code
:
'SQLITE_CONSTRAINT'
,
map
:
function
(
str
)
{
map
:
function
(
str
)
{
...
@@ -199,7 +181,7 @@ module.exports = (function() {
...
@@ -199,7 +181,7 @@ module.exports = (function() {
var
replacements
=
{
var
replacements
=
{
ignoreDuplicates
:
options
&&
options
.
ignoreDuplicates
?
' OR IGNORE'
:
''
,
ignoreDuplicates
:
options
&&
options
.
ignoreDuplicates
?
' OR IGNORE'
:
''
,
table
:
this
.
quote
Identifier
(
tableName
),
table
:
this
.
quote
Table
(
tableName
),
attributes
:
allAttributes
.
map
(
function
(
attr
){
attributes
:
allAttributes
.
map
(
function
(
attr
){
return
this
.
quoteIdentifier
(
attr
)
return
this
.
quoteIdentifier
(
attr
)
}.
bind
(
this
)).
join
(
","
),
}.
bind
(
this
)).
join
(
","
),
...
@@ -221,7 +203,7 @@ module.exports = (function() {
...
@@ -221,7 +203,7 @@ module.exports = (function() {
}
}
var
replacements
=
{
var
replacements
=
{
table
:
this
.
quote
Identifier
(
tableName
),
table
:
this
.
quote
Table
(
tableName
),
values
:
values
.
join
(
","
),
values
:
values
.
join
(
","
),
where
:
this
.
getWhereConditions
(
where
)
where
:
this
.
getWhereConditions
(
where
)
}
}
...
@@ -234,7 +216,7 @@ module.exports = (function() {
...
@@ -234,7 +216,7 @@ module.exports = (function() {
var
query
=
"DELETE FROM <%= table %> WHERE <%= where %>"
var
query
=
"DELETE FROM <%= table %> WHERE <%= where %>"
var
replacements
=
{
var
replacements
=
{
table
:
this
.
quote
Identifier
(
tableName
),
table
:
this
.
quote
Table
(
tableName
),
where
:
this
.
getWhereConditions
(
where
)
where
:
this
.
getWhereConditions
(
where
)
}
}
...
@@ -285,7 +267,7 @@ module.exports = (function() {
...
@@ -285,7 +267,7 @@ module.exports = (function() {
if
(
dataType
.
references
)
{
if
(
dataType
.
references
)
{
template
+=
" REFERENCES <%= referencesTable %> (<%= referencesKey %>)"
template
+=
" REFERENCES <%= referencesTable %> (<%= referencesKey %>)"
replacements
.
referencesTable
=
this
.
quote
Identifier
(
dataType
.
references
)
replacements
.
referencesTable
=
this
.
quote
Table
(
dataType
.
references
)
if
(
dataType
.
referencesKey
)
{
if
(
dataType
.
referencesKey
)
{
replacements
.
referencesKey
=
this
.
quoteIdentifier
(
dataType
.
referencesKey
)
replacements
.
referencesKey
=
this
.
quoteIdentifier
(
dataType
.
referencesKey
)
...
@@ -434,10 +416,6 @@ module.exports = (function() {
...
@@ -434,10 +416,6 @@ module.exports = (function() {
return
identifiers
.
split
(
'.'
).
map
(
function
(
v
)
{
return
this
.
quoteIdentifier
(
v
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
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.
* 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() {
...
@@ -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'
)
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'
)
,
Task
=
this
.
sequelize
.
define
(
'TaskXYZ'
,
{
title
:
Sequelize
.
STRING
,
status
:
Sequelize
.
STRING
}).
schema
(
'archive'
)
,
self
=
this
,
self
=
this
...
@@ -92,6 +92,8 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
...
@@ -92,6 +92,8 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
task
.
getUserXYZ
().
success
(
function
(
user
)
{
task
.
getUserXYZ
().
success
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
ok
expect
(
user
).
to
.
be
.
ok
done
()
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