Set column comment when create table for mssql database

This commit is contained in:
LiQiming 2020-09-14 17:51:54 +08:00
parent 435f4d5815
commit e9d8d416ef
1 changed files with 33 additions and 3 deletions

View File

@ -216,10 +216,12 @@ type mssql struct {
Base Base
defaultVarchar string defaultVarchar string
defaultChar string defaultChar string
defaultSchema string
} }
func (db *mssql) Init(uri *URI) error { func (db *mssql) Init(uri *URI) error {
db.quoter = mssqlQuoter db.quoter = mssqlQuoter
db.defaultSchema = "dbo"
return db.Base.Init(db, uri) return db.Base.Init(db, uri)
} }
@ -249,10 +251,21 @@ func (db *mssql) SetParams(params map[string]string) {
} else { } else {
db.defaultChar = "CHAR" db.defaultChar = "CHAR"
} }
if defaultSchema, ok := params["DEFAULT_SCHEMA"]; ok && defaultSchema != "" {
db.defaultSchema = defaultSchema
} else {
db.defaultSchema = "dbo"
}
} }
func (db *mssql) SQLType(c *schemas.Column) string { func (db *mssql) SQLType(c *schemas.Column) string {
var res string var res string
var defaultVarchar string = schemas.Varchar
if db.defaultVarchar != "" {
defaultVarchar = db.defaultVarchar
}
switch t := c.SQLType.Name; t { switch t := c.SQLType.Name; t {
case schemas.Bool: case schemas.Bool:
res = schemas.Bit res = schemas.Bit
@ -285,7 +298,7 @@ func (db *mssql) SQLType(c *schemas.Column) string {
case schemas.MediumInt: case schemas.MediumInt:
res = schemas.Int res = schemas.Int
case schemas.Text, schemas.MediumText, schemas.TinyText, schemas.LongText, schemas.Json: case schemas.Text, schemas.MediumText, schemas.TinyText, schemas.LongText, schemas.Json:
res = db.defaultVarchar + "(MAX)" res = defaultVarchar + "(MAX)"
case schemas.Double: case schemas.Double:
res = schemas.Real res = schemas.Real
case schemas.Uuid: case schemas.Uuid:
@ -303,7 +316,7 @@ func (db *mssql) SQLType(c *schemas.Column) string {
res += "(MAX)" res += "(MAX)"
} }
case schemas.Varchar: case schemas.Varchar:
res = db.defaultVarchar res = defaultVarchar
if c.Length == -1 { if c.Length == -1 {
res += "(MAX)" res += "(MAX)"
} }
@ -567,12 +580,23 @@ func (db *mssql) CreateTableSQL(table *schemas.Table, tableName string) ([]strin
pkList := table.PrimaryKeys pkList := table.PrimaryKeys
var multiCommentSql []string
for _, colName := range table.ColumnsSeq() { for _, colName := range table.ColumnsSeq() {
col := table.GetColumn(colName) col := table.GetColumn(colName)
s, _ := ColumnString(db, col, col.IsPrimaryKey && len(pkList) == 1) s, _ := ColumnString(db, col, col.IsPrimaryKey && len(pkList) == 1)
sql += s sql += s
sql = strings.TrimSpace(sql) sql = strings.TrimSpace(sql)
sql += ", " sql += ", "
if col.Comment != "" {
commentSql := fmt.Sprintf("EXEC sys.sp_addextendedproperty @name=N'MS_Description', "+
"@value=N'%s' , "+
"@level0type=N'SCHEMA',@level0name=N'%s', "+
"@level1type=N'TABLE',@level1name=N'%s', "+
"@level2type=N'COLUMN',@level2name=N'%s'; ", col.Comment, db.defaultSchema, tableName, col.FieldName)
multiCommentSql = append(multiCommentSql, commentSql)
}
} }
if len(pkList) > 1 { if len(pkList) > 1 {
@ -583,7 +607,13 @@ func (db *mssql) CreateTableSQL(table *schemas.Table, tableName string) ([]strin
sql = sql[:len(sql)-2] + ")" sql = sql[:len(sql)-2] + ")"
sql += ";" sql += ";"
return []string{sql}, true
multiSql := []string{sql}
if len(multiCommentSql) > 0 {
multiSql = append(multiSql, multiCommentSql...)
}
return multiSql, true
} }
func (db *mssql) ForUpdateSQL(query string) string { func (db *mssql) ForUpdateSQL(query string) string {