import "xorm.io/builder"
Package builder is a simple and powerful sql builder for Go.
Make sure you have installed Go 1.1+ and then:
go get xorm.io/builder
WARNNING: Currently, only query conditions are supported. Below is the supported conditions.
1. Eq is a redefine of a map, you can give one or more conditions to Eq
import . "xorm.io/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]
2. Neq is the same to Eq
import . "xorm.io/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]
3. Gt, Gte, Lt, Lte
import . "xorm.io/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]
4. Like
import . "xorm.io/builder" sql, args, _ := ToSQL(Like{"a", "c"}) // a LIKE ? [%c%]
5. Expr you can customerize your sql with Expr
import . "xorm.io/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]
6. In and NotIn
import . "xorm.io/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]
7. IsNull and NotNull
import . "xorm.io/builder" sql, args, _ := ToSQL(IsNull{"a"}) // a IS NULL [] sql, args, _ := ToSQL(NotNull{"b"}) // b IS NOT NULL []
8. And(conds ...Cond), And can connect one or more condtions via AND
import . "xorm.io/builder" sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2})) // a=? AND b LIKE ? AND d<>? [1, %c%, 2]
9. Or(conds ...Cond), Or can connect one or more conditions via Or
import . "xorm.io/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]
10. Between
import . "xorm.io/builder" sql, args, _ := ToSQL(Between("a", 1, 2)) // a BETWEEN 1 AND 2
11. define yourself conditions Since Cond is a interface, you can define yourself conditions and compare with them
builder.go builder_delete.go builder_insert.go builder_join.go builder_limit.go builder_select.go builder_set_operations.go builder_update.go cond.go cond_and.go cond_between.go cond_compare.go cond_eq.go cond_expr.go cond_if.go cond_in.go cond_like.go cond_neq.go cond_not.go cond_notin.go cond_null.go cond_or.go doc.go error.go sql.go writer.go
const ( POSTGRES = "postgres" SQLITE = "sqlite3" MYSQL = "mysql" MSSQL = "mssql" ORACLE = "oracle" UNION = "union" INTERSECT = "intersect" EXCEPT = "except" )
all databasees
var ( // ErrNotSupportType not supported SQL type error ErrNotSupportType = errors.New("Not supported SQL type") // ErrNoNotInConditions no NOT IN params error ErrNoNotInConditions = errors.New("No NOT IN conditions") // ErrNoInConditions no IN params error ErrNoInConditions = errors.New("No IN conditions") // ErrNeedMoreArguments need more arguments ErrNeedMoreArguments = errors.New("Need more sql arguments") // ErrNoTableName no table name ErrNoTableName = errors.New("No table indicated") // ErrNoColumnToUpdate no column to update ErrNoColumnToUpdate = errors.New("No column(s) to update") // ErrNoColumnToInsert no column to insert ErrNoColumnToInsert = errors.New("No column(s) to insert") // ErrNotSupportDialectType not supported dialect type error ErrNotSupportDialectType = errors.New("Not supported dialect type") // ErrNotUnexpectedUnionConditions using union in a wrong way ErrNotUnexpectedUnionConditions = errors.New("Unexpected conditional fields in UNION query") // ErrUnsupportedUnionMembers unexpected members in UNION query ErrUnsupportedUnionMembers = errors.New("Unexpected members in UNION query") // ErrUnexpectedSubQuery Unexpected sub-query in SELECT query ErrUnexpectedSubQuery = errors.New("Unexpected sub-query in SELECT query") // ErrDialectNotSetUp dialect is not setup yet ErrDialectNotSetUp = errors.New("Dialect is not setup yet, try to use `Dialect(dbType)` at first") // ErrInvalidLimitation offset or limit is not correct ErrInvalidLimitation = errors.New("Offset or limit is not correct") // ErrUnnamedDerivedTable Every derived table must have its own alias ErrUnnamedDerivedTable = errors.New("Every derived table must have its own alias") // ErrInconsistentDialect Inconsistent dialect in same builder ErrInconsistentDialect = errors.New("Inconsistent dialect in same builder") )
ConvertPlaceholder replaces the place holder ? to $1, $2 ... or :1, :2 ... according prefix
ConvertToBoundSQL will convert SQL and args to a bound SQL
ToBoundSQL convert a builder or conditions to parameters bound SQL
ToSQL convert a builder or conditions to SQL and args
WriteMap writes conditions' SQL to Writer, op could be =, <>, >, <, <=, >= and etc.
Between implmentes between condition
And implments And with other conditions
IsValid tests if the condition is valid
Or implments Or with other conditions
WriteTo write data to Writer
type Builder struct {
// contains filtered or unexported fields
}
Builder describes a SQL statement
Delete creates a delete Builder
Dialect sets the db dialect of Builder.
Insert creates an insert Builder
MsSQL is shortcut of Dialect(MsSQL)
MySQL is shortcut of Dialect(MySQL)
Oracle is shortcut of Dialect(Oracle)
Postgres is shortcut of Dialect(Postgres)
SQLite is shortcut of Dialect(SQLITE)
Select creates a select Builder
Update creates an update Builder
And sets AND condition
CrossJoin sets cross join SQL
Delete sets delete SQL
Except sets except conditions
From sets from subject(can be a table name in string or a builder pointer) and its alias
FullJoin sets full join SQL
GroupBy groupby SQL
Having having SQL
InnerJoin sets inner join
Insert sets insert SQL
Intersect sets intersect conditions
Into sets insert table name
Join sets join table and conditions
LeftJoin sets left join SQL
Limit sets limitN condition
Or sets OR condition
OrderBy orderBy SQL
RightJoin sets right join SQL
Select sets select SQL
TableName returns the table name
ToBoundSQL generated a bound SQL string
ToSQL convert a builder to SQL and args
Union sets union conditions
Update sets update SQL
Where sets where SQL
WriteTo implements Writer interface
BytesWriter implments Writer and save SQL in bytes.Buffer
func NewWriter() *BytesWriter
NewWriter creates a new string writer
func (w *BytesWriter) Append(args ...interface{})
Append appends args to Writer
func (w *BytesWriter) Args() []interface{}
Args returns args
Cond defines an interface
And generates AND conditions
Expr generate customerize SQL
If returns Cond via condition
In generates IN condition
NewCond creates an empty condition
NotIn generate NOT IN condition
Or sets OR conditions
Decr implements a type used by Eq
Eq defines equals conditions
And implements And with other conditions
IsValid tests if this Eq is valid
OpWriteTo writes conditions with special operator
Or implements Or with other conditions
WriteTo writes SQL to Writer
Gt defines > condition
And implements And with other conditions
IsValid tests if this Eq is valid
Or implements Or with other conditions
WriteTo write SQL to Writer
Gte defines >= condition
And implements And with other conditions
IsValid tests if this Eq is valid
Or implements Or with other conditions
WriteTo write SQL to Writer
Incr implements a type used by Eq
IsNull defines IS NULL condition
And implements And with other conditions
IsValid tests if this condition is valid
Or implements Or with other conditions
WriteTo write SQL to Writer
Like defines like condition
And implements And with other conditions
IsValid tests if this condition is valid
Or implements Or with other conditions
WriteTo write SQL to Writer
Lt defines < condition
And implements And with other conditions
IsValid tests if this Eq is valid
Or implements Or with other conditions
WriteTo write SQL to Writer
Lte defines <= condition
And implements And with other conditions
IsValid tests if this Eq is valid
Or implements Or with other conditions
WriteTo write SQL to Writer
Neq defines not equal conditions
And implements And with other conditions
IsValid tests if this condition is valid
Or implements Or with other conditions
WriteTo writes SQL to Writer
Not defines NOT condition
And implements And with other conditions
IsValid tests if this condition is valid
Or implements Or with other conditions
WriteTo writes SQL to Writer
NotNull defines NOT NULL condition
And implements And with other conditions
IsValid tests if this condition is valid
Or implements Or with other conditions
WriteTo write SQL to Writer
UpdateCond defines an interface that cond could be used with update
Writer defines the interface
Package builder imports 9 packages (graph) and is imported by 48 packages. Updated 2020-12-23. Refresh now. Tools for package owners.