mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 18:00:55 +00:00
Merge pull request #804 from ZhengYang/develop
QueryBuilder for building SQL queries quickly
This commit is contained in:
commit
0820e21738
45
orm/qb.go
Normal file
45
orm/qb.go
Normal file
@ -0,0 +1,45 @@
|
||||
// Copyright 2014 beego Author. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package orm
|
||||
|
||||
import "errors"
|
||||
|
||||
type QueryBuilder interface {
|
||||
Select(fields ...string) QueryBuilder
|
||||
From(tables ...string) QueryBuilder
|
||||
Where(cond string) QueryBuilder
|
||||
LimitOffset(limit int, offset int) QueryBuilder
|
||||
InnerJoin(table string) QueryBuilder
|
||||
LeftJoin(table string) QueryBuilder
|
||||
On(cond string) QueryBuilder
|
||||
And(cond string) QueryBuilder
|
||||
Or(cond string) QueryBuilder
|
||||
In(vals ...string) QueryBuilder
|
||||
Subquery(query string, rename string) string
|
||||
String() string
|
||||
}
|
||||
|
||||
func NewQueryBuilder(driver string) (qb QueryBuilder, err error) {
|
||||
if driver == "mysql" {
|
||||
qb = new(MySQLQueryBuilder)
|
||||
} else if driver == "postgres" {
|
||||
err = errors.New("postgres query builder is not supported yet!")
|
||||
} else if driver == "sqlite" {
|
||||
err = errors.New("sqlite query builder is not supported yet!")
|
||||
} else {
|
||||
err = errors.New("unknown driver for query builder!")
|
||||
}
|
||||
return
|
||||
}
|
85
orm/qb_mysql.go
Normal file
85
orm/qb_mysql.go
Normal file
@ -0,0 +1,85 @@
|
||||
// Copyright 2014 beego Author. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package orm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MySQLQueryBuilder struct {
|
||||
QueryString []string
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) Select(fields ...string) QueryBuilder {
|
||||
segment := fmt.Sprintf("SELECT %s", strings.Join(fields, ", "))
|
||||
qw.QueryString = append(qw.QueryString, segment)
|
||||
return qw
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) From(tables ...string) QueryBuilder {
|
||||
segment := fmt.Sprintf("FROM %s", strings.Join(tables, ", "))
|
||||
qw.QueryString = append(qw.QueryString, segment)
|
||||
return qw
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) Where(cond string) QueryBuilder {
|
||||
qw.QueryString = append(qw.QueryString, "WHERE "+cond)
|
||||
return qw
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) LimitOffset(limit int, offset int) QueryBuilder {
|
||||
qw.QueryString = append(qw.QueryString, fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset))
|
||||
return qw
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder {
|
||||
qw.QueryString = append(qw.QueryString, "INNER JOIN "+table)
|
||||
return qw
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder {
|
||||
qw.QueryString = append(qw.QueryString, "LEFT JOIN "+table)
|
||||
return qw
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) On(cond string) QueryBuilder {
|
||||
qw.QueryString = append(qw.QueryString, "ON "+cond)
|
||||
return qw
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) And(cond string) QueryBuilder {
|
||||
qw.QueryString = append(qw.QueryString, "AND "+cond)
|
||||
return qw
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) Or(cond string) QueryBuilder {
|
||||
qw.QueryString = append(qw.QueryString, "OR "+cond)
|
||||
return qw
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) In(vals ...string) QueryBuilder {
|
||||
segment := fmt.Sprintf("IN (%s)", strings.Join(vals, ", "))
|
||||
qw.QueryString = append(qw.QueryString, segment)
|
||||
return qw
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) Subquery(sub string, alias string) string {
|
||||
return fmt.Sprintf("(%s) AS %s", sub, alias)
|
||||
}
|
||||
|
||||
func (qw *MySQLQueryBuilder) String() string {
|
||||
return strings.Join(qw.QueryString, " ")
|
||||
}
|
Loading…
Reference in New Issue
Block a user