Skip to content

Instantly share code, notes, and snippets.

@boxabirds
Created December 15, 2024 12:13
Show Gist options
  • Save boxabirds/212e527103cc624fdcd21f09c968ff54 to your computer and use it in GitHub Desktop.
Save boxabirds/212e527103cc624fdcd21f09c968ff54 to your computer and use it in GitHub Desktop.
sqlc column name mapping question
SQL:
-- name: GetRecentPostsByTags :many
-- $1: TagNames
SELECT DISTINCT p.id, p.created_at, t.name AS tag_name
FROM posts p
JOIN post_tags pt ON p.id = pt.post_id
JOIN tags t ON pt.tag_id = t.id
WHERE t.name = ANY($1::text[]) AND p.created_at < $2
ORDER BY p.created_at DESC
LIMIT $3 OFFSET $4;
generated sqlc: see the annoying "Column1"
type GetRecentPostsByTagsParams struct {
Column1 []string
CreatedAt time.Time
Limit int32
Offset int32
}
type GetRecentPostsByTagsRow struct {
ID int32
CreatedAt time.Time
TagName string
}
// $1: TagNames
func (q *Queries) GetRecentPostsByTags(ctx context.Context, arg GetRecentPostsByTagsParams) ([]GetRecentPostsByTagsRow, error) {
rows, err := q.db.QueryContext(ctx, getRecentPostsByTags,
pq.Array(arg.Column1),
arg.CreatedAt,
arg.Limit,
arg.Offset,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetRecentPostsByTagsRow
for rows.Next() {
var i GetRecentPostsByTagsRow
if err := rows.Scan(&i.ID, &i.CreatedAt, &i.TagName); 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment