mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 15:20:54 +00:00
add more sql keywords
This commit is contained in:
parent
9013f5c6c7
commit
ccab9a7044
@ -34,6 +34,11 @@ type QueryBuilder interface {
|
|||||||
Offset(offset int) QueryBuilder
|
Offset(offset int) QueryBuilder
|
||||||
GroupBy(fields ...string) QueryBuilder
|
GroupBy(fields ...string) QueryBuilder
|
||||||
Having(cond string) QueryBuilder
|
Having(cond string) QueryBuilder
|
||||||
|
Update(tables ...string) QueryBuilder
|
||||||
|
Set(kv ...string) QueryBuilder
|
||||||
|
Delete(tables ...string) QueryBuilder
|
||||||
|
InsertInto(table string, fields ...string) QueryBuilder
|
||||||
|
Values(vals ...string) QueryBuilder
|
||||||
Subquery(sub string, alias string) string
|
Subquery(sub string, alias string) string
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
@ -20,95 +20,124 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const COMMA_SPACE = ", "
|
||||||
|
|
||||||
type MySQLQueryBuilder struct {
|
type MySQLQueryBuilder struct {
|
||||||
QueryTokens []string
|
Tokens []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) Select(fields ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Select(fields ...string) QueryBuilder {
|
||||||
segment := fmt.Sprintf("SELECT %s", strings.Join(fields, ", "))
|
qb.Tokens = append(qb.Tokens, "SELECT", strings.Join(fields, COMMA_SPACE))
|
||||||
qb.QueryTokens = append(qb.QueryTokens, segment)
|
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) From(tables ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) From(tables ...string) QueryBuilder {
|
||||||
segment := fmt.Sprintf("FROM %s", strings.Join(tables, ", "))
|
qb.Tokens = append(qb.Tokens, "FROM", strings.Join(tables, COMMA_SPACE))
|
||||||
qb.QueryTokens = append(qb.QueryTokens, segment)
|
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "INNER JOIN "+table)
|
qb.Tokens = append(qb.Tokens, "INNER JOIN", table)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "LEFT JOIN "+table)
|
qb.Tokens = append(qb.Tokens, "LEFT JOIN", table)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) RightJoin(table string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) RightJoin(table string) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "RIGHT JOIN "+table)
|
qb.Tokens = append(qb.Tokens, "RIGHT JOIN", table)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) On(cond string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) On(cond string) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "ON "+cond)
|
qb.Tokens = append(qb.Tokens, "ON", cond)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) Where(cond string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Where(cond string) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "WHERE "+cond)
|
qb.Tokens = append(qb.Tokens, "WHERE", cond)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) And(cond string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) And(cond string) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "AND "+cond)
|
qb.Tokens = append(qb.Tokens, "AND", cond)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) Or(cond string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Or(cond string) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "OR "+cond)
|
qb.Tokens = append(qb.Tokens, "OR", cond)
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) In(vals ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) In(vals ...string) QueryBuilder {
|
||||||
segment := fmt.Sprintf("IN (%s)", strings.Join(vals, ", "))
|
qb.Tokens = append(qb.Tokens, "IN", "(", strings.Join(vals, COMMA_SPACE), ")")
|
||||||
qb.QueryTokens = append(qb.QueryTokens, segment)
|
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) OrderBy(fields ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) OrderBy(fields ...string) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "ORDER BY "+strings.Join(fields, ", "))
|
qb.Tokens = append(qb.Tokens, "ORDER BY", strings.Join(fields, COMMA_SPACE))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) Asc() QueryBuilder {
|
func (qb *MySQLQueryBuilder) Asc() QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "ASC")
|
qb.Tokens = append(qb.Tokens, "ASC")
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) Desc() QueryBuilder {
|
func (qb *MySQLQueryBuilder) Desc() QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "DESC")
|
qb.Tokens = append(qb.Tokens, "DESC")
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) Limit(limit int) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Limit(limit int) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "LIMIT "+strconv.Itoa(limit))
|
qb.Tokens = append(qb.Tokens, "LIMIT", strconv.Itoa(limit))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) Offset(offset int) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Offset(offset int) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "OFFSET "+strconv.Itoa(offset))
|
qb.Tokens = append(qb.Tokens, "OFFSET", strconv.Itoa(offset))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) GroupBy(fields ...string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) GroupBy(fields ...string) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "GROUP BY "+strings.Join(fields, ", "))
|
qb.Tokens = append(qb.Tokens, "GROUP BY", strings.Join(fields, COMMA_SPACE))
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) Having(cond string) QueryBuilder {
|
func (qb *MySQLQueryBuilder) Having(cond string) QueryBuilder {
|
||||||
qb.QueryTokens = append(qb.QueryTokens, "HAVING "+cond)
|
qb.Tokens = append(qb.Tokens, "HAVING", cond)
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (qb *MySQLQueryBuilder) Update(tables ...string) QueryBuilder {
|
||||||
|
qb.Tokens = append(qb.Tokens, "UPDATE", strings.Join(tables, COMMA_SPACE))
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (qb *MySQLQueryBuilder) Set(kv ...string) QueryBuilder {
|
||||||
|
qb.Tokens = append(qb.Tokens, "SET", strings.Join(kv, COMMA_SPACE))
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (qb *MySQLQueryBuilder) Delete(tables ...string) QueryBuilder {
|
||||||
|
qb.Tokens = append(qb.Tokens, "DELETE", strings.Join(tables, COMMA_SPACE))
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (qb *MySQLQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder {
|
||||||
|
qb.Tokens = append(qb.Tokens, "INSERT INTO", table)
|
||||||
|
if len(fields) != 0 {
|
||||||
|
fieldsStr := strings.Join(fields, COMMA_SPACE)
|
||||||
|
qb.Tokens = append(qb.Tokens, "(", fieldsStr, ")")
|
||||||
|
}
|
||||||
|
return qb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (qb *MySQLQueryBuilder) Values(vals ...string) QueryBuilder {
|
||||||
|
valsStr := strings.Join(vals, COMMA_SPACE)
|
||||||
|
qb.Tokens = append(qb.Tokens, "VALUES", "(", valsStr, ")")
|
||||||
return qb
|
return qb
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,5 +146,5 @@ func (qb *MySQLQueryBuilder) Subquery(sub string, alias string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (qb *MySQLQueryBuilder) String() string {
|
func (qb *MySQLQueryBuilder) String() string {
|
||||||
return strings.Join(qb.QueryTokens, " ")
|
return strings.Join(qb.Tokens, " ")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user