buildsq

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package buildsq helps to build structured queries for the structured query language.

Deprecated: While this implementation was nice to prove the concept it is not a piece of well designed software. It simply has grown out of needs at hand. This makes it not more than a prototype a is now rewritten as the bsq package.

Index

Examples

Constants

View Source
const NamedArgs nmArgs = 0

NamedArgs maps named argument bindings to named SQL parameters, i.e. ':name' style parameters in SQL statements.

Variables

View Source
var PostgreSQL = pgSql{}
View Source
var SQLite3SQL = sqlite3Sql{}
View Source
var StdSQL = stdSql{}

Functions

func AllDecls

func AllDecls(do func(decl interface{}) (done bool)) (done bool, err error)

func CamelToSnake

func CamelToSnake() *nmconv.Conversion

func InitDecl

func InitDecl(schema string, opts *InitOpts, tableDecl interface{}) (alias string, err error)

func InitDecls

func InitDecls(schema string, opts *InitOpts, tableDecls ...interface{}) error

func MustInitDecls

func MustInitDecls(schema string, opts *InitOpts, tableDecls ...interface{})

func MustSQL

func MustSQL(sw SQLWriter, d Dialect) string

func SQL

func SQL(sw SQLWriter, d Dialect) (string, error)

func VisitDecl

func VisitDecl(tableDecl interface{}, v Visitor) (err error)

Types

type Alias

type Alias struct {
	T tabler
	A string
}

func (Alias) Params

func (_ Alias) Params(_ Dialect) ([]Param, error)

func (Alias) Q

func (a Alias) Q(c columner) Qualify

func (Alias) WriteSQL

func (a Alias) WriteSQL(wr writer, alias bool, d Dialect) (err error)

type Args

type Args interface {
	// Slice does the actual mapping, depending on the specific implementation.
	Slice(args ...sql.NamedArg) []interface{}
}

Args creates the binding slice of interfaces from sql.NamedArgs that can then be used with sql package's Scan method. Args has implementaions for '?', '$1' and ':name' style parameters, i.e. PosArgs, IndexArgs and NamedArgs. – In general this is not used directly but the SQL Dialect creates the correct Args implementation.

See also Dialect

func ArgsOf

func ArgsOf(sw SQLWriter, d Dialect) (Args, error)

func MustArgsOf

func MustArgsOf(sw SQLWriter, d Dialect) Args

type CRUD

type CRUD struct {
	Create Lazy
	Read   Lazy
	Update Lazy
	Delete Lazy
}

func CRUDFor

func CRUDFor(table interface{}, idCol *Column, insert bool, dialect func() Dialect) CRUD

type Clear

type Clear struct {
	Table interface{}
}

func (Clear) Params

func (del Clear) Params(_ Dialect) (res []Param, err error)

func (Clear) WriteSQL

func (del Clear) WriteSQL(wr writer, alias bool, d Dialect) error

type Cols

type Cols []columner

func ColsOf

func ColsOf(tableDecl interface{}, without ...*Column) Cols

Columns must be called on pointer to table structure

func (Cols) Params

func (_ Cols) Params(_ Dialect) ([]Param, error)

func (Cols) WriteSQL

func (cs Cols) WriteSQL(wr writer, alias bool, d Dialect) (err error)

type Column

type Column struct {
	Table *Table
	Name  string
}

func Columns

func Columns(strip bool, cs ...columner) []*Column
Example
package main

import (
	"fmt"
	"os"
	"strings"
)

var (
	_ SQLWriter = (*Table)(nil)
	_ SQLWriter = (*Column)(nil)
)

var tstPerson = struct {
	Table
	ID, Called, Name Column
}{
	Table: Table{TableName: "persons", TableAlias: "p"},
	ID:    Column{Name: "id"},
}

var tstAddress = struct {
	Table
	ID, Person, Street, No, City, ZIP Column
}{
	Table: Table{TableName: "addresses", TableAlias: "a"},
	ID:    Column{Name: "id"},
	ZIP:   Column{Name: "zip"},
}

func init() {
	InitDecls("", &InitOpts{NameConv: CamelToSnake()},
		&tstPerson,
		&tstAddress,
	)
}

func main() {
	var wr strings.Builder
	for _, col := range ColumnsOf(&tstPerson) {
		col.WriteSQL(&wr, false, StdSQL)
		fmt.Fprintln(&wr)
	}
	os.Stdout.WriteString(wr.String())
}
Output:

id
called
name

func ColumnsOf

func ColumnsOf(tableDecl interface{}, without ...*Column) []*Column

Columns must be called on pointer to table structure

func (*Column) ColumnPtr

func (c *Column) ColumnPtr() *Column

