render

package
v0.42.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package render provides the mechanism for rendering ast into SQL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	// Renderer holds the rendering functions.
	Renderer *Renderer

	// Dialect is the driver dialect.
	Dialect dialect.Dialect

	// The args map contains predefined variables that are
	// substituted into the query. It may be empty or nil.
	Args map[string]string
}

Context contains context for rendering a query.

type Fragments

type Fragments struct {
	Distinct string
	Columns  string
	From     string
	Where    string
	GroupBy  string
	OrderBy  string
	Range    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)

	// 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)

	// 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)

	// PreRender is a hook that is 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

	// Render renders f into a SQL query.
	Render func(rc *Context, f *Fragments) (string, 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL