Skip to content

Commit

Permalink
Merge pull request #39712 from arthurschreiber/arthur/use-subquery-fo…
Browse files Browse the repository at this point in the history
…r-information-schema

Use a subquery when filtering `information_schema.tables` by `table_name`.
  • Loading branch information
eileencodes committed Jun 25, 2020
2 parents 3e68892 + c900114 commit 0f6a4f5
Showing 1 changed file with 8 additions and 4 deletions.
Expand Up @@ -203,10 +203,14 @@ def add_options_for_index_columns(quoted_columns, **options)
def data_source_sql(name = nil, type: nil)
scope = quoted_scope(name, type: type)

sql = +"SELECT table_name FROM information_schema.tables"
sql << " WHERE table_schema = #{scope[:schema]}"
sql << " AND table_name = #{scope[:name]}" if scope[:name]
sql << " AND table_type = #{scope[:type]}" if scope[:type]
sql = +"SELECT table_name FROM (SELECT * FROM information_schema.tables "
sql << " WHERE table_schema = #{scope[:schema]}) _subquery"
if scope[:type] || scope[:name]
conditions = []
conditions << "_subquery.table_type = #{scope[:type]}" if scope[:type]
conditions << "_subquery.table_name = #{scope[:name]}" if scope[:name]
sql << " WHERE #{conditions.join(" AND ")}"
end
sql
end

Expand Down

0 comments on commit 0f6a4f5

Please sign in to comment.