grammar

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2021 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Package grammar generates SQL statements described by a configured grammar.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Default is a basic default grammar.
	Default = &Grammar{
		Parameter:   "?",
		ParameterFn: nil,
		Returning:   "RETURNING",
	}

	// Postgres is a PostgreSQL grammar.
	Postgres = &Grammar{
		Parameter: "",
		ParameterFn: func(column string, n int) string {
			return fmt.Sprintf("$%v", n+1)
		},
		Returning: "RETURNING",
	}
)

Functions

This section is empty.

Types

type Grammar

type Grammar struct {
	// Parameter and ParameterFn work together to define how parameters are inserted into SQL
	// statements.  If Parameter is non-empty - such as "?" - then its value is used in place
	// of query parameters.  Otherwise ParameterFn is called with the column name and parameter
	// number.
	Parameter   string
	ParameterFn ParameterFunc
	// Returning specifies if the database uses a RETURNING clause to return columns during
	// INSERT/UPDATE queries.  Setting this to an empty string disables this functionality.
	Returning string
}

Grammar creates SQL queries according to the grammar configuration.

func (Grammar) Delete

func (me Grammar) Delete(table string, keys []string) (Query *statements.Query)

Delete returns the SQL statement for deleting from the table. It also returns a slice of column names in the order they are used as parameters.

func (Grammar) Insert

func (me Grammar) Insert(table string, columns []string, auto []string) (Query *statements.Query)

Insert returns the SQL statement for inserting into table. It also returns a slice of column names in the order they are used as parameters. A RETURNING clause is appended to the statement if auto is non-empty and the grammar supports RETURNING.

func (Grammar) Update

func (me Grammar) Update(table string, columns []string, keys []string, auto []string) (Query *statements.Query)

Update returns the SQL statement for updating a record in a table. It also returns a slice of column names in the order they are used as parameters. A RETURNING clause is appended to the statement if auto is non-empty and the grammar supports RETURNING.

type ParameterFunc

type ParameterFunc func(string, int) string

ParameterFunc accepts a column name and the parameter's overall position in the query. The return value should be the SQL parameter value to insert into the query.

Jump to

Keyboard shortcuts

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