Help us shape the future of Go! Take the 2026 Go Developer Survey

queryfile

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package queryfile reads the annotated .sql files that feed pgc: it splits them into named queries, collects their documentation comments and decodes the per-query annotations (overrides and parameter names). It does not parse SQL - the query body is passed to the server verbatim.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InferParamNames

func InferParamNames(q Query, count int) map[int]string

InferParamNames derives a human name for each $N parameter by scanning the SQL at the token level - no parsing, just the handful of patterns that cover real queries:

  • column = $1 (any comparison or LIKE/ILIKE), in both directions;
  • column IN ($1);
  • INSERT INTO t (a, b) VALUES ($1, $2) - matched by position;
  • LIMIT $1 and OFFSET $2.

Names given explicitly with "-- param: $N name" (already in q.ParamNames) win; anything still unnamed falls back to argN. The result maps 1-based parameter numbers to raw (snake_case) names.

Types

type Override

type Override struct {
	Column string // result column name; empty when Param is set
	Param  int    // $N parameter number; 0 when Column is set
	GoType string // replacement Go type expression, may be empty
	Null   string // "notnull", "nullable" or empty
}

Override adjusts the Go type or nullability of one result column or one $N parameter, from a "-- override:" annotation.

type Query

type Query struct {
	Name    string // exported Go name from the annotation, e.g. GetUser
	Command string // one, many, exec or execrows
	Doc     string // annotation comment lines joined with a space
	SQL     string // the statement body, verbatim, without a trailing ;

	File string // source path, for error messages and headers
	Line int    // 1-based line of the -- name: header

	Overrides  []Override
	ParamNames map[int]string // $N (1-based) to a name from -- param:
}

Query is one named query from a .sql file, still untyped: the type information comes later from the database.

func ParseDir

func ParseDir(dir string) ([]Query, error)

ParseDir parses every *.sql file in dir, in name order.

func ParseFile

func ParseFile(path string, src []byte) ([]Query, error)

ParseFile splits one annotated .sql source into queries.

Jump to

Keyboard shortcuts

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