forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.sql.go
More file actions
106 lines (94 loc) · 2.57 KB
/
query.sql.go
File metadata and controls
106 lines (94 loc) · 2.57 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
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
package postgresql
import (
"context"
"fmt" // Ensure fmt is imported
"strings"
)
const getProducts = `-- name: GetProducts :many
SELECT id, name, category, price, is_available, created_at FROM products
WHERE 1=1
`
// GetProductsParams is a placeholder as the function takes optional params directly.
// It's not used by the generated GetProducts function itself but might be useful
// for users if they wanted to wrap the call.
type GetProductsParams struct {
Category interface{} `json:"category"`
MinPrice interface{} `json:"min_price"`
IsAvailable interface{} `json:"is_available"`
}
func (q *Queries) GetProducts(ctx context.Context, category interface{}, minPrice interface{}, isAvailable interface{}) ([]Product, error) {
var sqlBuilder strings.Builder
sqlBuilder.WriteString(getProducts) // Base query
var queryParams []interface{}
// Optional 'Category'
if category != nil {
sqlBuilder.WriteString(" AND category = $")
queryParams = append(queryParams, category)
sqlBuilder.WriteString(fmt.Sprintf("%d", len(queryParams)))
}
// Optional 'MinPrice'
if minPrice != nil {
sqlBuilder.WriteString(" AND price >= $")
queryParams = append(queryParams, minPrice)
sqlBuilder.WriteString(fmt.Sprintf("%d", len(queryParams)))
}
// Optional 'IsAvailable'
if isAvailable != nil {
sqlBuilder.WriteString(" AND is_available = $")
queryParams = append(queryParams, isAvailable)
sqlBuilder.WriteString(fmt.Sprintf("%d", len(queryParams)))
}
rows, err := q.db.Query(ctx, sqlBuilder.String(), queryParams...)
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.Category,
&i.Price,
&i.IsAvailable,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const addProduct = `-- name: AddProduct :one
INSERT INTO products (name, category, price, is_available)
VALUES ($1, $2, $3, $4)
RETURNING id, name, category, price, is_available, created_at
`
type AddProductParams struct {
Name string `json:"name"`
Category string `json:"category"`
Price int32 `json:"price"`
IsAvailable bool `json:"is_available"`
}
func (q *Queries) AddProduct(ctx context.Context, arg AddProductParams) (Product, error) {
row := q.db.QueryRow(ctx, addProduct,
arg.Name,
arg.Category,
arg.Price,
arg.IsAvailable,
)
var i Product
err := row.Scan(
&i.ID,
&i.Name,
&i.Category,
&i.Price,
&i.IsAvailable,
&i.CreatedAt,
)
return i, err
}