不要怂,就是干,撸起袖子干!

Commit 5e857245 by Colin Cheng Committed by GitHub

feat(index): improve to support multiple fields with operator (#11934)

1 parent 41aba1fd
...@@ -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,
......
...@@ -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
); );
......
...@@ -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',
......
...@@ -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', () => {
......
...@@ -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
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!