-
Notifications
You must be signed in to change notification settings - Fork 0
/
activitytype.xo.go
147 lines (136 loc) · 4.22 KB
/
activitytype.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 xo_wxstat
// Code generated by xo. DO NOT EDIT.
import (
"context"
"database/sql"
)
// ActivityType represents a row from 'activity_type'.
type ActivityType struct {
ActivityTypeID int `json:"activity_type_id"` // activity_type_id
Name string `json:"name"` // name
Scale int `json:"scale"` // scale
Comment sql.NullString `json:"comment"` // comment
// xo fields
_exists, _deleted bool
}
// Exists returns true when the ActivityType exists in the database.
func (at *ActivityType) Exists() bool {
return at._exists
}
// Deleted returns true when the ActivityType has been marked for deletion from
// the database.
func (at *ActivityType) Deleted() bool {
return at._deleted
}
// Insert inserts the ActivityType to the database.
func (at *ActivityType) Insert(ctx context.Context, db DB) error {
switch {
case at._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case at._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (manual)
const sqlstr = `INSERT INTO activity_type (` +
`activity_type_id, name, scale, comment` +
`) VALUES (` +
`$1, $2, $3, $4` +
`)`
// run
logf(sqlstr, at.ActivityTypeID, at.Name, at.Scale, at.Comment)
if _, err := db.ExecContext(ctx, sqlstr, at.ActivityTypeID, at.Name, at.Scale, at.Comment); err != nil {
return logerror(err)
}
// set exists
at._exists = true
return nil
}
// Update updates a ActivityType in the database.
func (at *ActivityType) Update(ctx context.Context, db DB) error {
switch {
case !at._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case at._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE activity_type SET ` +
`name = $1, scale = $2, comment = $3 ` +
`WHERE activity_type_id = $4`
// run
logf(sqlstr, at.Name, at.Scale, at.Comment, at.ActivityTypeID)
if _, err := db.ExecContext(ctx, sqlstr, at.Name, at.Scale, at.Comment, at.ActivityTypeID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the ActivityType to the database.
func (at *ActivityType) Save(ctx context.Context, db DB) error {
if at.Exists() {
return at.Update(ctx, db)
}
return at.Insert(ctx, db)
}
// Upsert performs an upsert for ActivityType.
func (at *ActivityType) Upsert(ctx context.Context, db DB) error {
switch {
case at._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO activity_type (` +
`activity_type_id, name, scale, comment` +
`) VALUES (` +
`$1, $2, $3, $4` +
`)` +
` ON CONFLICT (activity_type_id) DO ` +
`UPDATE SET ` +
`name = EXCLUDED.name, scale = EXCLUDED.scale, comment = EXCLUDED.comment `
// run
logf(sqlstr, at.ActivityTypeID, at.Name, at.Scale, at.Comment)
if _, err := db.ExecContext(ctx, sqlstr, at.ActivityTypeID, at.Name, at.Scale, at.Comment); err != nil {
return err
}
// set exists
at._exists = true
return nil
}
// Delete deletes the ActivityType from the database.
func (at *ActivityType) Delete(ctx context.Context, db DB) error {
switch {
case !at._exists: // doesn't exist
return nil
case at._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM activity_type ` +
`WHERE activity_type_id = $1`
// run
logf(sqlstr, at.ActivityTypeID)
if _, err := db.ExecContext(ctx, sqlstr, at.ActivityTypeID); err != nil {
return logerror(err)
}
// set deleted
at._deleted = true
return nil
}
// ActivityTypeByActivityTypeID retrieves a row from 'activity_type' as a ActivityType.
//
// Generated from index 'activity_type_activity_type_id_pkey'.
func ActivityTypeByActivityTypeID(ctx context.Context, db DB, activityTypeID int) (*ActivityType, error) {
// query
const sqlstr = `SELECT ` +
`activity_type_id, name, scale, comment ` +
`FROM activity_type ` +
`WHERE activity_type_id = $1`
// run
logf(sqlstr, activityTypeID)
at := ActivityType{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, activityTypeID).Scan(&at.ActivityTypeID, &at.Name, &at.Scale, &at.Comment); err != nil {
return nil, logerror(err)
}
return &at, nil
}