fix mssql default
This commit is contained in:
parent
1a7ddf84c2
commit
3bf78cd4fc
|
@ -338,6 +338,7 @@ func (db *mssql) TableCheckSql(tableName string) (string, []interface{}) {
|
||||||
func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
||||||
args := []interface{}{}
|
args := []interface{}{}
|
||||||
s := `select a.name as name, b.name as ctype,a.max_length,a.precision,a.scale,a.is_nullable as nullable,
|
s := `select a.name as name, b.name as ctype,a.max_length,a.precision,a.scale,a.is_nullable as nullable,
|
||||||
|
"default_is_null" = (CASE WHEN c.text is null THEN 1 ELSE 0 END),
|
||||||
replace(replace(isnull(c.text,''),'(',''),')','') as vdefault,
|
replace(replace(isnull(c.text,''),'(',''),')','') as vdefault,
|
||||||
ISNULL(i.is_primary_key, 0)
|
ISNULL(i.is_primary_key, 0)
|
||||||
from sys.columns a
|
from sys.columns a
|
||||||
|
@ -361,8 +362,8 @@ func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var name, ctype, vdefault string
|
var name, ctype, vdefault string
|
||||||
var maxLen, precision, scale int
|
var maxLen, precision, scale int
|
||||||
var nullable, isPK bool
|
var nullable, isPK, defaultIsNull bool
|
||||||
err = rows.Scan(&name, &ctype, &maxLen, &precision, &scale, &nullable, &vdefault, &isPK)
|
err = rows.Scan(&name, &ctype, &maxLen, &precision, &scale, &nullable, &defaultIsNull, &vdefault, &isPK)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -371,7 +372,10 @@ func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column
|
||||||
col.Indexes = make(map[string]int)
|
col.Indexes = make(map[string]int)
|
||||||
col.Name = strings.Trim(name, "` ")
|
col.Name = strings.Trim(name, "` ")
|
||||||
col.Nullable = nullable
|
col.Nullable = nullable
|
||||||
|
col.DefaultIsEmpty = defaultIsNull
|
||||||
|
if !defaultIsNull {
|
||||||
col.Default = vdefault
|
col.Default = vdefault
|
||||||
|
}
|
||||||
col.IsPrimaryKey = isPK
|
col.IsPrimaryKey = isPK
|
||||||
ct := strings.ToUpper(ctype)
|
ct := strings.ToUpper(ctype)
|
||||||
if ct == "DECIMAL" {
|
if ct == "DECIMAL" {
|
||||||
|
@ -395,15 +399,6 @@ func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if col.SQLType.IsText() || col.SQLType.IsTime() {
|
|
||||||
if col.Default != "" {
|
|
||||||
col.Default = "'" + col.Default + "'"
|
|
||||||
} else {
|
|
||||||
if col.DefaultIsEmpty {
|
|
||||||
col.Default = "''"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cols[col.Name] = col
|
cols[col.Name] = col
|
||||||
colSeq = append(colSeq, col.Name)
|
colSeq = append(colSeq, col.Name)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1045,12 +1045,18 @@ WHERE c.relkind = 'r'::char AND c.relname = $1%s AND f.attnum > 0 ORDER BY f.att
|
||||||
|
|
||||||
col.Length = maxLen
|
col.Length = maxLen
|
||||||
|
|
||||||
if !col.DefaultIsEmpty && col.SQLType.IsText() {
|
if !col.DefaultIsEmpty {
|
||||||
|
if col.SQLType.IsText() {
|
||||||
if strings.HasSuffix(col.Default, "::character varying") {
|
if strings.HasSuffix(col.Default, "::character varying") {
|
||||||
col.Default = strings.TrimRight(col.Default, "::character varying")
|
col.Default = strings.TrimRight(col.Default, "::character varying")
|
||||||
} else if !strings.HasPrefix(col.Default, "'") {
|
} else if !strings.HasPrefix(col.Default, "'") {
|
||||||
col.Default = "'" + col.Default + "'"
|
col.Default = "'" + col.Default + "'"
|
||||||
}
|
}
|
||||||
|
} else if col.SQLType.IsTime() {
|
||||||
|
if strings.HasSuffix(col.Default, "::timestamp without time zone") {
|
||||||
|
col.Default = strings.TrimRight(col.Default, "::timestamp without time zone")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cols[col.Name] = col
|
cols[col.Name] = col
|
||||||
colSeq = append(colSeq, col.Name)
|
colSeq = append(colSeq, col.Name)
|
||||||
|
|
|
@ -362,7 +362,9 @@ func TestTagDefault4(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert.True(t, isDefaultExist)
|
assert.True(t, isDefaultExist)
|
||||||
assert.EqualValues(t, "CURRENT_TIMESTAMP", defaultVal)
|
assert.True(t, "CURRENT_TIMESTAMP" == defaultVal ||
|
||||||
|
"now()" == defaultVal ||
|
||||||
|
"getdate" == defaultVal, defaultVal)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTagDefault5(t *testing.T) {
|
func TestTagDefault5(t *testing.T) {
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
go test -db=mssql -conn_str="server=localhost;user id=sa;password=MwantsaSecurePassword1;database=xorm_test"
|
go test -db=mssql -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test"
|
Loading…
Reference in New Issue