0820e217 by astaxie

Merge pull request #804 from ZhengYang/develop

QueryBuilder for building SQL queries quickly
2 parents 647a4751 38eb29fa
1 // Copyright 2014 beego Author. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package orm
16
17 import "errors"
18
19 type QueryBuilder interface {
20 Select(fields ...string) QueryBuilder
21 From(tables ...string) QueryBuilder
22 Where(cond string) QueryBuilder
23 LimitOffset(limit int, offset int) QueryBuilder
24 InnerJoin(table string) QueryBuilder
25 LeftJoin(table string) QueryBuilder
26 On(cond string) QueryBuilder
27 And(cond string) QueryBuilder
28 Or(cond string) QueryBuilder
29 In(vals ...string) QueryBuilder
30 Subquery(query string, rename string) string
31 String() string
32 }
33
34 func NewQueryBuilder(driver string) (qb QueryBuilder, err error) {
35 if driver == "mysql" {
36 qb = new(MySQLQueryBuilder)
37 } else if driver == "postgres" {
38 err = errors.New("postgres query builder is not supported yet!")
39 } else if driver == "sqlite" {
40 err = errors.New("sqlite query builder is not supported yet!")
41 } else {
42 err = errors.New("unknown driver for query builder!")
43 }
44 return
45 }
1 // Copyright 2014 beego Author. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package orm
16
17 import (
18 "fmt"
19 "strings"
20 )
21
22 type MySQLQueryBuilder struct {
23 QueryString []string
24 }
25
26 func (qw *MySQLQueryBuilder) Select(fields ...string) QueryBuilder {
27 segment := fmt.Sprintf("SELECT %s", strings.Join(fields, ", "))
28 qw.QueryString = append(qw.QueryString, segment)
29 return qw
30 }
31
32 func (qw *MySQLQueryBuilder) From(tables ...string) QueryBuilder {
33 segment := fmt.Sprintf("FROM %s", strings.Join(tables, ", "))
34 qw.QueryString = append(qw.QueryString, segment)
35 return qw
36 }
37
38 func (qw *MySQLQueryBuilder) Where(cond string) QueryBuilder {
39 qw.QueryString = append(qw.QueryString, "WHERE "+cond)
40 return qw
41 }
42
43 func (qw *MySQLQueryBuilder) LimitOffset(limit int, offset int) QueryBuilder {
44 qw.QueryString = append(qw.QueryString, fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset))
45 return qw
46 }
47
48 func (qw *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder {
49 qw.QueryString = append(qw.QueryString, "INNER JOIN "+table)
50 return qw
51 }
52
53 func (qw *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder {
54 qw.QueryString = append(qw.QueryString, "LEFT JOIN "+table)
55 return qw
56 }
57
58 func (qw *MySQLQueryBuilder) On(cond string) QueryBuilder {
59 qw.QueryString = append(qw.QueryString, "ON "+cond)
60 return qw
61 }
62
63 func (qw *MySQLQueryBuilder) And(cond string) QueryBuilder {
64 qw.QueryString = append(qw.QueryString, "AND "+cond)
65 return qw
66 }
67
68 func (qw *MySQLQueryBuilder) Or(cond string) QueryBuilder {
69 qw.QueryString = append(qw.QueryString, "OR "+cond)
70 return qw
71 }
72
73 func (qw *MySQLQueryBuilder) In(vals ...string) QueryBuilder {
74 segment := fmt.Sprintf("IN (%s)", strings.Join(vals, ", "))
75 qw.QueryString = append(qw.QueryString, segment)
76 return qw
77 }
78
79 func (qw *MySQLQueryBuilder) Subquery(sub string, alias string) string {
80 return fmt.Sprintf("(%s) AS %s", sub, alias)
81 }
82
83 func (qw *MySQLQueryBuilder) String() string {
84 return strings.Join(qw.QueryString, " ")
85 }
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!