Skip to content

Commit

Permalink
sql/mssql: inspect computed column (#1781)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Jun 29, 2023
1 parent 043d7ad commit 566da41
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 46 deletions.
4 changes: 4 additions & 0 deletions sql/mssql/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@ const (
IndexTypeUnique = "UNIQUE"
IndexTypeXML = "XML"
)

const (
computedPersisted = "PERSISTED"
)
26 changes: 23 additions & 3 deletions sql/mssql/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,15 @@ func (i *inspect) addColumn(s *schema.Schema, rows *sql.Rows, scope queryScope)
table, name, typeName, comment, collation sql.NullString
nullable, userDefined sql.NullInt64
identity, identitySeek, identityIncrement sql.NullInt64
size, precision, scale sql.NullInt64
size, precision, scale, isPersisted sql.NullInt64
genexpr sql.NullString
isComputed int64
)
if err = rows.Scan(
&table, &name, &typeName, &comment,
&nullable, &userDefined,
&identity, &identitySeek, &identityIncrement,
&collation, &size, &precision, &scale,
&collation, &size, &precision, &scale, &isComputed, &genexpr, &isPersisted,
); err != nil {
return err
}
Expand All @@ -309,6 +311,18 @@ func (i *inspect) addColumn(s *schema.Schema, rows *sql.Rows, scope queryScope)
Increment: identityIncrement.Int64,
})
}
if isComputed == 1 {
if !sqlx.ValidString(genexpr) {
return fmt.Errorf("mssql: computed column %q is missing its definition", name.String)
}
x := &schema.GeneratedExpr{
Expr: genexpr.String,
}
if isPersisted.Valid && isPersisted.Int64 == 1 {
x.Type = computedPersisted
}
c.SetGeneratedExpr(x)
}
if sqlx.ValidString(comment) {
c.SetComment(comment.String)
}
Expand Down Expand Up @@ -515,13 +529,19 @@ SELECT
[collation_name] = [c1].[collation_name],
[max_length] = [c1].[max_length],
[precision] = [c1].[precision],
[scale] = [c1].[scale]
[scale] = [c1].[scale],
[is_computed] = [c1].[is_computed],
[computed_definition] = [cc].[definition],
[computed_persisted] = [cc].[is_persisted]
FROM
[sys].[tables] [t1]
INNER JOIN [sys].[columns] [c1]
ON [t1].[object_id] = [c1].[object_id]
INNER JOIN [sys].[types] [tp]
ON [c1].[user_type_id] = [tp].[user_type_id]
LEFT JOIN [sys].[computed_columns] [cc]
ON [cc].[object_id] = [c1].[object_id]
AND [cc].[column_id] = [c1].[column_id]
LEFT JOIN [sys].[identity_columns] [ti]
ON [ti].[object_id] = [c1].[object_id]
AND [ti].[column_id] = [c1].[column_id]
Expand Down
Loading

0 comments on commit 566da41

Please sign in to comment.