Skip to content

Commit

Permalink
Merge pull request #7 from ontoportal-lirmm/pr/feature/add-optional-u…
Browse files Browse the repository at this point in the history
…nions-with-bind-query-clause

Add optional unions with bind query clause
  • Loading branch information
mdorf authored Apr 11, 2024
2 parents 55e7dbf + 05a31f3 commit 1657f0d
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions lib/sparql/client/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ def union_with_bind_as(*pattern_list)
self
end

def optional_union_with_bind_as(*pattern_list)
options[:optional_unions_with_bind] ||= []

pattern_list.each do |patterns,bind,filter|
options[:optional_unions_with_bind] << [build_patterns(patterns), bind, filter]
end
self
end

def cache_key
return nil if options[:from].nil? || options[:from].empty?
Expand Down Expand Up @@ -394,28 +402,55 @@ def to_s
buffer << "{ #{sq.to_s} } ."
end

buffer += serialize_patterns(patterns)
if options[:unions]
def add_union_with_bind(patterns)
include_union = nil
options[:unions].each do |union_block|
buffer = []
patterns.each do |pattern, options|
buffer << include_union if include_union
buffer << '{'
buffer += serialize_patterns(union_block)
buffer << '} '
buffer += serialize_patterns(pattern)
if options[:filters]
buffer += options[:filters].map do |filter|
str = filter[:values].map do |val|
"?#{filter[:predicate]} = <#{val}>"
end
"FILTER(#{str.join(' || ')}) "
end
end

if options[:binds]
buffer += options[:binds].map { |bind| "BIND( \"#{bind[:value]}\" as ?#{bind[:as]})" }
end


buffer << '}'
include_union = "UNION "
end
buffer
end
if options[:unions_with_bind]

buffer += serialize_patterns(patterns)
if options[:unions]
include_union = nil
options[:unions_with_bind].each do |union_block, value_bind, var_bind|
options[:unions].each do |union_block|

buffer << include_union if include_union
buffer << '{'
buffer += serialize_patterns(union_block)
buffer << "BIND (\"#{value_bind}\" as ?#{var_bind.to_s})"
buffer << '}'
buffer << '} '
include_union = "UNION "
end
end
if options[:unions_with_bind]
buffer << add_union_with_bind(options[:unions_with_bind])
end

if options[:optional_unions_with_bind] && !options[:optional_unions_with_bind].empty?
buffer << 'OPTIONAL {'
buffer << add_union_with_bind(options[:optional_unions_with_bind])
buffer << '}'
end

if options[:optionals]
options[:optionals].each do |patterns|
buffer << 'OPTIONAL {'
Expand Down

0 comments on commit 1657f0d

Please sign in to comment.