Skip to content

Commit

Permalink
Merge pull request #503 from kyrofa/feature/json-support
Browse files Browse the repository at this point in the history
column: add support for Arel::Nodes::SqlLiteral
  • Loading branch information
nertzy authored May 8, 2023
2 parents 17c0b34 + 0a4f9f1 commit 9a0ede0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
8 changes: 5 additions & 3 deletions lib/pg_search/configuration/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ class Column

def initialize(column_name, weight, model)
@name = column_name.to_s
@column_name = column_name.to_s
@column_name = column_name
@weight = weight
@model = model
@connection = model.connection
end

def full_name
return @column_name if @column_name.is_a?(Arel::Nodes::SqlLiteral)

"#{table_name}.#{column_name}"
end

def to_sql
"coalesce(#{expression}::text, '')"
"coalesce((#{expression})::text, '')"
end

private
Expand All @@ -30,7 +32,7 @@ def table_name
end

def column_name
@connection.quote_column_name(@column_name)
@connection.quote_column_name(@name)
end

def expression
Expand Down
14 changes: 13 additions & 1 deletion spec/lib/pg_search/configuration/column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,37 @@
with_model :Model do
table do |t|
t.string :name
t.json :object
end
end

it "returns the fully-qualified table and column name" do
column = described_class.new("name", nil, Model)
expect(column.full_name).to eq(%(#{Model.quoted_table_name}."name"))
end

it "returns nested json attributes" do
column = described_class.new(Arel.sql("object->>'name'"), nil, Model)
expect(column.full_name).to eq(%(object->>'name'))
end
end

describe "#to_sql" do
with_model :Model do
table do |t|
t.string :name
t.json :object
end
end

it "returns an expression that casts the column to text and coalesces it with an empty string" do
column = described_class.new("name", nil, Model)
expect(column.to_sql).to eq(%{coalesce(#{Model.quoted_table_name}."name"::text, '')})
expect(column.to_sql).to eq(%{coalesce((#{Model.quoted_table_name}."name")::text, '')})
end

it "returns an expression that casts the nested json attribute to text and coalesces it with an empty string" do
column = described_class.new(Arel.sql("object->>'name'"), nil, Model)
expect(column.to_sql).to eq(%{coalesce((object->>'name')::text, '')})
end
end
end
4 changes: 2 additions & 2 deletions spec/lib/pg_search/features/dmetaphone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

feature = described_class.new(query, options, columns, Model, normalizer)
expect(feature.rank.to_sql).to eq(
%{(ts_rank((to_tsvector('simple', pg_search_dmetaphone(coalesce(#{Model.quoted_table_name}."name"::text, ''))) || to_tsvector('simple', pg_search_dmetaphone(coalesce(#{Model.quoted_table_name}."content"::text, '')))), (to_tsquery('simple', ''' ' || pg_search_dmetaphone('query') || ' ''')), 0))}
%{(ts_rank((to_tsvector('simple', pg_search_dmetaphone(coalesce((#{Model.quoted_table_name}."name")::text, ''))) || to_tsvector('simple', pg_search_dmetaphone(coalesce((#{Model.quoted_table_name}."content")::text, '')))), (to_tsquery('simple', ''' ' || pg_search_dmetaphone('query') || ' ''')), 0))}
)
end
end
Expand All @@ -48,7 +48,7 @@

feature = described_class.new(query, options, columns, Model, normalizer)
expect(feature.conditions.to_sql).to eq(
%{((to_tsvector('simple', pg_search_dmetaphone(coalesce(#{Model.quoted_table_name}."name"::text, ''))) || to_tsvector('simple', pg_search_dmetaphone(coalesce(#{Model.quoted_table_name}."content"::text, '')))) @@ (to_tsquery('simple', ''' ' || pg_search_dmetaphone('query') || ' ''')))}
%{((to_tsvector('simple', pg_search_dmetaphone(coalesce((#{Model.quoted_table_name}."name")::text, ''))) || to_tsvector('simple', pg_search_dmetaphone(coalesce((#{Model.quoted_table_name}."content")::text, '')))) @@ (to_tsquery('simple', ''' ' || pg_search_dmetaphone('query') || ' ''')))}
)
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/pg_search/features/trigram_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

let(:coalesced_columns) do
<<~SQL.squish
coalesce(#{Model.quoted_table_name}."name"::text, '')
coalesce((#{Model.quoted_table_name}."name")::text, '')
|| ' '
|| coalesce(#{Model.quoted_table_name}."content"::text, '')
|| coalesce((#{Model.quoted_table_name}."content")::text, '')
SQL
end

Expand Down Expand Up @@ -88,7 +88,7 @@
let(:options) { {only: :name} }

it "only searches against the select column" do
coalesced_column = "coalesce(#{Model.quoted_table_name}.\"name\"::text, '')"
coalesced_column = "coalesce((#{Model.quoted_table_name}.\"name\")::text, '')"
expect(feature.conditions.to_sql).to eq("('#{query}' % (#{coalesced_column}))")
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/lib/pg_search/features/tsearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

feature = described_class.new(query, options, columns, Model, normalizer)
expect(feature.rank.to_sql).to eq(
%{(ts_rank((to_tsvector('simple', coalesce(#{Model.quoted_table_name}."name"::text, '')) || to_tsvector('simple', coalesce(#{Model.quoted_table_name}."content"::text, ''))), (to_tsquery('simple', ''' ' || 'query' || ' ''')), 0))}
%{(ts_rank((to_tsvector('simple', coalesce((#{Model.quoted_table_name}."name")::text, '')) || to_tsvector('simple', coalesce((#{Model.quoted_table_name}."content")::text, ''))), (to_tsquery('simple', ''' ' || 'query' || ' ''')), 0))}
)
end

Expand Down Expand Up @@ -67,7 +67,7 @@

feature = described_class.new(query, options, columns, Model, normalizer)
expect(feature.conditions.to_sql).to eq(
%{((to_tsvector('simple', coalesce(#{Model.quoted_table_name}."name"::text, '')) || to_tsvector('simple', coalesce(#{Model.quoted_table_name}."content"::text, ''))) @@ (to_tsquery('simple', ''' ' || 'query' || ' ''')))}
%{((to_tsvector('simple', coalesce((#{Model.quoted_table_name}."name")::text, '')) || to_tsvector('simple', coalesce((#{Model.quoted_table_name}."content")::text, ''))) @@ (to_tsquery('simple', ''' ' || 'query' || ' ''')))}
)
end

Expand All @@ -84,7 +84,7 @@

feature = described_class.new(query, options, columns, Model, normalizer)
expect(feature.conditions.to_sql).to eq(
%{((to_tsvector('simple', coalesce(#{Model.quoted_table_name}."name"::text, '')) || to_tsvector('simple', coalesce(#{Model.quoted_table_name}."content"::text, ''))) @@ (to_tsquery('simple', '!' || ''' ' || 'query' || ' ''')))}
%{((to_tsvector('simple', coalesce((#{Model.quoted_table_name}."name")::text, '')) || to_tsvector('simple', coalesce((#{Model.quoted_table_name}."content")::text, ''))) @@ (to_tsquery('simple', '!' || ''' ' || 'query' || ' ''')))}
)
end
end
Expand All @@ -102,7 +102,7 @@

feature = described_class.new(query, options, columns, Model, normalizer)
expect(feature.conditions.to_sql).to eq(
%{((to_tsvector('simple', coalesce(#{Model.quoted_table_name}."name"::text, '')) || to_tsvector('simple', coalesce(#{Model.quoted_table_name}."content"::text, ''))) @@ (to_tsquery('simple', ''' ' || '!query' || ' ''')))}
%{((to_tsvector('simple', coalesce((#{Model.quoted_table_name}."name")::text, '')) || to_tsvector('simple', coalesce((#{Model.quoted_table_name}."content")::text, ''))) @@ (to_tsquery('simple', ''' ' || '!query' || ' ''')))}
)
end
end
Expand Down Expand Up @@ -164,7 +164,7 @@

feature = described_class.new(query, options, columns, Model, normalizer)
expect(feature.highlight.to_sql).to eq(
"(ts_headline('simple', (coalesce(#{Model.quoted_table_name}.\"name\"::text, '')), (to_tsquery('simple', ''' ' || 'query' || ' ''')), ''))"
"(ts_headline('simple', (coalesce((#{Model.quoted_table_name}.\"name\")::text, '')), (to_tsquery('simple', ''' ' || 'query' || ' ''')), ''))"
)
end

Expand All @@ -189,7 +189,7 @@

feature = described_class.new(query, options, columns, Model, normalizer)

expected_sql = %{(ts_headline('spanish', (coalesce(#{Model.quoted_table_name}."name"::text, '') || ' ' || coalesce(#{Model.quoted_table_name}."content"::text, '')), (to_tsquery('spanish', ''' ' || 'query' || ' ''')), 'StartSel = "<b>", StopSel = "</b>"'))}
expected_sql = %{(ts_headline('spanish', (coalesce((#{Model.quoted_table_name}."name")::text, '') || ' ' || coalesce((#{Model.quoted_table_name}."content")::text, '')), (to_tsquery('spanish', ''' ' || 'query' || ' ''')), 'StartSel = "<b>", StopSel = "</b>"'))}

expect(feature.highlight.to_sql).to eq(expected_sql)
end
Expand Down Expand Up @@ -221,7 +221,7 @@

feature = described_class.new(query, options, columns, Model, normalizer)

expected_sql = %{(ts_headline('simple', (coalesce(#{Model.quoted_table_name}."name"::text, '')), (to_tsquery('simple', ''' ' || 'query' || ' ''')), 'StartSel = "<start class=""search"">", StopSel = "<stop>", MaxFragments = 3, MaxWords = 123, MinWords = 456, ShortWord = 4, FragmentDelimiter = "&hellip;", HighlightAll = TRUE'))}
expected_sql = %{(ts_headline('simple', (coalesce((#{Model.quoted_table_name}."name")::text, '')), (to_tsquery('simple', ''' ' || 'query' || ' ''')), 'StartSel = "<start class=""search"">", StopSel = "<stop>", MaxFragments = 3, MaxWords = 123, MinWords = 456, ShortWord = 4, FragmentDelimiter = "&hellip;", HighlightAll = TRUE'))}

expect(feature.highlight.to_sql).to eq(expected_sql)
end
Expand Down Expand Up @@ -252,7 +252,7 @@
feature = described_class.new(query, options, columns, Model, normalizer)

highlight_sql = ActiveSupport::Deprecation.silence { feature.highlight.to_sql }
expected_sql = %{(ts_headline('simple', (coalesce(#{Model.quoted_table_name}."name"::text, '')), (to_tsquery('simple', ''' ' || 'query' || ' ''')), 'StartSel = "<start class=""search"">", StopSel = "<stop>", MaxFragments = 3, MaxWords = 123, MinWords = 456, ShortWord = 4, FragmentDelimiter = "&hellip;", HighlightAll = FALSE'))}
expected_sql = %{(ts_headline('simple', (coalesce((#{Model.quoted_table_name}."name")::text, '')), (to_tsquery('simple', ''' ' || 'query' || ' ''')), 'StartSel = "<start class=""search"">", StopSel = "<stop>", MaxFragments = 3, MaxWords = 123, MinWords = 456, ShortWord = 4, FragmentDelimiter = "&hellip;", HighlightAll = FALSE'))}

expect(highlight_sql).to eq(expected_sql)
end
Expand Down

0 comments on commit 9a0ede0

Please sign in to comment.