Added column flag for autogenerated values like IDENTITY | TIMESTAMP | ROWVERSION
This commit is contained in:
parent
705befef07
commit
6fad1bd148
2 changed files with 21 additions and 16 deletions
|
@ -29,6 +29,11 @@ type Column struct {
|
||||||
// tinyint(1) instead of tinyint
|
// tinyint(1) instead of tinyint
|
||||||
// Used for "tinyint-as-bool" flag
|
// Used for "tinyint-as-bool" flag
|
||||||
FullDBType string
|
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.
|
// ColumnNames of the columns.
|
||||||
|
@ -57,7 +62,7 @@ func FilterColumnsByDefault(defaults bool, columns []Column) []Column {
|
||||||
var cols []Column
|
var cols []Column
|
||||||
|
|
||||||
for _, c := range columns {
|
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)
|
cols = append(cols, c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,10 +151,11 @@ func (m *MSSQLDriver) Columns(schema, tableName string) ([]bdb.Column, error) {
|
||||||
AND table_name = tc.table_name
|
AND table_name = tc.table_name
|
||||||
AND constraint_name = tc.constraint_name) = 1) THEN 1
|
AND constraint_name = tc.constraint_name) = 1) THEN 1
|
||||||
ELSE 0
|
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
|
FROM information_schema.columns c
|
||||||
WHERE table_name = ? AND table_schema = ?;
|
WHERE table_schema = $1 AND table_name = $2;
|
||||||
`, tableName, schema)
|
`, schema, tableName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -163,25 +164,24 @@ func (m *MSSQLDriver) Columns(schema, tableName string) ([]bdb.Column, error) {
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var colName, colType, colFullType string
|
var colName, colType, colFullType string
|
||||||
var nullable, unique bool
|
var nullable, unique, identity, auto bool
|
||||||
var defaultValue *string
|
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)
|
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{
|
column := bdb.Column{
|
||||||
Name: colName,
|
Name: colName,
|
||||||
FullDBType: colFullType, // example: tinyint(1) instead of tinyint
|
FullDBType: colFullType,
|
||||||
DBType: colType,
|
DBType: colType,
|
||||||
Nullable: nullable,
|
Nullable: nullable,
|
||||||
Unique: unique,
|
Unique: unique,
|
||||||
|
AutoGenerated: auto,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hack to exclude columns with types timestamp and rowversion from inserts
|
if defaultValue != nil && *defaultValue != "NULL" {
|
||||||
// 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" {
|
|
||||||
column.Default = *defaultValue
|
column.Default = *defaultValue
|
||||||
}
|
}
|
||||||
columns = append(columns, column)
|
columns = append(columns, column)
|
||||||
|
|
Loading…
Add table
Reference in a new issue