github.com/astaxie/beego@v1.12.3/orm/db_tidb.go (about) 1 // Copyright 2015 TiDB 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 ) 20 21 // mysql dbBaser implementation. 22 type dbBaseTidb struct { 23 dbBase 24 } 25 26 var _ dbBaser = new(dbBaseTidb) 27 28 // get mysql operator. 29 func (d *dbBaseTidb) OperatorSQL(operator string) string { 30 return mysqlOperators[operator] 31 } 32 33 // get mysql table field types. 34 func (d *dbBaseTidb) DbTypes() map[string]string { 35 return mysqlTypes 36 } 37 38 // show table sql for mysql. 39 func (d *dbBaseTidb) ShowTablesQuery() string { 40 return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = DATABASE()" 41 } 42 43 // show columns sql of table for mysql. 44 func (d *dbBaseTidb) ShowColumnsQuery(table string) string { 45 return fmt.Sprintf("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE FROM information_schema.columns "+ 46 "WHERE table_schema = DATABASE() AND table_name = '%s'", table) 47 } 48 49 // execute sql to check index exist. 50 func (d *dbBaseTidb) IndexExists(db dbQuerier, table string, name string) bool { 51 row := db.QueryRow("SELECT count(*) FROM information_schema.statistics "+ 52 "WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ?", table, name) 53 var cnt int 54 row.Scan(&cnt) 55 return cnt > 0 56 } 57 58 // create new mysql dbBaser. 59 func newdbBaseTidb() dbBaser { 60 b := new(dbBaseTidb) 61 b.ins = b 62 return b 63 }