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 b4fd4642
authored
Dec 11, 2017
by
Martin Gutfeldt
Committed by
Sushant
Dec 11, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(postgres): returning option for arithmetic queries (#8727) (#8748)
1 parent
438baac0
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
32 additions
and
5 deletions
docs/instances.md
lib/dialects/abstract/query-generator.js
lib/model.js
test/integration/instance.test.js
test/unit/dialects/postgres/query-generator.test.js
docs/instances.md
View file @
b4fd464
...
@@ -300,7 +300,10 @@ First of all you can define a field and the value you want to add to it.
...
@@ -300,7 +300,10 @@ First of all you can define a field and the value you want to add to it.
```
js
```
js
User
.
findById
(
1
).
then
(
user
=>
{
User
.
findById
(
1
).
then
(
user
=>
{
return
user
.
increment
(
'my-integer-field'
,
{
by
:
2
})
return
user
.
increment
(
'my-integer-field'
,
{
by
:
2
})
}).
then
(
/* ... */
)
}).
then
(
user
=>
{
// Postgres will return the updated user by default (unless disabled by setting { returning: false })
// In other dialects, you'll want to call user.reload() to get the updated instance...
})
```
```
Second
,
you can define multiple fields and the value you want to add to them
.
Second
,
you can define multiple fields and the value you want to add to them
.
...
@@ -331,7 +334,10 @@ First of all you can define a field and the value you want to add to it.
...
@@ -331,7 +334,10 @@ First of all you can define a field and the value you want to add to it.
```
js
```
js
User
.
findById
(
1
).
then
(
user
=>
{
User
.
findById
(
1
).
then
(
user
=>
{
return
user
.
decrement
(
'my-integer-field'
,
{
by
:
2
})
return
user
.
decrement
(
'my-integer-field'
,
{
by
:
2
})
}).
then
(
/* ... */
)
}).
then
(
user
=>
{
// Postgres will return the updated user by default (unless disabled by setting { returning: false })
// In other dialects, you'll want to call user.reload() to get the updated instance...
})
```
```
Second
,
you can define multiple fields and the value you want to add to them
.
Second
,
you can define multiple fields and the value you want to add to them
.
...
...
lib/dialects/abstract/query-generator.js
View file @
b4fd464
...
@@ -388,13 +388,16 @@ const QueryGenerator = {
...
@@ -388,13 +388,16 @@ const QueryGenerator = {
@private
@private
*/
*/
arithmeticQuery
(
operator
,
tableName
,
attrValueHash
,
where
,
options
,
attributes
)
{
arithmeticQuery
(
operator
,
tableName
,
attrValueHash
,
where
,
options
,
attributes
)
{
options
=
options
||
{};
_
.
defaults
(
options
,
{
returning
:
true
});
attrValueHash
=
Utils
.
removeNullValuesFromHash
(
attrValueHash
,
this
.
options
.
omitNull
);
attrValueHash
=
Utils
.
removeNullValuesFromHash
(
attrValueHash
,
this
.
options
.
omitNull
);
const
values
=
[];
const
values
=
[];
let
query
=
'UPDATE <%= table %> SET <%= values %><%= output %> <%= where %>'
;
let
query
=
'UPDATE <%= table %> SET <%= values %><%= output %> <%= where %>'
;
let
outputFragment
;
let
outputFragment
;
if
(
this
.
_dialect
.
supports
.
returnValues
)
{
if
(
this
.
_dialect
.
supports
.
returnValues
&&
options
.
returning
)
{
if
(
this
.
_dialect
.
supports
.
returnValues
.
returning
)
{
if
(
this
.
_dialect
.
supports
.
returnValues
.
returning
)
{
options
.
mapToModel
=
true
;
options
.
mapToModel
=
true
;
query
+=
' RETURNING *'
;
query
+=
' RETURNING *'
;
...
...
lib/model.js
View file @
b4fd464
...
@@ -3904,7 +3904,7 @@ class Model {
...
@@ -3904,7 +3904,7 @@ class Model {
* ```sql
* ```sql
* SET column = column + X
* SET column = column + X
* ```
* ```
* query. T
o get the correct value after an increment into the Instance you should do a reload
.
* query. T
he updated instance will be returned by default in Postgres. However, in other dialects, you will need to do a reload to get the new values
.
*
*
*```js
*```js
* instance.increment('number') // increment number by 1
* instance.increment('number') // increment number by 1
...
@@ -3921,6 +3921,7 @@ class Model {
...
@@ -3921,6 +3921,7 @@ class Model {
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
* @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
* @param {Boolean} [options.returning=true] Append RETURNING * to get back auto generated values (Postgres only)
*
*
* @return {Promise<this>}
* @return {Promise<this>}
* @since 4.0.0
* @since 4.0.0
...
@@ -3940,7 +3941,7 @@ class Model {
...
@@ -3940,7 +3941,7 @@ class Model {
* ```sql
* ```sql
* SET column = column - X
* SET column = column - X
* ```
* ```
* query. T
o get the correct value after an decrement into the Instance you should do a reload
.
* query. T
he updated instance will be returned by default in Postgres. However, in other dialects, you will need to do a reload to get the new values
.
*
*
* ```js
* ```js
* instance.decrement('number') // decrement number by 1
* instance.decrement('number') // decrement number by 1
...
@@ -3957,6 +3958,7 @@ class Model {
...
@@ -3957,6 +3958,7 @@ class Model {
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
* @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
* @param {Boolean} [options.returning=true] Append RETURNING * to get back auto generated values (Postgres only)
*
*
* @return {Promise}
* @return {Promise}
*/
*/
...
...
test/integration/instance.test.js
View file @
b4fd464
...
@@ -156,6 +156,9 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
...
@@ -156,6 +156,9 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
return
this
.
User
.
findById
(
1
).
then
(
user1
=>
{
return
this
.
User
.
findById
(
1
).
then
(
user1
=>
{
return
user1
.
increment
(
'aNumber'
,
{
by
:
2
}).
then
(()
=>
{
return
user1
.
increment
(
'aNumber'
,
{
by
:
2
}).
then
(()
=>
{
expect
(
user1
.
aNumber
).
to
.
be
.
equal
(
2
);
expect
(
user1
.
aNumber
).
to
.
be
.
equal
(
2
);
return
user1
.
increment
(
'bNumber'
,
{
by
:
2
,
returning
:
false
}).
then
(
user3
=>
{
expect
(
user3
.
bNumber
).
to
.
be
.
equal
(
0
);
});
});
});
});
});
});
});
...
@@ -323,6 +326,9 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
...
@@ -323,6 +326,9 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
return
this
.
User
.
findById
(
1
).
then
(
user1
=>
{
return
this
.
User
.
findById
(
1
).
then
(
user1
=>
{
return
user1
.
decrement
(
'aNumber'
,
{
by
:
2
}).
then
(()
=>
{
return
user1
.
decrement
(
'aNumber'
,
{
by
:
2
}).
then
(()
=>
{
expect
(
user1
.
aNumber
).
to
.
be
.
equal
(
-
2
);
expect
(
user1
.
aNumber
).
to
.
be
.
equal
(
-
2
);
return
user1
.
decrement
(
'bNumber'
,
{
by
:
2
,
returning
:
false
}).
then
(
user3
=>
{
expect
(
user3
.
bNumber
).
to
.
be
.
equal
(
0
);
});
});
});
});
});
});
});
...
...
test/unit/dialects/postgres/query-generator.test.js
View file @
b4fd464
...
@@ -26,6 +26,11 @@ if (dialect.match(/^postgres/)) {
...
@@ -26,6 +26,11 @@ if (dialect.match(/^postgres/)) {
expectation
:
'UPDATE "myTable" SET "foo"="foo"+ \'bar\' WHERE "bar" = \'biz\' RETURNING *'
expectation
:
'UPDATE "myTable" SET "foo"="foo"+ \'bar\' WHERE "bar" = \'biz\' RETURNING *'
},
},
{
{
title
:
'Should use the plus operator without returning clause'
,
arguments
:
[
'+'
,
'myTable'
,
{
foo
:
'bar'
},
{},
{
returning
:
false
}],
expectation
:
'UPDATE "myTable" SET "foo"="foo"+ \'bar\' '
},
{
title
:
'Should use the minus operator'
,
title
:
'Should use the minus operator'
,
arguments
:
[
'-'
,
'myTable'
,
{
foo
:
'bar'
},
{},
{}],
arguments
:
[
'-'
,
'myTable'
,
{
foo
:
'bar'
},
{},
{}],
expectation
:
'UPDATE "myTable" SET "foo"="foo"- \'bar\' RETURNING *'
expectation
:
'UPDATE "myTable" SET "foo"="foo"- \'bar\' RETURNING *'
...
@@ -39,6 +44,11 @@ if (dialect.match(/^postgres/)) {
...
@@ -39,6 +44,11 @@ if (dialect.match(/^postgres/)) {
title
:
'Should use the minus operator with where clause'
,
title
:
'Should use the minus operator with where clause'
,
arguments
:
[
'-'
,
'myTable'
,
{
foo
:
'bar'
},
{
bar
:
'biz'
},
{}],
arguments
:
[
'-'
,
'myTable'
,
{
foo
:
'bar'
},
{
bar
:
'biz'
},
{}],
expectation
:
'UPDATE "myTable" SET "foo"="foo"- \'bar\' WHERE "bar" = \'biz\' RETURNING *'
expectation
:
'UPDATE "myTable" SET "foo"="foo"- \'bar\' WHERE "bar" = \'biz\' RETURNING *'
},
{
title
:
'Should use the minus operator without returning clause'
,
arguments
:
[
'-'
,
'myTable'
,
{
foo
:
'bar'
},
{},
{
returning
:
false
}],
expectation
:
'UPDATE "myTable" SET "foo"="foo"- \'bar\' '
}
}
],
],
attributesToSQL
:
[
attributesToSQL
:
[
...
...
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