From 27517e222f4ad4565c7f98e4a13820a33ebaebf2 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Wed, 20 Jul 2022 16:03:43 +0200 Subject: [PATCH 1/2] add optional_union_with_bind_as --- lib/sparql/client/query.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/sparql/client/query.rb b/lib/sparql/client/query.rb index f819459c..647c50f7 100644 --- a/lib/sparql/client/query.rb +++ b/lib/sparql/client/query.rb @@ -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? @@ -394,10 +402,38 @@ def to_s buffer << "{ #{sq.to_s} } ." end + def add_union_with_bind(patterns) + include_union = nil + buffer = [] + patterns.each do |pattern, options| + buffer << include_union if include_union + 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 + buffer += serialize_patterns(patterns) if options[:unions] include_union = nil options[:unions].each do |union_block| + buffer << include_union if include_union buffer << '{' buffer += serialize_patterns(union_block) From 05a31f305446e1f2a9c39f08545fec76019e667e Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Wed, 20 Jul 2022 16:04:56 +0200 Subject: [PATCH 2/2] update :unions_with_bind and add :optional_unions_with_bind --- lib/sparql/client/query.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/sparql/client/query.rb b/lib/sparql/client/query.rb index 647c50f7..c155871f 100644 --- a/lib/sparql/client/query.rb +++ b/lib/sparql/client/query.rb @@ -442,16 +442,15 @@ def add_union_with_bind(patterns) end end if options[:unions_with_bind] - include_union = nil - options[:unions_with_bind].each do |union_block, value_bind, var_bind| - buffer << include_union if include_union - buffer << '{' - buffer += serialize_patterns(union_block) - buffer << "BIND (\"#{value_bind}\" as ?#{var_bind.to_s})" - buffer << '}' - include_union = "UNION " - end + 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 {'