go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/dbgen/index.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package dbgen
     9  
    10  import (
    11  	"strings"
    12  
    13  	"go.charczuk.com/sdk/db"
    14  )
    15  
    16  // Index generates an create index statement.
    17  func Index(obj any, columns ...string) string {
    18  	for _, column := range columns {
    19  		mustHaveColumn(obj, column)
    20  	}
    21  
    22  	objTableName := db.TableName(obj)
    23  	columnLabels := strings.Join(columns, "_")
    24  	columnCSV := strings.Join(columns, ",")
    25  	return f(`CREATE INDEX ix_{{ .objTableName }}_{{ .columnLabels }} ON {{ .objTableName }}({{ .columnCSV }})`, v{
    26  		"objTableName": objTableName,
    27  		"columnLabels": columnLabels,
    28  		"columnCSV":    columnCSV,
    29  	})
    30  }