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 cf21f351
authored
Jan 04, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
just ONE insertQuery
1 parent
c65f35e3
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
56 additions
and
73 deletions
lib/dialects/abstract/dialect.js
lib/dialects/abstract/query-generator.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite.js
lib/dialects/sqlite/query-generator.js
lib/dialects/abstract/dialect.js
View file @
cf21f35
...
...
@@ -3,7 +3,8 @@ var AbstractDialect = function() {
}
AbstractDialect
.
prototype
.
supports
=
{
'RETURNING'
:
false
'RETURNING'
:
false
,
'DEFAULT'
:
true
}
module
.
exports
=
AbstractDialect
\ No newline at end of file
lib/dialects/abstract/query-generator.js
View file @
cf21f35
...
...
@@ -104,10 +104,54 @@ module.exports = (function() {
/*
Returns an insert into command. Parameters: table name + hash of attribute-value-pairs.
*/
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
throwMethodUndefined
(
'insertQuery'
)
},
insertQuery
:
function
(
table
,
valueHash
,
modelAttributes
)
{
var
query
,
valueQuery
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>)"
,
emptyQuery
=
"INSERT INTO <%= table %>"
,
fields
=
[]
,
values
=
[]
,
key
,
value
if
(
this
.
_dialect
.
supports
[
'DEFAULT'
])
{
emptyQuery
+=
" DEFAULT VALUES"
}
if
(
this
.
_dialect
.
supports
[
'RETURNING'
])
{
valueQuery
+=
" RETURNING *"
emptyQuery
+=
" RETURNING *"
}
valueHash
=
Utils
.
removeNullValuesFromHash
(
valueHash
,
this
.
options
.
omitNull
)
for
(
key
in
valueHash
)
{
if
(
valueHash
.
hasOwnProperty
(
key
))
{
value
=
valueHash
[
key
]
fields
.
push
(
this
.
quoteIdentifier
(
key
))
// SERIALS' can't be NULL in postgresql, use DEFAULT where supported
if
(
modelAttributes
&&
modelAttributes
[
key
]
&&
modelAttributes
[
key
].
autoIncrement
===
true
&&
!
value
)
{
if
(
this
.
_dialect
.
supports
[
'DEFAULT'
])
{
values
.
push
(
'DEFAULT'
)
}
else
{
values
.
push
(
this
.
escape
(
null
))
}
}
else
{
values
.
push
(
this
.
escape
(
value
,
(
modelAttributes
&&
modelAttributes
[
key
])
||
undefined
))
}
}
}
var
replacements
=
{
table
:
this
.
quoteTable
(
table
),
attributes
:
fields
.
join
(
","
),
values
:
values
.
join
(
","
)
}
query
=
(
replacements
.
attributes
.
length
?
valueQuery
:
emptyQuery
)
+
";"
return
Utils
.
_
.
template
(
query
)(
replacements
)
},
/*
Returns an insert into command for multiple values.
Parameters: table name + list of hashes of attribute-value-pairs.
...
...
lib/dialects/mysql/query-generator.js
View file @
cf21f35
...
...
@@ -157,18 +157,6 @@ module.exports = (function() {
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
.
join
(
', '
)
})
},
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
attrValueHash
=
Utils
.
removeNullValuesFromHash
(
attrValueHash
,
this
.
options
.
omitNull
)
var
table
=
this
.
quoteIdentifier
(
tableName
)
var
attributes
=
Object
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
this
.
quoteIdentifier
(
attr
)}.
bind
(
this
)).
join
(
","
)
var
values
=
Utils
.
_
.
values
(
attrValueHash
).
map
(
function
(
v
)
{
return
this
.
escape
(
v
)
}.
bind
(
this
)).
join
(
","
)
var
query
=
"INSERT INTO "
+
table
+
" ("
+
attributes
+
") VALUES ("
+
values
+
");"
return
query
},
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
,
tuples
=
[]
...
...
lib/dialects/postgres/query-generator.js
View file @
cf21f35
...
...
@@ -247,43 +247,6 @@ module.exports = (function() {
})
},
insertQuery
:
function
(
tableName
,
attrValueHash
,
attributes
)
{
var
query
,
valueQuery
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;"
,
emptyQuery
=
"INSERT INTO <%= table %> DEFAULT VALUES RETURNING *;"
attrValueHash
=
Utils
.
removeNullValuesFromHash
(
attrValueHash
,
this
.
options
.
omitNull
)
// Remove serials that are null or undefined, which causes an error in PG
Utils
.
_
.
forEach
(
attrValueHash
,
function
(
value
,
key
,
hash
)
{
if
(
tables
[
tableName
])
{
switch
(
tables
[
tableName
][
key
])
{
case
'bigserial'
:
case
'serial'
:
if
([
null
,
undefined
].
indexOf
(
hash
[
key
])
!==
-
1
)
delete
hash
[
key
]
break
}
}
});
var
rowValues
=
[]
Object
.
keys
(
attrValueHash
).
forEach
(
function
(
attr
)
{
rowValues
[
rowValues
.
length
]
=
this
.
escape
(
attrValueHash
[
attr
],
(
!!
attributes
&&
!!
attributes
[
attr
]
?
attributes
[
attr
]
:
undefined
))
}.
bind
(
this
))
var
replacements
=
{
table
:
this
.
quoteIdentifiers
(
tableName
)
,
attributes
:
Object
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
this
.
quoteIdentifier
(
attr
)
}.
bind
(
this
)).
join
(
","
)
,
values
:
rowValues
.
join
(
","
)
}
query
=
replacements
.
attributes
.
length
?
valueQuery
:
emptyQuery
return
Utils
.
_
.
template
(
query
)(
replacements
)
},
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;"
,
tuples
=
[]
...
...
lib/dialects/sqlite.js
View file @
cf21f35
...
...
@@ -6,7 +6,7 @@ var SqliteDialect = function(sequelize) {
}
SqliteDialect
.
prototype
.
supports
=
_
.
extend
(
Abstract
.
prototype
.
supports
,
{
'DEFAULT'
:
false
})
module
.
exports
=
SqliteDialect
\ No newline at end of file
lib/dialects/sqlite/query-generator.js
View file @
cf21f35
...
...
@@ -153,25 +153,6 @@ module.exports = (function() {
return
"SELECT name FROM sqlite_master WHERE type='table' and name!='sqlite_sequence';"
},
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
attrValueHash
=
Utils
.
removeNullValuesFromHash
(
attrValueHash
,
this
.
options
.
omitNull
)
var
query
,
valueQuery
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"
,
emptyQuery
=
"INSERT INTO <%= table %> DEFAULT VALUES;"
var
replacements
=
{
table
:
this
.
quoteIdentifier
(
tableName
),
attributes
:
Object
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
this
.
quoteIdentifier
(
attr
)}.
bind
(
this
)).
join
(
","
),
values
:
Utils
.
_
.
values
(
attrValueHash
).
map
(
function
(
value
){
return
this
.
escape
(
value
)
}.
bind
(
this
)).
join
(
","
)
}
query
=
replacements
.
attributes
.
length
?
valueQuery
:
emptyQuery
return
Utils
.
_
.
template
(
query
)(
replacements
)
},
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
,
tuples
=
[]
...
...
@@ -371,6 +352,10 @@ module.exports = (function() {
})
},
startTransactionQuery
:
function
(
options
)
{
return
"BEGIN TRANSACTION;"
},
setAutocommitQuery
:
function
(
value
)
{
return
"-- SQLite does not support SET autocommit."
},
...
...
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