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 2443a317
authored
Jan 31, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1313 from schonfeld/insert-ignore-support
Bulk Insert / Ignore Option Edit
2 parents
a14b2695
e4b0af02
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
65 additions
and
8 deletions
lib/dao-factory.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
lib/query-interface.js
test/dao-factory/create.test.js
test/mysql/query-generator.test.js
test/sqlite/query-generator.test.js
lib/dao-factory.js
View file @
2443a31
...
@@ -790,7 +790,8 @@ module.exports = (function() {
...
@@ -790,7 +790,8 @@ module.exports = (function() {
options
=
Utils
.
_
.
extend
({
options
=
Utils
.
_
.
extend
({
validate
:
false
,
validate
:
false
,
hooks
:
false
hooks
:
false
,
ignoreDuplicates
:
false
},
options
||
{})
},
options
||
{})
if
(
fieldsOrOptions
instanceof
Array
)
{
if
(
fieldsOrOptions
instanceof
Array
)
{
...
@@ -800,6 +801,12 @@ module.exports = (function() {
...
@@ -800,6 +801,12 @@ module.exports = (function() {
options
=
Utils
.
_
.
extend
(
options
,
fieldsOrOptions
)
options
=
Utils
.
_
.
extend
(
options
,
fieldsOrOptions
)
}
}
if
(
this
.
daoFactoryManager
.
sequelize
.
options
.
dialect
===
'postgres'
&&
options
.
ignoreDuplicates
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
emitter
.
emit
(
'error'
,
new
Error
(
'Postgres does not support the \'ignoreDuplicates\' option.'
))
}).
run
();
}
var
self
=
this
var
self
=
this
,
updatedAtAttr
=
Utils
.
_
.
underscoredIf
(
self
.
options
.
updatedAt
,
self
.
options
.
underscored
)
,
updatedAtAttr
=
Utils
.
_
.
underscoredIf
(
self
.
options
.
updatedAt
,
self
.
options
.
underscored
)
,
createdAtAttr
=
Utils
.
_
.
underscoredIf
(
self
.
options
.
createdAt
,
self
.
options
.
underscored
)
,
createdAtAttr
=
Utils
.
_
.
underscoredIf
(
self
.
options
.
createdAt
,
self
.
options
.
underscored
)
...
...
lib/dialects/mysql/query-generator.js
View file @
2443a31
...
@@ -176,8 +176,8 @@ module.exports = (function() {
...
@@ -176,8 +176,8 @@ module.exports = (function() {
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
.
join
(
', '
)
})
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
.
join
(
', '
)
})
},
},
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
)
{
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
,
options
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
var
query
=
"INSERT
<%= ignoreDuplicates %>
INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
,
tuples
=
[]
,
tuples
=
[]
,
allAttributes
=
[]
,
allAttributes
=
[]
...
@@ -196,6 +196,7 @@ module.exports = (function() {
...
@@ -196,6 +196,7 @@ module.exports = (function() {
}.
bind
(
this
))
}.
bind
(
this
))
var
replacements
=
{
var
replacements
=
{
ignoreDuplicates
:
options
&&
options
.
ignoreDuplicates
?
' IGNORE'
:
''
,
table
:
this
.
quoteIdentifier
(
tableName
),
table
:
this
.
quoteIdentifier
(
tableName
),
attributes
:
allAttributes
.
map
(
function
(
attr
){
attributes
:
allAttributes
.
map
(
function
(
attr
){
return
this
.
quoteIdentifier
(
attr
)
return
this
.
quoteIdentifier
(
attr
)
...
...
lib/dialects/postgres/query-generator.js
View file @
2443a31
...
@@ -267,7 +267,7 @@ module.exports = (function() {
...
@@ -267,7 +267,7 @@ module.exports = (function() {
})
})
},
},
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
)
{
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
,
options
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;"
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;"
,
tuples
=
[]
,
tuples
=
[]
,
serials
=
[]
,
serials
=
[]
...
...
lib/dialects/sqlite/query-generator.js
View file @
2443a31
...
@@ -172,8 +172,8 @@ module.exports = (function() {
...
@@ -172,8 +172,8 @@ module.exports = (function() {
return
"SELECT name FROM sqlite_master WHERE type='table' and name!='sqlite_sequence';"
return
"SELECT name FROM sqlite_master WHERE type='table' and name!='sqlite_sequence';"
},
},
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
)
{
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
,
options
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
var
query
=
"INSERT
<%= ignoreDuplicates %>
INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
,
tuples
=
[]
,
tuples
=
[]
,
allAttributes
=
[]
,
allAttributes
=
[]
...
@@ -192,6 +192,7 @@ module.exports = (function() {
...
@@ -192,6 +192,7 @@ module.exports = (function() {
}.
bind
(
this
))
}.
bind
(
this
))
var
replacements
=
{
var
replacements
=
{
ignoreDuplicates
:
options
&&
options
.
ignoreDuplicates
?
' OR IGNORE'
:
''
,
table
:
this
.
quoteIdentifier
(
tableName
),
table
:
this
.
quoteIdentifier
(
tableName
),
attributes
:
allAttributes
.
map
(
function
(
attr
){
attributes
:
allAttributes
.
map
(
function
(
attr
){
return
this
.
quoteIdentifier
(
attr
)
return
this
.
quoteIdentifier
(
attr
)
...
...
lib/query-interface.js
View file @
2443a31
...
@@ -510,7 +510,7 @@ module.exports = (function() {
...
@@ -510,7 +510,7 @@ module.exports = (function() {
}
}
QueryInterface
.
prototype
.
bulkInsert
=
function
(
tableName
,
records
,
options
)
{
QueryInterface
.
prototype
.
bulkInsert
=
function
(
tableName
,
records
,
options
)
{
var
sql
=
this
.
QueryGenerator
.
bulkInsertQuery
(
tableName
,
records
)
var
sql
=
this
.
QueryGenerator
.
bulkInsertQuery
(
tableName
,
records
,
options
)
return
queryAndEmit
.
call
(
this
,
[
sql
,
null
,
options
],
'bulkInsert'
)
return
queryAndEmit
.
call
(
this
,
[
sql
,
null
,
options
],
'bulkInsert'
)
}
}
...
...
test/dao-factory/create.test.js
View file @
2443a31
...
@@ -24,7 +24,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -24,7 +24,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
data
:
DataTypes
.
STRING
,
data
:
DataTypes
.
STRING
,
intVal
:
DataTypes
.
INTEGER
,
intVal
:
DataTypes
.
INTEGER
,
theDate
:
DataTypes
.
DATE
,
theDate
:
DataTypes
.
DATE
,
aBool
:
DataTypes
.
BOOLEAN
aBool
:
DataTypes
.
BOOLEAN
,
uniqueName
:
{
type
:
DataTypes
.
STRING
,
unique
:
true
}
})
})
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
...
@@ -1004,6 +1005,47 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -1004,6 +1005,47 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
})
if
(
Support
.
getTestDialect
()
!==
'postgres'
)
{
it
(
"should support the ignoreDuplicates option"
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
uniqueName
:
'Peter'
,
secretValue
:
'42'
},
{
uniqueName
:
'Paul'
,
secretValue
:
'23'
}]
this
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
]
}).
success
(
function
()
{
data
.
push
({
uniqueName
:
'Michael'
,
secretValue
:
'26'
});
self
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
],
ignoreDuplicates
:
true
}).
success
(
function
()
{
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
3
)
expect
(
users
[
0
].
uniqueName
).
to
.
equal
(
"Peter"
)
expect
(
users
[
0
].
secretValue
).
to
.
equal
(
"42"
);
expect
(
users
[
1
].
uniqueName
).
to
.
equal
(
"Paul"
)
expect
(
users
[
1
].
secretValue
).
to
.
equal
(
"23"
);
expect
(
users
[
2
].
uniqueName
).
to
.
equal
(
"Michael"
)
expect
(
users
[
2
].
secretValue
).
to
.
equal
(
"26"
);
done
()
});
});
})
})
}
else
{
it
(
"should throw an error when the ignoreDuplicates option is passed"
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
uniqueName
:
'Peter'
,
secretValue
:
'42'
},
{
uniqueName
:
'Paul'
,
secretValue
:
'23'
}]
this
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
]
}).
success
(
function
()
{
data
.
push
({
uniqueName
:
'Michael'
,
secretValue
:
'26'
});
self
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
],
ignoreDuplicates
:
true
}).
error
(
function
(
err
)
{
expect
(
err
).
to
.
exist
expect
(
err
.
message
).
to
.
match
(
/Postgres does not support the
\'
ignoreDuplicates
\'
option./
)
done
();
})
})
})
}
describe
(
'enums'
,
function
()
{
describe
(
'enums'
,
function
()
{
it
(
'correctly restores enum values'
,
function
(
done
)
{
it
(
'correctly restores enum values'
,
function
(
done
)
{
var
self
=
this
var
self
=
this
...
...
test/mysql/query-generator.test.js
View file @
2443a31
...
@@ -389,6 +389,9 @@ if (Support.dialectIsMySQL()) {
...
@@ -389,6 +389,9 @@ if (Support.dialectIsMySQL()) {
},
{
},
{
arguments
:
[
'myTable'
,
[{
name
:
"foo"
,
value
:
true
},
{
name
:
'bar'
,
value
:
false
}]],
arguments
:
[
'myTable'
,
[{
name
:
"foo"
,
value
:
true
},
{
name
:
'bar'
,
value
:
false
}]],
expectation
:
"INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',true),('bar',false);"
expectation
:
"INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',true),('bar',false);"
},
{
arguments
:
[
'myTable'
,
[{
name
:
'foo'
},
{
name
:
'bar'
}],
{
ignoreDuplicates
:
true
}],
expectation
:
"INSERT IGNORE INTO `myTable` (`name`) VALUES ('foo'),('bar');"
}
}
],
],
...
...
test/sqlite/query-generator.test.js
View file @
2443a31
...
@@ -385,6 +385,9 @@ if (dialect === 'sqlite') {
...
@@ -385,6 +385,9 @@ if (dialect === 'sqlite') {
arguments
:
[
'myTable'
,
[{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
},
{
name
:
'bar'
,
foo
:
2
,
nullValue
:
null
}]],
arguments
:
[
'myTable'
,
[{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
},
{
name
:
'bar'
,
foo
:
2
,
nullValue
:
null
}]],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',2,NULL);"
,
expectation
:
"INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',2,NULL);"
,
context
:
{
options
:
{
omitNull
:
true
}}
// Note: As above
context
:
{
options
:
{
omitNull
:
true
}}
// Note: As above
},
{
arguments
:
[
'myTable'
,
[{
name
:
'foo'
},
{
name
:
'bar'
}],
{
ignoreDuplicates
:
true
}],
expectation
:
"INSERT OR IGNORE INTO `myTable` (`name`) VALUES ('foo'),('bar');"
}
}
],
],
...
...
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