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