forked from xo/xo
/
usstate.xo.go
147 lines (136 loc) · 4.15 KB
/
usstate.xo.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package postgres
// Code generated by xo. DO NOT EDIT.
import (
"context"
"database/sql"
)
// UsState represents a row from 'public.us_states'.
type UsState struct {
StateID int `json:"state_id"` // state_id
StateName sql.NullString `json:"state_name"` // state_name
StateAbbr sql.NullString `json:"state_abbr"` // state_abbr
StateRegion sql.NullString `json:"state_region"` // state_region
// xo fields
_exists, _deleted bool
}
// Exists returns true when the UsState exists in the database.
func (us *UsState) Exists() bool {
return us._exists
}
// Deleted returns true when the UsState has been marked for deletion from
// the database.
func (us *UsState) Deleted() bool {
return us._deleted
}
// Insert inserts the UsState to the database.
func (us *UsState) Insert(ctx context.Context, db DB) error {
switch {
case us._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case us._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (manual)
const sqlstr = `INSERT INTO public.us_states (` +
`state_id, state_name, state_abbr, state_region` +
`) VALUES (` +
`$1, $2, $3, $4` +
`)`
// run
logf(sqlstr, us.StateID, us.StateName, us.StateAbbr, us.StateRegion)
if _, err := db.ExecContext(ctx, sqlstr, us.StateID, us.StateName, us.StateAbbr, us.StateRegion); err != nil {
return logerror(err)
}
// set exists
us._exists = true
return nil
}
// Update updates a UsState in the database.
func (us *UsState) Update(ctx context.Context, db DB) error {
switch {
case !us._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case us._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with composite primary key
const sqlstr = `UPDATE public.us_states SET ` +
`state_name = $1, state_abbr = $2, state_region = $3 ` +
`WHERE state_id = $4`
// run
logf(sqlstr, us.StateName, us.StateAbbr, us.StateRegion, us.StateID)
if _, err := db.ExecContext(ctx, sqlstr, us.StateName, us.StateAbbr, us.StateRegion, us.StateID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the UsState to the database.
func (us *UsState) Save(ctx context.Context, db DB) error {
if us.Exists() {
return us.Update(ctx, db)
}
return us.Insert(ctx, db)
}
// Upsert performs an upsert for UsState.
func (us *UsState) Upsert(ctx context.Context, db DB) error {
switch {
case us._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO public.us_states (` +
`state_id, state_name, state_abbr, state_region` +
`) VALUES (` +
`$1, $2, $3, $4` +
`)` +
` ON CONFLICT (state_id) DO ` +
`UPDATE SET ` +
`state_name = EXCLUDED.state_name, state_abbr = EXCLUDED.state_abbr, state_region = EXCLUDED.state_region `
// run
logf(sqlstr, us.StateID, us.StateName, us.StateAbbr, us.StateRegion)
if _, err := db.ExecContext(ctx, sqlstr, us.StateID, us.StateName, us.StateAbbr, us.StateRegion); err != nil {
return logerror(err)
}
// set exists
us._exists = true
return nil
}
// Delete deletes the UsState from the database.
func (us *UsState) Delete(ctx context.Context, db DB) error {
switch {
case !us._exists: // doesn't exist
return nil
case us._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM public.us_states ` +
`WHERE state_id = $1`
// run
logf(sqlstr, us.StateID)
if _, err := db.ExecContext(ctx, sqlstr, us.StateID); err != nil {
return logerror(err)
}
// set deleted
us._deleted = true
return nil
}
// UsStateByStateID retrieves a row from 'public.us_states' as a UsState.
//
// Generated from index 'us_states_pkey'.
func UsStateByStateID(ctx context.Context, db DB, stateID int) (*UsState, error) {
// query
const sqlstr = `SELECT ` +
`state_id, state_name, state_abbr, state_region ` +
`FROM public.us_states ` +
`WHERE state_id = $1`
// run
logf(sqlstr, stateID)
us := UsState{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, stateID).Scan(&us.StateID, &us.StateName, &us.StateAbbr, &us.StateRegion); err != nil {
return nil, logerror(err)
}
return &us, nil
}