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 f14b8d71
authored
Jan 25, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1293 from janmeier/affectedRows
Affected rows
2 parents
4ad706aa
01997ae2
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
93 additions
and
15 deletions
lib/dao-factory.js
lib/dialects/abstract/query.js
lib/dialects/postgres/query.js
lib/dialects/sqlite/query.js
lib/query-interface.js
lib/query-types.js
lib/sequelize.js
test/dao-factory.test.js
lib/dao-factory.js
View file @
f14b8d7
...
...
@@ -5,6 +5,7 @@ var Utils = require("./utils")
,
sql
=
require
(
'sql'
)
,
SqlString
=
require
(
'./sql-string'
)
,
Transaction
=
require
(
'./transaction'
)
,
QueryTypes
=
require
(
'./query-types'
)
module
.
exports
=
(
function
()
{
var
DAOFactory
=
function
(
name
,
attributes
,
options
)
{
...
...
@@ -101,7 +102,7 @@ module.exports = (function() {
result
.
exec
=
function
(
options
)
{
options
=
Utils
.
_
.
extend
({
transaction
:
null
,
type
:
'SELECT'
type
:
QueryTypes
.
SELECT
},
options
||
{})
return
self
.
QueryInterface
.
queryAndEmit
([
result
.
toSql
(),
self
,
options
],
'snafu'
)
...
...
@@ -439,7 +440,7 @@ module.exports = (function() {
options
=
paranoidClause
.
call
(
this
,
options
)
return
this
.
QueryInterface
.
select
(
this
,
this
.
tableName
,
options
,
Utils
.
_
.
defaults
({
type
:
'SELECT'
,
type
:
QueryTypes
.
SELECT
,
hasJoin
:
hasJoin
},
queryOptions
,
{
transaction
:
(
options
||
{}).
transaction
}))
}
...
...
@@ -453,7 +454,7 @@ module.exports = (function() {
this
.
options
.
whereCollection
=
optcpy
.
where
||
null
;
return
this
.
QueryInterface
.
select
(
this
,
[
this
.
getTableName
(),
joinTableName
],
optcpy
,
Utils
.
_
.
defaults
({
type
:
'SELECT'
type
:
QueryTypes
.
SELECT
},
queryOptions
,
{
transaction
:
(
options
||
{}).
transaction
}))
}
...
...
@@ -534,7 +535,7 @@ module.exports = (function() {
return
this
.
QueryInterface
.
select
(
this
,
this
.
getTableName
(),
options
,
Utils
.
_
.
defaults
({
plain
:
true
,
type
:
'SELECT'
,
type
:
QueryTypes
.
SELECT
,
hasJoin
:
hasJoin
},
queryOptions
,
{
transaction
:
(
options
||
{}).
transaction
}))
}
...
...
@@ -927,6 +928,7 @@ module.exports = (function() {
DAOFactory
.
prototype
.
destroy
=
function
(
where
,
options
)
{
options
=
options
||
{}
options
.
force
=
options
.
force
===
undefined
?
false
:
Boolean
(
options
.
force
)
options
.
type
=
QueryTypes
.
BULKDELETE
var
self
=
this
,
query
=
null
...
...
@@ -1050,6 +1052,7 @@ module.exports = (function() {
options
=
options
||
{}
options
.
validate
=
options
.
validate
===
undefined
?
true
:
Boolean
(
options
.
validate
)
options
.
hooks
=
options
.
hooks
===
undefined
?
false
:
Boolean
(
options
.
hooks
)
options
.
type
=
QueryTypes
.
BULKUPDATE
if
(
self
.
options
.
timestamps
)
{
var
attr
=
Utils
.
_
.
underscoredIf
(
self
.
options
.
updatedAt
,
self
.
options
.
underscored
)
...
...
lib/dialects/abstract/query.js
View file @
f14b8d7
...
...
@@ -2,6 +2,7 @@ var Utils = require('../../utils')
,
CustomEventEmitter
=
require
(
"../../emitters/custom-event-emitter"
)
,
Dot
=
require
(
'dottie'
)
,
_
=
require
(
'lodash'
)
,
QueryTypes
=
require
(
'../../query-types'
)
module
.
exports
=
(
function
()
{
var
AbstractQuery
=
function
(
database
,
sequelize
,
callee
,
options
)
{}
...
...
@@ -95,6 +96,8 @@ module.exports = (function() {
}
}
else
if
(
isCallQuery
.
call
(
this
))
{
result
=
data
[
0
]
}
else
if
(
isBulkUpateQuery
.
call
(
this
)
||
isBulkDeleteQuery
.
call
(
this
))
{
result
=
data
.
affectedRows
}
return
result
...
...
@@ -191,7 +194,15 @@ module.exports = (function() {
}
var
isSelectQuery
=
function
()
{
return
this
.
options
.
type
===
'SELECT'
return
this
.
options
.
type
===
QueryTypes
.
SELECT
}
var
isBulkUpateQuery
=
function
()
{
return
this
.
options
.
type
===
QueryTypes
.
BULKUPDATE
}
var
isBulkDeleteQuery
=
function
()
{
return
this
.
options
.
type
===
QueryTypes
.
BULKDELETE
}
var
isUpdateQuery
=
function
()
{
...
...
lib/dialects/postgres/query.js
View file @
f14b8d7
...
...
@@ -2,6 +2,7 @@ var Utils = require("../../utils")
,
AbstractQuery
=
require
(
'../abstract/query'
)
,
DataTypes
=
require
(
'../../data-types'
)
,
hstore
=
require
(
'./hstore'
)
,
QueryTypes
=
require
(
'../../query-types'
)
module
.
exports
=
(
function
()
{
var
Query
=
function
(
client
,
sequelize
,
callee
,
options
)
{
...
...
@@ -41,14 +42,14 @@ module.exports = (function() {
self
.
emit
(
'error'
,
err
,
self
.
callee
)
})
query
.
on
(
'end'
,
function
()
{
query
.
on
(
'end'
,
function
(
result
)
{
self
.
emit
(
'sql'
,
self
.
sql
)
if
(
receivedError
)
{
return
}
onSuccess
.
call
(
self
,
rows
,
sql
)
onSuccess
.
call
(
self
,
rows
,
sql
,
result
)
})
return
this
...
...
@@ -58,7 +59,7 @@ module.exports = (function() {
return
'id'
}
var
onSuccess
=
function
(
rows
,
sql
)
{
var
onSuccess
=
function
(
rows
,
sql
,
result
)
{
var
results
=
rows
,
self
=
this
,
isTableNameQuery
=
(
sql
.
indexOf
(
'SELECT table_name FROM information_schema.tables'
)
===
0
)
...
...
@@ -144,6 +145,8 @@ module.exports = (function() {
}
}
this
.
emit
(
'success'
,
this
.
callee
)
}
else
if
([
QueryTypes
.
BULKUPDATE
,
QueryTypes
.
BULKDELETE
].
indexOf
(
this
.
options
.
type
)
!==
-
1
)
{
this
.
emit
(
'success'
,
result
.
rowCount
)
}
else
if
(
this
.
send
(
'isUpdateQuery'
))
{
if
(
this
.
callee
!==
null
)
{
// may happen for bulk updates
for
(
var
key
in
rows
[
0
])
{
...
...
lib/dialects/sqlite/query.js
View file @
f14b8d7
var
Utils
=
require
(
"../../utils"
)
,
AbstractQuery
=
require
(
'../abstract/query'
)
,
QueryTypes
=
require
(
'../../query-types'
)
module
.
exports
=
(
function
()
{
var
Query
=
function
(
database
,
sequelize
,
callee
,
options
)
{
...
...
@@ -81,7 +82,7 @@ module.exports = (function() {
//private
var
getDatabaseMethod
=
function
()
{
if
(
this
.
send
(
'isInsertQuery'
)
||
this
.
send
(
'isUpdateQuery'
)
||
(
this
.
sql
.
toLowerCase
().
indexOf
(
'CREATE TEMPORARY TABLE'
.
toLowerCase
())
!==
-
1
))
{
if
(
this
.
send
(
'isInsertQuery'
)
||
this
.
send
(
'isUpdateQuery'
)
||
(
this
.
sql
.
toLowerCase
().
indexOf
(
'CREATE TEMPORARY TABLE'
.
toLowerCase
())
!==
-
1
)
||
this
.
options
.
type
===
QueryTypes
.
BULKDELETE
)
{
return
'run'
}
else
{
return
'all'
...
...
@@ -157,6 +158,8 @@ module.exports = (function() {
result
=
results
[
0
]
}
else
if
(
this
.
sql
.
indexOf
(
'PRAGMA foreign_keys'
)
!==
-
1
)
{
result
=
results
}
else
if
([
QueryTypes
.
BULKUPDATE
,
QueryTypes
.
BULKDELETE
].
indexOf
(
this
.
options
.
type
)
!==
-
1
)
{
result
=
metaData
.
changes
}
this
.
emit
(
'success'
,
result
)
...
...
lib/query-interface.js
View file @
f14b8d7
...
...
@@ -2,6 +2,7 @@ var Utils = require(__dirname + '/utils')
,
DataTypes
=
require
(
__dirname
+
'/data-types'
)
,
SQLiteQueryInterface
=
require
(
__dirname
+
'/dialects/sqlite/query-interface'
)
,
Transaction
=
require
(
__dirname
+
'/transaction'
)
,
QueryTypes
=
require
(
'./query-types'
)
module
.
exports
=
(
function
()
{
var
QueryInterface
=
function
(
sequelize
)
{
...
...
@@ -102,7 +103,7 @@ module.exports = (function() {
for
(
i
=
0
;
i
<
keyLen
;
i
++
)
{
if
(
attributes
[
keys
[
i
]].
toString
().
match
(
/^ENUM
\(
/
))
{
sql
=
self
.
QueryGenerator
.
pgListEnums
(
getTableName
,
keys
[
i
],
options
)
chainer
.
add
(
self
.
sequelize
.
query
(
sql
,
null
,
{
plain
:
true
,
raw
:
true
,
type
:
'SELECT'
,
logging
:
options
.
logging
}))
chainer
.
add
(
self
.
sequelize
.
query
(
sql
,
null
,
{
plain
:
true
,
raw
:
true
,
type
:
QueryTypes
.
SELECT
,
logging
:
options
.
logging
}))
}
}
...
...
@@ -535,7 +536,7 @@ module.exports = (function() {
.
success
(
function
(
results
){
emitter
.
query
=
{
sql
:
sql
}
emitter
.
emit
(
'sql'
,
sql
)
emitter
.
emit
(
'success'
,
results
[
1
])
emitter
.
emit
(
'success'
,
results
[
0
])
})
.
error
(
function
(
err
)
{
emitter
.
query
=
{
sql
:
sql
}
...
...
@@ -650,7 +651,7 @@ module.exports = (function() {
.
success
(
function
(
results
){
emitter
.
query
=
{
sql
:
sql
}
emitter
.
emit
(
'sql'
,
sql
)
emitter
.
emit
(
'success'
,
results
[
1
])
emitter
.
emit
(
'success'
,
results
[
0
])
})
.
error
(
function
(
err
)
{
emitter
.
query
=
{
sql
:
sql
}
...
...
@@ -699,7 +700,7 @@ module.exports = (function() {
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
sql
=
self
.
QueryGenerator
.
selectQuery
(
tableName
,
options
)
,
queryOptions
=
Utils
.
_
.
extend
({
transaction
:
options
.
transaction
},
{
plain
:
true
,
raw
:
true
,
type
:
'SELECT'
})
,
queryOptions
=
Utils
.
_
.
extend
({
transaction
:
options
.
transaction
},
{
plain
:
true
,
raw
:
true
,
type
:
QueryTypes
.
SELECT
})
,
query
=
self
.
sequelize
.
query
(
sql
,
null
,
queryOptions
)
query
...
...
lib/query-types.js
0 → 100644
View file @
f14b8d7
module
.
exports
=
{
SELECT
:
'SELECT'
,
BULKUPDATE
:
'BULKUPDATE'
,
BULKDELETE
:
'BULKDELETE'
}
\ No newline at end of file
lib/sequelize.js
View file @
f14b8d7
...
...
@@ -7,6 +7,7 @@ var url = require("url")
,
QueryInterface
=
require
(
"./query-interface"
)
,
Transaction
=
require
(
"./transaction"
)
,
TransactionManager
=
require
(
'./transaction-manager'
)
,
QueryTypes
=
require
(
'./query-types'
)
module
.
exports
=
(
function
()
{
/**
...
...
@@ -129,6 +130,8 @@ module.exports = (function() {
*/
Sequelize
.
Utils
=
Utils
Sequelize
.
QueryTypes
=
QueryTypes
for
(
var
dataType
in
DataTypes
)
{
Sequelize
[
dataType
]
=
DataTypes
[
dataType
]
}
...
...
@@ -312,7 +315,7 @@ module.exports = (function() {
options
=
Utils
.
_
.
extend
(
Utils
.
_
.
clone
(
this
.
options
.
query
),
options
)
options
=
Utils
.
_
.
defaults
(
options
,
{
logging
:
this
.
options
.
hasOwnProperty
(
'logging'
)
?
this
.
options
.
logging
:
console
.
log
,
type
:
(
sql
.
toLowerCase
().
indexOf
(
'select'
)
===
0
)
?
'SELECT'
:
false
type
:
(
sql
.
toLowerCase
().
indexOf
(
'select'
)
===
0
)
?
QueryTypes
.
SELECT
:
false
})
return
this
.
transactionManager
.
query
(
sql
,
callee
,
options
)
...
...
test/dao-factory.test.js
View file @
f14b8d7
...
...
@@ -690,7 +690,6 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
update
({
username
:
'Bill'
},
{
secretValue
:
'42'
}).
done
(
function
(
err
)
{
console
.
log
(
err
)
expect
(
err
).
not
.
to
.
be
.
ok
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
3
)
...
...
@@ -707,6 +706,30 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
it
(
'returns the number of affected rows'
,
function
(
_done
)
{
var
self
=
this
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
},
{
username
:
'Paul'
,
secretValue
:
'42'
},
{
username
:
'Bob'
,
secretValue
:
'43'
}]
,
done
=
_
.
after
(
2
,
_done
)
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
update
({
username
:
'Bill'
},
{
secretValue
:
'42'
}).
done
(
function
(
err
,
affectedRows
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
affectedRows
).
to
.
equal
(
2
)
done
()
})
self
.
User
.
update
({
username
:
'Bill'
},
{
secretValue
:
'44'
}).
done
(
function
(
err
,
affectedRows
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
affectedRows
).
to
.
equal
(
0
)
done
()
})
})
})
})
describe
(
'destroy'
,
function
()
{
...
...
@@ -867,6 +890,31 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
it
(
'returns the number of affected rows'
,
function
(
_done
)
{
var
self
=
this
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
},
{
username
:
'Paul'
,
secretValue
:
'42'
},
{
username
:
'Bob'
,
secretValue
:
'43'
}]
,
done
=
_
.
after
(
2
,
_done
)
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
destroy
({
secretValue
:
'42'
}).
done
(
function
(
err
,
affectedRows
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
affectedRows
).
to
.
equal
(
2
)
done
()
})
self
.
User
.
destroy
({
secretValue
:
'44'
}).
done
(
function
(
err
,
affectedRows
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
affectedRows
).
to
.
equal
(
0
)
done
()
})
})
})
})
describe
(
'equals'
,
function
()
{
...
...
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