obreron

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: MIT Imports: 5 Imported by: 0

README

Obreron

Coverage

Fast and cheap sql builder.

Go Reference Go Report Card

Supported Dialects

  • Mysql
  • Postgresql (Work in progress)

Why?

Because writing SQL in Go is boring.

Instalation

Use go get to install v2

$ go get github.com/profe-ajedrez/obreron/v2

Use

You could see the examples directory.

Import package

import (
	v2 "github.com/profe-ajedrez/obreron/v2"
)
Select
  • Simple select
// Produces SELECT a1, a2, a3 FROM client
query, _ := v2.Select().Col("a1, a2, a3").From("client").Build()
r, error := db.Query(query)
  • Select/join/where/shuffled
// Produces SELECT a1, a2, ? AS diez, colIf1, colIf2, ? AS zero, a3, ? AS cien FROM client c JOIN addresses a ON a.id_cliente = a.id_cliente JOIN phones p ON p.id_cliente = c.id_cliente JOIN mailes m ON m.id_cliente = m.id_cliente AND c.estado_cliente = ? LEFT JOIN left_joined lj ON lj.a1 = c.a1 WHERE a1 = ? AND a2 = ? AND a3 = 10 AND a16 = ?
// with params = []any{10, 0, 100, 0, "'last name'", 1000.54, 75}
query, params := v2.Select().
    Where("a1 = ?", "'last name'").
    Col("a1, a2, ? AS diez", 10).
    Col(`colIf1, colIf2, ? AS zero`, 0).
    Col("a3, ? AS cien", 100).    
    Where("a2 = ?", 1000.54).
    And("a3 = 10").And("a16 = ?", 75).
    Join("addresses a ON a.id_cliente = a.id_cliente").
    Join("phones p").On("p.id_cliente = c.id_cliente").
    Join("mailes m").On("m.id_cliente = m.id_cliente").
    And("c.estado_cliente = ?", 0).    
    LeftJoin("left_joined lj").On("lj.a1 = c.a1").
    From("client c").
    Build()

r, error := db.Query(query, params...)

Note that in this example we purposely shuffled the order of the clauses and yet the query was built correctly

  • Conditional elements

Sometimes we need to check for a condition to build dynamic sql

This example adds the column name to the query only if the variable shouldAddName is true.

query, _ := v2.Select().
	Col("a1, a2, a3").
	ColIf(shouldAddName, "name")
	From("client").
	Build()

// Produces "SELECT a1, a2, a3 FROM client" when shouldAddName is false
// Produces "SELECT a1, a2, a3, name FROM client" when shouldAddName is true

This also can be applied to joins.

query, _ := v2.Select().
	Col("*").
	From("client c").
	Join("addresses a").On("a.client_id = c.client_id").
    JoinIf(shouldGetPhones, "phones p ON p.client_id = c.client_id").
    Build()

// Produces "SELECT * FROM client c JOIN a.client_id = c.client_id" if shouldGetPhones is false
// Produces "SELECT * FROM client c JOIN a.client_id = c.client_id JOIN phones p ON p.client_id = c.client_id" " if shouldGetPhones is true

And boolean connectors

query, _ := v2.Select().
	Col("*").
	From("client c").	
	Where("c.status = 0").AndIf(shouldFilterByCountry, "country = 'CL'").
    Build()

// Produces "SELECT * FROM client c WHERE c.status = 0" when shouldFilterByCountry is false
// Produces "SELECT * FROM client c WHERE c.status = 0 AND country = 'CL'" when shouldFilterByCountry is true
  • Params

You can add params to almost any clause

query, params := v2.Select().
	Col("name, mail, ? AS max_credit", 1000000).
	From("client c").	
	Where("c.status = 0").And("country = ?", "CL").
    Limit("?", "100").Build()
Delete
  • Simple delete
query, _ := v2.Delete().From("client").Build()
// Produces "DELETE FROM client"
  • Simple del where
