All Projects → go-xorm → Builder

go-xorm / Builder

Licence: bsd-3-clause
Lightweight and fast SQL builder for Go language, moved to https://gitea.com/xorm/builder

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Builder

Fluentpdo
A PHP SQL query builder using PDO
Stars: ✭ 783 (+459.29%)
Mutual labels:  builder, sql
Php Sql Query Builder
An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation.
Stars: ✭ 313 (+123.57%)
Mutual labels:  builder, sql
Loukoum
A simple SQL Query Builder
Stars: ✭ 305 (+117.86%)
Mutual labels:  builder, sql
Pypika
PyPika is a python SQL query builder that exposes the full richness of the SQL language using a syntax that reflects the resulting query. PyPika excels at all sorts of SQL queries but is especially useful for data analysis.
Stars: ✭ 1,111 (+693.57%)
Mutual labels:  builder, sql
Oreilly intermediate sql for data
Resources for the O'Reilly Online Training "Intermediate SQL For Data Analysis"
Stars: ✭ 136 (-2.86%)
Mutual labels:  sql
Klik Socialmediawebsite
Complete PHP-based Login/Registration system, Profile system, Chat room, Forum system and Blog/Polls/Event Management System.
Stars: ✭ 129 (-7.86%)
Mutual labels:  sql
Calcite Avatica
Mirror of Apache Calcite - Avatica
Stars: ✭ 130 (-7.14%)
Mutual labels:  sql
Vscode Deploy Reloaded
Recoded version of Visual Studio Code extension 'vs-deploy', which provides commands to deploy files to one or more destinations.
Stars: ✭ 129 (-7.86%)
Mutual labels:  sql
Stratosdb
☄️ ☁️ An All-in-One GUI for Cloud SQL that can help users design and test their AWS RDS Instances
Stars: ✭ 140 (+0%)
Mutual labels:  sql
Tidyquery
Query R data frames with SQL
Stars: ✭ 138 (-1.43%)
Mutual labels:  sql
Mybatis Boost
Boost your mybatis sql developing experience
Stars: ✭ 134 (-4.29%)
Mutual labels:  sql
Firenze
Adapter based JavaScript ORM for Node.js and the browser
Stars: ✭ 131 (-6.43%)
Mutual labels:  sql
Php Login System
Embeddable and Secure PHP Authentication System with Login, Signup, User Profiles, Profile Editing, Account Verification via Email, Password Reset System, Remember-Me Feature and more.
Stars: ✭ 135 (-3.57%)
Mutual labels:  sql
Join Monster Graphql Tools Adapter
Use Join Monster to fetch your data with Apollo Server.
Stars: ✭ 130 (-7.14%)
Mutual labels:  sql
Domino Ui
Domino-ui
Stars: ✭ 138 (-1.43%)
Mutual labels:  builder
Goose
A database migration tool. Supports SQL migrations and Go functions.
Stars: ✭ 2,112 (+1408.57%)
Mutual labels:  sql
Dumpling
Dumpling is a fast, easy-to-use tool written by Go for dumping data from the database(MySQL, TiDB...) to local/cloud(S3, GCP...) in multifarious formats(SQL, CSV...).
Stars: ✭ 134 (-4.29%)
Mutual labels:  sql
Quicksql
A Flexible, Fast, Federated(3F) SQL Analysis Middleware for Multiple Data Sources
Stars: ✭ 1,821 (+1200.71%)
Mutual labels:  sql
Developer Handbook
An opinionated guide on how to become a professional Web/Mobile App Developer.
Stars: ✭ 1,830 (+1207.14%)
Mutual labels:  sql
Reactive record
Generate ActiveRecord models for a pre-existing Postgres db
Stars: ✭ 132 (-5.71%)
Mutual labels:  sql

SQL builder

GitCI.cn codecov

Package builder is a lightweight and fast SQL builder for Go and XORM.

Make sure you have installed Go 1.8+ and then:

go get github.com/go-xorm/builder

Insert

