mssql自增长ID未判断 #1383

Closed
opened 2019-08-05 12:42:01 +00:00 by xiaohai244 · 0 comments
xiaohai244 commented 2019-08-05 12:42:01 +00:00 (Migrated from github.com)

这个是修改后的
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, replace(replace(isnull(c.text,''),'(',''),')','') as vdefault, ISNULL(i.is_primary_key, 0), a.is_identity from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id left join sys.syscomments c on a.default_object_id=c.id LEFT OUTER JOIN sys.index_columns ic ON ic.object_id = a.object_id AND ic.column_id = a.column_id LEFT OUTER JOIN sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id where a.object_id=object_id(' + tableName + ')
db.LogSQL(s, args)

rows, err := db.DB().Query(s, args...)
if err != nil {
	return nil, nil, err
}
defer rows.Close()

cols := make(map[string]*core.Column)
colSeq := make([]string, 0)
for rows.Next() {
	var name, ctype, vdefault string
	var maxLen, precision, scale int
	var nullable, isPK bool
	var isIncrement bool
	err = rows.Scan(&name, &ctype, &maxLen, &precision, &scale, &nullable, &vdefault, &isPK,&isIncrement)
	if err != nil {
		return nil, nil, err
	}

	col := new(core.Column)
	col.Indexes = make(map[string]int)
	col.Name = strings.Trim(name, "` ")
	col.Nullable = nullable
	col.Default = vdefault
	col.IsPrimaryKey = isPK
	col.IsAutoIncrement = isIncrement
	ct := strings.ToUpper(ctype)
	if ct == "DECIMAL" {
		col.Length = precision
		col.Length2 = scale
	} else {
		col.Length = maxLen
	}
	switch ct {
	case "DATETIMEOFFSET":
		col.SQLType = core.SQLType{Name: core.TimeStampz, DefaultLength: 0, DefaultLength2: 0}
	case "NVARCHAR":
		col.SQLType = core.SQLType{Name: core.NVarchar, DefaultLength: 0, DefaultLength2: 0}
	case "IMAGE":
		col.SQLType = core.SQLType{Name: core.VarBinary, DefaultLength: 0, DefaultLength2: 0}
	default:
		if _, ok := core.SqlTypes[ct]; ok {
			col.SQLType = core.SQLType{Name: ct, DefaultLength: 0, DefaultLength2: 0}
		} else {
			return nil, nil, fmt.Errorf("Unknown colType %v for %v - %v", ct, tableName, col.Name)
		}
	}

	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)
}
return colSeq, cols, nil

}

这个是修改后的 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, replace(replace(isnull(c.text,''),'(',''),')','') as vdefault, ISNULL(i.is_primary_key, 0), a.is_identity from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id left join sys.syscomments c on a.default_object_id=c.id LEFT OUTER JOIN sys.index_columns ic ON ic.object_id = a.object_id AND ic.column_id = a.column_id LEFT OUTER JOIN sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id where a.object_id=object_id('` + tableName + `')` db.LogSQL(s, args) rows, err := db.DB().Query(s, args...) if err != nil { return nil, nil, err } defer rows.Close() cols := make(map[string]*core.Column) colSeq := make([]string, 0) for rows.Next() { var name, ctype, vdefault string var maxLen, precision, scale int var nullable, isPK bool var isIncrement bool err = rows.Scan(&name, &ctype, &maxLen, &precision, &scale, &nullable, &vdefault, &isPK,&isIncrement) if err != nil { return nil, nil, err } col := new(core.Column) col.Indexes = make(map[string]int) col.Name = strings.Trim(name, "` ") col.Nullable = nullable col.Default = vdefault col.IsPrimaryKey = isPK col.IsAutoIncrement = isIncrement ct := strings.ToUpper(ctype) if ct == "DECIMAL" { col.Length = precision col.Length2 = scale } else { col.Length = maxLen } switch ct { case "DATETIMEOFFSET": col.SQLType = core.SQLType{Name: core.TimeStampz, DefaultLength: 0, DefaultLength2: 0} case "NVARCHAR": col.SQLType = core.SQLType{Name: core.NVarchar, DefaultLength: 0, DefaultLength2: 0} case "IMAGE": col.SQLType = core.SQLType{Name: core.VarBinary, DefaultLength: 0, DefaultLength2: 0} default: if _, ok := core.SqlTypes[ct]; ok { col.SQLType = core.SQLType{Name: ct, DefaultLength: 0, DefaultLength2: 0} } else { return nil, nil, fmt.Errorf("Unknown colType %v for %v - %v", ct, tableName, col.Name) } } 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) } return colSeq, cols, nil }
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1383
No description provided.