Skip to content

Commit

Permalink
Deparse ILIKE, COLLATE and DISCARD (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
emin100 authored and lfittl committed May 6, 2019
1 parent c2b1859 commit 374c297
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
29 changes: 29 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 AEXPR_ILIKE
deparse_aexpr_ilike(node)
when CONSTR_TYPE_FOREIGN
deparse_aexpr_like(node)
when AEXPR_BETWEEN, AEXPR_NOT_BETWEEN, AEXPR_BETWEEN_SYM, AEXPR_NOT_BETWEEN_SYM
Expand Down Expand Up @@ -78,6 +80,8 @@ def deparse_item(item, context = nil) # rubocop:disable Metrics/CyclomaticComple
deparse_case(node)
when COALESCE_EXPR
deparse_coalesce(node)
when COLLATE_CLAUSE
deparse_collate(node)
when COLUMN_DEF
deparse_columndef(node)
when COLUMN_REF
Expand All @@ -100,6 +104,8 @@ def deparse_item(item, context = nil) # rubocop:disable Metrics/CyclomaticComple
deparse_defelem(node)
when DELETE_STMT
deparse_delete_from(node)
when DISCARD_STMT
deparse_discard(node)
when DROP_STMT
deparse_drop(node)
when EXPLAIN_STMT
Expand Down Expand Up @@ -356,6 +362,12 @@ def deparse_aexpr_like(node)
format('%s %s %s', deparse_item(node['lexpr']), operator, value)
end

def deparse_aexpr_ilike(node)
value = deparse_item(node['rexpr'])
operator = node['name'][0]['String']['str'] == '~~*' ? 'ILIKE' : 'NOT ILIKE'
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 Expand Up @@ -510,6 +522,14 @@ def deparse_sortby(node)
output.join(' ')
end

def deparse_collate(node)
output = []
output << deparse_item(node['arg'])
output << 'COLLATE'
output << deparse_item_list(node['collname'])
output.join(' ')
end

def deparse_with_clause(node)
output = ['WITH']
output << 'RECURSIVE' if node['recursive']
Expand Down Expand Up @@ -1085,6 +1105,15 @@ def deparse_delete_from(node)
output.join(' ')
end

def deparse_discard(node)
output = ['DISCARD']
output << 'ALL' if (node['target']).zero?
output << 'PLANS' if node['target'] == 1
output << 'SEQUENCES' if node['target'] == 2
output << 'TEMP' if node['target'] == 3
output.join(' ')
end

def deparse_drop(node)
output = ['DROP']
output << 'TABLE' if node['removeType'] == OBJECT_TYPE_TABLE
Expand Down
2 changes: 2 additions & 0 deletions lib/pg_query/node_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PgQuery
CHECK_POINT_STMT = 'CheckPointStmt'.freeze
CLOSE_PORTAL_STMT = 'ClosePortalStmt'.freeze
COALESCE_EXPR = 'CoalesceExpr'.freeze
COLLATE_CLAUSE = 'CollateClause'.freeze
COLUMN_DEF = 'ColumnDef'.freeze
COLUMN_REF = 'ColumnRef'.freeze
COMMON_TABLE_EXPR = 'CommonTableExpr'.freeze
Expand All @@ -34,6 +35,7 @@ class PgQuery
DECLARE_CURSOR_STMT = 'DeclareCursorStmt'.freeze
DEF_ELEM = 'DefElem'.freeze
DELETE_STMT = 'DeleteStmt'.freeze
DISCARD_STMT = 'DiscardStmt'.freeze
DO_STMT = 'DoStmt'.freeze
DROP_STMT = 'DropStmt'.freeze
EXECUTE_STMT = 'ExecuteStmt'.freeze
Expand Down
47 changes: 47 additions & 0 deletions spec/lib/pg_query/deparse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@
it { is_expected.to eq query }
end

context 'ORDER BY with COLLATE' do
let(:query) { 'SELECT * FROM "a" ORDER BY "x" COLLATE "tr_TR" DESC NULLS LAST' }

it { is_expected.to eq query }
end

context 'text with COLLATE' do
let(:query) { "SELECT 'foo' COLLATE \"tr_TR\"" }

it { is_expected.to eq query }
end

context 'with specific column alias' do
let(:query) { "SELECT * FROM (VALUES ('anne', 'smith'), ('bob', 'jones'), ('joe', 'blow')) names(\"first\", \"last\")" }

Expand All @@ -80,6 +92,18 @@
it { is_expected.to eq oneline_query }
end

context 'with ILIKE filter' do
let(:query) { "SELECT * FROM \"users\" WHERE \"name\" ILIKE 'postgresql:%';" }

it { is_expected.to eq oneline_query }
end

context 'with NOT ILIKE filter' do
let(:query) { "SELECT * FROM \"users\" WHERE \"name\" NOT ILIKE '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"' }

Expand Down Expand Up @@ -1131,6 +1155,29 @@
it { is_expected.to eq oneline_query }
end
end

context 'DISCARD' do
context 'all' do
let(:query) { 'DISCARD ALL' }

it { is_expected.to eq oneline_query }
end
context 'plans' do
let(:query) { 'DISCARD PLANS' }

it { is_expected.to eq oneline_query }
end
context 'sequences' do
let(:query) { 'DISCARD SEQUENCES' }

it { is_expected.to eq oneline_query }
end
context 'temp' do
let(:query) { 'DISCARD TEMP' }

it { is_expected.to eq oneline_query }
end
end
end

describe '#deparse' do
Expand Down

0 comments on commit 374c297

Please sign in to comment.