| 37 | |
| 38 | def sql_indexes_for_field(self, model, f, style): |
| 39 | from django.db.models.fields import CharField, TextField |
| 40 | |
| 41 | if f.db_index and not f.unique: |
| 42 | qn = self.connection.ops.quote_name |
| 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, db_table, column, opclass=''): |
| 54 | return (style.SQL_KEYWORD('CREATE INDEX') + ' ' + |
| 55 | style.SQL_TABLE(index_name) + ' ' + |
| 56 | style.SQL_KEYWORD('ON') + ' ' + |
| 57 | style.SQL_TABLE(db_table) + ' ' + |
| 58 | "(%s%s)" % (style.SQL_FIELD(column), opclass) + |
| 59 | "%s;" % tablespace_sql) |
| 60 | |
| 61 | db_table = model._meta.db_table |
| 62 | output = [get_index_sql(qn('%s_%s' % (db_table, f.column)), |
| 63 | qn(db_table), qn(f.column))] |
| 64 | |
| 65 | if isinstance(f, CharField): |
| 66 | output.append(get_index_sql(qn('%s_%s_like' % (db_table, f.column)), |
| 67 | qn(db_table), qn(f.column), |
| 68 | ' varchar_pattern_ops')) |
| 69 | elif isinstance(f, TextField): |
| 70 | output.append(get_index_sql(qn('%s_%s_like' % (db_table, f.column)), |
| 71 | qn(db_table), qn(f.column), |
| 72 | ' text_pattern_ops')) |
| 73 | else: |
| 74 | output = [] |
| 75 | return output |