github.com/eden-framework/sqlx@v0.0.2/builder/def_key.go (about)

     1  package builder
     2  
     3  import (
     4  	"container/list"
     5  	"strings"
     6  )
     7  
     8  func PrimaryKey(columns *Columns) *Key {
     9  	return UniqueIndex("PRIMARY", columns)
    10  }
    11  
    12  func Index(name string, columns *Columns) *Key {
    13  	return &Key{
    14  		Name:    name,
    15  		Columns: columns,
    16  	}
    17  }
    18  
    19  func UniqueIndex(name string, columns *Columns) *Key {
    20  	return &Key{
    21  		Name:     name,
    22  		IsUnique: true,
    23  		Columns:  columns,
    24  	}
    25  }
    26  
    27  var _ TableDefinition = (*Key)(nil)
    28  
    29  type Key struct {
    30  	Columns *Columns
    31  	Table   *Table
    32  
    33  	Name     string
    34  	IsUnique bool
    35  	Method   string
    36  }
    37  
    38  func (key Key) On(table *Table) *Key {
    39  	key.Table = table
    40  	return &key
    41  }
    42  
    43  func (key Key) Using(method string) *Key {
    44  	key.Method = method
    45  	return &key
    46  }
    47  
    48  func (key *Key) T() *Table {
    49  	return key.T()
    50  }
    51  
    52  func (key *Key) IsPrimary() bool {
    53  	return key.IsUnique && (strings.ToLower(key.Name) == "primary" || strings.HasSuffix(strings.ToLower(key.Name), "pkey"))
    54  }
    55  
    56  type Keys struct {
    57  	m map[string]*list.Element
    58  	l *list.List
    59  }
    60  
    61  func (keys *Keys) Clone() *Keys {
    62  	k := &Keys{}
    63  	keys.Range(func(key *Key, idx int) {
    64  		k.Add(key)
    65  	})
    66  	return k
    67  }
    68  
    69  func (keys *Keys) Len() int {
    70  	if keys.l == nil {
    71  		return 0
    72  	}
    73  	return keys.l.Len()
    74  }
    75  
    76  func (keys *Keys) IsEmpty() bool {
    77  	return keys.l == nil || keys.l.Len() == 0
    78  }
    79  
    80  func (keys *Keys) Key(keyName string) (key *Key) {
    81  	if keys.m != nil {
    82  		if c, ok := keys.m[strings.ToLower(keyName)]; ok {
    83  			return c.Value.(*Key)
    84  		}
    85  	}
    86  	return nil
    87  }
    88  
    89  func (keys *Keys) Add(nextKeys ...*Key) {
    90  	if keys.m == nil {
    91  		keys.m = map[string]*list.Element{}
    92  		keys.l = list.New()
    93  	}
    94  	for _, key := range nextKeys {
    95  		if key == nil {
    96  			continue
    97  		}
    98  		key.Name = strings.ToLower(key.Name)
    99  		keys.m[key.Name] = keys.l.PushBack(key)
   100  	}
   101  }
   102  
   103  func (keys *Keys) Remove(name string) {
   104  	if keys.m != nil {
   105  		if e, exists := keys.m[name]; exists {
   106  			keys.l.Remove(e)
   107  			delete(keys.m, name)
   108  		}
   109  	}
   110  }
   111  
   112  func (keys *Keys) Range(cb func(key *Key, idx int)) {
   113  	if keys.l != nil {
   114  		i := 0
   115  		for e := keys.l.Front(); e != nil; e = e.Next() {
   116  			cb(e.Value.(*Key), i)
   117  			i++
   118  		}
   119  	}
   120  }