builder

package module
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 4 Imported by: 0

README

PostgreSQL Query Builder for "jackc/pgx"

Install

Use go get to install this package.

go get github.com/xloss/go-builder

Refer to the documentation here: http://godoc.org/github.com/xloss/go-builder

Basic usage

Select
table1 := builder.NewTable("table1")
table2 := builder.NewTable("table2")

q := builder.NewSelect()
q.From(table1)
q.Column(builder.ColumnName{Table: table1, Name: "id"})
q.Column(builder.ColumnName{Table: table2, Name: "col", Alias: "c1"})
q.LeftJoin(table2, builder.OnEq{Table1: table1, Table2: table2, Column1: "id", Column2: "table_id"})
q.Where(builder.WhereEq{Table: table1, Column: "id", Value: 1})
q.Order(builder.Order{Table: table1, Column: "name", Desc: true})
q.Limit(10)
q.Offset(5)

sql, binds, err := q.Get()
fmt.Println(sql)
fmt.Println(binds)

// Output:
// SELECT table1_pxaisxqdvb.id, table2_xftynvknii.col AS c1 FROM table1 AS table1_pxaisxqdvb LEFT JOIN table2 AS table2_xftynvknii ON table1_pxaisxqdvb.id = table2_xftynvknii.table_id WHERE table1_pxaisxqdvb.id = @id_ywfaoazuel ORDER BY table1_pxaisxqdvb.name DESC LIMIT @limit_jlhldzwuei OFFSET @offset_myzuqcgmdn
// map[id_ywfaoazuel:1 limit_jlhldzwuei:10 offset_myzuqcgmdn:5]

Subquery in FROM:

table1 := builder.NewTable("table1")
q1 := builder.NewSelect()
q1.From(table1)
q1.Column(builder.ColumnName{Table: table1, Name: "column1"})

table2 := NewTableSub(q1)

q2 := builder.NewSelect()
q2.From(table2)
q2.Column(builder.ColumnName{Table: table2, Name: "column1"})

sql, binds, err := q2.Get()
fmt.Println(sql)

// Output:
// SELECT wjsfhotgjo_vhncsrjhtg.column1 FROM (SELECT table1_ttwlctmwvj.column1 FROM table1 AS table1_ttwlctmwvj) AS wjsfhotgjo_vhncsrjhtg
Update
table := builder.NewTable("table")

q := builder.NewUpdate(table)
q.Set("col1", "value1")
q.SetNow("col2")
q.Where(builder.WhereEq{Table: table, Column: "col3", Value: 5})

q.Return(builder.ColumnName{Table: table, Name: "col1"})
q.Return(builder.ColumnName{Table: table, Name: "col2", Alias: "a1"})

sql, binds, err := q.Get()
fmt.Println(sql)
fmt.Println(binds)

// Output:
// UPDATE table AS table_kiykrrnhxf SET col1 = @col1_tolhdmocsn, col2 = NOW() WHERE table_kiykrrnhxf.col3 = @col3_tkdyhzjxqb RETURNING table_kiykrrnhxf.col1, table_kiykrrnhxf.col2 AS a1
// map[col1_tolhdmocsn:value1 col3_tkdyhzjxqb:5]
Insert
table := builder.NewTable("table")
q := builder.NewInsert(table)

q.Value("col1", 5)
q.Value("col2", "str")

q.Return(builder.ColumnName{Table: table, Name: "col1"})
q.Return(builder.ColumnName{Table: table, Name: "col2", Alias: "a1"})

sql, binds, err := q.Get()

// Output:
// INSERT INTO table AS table_jhpjqkzvkd (col1, col2) VALUES (@col1_buaonudjkx, @col2_amouztvkkt) RETURNING table_jhpjqkzvkd.col1, table_jhpjqkzvkd.col2 AS a1
// map[col1_buaonudjkx:5 col2_amouztvkkt:str]
Delete
table := builder.NewTable("table")
q := builder.NewDelete(table)

q.Where(builder.WhereEq{Table: table, Column: "col", Value: 5})

