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

LIKE / NOT LIKE to work with deparser #49

Merged
merged 1 commit into from
May 11, 2016
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
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