query, _ := v2.Delete().From("client").Where("client_id = 100").Build()
// Produces "DELETE FROM client WHERE client_id = 100"
  • Like with Select you can use parameters and conditionals with Delete
query, params := v2.Delete().From("client").Where("client_id = ?", 100).Build()
// Produces "DELETE FROM client WHERE client_id = ?"
query, params := v2.Delete().From("client").Where("1=1").AndIf(filterByClient, "client_id = ?", 100).Build()
// Produces "DELETE FROM client WHERE 1=1" when filterByClient is false
// Produces "DELETE FROM client WHERE 1=1 AND client_id = ?" when filterByClient is true
Update
  • Simple update
query, _ := v2.Update("client").Set("status = 0").Build()
// Produces UPDATE client SET status = 0
  • Update/where/order/limit
query, _ := v2.Update("client").
	Set("status = 0").
	Where("status = ?", 1).
	OrderBy("ciudad").
	Limit(10).
	Build()
  • You can use obreron to build an update/join query
query, _ := v2.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").
Build()

// Produces "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"
  • You can use obreron to build an update/select query
query, _ := v2.Update("items").
				ColSelect(Select().Col("id, retail / wholesale AS markup, quantity").From("items"), "discounted").
				Set("items.retail = items.retail * 0.9").
				Where("discounted.markup >= 1.3").
				And("discounted.quantity < 100").
				And("items.id = discounted.id").
	            Build()
// Produces UPDATE items ,( SELECT id, retail / wholesale AS markup, quantity FROM items ) discounted SET items.retail = items.retail * 0.9 WHERE discounted.markup >= 1.3 AND discounted.quantity < 100 AND items.id = discounted.id

Insert

  • Simple insert
query, params := Iv2.nsert().
	Into("client").
	Col("name, value", "'some name'", "'somemail@mail.net'").
    Build()

// Produces "INSERT INTO client ( name, value ) VALUES ( ?, ? )"
  • insert select
query, params := v2.Insert().
    Into("courses").
    ColSelect("name, location, gid", 
		Select().
		Col("name, location, 1").
	    From("courses").
	    Where("cid = 2")
	).Build()

// Produces       "INSERT INTO courses ( name, location, gid ) SELECT name, location, 1 FROM courses WHERE cid = 2"

Other clauses

You can add others clauses using the Clause method

query, params := v2.Insert().Clause("IGNORE", "")
	Into("client").
	Col("name, value", "'some name'", "'somemail@mail.net'").
    Build()

// Produces "INSERT IGNORE INTO client ( name, value ) VALUES ( ?, ? )"

The Clause method always will inject the clause after the last uses building command

Documentation

Overview

Package obreron provides a simple, fast and cheap query builder

Index

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 Delete

func Delete() *DeleteStm

func (*DeleteStm) And added in v2.0.2

func (dst *DeleteStm) And(expr string, p ...any) *DeleteStm

And adds a condition to the query connecting with an AND operator

func (*DeleteStm) AndIf added in v2.0.2

func (dst *DeleteStm) AndIf(cond bool, expr string, p ...any) *DeleteStm

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

func (st DeleteStm) Build() (string, []any)

Build return the query as a string with the added parameters

func (*DeleteStm) Clause added in v2.0.2

func (dst *DeleteStm) Clause(clause, expr string, p ...any) *DeleteStm

Clause adds a custom clause to the query in the position were is invoked

func (*DeleteStm) ClauseIf added in v2.0.2

func (dst *DeleteStm) ClauseIf(cond bool, clause, expr string, p ...any) *DeleteStm

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

func (dst *DeleteStm) From(source string) *DeleteStm

From sets the target table for the delete stament

Example

s := Delete() From("client")

func (*DeleteStm) In added in v2.0.2

func (dst *DeleteStm) In(value, expr string, p ...any) *DeleteStm

In adds a IN clause to the query after the las clause added

func (*DeleteStm) InArgs added in v2.0.2

func (dst *DeleteStm) InArgs(value string, p ...any) *DeleteStm

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

