Fix wrong dbmetas #1442
|
@ -298,6 +298,40 @@ func splitColStr(colStr string) []string {
|
|||
return results
|
||||
}
|
||||
|
||||
func parseString(colStr string) (*core.Column, error) {
|
||||
fields := splitColStr(colStr)
|
||||
col := new(core.Column)
|
||||
col.Indexes = make(map[string]int)
|
||||
col.Nullable = true
|
||||
col.DefaultIsEmpty = true
|
||||
|
||||
for idx, field := range fields {
|
||||
if idx == 0 {
|
||||
col.Name = strings.Trim(strings.Trim(field, "`[] "), `"`)
|
||||
continue
|
||||
} else if idx == 1 {
|
||||
col.SQLType = core.SQLType{Name: field, DefaultLength: 0, DefaultLength2: 0}
|
||||
continue
|
||||
}
|
||||
switch field {
|
||||
case "PRIMARY":
|
||||
col.IsPrimaryKey = true
|
||||
case "AUTOINCREMENT":
|
||||
col.IsAutoIncrement = true
|
||||
case "NULL":
|
||||
if fields[idx-1] == "NOT" {
|
||||
col.Nullable = false
|
||||
} else {
|
||||
col.Nullable = true
|
||||
}
|
||||
case "DEFAULT":
|
||||
col.Default = fields[idx+1]
|
||||
col.DefaultIsEmpty = false
|
||||
}
|
||||
}
|
||||
return col, nil
|
||||
}
|
||||
|
||||
func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
||||
args := []interface{}{tableName}
|
||||
s := "SELECT sql FROM sqlite_master WHERE type='table' and name = ?"
|
||||
|
@ -327,6 +361,7 @@ func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Colu
|
|||
colCreates := reg.FindAllString(name[nStart+1:nEnd], -1)
|
||||
cols := make(map[string]*core.Column)
|
||||
colSeq := make([]string, 0)
|
||||
|
||||
for _, colStr := range colCreates {
|
||||
reg = regexp.MustCompile(`,\s`)
|
||||
colStr = reg.ReplaceAllString(colStr, ",")
|
||||
|
@ -343,35 +378,11 @@ func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Colu
|
|||
continue
|
||||
}
|
||||
|
||||
fields := splitColStr(colStr)
|
||||
col := new(core.Column)
|
||||
col.Indexes = make(map[string]int)
|
||||
col.Nullable = true
|
||||
col.DefaultIsEmpty = true
|
||||
|
||||
for idx, field := range fields {
|
||||
if idx == 0 {
|
||||
col.Name = strings.Trim(strings.Trim(field, "`[] "), `"`)
|
||||
continue
|
||||
} else if idx == 1 {
|
||||
col.SQLType = core.SQLType{Name: field, DefaultLength: 0, DefaultLength2: 0}
|
||||
}
|
||||
switch field {
|
||||
case "PRIMARY":
|
||||
col.IsPrimaryKey = true
|
||||
case "AUTOINCREMENT":
|
||||
col.IsAutoIncrement = true
|
||||
case "NULL":
|
||||
if fields[idx-1] == "NOT" {
|
||||
col.Nullable = false
|
||||
} else {
|
||||
col.Nullable = true
|
||||
}
|
||||
case "DEFAULT":
|
||||
col.Default = fields[idx+1]
|
||||
col.DefaultIsEmpty = false
|
||||
}
|
||||
col, err := parseString(colStr)
|
||||
if err != nil {
|
||||
return colSeq, cols, err
|
||||
}
|
||||
|
||||
cols[col.Name] = col
|
||||
colSeq = append(colSeq, col.Name)
|
||||
}
|
||||
|
|
37
tag_test.go
37
tag_test.go
|
@ -560,16 +560,41 @@ func TestTagAutoIncr(t *testing.T) {
|
|||
|
||||
assertSync(t, new(TagAutoIncr))
|
||||
|
||||
mapper := testEngine.GetTableMapper()
|
||||
cmapper := testEngine.GetColumnMapper()
|
||||
tables, err := testEngine.DBMetas()
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, len(tables))
|
||||
assert.EqualValues(t, tableMapper.Obj2Table("TagAutoIncr"), tables[0].Name)
|
||||
col := tables[0].GetColumn(colMapper.Obj2Table("Id"))
|
||||
assert.NotNil(t, col)
|
||||
assert.True(t, col.IsPrimaryKey)
|
||||
assert.True(t, col.IsAutoIncrement)
|
||||
|
||||
col2 := tables[0].GetColumn(colMapper.Obj2Table("Name"))
|
||||
assert.NotNil(t, col2)
|
||||
assert.False(t, col2.IsPrimaryKey)
|
||||
assert.False(t, col2.IsAutoIncrement)
|
||||
}
|
||||
|
||||
func TestTagPrimarykey(t *testing.T) {
|
||||
assert.NoError(t, prepareEngine())
|
||||
type TagPrimaryKey struct {
|
||||
Id int64 `xorm:"pk"`
|
||||
Name string `xorm:"VARCHAR(20) pk"`
|
||||
}
|
||||
|
||||
assertSync(t, new(TagPrimaryKey))
|
||||
|
||||
tables, err := testEngine.DBMetas()
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, len(tables))
|
||||
assert.EqualValues(t, mapper.Obj2Table("TagAutoIncr"), tables[0].Name)
|
||||
col := tables[0].GetColumn(cmapper.Obj2Table("Name"))
|
||||
assert.EqualValues(t, tableMapper.Obj2Table("TagPrimaryKey"), tables[0].Name)
|
||||
col := tables[0].GetColumn(colMapper.Obj2Table("Id"))
|
||||
assert.NotNil(t, col)
|
||||
assert.True(t, col.IsPrimaryKey)
|
||||
assert.True(t, col.IsAutoIncrement)
|
||||
assert.EqualValues(t, 255, col.SQLType.DefaultLength)
|
||||
assert.False(t, col.IsAutoIncrement)
|
||||
|
||||
col2 := tables[0].GetColumn(colMapper.Obj2Table("Name"))
|
||||
assert.NotNil(t, col2)
|
||||
assert.True(t, col2.IsPrimaryKey)
|
||||
assert.False(t, col2.IsAutoIncrement)
|
||||
}
|
||||
|
|
|
@ -15,10 +15,10 @@ import (
|
|||
|
||||
_ "github.com/denisenkom/go-mssqldb"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"xorm.io/core"
|
||||
_ "github.com/lib/pq"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
_ "github.com/ziutek/mymysql/godrv"
|
||||
"xorm.io/core"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -35,6 +35,9 @@ var (
|
|||
splitter = flag.String("splitter", ";", "the splitter on connstr for cluster")
|
||||
schema = flag.String("schema", "", "specify the schema")
|
||||
ignoreSelectUpdate = flag.Bool("ignore_select_update", false, "ignore select update if implementation difference, only for tidb")
|
||||
|
||||
tableMapper core.IMapper
|
||||
colMapper core.IMapper
|
||||
)
|
||||
|
||||
func createEngine(dbType, connStr string) error {
|
||||
|
@ -122,6 +125,9 @@ func createEngine(dbType, connStr string) error {
|
|||
}
|
||||
}
|
||||
|
||||
tableMapper = testEngine.GetTableMapper()
|
||||
colMapper = testEngine.GetColumnMapper()
|
||||
|
||||
tables, err := testEngine.DBMetas()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
Loading…
Reference in New Issue
Block a user