forked from xo/xo
/
orderdetail.xo.go
161 lines (148 loc) · 4.95 KB
/
orderdetail.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
155
156
157
158
159
160
161
package sqlite3
// Code generated by xo. DO NOT EDIT.
import (
"context"
)
// OrderDetail represents a row from 'order_details'.
type OrderDetail struct {
OrderID int `json:"order_id"` // order_id
ProductID int `json:"product_id"` // product_id
UnitPrice float64 `json:"unit_price"` // unit_price
Quantity int `json:"quantity"` // quantity
Discount float64 `json:"discount"` // discount
// xo fields
_exists, _deleted bool
}
// Exists returns true when the OrderDetail exists in the database.
func (od *OrderDetail) Exists() bool {
return od._exists
}
// Deleted returns true when the OrderDetail has been marked for deletion from
// the database.
func (od *OrderDetail) Deleted() bool {
return od._deleted
}
// Insert inserts the OrderDetail to the database.
func (od *OrderDetail) Insert(ctx context.Context, db DB) error {
switch {
case od._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case od._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (manual)
const sqlstr = `INSERT INTO order_details (` +
`order_id, product_id, unit_price, quantity, discount` +
`) VALUES (` +
`$1, $2, $3, $4, $5` +
`)`
// run
logf(sqlstr, od.OrderID, od.ProductID, od.UnitPrice, od.Quantity, od.Discount)
if _, err := db.ExecContext(ctx, sqlstr, od.OrderID, od.ProductID, od.UnitPrice, od.Quantity, od.Discount); err != nil {
return logerror(err)
}
// set exists
od._exists = true
return nil
}
// Update updates a OrderDetail in the database.
func (od *OrderDetail) Update(ctx context.Context, db DB) error {
switch {
case !od._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case od._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE order_details SET ` +
`unit_price = $1, quantity = $2, discount = $3 ` +
`WHERE order_id = $4 AND product_id = $5`
// run
logf(sqlstr, od.UnitPrice, od.Quantity, od.Discount, od.OrderID, od.ProductID)
if _, err := db.ExecContext(ctx, sqlstr, od.UnitPrice, od.Quantity, od.Discount, od.OrderID, od.ProductID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the OrderDetail to the database.
func (od *OrderDetail) Save(ctx context.Context, db DB) error {
if od.Exists() {
return od.Update(ctx, db)
}
return od.Insert(ctx, db)
}
// Upsert performs an upsert for OrderDetail.
func (od *OrderDetail) Upsert(ctx context.Context, db DB) error {
switch {
case od._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO order_details (` +
`order_id, product_id, unit_price, quantity, discount` +
`) VALUES (` +
`$1, $2, $3, $4, $5` +
`)` +
` ON CONFLICT (order_id, product_id) DO ` +
`UPDATE SET ` +
`unit_price = EXCLUDED.unit_price, quantity = EXCLUDED.quantity, discount = EXCLUDED.discount `
// run
logf(sqlstr, od.OrderID, od.ProductID, od.UnitPrice, od.Quantity, od.Discount)
if _, err := db.ExecContext(ctx, sqlstr, od.OrderID, od.ProductID, od.UnitPrice, od.Quantity, od.Discount); err != nil {
return logerror(err)
}
// set exists
od._exists = true
return nil
}
// Delete deletes the OrderDetail from the database.
func (od *OrderDetail) Delete(ctx context.Context, db DB) error {
switch {
case !od._exists: // doesn't exist
return nil
case od._deleted: // deleted
return nil
}
// delete with composite primary key
const sqlstr = `DELETE FROM order_details ` +
`WHERE order_id = $1 AND product_id = $2`
// run
logf(sqlstr, od.OrderID, od.ProductID)
if _, err := db.ExecContext(ctx, sqlstr, od.OrderID, od.ProductID); err != nil {
return logerror(err)
}
// set deleted
od._deleted = true
return nil
}
// OrderDetailByOrderIDProductID retrieves a row from 'order_details' as a OrderDetail.
//
// Generated from index 'sqlite_autoindex_order_details_1'.
func OrderDetailByOrderIDProductID(ctx context.Context, db DB, orderID, productID int) (*OrderDetail, error) {
// query
const sqlstr = `SELECT ` +
`order_id, product_id, unit_price, quantity, discount ` +
`FROM order_details ` +
`WHERE order_id = $1 AND product_id = $2`
// run
logf(sqlstr, orderID, productID)
od := OrderDetail{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, orderID, productID).Scan(&od.OrderID, &od.ProductID, &od.UnitPrice, &od.Quantity, &od.Discount); err != nil {
return nil, logerror(err)
}
return &od, nil
}
// Order returns the Order associated with the OrderDetail's (OrderID).
//
// Generated from foreign key 'order_details_order_id_fkey'.
func (od *OrderDetail) Order(ctx context.Context, db DB) (*Order, error) {
return OrderByOrderID(ctx, db, od.OrderID)
}
// Product returns the Product associated with the OrderDetail's (ProductID).
//
// Generated from foreign key 'order_details_product_id_fkey'.
func (od *OrderDetail) Product(ctx context.Context, db DB) (*Product, error) {
return ProductByProductID(ctx, db, od.ProductID)
}