Implement GetTable method
This commit is contained in:
parent
b0c6124926
commit
2d8faf10b4
@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/jinzhu/gorm/builder"
|
|
||||||
"github.com/jinzhu/gorm/dialects/common/utils"
|
"github.com/jinzhu/gorm/dialects/common/utils"
|
||||||
|
|
||||||
// import sqlite3 driver
|
// import sqlite3 driver
|
||||||
@ -16,13 +15,12 @@ type Dialect struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Insert insert
|
// Insert insert
|
||||||
func (*Dialect) Insert(stmt *builder.Statement) error {
|
func (*Dialect) Insert(tx *gorm.DB) error {
|
||||||
var (
|
var (
|
||||||
args []interface{}
|
args []interface{}
|
||||||
defaultValueColumns []string
|
defaultValueColumns []string
|
||||||
errs = gorm.Errors{}
|
assignmentsChan = utils.GetCreatingAssignments(tx)
|
||||||
assignmentsChan = utils.GetCreatingAssignments(stmt, &errs)
|
tableNameChan = utils.GetTable(tx)
|
||||||
tableNameChan = utils.GetTable(stmt, &errs)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
s := bytes.NewBufferString("INSERT INTO ")
|
s := bytes.NewBufferString("INSERT INTO ")
|
||||||
@ -40,16 +38,16 @@ func (*Dialect) Insert(stmt *builder.Statement) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Query query
|
// Query query
|
||||||
func (*Dialect) Query(*builder.Statement) error {
|
func (*Dialect) Query(tx *gorm.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update update
|
// Update update
|
||||||
func (*Dialect) Update(*builder.Statement) error {
|
func (*Dialect) Update(tx *gorm.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete delete
|
// Delete delete
|
||||||
func (*Dialect) Delete(*builder.Statement) error {
|
func (*Dialect) Delete(tx *gorm.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
8
model/errors.go
Normal file
8
model/errors.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrInvalidTable invalid table name
|
||||||
|
ErrInvalidTable = errors.New("invalid table name")
|
||||||
|
)
|
@ -2,56 +2,70 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/jinzhu/gorm/builder"
|
|
||||||
"github.com/jinzhu/gorm/schema"
|
"github.com/jinzhu/gorm/schema"
|
||||||
|
"github.com/jinzhu/inflection"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultTableNameHandler default table name handler
|
// DefaultTableNameHandler default table name handler
|
||||||
var DefaultTableNameHandler = func(stmt *builder.Statement, tableName string) string {
|
// DefaultTableNameHandler = func(tx *gorm.DB, tableName string) string {
|
||||||
return tableName
|
// return tableName
|
||||||
}
|
// }
|
||||||
|
var DefaultTableNameHandler func(tx *gorm.DB, tableName string) string
|
||||||
|
|
||||||
// GetCreatingAssignments get creating assignments
|
// GetCreatingAssignments get creating assignments
|
||||||
func GetCreatingAssignments(stmt *builder.Statement, errs *gorm.Errors) chan []schema.Field {
|
func GetCreatingAssignments(tx *gorm.DB) chan []schema.Field {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTable get table name
|
// GetTable get table name for current db operation
|
||||||
func GetTable(stmt *builder.Statement, errs *gorm.Errors) chan string {
|
func GetTable(tx *gorm.DB) chan string {
|
||||||
tableChan := make(chan string)
|
tableChan := make(chan string)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if stmt.Table != nil {
|
var tableName string
|
||||||
if table, ok := stmt.Table.(string); ok {
|
if name, ok := tx.Statement.Table.(string); ok {
|
||||||
tableChan <- DefaultTableNameHandler(stmt, table)
|
tableName = name
|
||||||
} else if tableSchema := schema.Parse(stmt.Table); tableSchema != nil {
|
} else {
|
||||||
if tableSchema.TableName != "" {
|
for _, v := range []interface{}{tx.Statement.Table, tx.Statement.Dest} {
|
||||||
tableChan <- DefaultTableNameHandler(stmt, tableSchema.TableName)
|
if t, ok := v.(tabler); ok {
|
||||||
|
tableName = t.TableName()
|
||||||
|
} else if t, ok := v.(dbTabler); ok {
|
||||||
|
tableName = t.TableName(tx)
|
||||||
|
} else if s := schema.Parse(tx.Statement.Table); s != nil {
|
||||||
|
if s.TableName != "" {
|
||||||
|
tableName = s.TableName
|
||||||
|
} else {
|
||||||
|
tableName = schema.ToDBName(s.ModelType.Name())
|
||||||
|
if !tx.Config.SingularTable {
|
||||||
|
tableName = inflection.Plural(tableName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if tableName != "" {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
tableSchema.ModelType.Name
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tableName != "" {
|
||||||
|
if DefaultTableNameHandler != nil {
|
||||||
|
tableChan <- DefaultTableNameHandler(tx, tableName)
|
||||||
|
} else {
|
||||||
|
tableChan <- tableName
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tx.AddError(ErrInvalidTable)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return tableChan
|
return tableChan
|
||||||
}
|
}
|
||||||
|
|
||||||
// if scope.Value == nil {
|
type tabler interface {
|
||||||
// return &modelStruct
|
TableName() string
|
||||||
// }
|
}
|
||||||
// TableName get model's table name
|
|
||||||
// func (schema *Schema) TableName(stmt *builder.Statement) string {
|
type dbTabler interface {
|
||||||
// if s.defaultTableName == "" && db != nil && s.ModelType != nil {
|
TableName(*gorm.DB) string
|
||||||
// // Set default table name
|
}
|
||||||
// if tabler, ok := reflect.New(s.ModelType).Interface().(tabler); ok {
|
|
||||||
// s.defaultTableName = tabler.TableName()
|
|
||||||
// } else {
|
|
||||||
// tableName := ToDBName(s.ModelType.Name())
|
|
||||||
// if db == nil || !db.parent.singularTable {
|
|
||||||
// tableName = inflection.Plural(tableName)
|
|
||||||
// }
|
|
||||||
// s.defaultTableName = tableName
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return DefaultTableNameHandler(db, s.defaultTableName)
|
|
||||||
// }
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user