forked from xo/xo
/
category.xo.go
154 lines (143 loc) · 4.48 KB
/
category.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 sqlserver contains generated code for schema 'northwind'.
package sqlserver
// Code generated by xo. DO NOT EDIT.
import (
"context"
"database/sql"
)
// Category represents a row from 'northwind.categories'.
type Category struct {
CategoryID int16 `json:"category_id"` // category_id
CategoryName string `json:"category_name"` // category_name
Description sql.NullString `json:"description"` // description
Picture []byte `json:"picture"` // picture
// xo fields
_exists, _deleted bool
}
// Exists returns true when the Category exists in the database.
func (c *Category) Exists() bool {
return c._exists
}
// Deleted returns true when the Category has been marked for deletion from
// the database.
func (c *Category) Deleted() bool {
return c._deleted
}
// Insert inserts the Category to the database.
func (c *Category) 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 northwind.categories (` +
`category_id, category_name, description, picture` +
`) VALUES (` +
`@p1, @p2, @p3, @p4` +
`)`
// run
logf(sqlstr, c.CategoryID, c.CategoryName, c.Description, c.Picture)
if _, err := db.ExecContext(ctx, sqlstr, c.CategoryID, c.CategoryName, c.Description, c.Picture); err != nil {
return logerror(err)
}
// set exists
c._exists = true
return nil
}
// Update updates a Category in the database.
func (c *Category) 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 northwind.categories SET ` +
`category_name = @p1, description = @p2, picture = @p3 ` +
`WHERE category_id = @p4`
// run
logf(sqlstr, c.CategoryName, c.Description, c.Picture, c.CategoryID)
if _, err := db.ExecContext(ctx, sqlstr, c.CategoryName, c.Description, c.Picture, c.CategoryID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the Category to the database.
func (c *Category) 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 Category.
func (c *Category) Upsert(ctx context.Context, db DB) error {
switch {
case c._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `MERGE northwind.categories AS t ` +
`USING (` +
`SELECT @p1 category_id, @p2 category_name, @p3 description, @p4 picture ` +
`) AS s ` +
`ON s.category_id = t.category_id ` +
`WHEN MATCHED THEN ` +
`UPDATE SET ` +
`t.category_name = s.category_name, t.description = s.description, t.picture = s.picture ` +
`WHEN NOT MATCHED THEN ` +
`INSERT (` +
`category_id, category_name, description, picture` +
`) VALUES (` +
`s.category_id, s.category_name, s.description, s.picture` +
`);`
// run
logf(sqlstr, c.CategoryID, c.CategoryName, c.Description, c.Picture)
if _, err := db.ExecContext(ctx, sqlstr, c.CategoryID, c.CategoryName, c.Description, c.Picture); err != nil {
return logerror(err)
}
// set exists
c._exists = true
return nil
}
// Delete deletes the Category from the database.
func (c *Category) 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 northwind.categories ` +
`WHERE category_id = @p1`
// run
logf(sqlstr, c.CategoryID)
if _, err := db.ExecContext(ctx, sqlstr, c.CategoryID); err != nil {
return logerror(err)
}
// set deleted
c._deleted = true
return nil
}
// CategoryByCategoryID retrieves a row from 'northwind.categories' as a Category.
//
// Generated from index 'categories_pkey'.
func CategoryByCategoryID(ctx context.Context, db DB, categoryID int16) (*Category, error) {
// query
const sqlstr = `SELECT ` +
`category_id, category_name, description, picture ` +
`FROM northwind.categories ` +
`WHERE category_id = @p1`
// run
logf(sqlstr, categoryID)
c := Category{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, categoryID).Scan(&c.CategoryID, &c.CategoryName, &c.Description, &c.Picture); err != nil {
return nil, logerror(err)
}
return &c, nil
}