| | 38 | |
| | 39 | def sql_indexes_for_field(self, model, f, style): |
| | 40 | if f.db_index and not f.unique: |
| | 41 | qn = self.connection.ops.quote_name |
| | 42 | db_table = model._meta.db_table |
| | 43 | tablespace = f.db_tablespace or model._meta.db_tablespace |
| | 44 | if tablespace: |
| | 45 | sql = self.connection.ops.tablespace_sql(tablespace) |
| | 46 | if sql: |
| | 47 | tablespace_sql = ' ' + sql |
| | 48 | else: |
| | 49 | tablespace_sql = '' |
| | 50 | else: |
| | 51 | tablespace_sql = '' |
| | 52 | |
| | 53 | def get_index_sql(index_name, opclass=''): |
| | 54 | return (style.SQL_KEYWORD('CREATE INDEX') + ' ' + |
| | 55 | style.SQL_TABLE(qn(index_name)) + ' ' + |
| | 56 | style.SQL_KEYWORD('ON') + ' ' + |
| | 57 | style.SQL_TABLE(qn(db_table)) + ' ' + |
| | 58 | "(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) + |
| | 59 | "%s;" % tablespace_sql) |
| | 60 | |
| | 61 | output = [get_index_sql('%s_%s' % (db_table, f.column))] |
| | 62 | |
| | 63 | # Fields with database column types of `varchar` and `text` need |
| | 64 | # a second index that specifies their operator class, which is |
| | 65 | # needed for performing LIKE queries specific that will work |
| | 66 | # outside the C locale. See #12234. |
| | 67 | db_type = f.db_type() |
| | 68 | if db_type.startswith('varchar'): |
| | 69 | output.append(get_index_sql('%s_%s_like' % (db_table, f.column), |
| | 70 | ' varchar_pattern_ops')) |
| | 71 | elif db_type.startswith('text'): |
| | 72 | output.append(get_index_sql('%s_%s_like' % (db_table, f.column), |
| | 73 | ' text_pattern_ops')) |
| | 74 | else: |
| | 75 | output = [] |
| | 76 | return output |