Commit de701275 authored by Carlos Garnacho's avatar Carlos Garnacho

libtracker-data: Handle empty clauses before BIND

The way BIND works, we wrap the current statements with a SELECT
where we can set the variable name. This however broke if there's
no prior statements, eg:

  SELECT ?a { BIND (?a AS ... ) ... }

Generate correct SQL for this case.
parent cc4ffe93
......@@ -3203,16 +3203,25 @@ translate_Bind (TrackerSparql *sparql,
TrackerVariable *variable;
TrackerBinding *binding;
TrackerPropertyType type;
gboolean is_empty;
/* Bind ::= 'BIND' '(' Expression 'AS' Var ')'
str = _prepend_placeholder (sparql);
old = tracker_sparql_swap_builder (sparql, str);
is_empty = tracker_string_builder_is_empty (sparql->current_state.sql);
if (!is_empty) {
str = _prepend_placeholder (sparql);
old = tracker_sparql_swap_builder (sparql, str);
_append_string (sparql, "SELECT ");
if (!is_empty)
_append_string (sparql, "*, ");
_append_string (sparql, "SELECT *, ");
_call_rule (sparql, NAMED_RULE_Expression, error);
type = sparql->current_state.expression_type;
......@@ -3224,15 +3233,18 @@ translate_Bind (TrackerSparql *sparql,
if (tracker_variable_has_bindings (variable))
_raise (PARSE, "Expected undefined variable", "BIND");
_append_string_printf (sparql, "AS %s FROM (",
_append_string_printf (sparql, "AS %s ",
tracker_variable_get_sql_expression (variable));
binding = tracker_variable_binding_new (variable, NULL, NULL);
tracker_binding_set_data_type (binding, type);
tracker_variable_set_sample_binding (variable, TRACKER_VARIABLE_BINDING (binding));
tracker_sparql_swap_builder (sparql, old);
_append_string (sparql, ") ");
if (!is_empty) {
_append_string (sparql, "FROM (");
tracker_sparql_swap_builder (sparql, old);
_append_string (sparql, ") ");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment