-
Notifications
You must be signed in to change notification settings - Fork 0
/
oracle.type.go.tpl
134 lines (109 loc) · 3.39 KB
/
oracle.type.go.tpl
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
{{- $short := (shortname .Name "err" "res" "sqlstr" "db" "XOLog") -}}
{{- $table := (schema .Schema .Table.TableName) -}}
{{- if .Comment -}}
// {{ .Comment }}
{{- else -}}
// {{ .Name }} represents a row from '{{ $table }}'.
{{- end }}
type {{ .Name }} struct {
{{- range .Fields }}
{{ .Name }} {{ retype .Type }} `json:"{{ .Col.ColumnName }}"` // {{ .Col.ColumnName }}
{{- end }}
{{- if .PrimaryKey }}
// xo fields
_exists, _deleted bool
{{ end }}
}
{{ if .PrimaryKey }}
// Exists determines if the {{ .Name }} exists in the database.
func ({{ $short }} *{{ .Name }}) Exists() bool {
return {{ $short }}._exists
}
// Deleted provides information if the {{ .Name }} has been deleted from the database.
func ({{ $short }} *{{ .Name }}) Deleted() bool {
return {{ $short }}._deleted
}
// Insert inserts the {{ .Name }} to the database.
func ({{ $short }} *{{ .Name }}) Insert(db XODB) error {
var err error
// if already exist, bail
if {{ $short }}._exists {
return errors.New("insert failed: already exists")
}
// sql query
const sqlstr = `INSERT INTO {{ $table }} (` +
`{{ colnames .Fields .PrimaryKey.Name }}` +
`) VALUES (` +
`{{ colvals .Fields .PrimaryKey.Name }}` +
`) RETURNING {{ colname .PrimaryKey.Col }} /*lastInsertId*/ INTO :pk`
// run query
XOLog(sqlstr, {{ fieldnames .Fields $short .PrimaryKey.Name }}, nil)
res, err := db.Exec(sqlstr, {{ fieldnames .Fields $short .PrimaryKey.Name }}, nil)
if err != nil {
return err
}
// retrieve id
id, err := res.LastInsertId()
if err != nil {
return err
}
// set primary key and existence
{{ $short }}.{{ .PrimaryKey.Name }} = {{ .PrimaryKey.Type }}(id)
{{ $short }}._exists = true
return nil
}
{{ if ne (fieldnames .Fields $short .PrimaryKey.Name) "" }}
// Update updates the {{ .Name }} in the database.
func ({{ $short }} *{{ .Name }}) Update(db XODB) error {
var err error
// if doesn't exist, bail
if !{{ $short }}._exists {
return errors.New("update failed: does not exist")
}
// if deleted, bail
if {{ $short }}._deleted {
return errors.New("update failed: marked for deletion")
}
// sql query
const sqlstr = `UPDATE {{ $table }} SET ` +
`{{ colnamesquery .Fields ", " .PrimaryKey.Name }}` +
` WHERE {{ colname .PrimaryKey.Col }} = :{{ colcount .Fields .PrimaryKey.Name }}`
// run query
XOLog(sqlstr, {{ fieldnames .Fields $short .PrimaryKey.Name }}, {{ $short }}.{{ .PrimaryKey.Name }})
_, err = db.Exec(sqlstr, {{ fieldnames .Fields $short .PrimaryKey.Name }}, {{ $short }}.{{ .PrimaryKey.Name }})
return err
}
// Save saves the {{ .Name }} to the database.
func ({{ $short }} *{{ .Name }}) Save(db XODB) error {
if {{ $short }}.Exists() {
return {{ $short }}.Update(db)
}
return {{ $short }}.Insert(db)
}
{{ else }}
// Update statements omitted due to lack of fields other than primary key
{{ end }}
// Delete deletes the {{ .Name }} from the database.
func ({{ $short }} *{{ .Name }}) Delete(db XODB) error {
var err error
// if doesn't exist, bail
if !{{ $short }}._exists {
return nil
}
// if deleted, bail
if {{ $short }}._deleted {
return nil
}
// sql query
const sqlstr = `DELETE FROM {{ $table }} WHERE {{ colname .PrimaryKey.Col }} = :1`
// run query
XOLog(sqlstr, {{ $short }}.{{ .PrimaryKey.Name }})
_, err = db.Exec(sqlstr, {{ $short }}.{{ .PrimaryKey.Name }})
if err != nil {
return err
}
// set deleted
{{ $short }}._deleted = true
return nil
}
{{- end }}