Skip to content

Commit

Permalink
Merge pull request #77 from Papierkorb/master
Browse files Browse the repository at this point in the history
Deparse SELECT DISTINCT clause
  • Loading branch information
lfittl authored Mar 31, 2018
2 parents f316a76 + 2669bf4 commit a086233
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ def deparse_select(node) # rubocop:disable Metrics/CyclomaticComplexity

if node[TARGET_LIST_FIELD]
output << 'SELECT'
output << 'DISTINCT' if node['distinctClause']
output << node[TARGET_LIST_FIELD].map do |item|
deparse_item(item, :select)
end.join(', ')
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
it { is_expected.to eq query }
end

context 'with DISTINCT' do
let(:query) { 'SELECT DISTINCT "a", "b", * FROM "c" WHERE "d" = "e"' }

it { is_expected.to eq query }
end

context 'complex SELECT statement' do
let(:query) { 'SELECT "memory_total_bytes", "memory_swap_total_bytes" - "memory_swap_free_bytes" AS swap, date_part(?, "s"."collected_at") AS collected_at FROM "snapshots" s JOIN "system_snapshots" ON "snapshot_id" = "s"."id" WHERE "s"."database_id" = ? AND "s"."collected_at" >= ? AND "s"."collected_at" <= ? ORDER BY "collected_at" ASC' }

Expand Down

0 comments on commit a086233

Please sign in to comment.