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 5e857245
authored
Feb 20, 2020
by
Colin Cheng
Committed by
GitHub
Feb 20, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(index): improve to support multiple fields with operator (#11934)
1 parent
41aba1fd
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
80 additions
and
8 deletions
lib/dialects/abstract/index.js
lib/dialects/abstract/query-generator.js
lib/dialects/postgres/index.js
test/unit/sql/index.test.js
types/lib/query-interface.d.ts
lib/dialects/abstract/index.js
View file @
5e85724
...
@@ -59,7 +59,8 @@ AbstractDialect.prototype.supports = {
...
@@ -59,7 +59,8 @@ AbstractDialect.prototype.supports = {
concurrently
:
false
,
concurrently
:
false
,
type
:
false
,
type
:
false
,
using
:
true
,
using
:
true
,
functionBased
:
false
functionBased
:
false
,
operator
:
false
},
},
joinTableDependent
:
true
,
joinTableDependent
:
true
,
groupedLimit
:
true
,
groupedLimit
:
true
,
...
...
lib/dialects/abstract/query-generator.js
View file @
5e85724
...
@@ -505,12 +505,14 @@ class QueryGenerator {
...
@@ -505,12 +505,14 @@ class QueryGenerator {
}
}
const
fieldsSql
=
options
.
fields
.
map
(
field
=>
{
const
fieldsSql
=
options
.
fields
.
map
(
field
=>
{
if
(
typeof
field
===
'string'
)
{
return
this
.
quoteIdentifier
(
field
);
}
if
(
field
instanceof
Utils
.
SequelizeMethod
)
{
if
(
field
instanceof
Utils
.
SequelizeMethod
)
{
return
this
.
handleSequelizeMethod
(
field
);
return
this
.
handleSequelizeMethod
(
field
);
}
}
if
(
typeof
field
===
'string'
)
{
field
=
{
name
:
field
};
}
let
result
=
''
;
let
result
=
''
;
if
(
field
.
attribute
)
{
if
(
field
.
attribute
)
{
...
@@ -527,6 +529,13 @@ class QueryGenerator {
...
@@ -527,6 +529,13 @@ class QueryGenerator {
result
+=
` COLLATE
${
this
.
quoteIdentifier
(
field
.
collate
)}
`
;
result
+=
` COLLATE
${
this
.
quoteIdentifier
(
field
.
collate
)}
`
;
}
}
if
(
this
.
_dialect
.
supports
.
index
.
operator
)
{
const
operator
=
field
.
operator
||
options
.
operator
;
if
(
operator
)
{
result
+=
`
${
operator
}
`
;
}
}
if
(
this
.
_dialect
.
supports
.
index
.
length
&&
field
.
length
)
{
if
(
this
.
_dialect
.
supports
.
index
.
length
&&
field
.
length
)
{
result
+=
`(
${
field
.
length
}
)`
;
result
+=
`(
${
field
.
length
}
)`
;
}
}
...
@@ -581,7 +590,7 @@ class QueryGenerator {
...
@@ -581,7 +590,7 @@ class QueryGenerator {
this
.
_dialect
.
supports
.
index
.
using
===
1
&&
options
.
using
?
`USING
${
options
.
using
}
`
:
''
,
this
.
_dialect
.
supports
.
index
.
using
===
1
&&
options
.
using
?
`USING
${
options
.
using
}
`
:
''
,
!
this
.
_dialect
.
supports
.
indexViaAlter
?
`ON
${
tableName
}
`
:
undefined
,
!
this
.
_dialect
.
supports
.
indexViaAlter
?
`ON
${
tableName
}
`
:
undefined
,
this
.
_dialect
.
supports
.
index
.
using
===
2
&&
options
.
using
?
`USING
${
options
.
using
}
`
:
''
,
this
.
_dialect
.
supports
.
index
.
using
===
2
&&
options
.
using
?
`USING
${
options
.
using
}
`
:
''
,
`(
${
fieldsSql
.
join
(
', '
)}
${
options
.
operator
?
`
${
options
.
operator
}
`
:
''
}
)`
,
`(
${
fieldsSql
.
join
(
', '
)}
)`
,
this
.
_dialect
.
supports
.
index
.
parser
&&
options
.
parser
?
`WITH PARSER
${
options
.
parser
}
`
:
undefined
,
this
.
_dialect
.
supports
.
index
.
parser
&&
options
.
parser
?
`WITH PARSER
${
options
.
parser
}
`
:
undefined
,
this
.
_dialect
.
supports
.
index
.
where
&&
options
.
where
?
options
.
where
:
undefined
this
.
_dialect
.
supports
.
index
.
where
&&
options
.
where
?
options
.
where
:
undefined
);
);
...
...
lib/dialects/postgres/index.js
View file @
5e85724
...
@@ -39,7 +39,8 @@ PostgresDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototy
...
@@ -39,7 +39,8 @@ PostgresDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototy
concurrently
:
true
,
concurrently
:
true
,
using
:
2
,
using
:
2
,
where
:
true
,
where
:
true
,
functionBased
:
true
functionBased
:
true
,
operator
:
true
},
},
inserts
:
{
inserts
:
{
onConflictDoNothing
:
' ON CONFLICT DO NOTHING'
,
onConflictDoNothing
:
' ON CONFLICT DO NOTHING'
,
...
...
test/unit/sql/index.test.js
View file @
5e85724
...
@@ -171,6 +171,67 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
...
@@ -171,6 +171,67 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});
});
});
}
}
if
(
current
.
dialect
.
supports
.
index
.
operator
)
{
it
(
'operator with multiple fields'
,
()
=>
{
expectsql
(
sql
.
addIndexQuery
(
'table'
,
{
fields
:
[
'column1'
,
'column2'
],
using
:
'gist'
,
operator
:
'inet_ops'
}),
{
postgres
:
'CREATE INDEX "table_column1_column2" ON "table" USING gist ("column1" inet_ops, "column2" inet_ops)'
});
});
it
(
'operator in fields'
,
()
=>
{
expectsql
(
sql
.
addIndexQuery
(
'table'
,
{
fields
:
[{
name
:
'column'
,
operator
:
'inet_ops'
}],
using
:
'gist'
}),
{
postgres
:
'CREATE INDEX "table_column" ON "table" USING gist ("column" inet_ops)'
});
});
it
(
'operator in fields with order'
,
()
=>
{
expectsql
(
sql
.
addIndexQuery
(
'table'
,
{
fields
:
[{
name
:
'column'
,
order
:
'DESC'
,
operator
:
'inet_ops'
}],
using
:
'gist'
}),
{
postgres
:
'CREATE INDEX "table_column" ON "table" USING gist ("column" inet_ops DESC)'
});
});
it
(
'operator in multiple fields #1'
,
()
=>
{
expectsql
(
sql
.
addIndexQuery
(
'table'
,
{
fields
:
[{
name
:
'column1'
,
order
:
'DESC'
,
operator
:
'inet_ops'
},
'column2'
],
using
:
'gist'
}),
{
postgres
:
'CREATE INDEX "table_column1_column2" ON "table" USING gist ("column1" inet_ops DESC, "column2")'
});
});
it
(
'operator in multiple fields #2'
,
()
=>
{
expectsql
(
sql
.
addIndexQuery
(
'table'
,
{
fields
:
[{
name
:
'path'
,
operator
:
'text_pattern_ops'
},
'level'
,
{
name
:
'name'
,
operator
:
'varchar_pattern_ops'
}],
using
:
'btree'
}),
{
postgres
:
'CREATE INDEX "table_path_level_name" ON "table" USING btree ("path" text_pattern_ops, "level", "name" varchar_pattern_ops)'
});
});
}
});
});
describe
(
'removeIndex'
,
()
=>
{
describe
(
'removeIndex'
,
()
=>
{
...
...
types/lib/query-interface.d.ts
View file @
5e85724
...
@@ -171,9 +171,9 @@ export interface IndexesOptions {
...
@@ -171,9 +171,9 @@ export interface IndexesOptions {
* An array of the fields to index. Each field can either be a string containing the name of the field,
* An array of the fields to index. Each field can either be a string containing the name of the field,
* a sequelize object (e.g `sequelize.fn`), or an object with the following attributes: `name`
* a sequelize object (e.g `sequelize.fn`), or an object with the following attributes: `name`
* (field name), `length` (create a prefix index of length chars), `order` (the direction the column
* (field name), `length` (create a prefix index of length chars), `order` (the direction the column
* should be sorted in), `collate` (the collation (sort order) for the column)
* should be sorted in), `collate` (the collation (sort order) for the column)
, `operator` (likes IndexesOptions['operator'])
*/
*/
fields
?:
(
string
|
{
name
:
string
;
length
?:
number
;
order
?:
'ASC'
|
'DESC'
;
collate
?:
string
})[];
fields
?:
(
string
|
{
name
:
string
;
length
?:
number
;
order
?:
'ASC'
|
'DESC'
;
collate
?:
string
;
operator
?:
string
})[];
/**
/**
* The method to create the index by (`USING` statement in SQL). BTREE and HASH are supported by mysql and
* The method to create the index by (`USING` statement in SQL). BTREE and HASH are supported by mysql and
...
...
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