/
account.sql.go
305 lines (272 loc) · 7.55 KB
/
account.sql.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.13.0
// source: account.sql
package db
import (
"context"
"database/sql"
"time"
)
const countAccountByUserID = `-- name: CountAccountByUserID :one
select count(id)::int
from account
where user_id = $1
`
func (q *Queries) CountAccountByUserID(ctx context.Context, userID int64) (int32, error) {
row := q.db.QueryRow(ctx, countAccountByUserID, userID)
var column_1 int32
err := row.Scan(&column_1)
return column_1, err
}
const createAccount = `-- name: CreateAccount :exec
insert into account (id, user_id, name, avatar, gender, signature)
values ($1, $2, $3, $4, $5, $6)
`
type CreateAccountParams struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Gender Gender `json:"gender"`
Signature string `json:"signature"`
}
func (q *Queries) CreateAccount(ctx context.Context, arg *CreateAccountParams) error {
_, err := q.db.Exec(ctx, createAccount,
arg.ID,
arg.UserID,
arg.Name,
arg.Avatar,
arg.Gender,
arg.Signature,
)
return err
}
const deleteAccount = `-- name: DeleteAccount :exec
delete
from account
where id = $1
`
func (q *Queries) DeleteAccount(ctx context.Context, id int64) error {
_, err := q.db.Exec(ctx, deleteAccount, id)
return err
}
const deleteAccountsByUserID = `-- name: DeleteAccountsByUserID :many
delete
from account
where user_id = $1
returning id
`
func (q *Queries) DeleteAccountsByUserID(ctx context.Context, userID int64) ([]int64, error) {
rows, err := q.db.Query(ctx, deleteAccountsByUserID, userID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []int64{}
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
return nil, err
}
items = append(items, id)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const existsAccountByID = `-- name: ExistsAccountByID :one
select exists(
select 1
from account
where id = $1
)
`
func (q *Queries) ExistsAccountByID(ctx context.Context, id int64) (bool, error) {
row := q.db.QueryRow(ctx, existsAccountByID, id)
var exists bool
err := row.Scan(&exists)
return exists, err
}
const existsAccountByNameAndUserID = `-- name: ExistsAccountByNameAndUserID :one
select exists(
select 1
from account
where user_id = $1
and name = $2
)
`
type ExistsAccountByNameAndUserIDParams struct {
UserID int64 `json:"user_id"`
Name string `json:"name"`
}
func (q *Queries) ExistsAccountByNameAndUserID(ctx context.Context, arg *ExistsAccountByNameAndUserIDParams) (bool, error) {
row := q.db.QueryRow(ctx, existsAccountByNameAndUserID, arg.UserID, arg.Name)
var exists bool
err := row.Scan(&exists)
return exists, err
}
const getAccountByID = `-- name: GetAccountByID :one
select a.id, a.user_id, a.name, a.avatar, a.gender, a.signature, a.create_at, r.id as relation_id
from (select id, user_id, name, avatar, gender, signature, create_at from account where account.id = $1) a
left join relation r on
r.relation_type = 'friend' and
((r.friend_type).account1_id = a.id and (r.friend_type).account2_id = $2::bigint) or
(r.friend_type).account1_id = $2::bigint and (r.friend_type).account2_id = a.id
limit 1
`
type GetAccountByIDParams struct {
TargetID int64 `json:"target_id"`
SelfID int64 `json:"self_id"`
}
type GetAccountByIDRow struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Gender Gender `json:"gender"`
Signature string `json:"signature"`
CreateAt time.Time `json:"create_at"`
RelationID sql.NullInt64 `json:"relation_id"`
}
func (q *Queries) GetAccountByID(ctx context.Context, arg *GetAccountByIDParams) (*GetAccountByIDRow, error) {
row := q.db.QueryRow(ctx, getAccountByID, arg.TargetID, arg.SelfID)
var i GetAccountByIDRow
err := row.Scan(
&i.ID,
&i.UserID,
&i.Name,
&i.Avatar,
&i.Gender,
&i.Signature,
&i.CreateAt,
&i.RelationID,
)
return &i, err
}
const getAccountsByName = `-- name: GetAccountsByName :many
select a.id, a.name, a.avatar, a.gender, r.id as relation_id, count(*) over () as total
from (select id, name, avatar, gender from account where name like ($3::varchar || '%')) as a
left join relation r on (r.relation_type = 'friend' and
(((r.friend_type).account1_id = a.id and
(r.friend_type).account2_id = $4::bigint) or
(r.friend_type).account2_id = a.id and
(r.friend_type).account1_id = $4::bigint))
limit $1 offset $2
`
type GetAccountsByNameParams struct {
Limit int32 `json:"limit"`
Offset int32 `json:"offset"`
Name string `json:"name"`
AccountID int64 `json:"account_id"`
}
type GetAccountsByNameRow struct {
ID int64 `json:"id"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Gender Gender `json:"gender"`
RelationID sql.NullInt64 `json:"relation_id"`
Total int64 `json:"total"`
}
func (q *Queries) GetAccountsByName(ctx context.Context, arg *GetAccountsByNameParams) ([]*GetAccountsByNameRow, error) {
rows, err := q.db.Query(ctx, getAccountsByName,
arg.Limit,
arg.Offset,
arg.Name,
arg.AccountID,
)
if err != nil {
return nil, err
}
defer rows.Close()
items := []*GetAccountsByNameRow{}
for rows.Next() {
var i GetAccountsByNameRow
if err := rows.Scan(
&i.ID,
&i.Name,
&i.Avatar,
&i.Gender,
&i.RelationID,
&i.Total,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getAccountsByUserID = `-- name: GetAccountsByUserID :many
select id, name, avatar, gender
from account
where user_id = $1
`
type GetAccountsByUserIDRow struct {
ID int64 `json:"id"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Gender Gender `json:"gender"`
}
func (q *Queries) GetAccountsByUserID(ctx context.Context, userID int64) ([]*GetAccountsByUserIDRow, error) {
rows, err := q.db.Query(ctx, getAccountsByUserID, userID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []*GetAccountsByUserIDRow{}
for rows.Next() {
var i GetAccountsByUserIDRow
if err := rows.Scan(
&i.ID,
&i.Name,
&i.Avatar,
&i.Gender,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateAccount = `-- name: UpdateAccount :exec
update account
set name = $1,
gender = $2,
signature = $3
where id = $4
`
type UpdateAccountParams struct {
Name string `json:"name"`
Gender Gender `json:"gender"`
Signature string `json:"signature"`
ID int64 `json:"id"`
}
func (q *Queries) UpdateAccount(ctx context.Context, arg *UpdateAccountParams) error {
_, err := q.db.Exec(ctx, updateAccount,
arg.Name,
arg.Gender,
arg.Signature,
arg.ID,
)
return err
}
const updateAccountAvatar = `-- name: UpdateAccountAvatar :exec
update account
set avatar = $1
where id = $2
`
type UpdateAccountAvatarParams struct {
Avatar string `json:"avatar"`
ID int64 `json:"id"`
}
func (q *Queries) UpdateAccountAvatar(ctx context.Context, arg *UpdateAccountAvatarParams) error {
_, err := q.db.Exec(ctx, updateAccountAvatar, arg.Avatar, arg.ID)
return err
}