-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimestamp.go
79 lines (66 loc) · 1.25 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package postgres
import (
"fmt"
"github.com/aodin/sol/dialect"
)
const (
NowUTC = "now() at time zone 'utc'"
Now = "now()"
)
type timestamp struct {
name string
isNotNull bool
isUnique bool
withTimezone bool
defaultValue string // TODO Additional defaults?
}
func (t timestamp) Create(d dialect.Dialect) (string, error) {
compiled := t.name
if t.withTimezone {
compiled += " with time zone"
}
if t.isNotNull {
compiled += " NOT NULL"
}
if t.isUnique {
compiled += " UNIQUE"
}
if t.defaultValue != "" {
compiled += fmt.Sprintf(" DEFAULT (%s)", t.defaultValue)
}
return compiled, nil
}
func (t timestamp) Default(value string) timestamp {
t.defaultValue = value
return t
}
func (t timestamp) NotNull() timestamp {
t.isNotNull = true
return t
}
func (t timestamp) Unique() timestamp {
t.isUnique = true
return t
}
func (t timestamp) WithoutTimezone() timestamp {
t.withTimezone = false
return t
}
func (t timestamp) WithTimezone() timestamp {
// TODO specify timezone?
t.withTimezone = true
return t
}
// TODO Date cannot have a time zone
func Date() (t timestamp) {
t.name = "date"
return
}
func Time() (t timestamp) {
t.name = "time"
return
}
func Timestamp() (t timestamp) {
t.name = "timestamp"
return
}