forked from xo/xo
/
customerdemographic.xo.go
144 lines (133 loc) · 4.2 KB
/
customerdemographic.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
package mysql
// Code generated by xo. DO NOT EDIT.
import (
"context"
"database/sql"
)
// CustomerDemographic represents a row from 'northwind.customer_demographics'.
type CustomerDemographic struct {
CustomerTypeID string `json:"customer_type_id"` // customer_type_id
CustomerDesc sql.NullString `json:"customer_desc"` // customer_desc
// xo fields
_exists, _deleted bool
}
// Exists returns true when the CustomerDemographic exists in the database.
func (cd *CustomerDemographic) Exists() bool {
return cd._exists
}
// Deleted returns true when the CustomerDemographic has been marked for deletion from
// the database.
func (cd *CustomerDemographic) Deleted() bool {
return cd._deleted
}
// Insert inserts the CustomerDemographic to the database.
func (cd *CustomerDemographic) Insert(ctx context.Context, db DB) error {
switch {
case cd._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case cd._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (manual)
const sqlstr = `INSERT INTO northwind.customer_demographics (` +
`customer_type_id, customer_desc` +
`) VALUES (` +
`?, ?` +
`)`
// run
logf(sqlstr, cd.CustomerTypeID, cd.CustomerDesc)
if _, err := db.ExecContext(ctx, sqlstr, cd.CustomerTypeID, cd.CustomerDesc); err != nil {
return logerror(err)
}
// set exists
cd._exists = true
return nil
}
// Update updates a CustomerDemographic in the database.
func (cd *CustomerDemographic) Update(ctx context.Context, db DB) error {
switch {
case !cd._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case cd._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE northwind.customer_demographics SET ` +
`customer_desc = ? ` +
`WHERE customer_type_id = ?`
// run
logf(sqlstr, cd.CustomerDesc, cd.CustomerTypeID)
if _, err := db.ExecContext(ctx, sqlstr, cd.CustomerDesc, cd.CustomerTypeID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the CustomerDemographic to the database.
func (cd *CustomerDemographic) Save(ctx context.Context, db DB) error {
if cd.Exists() {
return cd.Update(ctx, db)
}
return cd.Insert(ctx, db)
}
// Upsert performs an upsert for CustomerDemographic.
func (cd *CustomerDemographic) Upsert(ctx context.Context, db DB) error {
switch {
case cd._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO northwind.customer_demographics (` +
`customer_type_id, customer_desc` +
`) VALUES (` +
`?, ?` +
`)` +
` ON DUPLICATE KEY UPDATE ` +
`customer_type_id = VALUES(customer_type_id), customer_desc = VALUES(customer_desc)`
// run
logf(sqlstr, cd.CustomerTypeID, cd.CustomerDesc)
if _, err := db.ExecContext(ctx, sqlstr, cd.CustomerTypeID, cd.CustomerDesc); err != nil {
return logerror(err)
}
// set exists
cd._exists = true
return nil
}
// Delete deletes the CustomerDemographic from the database.
func (cd *CustomerDemographic) Delete(ctx context.Context, db DB) error {
switch {
case !cd._exists: // doesn't exist
return nil
case cd._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM northwind.customer_demographics ` +
`WHERE customer_type_id = ?`
// run
logf(sqlstr, cd.CustomerTypeID)
if _, err := db.ExecContext(ctx, sqlstr, cd.CustomerTypeID); err != nil {
return logerror(err)
}
// set deleted
cd._deleted = true
return nil
}
// CustomerDemographicByCustomerTypeID retrieves a row from 'northwind.customer_demographics' as a CustomerDemographic.
//
// Generated from index 'customer_demographics_customer_type_id_pkey'.
func CustomerDemographicByCustomerTypeID(ctx context.Context, db DB, customerTypeID string) (*CustomerDemographic, error) {
// query
const sqlstr = `SELECT ` +
`customer_type_id, customer_desc ` +
`FROM northwind.customer_demographics ` +
`WHERE customer_type_id = ?`
// run
logf(sqlstr, customerTypeID)
cd := CustomerDemographic{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, customerTypeID).Scan(&cd.CustomerTypeID, &cd.CustomerDesc); err != nil {
return nil, logerror(err)
}
return &cd, nil
}