func (*Column) Params

func (c *Column) Params(_ Dialect) ([]Param, error)

func (*Column) WriteSQL

func (c *Column) WriteSQL(wr writer, alias bool, d Dialect) (err error)

type Concat

type Concat []interface{}
Example (Alias)
q := Concat{
	"SELECT ", Cols{
		Qualify{"q", &tstPerson.Name},
		&tstAddress.Street,
	},
	" FROM ", Alias{&tstPerson, "q"}, " JOIN ", &tstAddress,
	" ON (", &tstPerson.ID, "=", &tstAddress.Person, ") ",
	"WHERE ", Qualify{"q", &tstPerson.Called}, "=", P("person"),
}
fmt.Println(SQL(q, nil))
fmt.Println(MustArgsOf(q, nil).Slice(sql.Named("person", "John")))
Output:

SELECT q.name, a.street FROM persons q JOIN addresses a ON (p.id=a.person) WHERE q.called=$1 <nil>
[John]

func (Concat) Params

func (q Concat) Params(_ Dialect) (res []Param, err error)

func (Concat) WriteSQL

func (q Concat) WriteSQL(wr writer, alias bool, d Dialect) (err error)

type Create

type Create struct {
	Table interface{}
	// The selected
	// ID column is considered to be an auto-generated id, i.e. it will not be
	// part of the VALUES in the SQL statement.
	ID *Column
	// If Values is nil all columns of Table except the ID column will be used
	// as values. Otherwise the selected Columns are used.
	Columns Cols
}

CreateStatement will insert a new row into the specified Table that is considered to have an autogenerated id.

func (Create) Params

func (cs Create) Params(d Dialect) ([]Param, error)

func (Create) WriteSQL

func (cs Create) WriteSQL(wr writer, alias bool, d Dialect) error

type Delete

type Delete struct {
	SelectBy Cols
}

func (Delete) Params

func (del Delete) Params(_ Dialect) (res []Param, err error)

func (Delete) WriteSQL

func (del Delete) WriteSQL(wr writer, alias bool, d Dialect) error

type Dialect

type Dialect interface {
	Dialect() func() Dialect
	QuoteName(string) string
	AddParam(params interface{}, a Param) interface{}
	WriteParam(wr writer, params interface{}, param Param) error
	Args(params []Param) (Args, error)

	AutoIncInsertStmt(id *Column, set Cols) (Concat, error)
	UpsertStmt(tbl *Table, conflict Cols, cols Cols) (Concat, error)

	Create(tx sqlize.Tx, query string, args ...interface{}) (id int64, err error)
}
var DefaultDialect Dialect = StdSQL

type IndexArgs

type IndexArgs []string

IndexArgs maps named argument bindings to indexted SQL parameters, i.e. '$1' style parameters in SQL statements.

func NewIndexArgs

func NewIndexArgs(params []Param) (res IndexArgs, err error)

func (IndexArgs) Slice

func (ia IndexArgs) Slice(args ...sql.NamedArg) []interface{}

type IndexParams

type IndexParams struct {
	Format string
}

IndexParams make a refernec to the passed argument by index. Usually something like '$3' for the 3rd argument.

func (IndexParams) AddParam

func (_ IndexParams) AddParam(params interface{}, p Param) interface{}

func (IndexParams) Args

func (ia IndexParams) Args(params []Param) (Args, error)

func (IndexParams) WriteParam

func (ia IndexParams) WriteParam(wr writer, params interface{}, p Param) (err error)

type InitOpts

type InitOpts struct {
	NameConv  *nmconv.Conversion
	MustAlias bool
}

type Insert

type Insert struct {
	Columns Cols
}
Example
stmt := Insert{Cols{&tstPerson.Name, &tstPerson.Called}}
fmt.Println(Lazy{Query: stmt}.SQL(StdSQL))
Output:

INSERT INTO persons (name, called) VALUES ($1, $2) <nil>

func (Insert) Params

func (ins Insert) Params(d Dialect) ([]Param, error)

func (Insert) WriteSQL

func (ins Insert) WriteSQL(wr writer, alias bool, d Dialect) error

type JoinEq

type JoinEq struct {
	NewTabCol, JoinToCol *Column
	Outer                OuterJoin
}

func (JoinEq) Params

func (_ JoinEq) Params(_ Dialect) ([]Param, error)

func (JoinEq) WriteSQL

func (jq JoinEq) WriteSQL(wr writer, alias bool, d Dialect) (err error)

type Lazy

type Lazy struct {
	Query   SQLWriter
	Dialect func() Dialect
	// contains filtered or unexported fields
}

func (Lazy) Args