func (dst *DeleteStm) Like(expr string, p ...any) *DeleteStm

Like adds a LIKE clause to the query after the las clause added

func (*DeleteStm) LikeIf added in v2.0.2

func (dst *DeleteStm) LikeIf(cond bool, expr string, p ...any) *DeleteStm

LikeIf adds a LIKE clause to the query after the las clause added, when cond is true

func (*DeleteStm) Limit added in v2.0.2

func (dst *DeleteStm) Limit(limit int) *DeleteStm

Limit adds a LIMIT clause to the query

func (*DeleteStm) Or added in v2.0.2

func (dst *DeleteStm) Or(expr string, p ...any) *DeleteStm

func (*DeleteStm) OrIf added in v2.0.2

func (dst *DeleteStm) OrIf(cond bool, expr string, p ...any) *DeleteStm

func (*DeleteStm) OrderBy added in v2.0.2

func (dst *DeleteStm) OrderBy(expr string, p ...any) *DeleteStm

func (*DeleteStm) Where added in v2.0.2

func (dst *DeleteStm) Where(cond string, p ...any) *DeleteStm

Where adds a condition to filter the query

func (*DeleteStm) Y added in v2.0.2

func (dst *DeleteStm) Y() *DeleteStm

Y adds an AND conector to the stament where is called. Its helpful when used with In()

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

func (st *SelectStm) And(expr string, p ...any) *SelectStm

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

func (st *SelectStm) AndIf(cond bool, expr string, p ...any) *SelectStm

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) Build

func (st SelectStm) Build() (string, []any)

Build return the query as a string with the added parameters

func (*SelectStm) Clause

func (st *SelectStm) Clause(clause, expr string, p ...any) *SelectStm

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

func (st *SelectStm) ClauseIf(cond bool, clause, expr string, p ...any) *SelectStm

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

func (st *SelectStm) Col(expr string, p ...any) *SelectStm

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

func (st *SelectStm) ColIf(cond bool, expr string, p ...any) *SelectStm

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

func (st *SelectStm) From(source string) *SelectStm

From sets the source table for the select stament

Example

s := Select() s.Col("*"). From("client")

func (*SelectStm) GroupBy

func (st *SelectStm) GroupBy(grp string, p ...any) *SelectStm

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

func (st *SelectStm) Having(hav string, p ...any) *SelectStm

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

func (st *SelectStm) In(expr string, p ...any) *SelectStm

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

func (up *SelectStm) InArgs(value string, p ...any) *SelectStm

InArgs adds an In clause to the stament automatically setting the positional parameters of the query based on the passed parameters

func (*SelectStm) Join

func (st *SelectStm) Join(expr string, p ...any) *SelectStm

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

func (st *SelectStm) JoinIf(cond bool, expr string, p ...any) *SelectStm

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

func (st *SelectStm) LeftJoin(expr string, p ...any) *SelectStm

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

func (st *SelectStm) LeftJoinIf(cond bool, join string, p ...any) *SelectStm

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

func (st *SelectStm) Like(expr string, p ...any) *SelectStm

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

func (st *SelectStm) LikeIf(cond bool, expr string, p ...any) *SelectStm

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

func (st *SelectStm) Limit(limit int) *SelectStm

Limit adds a LIMIT clause to the query

Example

Select().Col("a1, a2, a3").From("client").Where("1 = 1").Limit(100)

func (*SelectStm) Offset

func (st *SelectStm) Offset(off int) *SelectStm

func (*SelectStm) On

func (st *SelectStm) On(on string, p ...any) *SelectStm

func (*SelectStm) OnIf

func (st *SelectStm) OnIf(cond bool, expr string, p ...any) *SelectStm

func (*SelectStm) Or

func (st *SelectStm) Or(expr string, p ...any) *SelectStm

func (*SelectStm) OrIf

func (st *SelectStm) OrIf(cond bool, expr string, p ...any) *SelectStm

func (*SelectStm) OrderBy

