github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_def_key.go (about)

     1  package builder
     2  
     3  import "strings"
     4  
     5  type Key struct {
     6  	Table    *Table
     7  	Name     string
     8  	IsUnique bool
     9  	Method   string
    10  	Def      IndexDef
    11  }
    12  
    13  func (k Key) On(t *Table) *Key { k.Table = t; return &k }
    14  
    15  func (k Key) Using(method string) *Key { k.Method = method; return &k }
    16  
    17  func (k *Key) T() *Table { return k.Table }
    18  
    19  func (k Key) IsPrimary() bool {
    20  	return k.IsUnique && (k.Name == "primary" || strings.HasSuffix(k.Name, "pkey"))
    21  }
    22  
    23  type Keys struct {
    24  	lst []*Key
    25  }
    26  
    27  func (ks *Keys) Len() int {
    28  	if ks == nil {
    29  		return 0
    30  	}
    31  	return len(ks.lst)
    32  }
    33  
    34  func (ks *Keys) Clone() *Keys {
    35  	cloned := &Keys{}
    36  	ks.Range(func(k *Key, idx int) {
    37  		cloned.Add(k)
    38  	})
    39  	return cloned
    40  }
    41  
    42  func (ks *Keys) Range(f func(k *Key, idx int)) {
    43  	for i := range ks.lst {
    44  		f(ks.lst[i], i)
    45  	}
    46  }
    47  
    48  func (ks *Keys) Add(keys ...*Key) {
    49  	for i := range keys {
    50  		if k := keys[i]; k != nil {
    51  			ks.lst = append(ks.lst, k)
    52  		}
    53  	}
    54  }
    55  
    56  func (ks *Keys) Key(name string) *Key {
    57  	name = strings.ToLower(name)
    58  	for i := range ks.lst {
    59  		if name == ks.lst[i].Name {
    60  			return ks.lst[i]
    61  		}
    62  	}
    63  	return nil
    64  }
    65  
    66  // IndexDef @def index xxx/BTREE FieldA FieldB ...
    67  type IndexDef struct {
    68  	FieldNames []string
    69  	ColNames   []string
    70  	Expr       string
    71  }
    72  
    73  func ParseIndexDef(names ...string) *IndexDef {
    74  	f := &IndexDef{}
    75  
    76  	if len(names) == 1 {
    77  		s := names[0]
    78  		if strings.Contains(s, "#") || strings.Contains(s, "(") {
    79  			f.Expr = s
    80  		} else {
    81  			f.FieldNames = strings.Split(s, " ")
    82  		}
    83  	} else {
    84  		f.FieldNames = names
    85  	}
    86  	return f
    87  }
    88  
    89  func (i IndexDef) ToDefs() []string {
    90  	if i.Expr != "" {
    91  		return []string{i.Expr}
    92  	}
    93  	return i.FieldNames
    94  }
    95  
    96  func (i IndexDef) TableExpr(t *Table) *Ex {
    97  	if i.Expr != "" {
    98  		return t.Expr(i.Expr)
    99  	}
   100  	if len(i.ColNames) != 0 {
   101  		ex := Expr("")
   102  		ex.WriteGroup(func(ex *Ex) {
   103  			ex.WriteExpr(t.MustCols(i.ColNames...))
   104  		})
   105  		return ex
   106  	}
   107  	ex := Expr("")
   108  	ex.WriteGroup(func(ex *Ex) {
   109  		ex.WriteExpr(t.MustColsByFieldNames(i.FieldNames...))
   110  	})
   111  	return ex
   112  }
   113  
   114  func PrimaryKey(cols *Columns) *Key { return UniqueIndex("PRIMARY", cols) }
   115  
   116  func UniqueIndex(name string, cols *Columns, exprs ...string) *Key {
   117  	k := Index(name, cols, exprs...)
   118  	k.IsUnique = true
   119  	return k
   120  }
   121  
   122  func Index(name string, cols *Columns, exprs ...string) *Key {
   123  	k := &Key{Name: strings.ToLower(name)}
   124  	if cols != nil {
   125  		k.Def.FieldNames = cols.FieldNames()
   126  		k.Def.ColNames = cols.ColNames()
   127  	}
   128  	if len(exprs) > 0 {
   129  		k.Def.Expr = strings.Join(exprs, " ")
   130  	}
   131  	return k
   132  }