AddIndex adds a new index into the current collection. If the collection has an existing index matching the new name it will be replaced with the new one.
(name string, unique bool, columnsExpr string, optWhereExpr string)
| 646 | // |
| 647 | // If the collection has an existing index matching the new name it will be replaced with the new one. |
| 648 | func (m *Collection) AddIndex(name string, unique bool, columnsExpr string, optWhereExpr string) { |
| 649 | m.RemoveIndex(name) |
| 650 | |
| 651 | var idx strings.Builder |
| 652 | |
| 653 | idx.WriteString("CREATE ") |
| 654 | if unique { |
| 655 | idx.WriteString("UNIQUE ") |
| 656 | } |
| 657 | idx.WriteString("INDEX `") |
| 658 | idx.WriteString(name) |
| 659 | idx.WriteString("` ") |
| 660 | idx.WriteString("ON `") |
| 661 | idx.WriteString(m.Name) |
| 662 | idx.WriteString("` (") |
| 663 | idx.WriteString(columnsExpr) |
| 664 | idx.WriteString(")") |
| 665 | if optWhereExpr != "" { |
| 666 | idx.WriteString(" WHERE ") |
| 667 | idx.WriteString(optWhereExpr) |
| 668 | } |
| 669 | |
| 670 | m.Indexes = append(m.Indexes, idx.String()) |
| 671 | } |
| 672 | |
| 673 | // RemoveIndex removes a single index with the specified name from the current collection. |
| 674 | func (m *Collection) RemoveIndex(name string) { |