|
| 1 | +/* |
| 2 | +Copyright 2024 The Vitess Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package schemadiff |
| 18 | + |
| 19 | +import ( |
| 20 | + "vitess.io/vitess/go/vt/sqlparser" |
| 21 | +) |
| 22 | + |
| 23 | +// IndexDefinitionEntity represents an index definition in a CREATE TABLE statement, |
| 24 | +// and includes the list of columns that are part of the index. |
| 25 | +type IndexDefinitionEntity struct { |
| 26 | + IndexDefinition *sqlparser.IndexDefinition |
| 27 | + ColumnList *ColumnDefinitionEntityList |
| 28 | + Env *Environment |
| 29 | +} |
| 30 | + |
| 31 | +func NewIndexDefinitionEntity(env *Environment, indexDefinition *sqlparser.IndexDefinition, columnDefinitionEntitiesList *ColumnDefinitionEntityList) *IndexDefinitionEntity { |
| 32 | + return &IndexDefinitionEntity{ |
| 33 | + IndexDefinition: indexDefinition, |
| 34 | + ColumnList: columnDefinitionEntitiesList, |
| 35 | + Env: env, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (i *IndexDefinitionEntity) Name() string { |
| 40 | + return i.IndexDefinition.Info.Name.String() |
| 41 | +} |
| 42 | + |
| 43 | +func (i *IndexDefinitionEntity) NameLowered() string { |
| 44 | + return i.IndexDefinition.Info.Name.Lowered() |
| 45 | +} |
| 46 | + |
| 47 | +// Clone returns a copy of this list, with copies of all the entities. |
| 48 | +func (i *IndexDefinitionEntity) Clone() *IndexDefinitionEntity { |
| 49 | + clone := &IndexDefinitionEntity{ |
| 50 | + IndexDefinition: sqlparser.Clone(i.IndexDefinition), |
| 51 | + ColumnList: i.ColumnList.Clone(), |
| 52 | + Env: i.Env, |
| 53 | + } |
| 54 | + return clone |
| 55 | +} |
| 56 | + |
| 57 | +func (i *IndexDefinitionEntity) Len() int { |
| 58 | + return len(i.IndexDefinition.Columns) |
| 59 | +} |
| 60 | + |
| 61 | +// IsPrimary returns true if the index is a primary key. |
| 62 | +func (i *IndexDefinitionEntity) IsPrimary() bool { |
| 63 | + return i.IndexDefinition.Info.Type == sqlparser.IndexTypePrimary |
| 64 | +} |
| 65 | + |
| 66 | +// IsUnique returns true if the index is a unique key. |
| 67 | +func (i *IndexDefinitionEntity) IsUnique() bool { |
| 68 | + return i.IndexDefinition.Info.IsUnique() |
| 69 | +} |
| 70 | + |
| 71 | +// HasExpression returns true if the index uses an expression, e.g. `KEY idx1 ((id + 1))`. |
| 72 | +func (i *IndexDefinitionEntity) HasExpression() bool { |
| 73 | + for _, col := range i.IndexDefinition.Columns { |
| 74 | + if col.Expression != nil { |
| 75 | + return true |
| 76 | + } |
| 77 | + } |
| 78 | + return false |
| 79 | +} |
| 80 | + |
| 81 | +// HasNullable returns true if any of the columns in the index are nullable. |
| 82 | +func (i *IndexDefinitionEntity) HasNullable() bool { |
| 83 | + for _, col := range i.ColumnList.Entities { |
| 84 | + if col.IsNullable() { |
| 85 | + return true |
| 86 | + } |
| 87 | + } |
| 88 | + return false |
| 89 | +} |
| 90 | + |
| 91 | +// HasFloat returns true if any of the columns in the index are floating point types. |
| 92 | +func (i *IndexDefinitionEntity) HasFloat() bool { |
| 93 | + for _, col := range i.ColumnList.Entities { |
| 94 | + if col.IsFloatingPointType() { |
| 95 | + return true |
| 96 | + } |
| 97 | + } |
| 98 | + return false |
| 99 | +} |
| 100 | + |
| 101 | +// HasColumnPrefix returns true if any of the columns in the index have a length prefix. |
| 102 | +func (i *IndexDefinitionEntity) HasColumnPrefix() bool { |
| 103 | + for _, col := range i.IndexDefinition.Columns { |
| 104 | + if col.Length != nil { |
| 105 | + return true |
| 106 | + } |
| 107 | + } |
| 108 | + return false |
| 109 | +} |
| 110 | + |
| 111 | +// ColumnNames returns the names of the columns in the index. |
| 112 | +func (i *IndexDefinitionEntity) ColumnNames() []string { |
| 113 | + names := make([]string, 0, len(i.IndexDefinition.Columns)) |
| 114 | + for _, col := range i.IndexDefinition.Columns { |
| 115 | + names = append(names, col.Column.String()) |
| 116 | + } |
| 117 | + return names |
| 118 | +} |
| 119 | + |
| 120 | +// ContainsColumns returns true if the index contains all the columns in the given list. |
| 121 | +func (i *IndexDefinitionEntity) ContainsColumns(columns *ColumnDefinitionEntityList) bool { |
| 122 | + return i.ColumnList.Contains(columns) |
| 123 | +} |
| 124 | + |
| 125 | +// CoveredByColumns returns true if the index is covered by the given list of columns. |
| 126 | +func (i *IndexDefinitionEntity) CoveredByColumns(columns *ColumnDefinitionEntityList) bool { |
| 127 | + return columns.Contains(i.ColumnList) |
| 128 | +} |
| 129 | + |
| 130 | +// IndexDefinitionEntityList is a formalized list of IndexDefinitionEntity objects with a few |
| 131 | +// utility methods. |
| 132 | +type IndexDefinitionEntityList struct { |
| 133 | + Entities []*IndexDefinitionEntity |
| 134 | +} |
| 135 | + |
| 136 | +func NewIndexDefinitionEntityList(entities []*IndexDefinitionEntity) *IndexDefinitionEntityList { |
| 137 | + return &IndexDefinitionEntityList{ |
| 138 | + Entities: entities, |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func (l *IndexDefinitionEntityList) Len() int { |
| 143 | + return len(l.Entities) |
| 144 | +} |
| 145 | + |
| 146 | +// Names returns the names of the indexes in the list. |
| 147 | +func (l *IndexDefinitionEntityList) Names() []string { |
| 148 | + names := make([]string, len(l.Entities)) |
| 149 | + for i, entity := range l.Entities { |
| 150 | + names[i] = entity.Name() |
| 151 | + } |
| 152 | + return names |
| 153 | +} |
| 154 | + |
| 155 | +// SubsetCoveredByColumns returns a new list of indexes that are covered by the given list of columns. |
| 156 | +func (l *IndexDefinitionEntityList) SubsetCoveredByColumns(columns *ColumnDefinitionEntityList) *IndexDefinitionEntityList { |
| 157 | + var subset []*IndexDefinitionEntity |
| 158 | + for _, entity := range l.Entities { |
| 159 | + if entity.CoveredByColumns(columns) { |
| 160 | + subset = append(subset, entity) |
| 161 | + } |
| 162 | + } |
| 163 | + return NewIndexDefinitionEntityList(subset) |
| 164 | +} |
| 165 | + |
| 166 | +// First returns the first index in the list, or nil if the list is empty. |
| 167 | +func (l *IndexDefinitionEntityList) First() *IndexDefinitionEntity { |
| 168 | + if len(l.Entities) == 0 { |
| 169 | + return nil |
| 170 | + } |
| 171 | + return l.Entities[0] |
| 172 | +} |
0 commit comments