(t *model.TableMetadata, name string, keyList []string, unique bool, tp string, option *tidbast.IndexOption)
| 1297 | } |
| 1298 | |
| 1299 | func tidbCreateIndexHelper(t *model.TableMetadata, name string, keyList []string, unique bool, tp string, option *tidbast.IndexOption) *storepb.Advice { |
| 1300 | if len(keyList) == 0 { |
| 1301 | content := fmt.Sprintf("Index `%s` in table `%s` has empty key", name, t.GetProto().Name) |
| 1302 | return &storepb.Advice{ |
| 1303 | Status: storepb.Advice_ERROR, |
| 1304 | Code: code.IndexEmptyKeys.Int32(), |
| 1305 | Title: content, |
| 1306 | Content: content, |
| 1307 | StartPosition: &storepb.Position{Line: 0}, |
| 1308 | } |
| 1309 | } |
| 1310 | if name != "" { |
| 1311 | if t.GetIndex(name) != nil { |
| 1312 | content := fmt.Sprintf("Index `%s` already exists in table `%s`", name, t.GetProto().Name) |
| 1313 | return &storepb.Advice{ |
| 1314 | Status: storepb.Advice_ERROR, |
| 1315 | Code: code.IndexExists.Int32(), |
| 1316 | Title: content, |
| 1317 | Content: content, |
| 1318 | StartPosition: &storepb.Position{Line: 0}, |
| 1319 | } |
| 1320 | } |
| 1321 | } else { |
| 1322 | suffix := 1 |
| 1323 | for { |
| 1324 | name = keyList[0] |
| 1325 | if suffix > 1 { |
| 1326 | name = fmt.Sprintf("%s_%d", keyList[0], suffix) |
| 1327 | } |
| 1328 | if t.GetIndex(name) == nil { |
| 1329 | break |
| 1330 | } |
| 1331 | suffix++ |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | visible := option == nil || option.Visibility != tidbast.IndexVisibilityInvisible |
| 1336 | |
| 1337 | index := &storepb.IndexMetadata{ |
| 1338 | Name: name, |
| 1339 | Expressions: keyList, |
| 1340 | Type: tp, |
| 1341 | Unique: unique, |
| 1342 | Primary: false, |
| 1343 | Visible: visible, |
| 1344 | } |
| 1345 | |
| 1346 | if err := t.CreateIndex(index); err != nil { |
| 1347 | return &storepb.Advice{ |
| 1348 | Status: storepb.Advice_ERROR, |
| 1349 | Code: code.IndexExists.Int32(), |
| 1350 | Title: err.Error(), |
| 1351 | Content: err.Error(), |
| 1352 | StartPosition: &storepb.Position{Line: 0}, |
| 1353 | } |
| 1354 | } |
| 1355 | return nil |
| 1356 | } |
no test coverage detected