func (st *SelectStm) OrderBy(expr string, p ...any) *SelectStm

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) OuterJoin

func (st *SelectStm) OuterJoin(expr string, p ...any) *SelectStm

func (*SelectStm) OuterJoinIf

func (st *SelectStm) OuterJoinIf(cond bool, expr string, p ...any) *SelectStm

func (*SelectStm) RightJoin

func (st *SelectStm) RightJoin(expr string, p ...any) *SelectStm

func (*SelectStm) RightJoinIf

func (st *SelectStm) RightJoinIf(cond bool, expr string, p ...any) *SelectStm

func (*SelectStm) Where

func (st *SelectStm) Where(cond string, p ...any) *SelectStm

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

func (up *SelectStm) Y() *SelectStm

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

func Update(table string) *UpdateStm

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

func (up *UpdateStm) And(expr string, p ...any) *UpdateStm

And adds an AND conector with eventual parameters to the stament where is called

func (*UpdateStm) AndIf added in v2.0.2

func (up *UpdateStm) AndIf(cond bool, expr string, p ...any) *UpdateStm

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

func (st UpdateStm) Build() (string, []any)

Build return the query as a string with the added parameters

func (*UpdateStm) Clause added in v2.0.2

func (up *UpdateStm) Clause(clause, expr string, p ...any) *UpdateStm

func (*UpdateStm) ClauseIf added in v2.0.2

func (up *UpdateStm) ClauseIf(cond bool, clause, expr string, p ...any) *UpdateStm

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

func (up *UpdateStm) ColSelect(col *SelectStm, alias string) *UpdateStm

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

func (up *UpdateStm) ColSelectIf(cond bool, col *SelectStm, alias string) *UpdateStm

ColSelectIf does the same work as [ColSelect] only when the cond parameter is true

func (*UpdateStm) In added in v2.0.2

func (up *UpdateStm) In(value, expr string, p ...any) *UpdateStm

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

func (up *UpdateStm) InArgs(value string, p ...any) *UpdateStm

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

func (up *UpdateStm) Join(expr string, p ...any) *UpdateStm

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) JoinIf added in v2.0.2

func (up *UpdateStm) JoinIf(cond bool, expr string, p ...any) *UpdateStm

func (*UpdateStm) Like added in v2.0.2

func (up *UpdateStm) Like(expr string, p ...any) *UpdateStm

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

func (up *UpdateStm) LikeIf(cond bool, expr string, p ...any) *UpdateStm

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) Limit added in v2.0.2

func (up *UpdateStm) Limit(limit int) *UpdateStm

func (*UpdateStm) On added in v2.0.2

func (up *UpdateStm) On(on string, p ...any) *UpdateStm

func (*UpdateStm) OnIf added in v2.0.2

func (up *UpdateStm) OnIf(cond bool, expr string, p ...any) *UpdateStm

func (*UpdateStm) Or added in v2.0.2

func (up *UpdateStm) Or(expr string, p ...any) *UpdateStm

Or adds an Or connector with eventual parameters to the stament where is called

func (*UpdateStm) OrIf added in v2.0.2

func (up *UpdateStm) OrIf(cond bool, expr string, p ...any) *UpdateStm

OrIf adds an Or connector with eventual parameters to the stament where is called only when cond parameter value is true

func (*UpdateStm) OrderBy added in v2.0.2

func (up *UpdateStm) OrderBy(expr string, p ...any) *UpdateStm

func (*UpdateStm) Set added in v2.0.2

func (up *UpdateStm) Set(expr string, p ...any) *UpdateStm

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

func (up *UpdateStm) SetIf(cond bool, expr string, p ...any) *UpdateStm

Set adds set clause to the update stament when the cond param is true

func (*UpdateStm) Where added in v2.0.2

func (up *UpdateStm) Where(cond string, p ...any) *UpdateStm

Where adds a where clause to the update stament

func (*UpdateStm) Y added in v2.0.2

func (up *UpdateStm) Y() *UpdateStm

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 (?, ?, ?, ?)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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