diff --git a/dialect_mssql.go b/dialect_mssql.go index ce4dd00c..524d05a4 100644 --- a/dialect_mssql.go +++ b/dialect_mssql.go @@ -338,6 +338,7 @@ func (db *mssql) TableCheckSql(tableName string) (string, []interface{}) { func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column, error) { args := []interface{}{} 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, ISNULL(i.is_primary_key, 0) from sys.columns a @@ -361,8 +362,8 @@ func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column for rows.Next() { var name, ctype, vdefault string var maxLen, precision, scale int - var nullable, isPK bool - err = rows.Scan(&name, &ctype, &maxLen, &precision, &scale, &nullable, &vdefault, &isPK) + var nullable, isPK, defaultIsNull bool + err = rows.Scan(&name, &ctype, &maxLen, &precision, &scale, &nullable, &defaultIsNull, &vdefault, &isPK) if err != nil { 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.Name = strings.Trim(name, "` ") col.Nullable = nullable - col.Default = vdefault + col.DefaultIsEmpty = defaultIsNull + if !defaultIsNull { + col.Default = vdefault + } col.IsPrimaryKey = isPK ct := strings.ToUpper(ctype) 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 colSeq = append(colSeq, col.Name) } diff --git a/dialect_postgres.go b/dialect_postgres.go index 6b2da93e..ccef3086 100644 --- a/dialect_postgres.go +++ b/dialect_postgres.go @@ -1045,11 +1045,17 @@ WHERE c.relkind = 'r'::char AND c.relname = $1%s AND f.attnum > 0 ORDER BY f.att col.Length = maxLen - if !col.DefaultIsEmpty && col.SQLType.IsText() { - if strings.HasSuffix(col.Default, "::character varying") { - col.Default = strings.TrimRight(col.Default, "::character varying") - } else if !strings.HasPrefix(col.Default, "'") { - col.Default = "'" + col.Default + "'" + if !col.DefaultIsEmpty { + if col.SQLType.IsText() { + if strings.HasSuffix(col.Default, "::character varying") { + col.Default = strings.TrimRight(col.Default, "::character varying") + } else if !strings.HasPrefix(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 diff --git a/tag_test.go b/tag_test.go index dde4f3d4..891c6ffc 100644 --- a/tag_test.go +++ b/tag_test.go @@ -362,7 +362,9 @@ func TestTagDefault4(t *testing.T) { } } 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) { diff --git a/test_mssql.sh b/test_mssql.sh index e26e1641..7f060cff 100755 --- a/test_mssql.sh +++ b/test_mssql.sh @@ -1 +1 @@ -go test -db=mssql -conn_str="server=localhost;user id=sa;password=MwantsaSecurePassword1;database=xorm_test" \ No newline at end of file +go test -db=mssql -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test" \ No newline at end of file