This package is only for creating sql text. To run the created sql text, you must create a database connection. (Ex: mysql, postgresql).
import "github.com/mstgnz/gobuilder"
var gb = gobuilder.NewGoBuilder(gobuilder.Postgres)
// all columns
gb.Table("users").Select().Where("id","=","1").Sql()
gb.Table("users").Select().Where("id","=","1").Prepare()
// filter columns
gb.Table("users").Select("firstname", "lastname", "created_at").
Where("id", "=", 1).
Prepare()
Result: SELECT * FROM users WHERE id = $1
Result: SELECT * FROM users WHERE id = 1
Result: SELECT firstname,lastname,created_at FROM users WHERE id = $1
Params: [1]
gb.Table("users").Select().
Where("id", "=", "1").
OrWhere("email", "=", "[email protected]").
Prepare()
Result: SELECT * FROM users WHERE id=$1 OR email=$2
Params: ["1", "[email protected]"]
gb.Table("users as u").Select("u.firstname", "u.lastname", "a.address").
Join("address as a", "a.user_id","=","u.id").
Where("u.email", "=", "[email protected]").
Prepare()
gb.Table("users as u").Select("u.firstname", "u.lastname", "a.address").
Join("address as a", "a.user_id","=","u.id").
Where("u.email", "=", "[email protected]").
Sql()
Result: SELECT u.firstname,u.lastname,a.address FROM users as u INNER JOIN address as a ON a.user_id=u.id WHERE u.email=$1
Params: ["[email protected]"]
Result: SELECT u.firstname,u.lastname,a.address FROM users as u INNER JOIN address as a ON a.user_id=u.id WHERE u.email='[email protected]'
gb.Table("users").Select().
Where("id", "=", "1").
Between("created_at", "2021-01-01", "2021-03-16").
Prepare()
gb.Table("users").Select().
Where("id", "=", "1").
Between("created_at", "2021-01-01", "2021-03-16").
Sql()
Result: SELECT * FROM users WHERE id=$1 AND created_at BETWEEN $2 AND $3
Params: [1, "2021-01-01", "2021-03-16"]
Result: SELECT * FROM users WHERE id=1 AND created_at BETWEEN '2021-01-01' AND '2021-03-16'
gb.Table("users").Select().
Where("id", "=", "1").
Between("created_at", "2021-01-01", "2021-03-16").
Limit(1, 5).
Prepare()
Result: SELECT * FROM users WHERE id=$1 AND created_at BETWEEN $2 AND $3 LIMIT 1,5
Params: [1, "2021-01-01", "2021-03-16"]
gb.Table("users").Select().
Where("id", "=", "1").
Between("created_at", "2021-01-01", "2021-03-16").
GroupBy("lastname").
Prepare()
Result: SELECT * FROM users WHERE id=$1 AND created_at BETWEEN $2 AND $3 GROUP BY lastname
Params: [1, "2021-01-01", "2021-03-16"]
gb.Table("users").Select().
Where("id", "=", "1").
Between("created_at", "2021-01-01", "2021-03-16").
GroupBy("lastname").
OrderBy("id", "DESC").
Prepare()
Result: SELECT * FROM users WHERE id=$1 AND created_at BETWEEN $2 AND $3 GROUP BY lastname ORDER BY id DESC
Params: [1, "2021-01-01", "2021-03-16"]
s1, _ := gb.Table("users").Select().Where("lastname", "=", "lorem").Prepare()
s2, _ := gb.Table("users").Select().Where("lastname", "=", "ipsum").Union(s1).Prepare()
Result: SELECT * FROM users WHERE lastname=$1 UNION SELECT * FROM users WHERE lastname=$2
Params: ["lorem", "ipsum"]
args := map[string]any{
"firstname": "Lorem",
"lastname": "IPSUM",
}
gb.Table("users").Create(args).Prepare()
Result: INSERT INTO users (lastname,firstname) VALUES ($1,$2)
Params: ["Lorem", "IPSUM"]
args := map[string]any{
"firstname": "Lorem",
"lastname": "IPSUM",
}
gb.Table("users").Update(args).Where("email", "=", "[email protected]").Prepare()
Result: UPDATE users SET firstname=$1, lastname=$2 WHERE email=$3
Params: ["Lorem", "IPSUM", "[email protected]"]
gb.Table("users").Delete().Where("email", "=", "[email protected]").Prepare()
Result: DELETE FROM users WHERE email=$1
Params: ["[email protected]"]