2014-09-08 09:37:01 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-09-08 10:31:32 +00:00
|
|
|
func (qw *MySQLQueryBuilder) Select(fields ...string) QueryBuilder {
|
2014-09-08 09:37:01 +00:00
|
|
|
segment := fmt.Sprintf("SELECT %s", strings.Join(fields, ", "))
|
|
|
|
qw.QueryString = append(qw.QueryString, segment)
|
|
|
|
return qw
|
|
|
|
}
|
|
|
|
|
2014-09-08 10:31:32 +00:00
|
|
|
func (qw *MySQLQueryBuilder) From(tables ...string) QueryBuilder {
|
2014-09-08 09:37:01 +00:00
|
|
|
segment := fmt.Sprintf("FROM %s", strings.Join(tables, ", "))
|
|
|
|
qw.QueryString = append(qw.QueryString, segment)
|
|
|
|
return qw
|
|
|
|
}
|
|
|
|
|
2014-09-08 10:31:32 +00:00
|
|
|
func (qw *MySQLQueryBuilder) Where(cond string) QueryBuilder {
|
2014-09-08 09:37:01 +00:00
|
|
|
qw.QueryString = append(qw.QueryString, "WHERE "+cond)
|
|
|
|
return qw
|
|
|
|
}
|
|
|
|
|
2014-09-08 10:31:32 +00:00
|
|
|
func (qw *MySQLQueryBuilder) LimitOffset(limit int, offset int) QueryBuilder {
|
2014-09-08 09:37:01 +00:00
|
|
|
qw.QueryString = append(qw.QueryString, fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset))
|
|
|
|
return qw
|
|
|
|
}
|
|
|
|
|
2014-09-08 10:31:32 +00:00
|
|
|
func (qw *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder {
|
2014-09-08 09:37:01 +00:00
|
|
|
qw.QueryString = append(qw.QueryString, "INNER JOIN "+table)
|
|
|
|
return qw
|
|
|
|
}
|
|
|
|
|
2014-09-08 10:31:32 +00:00
|
|
|
func (qw *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder {
|
2014-09-08 09:37:01 +00:00
|
|
|
qw.QueryString = append(qw.QueryString, "LEFT JOIN "+table)
|
|
|
|
return qw
|
|
|
|
}
|
|
|
|
|
2014-09-08 10:31:32 +00:00
|
|
|
func (qw *MySQLQueryBuilder) On(cond string) QueryBuilder {
|
2014-09-08 09:37:01 +00:00
|
|
|
qw.QueryString = append(qw.QueryString, "ON "+cond)
|
|
|
|
return qw
|
|
|
|
}
|
|
|
|
|
2014-09-08 10:31:32 +00:00
|
|
|
func (qw *MySQLQueryBuilder) And(cond string) QueryBuilder {
|
2014-09-08 09:37:01 +00:00
|
|
|
qw.QueryString = append(qw.QueryString, "AND "+cond)
|
|
|
|
return qw
|
|
|
|
}
|
|
|
|
|
2014-09-08 10:31:32 +00:00
|
|
|
func (qw *MySQLQueryBuilder) Or(cond string) QueryBuilder {
|
2014-09-08 09:37:01 +00:00
|
|
|
qw.QueryString = append(qw.QueryString, "OR "+cond)
|
|
|
|
return qw
|
|
|
|
}
|
|
|
|
|
2014-09-08 10:31:32 +00:00
|
|
|
func (qw *MySQLQueryBuilder) In(vals ...string) QueryBuilder {
|
2014-09-08 09:37:01 +00:00
|
|
|
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, " ")
|
|
|
|
}
|