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 ¶
- Constants
- Variables
- func AllDecls(do func(decl interface{}) (done bool)) (done bool, err error)
- func CamelToSnake() *nmconv.Conversion
- func InitDecl(schema string, opts *InitOpts, tableDecl interface{}) (alias string, err error)
- func InitDecls(schema string, opts *InitOpts, tableDecls ...interface{}) error
- func MustInitDecls(schema string, opts *InitOpts, tableDecls ...interface{})
- func MustSQL(sw SQLWriter, d Dialect) string
- func SQL(sw SQLWriter, d Dialect) (string, error)
- func VisitDecl(tableDecl interface{}, v Visitor) (err error)
- type Alias
- type Args
- type CRUD
- type Clear
- type Cols
- type Column
- type Concat
- type Create
- type Delete
- type Dialect
- type IndexArgs
- type IndexParams
- type InitOpts
- type Insert
- type JoinEq
- type Lazy
- func (l Lazy) Args(d Dialect) (Args, error)
- func (l *Lazy) EffDialect(d Dialect) Dialect
- func (l Lazy) MustArgs(d Dialect) Args
- func (l Lazy) MustSQL(d Dialect) string
- func (l Lazy) SQL(d Dialect) (string, error)
- func (l Lazy) TempArgs(d Dialect) (Args, Dialect, error)
- func (l Lazy) TempSQL(d Dialect) (string, Dialect, error)
- type OuterJoin
- type Param
- type PosArgs
- type PosParams
- type Qualify
- type SQLWriter
- type Select
- type Table
- type Update
- type Upsert
- type Visitor
Examples ¶
Constants ¶
const NamedArgs nmArgs = 0
NamedArgs maps named argument bindings to named SQL parameters, i.e. ':name' style parameters in SQL statements.
Variables ¶
var PostgreSQL = pgSql{}
var SQLite3SQL = sqlite3Sql{}
var StdSQL = stdSql{}
Functions ¶
func CamelToSnake ¶
func CamelToSnake() *nmconv.Conversion
func InitDecl ¶
Types ¶
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
type CRUD ¶
type Clear ¶
type Clear struct {
Table interface{}
}
type Column ¶
func Columns ¶
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 ¶
Columns must be called on pointer to table structure
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]
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.
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)
}
type IndexArgs ¶
type IndexArgs []string
IndexArgs maps named argument bindings to indexted SQL parameters, i.e. '$1' style parameters in SQL statements.
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>
type JoinEq ¶
type Lazy ¶
type Lazy struct {
Query SQLWriter
Dialect func() Dialect
// contains filtered or unexported fields
}
func (Lazy) Args ¶
SQL must not be used concurrently without explicit locking
func (Lazy) SQL ¶
SQL must not be used concurrently without explicit locking
type PosArgs ¶
type PosArgs []string
PosArgs maps named argument bindings to postitional SQL parameters, i.e. '?' style parameters in SQL statements.
type PosParams ¶
type PosParams string
PosParams expect an argument for each parameter, usually '?', in the statement
type Qualify ¶
type SQLWriter ¶
type Select ¶
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>
type Table ¶
type Update ¶
Source Files
¶
- args.go
- crud.go
- dialect.go
- postgres.go
- query.go
- schema.go
- sqlite3.go
- stdstmts.go