mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 12:00:55 +00:00
PostgresQueryBuilder
This commit is contained in:
parent
23792401b5
commit
c6c9ad46f9
@ -2620,3 +2620,40 @@ func TestStrPkInsert(t *testing.T) {
|
|||||||
throwFailNow(t, AssertIs(vForTesting2.Value, value2))
|
throwFailNow(t, AssertIs(vForTesting2.Value, value2))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPSQueryBuilder(t *testing.T) {
|
||||||
|
// only test postgres
|
||||||
|
if dORM.Driver().Type() != 4 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var user User
|
||||||
|
var l []userProfile
|
||||||
|
o := NewOrm()
|
||||||
|
|
||||||
|
qb, err := NewQueryBuilder("postgres")
|
||||||
|
if err != nil {
|
||||||
|
throwFailNow(t, err)
|
||||||
|
}
|
||||||
|
qb.Select("user.id", "user.user_name").
|
||||||
|
From("user").Where("id = ?").OrderBy("user_name").
|
||||||
|
Desc().Limit(1).Offset(0)
|
||||||
|
sql := qb.String()
|
||||||
|
err = o.Raw(sql, 2).QueryRow(&user)
|
||||||
|
if err != nil {
|
||||||
|
throwFailNow(t, err)
|
||||||
|
}
|
||||||
|
throwFail(t, AssertIs(user.UserName, "slene"))
|
||||||
|
|
||||||
|
qb.Select("*").
|
||||||
|
From("user_profile").InnerJoin("user").
|
||||||
|
On("user_profile.id = user.id")
|
||||||
|
sql = qb.String()
|
||||||
|
num, err := o.Raw(sql).QueryRows(&l)
|
||||||
|
if err != nil {
|
||||||
|
throwFailNow(t, err)
|
||||||
|
}
|
||||||
|
throwFailNow(t, AssertIs(num, 1))
|
||||||
|
throwFailNow(t, AssertIs(l[0].UserName, "astaxie"))
|
||||||
|
throwFailNow(t, AssertIs(l[0].Age, 30))
|
||||||
|
}
|
||||||
|
@ -52,7 +52,7 @@ func NewQueryBuilder(driver string) (qb QueryBuilder, err error) {
|
|||||||
} else if driver == "tidb" {
|
} else if driver == "tidb" {
|
||||||
qb = new(TiDBQueryBuilder)
|
qb = new(TiDBQueryBuilder)
|
||||||
} else if driver == "postgres" {
|
} else if driver == "postgres" {
|
||||||
err = errors.New("postgres query builder is not supported yet")
|
qb = new(PostgresQueryBuilder)
|
||||||
} else if driver == "sqlite" {
|
} else if driver == "sqlite" {
|
||||||
err = errors.New("sqlite query builder is not supported yet")
|
err = errors.New("sqlite query builder is not supported yet")
|
||||||
} else {
|
} else {
|
||||||
|
@ -25,144 +25,144 @@ const CommaSpace = ", "
|
|||||||
|
|
||||||
// MySQLQueryBuilder is the SQL build
|
// MySQLQueryBuilder is the SQL build
|
||||||
type MySQLQueryBuilder struct {
|
type MySQLQueryBuilder struct {
|
||||||
Tokens []string
|
tokens []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select will join the fields
|
// Select will join the fields
|
||||||
func (qb *MySQLQueryBuilder) Select(fields ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Select(fields ...string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "SELECT", strings.Join(fields, CommaSpace))
|
qb.tokens = append(qb.tokens, "SELECT", strings.Join(fields, CommaSpace))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForUpdate add the FOR UPDATE clause
|
// ForUpdate add the FOR UPDATE clause
|
||||||
func (qb *MySQLQueryBuilder) ForUpdate() QueryBuilder {
|
func (qb *MySQLQueryBuilder) ForUpdate() QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "FOR UPDATE")
|
qb.tokens = append(qb.tokens, "FOR UPDATE")
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// From join the tables
|
// From join the tables
|
||||||
func (qb *MySQLQueryBuilder) From(tables ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) From(tables ...string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "FROM", strings.Join(tables, CommaSpace))
|
qb.tokens = append(qb.tokens, "FROM", strings.Join(tables, CommaSpace))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// InnerJoin INNER JOIN the table
|
// InnerJoin INNER JOIN the table
|
||||||
func (qb *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "INNER JOIN", table)
|
qb.tokens = append(qb.tokens, "INNER JOIN", table)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// LeftJoin LEFT JOIN the table
|
// LeftJoin LEFT JOIN the table
|
||||||
func (qb *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "LEFT JOIN", table)
|
qb.tokens = append(qb.tokens, "LEFT JOIN", table)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// RightJoin RIGHT JOIN the table
|
// RightJoin RIGHT JOIN the table
|
||||||
func (qb *MySQLQueryBuilder) RightJoin(table string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) RightJoin(table string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "RIGHT JOIN", table)
|
qb.tokens = append(qb.tokens, "RIGHT JOIN", table)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// On join with on cond
|
// On join with on cond
|
||||||
func (qb *MySQLQueryBuilder) On(cond string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) On(cond string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "ON", cond)
|
qb.tokens = append(qb.tokens, "ON", cond)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Where join the Where cond
|
// Where join the Where cond
|
||||||
func (qb *MySQLQueryBuilder) Where(cond string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Where(cond string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "WHERE", cond)
|
qb.tokens = append(qb.tokens, "WHERE", cond)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// And join the and cond
|
// And join the and cond
|
||||||
func (qb *MySQLQueryBuilder) And(cond string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) And(cond string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "AND", cond)
|
qb.tokens = append(qb.tokens, "AND", cond)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Or join the or cond
|
// Or join the or cond
|
||||||
func (qb *MySQLQueryBuilder) Or(cond string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Or(cond string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "OR", cond)
|
qb.tokens = append(qb.tokens, "OR", cond)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// In join the IN (vals)
|
// In join the IN (vals)
|
||||||
func (qb *MySQLQueryBuilder) In(vals ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) In(vals ...string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "IN", "(", strings.Join(vals, CommaSpace), ")")
|
qb.tokens = append(qb.tokens, "IN", "(", strings.Join(vals, CommaSpace), ")")
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// OrderBy join the Order by fields
|
// OrderBy join the Order by fields
|
||||||
func (qb *MySQLQueryBuilder) OrderBy(fields ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) OrderBy(fields ...string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "ORDER BY", strings.Join(fields, CommaSpace))
|
qb.tokens = append(qb.tokens, "ORDER BY", strings.Join(fields, CommaSpace))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Asc join the asc
|
// Asc join the asc
|
||||||
func (qb *MySQLQueryBuilder) Asc() QueryBuilder {
|
func (qb *MySQLQueryBuilder) Asc() QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "ASC")
|
qb.tokens = append(qb.tokens, "ASC")
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Desc join the desc
|
// Desc join the desc
|
||||||
func (qb *MySQLQueryBuilder) Desc() QueryBuilder {
|
func (qb *MySQLQueryBuilder) Desc() QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "DESC")
|
qb.tokens = append(qb.tokens, "DESC")
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Limit join the limit num
|
// Limit join the limit num
|
||||||
func (qb *MySQLQueryBuilder) Limit(limit int) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Limit(limit int) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "LIMIT", strconv.Itoa(limit))
|
qb.tokens = append(qb.tokens, "LIMIT", strconv.Itoa(limit))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Offset join the offset num
|
// Offset join the offset num
|
||||||
func (qb *MySQLQueryBuilder) Offset(offset int) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Offset(offset int) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "OFFSET", strconv.Itoa(offset))
|
qb.tokens = append(qb.tokens, "OFFSET", strconv.Itoa(offset))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// GroupBy join the Group by fields
|
// GroupBy join the Group by fields
|
||||||
func (qb *MySQLQueryBuilder) GroupBy(fields ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) GroupBy(fields ...string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "GROUP BY", strings.Join(fields, CommaSpace))
|
qb.tokens = append(qb.tokens, "GROUP BY", strings.Join(fields, CommaSpace))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Having join the Having cond
|
// Having join the Having cond
|
||||||
func (qb *MySQLQueryBuilder) Having(cond string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Having(cond string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "HAVING", cond)
|
qb.tokens = append(qb.tokens, "HAVING", cond)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update join the update table
|
// Update join the update table
|
||||||
func (qb *MySQLQueryBuilder) Update(tables ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Update(tables ...string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "UPDATE", strings.Join(tables, CommaSpace))
|
qb.tokens = append(qb.tokens, "UPDATE", strings.Join(tables, CommaSpace))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set join the set kv
|
// Set join the set kv
|
||||||
func (qb *MySQLQueryBuilder) Set(kv ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Set(kv ...string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "SET", strings.Join(kv, CommaSpace))
|
qb.tokens = append(qb.tokens, "SET", strings.Join(kv, CommaSpace))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete join the Delete tables
|
// Delete join the Delete tables
|
||||||
func (qb *MySQLQueryBuilder) Delete(tables ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Delete(tables ...string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "DELETE")
|
qb.tokens = append(qb.tokens, "DELETE")
|
||||||
if len(tables) != 0 {
|
if len(tables) != 0 {
|
||||||
qb.Tokens = append(qb.Tokens, strings.Join(tables, CommaSpace))
|
qb.tokens = append(qb.tokens, strings.Join(tables, CommaSpace))
|
||||||
}
|
}
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertInto join the insert SQL
|
// InsertInto join the insert SQL
|
||||||
func (qb *MySQLQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder {
|
||||||
qb.Tokens = append(qb.Tokens, "INSERT INTO", table)
|
qb.tokens = append(qb.tokens, "INSERT INTO", table)
|
||||||
if len(fields) != 0 {
|
if len(fields) != 0 {
|
||||||
fieldsStr := strings.Join(fields, CommaSpace)
|
fieldsStr := strings.Join(fields, CommaSpace)
|
||||||
qb.Tokens = append(qb.Tokens, "(", fieldsStr, ")")
|
qb.tokens = append(qb.tokens, "(", fieldsStr, ")")
|
||||||
}
|
}
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
@ -170,7 +170,7 @@ func (qb *MySQLQueryBuilder) InsertInto(table string, fields ...string) QueryBui
|
|||||||
// Values join the Values(vals)
|
// Values join the Values(vals)
|
||||||
func (qb *MySQLQueryBuilder) Values(vals ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Values(vals ...string) QueryBuilder {
|
||||||
valsStr := strings.Join(vals, CommaSpace)
|
valsStr := strings.Join(vals, CommaSpace)
|
||||||
qb.Tokens = append(qb.Tokens, "VALUES", "(", valsStr, ")")
|
qb.tokens = append(qb.tokens, "VALUES", "(", valsStr, ")")
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +179,9 @@ func (qb *MySQLQueryBuilder) Subquery(sub string, alias string) string {
|
|||||||
return fmt.Sprintf("(%s) AS %s", sub, alias)
|
return fmt.Sprintf("(%s) AS %s", sub, alias)
|
||||||
}
|
}
|
||||||
|
|
||||||
// String join all Tokens
|
// String join all tokens
|
||||||
func (qb *MySQLQueryBuilder) String() string {
|
func (qb *MySQLQueryBuilder) String() string {
|
||||||
return strings.Join(qb.Tokens, " ")
|
s := strings.Join(qb.tokens, " ")
|
||||||
|
qb.tokens = qb.tokens[:0]
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
221
pkg/client/orm/qb_postgres.go
Normal file
221
pkg/client/orm/qb_postgres.go
Normal file
@ -0,0 +1,221 @@
|
|||||||
|
package orm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var quote string = `"`
|
||||||
|
|
||||||
|
// PostgresQueryBuilder is the SQL build
|
||||||
|
type PostgresQueryBuilder struct {
|
||||||
|
tokens []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func processingStr(str []string) string {
|
||||||
|
s := strings.Join(str, `","`)
|
||||||
|
s = fmt.Sprintf("%s%s%s", quote, s, quote)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select will join the fields
|
||||||
|
func (qb *PostgresQueryBuilder) Select(fields ...string) QueryBuilder {
|
||||||
|
|
||||||
|
var str string
|
||||||
|
n := len(fields)
|
||||||
|
|
||||||
|
if fields[0] == "*" {
|
||||||
|
str = "*"
|
||||||
|
} else {
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
sli := strings.Split(fields[i], ".")
|
||||||
|
s := strings.Join(sli, `"."`)
|
||||||
|
s = fmt.Sprintf("%s%s%s", quote, s, quote)
|
||||||
|
if n == 1 || i == n-1 {
|
||||||
|
str += s
|
||||||
|
} else {
|
||||||
|
str += s + ","
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
qb.tokens = append(qb.tokens, "SELECT", str)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForUpdate add the FOR UPDATE clause
|
||||||
|
func (qb *PostgresQueryBuilder) ForUpdate() QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "FOR UPDATE")
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// From join the tables
|
||||||
|
func (qb *PostgresQueryBuilder) From(tables ...string) QueryBuilder {
|
||||||
|
str := processingStr(tables)
|
||||||
|
qb.tokens = append(qb.tokens, "FROM", str)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// InnerJoin INNER JOIN the table
|
||||||
|
func (qb *PostgresQueryBuilder) InnerJoin(table string) QueryBuilder {
|
||||||
|
str := fmt.Sprintf("%s%s%s", quote, table, quote)
|
||||||
|
qb.tokens = append(qb.tokens, "INNER JOIN", str)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// LeftJoin LEFT JOIN the table
|
||||||
|
func (qb *PostgresQueryBuilder) LeftJoin(table string) QueryBuilder {
|
||||||
|
str := fmt.Sprintf("%s%s%s", quote, table, quote)
|
||||||
|
qb.tokens = append(qb.tokens, "LEFT JOIN", str)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// RightJoin RIGHT JOIN the table
|
||||||
|
func (qb *PostgresQueryBuilder) RightJoin(table string) QueryBuilder {
|
||||||
|
str := fmt.Sprintf("%s%s%s", quote, table, quote)
|
||||||
|
qb.tokens = append(qb.tokens, "RIGHT JOIN", str)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// On join with on cond
|
||||||
|
func (qb *PostgresQueryBuilder) On(cond string) QueryBuilder {
|
||||||
|
|
||||||
|
var str string
|
||||||
|
cond = strings.Replace(cond, " ", "", -1)
|
||||||
|
slice := strings.Split(cond, "=")
|
||||||
|
for i := 0; i < len(slice); i++ {
|
||||||
|
sli := strings.Split(slice[i], ".")
|
||||||
|
s := strings.Join(sli, `"."`)
|
||||||
|
s = fmt.Sprintf("%s%s%s", quote, s, quote)
|
||||||
|
if i == 0 {
|
||||||
|
str = s + " =" + " "
|
||||||
|
} else {
|
||||||
|
str += s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
qb.tokens = append(qb.tokens, "ON", str)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where join the Where cond
|
||||||
|
func (qb *PostgresQueryBuilder) Where(cond string) QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "WHERE", cond)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// And join the and cond
|
||||||
|
func (qb *PostgresQueryBuilder) And(cond string) QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "AND", cond)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or join the or cond
|
||||||
|
func (qb *PostgresQueryBuilder) Or(cond string) QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "OR", cond)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// In join the IN (vals)
|
||||||
|
func (qb *PostgresQueryBuilder) In(vals ...string) QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "IN", "(", strings.Join(vals, CommaSpace), ")")
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrderBy join the Order by fields
|
||||||
|
func (qb *PostgresQueryBuilder) OrderBy(fields ...string) QueryBuilder {
|
||||||
|
str := processingStr(fields)
|
||||||
|
qb.tokens = append(qb.tokens, "ORDER BY", str)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Asc join the asc
|
||||||
|
func (qb *PostgresQueryBuilder) Asc() QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "ASC")
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Desc join the desc
|
||||||
|
func (qb *PostgresQueryBuilder) Desc() QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "DESC")
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Limit join the limit num
|
||||||
|
func (qb *PostgresQueryBuilder) Limit(limit int) QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "LIMIT", strconv.Itoa(limit))
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Offset join the offset num
|
||||||
|
func (qb *PostgresQueryBuilder) Offset(offset int) QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "OFFSET", strconv.Itoa(offset))
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// GroupBy join the Group by fields
|
||||||
|
func (qb *PostgresQueryBuilder) GroupBy(fields ...string) QueryBuilder {
|
||||||
|
str := processingStr(fields)
|
||||||
|
qb.tokens = append(qb.tokens, "GROUP BY", str)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Having join the Having cond
|
||||||
|
func (qb *PostgresQueryBuilder) Having(cond string) QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "HAVING", cond)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update join the update table
|
||||||
|
func (qb *PostgresQueryBuilder) Update(tables ...string) QueryBuilder {
|
||||||
|
str := processingStr(tables)
|
||||||
|
qb.tokens = append(qb.tokens, "UPDATE", str)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set join the set kv
|
||||||
|
func (qb *PostgresQueryBuilder) Set(kv ...string) QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "SET", strings.Join(kv, CommaSpace))
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete join the Delete tables
|
||||||
|
func (qb *PostgresQueryBuilder) Delete(tables ...string) QueryBuilder {
|
||||||
|
qb.tokens = append(qb.tokens, "DELETE")
|
||||||
|
if len(tables) != 0 {
|
||||||
|
str := processingStr(tables)
|
||||||
|
qb.tokens = append(qb.tokens, str)
|
||||||
|
}
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsertInto join the insert SQL
|
||||||
|
func (qb *PostgresQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder {
|
||||||
|
str := fmt.Sprintf("%s%s%s", quote, table, quote)
|
||||||
|
qb.tokens = append(qb.tokens, "INSERT INTO", str)
|
||||||
|
if len(fields) != 0 {
|
||||||
|
fieldsStr := strings.Join(fields, CommaSpace)
|
||||||
|
qb.tokens = append(qb.tokens, "(", fieldsStr, ")")
|
||||||
|
}
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values join the Values(vals)
|
||||||
|
func (qb *PostgresQueryBuilder) Values(vals ...string) QueryBuilder {
|
||||||
|
valsStr := strings.Join(vals, CommaSpace)
|
||||||
|
qb.tokens = append(qb.tokens, "VALUES", "(", valsStr, ")")
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subquery join the sub as alias
|
||||||
|
func (qb *PostgresQueryBuilder) Subquery(sub string, alias string) string {
|
||||||
|
return fmt.Sprintf("(%s) AS %s", sub, alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
// String join all tokens
|
||||||
|
func (qb *PostgresQueryBuilder) String() string {
|
||||||
|
s := strings.Join(qb.tokens, " ")
|
||||||
|
qb.tokens = qb.tokens[:0]
|
||||||
|
return s
|
||||||
|
}
|
@ -14,169 +14,8 @@
|
|||||||
|
|
||||||
package orm
|
package orm
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TiDBQueryBuilder is the SQL build
|
// TiDBQueryBuilder is the SQL build
|
||||||
type TiDBQueryBuilder struct {
|
type TiDBQueryBuilder struct {
|
||||||
Tokens []string
|
MySQLQueryBuilder
|
||||||
}
|
tokens []string
|
||||||
|
|
||||||
// Select will join the fields
|
|
||||||
func (qb *TiDBQueryBuilder) Select(fields ...string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "SELECT", strings.Join(fields, CommaSpace))
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// ForUpdate add the FOR UPDATE clause
|
|
||||||
func (qb *TiDBQueryBuilder) ForUpdate() QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "FOR UPDATE")
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// From join the tables
|
|
||||||
func (qb *TiDBQueryBuilder) From(tables ...string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "FROM", strings.Join(tables, CommaSpace))
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// InnerJoin INNER JOIN the table
|
|
||||||
func (qb *TiDBQueryBuilder) InnerJoin(table string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "INNER JOIN", table)
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// LeftJoin LEFT JOIN the table
|
|
||||||
func (qb *TiDBQueryBuilder) LeftJoin(table string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "LEFT JOIN", table)
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// RightJoin RIGHT JOIN the table
|
|
||||||
func (qb *TiDBQueryBuilder) RightJoin(table string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "RIGHT JOIN", table)
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// On join with on cond
|
|
||||||
func (qb *TiDBQueryBuilder) On(cond string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "ON", cond)
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Where join the Where cond
|
|
||||||
func (qb *TiDBQueryBuilder) Where(cond string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "WHERE", cond)
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// And join the and cond
|
|
||||||
func (qb *TiDBQueryBuilder) And(cond string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "AND", cond)
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or join the or cond
|
|
||||||
func (qb *TiDBQueryBuilder) Or(cond string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "OR", cond)
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// In join the IN (vals)
|
|
||||||
func (qb *TiDBQueryBuilder) In(vals ...string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "IN", "(", strings.Join(vals, CommaSpace), ")")
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// OrderBy join the Order by fields
|
|
||||||
func (qb *TiDBQueryBuilder) OrderBy(fields ...string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "ORDER BY", strings.Join(fields, CommaSpace))
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Asc join the asc
|
|
||||||
func (qb *TiDBQueryBuilder) Asc() QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "ASC")
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Desc join the desc
|
|
||||||
func (qb *TiDBQueryBuilder) Desc() QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "DESC")
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit join the limit num
|
|
||||||
func (qb *TiDBQueryBuilder) Limit(limit int) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "LIMIT", strconv.Itoa(limit))
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Offset join the offset num
|
|
||||||
func (qb *TiDBQueryBuilder) Offset(offset int) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "OFFSET", strconv.Itoa(offset))
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// GroupBy join the Group by fields
|
|
||||||
func (qb *TiDBQueryBuilder) GroupBy(fields ...string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "GROUP BY", strings.Join(fields, CommaSpace))
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Having join the Having cond
|
|
||||||
func (qb *TiDBQueryBuilder) Having(cond string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "HAVING", cond)
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update join the update table
|
|
||||||
func (qb *TiDBQueryBuilder) Update(tables ...string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "UPDATE", strings.Join(tables, CommaSpace))
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set join the set kv
|
|
||||||
func (qb *TiDBQueryBuilder) Set(kv ...string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "SET", strings.Join(kv, CommaSpace))
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete join the Delete tables
|
|
||||||
func (qb *TiDBQueryBuilder) Delete(tables ...string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "DELETE")
|
|
||||||
if len(tables) != 0 {
|
|
||||||
qb.Tokens = append(qb.Tokens, strings.Join(tables, CommaSpace))
|
|
||||||
}
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// InsertInto join the insert SQL
|
|
||||||
func (qb *TiDBQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder {
|
|
||||||
qb.Tokens = append(qb.Tokens, "INSERT INTO", table)
|
|
||||||
if len(fields) != 0 {
|
|
||||||
fieldsStr := strings.Join(fields, CommaSpace)
|
|
||||||
qb.Tokens = append(qb.Tokens, "(", fieldsStr, ")")
|
|
||||||
}
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Values join the Values(vals)
|
|
||||||
func (qb *TiDBQueryBuilder) Values(vals ...string) QueryBuilder {
|
|
||||||
valsStr := strings.Join(vals, CommaSpace)
|
|
||||||
qb.Tokens = append(qb.Tokens, "VALUES", "(", valsStr, ")")
|
|
||||||
return qb
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subquery join the sub as alias
|
|
||||||
func (qb *TiDBQueryBuilder) Subquery(sub string, alias string) string {
|
|
||||||
return fmt.Sprintf("(%s) AS %s", sub, alias)
|
|
||||||
}
|
|
||||||
|
|
||||||
// String join all Tokens
|
|
||||||
func (qb *TiDBQueryBuilder) String() string {
|
|
||||||
return strings.Join(qb.Tokens, " ")
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user