Skip to content

Commit

Permalink
Improve performance of view default function lookup (rails-sqlserver#…
Browse files Browse the repository at this point in the history
…1074)

* Improve performance of view default function lookup (rails-sqlserver#1073)

* Update CHANGELOG.md

---------

Co-authored-by: David Genord II <[email protected]>
  • Loading branch information
2 people authored and Lavika committed Sep 26, 2023
1 parent f95780f commit 8de95b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

#### Changed

- [#1073](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1073) Improve performance of view default function lookup

## v7.0.3.0

#### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,15 @@ def column_definitions(table_name)
view_exists = view_exists?(table_name)
view_tblnm = view_table_name(table_name) if view_exists

if view_exists
results = sp_executesql %{
SELECT c.COLUMN_NAME AS [name], c.COLUMN_DEFAULT AS [default]
FROM #{database}.INFORMATION_SCHEMA.COLUMNS c
WHERE c.TABLE_NAME = #{quote(view_tblnm)}
}.squish, "SCHEMA", []
default_functions = results.each.with_object({}) {|row, out| out[row["name"]] = row["default"] }.compact
end

sql = column_definitions_sql(database, identifier)

binds = []
Expand Down Expand Up @@ -402,13 +411,8 @@ def column_definitions(table_name)
ci[:default_function] = begin
default = ci[:default_value]
if default.nil? && view_exists
default = select_value %{
SELECT c.COLUMN_DEFAULT
FROM #{database}.INFORMATION_SCHEMA.COLUMNS c
WHERE
c.TABLE_NAME = '#{view_tblnm}'
AND c.COLUMN_NAME = '#{views_real_column_name(table_name, ci[:name])}'
}.squish, "SCHEMA"
view_column = views_real_column_name(table_name, ci[:name])
default = default_functions[view_column] if view_column.present?
end
case default
when nil
Expand Down

0 comments on commit 8de95b9

Please sign in to comment.