generateAddIndex generates DDL to add an index.
(table QualifiedName, index Index)
| 3205 | |
| 3206 | // generateAddIndex generates DDL to add an index. |
| 3207 | func (g *Generator) generateAddIndex(table QualifiedName, index Index) string { |
| 3208 | var uniqueOption string |
| 3209 | var clusteredOption string |
| 3210 | if index.unique { |
| 3211 | uniqueOption = " UNIQUE" |
| 3212 | } |
| 3213 | if index.clustered { |
| 3214 | clusteredOption = " CLUSTERED" |
| 3215 | } else { |
| 3216 | clusteredOption = " NONCLUSTERED" |
| 3217 | } |
| 3218 | |
| 3219 | columns := []string{} |
| 3220 | for _, indexColumn := range index.columns { |
| 3221 | var column string |
| 3222 | // For simple column references (ColName), use escapeSQLIdent to preserve quoting |
| 3223 | if colName, ok := indexColumn.columnExpr.(*parser.ColName); ok { |
| 3224 | column = g.escapeSQLIdent(colName.Name) |
| 3225 | } else { |
| 3226 | // For expressions (functional indexes), format with quote awareness |
| 3227 | if !g.config.LegacyIgnoreQuotes { |
| 3228 | column = g.formatExprQuoteAware(indexColumn.columnExpr) |
| 3229 | } else { |
| 3230 | // Legacy mode: use parser.String for backward compatibility |
| 3231 | column = parser.String(indexColumn.columnExpr) |
| 3232 | } |
| 3233 | if g.indexKeyPartNeedsParens(indexColumn.columnExpr) { |
| 3234 | column = "(" + column + ")" |
| 3235 | } |
| 3236 | } |
| 3237 | if indexColumn.length != nil { |
| 3238 | column += fmt.Sprintf("(%d)", *indexColumn.length) |
| 3239 | } |
| 3240 | if indexColumn.direction == DescScr { |
| 3241 | column += fmt.Sprintf(" %s", indexColumn.direction) |
| 3242 | } |
| 3243 | if indexColumn.operatorClass != "" { |
| 3244 | column += " " + indexColumn.operatorClass |
| 3245 | } |
| 3246 | if indexColumn.withoutOverlaps { |
| 3247 | column += " WITHOUT OVERLAPS" |
| 3248 | } |
| 3249 | columns = append(columns, column) |
| 3250 | } |
| 3251 | |
| 3252 | optionDefinition := g.generateIndexOptionDefinition(index.options) |
| 3253 | |
| 3254 | switch g.mode { |
| 3255 | case GeneratorModeMssql: |
| 3256 | var ddl string |
| 3257 | var partition string |
| 3258 | if !index.primary { |
| 3259 | ddl = fmt.Sprintf( |
| 3260 | "CREATE%s%s INDEX %s ON %s", |
| 3261 | uniqueOption, |
| 3262 | clusteredOption, |
| 3263 | g.escapeSQLIdent(index.name), |
| 3264 | g.escapeQualifiedName(table), |
no test coverage detected