forked from xo/xo
/
territory.xo.go
152 lines (140 loc) · 4.32 KB
/
territory.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
148
149
150
151
152
package sqlite3
// Code generated by xo. DO NOT EDIT.
import (
"context"
)
// Territory represents a row from 'territories'.
type Territory struct {
TerritoryID string `json:"territory_id"` // territory_id
TerritoryDescription string `json:"territory_description"` // territory_description
RegionID int `json:"region_id"` // region_id
// xo fields
_exists, _deleted bool
}
// Exists returns true when the Territory exists in the database.
func (t *Territory) Exists() bool {
return t._exists
}
// Deleted returns true when the Territory has been marked for deletion from
// the database.
func (t *Territory) Deleted() bool {
return t._deleted
}
// Insert inserts the Territory to the database.
func (t *Territory) Insert(ctx context.Context, db DB) error {
switch {
case t._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case t._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (manual)
const sqlstr = `INSERT INTO territories (` +
`territory_id, territory_description, region_id` +
`) VALUES (` +
`$1, $2, $3` +
`)`
// run
logf(sqlstr, t.TerritoryID, t.TerritoryDescription, t.RegionID)
if _, err := db.ExecContext(ctx, sqlstr, t.TerritoryID, t.TerritoryDescription, t.RegionID); err != nil {
return logerror(err)
}
// set exists
t._exists = true
return nil
}
// Update updates a Territory in the database.
func (t *Territory) Update(ctx context.Context, db DB) error {
switch {
case !t._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case t._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE territories SET ` +
`territory_description = $1, region_id = $2 ` +
`WHERE territory_id = $3`
// run
logf(sqlstr, t.TerritoryDescription, t.RegionID, t.TerritoryID)
if _, err := db.ExecContext(ctx, sqlstr, t.TerritoryDescription, t.RegionID, t.TerritoryID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the Territory to the database.
func (t *Territory) Save(ctx context.Context, db DB) error {
if t.Exists() {
return t.Update(ctx, db)
}
return t.Insert(ctx, db)
}
// Upsert performs an upsert for Territory.
func (t *Territory) Upsert(ctx context.Context, db DB) error {
switch {
case t._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO territories (` +
`territory_id, territory_description, region_id` +
`) VALUES (` +
`$1, $2, $3` +
`)` +
` ON CONFLICT (territory_id) DO ` +
`UPDATE SET ` +
`territory_description = EXCLUDED.territory_description, region_id = EXCLUDED.region_id `
// run
logf(sqlstr, t.TerritoryID, t.TerritoryDescription, t.RegionID)
if _, err := db.ExecContext(ctx, sqlstr, t.TerritoryID, t.TerritoryDescription, t.RegionID); err != nil {
return logerror(err)
}
// set exists
t._exists = true
return nil
}
// Delete deletes the Territory from the database.
func (t *Territory) Delete(ctx context.Context, db DB) error {
switch {
case !t._exists: // doesn't exist
return nil
case t._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM territories ` +
`WHERE territory_id = $1`
// run
logf(sqlstr, t.TerritoryID)
if _, err := db.ExecContext(ctx, sqlstr, t.TerritoryID); err != nil {
return logerror(err)
}
// set deleted
t._deleted = true
return nil
}
// TerritoryByTerritoryID retrieves a row from 'territories' as a Territory.
//
// Generated from index 'sqlite_autoindex_territories_1'.
func TerritoryByTerritoryID(ctx context.Context, db DB, territoryID string) (*Territory, error) {
// query
const sqlstr = `SELECT ` +
`territory_id, territory_description, region_id ` +
`FROM territories ` +
`WHERE territory_id = $1`
// run
logf(sqlstr, territoryID)
t := Territory{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, territoryID).Scan(&t.TerritoryID, &t.TerritoryDescription, &t.RegionID); err != nil {
return nil, logerror(err)
}
return &t, nil
}
// Region returns the Region associated with the Territory's (RegionID).
//
// Generated from foreign key 'territories_region_id_fkey'.
func (t *Territory) Region(ctx context.Context, db DB) (*Region, error) {
return RegionByRegionID(ctx, db, t.RegionID)
}