sql, binds, err := q.Get()
// Output:
// DELETE FROM table AS table_iuulmrhwnt WHERE table_iuulmrhwnt.col = @col_ujpmtrhymk
// map[col_ujpmtrhymk:5]

Without WHERE:

table := builder.NewTable("table")
q := builder.NewDelete(table)

q.Full()

sql, binds, err := q.Get()
// Output:
// DELETE FROM table AS table_iuulmrhwnt
// map[]

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// UpdateNoSets means that when creating update query no set values
	UpdateNoSets = errors.New("no sets")
)

Functions

This section is empty.

Types

type Column added in v0.2.0

type Column interface {
	// contains filtered or unexported methods
}

type ColumnCoalesce added in v0.2.0

type ColumnCoalesce struct {
	Table   *Table // required
	Name    string // required
	Alias   string // required
	Default any    // required
}
Example
table1 := NewTable("table1")
query1 := NewSelect()
query1.Column(
	ColumnCoalesce{Table: table1, Name: "column1", Alias: "a1", Default: 5},
	ColumnCoalesce{Table: table1, Name: "column2", Alias: "a2", Default: "text"},
)
query1.From(table1)

fmt.Println(query1.Get())

// Result:
// SELECT COALESCE(table1_yzethlflca.column1, 5) AS a1, COALESCE(table1_yzethlflca.column2, 'text') AS a2 FROM table1 AS table1_yzethlflca
// map[]
// <nil>

type ColumnCount added in v0.2.0

type ColumnCount struct {
	Table    *Table // required
	Name     string
	Alias    string // required
	Distinct bool
}
Example
table1 := NewTable("table1")
query1 := NewSelect()
query1.Column(
	ColumnCount{Table: table1, Alias: "a1"},
	ColumnCount{Table: table1, Name: "column2", Alias: "a2"},
	ColumnCount{Table: table1, Name: "column3", Alias: "a3", Distinct: true},
)
query1.From(table1)

fmt.Println(query1.Get())

// Result:
// SELECT COUNT(*) AS a1, COUNT(table1_yyapxlsrva.column2) AS a2, COUNT(DISTINCT table1_yyapxlsrva.column3) AS a3 FROM table1 AS table1_yyapxlsrva
// map[]
// <nil>

type ColumnJsonbArrayElementsText added in v0.2.1

type ColumnJsonbArrayElementsText struct {
	Table    *Table // required
	Name     string // required
	Alias    string // required
	Distinct bool
}
Example
table1 := NewTable("table1")
query1 := NewSelect()
query1.Column(
	ColumnJsonbArrayElementsText{Table: table1, Name: "column1", Alias: "a1"},
	ColumnJsonbArrayElementsText{Table: table1, Name: "column2", Alias: "a2", Distinct: true},
)
query1.From(table1)

fmt.Println(query1.Get())

// Result:
// SELECT JSONB_ARRAY_ELEMENTS_TEXT(table1_yifmeamxwi.column1) AS a1, DISTINCT JSONB_ARRAY_ELEMENTS_TEXT(table1_yifmeamxwi.column2) AS a2 FROM table1 AS table1_yifmeamxwi
// map[]
// <nil>

type ColumnName added in v0.2.0

type ColumnName struct {
	Table    *Table // required
	Name     string // required
	Alias    string
	Distinct bool
}
Example
table1 := NewTable("table1")
query1 := NewSelect()
query1.Column(
	ColumnName{Table: table1, Name: "column1"},
	ColumnName{Table: table1, Name: "column2", Alias: "a2"},
	ColumnName{Table: table1, Name: "column3", Distinct: true},
)
query1.From(table1)

fmt.Println(query1.Get())

// Result:
// SELECT table1_punanojozl.column1, table1_punanojozl.column2 AS a2, DISTINCT table1_punanojozl.column3 FROM table1 AS table1_punanojozl
// map[]
// <nil>

type ColumnValue added in v0.2.8

