Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql/mssql: inspect computed column #1781

Merged
merged 1 commit into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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