sql, args, err := builder.Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL()

// INSERT INTO table1 SELECT * FROM table2
sql, err := builder.Insert().Into("table1").Select().From("table2").ToBoundSQL()

// INSERT INTO table1 (a, b) SELECT b, c FROM table2
sql, err = builder.Insert("a, b").Into("table1").Select("b, c").From("table2").ToBoundSQL()

Select

// Simple Query
sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()
// With join
sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
		RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
// From sub query
sql, args, err := Select("sub.id").From(Select("c").From("table1").Where(Eq{"a": 1}), "sub").Where(Eq{"b": 1}).ToSQL()
// From union query
sql, args, err = Select("sub.id").From(
	Select("id").From("table1").Where(Eq{"a": 1}).Union("all", Select("id").From("table1").Where(Eq{"a": 2})),"sub").
	Where(Eq{"b": 1}).ToSQL()
// With order by
sql, args, err = Select("a", "b", "c").From("table1").Where(Eq{"f1": "v1", "f2": "v2"}).
		OrderBy("a ASC").ToSQL()
// With limit.
// Be careful! You should set up specific dialect for builder before performing a query with LIMIT
sql, args, err = Dialect(MYSQL).Select("a", "b", "c").From("table1").OrderBy("a ASC").
		Limit(5, 10).ToSQL()

Update

sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL()

Delete

sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL()

Union

sql, args, err := Select("*").From("a").Where(Eq{"status": "1"}).
		Union("all", Select("*").From("a").Where(Eq{"status": "2"})).
		Union("distinct", Select("*").From("a").Where(Eq{"status": "3"})).
		Union("", Select("*").From("a").Where(Eq{"status": "4"})).
		ToSQL()

Conditions

  • Eq is a redefine of a map, you can give one or more conditions to Eq
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Eq{"a":1})
// a=? [1]
sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
// b=? AND c=? ["c", 0]
sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
// b=? AND c=? ["c", 0]
sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
// b=? OR b=? ["c", "d"]
sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
// b IN (?,?) ["c", "d"]
sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
// b=? AND c IN (?,?) [1, 2, 3]
  • Neq is the same to Eq
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Neq{"a":1})
// a<>? [1]
sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
// b<>? AND c<>? ["c", 0]
sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
// b<>? AND c<>? ["c", 0]
sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
// b<>? OR b<>? ["c", "d"]
sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
// b NOT IN (?,?) ["c", "d"]
sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
// b<>? AND c NOT IN (?,?) [1, 2, 3]
  • Gt, Gte, Lt, Lte
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
// a>? AND b>=? [1, 2]
sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
// a<? OR b<=? [1, 2]
  • Like
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Like{"a", "c"})
// a LIKE ? [%c%]
  • Expr you can customerize your sql with Expr
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Expr("a = ? ", 1))
// a = ? [1]
sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
// a=(select id from table where c = ?) [1]
  • In and NotIn
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(In("a", 1, 2, 3))
// a IN (?,?,?) [1,2,3]
sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
// a IN (?,?,?) [1,2,3]
sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1))))
// a IN (select id from b where c = ?) [1]
  • IsNull and NotNull
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(IsNull{"a"})
// a IS NULL []
sql, args, _ := ToSQL(NotNull{"b"})
	// b IS NOT NULL []
  • And(conds ...Cond), And can connect one or more condtions via And
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
// a=? AND b LIKE ? AND d<>? [1, %c%, 2]
  • Or(conds ...Cond), Or can connect one or more conditions via Or
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
// a=? OR b LIKE ? OR d<>? [1, %c%, 2]
sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
// a=? OR (b LIKE ? AND d<>?) [1, %c%, 2]
  • Between
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Between{"a", 1, 2})
// a BETWEEN 1 AND 2
  • Define yourself conditions

Since Cond is an interface.

type Cond interface {
	WriteTo(Writer) error
	And(...Cond) Cond
	Or(...Cond) Cond
	IsValid() bool
}

You can define yourself conditions and compose with other Cond.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].