forked from beego/beego
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
236 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2015 TiDB 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" | ||
) | ||
|
||
// mysql dbBaser implementation. | ||
type dbBaseTidb struct { | ||
dbBase | ||
} | ||
|
||
var _ dbBaser = new(dbBaseTidb) | ||
|
||
// get mysql operator. | ||
func (d *dbBaseTidb) OperatorSQL(operator string) string { | ||
return mysqlOperators[operator] | ||
} | ||
|
||
// get mysql table field types. | ||
func (d *dbBaseTidb) DbTypes() map[string]string { | ||
return mysqlTypes | ||
} | ||
|
||
// show table sql for mysql. | ||
func (d *dbBaseTidb) ShowTablesQuery() string { | ||
return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = DATABASE()" | ||
} | ||
|
||
// show columns sql of table for mysql. | ||
func (d *dbBaseTidb) ShowColumnsQuery(table string) string { | ||
return fmt.Sprintf("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE FROM information_schema.columns "+ | ||
"WHERE table_schema = DATABASE() AND table_name = '%s'", table) | ||
} | ||
|
||
// execute sql to check index exist. | ||
func (d *dbBaseTidb) IndexExists(db dbQuerier, table string, name string) bool { | ||
row := db.QueryRow("SELECT count(*) FROM information_schema.statistics "+ | ||
"WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ?", table, name) | ||
var cnt int | ||
row.Scan(&cnt) | ||
return cnt > 0 | ||
} | ||
|
||
// create new mysql dbBaser. | ||
func newdbBaseTidb() dbBaser { | ||
b := new(dbBaseTidb) | ||
b.ins = b | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// Copyright 2015 TiDB 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" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type TiDBQueryBuilder struct { | ||
Tokens []string | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Select(fields ...string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "SELECT", strings.Join(fields, CommaSpace)) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) From(tables ...string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "FROM", strings.Join(tables, CommaSpace)) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) InnerJoin(table string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "INNER JOIN", table) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) LeftJoin(table string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "LEFT JOIN", table) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) RightJoin(table string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "RIGHT JOIN", table) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) On(cond string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "ON", cond) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Where(cond string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "WHERE", cond) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) And(cond string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "AND", cond) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Or(cond string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "OR", cond) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) In(vals ...string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "IN", "(", strings.Join(vals, CommaSpace), ")") | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) OrderBy(fields ...string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "ORDER BY", strings.Join(fields, CommaSpace)) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Asc() QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "ASC") | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Desc() QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "DESC") | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Limit(limit int) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "LIMIT", strconv.Itoa(limit)) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Offset(offset int) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "OFFSET", strconv.Itoa(offset)) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) GroupBy(fields ...string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "GROUP BY", strings.Join(fields, CommaSpace)) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Having(cond string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "HAVING", cond) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Update(tables ...string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "UPDATE", strings.Join(tables, CommaSpace)) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Set(kv ...string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "SET", strings.Join(kv, CommaSpace)) | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Delete(tables ...string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "DELETE") | ||
if len(tables) != 0 { | ||
qb.Tokens = append(qb.Tokens, strings.Join(tables, CommaSpace)) | ||
} | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder { | ||
qb.Tokens = append(qb.Tokens, "INSERT INTO", table) | ||
if len(fields) != 0 { | ||
fieldsStr := strings.Join(fields, CommaSpace) | ||
qb.Tokens = append(qb.Tokens, "(", fieldsStr, ")") | ||
} | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Values(vals ...string) QueryBuilder { | ||
valsStr := strings.Join(vals, CommaSpace) | ||
qb.Tokens = append(qb.Tokens, "VALUES", "(", valsStr, ")") | ||
return qb | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) Subquery(sub string, alias string) string { | ||
return fmt.Sprintf("(%s) AS %s", sub, alias) | ||
} | ||
|
||
func (qb *TiDBQueryBuilder) String() string { | ||
return strings.Join(qb.Tokens, " ") | ||
} |