You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
431 lines
11 KiB
431 lines
11 KiB
8 years ago
|
// Copyright 2016 The Xorm Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package xorm
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
5 years ago
|
"xorm.io/core"
|
||
8 years ago
|
)
|
||
|
|
||
|
// Ping test if database is ok
|
||
|
func (session *Session) Ping() error {
|
||
7 years ago
|
if session.isAutoClose {
|
||
8 years ago
|
defer session.Close()
|
||
|
}
|
||
|
|
||
7 years ago
|
session.engine.logger.Infof("PING DATABASE %v", session.engine.DriverName())
|
||
5 years ago
|
return session.DB().PingContext(session.ctx)
|
||
8 years ago
|
}
|
||
|
|
||
|
// CreateTable create a table according a bean
|
||
|
func (session *Session) CreateTable(bean interface{}) error {
|
||
7 years ago
|
if session.isAutoClose {
|
||
|
defer session.Close()
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
return session.createTable(bean)
|
||
|
}
|
||
|
|
||
|
func (session *Session) createTable(bean interface{}) error {
|
||
6 years ago
|
if err := session.statement.setRefBean(bean); err != nil {
|
||
7 years ago
|
return err
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
sqlStr := session.statement.genCreateTableSQL()
|
||
|
_, err := session.exec(sqlStr)
|
||
|
return err
|
||
8 years ago
|
}
|
||
|
|
||
|
// CreateIndexes create indexes
|
||
|
func (session *Session) CreateIndexes(bean interface{}) error {
|
||
7 years ago
|
if session.isAutoClose {
|
||
|
defer session.Close()
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
return session.createIndexes(bean)
|
||
|
}
|
||
|
|
||
|
func (session *Session) createIndexes(bean interface{}) error {
|
||
6 years ago
|
if err := session.statement.setRefBean(bean); err != nil {
|
||
7 years ago
|
return err
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
sqls := session.statement.genIndexSQL()
|
||
8 years ago
|
for _, sqlStr := range sqls {
|
||
|
_, err := session.exec(sqlStr)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// CreateUniques create uniques
|
||
|
func (session *Session) CreateUniques(bean interface{}) error {
|
||
7 years ago
|
if session.isAutoClose {
|
||
|
defer session.Close()
|
||
8 years ago
|
}
|
||
7 years ago
|
return session.createUniques(bean)
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
func (session *Session) createUniques(bean interface{}) error {
|
||
6 years ago
|
if err := session.statement.setRefBean(bean); err != nil {
|
||
7 years ago
|
return err
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
sqls := session.statement.genUniqueSQL()
|
||
8 years ago
|
for _, sqlStr := range sqls {
|
||
|
_, err := session.exec(sqlStr)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
7 years ago
|
// DropIndexes drop indexes
|
||
|
func (session *Session) DropIndexes(bean interface{}) error {
|
||
|
if session.isAutoClose {
|
||
8 years ago
|
defer session.Close()
|
||
|
}
|
||
|
|
||
7 years ago
|
return session.dropIndexes(bean)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
func (session *Session) dropIndexes(bean interface{}) error {
|
||
6 years ago
|
if err := session.statement.setRefBean(bean); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
sqls := session.statement.genDelIndexSQL()
|
||
8 years ago
|
for _, sqlStr := range sqls {
|
||
|
_, err := session.exec(sqlStr)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// DropTable drop table will drop table if exist, if drop failed, it will return error
|
||
|
func (session *Session) DropTable(beanOrTableName interface{}) error {
|
||
7 years ago
|
if session.isAutoClose {
|
||
|
defer session.Close()
|
||
|
}
|
||
|
|
||
|
return session.dropTable(beanOrTableName)
|
||
|
}
|
||
|
|
||
|
func (session *Session) dropTable(beanOrTableName interface{}) error {
|
||
6 years ago
|
tableName := session.engine.TableName(beanOrTableName)
|
||
8 years ago
|
var needDrop = true
|
||
7 years ago
|
if !session.engine.dialect.SupportDropIfExists() {
|
||
|
sqlStr, args := session.engine.dialect.TableCheckSql(tableName)
|
||
7 years ago
|
results, err := session.queryBytes(sqlStr, args...)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
needDrop = len(results) > 0
|
||
|
}
|
||
|
|
||
|
if needDrop {
|
||
6 years ago
|
sqlStr := session.engine.Dialect().DropTableSql(session.engine.TableName(tableName, true))
|
||
|
_, err := session.exec(sqlStr)
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// IsTableExist if a table is exist
|
||
|
func (session *Session) IsTableExist(beanOrTableName interface{}) (bool, error) {
|
||
7 years ago
|
if session.isAutoClose {
|
||
|
defer session.Close()
|
||
|
}
|
||
|
|
||
6 years ago
|
tableName := session.engine.TableName(beanOrTableName)
|
||
8 years ago
|
|
||
|
return session.isTableExist(tableName)
|
||
|
}
|
||
|
|
||
|
func (session *Session) isTableExist(tableName string) (bool, error) {
|
||
7 years ago
|
sqlStr, args := session.engine.dialect.TableCheckSql(tableName)
|
||
7 years ago
|
results, err := session.queryBytes(sqlStr, args...)
|
||
8 years ago
|
return len(results) > 0, err
|
||
|
}
|
||
|
|
||
|
// IsTableEmpty if table have any records
|
||
|
func (session *Session) IsTableEmpty(bean interface{}) (bool, error) {
|
||
6 years ago
|
if session.isAutoClose {
|
||
|
defer session.Close()
|
||
8 years ago
|
}
|
||
6 years ago
|
return session.isTableEmpty(session.engine.TableName(bean))
|
||
8 years ago
|
}
|
||
|
|
||
|
func (session *Session) isTableEmpty(tableName string) (bool, error) {
|
||
|
var total int64
|
||
6 years ago
|
sqlStr := fmt.Sprintf("select count(*) from %s", session.engine.Quote(session.engine.TableName(tableName, true)))
|
||
7 years ago
|
err := session.queryRow(sqlStr).Scan(&total)
|
||
8 years ago
|
if err != nil {
|
||
|
if err == sql.ErrNoRows {
|
||
|
err = nil
|
||
|
}
|
||
|
return true, err
|
||
|
}
|
||
|
|
||
|
return total == 0, nil
|
||
|
}
|
||
|
|
||
|
// find if index is exist according cols
|
||
|
func (session *Session) isIndexExist2(tableName string, cols []string, unique bool) (bool, error) {
|
||
7 years ago
|
indexes, err := session.engine.dialect.GetIndexes(tableName)
|
||
8 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
for _, index := range indexes {
|
||
|
if sliceEq(index.Cols, cols) {
|
||
|
if unique {
|
||
|
return index.Type == core.UniqueType, nil
|
||
|
}
|
||
|
return index.Type == core.IndexType, nil
|
||
|
}
|
||
|
}
|
||
|
return false, nil
|
||
|
}
|
||
|
|
||
|
func (session *Session) addColumn(colName string) error {
|
||
7 years ago
|
col := session.statement.RefTable.GetColumn(colName)
|
||
|
sql, args := session.statement.genAddColumnStr(col)
|
||
8 years ago
|
_, err := session.exec(sql, args...)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (session *Session) addIndex(tableName, idxName string) error {
|
||
7 years ago
|
index := session.statement.RefTable.Indexes[idxName]
|
||
|
sqlStr := session.engine.dialect.CreateIndexSql(tableName, index)
|
||
8 years ago
|
_, err := session.exec(sqlStr)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (session *Session) addUnique(tableName, uqeName string) error {
|
||
7 years ago
|
index := session.statement.RefTable.Indexes[uqeName]
|
||
|
sqlStr := session.engine.dialect.CreateIndexSql(tableName, index)
|
||
8 years ago
|
_, err := session.exec(sqlStr)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Sync2 synchronize structs to database tables
|
||
|
func (session *Session) Sync2(beans ...interface{}) error {
|
||
7 years ago
|
engine := session.engine
|
||
|
|
||
|
if session.isAutoClose {
|
||
|
session.isAutoClose = false
|
||
|
defer session.Close()
|
||
|
}
|
||
8 years ago
|
|
||
5 years ago
|
tables, err := engine.dialect.GetTables()
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
6 years ago
|
session.autoResetStatement = false
|
||
|
defer func() {
|
||
|
session.autoResetStatement = true
|
||
|
session.resetStatement()
|
||
|
}()
|
||
|
|
||
8 years ago
|
for _, bean := range beans {
|
||
|
v := rValue(bean)
|
||
8 years ago
|
table, err := engine.mapType(v)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
5 years ago
|
var tbName string
|
||
|
if len(session.statement.AltTableName) > 0 {
|
||
|
tbName = session.statement.AltTableName
|
||
|
} else {
|
||
|
tbName = engine.TableName(bean)
|
||
|
}
|
||
|
tbNameWithSchema := engine.tbNameWithSchema(tbName)
|
||
8 years ago
|
|
||
|
var oriTable *core.Table
|
||
|
for _, tb := range tables {
|
||
5 years ago
|
if strings.EqualFold(engine.tbNameWithSchema(tb.Name), engine.tbNameWithSchema(tbName)) {
|
||
8 years ago
|
oriTable = tb
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// this is a new table
|
||
8 years ago
|
if oriTable == nil {
|
||
7 years ago
|
err = session.StoreEngine(session.statement.StoreEngine).createTable(bean)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
7 years ago
|
err = session.createUniques(bean)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
7 years ago
|
err = session.createIndexes(bean)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
5 years ago
|
continue
|
||
|
}
|
||
8 years ago
|
|
||
5 years ago
|
// this will modify an old table
|
||
|
if err = engine.loadTableInfo(oriTable); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// check columns
|
||
|
for _, col := range table.Columns() {
|
||
|
var oriCol *core.Column
|
||
|
for _, col2 := range oriTable.Columns() {
|
||
|
if strings.EqualFold(col.Name, col2.Name) {
|
||
|
oriCol = col2
|
||
|
break
|
||
8 years ago
|
}
|
||
5 years ago
|
}
|
||
|
|
||
|
// column is not exist on table
|
||
|
if oriCol == nil {
|
||
|
session.statement.RefTable = table
|
||
|
session.statement.tableName = tbNameWithSchema
|
||
|
if err = session.addColumn(col.Name); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
5 years ago
|
continue
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
err = nil
|
||
|
expectedType := engine.dialect.SqlType(col)
|
||
|
curType := engine.dialect.SqlType(oriCol)
|
||
|
if expectedType != curType {
|
||
|
if expectedType == core.Text &&
|
||
|
strings.HasPrefix(curType, core.Varchar) {
|
||
|
// currently only support mysql & postgres
|
||
|
if engine.dialect.DBType() == core.MYSQL ||
|
||
|
engine.dialect.DBType() == core.POSTGRES {
|
||
|
engine.logger.Infof("Table %s column %s change type from %s to %s\n",
|
||
|
tbNameWithSchema, col.Name, curType, expectedType)
|
||
|
_, err = session.exec(engine.dialect.ModifyColumnSql(tbNameWithSchema, col))
|
||
|
} else {
|
||
|
engine.logger.Warnf("Table %s column %s db type is %s, struct type is %s\n",
|
||
|
tbNameWithSchema, col.Name, curType, expectedType)
|
||
8 years ago
|
}
|
||
5 years ago
|
} else if strings.HasPrefix(curType, core.Varchar) && strings.HasPrefix(expectedType, core.Varchar) {
|
||
|
if engine.dialect.DBType() == core.MYSQL {
|
||
|
if oriCol.Length < col.Length {
|
||
|
engine.logger.Infof("Table %s column %s change type from varchar(%d) to varchar(%d)\n",
|
||
|
tbNameWithSchema, col.Name, oriCol.Length, col.Length)
|
||
|
_, err = session.exec(engine.dialect.ModifyColumnSql(tbNameWithSchema, col))
|
||
8 years ago
|
}
|
||
5 years ago
|
}
|
||
|
} else {
|
||
|
if !(strings.HasPrefix(curType, expectedType) && curType[len(expectedType)] == '(') {
|
||
|
engine.logger.Warnf("Table %s column %s db type is %s, struct type is %s",
|
||
|
tbNameWithSchema, col.Name, curType, expectedType)
|
||
|
}
|
||
|
}
|
||
|
} else if expectedType == core.Varchar {
|
||
|
if engine.dialect.DBType() == core.MYSQL {
|
||
|
if oriCol.Length < col.Length {
|
||
|
engine.logger.Infof("Table %s column %s change type from varchar(%d) to varchar(%d)\n",
|
||
|
tbNameWithSchema, col.Name, oriCol.Length, col.Length)
|
||
|
_, err = session.exec(engine.dialect.ModifyColumnSql(tbNameWithSchema, col))
|
||
8 years ago
|
}
|
||
|
}
|
||
5 years ago
|
}
|
||
8 years ago
|
|
||
5 years ago
|
if col.Default != oriCol.Default {
|
||
|
if (col.SQLType.Name == core.Bool || col.SQLType.Name == core.Boolean) &&
|
||
|
((strings.EqualFold(col.Default, "true") && oriCol.Default == "1") ||
|
||
|
(strings.EqualFold(col.Default, "false") && oriCol.Default == "0")) {
|
||
|
} else {
|
||
|
engine.logger.Warnf("Table %s Column %s db default is %s, struct default is %s",
|
||
|
tbName, col.Name, oriCol.Default, col.Default)
|
||
8 years ago
|
}
|
||
|
}
|
||
5 years ago
|
if col.Nullable != oriCol.Nullable {
|
||
|
engine.logger.Warnf("Table %s Column %s db nullable is %v, struct nullable is %v",
|
||
|
tbName, col.Name, oriCol.Nullable, col.Nullable)
|
||
|
}
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var foundIndexNames = make(map[string]bool)
|
||
|
var addedNames = make(map[string]*core.Index)
|
||
8 years ago
|
|
||
5 years ago
|
for name, index := range table.Indexes {
|
||
|
var oriIndex *core.Index
|
||
8 years ago
|
for name2, index2 := range oriTable.Indexes {
|
||
5 years ago
|
if index.Equal(index2) {
|
||
|
oriIndex = index2
|
||
|
foundIndexNames[name2] = true
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if oriIndex != nil {
|
||
|
if oriIndex.Type != index.Type {
|
||
|
sql := engine.dialect.DropIndexSql(tbNameWithSchema, oriIndex)
|
||
7 years ago
|
_, err = session.exec(sql)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
5 years ago
|
oriIndex = nil
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
if oriIndex == nil {
|
||
|
addedNames[name] = index
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for name2, index2 := range oriTable.Indexes {
|
||
|
if _, ok := foundIndexNames[name2]; !ok {
|
||
|
sql := engine.dialect.DropIndexSql(tbNameWithSchema, index2)
|
||
|
_, err = session.exec(sql)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
for name, index := range addedNames {
|
||
|
if index.Type == core.UniqueType {
|
||
|
session.statement.RefTable = table
|
||
|
session.statement.tableName = tbNameWithSchema
|
||
|
err = session.addUnique(tbNameWithSchema, name)
|
||
|
} else if index.Type == core.IndexType {
|
||
|
session.statement.RefTable = table
|
||
|
session.statement.tableName = tbNameWithSchema
|
||
|
err = session.addIndex(tbNameWithSchema, name)
|
||
|
}
|
||
|
if err != nil {
|
||
|
return err
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// check all the columns which removed from struct fields but left on database tables.
|
||
|
for _, colName := range oriTable.ColumnsSeq() {
|
||
|
if table.GetColumn(colName) == nil {
|
||
|
engine.logger.Warnf("Table %s has column %s but struct has not related field", engine.TableName(oriTable.Name, true), colName)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
5 years ago
|
|
||
8 years ago
|
return nil
|
||
|
}
|