[tracker: 4/8] tracker-data, tracker-sparql: Exaustive cases in switch statements




commit 9c7bc6ecdb7f45cb6dce31af901bac45b5fd10c2
Author: Daniele Nicolodi <daniele grinta net>
Date:   Sat May 1 19:40:11 2021 +0200

    tracker-data,tracker-sparql: Exaustive cases in switch statements
    
    Having a case for each enumeration value allows to more easily verify
    the correctness of the code. Removing the default case allows to use
    the -Wswitch warning to identify code locations that need to be
    updated when a new enumeration value is added.

 src/libtracker-data/tracker-data-manager.c        |  2 +-
 src/libtracker-data/tracker-data-update.c         |  2 +-
 src/libtracker-data/tracker-db-interface-sqlite.c | 13 ++++++++----
 src/libtracker-data/tracker-sparql.c              |  2 +-
 src/libtracker-data/tracker-vtab-triples.c        |  3 ++-
 src/libtracker-sparql/tracker-serializer-json.c   | 18 +++++++----------
 src/libtracker-sparql/tracker-serializer-xml.c    | 24 ++++++++++-------------
 7 files changed, 31 insertions(+), 33 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index 264f6a5ea..ab20adda6 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -2543,7 +2543,7 @@ create_decomposed_metadata_property_table (TrackerDBInterface *iface,
        case TRACKER_PROPERTY_TYPE_DOUBLE:
                sql_type = "REAL";
                break;
-       default:
+       case TRACKER_PROPERTY_TYPE_UNKNOWN:
                sql_type = "";
                break;
        }
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index 7ed6beed2..7e2233c42 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -1715,7 +1715,7 @@ bytes_to_gvalue (GBytes              *bytes,
                g_value_init (gvalue, G_TYPE_INT64);
                g_value_set_int64 (gvalue, object_id);
                break;
-       default:
+       case TRACKER_PROPERTY_TYPE_UNKNOWN:
                g_warn_if_reached ();
                break;
        }
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c 
b/src/libtracker-data/tracker-db-interface-sqlite.c
index 2615c1403..5adac80be 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -3616,7 +3616,9 @@ tracker_db_cursor_get_value_type (TrackerDBCursor *cursor,
 
        if (column_type == SQLITE_NULL) {
                return TRACKER_SPARQL_VALUE_TYPE_UNBOUND;
-       } else if (column < cursor->n_types) {
+        }
+
+       if (column < cursor->n_types) {
                switch (cursor->types[column]) {
                case TRACKER_PROPERTY_TYPE_RESOURCE:
                        return TRACKER_SPARQL_VALUE_TYPE_URI;
@@ -3628,12 +3630,15 @@ tracker_db_cursor_get_value_type (TrackerDBCursor *cursor,
                        return TRACKER_SPARQL_VALUE_TYPE_DATETIME;
                case TRACKER_PROPERTY_TYPE_BOOLEAN:
                        return TRACKER_SPARQL_VALUE_TYPE_BOOLEAN;
-               default:
+                case TRACKER_PROPERTY_TYPE_DATE:
+                case TRACKER_PROPERTY_TYPE_LANGSTRING:
+               case TRACKER_PROPERTY_TYPE_STRING:
+                case TRACKER_PROPERTY_TYPE_UNKNOWN:
                        return TRACKER_SPARQL_VALUE_TYPE_STRING;
                }
-       } else {
-               return TRACKER_SPARQL_VALUE_TYPE_STRING;
        }
+
+        return TRACKER_SPARQL_VALUE_TYPE_STRING;
 }
 
 const gchar*
diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c
index d0fef654b..08a7a7cee 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -2177,7 +2177,7 @@ convert_expression_to_string (TrackerSparql       *sparql,
                _append_string (sparql, ") ");
                break;
        case TRACKER_PROPERTY_TYPE_LANGSTRING:
-       default:
+        case TRACKER_PROPERTY_TYPE_UNKNOWN:
                /* Let sqlite convert the expression to string */
                _prepend_string (sparql, "CAST (");
                _append_string (sparql, " AS TEXT) ");
diff --git a/src/libtracker-data/tracker-vtab-triples.c b/src/libtracker-data/tracker-vtab-triples.c
index 081566ae0..c9a0a2f40 100644
--- a/src/libtracker-data/tracker-vtab-triples.c
+++ b/src/libtracker-data/tracker-vtab-triples.c
@@ -360,7 +360,8 @@ convert_to_string (const gchar         *table_name,
        case TRACKER_PROPERTY_TYPE_DATETIME:
                return g_strdup_printf ("SparqlFormatTime (t.\"%s\")",
                                        table_name);
-       default:
+        case TRACKER_PROPERTY_TYPE_DOUBLE:
+       case TRACKER_PROPERTY_TYPE_UNKNOWN:
                /* Let sqlite convert the expression to string */
                return g_strdup_printf ("CAST (t.\"%s\" AS TEXT)",
                                        table_name);
diff --git a/src/libtracker-sparql/tracker-serializer-json.c b/src/libtracker-sparql/tracker-serializer-json.c
index 1b0570d3a..c9cfee523 100644
--- a/src/libtracker-sparql/tracker-serializer-json.c
+++ b/src/libtracker-sparql/tracker-serializer-json.c
@@ -139,14 +139,6 @@ serialize_up_to_position (TrackerSerializerJson  *serializer_json,
                for (i = 0; i < tracker_sparql_cursor_get_n_columns (cursor); i++) {
                        const gchar *var, *str, *type = NULL, *datatype = NULL;
 
-                       if (tracker_sparql_cursor_get_value_type (cursor, i) == 
TRACKER_SPARQL_VALUE_TYPE_UNBOUND)
-                               continue;
-
-                       var = g_ptr_array_index (serializer_json->vars, i);
-                       json_builder_set_member_name (builder, var);
-
-                       json_builder_begin_object (builder);
-
                        switch (tracker_sparql_cursor_get_value_type (cursor, i)) {
                        case TRACKER_SPARQL_VALUE_TYPE_URI:
                                type = "uri";
@@ -171,11 +163,15 @@ serialize_up_to_position (TrackerSerializerJson  *serializer_json,
                        case TRACKER_SPARQL_VALUE_TYPE_BLANK_NODE:
                                type = "bnode";
                                break;
-                       default:
-                               g_warn_if_reached ();
-                               break;
+                       case TRACKER_SPARQL_VALUE_TYPE_UNBOUND:
+                                continue;
                        }
 
+                       var = g_ptr_array_index (serializer_json->vars, i);
+                       json_builder_set_member_name (builder, var);
+
+                       json_builder_begin_object (builder);
+
                        json_builder_set_member_name (builder, "type");
                        json_builder_add_string_value (builder, type);
 
diff --git a/src/libtracker-sparql/tracker-serializer-xml.c b/src/libtracker-sparql/tracker-serializer-xml.c
index 1d1f5c10f..a722809bd 100644
--- a/src/libtracker-sparql/tracker-serializer-xml.c
+++ b/src/libtracker-sparql/tracker-serializer-xml.c
@@ -127,17 +127,6 @@ serialize_up_to_position (TrackerSerializerXml  *serializer_xml,
                for (i = 0; i < tracker_sparql_cursor_get_n_columns (cursor); i++) {
                        const gchar *var, *str, *type = NULL, *datatype = NULL;
 
-                       if (tracker_sparql_cursor_get_value_type (cursor, i) == 
TRACKER_SPARQL_VALUE_TYPE_UNBOUND)
-                               continue;
-
-                       var = g_ptr_array_index (serializer_xml->vars, i);
-
-                       xmlTextWriterStartElement (serializer_xml->writer, "binding");
-                       xmlTextWriterWriteFormatAttribute (serializer_xml->writer,
-                                                          "name",
-                                                          "%s",
-                                                          var);
-
                        switch (tracker_sparql_cursor_get_value_type (cursor, i)) {
                        case TRACKER_SPARQL_VALUE_TYPE_URI:
                                type = "uri";
@@ -162,11 +151,18 @@ serialize_up_to_position (TrackerSerializerXml  *serializer_xml,
                        case TRACKER_SPARQL_VALUE_TYPE_BLANK_NODE:
                                type = "bnode";
                                break;
-                       default:
-                               g_warn_if_reached ();
-                               break;
+                       case TRACKER_SPARQL_VALUE_TYPE_UNBOUND:
+                                continue;
                        }
 
+                       var = g_ptr_array_index (serializer_xml->vars, i);
+
+                       xmlTextWriterStartElement (serializer_xml->writer, "binding");
+                       xmlTextWriterWriteFormatAttribute (serializer_xml->writer,
+                                                          "name",
+                                                          "%s",
+                                                          var);
+
                        xmlTextWriterStartElement (serializer_xml->writer, type);
 
                        if (datatype) {


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