mirror of
https://github.com/geektutu/7days-golang.git
synced 2024-04-21 12:32:11 +00:00
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package session
|
|
|
|
import (
|
|
"fmt"
|
|
"geeorm/log"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"geeorm/schema"
|
|
)
|
|
|
|
// Model assigns refTable
|
|
func (s *Session) Model(value interface{}) *Session {
|
|
// nil or different model, update refTable
|
|
if s.refTable == nil || reflect.TypeOf(value) != reflect.TypeOf(s.refTable.Model) {
|
|
s.refTable = schema.Parse(value, s.dialect)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// RefTable returns a Schema instance that contains all parsed fields
|
|
func (s *Session) RefTable() *schema.Schema {
|
|
if s.refTable == nil {
|
|
log.Error("Model is not set")
|
|
}
|
|
return s.refTable
|
|
}
|
|
|
|
// CreateTable create a table in database with a model
|
|
func (s *Session) CreateTable() error {
|
|
table := s.RefTable()
|
|
var columns []string
|
|
for _, field := range table.Fields {
|
|
columns = append(columns, fmt.Sprintf("%s %s %s", field.Name, field.Type, field.Tag))
|
|
}
|
|
desc := strings.Join(columns, ",")
|
|
_, err := s.Raw(fmt.Sprintf("CREATE TABLE %s (%s);", table.Name, desc)).Exec()
|
|
return err
|
|
}
|
|
|
|
// DropTable drops a table with the name of model
|
|
func (s *Session) DropTable() error {
|
|
_, err := s.Raw(fmt.Sprintf("DROP TABLE IF EXISTS %s", s.RefTable().Name)).Exec()
|
|
return err
|
|
}
|
|
|
|
// HasTable returns true of the table exists
|
|
func (s *Session) HasTable() bool {
|
|
sql, values := s.dialect.TableExistSQL(s.RefTable().Name)
|
|
row := s.Raw(sql, values...).QueryRow()
|
|
var tmp string
|
|
_ = row.Scan(&tmp)
|
|
return tmp == s.RefTable().Name
|
|
}
|