sqler

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2021 License: MIT Imports: 5 Imported by: 0

README

Sqler

A SQL builder for Golang.

Demo | Test

func main() {
	q := sqler.New()
	q.SelectString("id, username, nickname, type, age, status, createdAt")
	q.From("users")
	q.Where(func(where *sqler.Condition) {
		where.And("status = ?", 1)
		where.Or(func(or *sqler.Or) {
			or.Add("type = ?", 1)
			or.Add("type = ?", 2)
		})
	})
	q.Order(func(order *sqler.Order) {
		order.Add("age", sqler.DESC)
		order.Add("id", sqler.ASC)
	})
	// q.OrderString("age desc, id asc")
	q.Limit(0, 10)

	fmt.Println(q.Do()) // select id, username, nickname, type, age, status, createdAt from users where status = ? and (type = ? or type = ?) order by age desc, id asc limit ?, ? [1 1 2 0 10] <nil>
	fmt.Println(q.DoCount()) // select count(1) as count from users where status = ? and (type = ? or type = ?) [1 1 2] <nil>
}
Condition

sqler.NewCondition(name string) Use for where, on and having condition builder.

func main() {
	w := sqler.NewCondition("where")

	w.And("field1 = ?", 1)
	w.And("field2 in(?)", []int{21, 22, 23, 24})
	w.Or(func(or *sqler.Or) {
		or.Add("field3 = ?", 3)
		or.And(func(and *sqler.Condition) {
			and.And("field4 = ?", 4)
			and.And("field5 = ?", 5)
		})
	})
	sql, args, err := w.Do()
	fmt.Println("result:", sql, args, err) // "where field1 = ? and field2 in(?, ?, ?, ?) and (field3 = ? or (field4 = ? and field5 = ?))" [1 21 22 23 24 3 4 5] <nil>
}
Order

sqler.Order{} Use for order by builder.

func main() {
	o := sqler.Order{}
	fmt.Println("sql:", o.String()) // ""
	o.Add("field1", sqler.DESC)
	o.Add("field2", sqler.ASC)
	o.Add("field3", sqler.DESC)
	fmt.Println("sql:", o.String()) // "order by field1 desc, field2 asc, field3 desc"
}
Group

sqler.Group{} Use for group by builder.

func main() {
	g := sqler.Group{}
	fmt.Println("sql:", g.String()) // ""
	g.Add("field1")
	g.Add("field2")
	g.Add("field3")
	fmt.Println("sql:", g.String()) // "group by field1, field2, field3"
}

Documentation

Overview

copy from github.com/jmoiron/sqlx/bind.go

Index

Constants

View Source
const (
	DESC = "desc"
	ASC  = "asc"
)

Variables

This section is empty.

Functions

func In

func In(query string, args ...interface{}) (string, []interface{}, error)

In expands slice values in args, returning the modified query string and a new arg list that can be executed by a database. The `query` should use the `?` bindVar. The return value uses the `?` bindVar.

Types

type Block

type Block struct {
	// contains filtered or unexported fields
}

func (*Block) Add

func (b *Block) Add(sql string, args ...interface{})

func (*Block) Join

func (b *Block) Join(sep string) (string, []interface{}, error)

func (*Block) Set

func (b *Block) Set(sql string, args ...interface{})

type Condition

type Condition struct {
	// contains filtered or unexported fields
}

Condition builder, use for `where`, `on`, `having`

func NewCondition

func NewCondition(name string) *Condition

func (*Condition) And

func (c *Condition) And(sql string, args ...interface{})

func (*Condition) Do

func (c *Condition) Do() (string, []interface{}, error)

func (*Condition) Or

func (c *Condition) Or(orFn func(or *Or))

type Group

type Group struct {
	// contains filtered or unexported fields
}

func (*Group) Add

func (g *Group) Add(field string)

func (*Group) String

func (g *Group) String() string

type Or

type Or struct {
	// contains filtered or unexported fields
}

func (*Or) Add

func (o *Or) Add(sql string, args ...interface{})

func (*Or) And

func (o *Or) And(andFn func(and *Condition))

func (*Or) Do

func (o *Or) Do() (string, []interface{}, error)

type Order

type Order struct {
	// contains filtered or unexported fields
}

func (*Order) Add

func (o *Order) Add(field string, sort string)

func (*Order) String

func (o *Order) String() string

type Sqler

type Sqler struct {
	// contains filtered or unexported fields
}

func New

func New() *Sqler

func (*Sqler) Do

func (s *Sqler) Do() (string, []interface{}, error)

func (*Sqler) DoCount

func (s *Sqler) DoCount(countSql ...string) (string, []interface{}, error)

func (*Sqler) From

func (s *Sqler) From(sql string, args ...interface{})

func (*Sqler) Group

func (s *Sqler) Group(groupFn func(group *Group))

func (*Sqler) GroupString

func (s *Sqler) GroupString(fields ...string)

func (*Sqler) Having

func (s *Sqler) Having(condition func(having *Condition))

func (*Sqler) HavingString

func (s *Sqler) HavingString(sql string, args ...interface{})

func (*Sqler) Join

func (s *Sqler) Join(sql string, condition func(on *Condition))

Join table, can be called multiple times

func (*Sqler) JoinString

func (s *Sqler) JoinString(sql string, args ...interface{})

Join table, can be called multiple times

func (*Sqler) Limit

func (s *Sqler) Limit(offset uint, limit uint)

func (*Sqler) Order

func (s *Sqler) Order(order func(order *Order))

func (*Sqler) OrderString

func (s *Sqler) OrderString(sql string)

func (*Sqler) Select

func (s *Sqler) Select(selectFn func(field *Block))

func (*Sqler) SelectString

func (s *Sqler) SelectString(sql string, args ...interface{})

func (*Sqler) Where

func (s *Sqler) Where(condition func(where *Condition))

Where builder, cannot be called more than once

func (*Sqler) WhereString

func (s *Sqler) WhereString(sql string, args ...interface{})

Where builder, cannot be called more than once

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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