Documentation
¶
Overview ¶
Package sqlbind parses and compiles typed SQL template sources.
Index ¶
- Constants
- Variables
- func Generate(filename string, source []byte, options GenerateOptions) ([]byte, error)
- func Printer() syntax.BodyPrinter
- func RootPrinter() syntax.RootPrinter
- func ValidateDialect(name string) error
- type BinaryExpr
- type Body
- type CallExpr
- type CheckNode
- type CompileError
- type ConditionalExpr
- type Declaration
- type EnumDecl
- type ExecutorResolver
- type Expr
- type ExpressionNode
- type ExternalDecl
- type Field
- type ForNode
- type GenerateOptions
- type IdentifierExpr
- type IfNode
- type IndexExpr
- type LiteralExpr
- type MemberExpr
- type MessageExpr
- type Module
- type Node
- type Parameter
- type ParseError
- type Position
- type RelationNode
- type TemplateDecl
- type TextNode
- type TypeDecl
- type TypeRef
- type UnaryExpr
- type ValBinding
- type ValNode
Constants ¶
const ( DialectPostgreSQL = "postgresql" DialectMySQL = "mysql" DialectSQLite = "sqlite" )
Dialect names a supported target database. It is the single generation-time value carrying every engine difference, so a target is chosen once instead of per facet.
Variables ¶
var SupportedDialects = []string{DialectPostgreSQL, DialectMySQL, DialectSQLite}
SupportedDialects lists every accepted Dialect value, in the order error messages report them.
Functions ¶
func Generate ¶
func Generate(filename string, source []byte, options GenerateOptions) ([]byte, error)
Generate parses, validates, and compiles a SQL template module to Go.
func Printer ¶ added in v0.3.1
func Printer() syntax.BodyPrinter
Printer returns the SQL body printer, the printing half of the format parser registered by Parse. It implements rule:sql-template-layout.
func RootPrinter ¶ added in v0.3.1
func RootPrinter() syntax.RootPrinter
RootPrinter is the registration the shared module printer needs.
func ValidateDialect ¶ added in v0.2.2
ValidateDialect reports whether name selects a supported database. It is exported so a caller discovering SQL templates can reject a missing or unknown dialect as a configuration error, before generating anything.
Types ¶
type BinaryExpr ¶
type BinaryExpr = syntax.BinaryExpr
type CompileError ¶
func (*CompileError) Error ¶
func (e *CompileError) Error() string
type ConditionalExpr ¶
type ConditionalExpr = syntax.ConditionalExpr
type Declaration ¶
type Declaration = syntax.Declaration
type ExecutorResolver ¶ added in v0.1.6
ExecutorResolver identifies a package function with the signature func(context.Context) (SQLExecutor, error).
type ExpressionNode ¶
type ExpressionNode = syntax.ExpressionNode
type ExternalDecl ¶
type ExternalDecl = syntax.ExternalDecl
type GenerateOptions ¶
type GenerateOptions struct {
Package string
// Dialect selects the target database. It is required: there is no implicit
// default, because a silently assumed dialect emits placeholders the target
// engine rejects.
Dialect string
// ErrorExternals names the external functions whose Go implementation
// returns a trailing error. A non-nil error fails the statement build, so
// such a function may only be called as the whole value of a
// requirement:template-value-binding binding, where the failure has a place
// to go and a name in the source.
//
// The caller discovers this by reading the package's Go sources, so the
// template declaration stays the same either way.
ErrorExternals map[string]bool
// ContextAPI adds <Component>Context wrappers which resolve an executor
// from context.Context while preserving the explicit executor APIs.
ContextAPI bool
// ContextOnly publishes the Context-resolved function under the declared
// component name and makes the executor-taking function unexported. It
// implies ContextAPI and generates no <Component>Context wrapper.
ContextOnly bool
// ExecutorResolver selects a framework-specific Context resolver. A nil
// resolver uses sqlbind.SQLExecutorFromContext. Setting it implies ContextAPI.
ExecutorResolver *ExecutorResolver
// LineDirectives maps each emitted statement line back to the template line
// that produced it, so a compile error and a runtime stack frame both name
// the .tb.sql file rather than the generated Go one.
//
// It is off by default. Turning it on changes the bytes of every generated
// file carrying a statement, and a covered test run reports lines that do
// not exist in the file it names, per rule:line-directive-emission.
LineDirectives bool
// OutputName is the base name of the Go file this output becomes. A mapped
// span ends with a directive naming that file and the line the reader is on,
// and neither is known until the bytes are final.
//
// Leaving it empty returns the output with those directives unresolved, for
// a caller that concatenates several results and resolves the combined file
// itself with generator.ResolveTemplatePositions. A caller writing this
// result as a whole file must name it here.
OutputName string
}
GenerateOptions controls SQL code generation.
type IdentifierExpr ¶
type IdentifierExpr = syntax.IdentifierExpr
type LiteralExpr ¶
type LiteralExpr = syntax.LiteralExpr
type MemberExpr ¶
type MemberExpr = syntax.MemberExpr
type MessageExpr ¶ added in v0.5.13
type MessageExpr = syntax.MessageExpr
type ParseError ¶
type ParseError = syntax.ParseError
type RelationNode ¶
type RelationNode struct {
Kind string `json:"kind"`
Pos Position `json:"pos"`
Name string `json:"name"`
Arguments []Expr `json:"arguments,omitempty"`
Alias string `json:"alias"`
}
RelationNode is a structurally embedded private sql.relation<T> statement. It is emitted into the caller's builder, so placeholders remain globally ordered after runtime conditions are resolved.
func (*RelationNode) NodeType ¶
func (n *RelationNode) NodeType() string
type TemplateDecl ¶
type TemplateDecl = syntax.TemplateDecl
type TextNode ¶
type ValBinding ¶ added in v0.5.10
type ValBinding = syntax.ValBinding