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 62c33c23
authored
Sep 30, 2012
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
don't use underscore's map method for objects
1 parent
6b3b639c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
230 additions
and
141 deletions
lib/dao.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
lib/sequelize.js
lib/dao.js
View file @
62c33c2
...
...
@@ -279,11 +279,13 @@ module.exports = (function() {
}
}
Utils
.
_
.
map
(
defaults
,
function
(
value
,
attr
)
{
if
(
!
self
.
hasOwnProperty
(
attr
))
{
self
.
addAttribute
(
attr
,
Utils
.
toDefaultValue
(
value
))
for
(
var
attr
in
defaults
)
{
var
value
=
defaults
[
attr
]
if
(
!
this
.
hasOwnProperty
(
attr
))
{
this
.
addAttribute
(
attr
,
Utils
.
toDefaultValue
(
value
))
}
}
)
}
}
/* Add the instance methods to DAO */
...
...
lib/dialects/mysql/query-generator.js
View file @
62c33c2
...
...
@@ -12,24 +12,30 @@ module.exports = (function() {
var
query
=
"CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>) ENGINE=<%= engine %> <%= charset %>"
,
primaryKeys
=
[]
,
attrStr
=
Utils
.
_
.
map
(
attributes
,
function
(
dataType
,
attr
)
{
var
dt
=
dataType
if
(
Utils
.
_
.
includes
(
dt
,
'PRIMARY KEY'
))
{
primaryKeys
.
push
(
attr
)
return
Utils
.
addTicks
(
attr
)
+
" "
+
dt
.
replace
(
/PRIMARY KEY/
,
''
)
}
else
{
return
Utils
.
addTicks
(
attr
)
+
" "
+
dt
}
}).
join
(
", "
)
,
values
=
{
table
:
Utils
.
addTicks
(
tableName
),
attributes
:
attrStr
,
engine
:
options
.
engine
,
charset
:
(
options
.
charset
?
"DEFAULT CHARSET="
+
options
.
charset
:
""
)
}
,
pkString
=
primaryKeys
.
map
(
function
(
pk
)
{
return
Utils
.
addTicks
(
pk
)}).
join
(
", "
)
,
attrStr
=
[]
for
(
var
attr
in
attributes
)
{
var
dataType
=
attributes
[
attr
]
if
(
Utils
.
_
.
includes
(
dataType
,
'PRIMARY KEY'
))
{
primaryKeys
.
push
(
attr
)
attrStr
.
push
(
Utils
.
addTicks
(
attr
)
+
" "
+
dataType
.
replace
(
/PRIMARY KEY/
,
''
))
}
else
{
attrStr
.
push
(
Utils
.
addTicks
(
attr
)
+
" "
+
dataType
)
}
}
if
(
pkString
.
length
>
0
)
values
.
attributes
+=
", PRIMARY KEY ("
+
pkString
+
")"
var
values
=
{
table
:
Utils
.
addTicks
(
tableName
),
attributes
:
attrStr
.
join
(
", "
),
engine
:
options
.
engine
,
charset
:
(
options
.
charset
?
"DEFAULT CHARSET="
+
options
.
charset
:
""
)
}
,
pkString
=
primaryKeys
.
map
(
function
(
pk
)
{
return
Utils
.
addTicks
(
pk
)
}).
join
(
", "
)
if
(
pkString
.
length
>
0
)
{
values
.
attributes
+=
", PRIMARY KEY ("
+
pkString
+
")"
}
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
},
...
...
@@ -53,14 +59,18 @@ module.exports = (function() {
addColumnQuery
:
function
(
tableName
,
attributes
)
{
var
query
=
"ALTER TABLE `<%= tableName %>` ADD <%= attributes %>;"
,
attrString
=
Utils
.
_
.
map
(
attributes
,
function
(
definition
,
attributeName
)
{
return
Utils
.
_
.
template
(
'`<%= attributeName %>` <%= definition %>'
)({
attributeName
:
attributeName
,
definition
:
definition
})
}).
join
(
', '
)
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
})
,
attrString
=
[]
for
(
var
attrName
in
attributes
)
{
var
definition
=
attributes
[
attrName
]
attrString
.
push
(
Utils
.
_
.
template
(
'`<%= attrName %>` <%= definition %>'
)({
attrName
:
attrName
,
definition
:
definition
}))
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
.
join
(
', '
)
})
},
removeColumnQuery
:
function
(
tableName
,
attributeName
)
{
...
...
@@ -70,27 +80,35 @@ module.exports = (function() {
changeColumnQuery
:
function
(
tableName
,
attributes
)
{
var
query
=
"ALTER TABLE `<%= tableName %>` CHANGE <%= attributes %>;"
var
attrString
=
Utils
.
_
.
map
(
attributes
,
function
(
definition
,
attributeName
)
{
return
Utils
.
_
.
template
(
'`<%= attributeName %>` `<%= attributeName %>` <%= definition %>'
)({
attributeName
:
attributeName
,
var
attrString
=
[]
for
(
attrName
in
attributes
)
{
var
definition
=
attributes
[
attrName
]
attrString
.
push
(
Utils
.
_
.
template
(
'`<%= attrName %>` `<%= attrName %>` <%= definition %>'
)({
attrName
:
attrName
,
definition
:
definition
})
}
).
join
(
', '
)
})
)
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
})
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
.
join
(
', '
)
})
},
renameColumnQuery
:
function
(
tableName
,
attrBefore
,
attributes
)
{
var
query
=
"ALTER TABLE `<%= tableName %>` CHANGE <%= attributes %>;"
var
attrString
=
Utils
.
_
.
map
(
attributes
,
function
(
definition
,
attributeName
)
{
return
Utils
.
_
.
template
(
'`<%= before %>` `<%= after %>` <%= definition %>'
)({
var
attrString
=
[]
for
(
var
attrName
in
attributes
)
{
var
definition
=
attributes
[
attrName
]
attrString
.
push
(
Utils
.
_
.
template
(
'`<%= before %>` `<%= after %>` <%= definition %>'
)({
before
:
attrBefore
,
after
:
attr
ibute
Name
,
after
:
attrName
,
definition
:
definition
})
}
).
join
(
', '
)
})
)
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
})
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
.
join
(
', '
)
})
},
selectQuery
:
function
(
tableName
,
options
)
{
...
...
@@ -99,10 +117,11 @@ module.exports = (function() {
options
=
options
||
{}
options
.
table
=
Array
.
isArray
(
tableName
)
?
tableName
.
map
(
function
(
tbl
){
return
Utils
.
addTicks
(
tbl
)
}).
join
(
", "
)
:
Utils
.
addTicks
(
tableName
)
options
.
attributes
=
options
.
attributes
&&
options
.
attributes
.
map
(
function
(
attr
){
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
==
2
)
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
==
2
)
{
return
[
attr
[
0
],
Utils
.
addTicks
(
attr
[
1
])].
join
(
' as '
)
else
}
else
{
return
attr
.
indexOf
(
Utils
.
TICK_CHAR
)
<
0
?
Utils
.
addTicks
(
attr
)
:
attr
}
}).
join
(
", "
)
options
.
attributes
=
options
.
attributes
||
'*'
...
...
@@ -162,13 +181,19 @@ module.exports = (function() {
updateQuery
:
function
(
tableName
,
attrValueHash
,
where
)
{
attrValueHash
=
Utils
.
removeNullValuesFromHash
(
attrValueHash
,
this
.
options
.
omitNull
)
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
,
values
=
[]
for
(
var
key
in
attrValueHash
)
{
var
value
=
attrValueHash
[
key
]
,
_value
=
(
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
values
.
push
(
Utils
.
addTicks
(
key
)
+
"="
+
Utils
.
escape
(
_value
))
}
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
values
:
Utils
.
_
.
map
(
attrValueHash
,
function
(
value
,
key
){
return
Utils
.
addTicks
(
key
)
+
"="
+
Utils
.
escape
((
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
)
}).
join
(
","
),
values
:
values
.
join
(
","
),
where
:
QueryGenerator
.
getWhereConditions
(
where
)
}
...
...
@@ -266,7 +291,11 @@ module.exports = (function() {
},
hashToWhereConditions
:
function
(
hash
)
{
return
Utils
.
_
.
map
(
hash
,
function
(
value
,
key
)
{
var
result
=
[]
for
(
var
key
in
hash
)
{
var
value
=
hash
[
key
]
//handle qualified key names
var
_key
=
key
.
split
(
'.'
).
map
(
function
(
col
){
return
Utils
.
addTicks
(
col
)}).
join
(
"."
)
,
_value
=
null
...
...
@@ -274,28 +303,32 @@ module.exports = (function() {
if
(
Array
.
isArray
(
value
))
{
// is value an array?
_value
=
"("
+
Utils
.
_
.
map
(
value
,
function
(
subv
alue
)
{
return
Utils
.
escape
(
sub
v
alue
);
_value
=
"("
+
value
.
map
(
function
(
subV
alue
)
{
return
Utils
.
escape
(
sub
V
alue
);
}).
join
(
','
)
+
")"
re
turn
[
_key
,
_value
].
join
(
" IN "
)
re
sult
.
push
([
_key
,
_value
].
join
(
" IN "
)
)
}
else
if
((
value
)
&&
(
typeof
value
==
'object'
))
{
// is value an object?
//using as sentinel for join column => value
_value
=
value
.
join
.
split
(
'.'
).
map
(
function
(
col
){
return
Utils
.
addTicks
(
col
)
}).
join
(
"."
)
re
turn
[
_key
,
_value
].
join
(
"="
)
_value
=
value
.
join
.
split
(
'.'
).
map
(
function
(
col
){
return
Utils
.
addTicks
(
col
)
}).
join
(
"."
)
re
sult
.
push
([
_key
,
_value
].
join
(
"="
)
)
}
else
{
_value
=
Utils
.
escape
(
value
)
re
turn
(
_value
==
'NULL'
)
?
_key
+
" IS NULL"
:
[
_key
,
_value
].
join
(
"="
)
re
sult
.
push
((
_value
==
'NULL'
)
?
_key
+
" IS NULL"
:
[
_key
,
_value
].
join
(
"="
)
)
}
}).
join
(
" AND "
)
}
return
result
.
join
(
" AND "
)
},
attributesToSQL
:
function
(
attributes
)
{
var
result
=
{}
Utils
.
_
.
map
(
attributes
,
function
(
dataType
,
name
)
{
for
(
var
name
in
attributes
)
{
var
dataType
=
attributes
[
name
]
if
(
Utils
.
isHash
(
dataType
))
{
var
template
=
"<%= type %>"
,
replacements
=
{
type
:
dataType
.
type
}
...
...
@@ -325,18 +358,23 @@ module.exports = (function() {
}
else
{
result
[
name
]
=
dataType
}
}
)
}
return
result
},
findAutoIncrementField
:
function
(
factory
)
{
var
fields
=
Utils
.
_
.
map
(
factory
.
attributes
,
function
(
definition
,
name
)
{
var
isAutoIncrementField
=
(
definition
&&
(
definition
.
indexOf
(
'auto_increment'
)
>
-
1
))
return
isAutoIncrementField
?
name
:
null
})
var
fields
=
[]
for
(
var
name
in
factory
.
attributes
)
{
var
definition
=
factory
.
attributes
[
name
]
if
(
definition
&&
(
definition
.
indexOf
(
'auto_increment'
)
>
-
1
))
{
fields
.
push
(
name
)
}
}
return
Utils
.
_
.
compact
(
fields
)
return
fields
}
}
...
...
lib/dialects/postgres/query-generator.js
View file @
62c33c2
...
...
@@ -68,17 +68,22 @@ module.exports = (function() {
tables
[
tableName
]
=
{}
var
query
=
"CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)"
,
attrStr
=
Utils
.
_
.
map
(
attributes
,
function
(
dataType
,
attr
)
{
dataType
=
pgDataTypeMapping
(
tableName
,
attr
,
dataType
)
return
addQuotes
(
attr
)
+
" "
+
dataType
}).
join
(
", "
)
,
values
=
{
table
:
addQuotes
(
tableName
),
attributes
:
attrStr
,
}
,
attrStr
=
[]
for
(
var
attr
in
attributes
)
{
var
dataType
=
pgDataTypeMapping
(
tableName
,
attr
,
attributes
[
attr
])
attrStr
.
push
(
addQuotes
(
attr
)
+
" "
+
dataType
)
}
var
pks
=
primaryKeys
[
tableName
].
map
(
function
(
pk
){
return
addQuotes
(
pk
)}).
join
(
","
)
if
(
pks
.
length
>
0
)
values
.
attributes
+=
", PRIMARY KEY ("
+
pks
+
")"
var
values
=
{
table
:
addQuotes
(
tableName
),
attributes
:
attrStr
.
join
(
", "
),
}
var
pks
=
primaryKeys
[
tableName
].
map
(
function
(
pk
){
return
addQuotes
(
pk
)
}).
join
(
","
)
if
(
pks
.
length
>
0
)
{
values
.
attributes
+=
", PRIMARY KEY ("
+
pks
+
")"
}
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
},
...
...
@@ -105,14 +110,18 @@ module.exports = (function() {
addColumnQuery
:
function
(
tableName
,
attributes
)
{
var
query
=
"ALTER TABLE <%= tableName %> ADD COLUMN <%= attributes %>;"
,
attrString
=
Utils
.
_
.
map
(
attributes
,
function
(
definition
,
attributeName
)
{
return
Utils
.
_
.
template
(
'<%= attributeName %> <%= definition %>'
)({
attributeName
:
addQuotes
(
attributeName
),
definition
:
pgDataTypeMapping
(
tableName
,
attributeName
,
definition
)
})
}).
join
(
', '
)
return
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
attributes
:
attrString
})
,
attrString
=
[]
for
(
var
attrName
in
attributes
)
{
var
definition
=
attributes
[
attrName
]
attrString
.
push
(
Utils
.
_
.
template
(
'<%= attrName %> <%= definition %>'
)({
attrName
:
addQuotes
(
attrName
),
definition
:
pgDataTypeMapping
(
tableName
,
attrName
,
definition
)
}))
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
attributes
:
attrString
.
join
(
', '
)
})
},
removeColumnQuery
:
function
(
tableName
,
attributeName
)
{
...
...
@@ -122,9 +131,11 @@ module.exports = (function() {
changeColumnQuery
:
function
(
tableName
,
attributes
)
{
var
query
=
"ALTER TABLE <%= tableName %> ALTER COLUMN <%= query %>;"
,
sql
=
[]
var
sql
=
Utils
.
_
.
map
(
attributes
,
function
(
definition
,
attributeName
)
{
for
(
var
attributeName
in
attributes
)
{
var
attrSql
=
''
if
(
definition
.
indexOf
(
'NOT NULL'
)
>
0
)
{
attrSql
+=
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
...
...
@@ -137,26 +148,30 @@ module.exports = (function() {
query
:
addQuotes
(
attributeName
)
+
' DROP NOT NULL'
})
}
attrSql
+=
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
query
:
addQuotes
(
attributeName
)
+
' TYPE '
+
definition
})
return
attrSql
;
}).
join
(
''
)
return
sql
sql
.
push
(
attrSql
)
}
return
sql
.
join
(
''
)
},
renameColumnQuery
:
function
(
tableName
,
attrBefore
,
attributes
)
{
var
query
=
"ALTER TABLE <%= tableName %> RENAME COLUMN <%= attributes %>;"
var
attrString
=
Utils
.
_
.
map
(
attributes
,
function
(
definition
,
attributeName
)
{
return
Utils
.
_
.
template
(
'<%= before %> TO <%= after %>'
)({
var
attrString
=
[]
for
(
var
attributeName
in
attributes
)
{
attrString
.
push
(
Utils
.
_
.
template
(
'<%= before %> TO <%= after %>'
)({
before
:
addQuotes
(
attrBefore
),
after
:
addQuotes
(
attributeName
),
})
}
).
join
(
', '
)
})
)
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
attributes
:
attrString
})
return
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
attributes
:
attrString
.
join
(
', '
)
})
},
selectQuery
:
function
(
tableName
,
options
)
{
...
...
@@ -226,13 +241,17 @@ module.exports = (function() {
updateQuery
:
function
(
tableName
,
attrValueHash
,
where
)
{
attrValueHash
=
Utils
.
removeNullValuesFromHash
(
attrValueHash
,
this
.
options
.
omitNull
)
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
,
values
=
[]
for
(
var
key
in
attrValueHash
)
{
var
value
=
attrValueHash
[
key
]
values
.
push
(
addQuotes
(
key
)
+
"="
+
pgEscape
((
value
instanceof
Date
)
?
pgSqlDate
(
value
)
:
value
))
}
var
replacements
=
{
table
:
addQuotes
(
tableName
),
values
:
Utils
.
_
.
map
(
attrValueHash
,
function
(
value
,
key
){
return
addQuotes
(
key
)
+
"="
+
pgEscape
((
value
instanceof
Date
)
?
pgSqlDate
(
value
)
:
value
)
}).
join
(
","
),
values
:
values
.
join
(
","
),
where
:
QueryGenerator
.
getWhereConditions
(
where
)
}
...
...
@@ -335,33 +354,41 @@ module.exports = (function() {
},
hashToWhereConditions
:
function
(
hash
)
{
return
Utils
.
_
.
map
(
hash
,
function
(
value
,
key
)
{
//handle qualified key names
var
result
=
[]
for
(
var
key
in
hash
)
{
var
value
=
hash
[
key
]
//handle qualified key names
var
_key
=
key
.
split
(
'.'
).
map
(
function
(
col
){
return
addQuotes
(
col
)}).
join
(
"."
)
,
_value
=
null
if
(
Array
.
isArray
(
value
))
{
_value
=
"("
+
Utils
.
_
.
map
(
value
,
function
(
subv
alue
)
{
return
pgEscape
(
sub
v
alue
);
if
(
Array
.
isArray
(
value
))
{
_value
=
"("
+
value
.
map
(
function
(
subV
alue
)
{
return
pgEscape
(
sub
V
alue
);
}).
join
(
','
)
+
")"
re
turn
[
_key
,
_value
].
join
(
" IN "
)
}
else
if
((
value
)
&&
(
typeof
value
==
'object'
))
{
//using as sentinel for join column => value
_value
=
value
.
join
.
split
(
'.'
).
map
(
function
(
col
){
return
addQuotes
(
col
)}).
join
(
"."
)
return
[
_key
,
_value
].
join
(
"="
)
}
else
{
re
sult
.
push
([
_key
,
_value
].
join
(
" IN "
)
)
}
else
if
((
value
)
&&
(
typeof
value
==
'object'
))
{
//using as sentinel for join column => value
_value
=
value
.
join
.
split
(
'.'
).
map
(
function
(
col
){
return
addQuotes
(
col
)}).
join
(
"."
)
result
.
push
([
_key
,
_value
].
join
(
"="
)
)
}
else
{
_value
=
pgEscape
(
value
)
re
turn
(
_value
==
'NULL'
)
?
_key
+
" IS NULL"
:
[
_key
,
_value
].
join
(
"="
)
re
sult
.
push
((
_value
==
'NULL'
)
?
_key
+
" IS NULL"
:
[
_key
,
_value
].
join
(
"="
)
)
}
}).
join
(
" AND "
)
}
return
result
.
join
(
' AND '
)
},
attributesToSQL
:
function
(
attributes
)
{
var
result
=
{}
Utils
.
_
.
map
(
attributes
,
function
(
dataType
,
name
)
{
for
(
var
name
in
attributes
)
{
var
dataType
=
attributes
[
name
]
if
(
Utils
.
isHash
(
dataType
))
{
var
template
=
"<%= type %>"
,
replacements
=
{
type
:
dataType
.
type
}
...
...
@@ -382,18 +409,23 @@ module.exports = (function() {
}
else
{
result
[
name
]
=
dataType
}
}
)
}
return
result
},
findAutoIncrementField
:
function
(
factory
)
{
var
fields
=
Utils
.
_
.
map
(
factory
.
attributes
,
function
(
definition
,
name
)
{
var
isAutoIncrementField
=
(
definition
&&
(
definition
.
indexOf
(
'SERIAL'
)
>
-
1
))
return
isAutoIncrementField
?
name
:
null
})
var
fields
=
[]
for
(
var
name
in
factory
.
attributes
)
{
var
definition
=
factory
.
attributes
[
name
]
if
(
definition
&&
(
definition
.
indexOf
(
'SERIAL'
)
>
-
1
))
{
fields
.
push
(
name
)
}
}
return
Utils
.
_
.
compact
(
fields
)
return
fields
},
databaseConnectionUri
:
function
(
config
)
{
...
...
lib/dialects/sqlite/query-generator.js
View file @
62c33c2
...
...
@@ -20,21 +20,24 @@ module.exports = (function() {
createTableQuery
:
function
(
tableName
,
attributes
,
options
)
{
options
=
options
||
{}
var
query
=
"CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)"
,
primaryKeys
=
[]
,
attrStr
=
Utils
.
_
.
map
(
attributes
,
function
(
dataType
,
attr
)
{
if
(
Utils
.
_
.
includes
(
dataType
,
'PRIMARY KEY'
))
{
primaryKeys
.
push
(
attr
)
return
Utils
.
addTicks
(
attr
)
+
" "
+
dataType
}
else
{
return
Utils
.
addTicks
(
attr
)
+
" "
+
dataType
}
}).
join
(
", "
)
,
values
=
{
table
:
Utils
.
addTicks
(
tableName
),
attributes
:
attrStr
,
charset
:
(
options
.
charset
?
"DEFAULT CHARSET="
+
options
.
charset
:
""
)
}
var
query
=
"CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)"
,
attrStr
=
[]
for
(
var
attr
in
attributes
)
{
var
dataType
=
attributes
[
attr
]
if
(
Utils
.
_
.
includes
(
dataType
,
'PRIMARY KEY'
))
{
attrStr
.
push
(
Utils
.
addTicks
(
attr
)
+
" "
+
dataType
)
}
else
{
attrStr
.
push
(
Utils
.
addTicks
(
attr
)
+
" "
+
dataType
)
}
}
var
values
=
{
table
:
Utils
.
addTicks
(
tableName
),
attributes
:
attrStr
.
join
(
", "
),
charset
:
(
options
.
charset
?
"DEFAULT CHARSET="
+
options
.
charset
:
""
)
}
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
},
...
...
@@ -62,13 +65,17 @@ module.exports = (function() {
updateQuery
:
function
(
tableName
,
attrValueHash
,
where
)
{
attrValueHash
=
Utils
.
removeNullValuesFromHash
(
attrValueHash
,
this
.
options
.
omitNull
)
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
var
query
=
"UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
,
values
=
[]
for
(
var
key
in
attrValueHash
)
{
var
value
=
attrValueHash
[
key
]
values
.
push
(
Utils
.
addTicks
(
key
)
+
"="
+
escape
((
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
))
}
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
values
:
Utils
.
_
.
map
(
attrValueHash
,
function
(
value
,
key
){
return
Utils
.
addTicks
(
key
)
+
"="
+
escape
((
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
)
}).
join
(
","
),
values
:
values
.
join
(
","
),
where
:
MySqlQueryGenerator
.
getWhereConditions
(
where
)
}
...
...
@@ -91,7 +98,9 @@ module.exports = (function() {
attributesToSQL
:
function
(
attributes
)
{
var
result
=
{}
Utils
.
_
.
map
(
attributes
,
function
(
dataType
,
name
)
{
for
(
var
name
in
attributes
)
{
var
dataType
=
attributes
[
name
]
if
(
Utils
.
isHash
(
dataType
))
{
var
template
=
"<%= type %>"
,
replacements
=
{
type
:
dataType
.
type
}
...
...
@@ -111,18 +120,23 @@ module.exports = (function() {
}
else
{
result
[
name
]
=
dataType
}
}
)
}
return
result
},
findAutoIncrementField
:
function
(
factory
)
{
var
fields
=
Utils
.
_
.
map
(
factory
.
attributes
,
function
(
definition
,
name
)
{
var
isAutoIncrementField
=
(
definition
&&
(
definition
.
indexOf
(
'INTEGER PRIMARY KEY'
)
==
0
))
return
isAutoIncrementField
?
name
:
null
})
var
fields
=
[]
for
(
var
name
in
factory
.
attributes
)
{
var
definition
=
factory
.
attributes
[
name
]
if
(
definition
&&
(
definition
.
indexOf
(
'INTEGER PRIMARY KEY'
)
==
0
))
{
fields
.
push
(
name
)
}
}
return
Utils
.
_
.
compact
(
fields
)
return
fields
}
}
...
...
lib/sequelize.js
View file @
62c33c2
...
...
@@ -47,7 +47,10 @@ module.exports = (function() {
}
Sequelize
.
Utils
=
Utils
Sequelize
.
Utils
.
_
.
map
(
DataTypes
,
function
(
sql
,
accessor
)
{
Sequelize
[
accessor
]
=
sql
})
for
(
var
dataType
in
DataTypes
)
{
Sequelize
[
dataType
]
=
DataTypes
[
dataType
]
}
Sequelize
.
prototype
.
getQueryInterface
=
function
()
{
this
.
queryInterface
=
this
.
queryInterface
||
new
QueryInterface
(
this
)
...
...
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