github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/databases/orm/db_tidb.go (about)

     1  // The original package is migrated from beego and modified, you can find orignal from following link:
     2  //    "github.com/beego/beego/"
     3  //
     4  // Copyright 2023 IAC. All Rights Reserved.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //      http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package orm
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  )
    24  
    25  // mysql dbBaser implementation.
    26  type dbBaseTidb struct {
    27  	dbBase
    28  }
    29  
    30  var _ dbBaser = new(dbBaseTidb)
    31  
    32  // get mysql operator.
    33  func (d *dbBaseTidb) OperatorSQL(operator string) string {
    34  	return mysqlOperators[operator]
    35  }
    36  
    37  // get mysql table field types.
    38  func (d *dbBaseTidb) DbTypes() map[string]string {
    39  	return mysqlTypes
    40  }
    41  
    42  // show table sql for mysql.
    43  func (d *dbBaseTidb) ShowTablesQuery() string {
    44  	return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = DATABASE()"
    45  }
    46  
    47  // show columns sql of table for mysql.
    48  func (d *dbBaseTidb) ShowColumnsQuery(table string) string {
    49  	return fmt.Sprintf("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE FROM information_schema.columns "+
    50  		"WHERE table_schema = DATABASE() AND table_name = '%s'", table)
    51  }
    52  
    53  // execute sql to check index exist.
    54  func (d *dbBaseTidb) IndexExists(ctx context.Context, db dbQuerier, table string, name string) bool {
    55  	row := db.QueryRowContext(ctx, "SELECT count(*) FROM information_schema.statistics "+
    56  		"WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ?", table, name)
    57  	var cnt int
    58  	row.Scan(&cnt)
    59  	return cnt > 0
    60  }
    61  
    62  // create new mysql dbBaser.
    63  func newdbBaseTidb() dbBaser {
    64  	b := new(dbBaseTidb)
    65  	b.ins = b
    66  	return b
    67  }