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