Skip to content

Commit

Permalink
get LIKE / NOT LIKE to work with deparser
Browse files Browse the repository at this point in the history
  • Loading branch information
Winslett committed May 10, 2016
1 parent 1d53280 commit 9006bd2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/pg_query/deparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def deparse_item(item, context = nil) # rubocop:disable Metrics/CyclomaticComple
deparse_aexpr_any(node)
when AEXPR_IN
deparse_aexpr_in(node)
when CONSTR_TYPE_FOREIGN
deparse_aexpr_like(node)
else
fail format("Can't deparse: %s: %s", type, node.inspect)
end
Expand Down Expand Up @@ -312,6 +314,12 @@ def deparse_aexpr_in(node)
format('%s %s (%s)', deparse_item(node['lexpr']), operator, rexpr.join(', '))
end

def deparse_aexpr_like(node)
value = deparse_item(node['rexpr'])
operator = node['name'].map { |n| deparse_item(n, :operator) } == ['~~'] ? 'LIKE' : 'NOT LIKE'
format('%s %s %s', deparse_item(node['lexpr']), operator, value)
end

def deparse_bool_expr_not(node)
format('NOT %s', deparse_item(node['args'][0]))
end
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
it { is_expected.to eq oneline_query }
end

context 'with LIKE filter' do
let(:query) { "SELECT * FROM \"users\" WHERE \"name\" LIKE 'postgresql:%';" }
it { is_expected.to eq oneline_query }
end

context 'with NOT LIKE filter' do
let(:query) { "SELECT * FROM \"users\" WHERE \"name\" NOT LIKE 'postgresql:%';" }
it { is_expected.to eq oneline_query }
end

context 'simple WITH statement' do
let(:query) { 'WITH t AS (SELECT random() AS x FROM generate_series(1, 3)) SELECT * FROM "t"' }
it { is_expected.to eq query }
Expand Down

0 comments on commit 9006bd2

Please sign in to comment.