1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 03:01:28 +00:00

more complete support for sql language

This commit is contained in:
Zheng Yang 2014-09-09 14:17:12 +08:00
parent 38eb29fa7b
commit 29b7ff84e1
2 changed files with 80 additions and 37 deletions

View File

@ -19,15 +19,22 @@ import "errors"
type QueryBuilder interface { type QueryBuilder interface {
Select(fields ...string) QueryBuilder Select(fields ...string) QueryBuilder
From(tables ...string) QueryBuilder From(tables ...string) QueryBuilder
Where(cond string) QueryBuilder
LimitOffset(limit int, offset int) QueryBuilder
InnerJoin(table string) QueryBuilder InnerJoin(table string) QueryBuilder
LeftJoin(table string) QueryBuilder LeftJoin(table string) QueryBuilder
RightJoin(table string) QueryBuilder
On(cond string) QueryBuilder On(cond string) QueryBuilder
Where(cond string) QueryBuilder
And(cond string) QueryBuilder And(cond string) QueryBuilder
Or(cond string) QueryBuilder Or(cond string) QueryBuilder
In(vals ...string) QueryBuilder In(vals ...string) QueryBuilder
Subquery(query string, rename string) string OrderBy(fields ...string) QueryBuilder
Asc() QueryBuilder
Desc() QueryBuilder
Limit(limit int) QueryBuilder
Offset(offset int) QueryBuilder
GroupBy(fields ...string) QueryBuilder
Having(cond string) QueryBuilder
Subquery(sub string, alias string) string
String() string String() string
} }

View File

@ -16,70 +16,106 @@ package orm
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
) )
type MySQLQueryBuilder struct { type MySQLQueryBuilder struct {
QueryString []string QueryTokens []string
} }
func (qw *MySQLQueryBuilder) Select(fields ...string) QueryBuilder { func (qb *MySQLQueryBuilder) Select(fields ...string) QueryBuilder {
segment := fmt.Sprintf("SELECT %s", strings.Join(fields, ", ")) segment := fmt.Sprintf("SELECT %s", strings.Join(fields, ", "))
qw.QueryString = append(qw.QueryString, segment) qb.QueryTokens = append(qb.QueryTokens, segment)
return qw return qb
} }
func (qw *MySQLQueryBuilder) From(tables ...string) QueryBuilder { func (qb *MySQLQueryBuilder) From(tables ...string) QueryBuilder {
segment := fmt.Sprintf("FROM %s", strings.Join(tables, ", ")) segment := fmt.Sprintf("FROM %s", strings.Join(tables, ", "))
qw.QueryString = append(qw.QueryString, segment) qb.QueryTokens = append(qb.QueryTokens, segment)
return qw return qb
} }
func (qw *MySQLQueryBuilder) Where(cond string) QueryBuilder { func (qb *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder {
qw.QueryString = append(qw.QueryString, "WHERE "+cond) qb.QueryTokens = append(qb.QueryTokens, "INNER JOIN "+table)
return qw return qb
} }
func (qw *MySQLQueryBuilder) LimitOffset(limit int, offset int) QueryBuilder { func (qb *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder {
qw.QueryString = append(qw.QueryString, fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset)) qb.QueryTokens = append(qb.QueryTokens, "LEFT JOIN "+table)
return qw return qb
} }
func (qw *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder { func (qb *MySQLQueryBuilder) RightJoin(table string) QueryBuilder {
qw.QueryString = append(qw.QueryString, "INNER JOIN "+table) qb.QueryTokens = append(qb.QueryTokens, "RIGHT JOIN "+table)
return qw return qb
} }
func (qw *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder { func (qb *MySQLQueryBuilder) On(cond string) QueryBuilder {
qw.QueryString = append(qw.QueryString, "LEFT JOIN "+table) qb.QueryTokens = append(qb.QueryTokens, "ON "+cond)
return qw return qb
} }
func (qw *MySQLQueryBuilder) On(cond string) QueryBuilder { func (qb *MySQLQueryBuilder) Where(cond string) QueryBuilder {
qw.QueryString = append(qw.QueryString, "ON "+cond) qb.QueryTokens = append(qb.QueryTokens, "WHERE "+cond)
return qw return qb
} }
func (qw *MySQLQueryBuilder) And(cond string) QueryBuilder { func (qb *MySQLQueryBuilder) And(cond string) QueryBuilder {
qw.QueryString = append(qw.QueryString, "AND "+cond) qb.QueryTokens = append(qb.QueryTokens, "AND "+cond)
return qw return qb
} }
func (qw *MySQLQueryBuilder) Or(cond string) QueryBuilder { func (qb *MySQLQueryBuilder) Or(cond string) QueryBuilder {
qw.QueryString = append(qw.QueryString, "OR "+cond) qb.QueryTokens = append(qb.QueryTokens, "OR "+cond)
return qw return qb
} }
func (qw *MySQLQueryBuilder) In(vals ...string) QueryBuilder { func (qb *MySQLQueryBuilder) In(vals ...string) QueryBuilder {
segment := fmt.Sprintf("IN (%s)", strings.Join(vals, ", ")) segment := fmt.Sprintf("IN (%s)", strings.Join(vals, ", "))
qw.QueryString = append(qw.QueryString, segment) qb.QueryTokens = append(qb.QueryTokens, segment)
return qw return qb
} }
func (qw *MySQLQueryBuilder) Subquery(sub string, alias string) string { func (qb *MySQLQueryBuilder) OrderBy(fields ...string) QueryBuilder {
qb.QueryTokens = append(qb.QueryTokens, "ORDER BY "+strings.Join(fields, ", "))
return qb
}
func (qb *MySQLQueryBuilder) Asc() QueryBuilder {
qb.QueryTokens = append(qb.QueryTokens, "ASC")
return qb
}
func (qb *MySQLQueryBuilder) Desc() QueryBuilder {
qb.QueryTokens = append(qb.QueryTokens, "DESC")
return qb
}
func (qb *MySQLQueryBuilder) Limit(limit int) QueryBuilder {
qb.QueryTokens = append(qb.QueryTokens, "LIMIT "+strconv.Itoa(limit))
return qb
}
func (qb *MySQLQueryBuilder) Offset(offset int) QueryBuilder {
qb.QueryTokens = append(qb.QueryTokens, "OFFSET "+strconv.Itoa(offset))
return qb
}
func (qb *MySQLQueryBuilder) GroupBy(fields ...string) QueryBuilder {
qb.QueryTokens = append(qb.QueryTokens, "GROUP BY "+strings.Join(fields, ", "))
return qb
}
func (qb *MySQLQueryBuilder) Having(cond string) QueryBuilder {
qb.QueryTokens = append(qb.QueryTokens, "HAVING "+cond)
return qb
}
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)
} }
func (qw *MySQLQueryBuilder) String() string { func (qb *MySQLQueryBuilder) String() string {
return strings.Join(qw.QueryString, " ") return strings.Join(qb.QueryTokens, " ")
} }