Documentation ¶
Overview ¶
Package render provides the mechanism for rendering ast into SQL.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendSQL ¶ added in v0.44.0
AppendSQL is a convenience function for building the SQL string. The main purpose is to ensure that there's always a consistent amount of whitespace. Thus, if existing has a space suffix and add has a space prefix, the returned string will only have one space. If add is the empty string or just whitespace, this function simply returns existing.
Types ¶
type Context ¶
type Context struct { // Renderer holds the rendering functions. Renderer *Renderer // The args map contains predefined variables that are // substituted into the query. It may be empty or nil. Args map[string]string // Fragments is the set of fragments that are rendered into // a SQL query. It may not be initialized until late in // the day. Fragments *Fragments // Dialect is the driver dialect. Dialect dialect.Dialect }
Context contains context for rendering a query.
type Fragments ¶
type Fragments struct { Distinct string Columns string From string Where string GroupBy string Having string OrderBy string Range string // PreExecStmts are statements that are executed before the query. // These can be used for edge-case behavior, such as setting up // variables in the session. // // See also: Fragments.PostExecStmts. PreExecStmts []string // PostExecStmts are statements that are executed after the query. // // See also: Fragments.PreExecStmts. PostExecStmts []string }
Fragments holds the fragments of a SQL query. It is passed to Renderer.PreRender and Renderer.Render.
type Renderer ¶
type Renderer struct { // FromTable renders a FROM table fragment. FromTable func(rc *Context, tblSel *ast.TblSelectorNode) (string, error) // SelectCols renders a column names/expression fragment. // It shouldn't render the actual SELECT keyword. Example return value: // // "first_name" AS "given_name", "last name" AS "family_name" SelectCols func(rc *Context, cols []ast.ResultColumn) (string, error) // Range renders a row range fragment. Range func(rc *Context, rr *ast.RowRangeNode) (string, error) // OrderBy renders the ORDER BY fragment. OrderBy func(rc *Context, ob *ast.OrderByNode) (string, error) // GroupBy renders the GROUP BY fragment. GroupBy func(rc *Context, gb *ast.GroupByNode) (string, error) // Having renders the HAVING fragment. Having func(rc *Context, having *ast.HavingNode) (string, error) // Join renders a join fragment. Join func(rc *Context, leftTbl *ast.TblSelectorNode, joins []*ast.JoinNode) (string, error) // Function renders a function fragment. Function func(rc *Context, fn *ast.FuncNode) (string, error) // FunctionNames is a map of SLQ function name to SQL function name. // It can be used by the Renderer.Function impl. Note that FunctionOverrides // has precedence over FunctionNames. FunctionNames map[string]string // FunctionOverrides is a map of SLQ function name to a custom // function to render that function. It can be used by the Renderer.Function // imp. FunctionOverrides has precedence over FunctionNames. FunctionOverrides map[string]func(rc *Context, fn *ast.FuncNode) (string, error) // Literal renders a literal fragment. Literal func(rc *Context, lit *ast.LiteralNode) (string, error) // Where renders a WHERE fragment. Where func(rc *Context, where *ast.WhereNode) (string, error) // Expr renders an expression fragment. Expr func(rc *Context, expr *ast.ExprNode) (string, error) // Operator renders an operator fragment. Operator func(rc *Context, op *ast.OperatorNode) (string, error) // Distinct renders the DISTINCT fragment. Returns an // empty string if n is nil. Distinct func(rc *Context, n *ast.UniqueNode) (string, error) // Render renders f into a SQL query. Render func(rc *Context, f *Fragments) (string, error) // PreRender is a set of hooks that are called before Render. It is a final // opportunity to customize f before rendering. It is nil by default. PreRender []func(rc *Context, f *Fragments) error }
Renderer is a set of functions for rendering ast elements into SQL. Use NewDefaultRenderer to get a new instance. Each function can be swapped with a custom implementation for a SQL dialect.
func NewDefaultRenderer ¶
func NewDefaultRenderer() *Renderer
NewDefaultRenderer returns a Renderer that works for most SQL dialects. Driver implementations can override specific rendering functions as needed.