Added column flag for autogenerated values like IDENTITY | TIMESTAMP | ROWVERSION

This commit is contained in:
Sergey Kurt 2017-03-15 15:31:09 +03:00
parent 705befef07
commit 6fad1bd148
2 changed files with 21 additions and 16 deletions

View file

@ -29,6 +29,11 @@ type Column struct {
// tinyint(1) instead of tinyint
// Used for "tinyint-as-bool" flag
FullDBType string
// MS SQL only bits
// Used to indicate that the value
// for this column is auto generated by database on insert (i.e. - timestamp (old) or rowversion (new))
AutoGenerated bool
}
// ColumnNames of the columns.
@ -57,7 +62,7 @@ func FilterColumnsByDefault(defaults bool, columns []Column) []Column {
var cols []Column
for _, c := range columns {
if (defaults && len(c.Default) != 0) || (!defaults && len(c.Default) == 0) {
if (defaults && (len(c.Default) != 0 || c.AutoGenerated)) || (!defaults && len(c.Default) == 0 && !c.AutoGenerated) {
cols = append(cols, c)
}
}

View file

@ -151,10 +151,11 @@ func (m *MSSQLDriver) Columns(schema, tableName string) ([]bdb.Column, error) {
AND table_name = tc.table_name
AND constraint_name = tc.constraint_name) = 1) THEN 1
ELSE 0
END AS is_unique
END AS is_unique,
COLUMNPROPERTY(object_id($1 + '.' + $2), c.column_name, 'IsIdentity') as is_identity
FROM information_schema.columns c
WHERE table_name = ? AND table_schema = ?;
`, tableName, schema)
WHERE table_schema = $1 AND table_name = $2;
`, schema, tableName)
if err != nil {
return nil, err
@ -163,25 +164,24 @@ func (m *MSSQLDriver) Columns(schema, tableName string) ([]bdb.Column, error) {
for rows.Next() {
var colName, colType, colFullType string
var nullable, unique bool
var nullable, unique, identity, auto bool
var defaultValue *string
if err := rows.Scan(&colName, &colFullType, &colType, &defaultValue, &nullable, &unique); err != nil {
if err := rows.Scan(&colName, &colFullType, &colType, &defaultValue, &nullable, &unique, &identity); err != nil {
return nil, errors.Wrapf(err, "unable to scan for table %s", tableName)
}
auto = identity || strings.EqualFold(colType, "timestamp") || strings.EqualFold(colType, "rowversion")
column := bdb.Column{
Name: colName,
FullDBType: colFullType, // example: tinyint(1) instead of tinyint
DBType: colType,
Nullable: nullable,
Unique: unique,
Name: colName,
FullDBType: colFullType,
DBType: colType,
Nullable: nullable,
Unique: unique,
AutoGenerated: auto,
}
// Hack to exclude columns with types timestamp and rowversion from inserts
// Values for this columns provides by SQL Server on Insert clause
if (strings.EqualFold(colType, "timestamp") || strings.EqualFold(colType, "rowversion")) && defaultValue == nil {
column.Default = colType
} else if defaultValue != nil && *defaultValue != "NULL" {
if defaultValue != nil && *defaultValue != "NULL" {
column.Default = *defaultValue
}
columns = append(columns, column)