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
- type CompositeTypeDeclarer
- type ConstantDeclarer
- type Declarer
- type DeclarerSet
- type Emitter
- type EnumTypeDeclarer
- type GenerateOptions
- type ImportPkg
- type ImportSet
- type TemplatedColumn
- type TemplatedFile
- type TemplatedPackage
- type TemplatedParam
- type TemplatedQuery
- func (tq TemplatedQuery) EmitParamNames() string
- func (tq TemplatedQuery) EmitParamStruct() string
- func (tq TemplatedQuery) EmitParams() string
- func (tq TemplatedQuery) EmitPreparedSQL() string
- func (tq TemplatedQuery) EmitResultAssigns(zeroVal string) (string, error)
- func (tq TemplatedQuery) EmitResultDecoders() (string, error)
- func (tq TemplatedQuery) EmitResultElem() (string, error)
- 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
- type Templater
- type TemplaterOpts
- type TypeRegistrationDeclarer
- 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 a hypothetical array init function. Kept for backward compat in name generation only.
Types ¶
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 (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 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 ImportPkg ¶ added in v0.0.3
type ImportPkg struct {
PkgPath string // e.g. "github.com/jackc/pgtype"
Alias string // e.g. "pgtype4", or "" for no alias
}
ImportPkg is a single import entry, optionally with an alias.
func (ImportPkg) FormatImport ¶ added in v0.0.3
FormatImport returns the Go import line content, e.g. `"pkg"` or `alias "pkg"`.
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/mbark/pggen/foo".
func (*ImportSet) AddType ¶
AddType adds all fully qualified package paths needed for type and any child types.
func (*ImportSet) AliasMap ¶ added in v0.0.3
AliasMap returns a map from full package path to the alias that should be used when qualifying types. Returns nil if there are no collisions.
func (*ImportSet) SortedImports ¶ added in v0.0.3
SortedImports returns import entries with aliases assigned where needed to resolve short-name collisions, sorted by package path.
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 []ImportPkg // Go imports, with aliases for collisions
RawImports []string // Go import paths (no aliases), for internal use
// 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 // output columns of the query
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) 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) EmitResultAssigns ¶
func (tq TemplatedQuery) EmitResultAssigns(zeroVal string) (string, error)
EmitResultAssigns writes all the assign statements after scanning the result from pgx. In pgx v5, scanning is direct so no assigns are needed.
func (TemplatedQuery) EmitResultDecoders ¶
func (tq TemplatedQuery) EmitResultDecoders() (string, error)
EmitResultDecoders declares all initialization required for output types. In pgx v5, scanning is direct so no decoders are needed.
func (TemplatedQuery) EmitResultElem ¶
func (tq TemplatedQuery) EmitResultElem() (string, error)
EmitResultElem returns the string representing a single item in the overall query result type. For :one and :exec queries, this is the same as EmitResultType. For :many queries, this is the element type of the slice result type.
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.
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 TypeRegistrationDeclarer ¶ added in v0.0.3
type TypeRegistrationDeclarer struct {
// contains filtered or unexported fields
}
TypeRegistrationDeclarer declares a RegisterTypes function that registers composite and enum types with a pgx v5 connection's TypeMap.
func NewTypeRegistrationDeclarer ¶ added in v0.0.3
func NewTypeRegistrationDeclarer(names []string) TypeRegistrationDeclarer
func (TypeRegistrationDeclarer) Declare ¶ added in v0.0.3
func (t TypeRegistrationDeclarer) Declare(string) (string, error)
func (TypeRegistrationDeclarer) DedupeKey ¶ added in v0.0.3
func (t TypeRegistrationDeclarer) DedupeKey() string
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