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

Parse: Detect tables in a SELECT INTO clause as DDL tables #281

Merged
merged 2 commits into from
Mar 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 lib/pg_query/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ def load_objects! # rubocop:disable Metrics/CyclomaticComplexity
@cte_names.concat(cte_names)
statements.concat(cte_statements)
end

if statement.select_stmt.into_clause
from_clause_items << { item: PgQuery::Node.new(range_var: statement.select_stmt.into_clause.rel), type: :ddl }
end
# The following statements modify the contents of a table
when :insert_stmt, :update_stmt, :delete_stmt
value = statement.public_send(statement.node)
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,15 @@
expect(query.call_functions).to eq ['unnest', 'json_build_object', 'row_to_json', 'public.my_function']
end

it 'finds the table in a SELECT INTO that is being created' do
query = described_class.parse(<<-SQL)
SELECT * INTO films_recent FROM films WHERE date_prod >= '2002-01-01';
SQL
expect(query.tables).to eq(['films', 'films_recent'])
expect(query.ddl_tables).to eq(['films_recent'])
expect(query.select_tables).to eq(['films'])
end

describe 'parsing INSERT' do
it 'finds the table inserted into' do
query = described_class.parse(<<-SQL)
Expand Down