forked from slicebit/qb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.go
30 lines (25 loc) · 830 Bytes
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package qb
import (
"fmt"
"strings"
)
// CompositeIndex is the struct definition when building composite indices for any struct that will be mapped into a table
type CompositeIndex string
// Index generates an index clause given table and columns as params
func Index(table string, cols ...string) IndexElem {
return IndexElem{
Table: table,
Name: fmt.Sprintf("i_%s", strings.Join(cols, "_")),
Columns: cols,
}
}
// IndexElem is the definition of any index elements for a table
type IndexElem struct {
Table string
Name string
Columns []string
}
// String returns the index element as an sql clause
func (i IndexElem) String(dialect Dialect) string {
return fmt.Sprintf("CREATE INDEX %s ON %s(%s);", dialect.Escape(i.Name), dialect.Escape(i.Table), strings.Join(dialect.EscapeAll(i.Columns), ", "))
}