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 60c04d97
authored
Feb 25, 2018
by
Kirill
Committed by
Sushant
Feb 25, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(query): add ability to pass arrays as replacements (#9050) (#9054)
1 parent
222b13fd
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
1 deletions
lib/sql-string.js
test/integration/sequelize.test.js
test/unit/dialects/abstract/query-generator.test.js
lib/sql-string.js
View file @
60c04d9
...
...
@@ -4,6 +4,21 @@ const dataTypes = require('./data-types');
const
util
=
require
(
'util'
);
const
_
=
require
(
'lodash'
);
function
arrayToList
(
array
,
timeZone
,
dialect
,
format
)
{
return
array
.
reduce
((
sql
,
val
,
i
)
=>
{
if
(
i
!==
0
)
{
sql
+=
', '
;
}
if
(
Array
.
isArray
(
val
))
{
sql
+=
`(
${
arrayToList
(
val
,
timeZone
,
dialect
,
format
)}
)`
;
}
else
{
sql
+=
escape
(
val
,
timeZone
,
dialect
,
format
);
}
return
sql
;
},
''
);
}
exports
.
arrayToList
=
arrayToList
;
function
escape
(
val
,
timeZone
,
dialect
,
format
)
{
let
prependN
=
false
;
if
(
val
===
undefined
||
val
===
null
)
{
...
...
@@ -44,7 +59,7 @@ function escape(val, timeZone, dialect, format) {
if
(
dialect
===
'postgres'
&&
!
format
)
{
return
dataTypes
.
ARRAY
.
prototype
.
stringify
(
val
,
{
escape
:
partialEscape
});
}
return
val
.
map
(
partialEscape
);
return
arrayToList
(
val
,
timeZone
,
dialect
,
format
);
}
if
(
!
val
.
replace
)
{
...
...
test/integration/sequelize.test.js
View file @
60c04d9
...
...
@@ -273,6 +273,23 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
return
this
.
sequelize
.
query
(
this
.
insertQuery
);
});
it
(
'executes a query if a placeholder value is an array'
,
function
()
{
return
this
.
sequelize
.
query
(
`INSERT INTO
${
qq
(
this
.
User
.
tableName
)}
(username, email_address, `
+
`
${
qq
(
'createdAt'
)}
,
${
qq
(
'updatedAt'
)}
) VALUES ?;`
,
{
replacements
:
[[
[
'john'
,
'john@gmail.com'
,
'2012-01-01 10:10:10'
,
'2012-01-01 10:10:10'
],
[
'michael'
,
'michael@gmail.com'
,
'2012-01-01 10:10:10'
,
'2012-01-01 10:10:10'
]
]]
})
.
then
(()
=>
this
.
sequelize
.
query
(
`SELECT * FROM
${
qq
(
this
.
User
.
tableName
)}
;`
,
{
type
:
this
.
sequelize
.
QueryTypes
.
SELECT
}))
.
then
(
rows
=>
{
expect
(
rows
).
to
.
be
.
lengthOf
(
2
);
expect
(
rows
[
0
].
username
).
to
.
be
.
equal
(
'john'
);
expect
(
rows
[
1
].
username
).
to
.
be
.
equal
(
'michael'
);
});
});
describe
(
'logging'
,
()
=>
{
it
(
'executes a query with global benchmarking option and default logger'
,
()
=>
{
...
...
test/unit/dialects/abstract/query-generator.test.js
View file @
60c04d9
...
...
@@ -6,6 +6,15 @@ const chai = require('chai'),
getAbstractQueryGenerator
=
require
(
__dirname
+
'/../../support'
).
getAbstractQueryGenerator
;
describe
(
'QueryGenerator'
,
()
=>
{
describe
(
'selectQuery'
,
()
=>
{
it
(
'should generate correct query using array placeholder'
,
function
()
{
const
QG
=
getAbstractQueryGenerator
(
this
.
sequelize
);
QG
.
selectQuery
(
'foo'
,
{
where
:
{
bar
:
{[
Op
.
like
]:
{[
Op
.
any
]:
[
'a'
,
'b'
]}}}})
.
should
.
be
.
equal
(
'SELECT * FROM foo WHERE foo.bar LIKE ANY (\'a\', \'b\');'
);
});
});
describe
(
'whereItemQuery'
,
()
=>
{
it
(
'should generate correct query for Symbol operators'
,
function
()
{
const
QG
=
getAbstractQueryGenerator
(
this
.
sequelize
);
...
...
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