forked from xo/xo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.xo.go
167 lines (154 loc) · 6.49 KB
/
product.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
162
163
164
165
166
167
package sqlite3
// Code generated by xo. DO NOT EDIT.
import (
"context"
"database/sql"
)
// Product represents a row from 'products'.
type Product struct {
ProductID int `json:"product_id"` // product_id
ProductName string `json:"product_name"` // product_name
SupplierID sql.NullInt64 `json:"supplier_id"` // supplier_id
CategoryID sql.NullInt64 `json:"category_id"` // category_id
QuantityPerUnit sql.NullString `json:"quantity_per_unit"` // quantity_per_unit
UnitPrice sql.NullFloat64 `json:"unit_price"` // unit_price
UnitsInStock sql.NullInt64 `json:"units_in_stock"` // units_in_stock
UnitsOnOrder sql.NullInt64 `json:"units_on_order"` // units_on_order
ReorderLevel sql.NullInt64 `json:"reorder_level"` // reorder_level
Discontinued int `json:"discontinued"` // discontinued
// xo fields
_exists, _deleted bool
}
// Exists returns true when the Product exists in the database.
func (p *Product) Exists() bool {
return p._exists
}
// Deleted returns true when the Product has been marked for deletion from
// the database.
func (p *Product) Deleted() bool {
return p._deleted
}
// Insert inserts the Product to the database.
func (p *Product) Insert(ctx context.Context, db DB) error {
switch {
case p._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case p._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (manual)
const sqlstr = `INSERT INTO products (` +
`product_id, product_name, supplier_id, category_id, quantity_per_unit, unit_price, units_in_stock, units_on_order, reorder_level, discontinued` +
`) VALUES (` +
`$1, $2, $3, $4, $5, $6, $7, $8, $9, $10` +
`)`
// run
logf(sqlstr, p.ProductID, p.ProductName, p.SupplierID, p.CategoryID, p.QuantityPerUnit, p.UnitPrice, p.UnitsInStock, p.UnitsOnOrder, p.ReorderLevel, p.Discontinued)
if _, err := db.ExecContext(ctx, sqlstr, p.ProductID, p.ProductName, p.SupplierID, p.CategoryID, p.QuantityPerUnit, p.UnitPrice, p.UnitsInStock, p.UnitsOnOrder, p.ReorderLevel, p.Discontinued); err != nil {
return logerror(err)
}
// set exists
p._exists = true
return nil
}
// Update updates a Product in the database.
func (p *Product) Update(ctx context.Context, db DB) error {
switch {
case !p._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case p._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE products SET ` +
`product_name = $1, supplier_id = $2, category_id = $3, quantity_per_unit = $4, unit_price = $5, units_in_stock = $6, units_on_order = $7, reorder_level = $8, discontinued = $9 ` +
`WHERE product_id = $10`
// run
logf(sqlstr, p.ProductName, p.SupplierID, p.CategoryID, p.QuantityPerUnit, p.UnitPrice, p.UnitsInStock, p.UnitsOnOrder, p.ReorderLevel, p.Discontinued, p.ProductID)
if _, err := db.ExecContext(ctx, sqlstr, p.ProductName, p.SupplierID, p.CategoryID, p.QuantityPerUnit, p.UnitPrice, p.UnitsInStock, p.UnitsOnOrder, p.ReorderLevel, p.Discontinued, p.ProductID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the Product to the database.
func (p *Product) Save(ctx context.Context, db DB) error {
if p.Exists() {
return p.Update(ctx, db)
}
return p.Insert(ctx, db)
}
// Upsert performs an upsert for Product.
func (p *Product) Upsert(ctx context.Context, db DB) error {
switch {
case p._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO products (` +
`product_id, product_name, supplier_id, category_id, quantity_per_unit, unit_price, units_in_stock, units_on_order, reorder_level, discontinued` +
`) VALUES (` +
`$1, $2, $3, $4, $5, $6, $7, $8, $9, $10` +
`)` +
` ON CONFLICT (product_id) DO ` +
`UPDATE SET ` +
`product_name = EXCLUDED.product_name, supplier_id = EXCLUDED.supplier_id, category_id = EXCLUDED.category_id, quantity_per_unit = EXCLUDED.quantity_per_unit, unit_price = EXCLUDED.unit_price, units_in_stock = EXCLUDED.units_in_stock, units_on_order = EXCLUDED.units_on_order, reorder_level = EXCLUDED.reorder_level, discontinued = EXCLUDED.discontinued `
// run
logf(sqlstr, p.ProductID, p.ProductName, p.SupplierID, p.CategoryID, p.QuantityPerUnit, p.UnitPrice, p.UnitsInStock, p.UnitsOnOrder, p.ReorderLevel, p.Discontinued)
if _, err := db.ExecContext(ctx, sqlstr, p.ProductID, p.ProductName, p.SupplierID, p.CategoryID, p.QuantityPerUnit, p.UnitPrice, p.UnitsInStock, p.UnitsOnOrder, p.ReorderLevel, p.Discontinued); err != nil {
return logerror(err)
}
// set exists
p._exists = true
return nil
}
// Delete deletes the Product from the database.
func (p *Product) Delete(ctx context.Context, db DB) error {
switch {
case !p._exists: // doesn't exist
return nil
case p._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM products ` +
`WHERE product_id = $1`
// run
logf(sqlstr, p.ProductID)
if _, err := db.ExecContext(ctx, sqlstr, p.ProductID); err != nil {
return logerror(err)
}
// set deleted
p._deleted = true
return nil
}
// ProductByProductID retrieves a row from 'products' as a Product.
//
// Generated from index 'sqlite_autoindex_products_1'.
func ProductByProductID(ctx context.Context, db DB, productID int) (*Product, error) {
// query
const sqlstr = `SELECT ` +
`product_id, product_name, supplier_id, category_id, quantity_per_unit, unit_price, units_in_stock, units_on_order, reorder_level, discontinued ` +
`FROM products ` +
`WHERE product_id = $1`
// run
logf(sqlstr, productID)
p := Product{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, productID).Scan(&p.ProductID, &p.ProductName, &p.SupplierID, &p.CategoryID, &p.QuantityPerUnit, &p.UnitPrice, &p.UnitsInStock, &p.UnitsOnOrder, &p.ReorderLevel, &p.Discontinued); err != nil {
return nil, logerror(err)
}
return &p, nil
}
// Category returns the Category associated with the Product's (CategoryID).
//
// Generated from foreign key 'products_category_id_fkey'.
func (p *Product) Category(ctx context.Context, db DB) (*Category, error) {
return CategoryByCategoryID(ctx, db, int(p.CategoryID.Int64))
}
// Supplier returns the Supplier associated with the Product's (SupplierID).
//
// Generated from foreign key 'products_supplier_id_fkey'.
func (p *Product) Supplier(ctx context.Context, db DB) (*Supplier, error) {
return SupplierBySupplierID(ctx, db, int(p.SupplierID.Int64))
}