Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

Commit c5ee68f

Browse files
authored
Fix wrong dbmetas (#1442)
* add tests for db metas * add more tests * fix bug on mssql
1 parent a5702e5 commit c5ee68f

File tree

4 files changed

+99
-32
lines changed

4 files changed

+99
-32
lines changed

dialect_mssql.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column
340340
s := `select a.name as name, b.name as ctype,a.max_length,a.precision,a.scale,a.is_nullable as nullable,
341341
"default_is_null" = (CASE WHEN c.text is null THEN 1 ELSE 0 END),
342342
replace(replace(isnull(c.text,''),'(',''),')','') as vdefault,
343-
ISNULL(i.is_primary_key, 0)
343+
ISNULL(i.is_primary_key, 0), a.is_identity as is_identity
344344
from sys.columns a
345345
left join sys.types b on a.user_type_id=b.user_type_id
346346
left join sys.syscomments c on a.default_object_id=c.id
@@ -362,8 +362,8 @@ func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column
362362
for rows.Next() {
363363
var name, ctype, vdefault string
364364
var maxLen, precision, scale int
365-
var nullable, isPK, defaultIsNull bool
366-
err = rows.Scan(&name, &ctype, &maxLen, &precision, &scale, &nullable, &defaultIsNull, &vdefault, &isPK)
365+
var nullable, isPK, defaultIsNull, isIncrement bool
366+
err = rows.Scan(&name, &ctype, &maxLen, &precision, &scale, &nullable, &defaultIsNull, &vdefault, &isPK, &isIncrement)
367367
if err != nil {
368368
return nil, nil, err
369369
}
@@ -377,6 +377,7 @@ func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column
377377
col.Default = vdefault
378378
}
379379
col.IsPrimaryKey = isPK
380+
col.IsAutoIncrement = isIncrement
380381
ct := strings.ToUpper(ctype)
381382
if ct == "DECIMAL" {
382383
col.Length = precision

dialect_sqlite3.go

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,40 @@ func splitColStr(colStr string) []string {
298298
return results
299299
}
300300

301+
func parseString(colStr string) (*core.Column, error) {
302+
fields := splitColStr(colStr)
303+
col := new(core.Column)
304+
col.Indexes = make(map[string]int)
305+
col.Nullable = true
306+
col.DefaultIsEmpty = true
307+
308+
for idx, field := range fields {
309+
if idx == 0 {
310+
col.Name = strings.Trim(strings.Trim(field, "`[] "), `"`)
311+
continue
312+
} else if idx == 1 {
313+
col.SQLType = core.SQLType{Name: field, DefaultLength: 0, DefaultLength2: 0}
314+
continue
315+
}
316+
switch field {
317+
case "PRIMARY":
318+
col.IsPrimaryKey = true
319+
case "AUTOINCREMENT":
320+
col.IsAutoIncrement = true
321+
case "NULL":
322+
if fields[idx-1] == "NOT" {
323+
col.Nullable = false
324+
} else {
325+
col.Nullable = true
326+
}
327+
case "DEFAULT":
328+
col.Default = fields[idx+1]
329+
col.DefaultIsEmpty = false
330+
}
331+
}
332+
return col, nil
333+
}
334+
301335
func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
302336
args := []interface{}{tableName}
303337
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
327361
colCreates := reg.FindAllString(name[nStart+1:nEnd], -1)
328362
cols := make(map[string]*core.Column)
329363
colSeq := make([]string, 0)
364+
330365
for _, colStr := range colCreates {
331366
reg = regexp.MustCompile(`,\s`)
332367
colStr = reg.ReplaceAllString(colStr, ",")
@@ -343,35 +378,11 @@ func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Colu
343378
continue
344379
}
345380

346-
fields := splitColStr(colStr)
347-
col := new(core.Column)
348-
col.Indexes = make(map[string]int)
349-
col.Nullable = true
350-
col.DefaultIsEmpty = true
351-
352-
for idx, field := range fields {
353-
if idx == 0 {
354-
col.Name = strings.Trim(strings.Trim(field, "`[] "), `"`)
355-
continue
356-
} else if idx == 1 {
357-
col.SQLType = core.SQLType{Name: field, DefaultLength: 0, DefaultLength2: 0}
358-
}
359-
switch field {
360-
case "PRIMARY":
361-
col.IsPrimaryKey = true
362-
case "AUTOINCREMENT":
363-
col.IsAutoIncrement = true
364-
case "NULL":
365-
if fields[idx-1] == "NOT" {
366-
col.Nullable = false
367-
} else {
368-
col.Nullable = true
369-
}
370-
case "DEFAULT":
371-
col.Default = fields[idx+1]
372-
col.DefaultIsEmpty = false
373-
}
381+
col, err := parseString(colStr)
382+
if err != nil {
383+
return colSeq, cols, err
374384
}
385+
375386
cols[col.Name] = col
376387
colSeq = append(colSeq, col.Name)
377388
}

tag_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,52 @@ func TestSplitTag(t *testing.T) {
549549
}
550550
}
551551
}
552+
553+
func TestTagAutoIncr(t *testing.T) {
554+
assert.NoError(t, prepareEngine())
555+
556+
type TagAutoIncr struct {
557+
Id int64
558+
Name string
559+
}
560+
561+
assertSync(t, new(TagAutoIncr))
562+
563+
tables, err := testEngine.DBMetas()
564+
assert.NoError(t, err)
565+
assert.EqualValues(t, 1, len(tables))
566+
assert.EqualValues(t, tableMapper.Obj2Table("TagAutoIncr"), tables[0].Name)
567+
col := tables[0].GetColumn(colMapper.Obj2Table("Id"))
568+
assert.NotNil(t, col)
569+
assert.True(t, col.IsPrimaryKey)
570+
assert.True(t, col.IsAutoIncrement)
571+
572+
col2 := tables[0].GetColumn(colMapper.Obj2Table("Name"))
573+
assert.NotNil(t, col2)
574+
assert.False(t, col2.IsPrimaryKey)
575+
assert.False(t, col2.IsAutoIncrement)
576+
}
577+
578+
func TestTagPrimarykey(t *testing.T) {
579+
assert.NoError(t, prepareEngine())
580+
type TagPrimaryKey struct {
581+
Id int64 `xorm:"pk"`
582+
Name string `xorm:"VARCHAR(20) pk"`
583+
}
584+
585+
assertSync(t, new(TagPrimaryKey))
586+
587+
tables, err := testEngine.DBMetas()
588+
assert.NoError(t, err)
589+
assert.EqualValues(t, 1, len(tables))
590+
assert.EqualValues(t, tableMapper.Obj2Table("TagPrimaryKey"), tables[0].Name)
591+
col := tables[0].GetColumn(colMapper.Obj2Table("Id"))
592+
assert.NotNil(t, col)
593+
assert.True(t, col.IsPrimaryKey)
594+
assert.False(t, col.IsAutoIncrement)
595+
596+
col2 := tables[0].GetColumn(colMapper.Obj2Table("Name"))
597+
assert.NotNil(t, col2)
598+
assert.True(t, col2.IsPrimaryKey)
599+
assert.False(t, col2.IsAutoIncrement)
600+
}

xorm_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515

1616
_ "github.com/denisenkom/go-mssqldb"
1717
_ "github.com/go-sql-driver/mysql"
18-
"xorm.io/core"
1918
_ "github.com/lib/pq"
2019
_ "github.com/mattn/go-sqlite3"
2120
_ "github.com/ziutek/mymysql/godrv"
21+
"xorm.io/core"
2222
)
2323

2424
var (
@@ -35,6 +35,9 @@ var (
3535
splitter = flag.String("splitter", ";", "the splitter on connstr for cluster")
3636
schema = flag.String("schema", "", "specify the schema")
3737
ignoreSelectUpdate = flag.Bool("ignore_select_update", false, "ignore select update if implementation difference, only for tidb")
38+
39+
tableMapper core.IMapper
40+
colMapper core.IMapper
3841
)
3942

4043
func createEngine(dbType, connStr string) error {
@@ -122,6 +125,9 @@ func createEngine(dbType, connStr string) error {
122125
}
123126
}
124127

128+
tableMapper = testEngine.GetTableMapper()
129+
colMapper = testEngine.GetColumnMapper()
130+
125131
tables, err := testEngine.DBMetas()
126132
if err != nil {
127133
return err

0 commit comments

Comments
 (0)