db_tidb.go 1.8 KB

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