forked from cloudspannerecosystem/yo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fereignitem.yo.go
181 lines (152 loc) · 5.1 KB
/
fereignitem.yo.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Code generated by yo. DO NOT EDIT.
// Package customtypes contains the types.
package customtypes
import (
"context"
"fmt"
"cloud.google.com/go/spanner"
"google.golang.org/grpc/codes"
)
// FereignItem represents a row from 'FereignItems'.
type FereignItem struct {
ID int64 `spanner:"ID" json:"ID"` // ID
ItemID int64 `spanner:"ItemID" json:"ItemID"` // ItemID
Category int64 `spanner:"Category" json:"Category"` // Category
}
func FereignItemPrimaryKeys() []string {
return []string{
"ID",
}
}
func FereignItemColumns() []string {
return []string{
"ID",
"ItemID",
"Category",
}
}
func FereignItemWritableColumns() []string {
return []string{
"ID",
"ItemID",
"Category",
}
}
func (fi *FereignItem) columnsToPtrs(cols []string, customPtrs map[string]interface{}) ([]interface{}, error) {
ret := make([]interface{}, 0, len(cols))
for _, col := range cols {
if val, ok := customPtrs[col]; ok {
ret = append(ret, val)
continue
}
switch col {
case "ID":
ret = append(ret, &fi.ID)
case "ItemID":
ret = append(ret, &fi.ItemID)
case "Category":
ret = append(ret, &fi.Category)
default:
return nil, fmt.Errorf("unknown column: %s", col)
}
}
return ret, nil
}
func (fi *FereignItem) columnsToValues(cols []string) ([]interface{}, error) {
ret := make([]interface{}, 0, len(cols))
for _, col := range cols {
switch col {
case "ID":
ret = append(ret, fi.ID)
case "ItemID":
ret = append(ret, fi.ItemID)
case "Category":
ret = append(ret, fi.Category)
default:
return nil, fmt.Errorf("unknown column: %s", col)
}
}
return ret, nil
}
// newFereignItem_Decoder returns a decoder which reads a row from *spanner.Row
// into FereignItem. The decoder is not goroutine-safe. Don't use it concurrently.
func newFereignItem_Decoder(cols []string) func(*spanner.Row) (*FereignItem, error) {
customPtrs := map[string]interface{}{}
return func(row *spanner.Row) (*FereignItem, error) {
var fi FereignItem
ptrs, err := fi.columnsToPtrs(cols, customPtrs)
if err != nil {
return nil, err
}
if err := row.Columns(ptrs...); err != nil {
return nil, err
}
return &fi, nil
}
}
// Insert returns a Mutation to insert a row into a table. If the row already
// exists, the write or transaction fails.
func (fi *FereignItem) Insert(ctx context.Context) *spanner.Mutation {
values, _ := fi.columnsToValues(FereignItemWritableColumns())
return spanner.Insert("FereignItems", FereignItemWritableColumns(), values)
}
// Update returns a Mutation to update a row in a table. If the row does not
// already exist, the write or transaction fails.
func (fi *FereignItem) Update(ctx context.Context) *spanner.Mutation {
values, _ := fi.columnsToValues(FereignItemWritableColumns())
return spanner.Update("FereignItems", FereignItemWritableColumns(), values)
}
// InsertOrUpdate returns a Mutation to insert a row into a table. If the row
// already exists, it updates it instead. Any column values not explicitly
// written are preserved.
func (fi *FereignItem) InsertOrUpdate(ctx context.Context) *spanner.Mutation {
values, _ := fi.columnsToValues(FereignItemWritableColumns())
return spanner.InsertOrUpdate("FereignItems", FereignItemWritableColumns(), values)
}
// UpdateColumns returns a Mutation to update specified columns of a row in a table.
func (fi *FereignItem) UpdateColumns(ctx context.Context, cols ...string) (*spanner.Mutation, error) {
// add primary keys to columns to update by primary keys
colsWithPKeys := append(cols, FereignItemPrimaryKeys()...)
values, err := fi.columnsToValues(colsWithPKeys)
if err != nil {
return nil, newErrorWithCode(codes.InvalidArgument, "FereignItem.UpdateColumns", "FereignItems", err)
}
return spanner.Update("FereignItems", colsWithPKeys, values), nil
}
// FindFereignItem gets a FereignItem by primary key
func FindFereignItem(ctx context.Context, db YORODB, id int64) (*FereignItem, error) {
key := spanner.Key{id}
row, err := db.ReadRow(ctx, "FereignItems", key, FereignItemColumns())
if err != nil {
return nil, newError("FindFereignItem", "FereignItems", err)
}
decoder := newFereignItem_Decoder(FereignItemColumns())
fi, err := decoder(row)
if err != nil {
return nil, newErrorWithCode(codes.Internal, "FindFereignItem", "FereignItems", err)
}
return fi, nil
}
// ReadFereignItem retrieves multiples rows from FereignItem by KeySet as a slice.
func ReadFereignItem(ctx context.Context, db YORODB, keys spanner.KeySet) ([]*FereignItem, error) {
var res []*FereignItem
decoder := newFereignItem_Decoder(FereignItemColumns())
rows := db.Read(ctx, "FereignItems", keys, FereignItemColumns())
err := rows.Do(func(row *spanner.Row) error {
fi, err := decoder(row)
if err != nil {
return err
}
res = append(res, fi)
return nil
})
if err != nil {
return nil, newErrorWithCode(codes.Internal, "ReadFereignItem", "FereignItems", err)
}
return res, nil
}
// Delete deletes the FereignItem from the database.
func (fi *FereignItem) Delete(ctx context.Context) *spanner.Mutation {
values, _ := fi.columnsToValues(FereignItemPrimaryKeys())
return spanner.Delete("FereignItems", spanner.Key(values))
}