Properly handle MAX

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
Andrew Thornton 2020-09-05 21:49:46 +01:00
parent c825309e5b
commit 749818bddd
No known key found for this signature in database
GPG Key ID: 3CDE74631F13A748
2 changed files with 29 additions and 9 deletions

View File

@ -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 {

View File

@ -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")))
}
}