Documentation
¶
Index ¶
- func ArrayRegisterFunc(typ *gotype.ArrayType) string
- func CreateCompositeType(pkgPath string, pgt pg.CompositeType, resolver TypeResolver, ...) (gotype.Type, error)
- func Generate(opts GenerateOptions, queryFiles []codegen.QueryFile) error
- func NameCompositeCodecFunc(typ *gotype.CompositeType) string
- func NameCompositeInitFunc(typ *gotype.CompositeType) string
- func NameCompositeRawFunc(typ *gotype.CompositeType) string
- func NameEnumCodecFunc(typ *gotype.EnumType) string
- type ArrayTranscoderDeclarer
- 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) 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) EmitScanColumn(idx int, out TemplatedColumn) (string, error)
- func (tq TemplatedQuery) EmitSingularResultType() string
- func (tq TemplatedQuery) EmitZeroResult() (string, error)
- type Templater
- type TemplaterOpts
- type TypeResolver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArrayRegisterFunc ¶ added in v1.4.0
ArrayRegisterFunc returns the function name that registers the array in the typemap.
func CreateCompositeType ¶
func CreateCompositeType( pkgPath string, pgt pg.CompositeType, resolver TypeResolver, caser casing.Caser, nullable bool, ) (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 NameCompositeCodecFunc ¶ added in v1.1.0
func NameCompositeCodecFunc(typ *gotype.CompositeType) string
NameCompositeCodecFunc returns the function name that creates a pgtype.ValueTranscoder for the composite 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 NameEnumCodecFunc ¶ added in v1.1.0
Types ¶
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 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 (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/robbert229/pggen/foo".
func (*ImportSet) AddType ¶
AddType adds all fully qualified package paths needed for type and any child types.
func (*ImportSet) SortedPackages ¶
SortedPackages returns a new slice containing the sorted packages, suitable for an import statement.
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 []string // 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.
func (TemplatedFile) NeedsNetSupport ¶ added in v1.6.0
func (tf TemplatedFile) NeedsNetSupport() bool
func (TemplatedFile) NeedsVoidSupport ¶ added in v1.4.0
func (tf TemplatedFile) NeedsVoidSupport() bool
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
PkgPath string // full package path, like "github.com/foo/bar"
}
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) 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) 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.
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