type ColumnValue struct {
	Value any // required
	Alias string
}
Example
table1 := NewTable("table1")
query1 := NewSelect()
query1.Column(
	ColumnValue{Value: 1},
	ColumnValue{Value: "1", Alias: "a1"},
)
query1.From(table1)

fmt.Println(query1.Get())

// Result:
// SELECT 1, '1' AS a1 FROM table1 AS table1_punanojozl
// map[]
// <nil>

type DeleteQuery

type DeleteQuery struct {
	// contains filtered or unexported fields
}

func NewDelete

func NewDelete(table *Table) *DeleteQuery

func (*DeleteQuery) Full

func (q *DeleteQuery) Full() *DeleteQuery

func (*DeleteQuery) Get

func (q *DeleteQuery) Get() (string, map[string]any, error)

func (*DeleteQuery) Where

func (q *DeleteQuery) Where(w Where) *DeleteQuery

type Group added in v0.2.3

type Group interface {
	// contains filtered or unexported methods
}

type GroupColumn added in v0.2.3

type GroupColumn struct {
	Table  *Table
	Column string
}

type InsertQuery

type InsertQuery struct {
	// contains filtered or unexported fields
}

func NewInsert

func NewInsert(table *Table) *InsertQuery

func (*InsertQuery) Get

func (q *InsertQuery) Get() (string, map[string]any, error)

func (*InsertQuery) OnConflict added in v0.2.6

func (q *InsertQuery) OnConflict(c ...string) *InsertQuery

func (*InsertQuery) Return added in v0.2.0

func (q *InsertQuery) Return(c ...Column) *InsertQuery

func (*InsertQuery) UpdateSet added in v0.2.6

func (q *InsertQuery) UpdateSet(column string, value any) *InsertQuery

func (*InsertQuery) UpdateSetNow added in v0.2.6

func (q *InsertQuery) UpdateSetNow(column string) *InsertQuery

func (*InsertQuery) Value

func (q *InsertQuery) Value(column string, v any) *InsertQuery

type On

type On interface {
	// contains filtered or unexported methods
}

type OnAnd

type OnAnd struct {
	List []On
}

type OnEq

type OnEq struct {
	Table1  *Table
	Table2  *Table
	Column1 string
	Column2 string
}

type OnLess

type OnLess struct {
	Table1  *Table
	Table2  *Table
	Column1 string
	Column2 string
}

type OnMore

type OnMore struct {
	Table1  *Table
	Table2  *Table
	Column1 string
	Column2 string
}

type Order

type Order struct {
	Table  *Table
	Column string
	Desc   bool
}

type SelectQuery

type SelectQuery struct {
	// contains filtered or unexported fields
}

func NewSelect

func NewSelect() *SelectQuery
Example
table1 := NewTable("table1")
query1 := NewSelect()
query1.Column(ColumnName{Table: table1, Name: "column1"})
query1.From(table1)

fmt.Println(query1.Get())

// Result:
// SELECT table1_pdddspkqfu.column1 FROM table1 AS table1_pdddspkqfu
// map[]
// <nil>

func (*SelectQuery) Column

func (q *SelectQuery) Column(c ...Column) *SelectQuery

func (*SelectQuery) From

func (q *SelectQuery) From(t ...*Table) *SelectQuery

func (*SelectQuery) Get

func (q *SelectQuery) Get() (string, map[string]any, error)

func (*SelectQuery) Group added in v0.2.3

func (q *SelectQuery) Group(g ...Group) *SelectQuery

func (*SelectQuery) IsSub added in v0.2.8

func (q *SelectQuery) IsSub() *SelectQuery

func (*SelectQuery) LeftJoin

func (q *SelectQuery) LeftJoin(table *Table, on On) *SelectQuery

func (*SelectQuery) Limit

func (q *SelectQuery) Limit(limit int) *SelectQuery

func (*SelectQuery) Offset

func (q *SelectQuery) Offset(offset int) *SelectQuery

func (*SelectQuery) Order

func (q *SelectQuery) Order(o ...Order) *SelectQuery

