added query builder for orm
Showing
2 changed files
with
115 additions
and
0 deletions
orm/qb.go
0 → 100644
| 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 | type QueryBuilder interface { | ||
| 18 | Select(fields ...string) QueryWriter | ||
| 19 | From(tables ...string) QueryWriter | ||
| 20 | Where(cond string) QueryWriter | ||
| 21 | LimitOffset(limit int, offset int) QueryWriter | ||
| 22 | InnerJoin(table string) QueryWriter | ||
| 23 | LeftJoin(table string) QueryWriter | ||
| 24 | On(cond string) QueryWriter | ||
| 25 | And(cond string) QueryWriter | ||
| 26 | Or(cond string) QueryWriter | ||
| 27 | In(vals ...string) QueryWriter | ||
| 28 | Subquery(query string, rename string) string | ||
| 29 | String() string | ||
| 30 | } |
orm/qb_mysql.go
0 → 100644
| 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) QueryWriter { | ||
| 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) QueryWriter { | ||
| 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) QueryWriter { | ||
| 39 | qw.QueryString = append(qw.QueryString, "WHERE "+cond) | ||
| 40 | return qw | ||
| 41 | } | ||
| 42 | |||
| 43 | func (qw *MySQLQueryBuilder) LimitOffset(limit int, offset int) QueryWriter { | ||
| 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) QueryWriter { | ||
| 49 | qw.QueryString = append(qw.QueryString, "INNER JOIN "+table) | ||
| 50 | return qw | ||
| 51 | } | ||
| 52 | |||
| 53 | func (qw *MySQLQueryBuilder) LeftJoin(table string) QueryWriter { | ||
| 54 | qw.QueryString = append(qw.QueryString, "LEFT JOIN "+table) | ||
| 55 | return qw | ||
| 56 | } | ||
| 57 | |||
| 58 | func (qw *MySQLQueryBuilder) On(cond string) QueryWriter { | ||
| 59 | qw.QueryString = append(qw.QueryString, "ON "+cond) | ||
| 60 | return qw | ||
| 61 | } | ||
| 62 | |||
| 63 | func (qw *MySQLQueryBuilder) And(cond string) QueryWriter { | ||
| 64 | qw.QueryString = append(qw.QueryString, "AND "+cond) | ||
| 65 | return qw | ||
| 66 | } | ||
| 67 | |||
| 68 | func (qw *MySQLQueryBuilder) Or(cond string) QueryWriter { | ||
| 69 | qw.QueryString = append(qw.QueryString, "OR "+cond) | ||
| 70 | return qw | ||
| 71 | } | ||
| 72 | |||
| 73 | func (qw *MySQLQueryBuilder) In(vals ...string) QueryWriter { | ||
| 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 | } |
-
Please register or sign in to post a comment