-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathquery.sql.go
More file actions
54 lines (47 loc) · 1.31 KB
/
query.sql.go
File metadata and controls
54 lines (47 loc) · 1.31 KB
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
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: query.sql
package querytest
import (
"context"
"database/sql"
)
const getProductStats = `-- name: GetProductStats :one
WITH product_stats AS (SELECT count(*) AS total, avg(price) AS avg_price FROM products) SELECT total, avg_price FROM product_stats;
`
type GetProductStatsRow struct {
Total sql.NullInt64
AvgPrice sql.NullString
}
func (q *Queries) GetProductStats(ctx context.Context) (GetProductStatsRow, error) {
row := q.db.QueryRowContext(ctx, getProductStats)
var i GetProductStatsRow
err := row.Scan(&i.Total, &i.AvgPrice)
return i, err
}
const listExpensiveProducts = `-- name: ListExpensiveProducts :many
WITH expensive AS (SELECT id, name, price FROM products WHERE price > 100) SELECT id, name, price FROM expensive;
`
func (q *Queries) ListExpensiveProducts(ctx context.Context) ([]Product, error) {
rows, err := q.db.QueryContext(ctx, listExpensiveProducts)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Product
for rows.Next() {
var i Product
if err := rows.Scan(&i.ID, &i.Name, &i.Price); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}