1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-23 02:40:54 +00:00
Beego/orm/db_mysql.go

95 lines
2.6 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors slene
2013-07-30 12:32:38 +00:00
package orm
2013-08-27 04:33:27 +00:00
import (
"fmt"
)
2014-01-17 09:25:17 +00:00
// mysql operators.
var mysqlOperators = map[string]string{
"exact": "= ?",
"iexact": "LIKE ?",
"contains": "LIKE BINARY ?",
"icontains": "LIKE ?",
// "regex": "REGEXP BINARY ?",
// "iregex": "REGEXP ?",
"gt": "> ?",
"gte": ">= ?",
"lt": "< ?",
"lte": "<= ?",
"startswith": "LIKE BINARY ?",
"endswith": "LIKE BINARY ?",
"istartswith": "LIKE ?",
"iendswith": "LIKE ?",
}
2014-01-17 09:25:17 +00:00
// mysql column field types.
2013-08-19 14:37:39 +00:00
var mysqlTypes = map[string]string{
"auto": "AUTO_INCREMENT NOT NULL PRIMARY KEY",
"pk": "NOT NULL PRIMARY KEY",
"bool": "bool",
"string": "varchar(%d)",
"string-text": "longtext",
"time.Time-date": "date",
"time.Time": "datetime",
"int8": "tinyint",
"int16": "smallint",
"int32": "integer",
"int64": "bigint",
"uint8": "tinyint unsigned",
"uint16": "smallint unsigned",
"uint32": "integer unsigned",
"uint64": "bigint unsigned",
"float64": "double precision",
"float64-decimal": "numeric(%d, %d)",
}
2014-01-17 09:25:17 +00:00
// mysql dbBaser implementation.
2013-07-30 12:32:38 +00:00
type dbBaseMysql struct {
dbBase
}
var _ dbBaser = new(dbBaseMysql)
2014-01-17 09:25:17 +00:00
// get mysql operator.
func (d *dbBaseMysql) OperatorSql(operator string) string {
return mysqlOperators[operator]
2013-07-30 12:32:38 +00:00
}
2014-01-17 09:25:17 +00:00
// get mysql table field types.
2013-08-19 14:37:39 +00:00
func (d *dbBaseMysql) DbTypes() map[string]string {
return mysqlTypes
}
2014-01-17 09:25:17 +00:00
// show table sql for mysql.
2013-08-27 04:33:27 +00:00
func (d *dbBaseMysql) ShowTablesQuery() string {
return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = DATABASE()"
}
2014-01-17 09:25:17 +00:00
// show columns sql of table for mysql.
2013-08-27 04:33:27 +00:00
func (d *dbBaseMysql) 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)
}
2014-01-17 09:25:17 +00:00
// execute sql to check index exist.
2013-08-27 04:33:27 +00:00
func (d *dbBaseMysql) 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
}
2014-01-17 09:25:17 +00:00
// create new mysql dbBaser.
2013-07-30 12:32:38 +00:00
func newdbBaseMysql() dbBaser {
b := new(dbBaseMysql)
b.ins = b
return b
}