sqlcgen

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Author

type Author struct {
	ID           string
	OrgID        string
	Email        string
	Name         string
	PasswordHash string
	CreatedAt    pgtype.Timestamptz
	UpdatedAt    pgtype.Timestamptz
}

type AuthorLeaderboardRow

type AuthorLeaderboardRow struct {
	ID             string
	Name           string
	OrgID          string
	PublishedPosts int64
	OrgRank        int64
}

type DBTX

type DBTX interface {
	Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
	Query(context.Context, string, ...interface{}) (pgx.Rows, error)
	QueryRow(context.Context, string, ...interface{}) pgx.Row
}

type ListPostsParams added in v0.6.0

type ListPostsParams struct {
	OrgID     string
	Status    pgtype.Text
	MinViews  pgtype.Int8
	Search    pgtype.Text
	PageLimit int32
}

type Org

type Org struct {
	ID        string
	Name      string
	Slug      string
	CreatedAt pgtype.Timestamptz
	UpdatedAt pgtype.Timestamptz
}

A tenant. Every other table is scoped to one.

type Post

type Post struct {
	ID          string
	OrgID       string
	AuthorID    string
	Title       string
	Body        string
	Status      string
	ViewCount   int64
	PublishedAt pgtype.Timestamptz
	CreatedAt   pgtype.Timestamptz
	UpdatedAt   pgtype.Timestamptz
	DeletedAt   pgtype.Timestamptz
}

A blog post.

type PostViewsByStatusRow

type PostViewsByStatusRow struct {
	Status     string
	PostCount  int64
	TotalViews int64
}

type Queries

type Queries struct {
	// contains filtered or unexported fields
}

func New

func New(db DBTX) *Queries

func (*Queries) AuthorLeaderboard

func (q *Queries) AuthorLeaderboard(ctx context.Context) ([]AuthorLeaderboardRow, error)

These are deliberately the queries sqlb is bad at.

sqlb's non-goal is static analytical SQL: a window function or a recursive CTE goes through sqlb.Raw, which is an escape hatch rather than a feature (ADR-0009). sqlc types both at compile time, which is its whole guarantee. So they live here, and the filterable list endpoints live in sqlb. A window function: rank authors within their org by published posts. Nothing about this benefits from being expressible as a runtime filter.

func (*Queries) GetAuthor

func (q *Queries) GetAuthor(ctx context.Context, id string) (Author, error)

A plain lookup, included only so the generated Author struct exists for the adoption test to point sqlb.Describe at.

func (*Queries) ListPosts added in v0.6.0

func (q *Queries) ListPosts(ctx context.Context, arg ListPostsParams) ([]Post, error)

The exception, and the one query here that is NOT what sqlc is good at.

This is stage 1 of docs/refactoring-from-sqlc.md: a filterable list endpoint written the only way static SQL can express one. Every optional filter becomes an arm that is always sent and usually means nothing, which is the documented sqlc workaround rather than a strawman — see comparisons.md.

It is here to be replaced, not to be imitated. stage1.go calls it, stage2.go through stage4.go do the same job, and refactor_test.go holds all four to the same answers.

What the shape costs, beyond reading badly:

  • Three predicates reach Postgres on every request, each guarded by a NULL check the planner has to see through. Stage 2 sends only what was asked.
  • The sort is baked in. `ORDER BY published_at DESC` cannot become `ORDER BY view_count ASC` without a second copy of the whole query, so a column the UI can sort by is a query, and n columns times two directions is 2n of them.
  • Nothing here says which columns a *client* may filter on. That decision lives in whatever handler assembles these parameters, so it is reviewable only by reading that handler — where ADR-0006 puts it in the schema.

func (*Queries) PostViewsByStatus

func (q *Queries) PostViewsByStatus(ctx context.Context, orgID string) ([]PostViewsByStatusRow, error)

A grouped aggregate over one org. sqlb can express this with Collect, but the result shape is not the table shape, so the typed guarantee is worth more here than the runtime filterability sqlb trades it for.

func (*Queries) WithTx

func (q *Queries) WithTx(tx pgx.Tx) *Queries

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL