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 3b9faacf
authored
Jan 28, 2014
by
Michael Schonfeld
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add support for mysql insert ignore + tests
1 parent
07c67c43
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
33 additions
and
5 deletions
lib/dao-factory.js
lib/dialects/mysql/query-generator.js
lib/query-interface.js
test/dao-factory/create.test.js
test/mysql/query-generator.test.js
lib/dao-factory.js
View file @
3b9faac
...
...
@@ -778,7 +778,8 @@ module.exports = (function() {
options
=
Utils
.
_
.
extend
({
validate
:
false
,
hooks
:
false
hooks
:
false
,
ignore
:
false
},
options
||
{})
if
(
fieldsOrOptions
instanceof
Array
)
{
...
...
lib/dialects/mysql/query-generator.js
View file @
3b9faac
...
...
@@ -176,8 +176,8 @@ module.exports = (function() {
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
.
join
(
', '
)
})
},
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
,
options
)
{
var
query
=
"INSERT
<%= ignore %>
INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
,
tuples
=
[]
,
allAttributes
=
[]
...
...
@@ -196,6 +196,7 @@ module.exports = (function() {
}.
bind
(
this
))
var
replacements
=
{
ignore
:
options
&&
options
.
ignore
?
' IGNORE'
:
''
,
table
:
this
.
quoteIdentifier
(
tableName
),
attributes
:
allAttributes
.
map
(
function
(
attr
){
return
this
.
quoteIdentifier
(
attr
)
...
...
lib/query-interface.js
View file @
3b9faac
...
...
@@ -480,7 +480,7 @@ module.exports = (function() {
}
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'
)
}
...
...
test/dao-factory/create.test.js
View file @
3b9faac
...
...
@@ -24,7 +24,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
data
:
DataTypes
.
STRING
,
intVal
:
DataTypes
.
INTEGER
,
theDate
:
DataTypes
.
DATE
,
aBool
:
DataTypes
.
BOOLEAN
aBool
:
DataTypes
.
BOOLEAN
,
uniqueName
:
{
type
:
DataTypes
.
STRING
,
unique
:
true
}
})
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
...
...
@@ -990,6 +991,28 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it
(
"should support the insert ignore 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'
],
ignore
:
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
()
});
});
})
})
describe
(
'enums'
,
function
()
{
it
(
'correctly restores enum values'
,
function
(
done
)
{
var
self
=
this
...
...
test/mysql/query-generator.test.js
View file @
3b9faac
...
...
@@ -389,6 +389,9 @@ if (Support.dialectIsMySQL()) {
},
{
arguments
:
[
'myTable'
,
[{
name
:
"foo"
,
value
:
true
},
{
name
:
'bar'
,
value
:
false
}]],
expectation
:
"INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',true),('bar',false);"
},
{
arguments
:
[
'myTable'
,
[{
name
:
'foo'
},
{
name
:
'bar'
}],
{
ignore
:
true
}],
expectation
:
"INSERT 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