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

Allow complex queries with table joins #62

Merged
merged 5 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 10 additions & 5 deletions app/models/scimitar/lists/query_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,15 @@ def apply_scim_filter(
column_names = self.activerecord_columns(scim_attribute)
value = self.activerecord_parameter(scim_parameter)
value_for_like = self.sql_modified_value(scim_operator, value)
all_supported = column_names.all? { | column_name | base_scope.model.column_names.include?(column_name.to_s) }
arel_columns = column_names.map do |column|
if base_scope.model.column_names.include?(column.to_s)
arel_table[column]
elsif column.is_a?(Arel::Attribute)
column
end
end

raise Scimitar::FilterError unless all_supported
raise Scimitar::FilterError unless arel_columns.all?

unless case_sensitive
lc_scim_attribute = scim_attribute.downcase()
Expand All @@ -615,8 +621,7 @@ def apply_scim_filter(
)
end

column_names.each.with_index do | column_name, index |
arel_column = arel_table[column_name]
arel_columns.each.with_index do | arel_column, index |
arel_operation = case scim_operator
when 'eq'
if case_sensitive
Expand All @@ -641,7 +646,7 @@ def apply_scim_filter(
when 'co', 'sw', 'ew'
arel_column.matches(value_for_like, nil, case_sensitive)
when 'pr'
arel_table.grouping(arel_column.not_eq_all(['', nil]))
arel_column.relation.grouping(arel_column.not_eq_all(['', nil]))
else
raise Scimitar::FilterError.new("Unsupported operator: '#{scim_operator}'")
end
Expand Down
2 changes: 2 additions & 0 deletions spec/apps/dummy/app/models/mock_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ def self.scim_queryable_attributes
'meta.lastModified' => { column: :updated_at },
'name.givenName' => { column: :first_name },
'name.familyName' => { column: :last_name },
'groups' => { column: MockGroup.arel_table[:id] },
'groups.value' => { column: MockGroup.arel_table[:id] },
'emails' => { columns: [ :work_email_address, :home_email_address ] },
'emails.value' => { columns: [ :work_email_address, :home_email_address ] },
'emails.type' => { ignore: true } # We can't filter on that; it'll just search all e-mails
Expand Down
60 changes: 60 additions & 0 deletions spec/models/scimitar/lists/query_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,66 @@
end
end # "context 'when instructed to ignore an attribute' do"

context 'when an arel column is mapped' do
let(:scope_with_groups) { MockUser.left_joins(:mock_groups) }

context 'with binary operators' do
it 'reads across all using OR' do
@instance.parse('groups eq "12345"')
query = @instance.to_activerecord_query(scope_with_groups)

expect(query.to_sql).to eql(<<~SQL.squish)
SELECT "mock_users".*
FROM "mock_users"
LEFT OUTER JOIN "mock_groups_users" ON "mock_groups_users"."mock_user_id" = "mock_users"."primary_key"
LEFT OUTER JOIN "mock_groups" ON "mock_groups"."id" = "mock_groups_users"."mock_group_id"
WHERE "mock_groups"."id" ILIKE 12345
SQL
end

it 'works with other query elements using correct precedence' do
@instance.parse('groups eq "12345" and emails eq "any@test.com"')
query = @instance.to_activerecord_query(scope_with_groups)

expect(query.to_sql).to eql(<<~SQL.squish)
SELECT "mock_users".*
FROM "mock_users"
LEFT OUTER JOIN "mock_groups_users" ON "mock_groups_users"."mock_user_id" = "mock_users"."primary_key"
LEFT OUTER JOIN "mock_groups" ON "mock_groups"."id" = "mock_groups_users"."mock_group_id"
WHERE "mock_groups"."id" ILIKE 12345 AND ("mock_users"."work_email_address" ILIKE 'any@test.com' OR "mock_users"."home_email_address" ILIKE 'any@test.com')
SQL
end
end # "context 'with binary operators' do"

context 'with unary operators' do
it 'reads across all using OR' do
@instance.parse('groups pr')
query = @instance.to_activerecord_query(scope_with_groups)

expect(query.to_sql).to eql(<<~SQL.squish)
SELECT "mock_users".*
FROM "mock_users"
LEFT OUTER JOIN "mock_groups_users" ON "mock_groups_users"."mock_user_id" = "mock_users"."primary_key"
LEFT OUTER JOIN "mock_groups" ON "mock_groups"."id" = "mock_groups_users"."mock_group_id"
WHERE ("mock_groups"."id" != NULL AND "mock_groups"."id" IS NOT NULL)
SQL
end

it 'works with other query elements using correct precedence' do
@instance.parse('name.familyName eq "John" and groups pr')
query = @instance.to_activerecord_query(scope_with_groups)

expect(query.to_sql).to eql(<<~SQL.squish)
SELECT "mock_users".*
FROM "mock_users"
LEFT OUTER JOIN "mock_groups_users" ON "mock_groups_users"."mock_user_id" = "mock_users"."primary_key"
LEFT OUTER JOIN "mock_groups" ON "mock_groups"."id" = "mock_groups_users"."mock_group_id"
WHERE "mock_users"."last_name" ILIKE 'John' AND ("mock_groups"."id" != NULL AND "mock_groups"."id" IS NOT NULL)
SQL
end
end # "context 'with unary operators' do
end # "context 'when an arel column is mapped' do"

context 'with complex cases' do
context 'using AND' do
it 'generates expected SQL' do
Expand Down