github.com/kunlun-qilian/sqlx/v3@v3.0.0/builder/def_key.go (about) 1 package builder 2 3 import ( 4 "strings" 5 ) 6 7 func PrimaryKey(columns *Columns) *Key { 8 return UniqueIndex("PRIMARY", columns) 9 } 10 11 func UniqueIndex(name string, columns *Columns, exprs ...string) *Key { 12 key := Index(name, columns, exprs...) 13 key.IsUnique = true 14 return key 15 } 16 17 func Index(name string, columns *Columns, exprs ...string) *Key { 18 k := &Key{ 19 Name: strings.ToLower(name), 20 } 21 22 if columns != nil { 23 k.Def.FieldNames = columns.FieldNames() 24 k.Def.ColNames = columns.ColNames() 25 } 26 27 if len(exprs) > 0 { 28 k.Def.Expr = strings.Join(exprs, " ") 29 } 30 31 return k 32 } 33 34 var _ TableDefinition = (*Key)(nil) 35 36 func ParseIndexDef(parts ...string) *IndexDef { 37 fe := IndexDef{} 38 39 if len(parts) == 1 { 40 s := parts[0] 41 42 if strings.Contains(s, "#") || strings.Contains(s, "(") { 43 fe.Expr = s 44 } else { 45 fe.FieldNames = strings.Split(s, " ") 46 } 47 } else { 48 fe.FieldNames = parts 49 } 50 51 return &fe 52 } 53 54 type IndexDef struct { 55 FieldNames []string 56 ColNames []string 57 Expr string 58 } 59 60 func (e IndexDef) ToDefs() []string { 61 if e.Expr != "" { 62 return []string{e.Expr} 63 } 64 return e.FieldNames 65 } 66 67 func (e IndexDef) TableExpr(t *Table) *Ex { 68 if len(e.Expr) != 0 { 69 return t.Expr(e.Expr) 70 } 71 if len(e.ColNames) != 0 { 72 ex := Expr("") 73 ex.WriteGroup(func(ex *Ex) { 74 ex.WriteExpr(t.MustCols(e.ColNames...)) 75 }) 76 return ex 77 } 78 ex := Expr("") 79 ex.WriteGroup(func(ex *Ex) { 80 ex.WriteExpr(t.MustFields(e.FieldNames...)) 81 }) 82 return ex 83 } 84 85 type Key struct { 86 Table *Table 87 88 Name string 89 IsUnique bool 90 Method string 91 Def IndexDef 92 } 93 94 func (key Key) On(table *Table) *Key { 95 key.Table = table 96 return &key 97 } 98 99 func (key Key) Using(method string) *Key { 100 key.Method = method 101 return &key 102 } 103 104 func (key *Key) T() *Table { 105 return key.Table 106 } 107 108 func (key *Key) IsPrimary() bool { 109 return key.IsUnique && key.Name == "primary" || strings.HasSuffix(key.Name, "pkey") 110 } 111 112 func (key *Key) IsPartition() bool { 113 return key.Name == "partition" 114 } 115 116 type Keys struct { 117 l []*Key 118 } 119 120 func (keys *Keys) Clone() *Keys { 121 k := &Keys{} 122 keys.Range(func(key *Key, idx int) { 123 k.Add(key) 124 }) 125 return k 126 } 127 128 func (keys *Keys) Len() int { 129 if keys == nil { 130 return 0 131 } 132 return len(keys.l) 133 } 134 135 func (keys *Keys) IsEmpty() bool { 136 return keys.Len() == 0 137 } 138 139 func (keys *Keys) Key(keyName string) (key *Key) { 140 keyName = strings.ToLower(keyName) 141 for i := range keys.l { 142 k := keys.l[i] 143 if keyName == k.Name { 144 return k 145 } 146 } 147 return nil 148 } 149 150 func (keys *Keys) Add(nextKeys ...*Key) { 151 for i := range nextKeys { 152 key := nextKeys[i] 153 if key == nil { 154 continue 155 } 156 keys.l = append(keys.l, key) 157 } 158 } 159 160 func (keys *Keys) Range(cb func(key *Key, idx int)) { 161 for i := range keys.l { 162 cb(keys.l[i], i) 163 } 164 }