-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprimary_keys.go
More file actions
49 lines (39 loc) · 1.33 KB
/
primary_keys.go
File metadata and controls
49 lines (39 loc) · 1.33 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package norm
import (
"github.com/picatic/norm/field"
)
// PrimaryKeyer a models primary key(s)
type PrimaryKeyer interface {
Fields() field.Names
Generator(model Model) (field.Names, error)
}
// CustomPrimaryKeyFn function prototype for custom Generator function wrap
type CustomPrimaryKeyFn func(pk PrimaryKeyer, model Model) (field.Names, error)
// noopPrimaryKeyGenerator used for auto-increment primary keys
func noopPrimaryKeyGenerator(pk PrimaryKeyer, model Model) (field.Names, error) {
return field.Names{}, nil
}
type primaryKey struct {
fields field.Names
fn CustomPrimaryKeyFn
}
// Fields
func (pks *primaryKey) Fields() field.Names {
return pks.fields
}
// Generator NOOP
func (pks *primaryKey) Generator(model Model) (field.Names, error) {
return pks.fn(pks, model)
}
// NewSinglePrimaryKey returns a single field PrimaryKeyer
func NewSinglePrimaryKey(primaryKeyField field.Name) PrimaryKeyer {
return &primaryKey{fields: field.Names{primaryKeyField}, fn: noopPrimaryKeyGenerator}
}
// NewMultiplePrimaryKey returns a multiple
func NewMultiplePrimaryKey(fields field.Names) PrimaryKeyer {
return &primaryKey{fields: fields, fn: noopPrimaryKeyGenerator}
}
// NewCustomPrimaryKey custom key generator
func NewCustomPrimaryKey(fields field.Names, fn CustomPrimaryKeyFn) PrimaryKeyer {
return &primaryKey{fields: fields, fn: fn}
}