Documentation ¶
Index ¶
- func CreateCompositeType(pkgPath string, pgt pg.CompositeType, resolver TypeResolver, ...) (gotype.Type, error)
- func Generate(opts GenerateOptions, queryFiles []codegen.QueryFile) error
- func NameArrayInitFunc(typ *gotype.ArrayType) string
- func NameArrayRawFunc(typ *gotype.ArrayType) string
- func NameArrayTranscoderFunc(typ *gotype.ArrayType) string
- func NameCompositeInitFunc(typ *gotype.CompositeType) string
- func NameCompositeRawFunc(typ *gotype.CompositeType) string
- func NameCompositeTranscoderFunc(typ *gotype.CompositeType) string
- func NameEnumTranscoderFunc(typ *gotype.EnumType) string
- type ArrayInitDeclarer
- type ArrayRawDeclarer
- type ArrayTranscoderDeclarer
- type CompositeInitDeclarer
- type CompositeRawDeclarer
- type CompositeTranscoderDeclarer
- type CompositeTypeDeclarer
- type ConstantDeclarer
- type Declarer
- type DeclarerSet
- type Emitter
- type EnumTranscoderDeclarer
- type EnumTypeDeclarer
- type GenerateOptions
- type ImportSet
- type TemplatedColumn
- type TemplatedFile
- type TemplatedPackage
- type TemplatedParam
- type TemplatedQuery
- func (tq TemplatedQuery) EmitCollectionFunc() (string, error)
- func (tq TemplatedQuery) EmitParamNames() string
- func (tq TemplatedQuery) EmitParamStruct() string
- func (tq TemplatedQuery) EmitParams() string
- func (tq TemplatedQuery) EmitPlanScan(idx int, out TemplatedColumn) (string, error)
- func (tq TemplatedQuery) EmitPreparedSQL() string
- func (tq TemplatedQuery) EmitResultExpr(name string) (string, error)
- func (tq TemplatedQuery) EmitResultType() (string, error)
- func (tq TemplatedQuery) EmitResultTypeInit(name string) (string, error)
- func (tq TemplatedQuery) EmitRowScanArgs() (string, error)
- func (tq TemplatedQuery) EmitRowStruct() string
- func (tq TemplatedQuery) EmitRowToFunc() (string, error)
- func (tq TemplatedQuery) EmitScanColumn(idx int, out TemplatedColumn) (string, error)
- func (tq TemplatedQuery) EmitSingularResultType() string
- func (tq TemplatedQuery) EmitZeroResult() (string, error)
- func (tq TemplatedQuery) Executable() bool
- type Templater
- type TemplaterOpts
- type TypeResolver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateCompositeType ¶
func CreateCompositeType( pkgPath string, pgt pg.CompositeType, resolver TypeResolver, caser casing.Caser, ) (gotype.Type, error)
CreateCompositeType creates a struct to represent a Postgres composite type. The type is rooted under pkgPath.
func Generate ¶
func Generate(opts GenerateOptions, queryFiles []codegen.QueryFile) error
Generate emits generated Go files for each of the queryFiles.
func NameArrayInitFunc ¶
NameArrayInitFunc returns the name for the function that creates an initialized pgtype.ValueTranscoder for the array type that's used to encode query parameters. This function is only necessary for top-level types. Descendant types use the raw functions, named by NameArrayRawFunc.
func NameArrayRawFunc ¶
NameArrayRawFunc returns the function name that create the []interface{} array for the array type so that we can use it with a parent encoder function, like NameCompositeInitFunc, in the pgtype.Value Set call.
func NameArrayTranscoderFunc ¶
NameArrayTranscoderFunc returns the function name that creates a pgtype.ValueTranscoder for the array type that's used to decode rows returned by Postgres.
func NameCompositeInitFunc ¶
func NameCompositeInitFunc(typ *gotype.CompositeType) string
NameCompositeInitFunc returns the name of the function that creates an initialized pgtype.ValueTranscoder for the composite type used as a query parameters. This function is only necessary for top-level types. Descendant types use the raw functions, named by NameCompositeRawFunc.
func NameCompositeRawFunc ¶
func NameCompositeRawFunc(typ *gotype.CompositeType) string
NameCompositeRawFunc returns the function name that creates the []interface{} array for the composite type so that we can use it with a parent encoder function, like NameCompositeInitFunc, in the pgtype.Value Set call.
func NameCompositeTranscoderFunc ¶
func NameCompositeTranscoderFunc(typ *gotype.CompositeType) string
NameCompositeTranscoderFunc returns the function name that creates a pgtype.ValueTranscoder for the composite type that's used to decode rows returned by Postgres.
func NameEnumTranscoderFunc ¶
Types ¶
type ArrayInitDeclarer ¶
type ArrayInitDeclarer struct {
// contains filtered or unexported fields
}
ArrayInitDeclarer declares a new Go function that creates an *initialized* pgtype.ValueTranscoder for the Postgres type represented by the gotype.ArrayType.
We need a separate declarer from ArrayTranscoderDeclarer because setting a pgtype.ValueTranscoder is much less flexible on the values allowed compared to AssignTo. We can assign a pgtype.ArrayType to any struct but we can only set it with an [][]interface{} if the array elements are composite types.
Additionally, we need to use the Postgres text format exclusively because the Postgres binary format requires the type OID but pggen doesn't necessarily know the OIDs of the types. The text format, however, doesn't require OIDs.
func NewArrayInitDeclarer ¶
func NewArrayInitDeclarer(typ *gotype.ArrayType) ArrayInitDeclarer
func (ArrayInitDeclarer) DedupeKey ¶
func (a ArrayInitDeclarer) DedupeKey() string
type ArrayRawDeclarer ¶
type ArrayRawDeclarer struct {
// contains filtered or unexported fields
}
ArrayRawDeclarer declares a new Go function that returns all fields as a generic array: []interface{}. Necessary because we can only set pgtype.ArrayType from a []interface{}.
func NewArrayRawDeclarer ¶
func NewArrayRawDeclarer(typ *gotype.ArrayType) ArrayRawDeclarer
func (ArrayRawDeclarer) DedupeKey ¶
func (a ArrayRawDeclarer) DedupeKey() string
type ArrayTranscoderDeclarer ¶
type ArrayTranscoderDeclarer struct {
// contains filtered or unexported fields
}
ArrayTranscoderDeclarer declares a new Go function that creates a pgtype.ValueTranscoder decoder for an array Postgres type.
func NewArrayDecoderDeclarer ¶
func NewArrayDecoderDeclarer(typ *gotype.ArrayType) ArrayTranscoderDeclarer
func (ArrayTranscoderDeclarer) Declare ¶
func (a ArrayTranscoderDeclarer) Declare(string) (string, error)
func (ArrayTranscoderDeclarer) DedupeKey ¶
func (a ArrayTranscoderDeclarer) DedupeKey() string
type CompositeInitDeclarer ¶
type CompositeInitDeclarer struct {
// contains filtered or unexported fields
}
CompositeInitDeclarer declares a new Go function that creates an initialized pgtype.ValueTranscoder for the Postgres type represented by the gotype.CompositeType.
We need a separate encoder because setting a pgtype.ValueTranscoder is much less flexible on the values allowed compared to AssignTo. We can assign a pgtype.CompositeType to any struct but we can only set it with an []interface{}.
Additionally, we need to use the Postgres text format exclusively because the Postgres binary format requires the type OID but pggen doesn't necessarily know the OIDs of the types. The text format, however, doesn't require OIDs.
func NewCompositeInitDeclarer ¶
func NewCompositeInitDeclarer(typ *gotype.CompositeType) CompositeInitDeclarer
func (CompositeInitDeclarer) Declare ¶
func (c CompositeInitDeclarer) Declare(string) (string, error)
func (CompositeInitDeclarer) DedupeKey ¶
func (c CompositeInitDeclarer) DedupeKey() string
type CompositeRawDeclarer ¶
type CompositeRawDeclarer struct {
// contains filtered or unexported fields
}
CompositeRawDeclarer declares a new Go function that returns all fields of a composite type as a generic array: []interface{}. Necessary because we can only set pgtype.CompositeType from a []interface{}.
Revisit after https://github.com/jackc/pgx/v5/pgtype/pull/100 to see if we can simplify.
func NewCompositeRawDeclarer ¶
func NewCompositeRawDeclarer(typ *gotype.CompositeType) CompositeRawDeclarer
func (CompositeRawDeclarer) DedupeKey ¶
func (c CompositeRawDeclarer) DedupeKey() string
type CompositeTranscoderDeclarer ¶
type CompositeTranscoderDeclarer struct {
// contains filtered or unexported fields
}
CompositeTranscoderDeclarer declares a new Go function that creates a pgx decoder for the Postgres type represented by the gotype.CompositeType.
func NewCompositeTranscoderDeclarer ¶
func NewCompositeTranscoderDeclarer(typ *gotype.CompositeType) CompositeTranscoderDeclarer
func (CompositeTranscoderDeclarer) Declare ¶
func (c CompositeTranscoderDeclarer) Declare(pkgPath string) (string, error)
func (CompositeTranscoderDeclarer) DedupeKey ¶
func (c CompositeTranscoderDeclarer) DedupeKey() string
type CompositeTypeDeclarer ¶
type CompositeTypeDeclarer struct {
// contains filtered or unexported fields
}
CompositeTypeDeclarer declares a new Go struct to represent a Postgres composite type.
func NewCompositeTypeDeclarer ¶
func NewCompositeTypeDeclarer(comp *gotype.CompositeType) CompositeTypeDeclarer
func (CompositeTypeDeclarer) Declare ¶
func (c CompositeTypeDeclarer) Declare(pkgPath string) (string, error)
func (CompositeTypeDeclarer) DedupeKey ¶
func (c CompositeTypeDeclarer) DedupeKey() string
type ConstantDeclarer ¶
type ConstantDeclarer struct {
// contains filtered or unexported fields
}
ConstantDeclarer declares a new string literal.
func NewConstantDeclarer ¶
func NewConstantDeclarer(key, str string) ConstantDeclarer
func NewTypeResolverDeclarer ¶
func NewTypeResolverDeclarer() ConstantDeclarer
NewTypeResolverDeclarer declares type resolver body code sometimes needed.
func NewTypeResolverInitDeclarer ¶
func NewTypeResolverInitDeclarer() ConstantDeclarer
NewTypeResolverInitDeclarer declare type resolver init code always needed.
func (ConstantDeclarer) DedupeKey ¶
func (c ConstantDeclarer) DedupeKey() string
type Declarer ¶
type Declarer interface { // DedupeKey uniquely identifies the declaration so that we only emit // declarations once. Should be namespaced like enum::some_enum. DedupeKey() string // Declare returns the string of the Go code for the declaration. Declare(pkgPath string) (string, error) }
Declarer is implemented by any value that needs to declare types, data, or functions before use. For example, Postgres enums map to a Go enum with a type declaration and const values. If we use the enum in any Querier function, we need to declare the enum.
type DeclarerSet ¶
DeclarerSet is a set of declarers, identified by the dedupe key.
func FindInputDeclarers ¶
func FindInputDeclarers(typ gotype.Type) DeclarerSet
FindInputDeclarers finds all necessary Declarers for types that appear in the input parameters. Returns nil if no declarers are needed.
func FindOutputDeclarers ¶
func FindOutputDeclarers(typ gotype.Type) DeclarerSet
FindOutputDeclarers finds all necessary Declarers for types that appear in the output rows. Returns nil if no declarers are needed.
func NewDeclarerSet ¶
func NewDeclarerSet(decls ...Declarer) DeclarerSet
func (DeclarerSet) AddAll ¶
func (d DeclarerSet) AddAll(decls ...Declarer)
func (DeclarerSet) ListAll ¶
func (d DeclarerSet) ListAll() []Declarer
ListAll gets all declarers in the set in a stable sort order.
type Emitter ¶
type Emitter struct {
// contains filtered or unexported fields
}
Emitter writes a templated query file to a file.
func (Emitter) EmitAllQueryFiles ¶
func (em Emitter) EmitAllQueryFiles(tfs []TemplatedFile) (mErr error)
EmitAllQueryFiles emits a query file for each TemplatedFile. Ensure that emitted files don't clash by prefixing with the parent directory if necessary.
type EnumTranscoderDeclarer ¶
type EnumTranscoderDeclarer struct {
// contains filtered or unexported fields
}
EnumTranscoderDeclarer declares a new Go function that creates a pgx decoder for the Postgres type represented by the gotype.EnumType.
func NewEnumTranscoderDeclarer ¶
func NewEnumTranscoderDeclarer(enum *gotype.EnumType) EnumTranscoderDeclarer
func (EnumTranscoderDeclarer) Declare ¶
func (e EnumTranscoderDeclarer) Declare(string) (string, error)
func (EnumTranscoderDeclarer) DedupeKey ¶
func (e EnumTranscoderDeclarer) DedupeKey() string
type EnumTypeDeclarer ¶
type EnumTypeDeclarer struct {
// contains filtered or unexported fields
}
EnumTypeDeclarer declares a new string type and the const values to map to a Postgres enum.
func NewEnumTypeDeclarer ¶
func NewEnumTypeDeclarer(enum *gotype.EnumType) EnumTypeDeclarer
func (EnumTypeDeclarer) DedupeKey ¶
func (e EnumTypeDeclarer) DedupeKey() string
type GenerateOptions ¶
type GenerateOptions struct { GoPkg string OutputDir string // A map of lowercase acronyms to the upper case equivalent, like: // "api" => "API". Acronyms map[string]string // A map from a Postgres type name to a fully qualified Go type. TypeOverrides map[string]string // How many params to inline when calling querier methods. // Set to 0 to always create a struct for params. InlineParamCount int }
GenerateOptions are options to control generated Go output.
type ImportSet ¶
type ImportSet struct {
// contains filtered or unexported fields
}
ImportSet contains a set of imports required by one Go file.
func NewImportSet ¶
func NewImportSet() *ImportSet
func (*ImportSet) AddPackage ¶
AddPackage adds a fully qualified package path to the set, like "github.com/mypricehealth/pggen/foo".
func (*ImportSet) AddType ¶
AddType adds all fully qualified package paths needed for type and any child types.
func (*ImportSet) Get ¶
Get returns a new slice containing the packages, suitable for an import statement.
func (*ImportSet) RemovePackage ¶
AddPackage removes a fully qualified package path from the set, like "github.com/mypricehealth/pggen/foo".
type TemplatedColumn ¶
type TemplatedColumn struct { PgName string // original name of the Postgres column UpperName string // name in Go-style (UpperCamelCase) to use for the column LowerName string // name in Go-style (lowerCamelCase) Type gotype.Type QualType string // package qualified Go type to use for the column, like "pgtype.Text" }
type TemplatedFile ¶
type TemplatedFile struct { Pkg TemplatedPackage // the parent package containing this file PkgPath string // full package path, like "github.com/foo/bar" GoPkg string // the name of the Go package to use for the "package foo" declaration SourcePath string // absolute path to source SQL file Queries []TemplatedQuery // the queries with all template information Imports *ImportSet // Go imports // True if this file is the leader file. The leader defines common code used // by all queries in the same directory. Only one leader per directory. IsLeader bool // Any declarations this file should declare. Only set on leader. Declarers []Declarer }
TemplatedFile is the Go version of a SQL query file with all information needed to execute the codegen template.
type TemplatedPackage ¶
type TemplatedPackage struct {
Files []TemplatedFile // sorted lexicographically by path
}
TemplatedPackage is all templated files in a pggen invocation. The templated files do not necessarily reside in the same directory.
type TemplatedParam ¶
type TemplatedParam struct { UpperName string // name of the param in UpperCamelCase, like 'FirstName' from pggen.arg('first_name') LowerName string // name of the param in lowerCamelCase, like 'firstName' from pggen.arg('first_name') QualType string // package-qualified Go type to use for this param Type gotype.Type RawName pginfer.InputParam }
type TemplatedQuery ¶
type TemplatedQuery struct { Name string // name of the query, from the comment preceding the query SQLVarName string // name of the string variable containing the SQL ResultKind ast.ResultKind // kind of result: :one, :many, or :exec Doc string // doc from the source query file, formatted for Go PreparedSQL string // SQL query, ready to run with PREPARE statement Inputs []TemplatedParam // input parameters to the query Outputs []TemplatedColumn // non-void output columns of the query ScanCols []TemplatedColumn // all columns of the query, including void columns InlineParamCount int // inclusive count of params that will be inlined }
TemplatedQuery is a query with all information required to execute the codegen template.
func (TemplatedQuery) EmitCollectionFunc ¶
func (tq TemplatedQuery) EmitCollectionFunc() (string, error)
func (TemplatedQuery) EmitParamNames ¶
func (tq TemplatedQuery) EmitParamNames() string
EmitParamNames emits the TemplatedQuery.Inputs into comma separated names for use in a method invocation.
func (TemplatedQuery) EmitParamStruct ¶
func (tq TemplatedQuery) EmitParamStruct() string
EmitParamStruct emits the struct definition for query params if needed.
func (TemplatedQuery) EmitParams ¶
func (tq TemplatedQuery) EmitParams() string
EmitParams emits the TemplatedQuery.Inputs into method parameters with both a name and type based on the number of params. For use in a method definition.
func (TemplatedQuery) EmitPlanScan ¶
func (tq TemplatedQuery) EmitPlanScan(idx int, out TemplatedColumn) (string, error)
EmitPlanScan emits the variable that hold the pgtype.ScanPlan for a query output.
func (TemplatedQuery) EmitPreparedSQL ¶
func (tq TemplatedQuery) EmitPreparedSQL() string
EmitPreparedSQL emits the prepared SQL query with appropriate quoting.
func (TemplatedQuery) EmitResultExpr ¶
func (tq TemplatedQuery) EmitResultExpr(name string) (string, error)
EmitResultExpr returns the string representation of a single item to return for :one queries or to append for :many queries. Useful for figuring out if we need to use the address operator. Controls the string item and &item in:
items = append(items, item) items = append(items, &item)
func (TemplatedQuery) EmitResultType ¶
func (tq TemplatedQuery) EmitResultType() (string, error)
EmitResultType returns the string representing the overall query result type, meaning the return result.
func (TemplatedQuery) EmitResultTypeInit ¶
func (tq TemplatedQuery) EmitResultTypeInit(name string) (string, error)
EmitResultTypeInit returns the initialization code for the result type with name, typically "item" or "items". For array types, we take care to not use a var declaration so that JSON serialization returns an empty array instead of null.
func (TemplatedQuery) EmitRowScanArgs ¶
func (tq TemplatedQuery) EmitRowScanArgs() (string, error)
EmitRowScanArgs emits the args to scan a single row from a pgx.Row or pgx.Rows.
func (TemplatedQuery) EmitRowStruct ¶
func (tq TemplatedQuery) EmitRowStruct() string
EmitRowStruct writes the struct definition for query output row if one is needed.
func (TemplatedQuery) EmitRowToFunc ¶
func (tq TemplatedQuery) EmitRowToFunc() (string, error)
func (TemplatedQuery) EmitScanColumn ¶
func (tq TemplatedQuery) EmitScanColumn(idx int, out TemplatedColumn) (string, error)
EmitScanColumn emits scan call for a single TemplatedColumn.
func (TemplatedQuery) EmitSingularResultType ¶
func (tq TemplatedQuery) EmitSingularResultType() string
EmitSingularResultType returns the string representing a single element of the overall query result type, like FindAuthorsRow when the overall return type is []FindAuthorsRow.
func (TemplatedQuery) EmitZeroResult ¶
func (tq TemplatedQuery) EmitZeroResult() (string, error)
EmitZeroResult returns the string representing the zero value of a result.
func (TemplatedQuery) Executable ¶
func (tq TemplatedQuery) Executable() bool
type Templater ¶
type Templater struct {
// contains filtered or unexported fields
}
Templater creates query file templates.
func NewTemplater ¶
func NewTemplater(opts TemplaterOpts) Templater
func (Templater) TemplateAll ¶
func (tm Templater) TemplateAll(files []codegen.QueryFile) ([]TemplatedFile, error)
TemplateAll creates query template files for each codegen.QueryFile.
type TemplaterOpts ¶
type TemplaterOpts struct { Caser casing.Caser Resolver TypeResolver Pkg string // Go package name // How many params to inline when calling querier methods. InlineParamCount int }
TemplaterOpts is options to control the template logic.
type TypeResolver ¶
type TypeResolver struct {
// contains filtered or unexported fields
}
TypeResolver handles the mapping between Postgres and Go types.
func NewTypeResolver ¶
func NewTypeResolver(c casing.Caser, overrides map[string]string) TypeResolver