[tracker/wip/carlosg/sparql-parser-ng: 276/306] libtracker-data: Bring back custom sparql syntax



commit 41205a490e19ca009f867ab9c6cad2e3a0b7a12b
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Mar 19 13:27:42 2018 +0100

    libtracker-data: Bring back custom sparql syntax
    
    Tracker used to accept plain Expression in SelectClause, i.e.
    no parentheses, and "AS ?var" being optional. Bring that back.

 src/libtracker-data/tracker-sparql-grammar.h | 15 ++++++--
 src/libtracker-data/tracker-sparql.c         | 52 ++++++++++++++++++++--------
 2 files changed, 50 insertions(+), 17 deletions(-)
---
diff --git a/src/libtracker-data/tracker-sparql-grammar.h b/src/libtracker-data/tracker-sparql-grammar.h
index c9b80faa3..e649acf54 100644
--- a/src/libtracker-data/tracker-sparql-grammar.h
+++ b/src/libtracker-data/tracker-sparql-grammar.h
@@ -1399,10 +1399,19 @@ static const TrackerGrammarRule helper_ConstructQuery_or[] = { S (helper_Constru
 static const TrackerGrammarRule rule_ConstructQuery[] = { L(CONSTRUCT), OR(helper_ConstructQuery_or), NIL };
 
 /* SelectClause ::= 'SELECT' ( 'DISTINCT' | 'REDUCED' )? ( ( Var | ( '(' Expression 'AS' Var ')' ) )+ | '*' )
- */
-static const TrackerGrammarRule helper_SelectClause_seq[] = { L(OPEN_PARENS), R(Expression), L(AS), R(Var), 
L(CLOSE_PARENS), NIL };
+ *
+ * TRACKER EXTENSION:
+ * Variable set also accepts the following syntax:
+ *   Expression ('AS' Var)?
+ *   Var ('AS' Var)?
+ */
+static const TrackerGrammarRule ext_SelectClause_seq_1[] = { L(AS), R(Var), NIL };
+static const TrackerGrammarRule ext_SelectClause_opt[] = { S(ext_SelectClause_seq_1), NIL };
+static const TrackerGrammarRule ext_SelectClause_seq_2[] = { R(Var), OPT(ext_SelectClause_opt), NIL };
+static const TrackerGrammarRule ext_SelectClause_seq_3[] = { R(Expression), OPT(ext_SelectClause_opt), NIL };
+static const TrackerGrammarRule helper_SelectClause_seq_1[] = { L(OPEN_PARENS), R(Expression), L(AS), 
R(Var), L(CLOSE_PARENS), NIL };
 static const TrackerGrammarRule helper_SelectClause_or_1[] = { L(DISTINCT), L(REDUCED), NIL };
-static const TrackerGrammarRule helper_SelectClause_or_2[] = { R(Var), S(helper_SelectClause_seq), NIL };
+static const TrackerGrammarRule helper_SelectClause_or_2[] = { S(ext_SelectClause_seq_2), 
S(ext_SelectClause_seq_3), R(Var), S(helper_SelectClause_seq_1), NIL };
 static const TrackerGrammarRule helper_SelectClause_gt0[] = { OR(helper_SelectClause_or_2), NIL };
 static const TrackerGrammarRule helper_SelectClause_opt[] = { OR(helper_SelectClause_or_1), NIL };
 static const TrackerGrammarRule helper_SelectClause_or_3[] = { L(GLOB), GT0(helper_SelectClause_gt0), NIL };
diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c
index 84e545f2b..e0778b68a 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -1641,6 +1641,29 @@ tracker_sparql_add_select_var (TrackerSparql       *sparql,
        g_array_append_val (sparql->var_types, type);
 }
 
+static gboolean
+handle_as (TrackerSparql        *sparql,
+          TrackerPropertyType   type,
+          GError              **error)
+{
+       TrackerBinding *binding;
+       TrackerVariable *var;
+
+       _call_rule (sparql, NAMED_RULE_Var, error);
+       var = _last_node_variable (sparql);
+
+       binding = tracker_variable_binding_new (var, NULL, NULL);
+       tracker_binding_set_data_type (binding, type);
+       tracker_variable_set_sample_binding (var, TRACKER_VARIABLE_BINDING (binding));
+       _append_string_printf (sparql, "AS %s ",
+                              tracker_variable_get_sql_expression (var));
+
+       if (sparql->current_state.select_context == sparql->context)
+               tracker_sparql_add_select_var (sparql, var->name, type);
+
+       return TRUE;
+}
+
 static gboolean
 translate_SelectClause (TrackerSparql  *sparql,
                         GError        **error)
@@ -1650,6 +1673,10 @@ translate_SelectClause (TrackerSparql  *sparql,
        gboolean first = TRUE;
 
        /* SelectClause ::= 'SELECT' ( 'DISTINCT' | 'REDUCED' )? ( ( Var | ( '(' Expression 'AS' Var ')' ) )+ 
| '*' )
+        *
+        * TRACKER EXTENSION:
+        * Variable set also accepts the following syntax:
+        *   Expression ('AS' Var)?
         */
        _expect (sparql, RULE_TYPE_LITERAL, LITERAL_SELECT);
        _append_string (sparql, "SELECT ");
@@ -1717,13 +1744,17 @@ translate_SelectClause (TrackerSparql  *sparql,
 
                                select_context->type = binding->data_type;
 
-                               if (sparql->current_state.select_context == sparql->context) {
+                               if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_AS)) {
+                                       if (!handle_as (sparql, binding->data_type, error))
+                                               return FALSE;
+                               } else if (sparql->current_state.select_context == sparql->context) {
                                        convert_expression_to_string (sparql, binding->data_type);
                                        tracker_sparql_add_select_var (sparql, var->name, binding->data_type);
                                }
 
                                tracker_sparql_swap_builder (sparql, old);
-                       } else if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_OPEN_PARENS)) {
+                       } else if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_OPEN_PARENS) ||
+                                  _check_in_rule (sparql, NAMED_RULE_Expression)) {
                                if (!first)
                                        _append_string (sparql, ", ");
 
@@ -1738,22 +1769,15 @@ translate_SelectClause (TrackerSparql  *sparql,
 
                                select_context->type = sparql->current_state.expression_type;
 
-                               _expect (sparql, RULE_TYPE_LITERAL, LITERAL_AS);
-                               _call_rule (sparql, NAMED_RULE_Var, error);
-                               var = _last_node_variable (sparql);
-
-                               if (sparql->current_state.select_context == sparql->context) {
-                                       _append_string_printf (sparql, "AS \"%s\" ", var->name);
+                               if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_AS)) {
+                                       if (!handle_as (sparql, sparql->current_state.expression_type, error))
+                                               return FALSE;
                                } else {
-                                       binding = tracker_variable_binding_new (var, NULL, NULL);
-                                       tracker_binding_set_data_type (binding, 
sparql->current_state.expression_type);
-                                       tracker_variable_set_sample_binding (var, TRACKER_VARIABLE_BINDING 
(binding));
-                                       _append_string_printf (sparql, "AS %s ",
-                                                              tracker_variable_get_sql_expression (var));
+                                       tracker_sparql_add_select_var (sparql, "", 
sparql->current_state.expression_type);
                                }
 
                                tracker_sparql_swap_builder (sparql, old);
-                               _expect (sparql, RULE_TYPE_LITERAL, LITERAL_CLOSE_PARENS);
+                               _accept (sparql, RULE_TYPE_LITERAL, LITERAL_CLOSE_PARENS);
                        } else {
                                break;
                        }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]