func (l Lazy) Args(d Dialect) (Args, error)

SQL must not be used concurrently without explicit locking

func (*Lazy) EffDialect

func (l *Lazy) EffDialect(d Dialect) Dialect

func (Lazy) MustArgs

func (l Lazy) MustArgs(d Dialect) Args

func (Lazy) MustSQL

func (l Lazy) MustSQL(d Dialect) string

func (Lazy) SQL

func (l Lazy) SQL(d Dialect) (string, error)

SQL must not be used concurrently without explicit locking

func (Lazy) TempArgs

func (l Lazy) TempArgs(d Dialect) (Args, Dialect, error)

func (Lazy) TempSQL

func (l Lazy) TempSQL(d Dialect) (string, Dialect, error)

type OuterJoin

type OuterJoin int
const (
	LeftOuter OuterJoin = iota + 1
	RightOuter
	FullOuter
)

type Param

type Param string

func P

func P(tag interface{}) Param

Create a Param that must have a tag that is either a string or implements fmt.Stringer. P panics if tag is not OK.

type PosArgs

type PosArgs []string

PosArgs maps named argument bindings to postitional SQL parameters, i.e. '?' style parameters in SQL statements.

func NewPosArgs

func NewPosArgs(params []Param) (res PosArgs, err error)

func (PosArgs) Slice

func (pa PosArgs) Slice(args ...sql.NamedArg) []interface{}

type PosParams

type PosParams string

PosParams expect an argument for each parameter, usually '?', in the statement

func (PosParams) AddParam

func (_ PosParams) AddParam(params interface{}, a Param) interface{}

func (PosParams) Args

func (pa PosParams) Args(params []Param) (Args, error)

func (PosParams) WriteParam

func (pa PosParams) WriteParam(wr writer, params interface{}, param Param) (err error)

type Qualify

type Qualify struct {
	Q string
	C *Column
}

func (Qualify) ColumnPtr

func (q Qualify) ColumnPtr() *Column

func (Qualify) Params

func (_ Qualify) Params(_ Dialect) ([]Param, error)

func (Qualify) WriteSQL

func (q Qualify) WriteSQL(wr writer, alias bool, d Dialect) (err error)

type SQLWriter

type SQLWriter interface {
	WriteSQL(wr writer, alias bool, d Dialect) error
	Params(d Dialect) ([]Param, error)
}

type Select

type Select struct {
	From     interface{}
	SelectBy Cols
	Columns  Cols
}
Example
stmt := Select{
	Columns:  ColsOf(&tstAddress, &tstAddress.ZIP, &tstAddress.Street),
	SelectBy: Cols{&tstAddress.ZIP, &tstAddress.Street},
}
fmt.Println(Lazy{Query: stmt}.SQL(StdSQL))
Output:

SELECT id, person, no, city FROM addresses WHERE zip=$1 AND street=$2 <nil>

func (Select) Params

func (rt Select) Params(_ Dialect) (res []Param, err error)

func (Select) WriteSQL

func (rt Select) WriteSQL(wr writer, alias bool, d Dialect) error

type Table

type Table struct {
	TableSchema string
	TableName   string
	TableAlias  string
	// contains filtered or unexported fields
}

func TableData

func TableData(t interface{}) *Table

func (*Table) Params

func (_ *Table) Params(_ Dialect) ([]Param, error)

func (*Table) String

func (t *Table) String() string

func (*Table) WriteSQL

func (t *Table) WriteSQL(wr writer, alias bool, d Dialect) (err error)

type Update

type Update struct {
	Table    interface{}
	Set      Cols
	SelectBy Cols
}

func (Update) Params

func (upd Update) Params(_ Dialect) (res []Param, err error)

func (Update) WriteSQL

func (upd Update) WriteSQL(wr writer, alias bool, d Dialect) error

type Upsert

type Upsert struct {
	Table    interface{}
	Conflict Cols
	Columns  Cols
}
Example (Pgsql)
stmt := Upsert{Table: &tstPerson, Conflict: Cols{&tstPerson.ID}}
fmt.Println(Lazy{Query: stmt, Dialect: PostgreSQL.Dialect()}.SQL(nil))
Output:

INSERT INTO persons (id, called, "name") VALUES ($1, $2, $3) ON CONFLICT (id) DO UPDATE SET called=$2, "name"=$3 <nil>

func (Upsert) Params

func (ups Upsert) Params(d Dialect) ([]Param, error)

func (Upsert) WriteSQL

func (ups Upsert) WriteSQL(wr writer, alias bool, d Dialect) error

type Visitor

type Visitor interface {
	Table(decl interface{}, t *Table) error
	Column(decl interface{}, c *Column, field string) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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