/
timestamp.go
54 lines (46 loc) · 1.18 KB
/
timestamp.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package aspect
import (
"fmt"
"time"
)
// Timestamp represents TIMESTAMP column types.
// TODO take a time.Location for timezone options
type Timestamp struct {
NotNull bool
PrimaryKey bool
WithTimezone bool
WithoutTimezone bool
Default string // TODO Should this be a clause that compiles?
}
var _ Type = Timestamp{}
// Create returns the syntax need to create this column in CREATE statements.
func (s Timestamp) Create(d Dialect) (string, error) {
compiled := "TIMESTAMP"
if s.WithTimezone {
compiled += " WITH TIME ZONE"
} else if s.WithoutTimezone { // Only one timezone modifier is allowed
compiled += " WITHOUT TIME ZONE"
}
if s.NotNull {
compiled += " NOT NULL"
}
if s.Default != "" {
compiled += fmt.Sprintf(" DEFAULT (%s)", s.Default)
}
return compiled, nil
}
func (s Timestamp) IsPrimaryKey() bool {
return s.PrimaryKey
}
func (s Timestamp) IsRequired() bool {
return s.NotNull && s.Default == ""
}
func (s Timestamp) IsUnique() bool {
return s.PrimaryKey
}
func (s Timestamp) Validate(i interface{}) (interface{}, error) {
if _, ok := i.(time.Time); !ok {
return i, fmt.Errorf("value is of non-time type %T", i)
}
return i, nil
}