func (*SelectQuery) Where

func (q *SelectQuery) Where(w Where) *SelectQuery

type Table

type Table struct {
	Name  string
	Alias string
	Query query
}

func NewTable

func NewTable(name string) *Table

Creating Table struct for use in Builder

func NewTableSub added in v0.2.3

func NewTableSub(q query) *Table

Using Query as subquery in FROM

Example
table1 := NewTable("table1")
query1 := NewSelect()
query1.Column(ColumnName{Table: table1, Name: "column1"})
query1.From(table1)

table2 := NewTableSub(query1)
query2 := NewSelect()
query2.Column(ColumnName{Table: table2, Name: "column2"})
query2.From(table2)

fmt.Println(query2.Get())

// Result:
// SELECT yccakzcfbx_dxaolgmqrw.column2 FROM (SELECT table1_fmxwghcgnt.column1 FROM table1 AS table1_fmxwghcgnt) AS yccakzcfbx_dxaolgmqrw
// map[]
// <nil>

type UpdateQuery

type UpdateQuery struct {
	// contains filtered or unexported fields
}

func NewUpdate

func NewUpdate(table *Table) *UpdateQuery

func (*UpdateQuery) Get

func (q *UpdateQuery) Get() (string, map[string]any, error)

func (*UpdateQuery) Return added in v0.2.9

func (q *UpdateQuery) Return(c ...Column) *UpdateQuery

func (*UpdateQuery) Set

func (q *UpdateQuery) Set(column string, value any) *UpdateQuery

func (*UpdateQuery) SetNow

func (q *UpdateQuery) SetNow(column string) *UpdateQuery

func (*UpdateQuery) Where

func (q *UpdateQuery) Where(w Where) *UpdateQuery

type Where

type Where interface {
	// contains filtered or unexported methods
}

type WhereAnd

type WhereAnd struct {
	List []Where
}

type WhereEq

type WhereEq struct {
	Table  *Table
	Column string
	Value  interface{}
}

type WhereEqColumn added in v0.2.8

type WhereEqColumn struct {
	Table1  *Table
	Table2  *Table
	Column1 string
	Column2 string
}

type WhereExists added in v0.2.8

type WhereExists struct {
	Query *SelectQuery
}

type WhereFullText

type WhereFullText struct {
	Table    *Table
	Column   string
	Language string
	Value    string
}

type WhereILike

type WhereILike struct {
	Table  *Table
	Column string
	Value  interface{}
}

type WhereIn

type WhereIn struct {
	Table  *Table
	Column string
	Values interface{}
}

type WhereIsNotNull

type WhereIsNotNull struct {
	Table  *Table
	Column string
}

type WhereIsNull

type WhereIsNull struct {
	Table  *Table
	Column string
}

type WhereJsonbTextExist added in v0.2.1

type WhereJsonbTextExist struct {
	Table  *Table
	Column string
	Value  string
}

type WhereJsonbTextInExist added in v0.2.4

type WhereJsonbTextInExist struct {
	Table  *Table
	Column string
	Values []string
}

type WhereLess

type WhereLess struct {
	Table  *Table
	Column string
	Value  interface{}
}

type WhereLessEq

type WhereLessEq struct {
	Table  *Table
	Column string
	Value  interface{}
}

type WhereMore

type WhereMore struct {
	Table  *Table
	Column string
	Value  interface{}
}

type WhereMoreColumn

type WhereMoreColumn struct {
	Table1, Table2   *Table
	Column1, Column2 string
}

type WhereMoreEq

type WhereMoreEq struct {
	Table  *Table
	Column string
	Value  interface{}
}

type WhereNotEq added in v0.2.10

type WhereNotEq struct {
	Table  *Table
	Column string
	Value  interface{}
}

type WhereNotEqColumn added in v0.2.10

type WhereNotEqColumn struct {
	Table1  *Table
	Table2  *Table
	Column1 string
	Column2 string
}

type WhereOr

type WhereOr struct {
	List []Where
}

Jump to

Keyboard shortcuts

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