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 f10c6929
authored
Apr 26, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(promises): some query interface refactoring
1 parent
d71a2e7b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
68 deletions
lib/dao.js
lib/dialects/sqlite/query-interface.js
lib/query-interface.js
test/query-interface.test.js
lib/dao.js
View file @
f10c692
...
@@ -672,7 +672,7 @@ module.exports = (function() {
...
@@ -672,7 +672,7 @@ module.exports = (function() {
* @param {Integer} [options.by=1] The number to increment by
* @param {Integer} [options.by=1] The number to increment by
* @param {Transaction} [options.transaction=null]
* @param {Transaction} [options.transaction=null]
*
*
* @return {
EventEmitter
}
* @return {
Promise
}
*/
*/
DAO
.
prototype
.
increment
=
function
(
fields
,
countOrOptions
)
{
DAO
.
prototype
.
increment
=
function
(
fields
,
countOrOptions
)
{
Utils
.
validateParameter
(
countOrOptions
,
Object
,
{
Utils
.
validateParameter
(
countOrOptions
,
Object
,
{
...
@@ -737,7 +737,7 @@ module.exports = (function() {
...
@@ -737,7 +737,7 @@ module.exports = (function() {
* @param {Integer} [options.by=1] The number to decrement by
* @param {Integer} [options.by=1] The number to decrement by
* @param {Transaction} [options.transaction=null]
* @param {Transaction} [options.transaction=null]
*
*
* @return {
EventEmitter
}
* @return {
Promise
}
*/
*/
DAO
.
prototype
.
decrement
=
function
(
fields
,
countOrOptions
)
{
DAO
.
prototype
.
decrement
=
function
(
fields
,
countOrOptions
)
{
Utils
.
validateParameter
(
countOrOptions
,
Object
,
{
Utils
.
validateParameter
(
countOrOptions
,
Object
,
{
...
...
lib/dialects/sqlite/query-interface.js
View file @
f10c692
...
@@ -23,17 +23,13 @@ var QueryInterface = module.exports = {
...
@@ -23,17 +23,13 @@ var QueryInterface = module.exports = {
@since 1.6.0
@since 1.6.0
*/
*/
removeColumn
:
function
(
tableName
,
attributeName
,
emitter
,
queryAndEmit
)
{
removeColumn
:
function
(
tableName
,
attributeName
,
emitter
,
queryAndEmit
)
{
this
.
describeTable
(
tableName
).
complete
(
function
(
err
,
fields
)
{
return
this
.
describeTable
(
tableName
).
then
(
function
(
fields
)
{
if
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
}
else
{
delete
fields
[
attributeName
]
delete
fields
[
attributeName
]
var
sql
=
this
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
fields
)
var
sql
=
this
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
fields
)
,
subQueries
=
sql
.
split
(
';'
).
filter
(
function
(
q
)
{
return
q
!==
''
})
,
subQueries
=
sql
.
split
(
';'
).
filter
(
function
(
q
)
{
return
q
!==
''
})
QueryInterface
.
execMultiQuery
.
call
(
this
,
subQueries
,
'removeColumn'
,
emitter
,
queryAndEmit
)
return
QueryInterface
.
execMultiQuery
.
call
(
this
,
subQueries
,
'removeColumn'
,
null
,
queryAndEmit
)
}
}.
bind
(
this
))
}.
bind
(
this
))
},
},
...
@@ -55,17 +51,13 @@ var QueryInterface = module.exports = {
...
@@ -55,17 +51,13 @@ var QueryInterface = module.exports = {
changeColumn
:
function
(
tableName
,
attributes
,
emitter
,
queryAndEmit
)
{
changeColumn
:
function
(
tableName
,
attributes
,
emitter
,
queryAndEmit
)
{
var
attributeName
=
Utils
.
_
.
keys
(
attributes
)[
0
]
var
attributeName
=
Utils
.
_
.
keys
(
attributes
)[
0
]
this
.
describeTable
(
tableName
).
complete
(
function
(
err
,
fields
)
{
return
this
.
describeTable
(
tableName
).
then
(
function
(
fields
)
{
if
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
}
else
{
fields
[
attributeName
]
=
attributes
[
attributeName
]
fields
[
attributeName
]
=
attributes
[
attributeName
]
var
sql
=
this
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
fields
)
var
sql
=
this
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
fields
)
,
subQueries
=
sql
.
split
(
';'
).
filter
(
function
(
q
)
{
return
q
!==
''
})
,
subQueries
=
sql
.
split
(
';'
).
filter
(
function
(
q
)
{
return
q
!==
''
})
QueryInterface
.
execMultiQuery
.
call
(
this
,
subQueries
,
'changeColumn'
,
emitter
,
queryAndEmit
)
return
QueryInterface
.
execMultiQuery
.
call
(
this
,
subQueries
,
'changeColumn'
,
emitter
,
queryAndEmit
)
}
}.
bind
(
this
))
}.
bind
(
this
))
},
},
...
@@ -86,18 +78,14 @@ var QueryInterface = module.exports = {
...
@@ -86,18 +78,14 @@ var QueryInterface = module.exports = {
@since 1.6.0
@since 1.6.0
*/
*/
renameColumn
:
function
(
tableName
,
attrNameBefore
,
attrNameAfter
,
emitter
,
queryAndEmit
)
{
renameColumn
:
function
(
tableName
,
attrNameBefore
,
attrNameAfter
,
emitter
,
queryAndEmit
)
{
this
.
describeTable
(
tableName
).
complete
(
function
(
err
,
fields
)
{
return
this
.
describeTable
(
tableName
).
then
(
function
(
fields
)
{
if
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
}
else
{
fields
[
attrNameAfter
]
=
Utils
.
_
.
clone
(
fields
[
attrNameBefore
])
fields
[
attrNameAfter
]
=
Utils
.
_
.
clone
(
fields
[
attrNameBefore
])
delete
fields
[
attrNameBefore
]
delete
fields
[
attrNameBefore
]
var
sql
=
this
.
QueryGenerator
.
renameColumnQuery
(
tableName
,
attrNameBefore
,
attrNameAfter
,
fields
)
var
sql
=
this
.
QueryGenerator
.
renameColumnQuery
(
tableName
,
attrNameBefore
,
attrNameAfter
,
fields
)
,
subQueries
=
sql
.
split
(
';'
).
filter
(
function
(
q
)
{
return
q
!==
''
})
,
subQueries
=
sql
.
split
(
';'
).
filter
(
function
(
q
)
{
return
q
!==
''
})
QueryInterface
.
execMultiQuery
.
call
(
this
,
subQueries
,
'renameColumn'
,
emitter
,
queryAndEmit
)
return
QueryInterface
.
execMultiQuery
.
call
(
this
,
subQueries
,
'renameColumn'
,
emitter
,
queryAndEmit
)
}
}.
bind
(
this
))
}.
bind
(
this
))
},
},
...
@@ -108,15 +96,10 @@ var QueryInterface = module.exports = {
...
@@ -108,15 +96,10 @@ var QueryInterface = module.exports = {
chainer
.
add
(
this
.
sequelize
,
'query'
,
[
query
+
";"
,
null
,
{
raw
:
true
}])
chainer
.
add
(
this
.
sequelize
,
'query'
,
[
query
+
";"
,
null
,
{
raw
:
true
}])
}.
bind
(
this
))
}.
bind
(
this
))
chainer
return
chainer
.
runSerially
()
.
runSerially
()
.
complete
(
function
(
err
)
{
.
then
(
function
()
{
if
(
err
)
{
return
queryAndEmit
.
call
(
this
,
queries
.
splice
(
queries
.
length
-
1
)[
0
],
methodName
,
{},
emitter
)
emitter
.
emit
(
methodName
,
err
)
emitter
.
emit
(
'error'
,
err
)
}
else
{
queryAndEmit
.
call
(
this
,
queries
.
splice
(
queries
.
length
-
1
)[
0
],
methodName
,
{},
emitter
)
}
}.
bind
(
this
))
}.
bind
(
this
))
},
},
...
...
lib/query-interface.js
View file @
f10c692
...
@@ -288,13 +288,11 @@ module.exports = (function() {
...
@@ -288,13 +288,11 @@ module.exports = (function() {
raw
:
true
raw
:
true
},
options
||
{})
},
options
||
{})
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
showTablesSql
=
self
.
QueryGenerator
.
showTablesQuery
()
var
showTablesSql
=
self
.
QueryGenerator
.
showTablesQuery
()
self
.
sequelize
.
query
(
showTablesSql
,
null
,
options
).
success
(
function
(
tableNames
)
{
return
self
.
sequelize
.
query
(
showTablesSql
,
null
,
options
).
then
(
function
(
tableNames
)
{
emitter
.
emit
(
'success'
,
Utils
.
_
.
flatten
(
tableNames
))
return
Utils
.
_
.
flatten
(
tableNames
)
}).
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]})
})
}).
run
()
}
}
QueryInterface
.
prototype
.
describeTable
=
function
(
tableName
,
options
)
{
QueryInterface
.
prototype
.
describeTable
=
function
(
tableName
,
options
)
{
...
@@ -349,9 +347,7 @@ module.exports = (function() {
...
@@ -349,9 +347,7 @@ module.exports = (function() {
QueryInterface
.
prototype
.
removeColumn
=
function
(
tableName
,
attributeName
)
{
QueryInterface
.
prototype
.
removeColumn
=
function
(
tableName
,
attributeName
)
{
if
(
this
.
sequelize
.
options
.
dialect
===
'sqlite'
)
{
if
(
this
.
sequelize
.
options
.
dialect
===
'sqlite'
)
{
// sqlite needs some special treatment as it cannot drop a column
// sqlite needs some special treatment as it cannot drop a column
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
SQLiteQueryInterface
.
removeColumn
.
call
(
this
,
tableName
,
attributeName
,
null
,
queryAndEmit
)
SQLiteQueryInterface
.
removeColumn
.
call
(
this
,
tableName
,
attributeName
,
emitter
,
queryAndEmit
)
}.
bind
(
this
)).
run
()
}
else
{
}
else
{
var
sql
=
this
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
attributeName
)
var
sql
=
this
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
attributeName
)
return
queryAndEmit
.
call
(
this
,
sql
,
'removeColumn'
)
return
queryAndEmit
.
call
(
this
,
sql
,
'removeColumn'
)
...
@@ -369,9 +365,7 @@ module.exports = (function() {
...
@@ -369,9 +365,7 @@ module.exports = (function() {
if
(
this
.
sequelize
.
options
.
dialect
===
'sqlite'
)
{
if
(
this
.
sequelize
.
options
.
dialect
===
'sqlite'
)
{
// sqlite needs some special treatment as it cannot change a column
// sqlite needs some special treatment as it cannot change a column
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
SQLiteQueryInterface
.
changeColumn
.
call
(
this
,
tableName
,
attributes
,
null
,
queryAndEmit
)
SQLiteQueryInterface
.
changeColumn
.
call
(
this
,
tableName
,
attributes
,
emitter
,
queryAndEmit
)
}.
bind
(
this
)).
run
()
}
else
{
}
else
{
var
options
=
this
.
QueryGenerator
.
attributesToSQL
(
attributes
)
var
options
=
this
.
QueryGenerator
.
attributesToSQL
(
attributes
)
,
sql
=
this
.
QueryGenerator
.
changeColumnQuery
(
tableName
,
options
)
,
sql
=
this
.
QueryGenerator
.
changeColumnQuery
(
tableName
,
options
)
...
@@ -381,8 +375,7 @@ module.exports = (function() {
...
@@ -381,8 +375,7 @@ module.exports = (function() {
}
}
QueryInterface
.
prototype
.
renameColumn
=
function
(
tableName
,
attrNameBefore
,
attrNameAfter
)
{
QueryInterface
.
prototype
.
renameColumn
=
function
(
tableName
,
attrNameBefore
,
attrNameAfter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
this
.
describeTable
(
tableName
).
then
(
function
(
data
)
{
this
.
describeTable
(
tableName
).
success
(
function
(
data
)
{
data
=
data
[
attrNameBefore
]
||
{}
data
=
data
[
attrNameBefore
]
||
{}
var
options
=
{}
var
options
=
{}
...
@@ -401,20 +394,15 @@ module.exports = (function() {
...
@@ -401,20 +394,15 @@ module.exports = (function() {
if
(
this
.
sequelize
.
options
.
dialect
===
'sqlite'
)
{
if
(
this
.
sequelize
.
options
.
dialect
===
'sqlite'
)
{
// sqlite needs some special treatment as it cannot rename a column
// sqlite needs some special treatment as it cannot rename a column
SQLiteQueryInterface
.
renameColumn
.
call
(
this
,
tableName
,
attrNameBefore
,
attrNameAfter
,
emitter
,
queryAndEmit
)
return
SQLiteQueryInterface
.
renameColumn
.
call
(
this
,
tableName
,
attrNameBefore
,
attrNameAfter
,
null
,
queryAndEmit
)
}
else
{
}
else
{
var
sql
=
this
.
QueryGenerator
.
renameColumnQuery
(
tableName
,
var
sql
=
this
.
QueryGenerator
.
renameColumnQuery
(
tableName
,
attrNameBefore
,
attrNameBefore
,
this
.
QueryGenerator
.
attributesToSQL
(
options
)
this
.
QueryGenerator
.
attributesToSQL
(
options
)
)
)
queryAndEmit
.
call
(
this
,
sql
,
'renameColumn'
,
{},
emitter
)
return
queryAndEmit
.
call
(
this
,
sql
,
'renameColumn'
,
{}
)
}
}
}.
bind
(
this
))
}.
bind
(
this
));
.
error
(
function
(
err
)
{
this
.
emit
(
'renameColumn'
,
err
)
emitter
.
emit
(
'error'
,
err
)
}.
bind
(
this
))
}.
bind
(
this
)).
run
()
}
}
QueryInterface
.
prototype
.
addIndex
=
function
(
tableName
,
attributes
,
options
)
{
QueryInterface
.
prototype
.
addIndex
=
function
(
tableName
,
attributes
,
options
)
{
...
...
test/query-interface.test.js
View file @
f10c692
...
@@ -153,40 +153,34 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
...
@@ -153,40 +153,34 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
})
})
describe
(
'createTable'
,
function
()
{
describe
(
'createTable'
,
function
()
{
it
(
'should work with enums (1)'
,
function
(
done
)
{
it
(
'should work with enums (1)'
,
function
()
{
this
.
queryInterface
.
createTable
(
'SomeTable'
,
{
return
this
.
queryInterface
.
createTable
(
'SomeTable'
,
{
someEnum
:
DataTypes
.
ENUM
(
'value1'
,
'value2'
,
'value3'
)
someEnum
:
DataTypes
.
ENUM
(
'value1'
,
'value2'
,
'value3'
)
}).
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
done
()
})
})
})
})
it
(
'should work with enums (2)'
,
function
(
done
)
{
it
(
'should work with enums (2)'
,
function
()
{
this
.
queryInterface
.
createTable
(
'SomeTable'
,
{
return
this
.
queryInterface
.
createTable
(
'SomeTable'
,
{
someEnum
:
{
someEnum
:
{
type
:
DataTypes
.
ENUM
,
type
:
DataTypes
.
ENUM
,
values
:
[
'value1'
,
'value2'
,
'value3'
]
values
:
[
'value1'
,
'value2'
,
'value3'
]
}
}
}).
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
done
()
})
})
})
})
})
})
describe
(
'renameColumn'
,
function
()
{
describe
(
'renameColumn'
,
function
()
{
it
(
'rename a simple column'
,
function
(
done
)
{
it
(
'rename a simple column'
,
function
()
{
var
self
=
this
var
self
=
this
var
Users
=
self
.
sequelize
.
define
(
'_Users'
,
{
var
Users
=
self
.
sequelize
.
define
(
'_Users'
,
{
username
:
DataTypes
.
STRING
username
:
DataTypes
.
STRING
},
{
freezeTableName
:
true
})
},
{
freezeTableName
:
true
})
Users
.
sync
({
force
:
true
}).
success
(
function
()
{
return
Users
.
sync
({
force
:
true
}).
then
(
function
()
{
self
.
queryInterface
.
renameColumn
(
'_Users'
,
'username'
,
'pseudo'
).
complete
(
done
)
return
self
.
queryInterface
.
renameColumn
(
'_Users'
,
'username'
,
'pseudo'
)
})
})
})
})
it
(
'rename a column non-null without default value'
,
function
(
done
)
{
it
(
'rename a column non-null without default value'
,
function
()
{
var
self
=
this
var
self
=
this
var
Users
=
self
.
sequelize
.
define
(
'_Users'
,
{
var
Users
=
self
.
sequelize
.
define
(
'_Users'
,
{
username
:
{
username
:
{
...
@@ -195,11 +189,11 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
...
@@ -195,11 +189,11 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
}
}
},
{
freezeTableName
:
true
})
},
{
freezeTableName
:
true
})
Users
.
sync
({
force
:
true
}).
success
(
function
()
{
return
Users
.
sync
({
force
:
true
}).
then
(
function
()
{
self
.
queryInterface
.
renameColumn
(
'_Users'
,
'username'
,
'pseudo'
).
complete
(
done
)
return
self
.
queryInterface
.
renameColumn
(
'_Users'
,
'username'
,
'pseudo'
)
})
})
})
})
it
(
'rename a boolean column non-null without default value'
,
function
(
done
)
{
it
(
'rename a boolean column non-null without default value'
,
function
()
{
var
self
=
this
var
self
=
this
var
Users
=
self
.
sequelize
.
define
(
'_Users'
,
{
var
Users
=
self
.
sequelize
.
define
(
'_Users'
,
{
active
:
{
active
:
{
...
@@ -209,8 +203,8 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
...
@@ -209,8 +203,8 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
}
}
},
{
freezeTableName
:
true
})
},
{
freezeTableName
:
true
})
Users
.
sync
({
force
:
true
}).
success
(
function
()
{
return
Users
.
sync
({
force
:
true
}).
then
(
function
()
{
self
.
queryInterface
.
renameColumn
(
'_Users'
,
'active'
,
'enabled'
).
complete
(
done
)
return
self
.
queryInterface
.
renameColumn
(
'_Users'
,
'active'
,
'enabled'
)
})
})
})
})
})
})
...
...
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