Documentation ¶
Overview ¶
Package obreron provides a simple, fast and cheap query builder
Index ¶
- type DeleteStm
- func (dst *DeleteStm) And(expr string, p ...any) *DeleteStm
- func (dst *DeleteStm) AndIf(cond bool, expr string, p ...any) *DeleteStm
- func (st DeleteStm) Build() (string, []any)
- func (dst *DeleteStm) Clause(clause, expr string, p ...any) *DeleteStm
- func (dst *DeleteStm) ClauseIf(cond bool, clause, expr string, p ...any) *DeleteStm
- func (dst *DeleteStm) Close()
- func (dst *DeleteStm) From(source string) *DeleteStm
- func (dst *DeleteStm) In(value, expr string, p ...any) *DeleteStm
- func (dst *DeleteStm) InArgs(value string, p ...any) *DeleteStm
- func (dst *DeleteStm) Like(expr string, p ...any) *DeleteStm
- func (dst *DeleteStm) LikeIf(cond bool, expr string, p ...any) *DeleteStm
- func (dst *DeleteStm) Limit(limit int) *DeleteStm
- func (dst *DeleteStm) Or(expr string, p ...any) *DeleteStm
- func (dst *DeleteStm) OrIf(cond bool, expr string, p ...any) *DeleteStm
- func (dst *DeleteStm) OrderBy(expr string, p ...any) *DeleteStm
- func (dst *DeleteStm) Where(cond string, p ...any) *DeleteStm
- func (dst *DeleteStm) Y() *DeleteStm
- type InsertStament
- func (in *InsertStament) Build() (string, []any)
- func (in *InsertStament) Clause(clause, expr string, p ...any) *InsertStament
- func (in *InsertStament) Close()
- func (in *InsertStament) Col(col string, p ...any) *InsertStament
- func (in *InsertStament) ColIf(cond bool, col string, p ...any) *InsertStament
- func (in *InsertStament) ColSelect(col string, expr *SelectStm) *InsertStament
- func (in *InsertStament) ColSelectIf(cond bool, col string, expr *SelectStm) *InsertStament
- func (in *InsertStament) Ignore() *InsertStament
- func (in *InsertStament) Into(table string) *InsertStament
- type SelectStm
- func (st *SelectStm) And(expr string, p ...any) *SelectStm
- func (st *SelectStm) AndIf(cond bool, expr string, p ...any) *SelectStm
- func (st SelectStm) Build() (string, []any)
- func (st *SelectStm) Clause(clause, expr string, p ...any) *SelectStm
- func (st *SelectStm) ClauseIf(cond bool, clause, expr string, p ...any) *SelectStm
- func (st *SelectStm) Close()
- func (st *SelectStm) Col(expr string, p ...any) *SelectStm
- func (st *SelectStm) ColIf(cond bool, expr string, p ...any) *SelectStm
- func (st *SelectStm) From(source string) *SelectStm
- func (st *SelectStm) GroupBy(grp string, p ...any) *SelectStm
- func (st *SelectStm) Having(hav string, p ...any) *SelectStm
- func (st *SelectStm) In(expr string, p ...any) *SelectStm
- func (up *SelectStm) InArgs(value string, p ...any) *SelectStm
- func (st *SelectStm) Join(expr string, p ...any) *SelectStm
- func (st *SelectStm) JoinIf(cond bool, expr string, p ...any) *SelectStm
- func (st *SelectStm) LeftJoin(expr string, p ...any) *SelectStm
- func (st *SelectStm) LeftJoinIf(cond bool, join string, p ...any) *SelectStm
- func (st *SelectStm) Like(expr string, p ...any) *SelectStm
- func (st *SelectStm) LikeIf(cond bool, expr string, p ...any) *SelectStm
- func (st *SelectStm) Limit(limit int) *SelectStm
- func (st *SelectStm) Offset(off int) *SelectStm
- func (st *SelectStm) On(on string, p ...any) *SelectStm
- func (st *SelectStm) OnIf(cond bool, expr string, p ...any) *SelectStm
- func (st *SelectStm) Or(expr string, p ...any) *SelectStm
- func (st *SelectStm) OrIf(cond bool, expr string, p ...any) *SelectStm
- func (st *SelectStm) OrderBy(expr string, p ...any) *SelectStm
- func (st *SelectStm) OuterJoin(expr string, p ...any) *SelectStm
- func (st *SelectStm) OuterJoinIf(cond bool, expr string, p ...any) *SelectStm
- func (st *SelectStm) RightJoin(expr string, p ...any) *SelectStm
- func (st *SelectStm) RightJoinIf(cond bool, expr string, p ...any) *SelectStm
- func (st *SelectStm) Where(cond string, p ...any) *SelectStm
- func (up *SelectStm) Y() *SelectStm
- type UpdateStm
- func (up *UpdateStm) And(expr string, p ...any) *UpdateStm
- func (up *UpdateStm) AndIf(cond bool, expr string, p ...any) *UpdateStm
- func (st UpdateStm) Build() (string, []any)
- func (up *UpdateStm) Clause(clause, expr string, p ...any) *UpdateStm
- func (up *UpdateStm) ClauseIf(cond bool, clause, expr string, p ...any) *UpdateStm
- func (up *UpdateStm) Close()
- func (up *UpdateStm) ColSelect(col *SelectStm, alias string) *UpdateStm
- func (up *UpdateStm) ColSelectIf(cond bool, col *SelectStm, alias string) *UpdateStm
- func (up *UpdateStm) In(value, expr string, p ...any) *UpdateStm
- func (up *UpdateStm) InArgs(value string, p ...any) *UpdateStm
- func (up *UpdateStm) Join(expr string, p ...any) *UpdateStm
- func (up *UpdateStm) JoinIf(cond bool, expr string, p ...any) *UpdateStm
- func (up *UpdateStm) Like(expr string, p ...any) *UpdateStm
- func (up *UpdateStm) LikeIf(cond bool, expr string, p ...any) *UpdateStm
- func (up *UpdateStm) Limit(limit int) *UpdateStm
- func (up *UpdateStm) On(on string, p ...any) *UpdateStm
- func (up *UpdateStm) OnIf(cond bool, expr string, p ...any) *UpdateStm
- func (up *UpdateStm) Or(expr string, p ...any) *UpdateStm
- func (up *UpdateStm) OrIf(cond bool, expr string, p ...any) *UpdateStm
- func (up *UpdateStm) OrderBy(expr string, p ...any) *UpdateStm
- func (up *UpdateStm) Set(expr string, p ...any) *UpdateStm
- func (up *UpdateStm) SetIf(cond bool, expr string, p ...any) *UpdateStm
- func (up *UpdateStm) Where(cond string, p ...any) *UpdateStm
- func (up *UpdateStm) Y() *UpdateStm
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DeleteStm ¶ added in v2.0.2
type DeleteStm struct {
// contains filtered or unexported fields
}
DeleteStm represents a DELETE SQL statement builder. It provides a fluent interface for constructing DELETE queries.
func (*DeleteStm) And ¶ added in v2.0.2
And adds a condition to the query connecting with an AND operator
func (*DeleteStm) AndIf ¶ added in v2.0.2
AndIf adds a condition to the query connecting with an AND operator only when cond parameter is true
func (DeleteStm) Build ¶ added in v2.0.2
Build return the query as a string with the added parameters
func (*DeleteStm) Clause ¶ added in v2.0.2
Clause adds a custom clause to the query in the position were is invoked
func (*DeleteStm) Close ¶ added in v2.0.2
func (dst *DeleteStm) Close()
Close releases the statement back to the pool. After calling Close, the statement should not be used.
func (*DeleteStm) From ¶ added in v2.0.2
From sets the target table for the delete stament
Example ¶
s := Delete() From("client")
func (*DeleteStm) InArgs ¶ added in v2.0.2
InArgs adds an IN clause to the statement with automatically generated positional parameters. Example:
Delete().From("users").Where("active = ?", true).InArgs("id", 1, 2, 3)
Generates: DELETE FROM users WHERE active = ? AND id IN (?, ?, ?)
func (*DeleteStm) Like ¶ added in v2.0.2
Like adds a LIKE clause to the query after the las clause added
func (*DeleteStm) LikeIf ¶ added in v2.0.2
LikeIf adds a LIKE clause to the query after the las clause added, when cond is true
type InsertStament ¶
type InsertStament struct {
// contains filtered or unexported fields
}
InsertStament represents an insert stament
func Insert ¶
func Insert() *InsertStament
Insert Returns an insert stament
Example ¶
ins := Insert().Into("client").Col("name", "'some name'").Col("value", "'somemail@mail.net'").ColIf(true, "data", "'some data'").ColIf(false, "info", 12)
query, p := ins.Build()
r, err := db.Exec(q, p...)
func (*InsertStament) Build ¶
func (in *InsertStament) Build() (string, []any)
Build returns the query and the parameters as to be used by *sql.DB.query or *sql.DB.Exec
func (*InsertStament) Clause ¶
func (in *InsertStament) Clause(clause, expr string, p ...any) *InsertStament
func (*InsertStament) Close ¶
func (in *InsertStament) Close()
Close free resources used by the stament
func (*InsertStament) Col ¶
func (in *InsertStament) Col(col string, p ...any) *InsertStament
Col adds columns and values to the insert clause
Example ¶
ins := insInsert().Col("name, value", "'some name'", "'somemail@mail.net'").Into("client")
query, p := ins.Build()
r, err := db.Exec(q, p...)
func (*InsertStament) ColIf ¶
func (in *InsertStament) ColIf(cond bool, col string, p ...any) *InsertStament
ColIf adds columns and values to the insert clause when the cond parameter is true
Example ¶
ins := insInsert().ColIf(true, "name, value", "'some name'", "'somemail@mail.net'").Into("client")
query, p := ins.Build()
r, err := db.Exec(q, p...)
func (*InsertStament) ColSelect ¶
func (in *InsertStament) ColSelect(col string, expr *SelectStm) *InsertStament
ColSelect is a helper method used to build insert select... staments
Example ¶
ins := Insert().Into("courses").ColSelectIf(true, "name, location, gid", Select().Col("name, location, 1").From("courses").Where("cid = 2")).ColSelectIf(false, "last_name, last_location, grid", Select().Col("last_name, last_location, 11").From("courses").Where("cid = 2"))
query, p := ins.Build()
r, err := db.Exec(q, p...)
Produces: INSERT INTO courses ( name, location, gid ) SELECT name, location, 1 FROM courses WHERE cid = 2
func (*InsertStament) ColSelectIf ¶
func (in *InsertStament) ColSelectIf(cond bool, col string, expr *SelectStm) *InsertStament
func (*InsertStament) Ignore ¶
func (in *InsertStament) Ignore() *InsertStament
Ignore adds Ignore clause to the insert stament
Example ¶
ins := Insert().Ignore().Into("client").Col("name, value", "'some name'", "'somemail@mail.net'")
query, p := ins.Build()
r, err := db.Exec(q, p...)
func (*InsertStament) Into ¶
func (in *InsertStament) Into(table string) *InsertStament
Into adds into clause to the insert stament
type SelectStm ¶
type SelectStm struct {
// contains filtered or unexported fields
}
SelectStm is a select stament
func Select ¶
func Select() *SelectStm
Select Returns a select stament
Example ¶
query, _ := Select().Col("a1, a2, a3").From("client").Build() r, error := db.Query(q)
func (*SelectStm) And ¶
And adds a condition to the query connecting with an AND operator
Example ¶
s := Select().Col("*").From("client"). Where("status = ?", 1).And("country = ?", "CL")
Also can be used in join and having clauses
func (*SelectStm) AndIf ¶
AndIf adds a condition to the query connecting with an AND operator only when cond parameter is true
Example ¶
filterByCountry = true s := Select().Col("*").From("client"). Where("status = ?", 1).AndIf("country = ?", "CL")
Also can be used in join and having clauses
func (*SelectStm) Clause ¶
Clause adds a custom clause to the query in the position were is invoked
Example ¶
Select().Clause("SQL NO CACHE").Col("a1, a2, a3").From("client").Where("1 = 1")
func (*SelectStm) ClauseIf ¶
ClauseIf adds a custom clause to the query in the position were is invoked, whencond is true
Example ¶
Select().ClauseIf(true, "SQL NO CACHE").Col("a1, a2, a3").From("client").Where("1 = 1")
func (*SelectStm) Close ¶
func (st *SelectStm) Close()
Close release the resources used by the stament
func (*SelectStm) Col ¶
Col adds a column to the select stament.
Example ¶
s := Select() s.Col("name, mail").Col("? AS max_credit", 1000000). From("client")
func (*SelectStm) ColIf ¶
ColIf adds a column to the select stament only when `cond` parameter is true.
Example ¶
addMaxCredit := true
s := Select() s.Col("name, mail").ColIf(addMaxCredit, "? AS max_credit", 1000000). From("client")
func (*SelectStm) From ¶
From sets the source table for the select stament
Example ¶
s := Select() s.Col("*"). From("client")
func (*SelectStm) GroupBy ¶
GroupBy adds a GROUP BY clause to the query
Example ¶
Select().Col("a1, a2, a3").From("client").Where("1 = 1").GroupBy("a1")
func (*SelectStm) Having ¶
Having adds a HAVING clause to the query
Example ¶
Select().Col("a1, a2, a3, COUNT(1) AS how_many").From("client").Where("1 = 1").GroupBy("a1").Having(how_many > 100)
func (*SelectStm) In ¶
In adds a IN clause to the query after the las clause added
Example ¶
Select().Col("a1, a2, a3").From("client").Where("1 = 1").And("city").In("'Nagoya'", "'Tokio", "'Parral'")
func (*SelectStm) InArgs ¶ added in v2.0.2
InArgs adds an In clause to the stament automatically setting the positional parameters of the query based on the passed parameters
func (*SelectStm) Join ¶
Join adds a relation to the query in the form of an inner join
Example ¶
s := Select().Col("*").From("client"). Join("addresses a ON a.client_id = c.client_id")
Also On clause can be used along with connectors and parameters ¶
s := Select().Col("*").From("client"). Join("addresses a").On("a.client_id = c.client_id").And("c.status = ?", 0)
func (*SelectStm) JoinIf ¶
JoinIf adds a relation to the query in the form of an inner join only when the cond parameter is true
Example ¶
addJoin := true s := Select().Col("*"). From("client"). JoinIf(addJoin, "addresses a ON a.client_id = c.client_id")
Also OnIf clause can be used along with connectors and parameters ¶
s := Select().Col("*"). From("client"). JoinIf(aaddJoin, "addresses a"). OnIf(addJoin, "a.client_id = c.client_id").And("c.status = ?", 0)
func (*SelectStm) LeftJoin ¶
LeftJoin adds a relation to the query in the form of a left join
Example ¶
s := Select().Col("*").From("client"). LeftJoin("addresses a ON a.client_id = c.client_id")
Also On clause can be used along with connectors and parameters ¶
s := Select().Col("*").From("client"). LeftJoin("addresses a").On("a.client_id = c.client_id").And("c.status = ?", 0)
func (*SelectStm) LeftJoinIf ¶
LeftJoinIf adds a relation to the query in the form of a left join only when the cond parameter is true
Example ¶
addJoin := true s := Select().Col("*"). From("client"). LeftJoinIf(addJoin, "addresses a ON a.client_id = c.client_id")
Also OnIf clause can be used along with connectors and parameters ¶
s := Select().Col("*"). From("client"). LeftJoinIf(aaddJoin, "addresses a"). OnIf(addJoin, "a.client_id = c.client_id").And("c.status = ?", 0)
func (*SelectStm) Like ¶
Like adds a LIKE clause to the query after the las clause added
Example ¶
Select().Col("a1, a2, a3").From("client").Where("1 = 1").And("city").Like("'%ago%'")
Observe that if you use it like Select().Like(..., will produce "SELECT LIKE"
func (*SelectStm) LikeIf ¶
LikeIf adds a LIKE clause to the query after the las clause added, when cond is true
Example ¶
Select().Col("a1, a2, a3").From("client").Where("1 = 1").And("city").LikeIf(true, "'%ago%'")
func (*SelectStm) Limit ¶
Limit adds a LIMIT clause to the query
Example ¶
Select().Col("a1, a2, a3").From("client").Where("1 = 1").Limit(100)
func (*SelectStm) OrderBy ¶
OrderBy adds an ORDER BY clause to the query
Example ¶
Select().Col("a1, a2, a3").From("client").Where("1 = 1").OrderBy("a1 ASC")
func (*SelectStm) OuterJoinIf ¶
func (*SelectStm) RightJoinIf ¶
func (*SelectStm) Where ¶
Where adds a condition to filter the query
Example ¶
s := Select().Col("*").From("client"). Where("status = ?", 1)
func (*SelectStm) Y ¶ added in v2.0.2
Y adds an AND conector to the stament where is called. Its helpful when used with In()
Example ¶
Select(). Col("*"). From("client"). Where("country = ?", "CL"). Y(). In("status", "", 1, 2, 3, 4)
Produces: SELECT * FROM client WHERE country = ? AND status IN (?, ?, ?, ?)
type UpdateStm ¶ added in v2.0.2
type UpdateStm struct {
// contains filtered or unexported fields
}
UpdateStm represents an update stament
func Update ¶
Update returns an update stament
Example ¶
upd := Update("client").Set("status = 0").Where("status = ?", 1)
query, p := upd.Build() // builds UPDATE client SET status = 0 WHERE status = ? and stores in p []any{1}
r, err := db.Exec(query, p...)
func (*UpdateStm) And ¶ added in v2.0.2
And adds an AND conector with eventual parameters to the stament where is called
func (*UpdateStm) AndIf ¶ added in v2.0.2
And adds an AND conector with eventual parameters to the stament where is called, only when cond parameter is true
func (UpdateStm) Build ¶ added in v2.0.2
Build return the query as a string with the added parameters
func (*UpdateStm) Close ¶ added in v2.0.2
func (up *UpdateStm) Close()
Close frees up the resources used in the stament
func (*UpdateStm) ColSelect ¶ added in v2.0.2
ColSelect is a helper method which provides a way to build an update (select ...) stament
Example ¶
upd := Update("items"). ColSelectIf( true, Select(). Col("id, retail / wholesale AS markup, quantity"). From("items"), "discounted" ).Set("items.retail = items.retail * 0.9"). Set("a = 2"). SetIf(true, "c = 3"). Where("discounted.markup >= 1.3"). And("discounted.quantity < 100"). And("items.id = discounted.id"). query, p := upd.Build() // builds UPDATE items, ( SELECT id, retail / wholesale AS markup, quantity FROM items) discounted SET a = 2, c = 3 WHERE 1 = 1 AND discounted.markup >= 1.3 AND discounted.quantity < 100 AND items.id = discounted.id
func (*UpdateStm) ColSelectIf ¶ added in v2.0.2
ColSelectIf does the same work as [ColSelect] only when the cond parameter is true
func (*UpdateStm) In ¶ added in v2.0.2
In adds a IN clause to the query after the las clause added
Example ¶
Update("client"). Set("status = 0"). Where("country = ?", "CL"). Y().In("status", "?, ?, ?, ?", 1, 2, 3, 4)
func (*UpdateStm) InArgs ¶ added in v2.0.2
InArgs adds an In clause to the stament automatically setting the positional parameters of the query based on the passed parameters
Example ¶
Update("client").Set("status = 0").Where("country = ?", "CL").Y().InArgs("status", 1, 2, 3, 4)
Produces: UPDATE client SET status = 0 WHERE country = ? AND status IN (?, ?, ?, ?)"
func (*UpdateStm) Join ¶ added in v2.0.2
Join adds a relation to the query in the form of an inner join
Example ¶
Update("business AS b"). Join("business_geocode AS g").On("b.business_id = g.business_id"). Set("b.mapx = g.latitude, b.mapy = g.longitude"). Where("(b.mapx = '' or b.mapx = 0)").And("g.latitude > 0") OUTPUT: UPDATE business AS b JOIN business_geocode AS g ON b.business_id = g.business_id SET b.mapx = g.latitude, b.mapy = g.longitude WHERE (b.mapx = '' or b.mapx = 0) AND g.latitude > 0 AND 3 = 3
func (*UpdateStm) Like ¶ added in v2.0.2
Like adds a LIKE clause to the query after the last added clause
# Example Update("items"). Set("items.retail = items.retail * 0.9"). Set("a = 2"). Where("discounted.markup >= 1.3"). And("colX"). Like("'%ago%'")
func (*UpdateStm) LikeIf ¶ added in v2.0.2
LikeIf adds a LIKE clause to the query after the last added clause only when cond parameter value is true
# Example Update("items"). Set("items.retail = items.retail * 0.9"). Set("a = 2"). Where("discounted.markup >= 1.3"). And("colX"). Like("'%ago%'")
func (*UpdateStm) Or ¶ added in v2.0.2
Or adds an Or connector with eventual parameters to the stament where is called
func (*UpdateStm) OrIf ¶ added in v2.0.2
OrIf adds an Or connector with eventual parameters to the stament where is called only when cond parameter value is true
func (*UpdateStm) Set ¶ added in v2.0.2
Set adds set clause to the update stament
Examples ¶
upd := Update("client").Set("status = 0").Where("status = ?", 1) up2 := Update("client").Set("status = ?", 0).Where("status = ?", 1) up3 := Update("client").Set("status = ?", 0).Set("name = ?", "stitch").Where("status = ?", 1)
func (*UpdateStm) SetIf ¶ added in v2.0.2
Set adds set clause to the update stament when the cond param is true
func (*UpdateStm) Y ¶ added in v2.0.2
Y adds an AND conector to the stament where is called. Its helpful when used with In()
Example ¶
Update("client").Set("status = 0").Where("country = ?", "CL").Y().In("status", "", 1, 2, 3, 4)
Produces: UPDATE client SET status = 0 WHERE country = ? AND status IN (?, ?, ?, ?)