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 8a8366b4
authored
Oct 09, 2018
by
Fernando Fabricio dos Santos
Committed by
Sushant
Oct 09, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(query-generator): add startsWith, endsWith and substring operators (#9935) (#9999)
1 parent
8ac97efa
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
50 additions
and
4 deletions
docs/querying.md
lib/dialects/abstract/query-generator.js
lib/dialects/abstract/query-generator/operators.js
lib/operators.js
test/unit/sql/where.test.js
docs/querying.md
View file @
8a8366b
...
...
@@ -156,6 +156,9 @@ const Op = Sequelize.Op
[
Op
.
notLike
]:
'%hat'
// NOT LIKE '%hat'
[
Op
.
iLike
]:
'%hat'
// ILIKE '%hat' (case insensitive) (PG only)
[
Op
.
notILike
]:
'%hat'
// NOT ILIKE '%hat' (PG only)
[
Op
.
startsWith
]:
'hat'
// LIKE '%hat'
[
Op
.
endsWith
]:
'hat'
// LIKE 'hat%'
[
Op
.
substring
]:
'hat'
// LIKE '%hat%'
[
Op
.
regexp
]:
'^[h|a|t]'
// REGEXP/~ '^[h|a|t]' (MySQL/PG only)
[
Op
.
notRegexp
]:
'^[h|a|t]'
// NOT REGEXP/!~ '^[h|a|t]' (MySQL/PG only)
[
Op
.
iRegexp
]:
'^[h|a|t]'
// ~* '^[h|a|t]' (PG only)
...
...
lib/dialects/abstract/query-generator.js
View file @
8a8366b
...
...
@@ -2428,6 +2428,15 @@ class QueryGenerator {
}
return
this
.
_joinKeyValue
(
key
,
value
.
map
(
identifier
=>
this
.
quoteIdentifier
(
identifier
)).
join
(
'.'
),
comparator
,
options
.
prefix
);
case
Op
.
startsWith
:
comparator
=
this
.
OperatorMap
[
Op
.
like
];
return
this
.
_joinKeyValue
(
key
,
this
.
escape
(
`%
${
value
}
`
),
comparator
,
options
.
prefix
);
case
Op
.
endsWith
:
comparator
=
this
.
OperatorMap
[
Op
.
like
];
return
this
.
_joinKeyValue
(
key
,
this
.
escape
(
`
${
value
}
%`
),
comparator
,
options
.
prefix
);
case
Op
.
substring
:
comparator
=
this
.
OperatorMap
[
Op
.
like
];
return
this
.
_joinKeyValue
(
key
,
this
.
escape
(
`%
${
value
}
%`
),
comparator
,
options
.
prefix
);
}
const
escapeOptions
=
{
...
...
lib/dialects/abstract/query-generator/operators.js
View file @
8a8366b
...
...
@@ -20,6 +20,9 @@ const OperatorHelpers = {
[
Op
.
notLike
]:
'NOT LIKE'
,
[
Op
.
iLike
]:
'ILIKE'
,
[
Op
.
notILike
]:
'NOT ILIKE'
,
[
Op
.
startsWith
]:
'LIKE'
,
[
Op
.
endsWith
]:
'LIKE'
,
[
Op
.
substring
]:
'LIKE'
,
[
Op
.
regexp
]:
'~'
,
[
Op
.
notRegexp
]:
'!~'
,
[
Op
.
iRegexp
]:
'~*'
,
...
...
@@ -78,4 +81,4 @@ const OperatorHelpers = {
}
};
module
.
exports
=
OperatorHelpers
;
\ No newline at end of file
module
.
exports
=
OperatorHelpers
;
lib/operators.js
View file @
8a8366b
...
...
@@ -19,6 +19,9 @@
* @property notLike
* @property iLike
* @property notILike
* @property startsWith
* @property endsWith
* @property substring
* @property regexp
* @property notRegexp
* @property iRegexp
...
...
@@ -57,6 +60,9 @@ const Op = {
notLike
:
Symbol
.
for
(
'notLike'
),
iLike
:
Symbol
.
for
(
'iLike'
),
notILike
:
Symbol
.
for
(
'notILike'
),
startsWith
:
Symbol
.
for
(
'startsWith'
),
endsWith
:
Symbol
.
for
(
'endsWith'
),
substring
:
Symbol
.
for
(
'substring'
),
regexp
:
Symbol
.
for
(
'regexp'
),
notRegexp
:
Symbol
.
for
(
'notRegexp'
),
iRegexp
:
Symbol
.
for
(
'iRegexp'
),
...
...
@@ -81,4 +87,4 @@ const Op = {
join
:
Symbol
.
for
(
'join'
)
};
module
.
exports
=
Op
;
\ No newline at end of file
module
.
exports
=
Op
;
test/unit/sql/where.test.js
View file @
8a8366b
...
...
@@ -432,6 +432,33 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
});
});
suite
(
'$startsWith'
,
()
=>
{
testsql
(
'username'
,
{
[
Op
.
startsWith
]:
'swagger'
},
{
default
:
"[username] LIKE '%swagger'"
,
mssql
:
"[username] LIKE N'%swagger'"
});
});
suite
(
'$endsWith'
,
()
=>
{
testsql
(
'username'
,
{
[
Op
.
endsWith
]:
'swagger'
},
{
default
:
"[username] LIKE 'swagger%'"
,
mssql
:
"[username] LIKE N'swagger%'"
});
});
suite
(
'$substring'
,
()
=>
{
testsql
(
'username'
,
{
[
Op
.
substring
]:
'swagger'
},
{
default
:
"[username] LIKE '%swagger%'"
,
mssql
:
"[username] LIKE N'%swagger%'"
});
});
suite
(
'$between'
,
()
=>
{
testsql
(
'date'
,
{
[
Op
.
between
]:
[
'2013-01-01'
,
'2013-01-11'
]
...
...
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