diff --git a/dialects/mssql.go b/dialects/mssql.go index 7617c4ad..8e76e538 100644 --- a/dialects/mssql.go +++ b/dialects/mssql.go @@ -242,7 +242,7 @@ func (db *mssql) SetParams(params map[string]string) { var t = strings.ToUpper(defaultChar) switch t { case "NCHAR", "CHAR": - db.defaultChar = defaultChar + db.defaultChar = t default: db.defaultChar = "CHAR" } @@ -297,10 +297,26 @@ func (db *mssql) SQLType(c *schemas.Column) string { case schemas.BigInt: res = schemas.BigInt c.Length = 0 + case schemas.NVarchar: + res = t + if c.Length == -1 { + res += "(MAX)" + } case schemas.Varchar: res = db.defaultVarchar + if c.Length == -1 { + res += "(MAX)" + } case schemas.Char: res = db.defaultChar + if c.Length == -1 { + res += "(MAX)" + } + case schemas.NChar: + res = t + if c.Length == -1 { + res += "(MAX)" + } default: res = t } @@ -424,13 +440,17 @@ func (db *mssql) GetColumns(queryer core.Queryer, ctx context.Context, tableName col.SQLType = schemas.SQLType{Name: schemas.TimeStampz, DefaultLength: 0, DefaultLength2: 0} case "NVARCHAR": col.SQLType = schemas.SQLType{Name: schemas.NVarchar, DefaultLength: 0, DefaultLength2: 0} - col.Length /= 2 - col.Length2 /= 2 + if col.Length > 0 { + col.Length /= 2 + col.Length2 /= 2 + } case "IMAGE": col.SQLType = schemas.SQLType{Name: schemas.VarBinary, DefaultLength: 0, DefaultLength2: 0} case "NCHAR": - col.Length /= 2 - col.Length2 /= 2 + if col.Length > 0 { + col.Length /= 2 + col.Length2 /= 2 + } fallthrough default: if _, ok := schemas.SqlTypes[ct]; ok { diff --git a/integrations/session_schema_test.go b/integrations/session_schema_test.go index 0f839c02..bcaf30aa 100644 --- a/integrations/session_schema_test.go +++ b/integrations/session_schema_test.go @@ -157,7 +157,7 @@ func TestSyncTable3(t *testing.T) { Id int64 Name string Text string `xorm:"TEXT"` - Char byte `xorm:"CHAR"` + Char byte `xorm:"CHAR(1)"` } assert.NoError(t, PrepareEngine()) @@ -197,9 +197,9 @@ func TestSyncTable3(t *testing.T) { assert.NoError(t, err) tableInfo, err := testEngine.TableInfo(new(SyncTable5)) assert.NoError(t, err) - assert.EqualValues(t, testEngine.Dialect().SQLType(tables[0].GetColumn("name")), testEngine.Dialect().SQLType(tableInfo.GetColumn("name"))) - assert.EqualValues(t, testEngine.Dialect().SQLType(tables[0].GetColumn("text")), testEngine.Dialect().SQLType(tableInfo.GetColumn("text"))) - assert.EqualValues(t, testEngine.Dialect().SQLType(tables[0].GetColumn("char")), testEngine.Dialect().SQLType(tableInfo.GetColumn("char"))) + assert.EqualValues(t, testEngine.Dialect().SQLType(tableInfo.GetColumn("name")), testEngine.Dialect().SQLType(tables[0].GetColumn("name"))) + assert.EqualValues(t, testEngine.Dialect().SQLType(tableInfo.GetColumn("text")), testEngine.Dialect().SQLType(tables[0].GetColumn("text"))) + assert.EqualValues(t, testEngine.Dialect().SQLType(tableInfo.GetColumn("char")), testEngine.Dialect().SQLType(tables[0].GetColumn("char"))) } }