forked from xo/xo
/
customer.xo.go
154 lines (143 loc) · 5.62 KB
/
customer.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
153
154
package sqlite3
// Code generated by xo. DO NOT EDIT.
import (
"context"
"database/sql"
)
// Customer represents a row from 'customers'.
type Customer struct {
CustomerID string `json:"customer_id"` // customer_id
CompanyName string `json:"company_name"` // company_name
ContactName sql.NullString `json:"contact_name"` // contact_name
ContactTitle sql.NullString `json:"contact_title"` // contact_title
Address sql.NullString `json:"address"` // address
City sql.NullString `json:"city"` // city
Region sql.NullString `json:"region"` // region
PostalCode sql.NullString `json:"postal_code"` // postal_code
Country sql.NullString `json:"country"` // country
Phone sql.NullString `json:"phone"` // phone
Fax sql.NullString `json:"fax"` // fax
// xo fields
_exists, _deleted bool
}
// Exists returns true when the Customer exists in the database.
func (c *Customer) Exists() bool {
return c._exists
}
// Deleted returns true when the Customer has been marked for deletion from
// the database.
func (c *Customer) Deleted() bool {
return c._deleted
}
// Insert inserts the Customer to the database.
func (c *Customer) Insert(ctx context.Context, db DB) error {
switch {
case c._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case c._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (manual)
const sqlstr = `INSERT INTO customers (` +
`customer_id, company_name, contact_name, contact_title, address, city, region, postal_code, country, phone, fax` +
`) VALUES (` +
`$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11` +
`)`
// run
logf(sqlstr, c.CustomerID, c.CompanyName, c.ContactName, c.ContactTitle, c.Address, c.City, c.Region, c.PostalCode, c.Country, c.Phone, c.Fax)
if _, err := db.ExecContext(ctx, sqlstr, c.CustomerID, c.CompanyName, c.ContactName, c.ContactTitle, c.Address, c.City, c.Region, c.PostalCode, c.Country, c.Phone, c.Fax); err != nil {
return logerror(err)
}
// set exists
c._exists = true
return nil
}
// Update updates a Customer in the database.
func (c *Customer) Update(ctx context.Context, db DB) error {
switch {
case !c._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case c._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE customers SET ` +
`company_name = $1, contact_name = $2, contact_title = $3, address = $4, city = $5, region = $6, postal_code = $7, country = $8, phone = $9, fax = $10 ` +
`WHERE customer_id = $11`
// run
logf(sqlstr, c.CompanyName, c.ContactName, c.ContactTitle, c.Address, c.City, c.Region, c.PostalCode, c.Country, c.Phone, c.Fax, c.CustomerID)
if _, err := db.ExecContext(ctx, sqlstr, c.CompanyName, c.ContactName, c.ContactTitle, c.Address, c.City, c.Region, c.PostalCode, c.Country, c.Phone, c.Fax, c.CustomerID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the Customer to the database.
func (c *Customer) Save(ctx context.Context, db DB) error {
if c.Exists() {
return c.Update(ctx, db)
}
return c.Insert(ctx, db)
}
// Upsert performs an upsert for Customer.
func (c *Customer) Upsert(ctx context.Context, db DB) error {
switch {
case c._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO customers (` +
`customer_id, company_name, contact_name, contact_title, address, city, region, postal_code, country, phone, fax` +
`) VALUES (` +
`$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11` +
`)` +
` ON CONFLICT (customer_id) DO ` +
`UPDATE SET ` +
`company_name = EXCLUDED.company_name, contact_name = EXCLUDED.contact_name, contact_title = EXCLUDED.contact_title, address = EXCLUDED.address, city = EXCLUDED.city, region = EXCLUDED.region, postal_code = EXCLUDED.postal_code, country = EXCLUDED.country, phone = EXCLUDED.phone, fax = EXCLUDED.fax `
// run
logf(sqlstr, c.CustomerID, c.CompanyName, c.ContactName, c.ContactTitle, c.Address, c.City, c.Region, c.PostalCode, c.Country, c.Phone, c.Fax)
if _, err := db.ExecContext(ctx, sqlstr, c.CustomerID, c.CompanyName, c.ContactName, c.ContactTitle, c.Address, c.City, c.Region, c.PostalCode, c.Country, c.Phone, c.Fax); err != nil {
return logerror(err)
}
// set exists
c._exists = true
return nil
}
// Delete deletes the Customer from the database.
func (c *Customer) Delete(ctx context.Context, db DB) error {
switch {
case !c._exists: // doesn't exist
return nil
case c._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM customers ` +
`WHERE customer_id = $1`
// run
logf(sqlstr, c.CustomerID)
if _, err := db.ExecContext(ctx, sqlstr, c.CustomerID); err != nil {
return logerror(err)
}
// set deleted
c._deleted = true
return nil
}
// CustomerByCustomerID retrieves a row from 'customers' as a Customer.
//
// Generated from index 'sqlite_autoindex_customers_1'.
func CustomerByCustomerID(ctx context.Context, db DB, customerID string) (*Customer, error) {
// query
const sqlstr = `SELECT ` +
`customer_id, company_name, contact_name, contact_title, address, city, region, postal_code, country, phone, fax ` +
`FROM customers ` +
`WHERE customer_id = $1`
// run
logf(sqlstr, customerID)
c := Customer{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, customerID).Scan(&c.CustomerID, &c.CompanyName, &c.ContactName, &c.ContactTitle, &c.Address, &c.City, &c.Region, &c.PostalCode, &c.Country, &c.Phone, &c.Fax); err != nil {
return nil, logerror(err)
}
return &c, nil
}