GPtrArray* tracker_data_query_rdf_type (gint id) { TrackerDBCursor *cursor = NULL; TrackerDBInterface *iface; TrackerDBStatement *stmt; GPtrArray *ret = NULL; GError *error = NULL; iface = tracker_db_manager_get_db_interface (); stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_SELECT, &error, "SELECT (SELECT Uri FROM Resource WHERE ID = \"rdf:type\") " "FROM \"rdfs:Resource_rdf:type\" " "WHERE ID = ?"); if (stmt) { tracker_db_statement_bind_int (stmt, 0, id); cursor = tracker_db_statement_start_cursor (stmt, &error); g_object_unref (stmt); } if (cursor) { /* Query is usually a rather small result, but let's try to * avoid reallocs in gptrarray.c as much as possible (this * function is called fairly often) */ ret = g_ptr_array_sized_new (20); while (tracker_db_cursor_iter_next (cursor, NULL, &error)) { const gchar *class_uri; TrackerClass *cl; class_uri = tracker_db_cursor_get_string (cursor, 0, NULL); cl = tracker_ontologies_get_class_by_uri (class_uri); if (!cl) { g_critical ("Unknown class %s", class_uri); continue; } g_ptr_array_add (ret, cl); } g_object_unref (cursor); } if (G_UNLIKELY (error)) { g_critical ("Could not query RDF type: %s\n", error->message); g_error_free (error); if (ret) { g_ptr_array_free (ret, FALSE); ret = NULL; } } return ret; }
static void query_helper (const gchar *query_filename, const gchar *results_filename) { GError *error = NULL; gchar *queries = NULL, *query; gchar *results = NULL; GString *test_results = NULL; g_file_get_contents (query_filename, &queries, NULL, &error); g_assert_no_error (error); g_file_get_contents (results_filename, &results, NULL, &error); g_assert_no_error (error); /* perform actual query */ query = strtok (queries, "~"); while (query) { TrackerDBCursor *cursor; cursor = tracker_data_query_sparql_cursor (query, &error); g_assert_no_error (error); /* compare results with reference output */ if (!test_results) { test_results = g_string_new (""); } else { g_string_append (test_results, "~\n"); } if (cursor) { gint col; while (tracker_db_cursor_iter_next (cursor, NULL, &error)) { for (col = 0; col < tracker_db_cursor_get_n_columns (cursor); col++) { const gchar *str; if (col > 0) { g_string_append (test_results, "\t"); } str = tracker_db_cursor_get_string (cursor, col, NULL); if (str != NULL) { /* bound variable */ g_string_append_printf (test_results, "\"%s\"", str); } } g_string_append (test_results, "\n"); } g_object_unref (cursor); } query = strtok (NULL, "~"); } if (strcmp (results, test_results->str)) { /* print result difference */ gchar *quoted_results; gchar *command_line; gchar *quoted_command_line; gchar *shell; gchar *diff; quoted_results = g_shell_quote (test_results->str); command_line = g_strdup_printf ("echo -n %s | diff -u %s -", quoted_results, results_filename); quoted_command_line = g_shell_quote (command_line); shell = g_strdup_printf ("sh -c %s", quoted_command_line); g_spawn_command_line_sync (shell, &diff, NULL, NULL, &error); g_assert_no_error (error); g_error ("%s", diff); g_free (quoted_results); g_free (command_line); g_free (quoted_command_line); g_free (shell); g_free (diff); } g_string_free (test_results, TRUE); g_free (results); g_free (queries); }
static void check_result (TrackerDBCursor *cursor, const TestInfo *test_info, const gchar *results_filename, GError *error) { int (*comparer) (const char *a, const char *b); GString *test_results; gchar *results; GError *nerror = NULL; if (test_info->expect_query_error) { comparer = strstr_i; g_assert (error != NULL); } else { comparer = strcmp; g_assert_no_error (error); } g_file_get_contents (results_filename, &results, NULL, &nerror); g_assert_no_error (nerror); g_clear_error (&nerror); /* compare results with reference output */ test_results = g_string_new (""); if (cursor) { gint col; while (tracker_db_cursor_iter_next (cursor, NULL, &error)) { for (col = 0; col < tracker_db_cursor_get_n_columns (cursor); col++) { const gchar *str; if (col > 0) { g_string_append (test_results, "\t"); } str = tracker_db_cursor_get_string (cursor, col, NULL); if (str != NULL) { /* bound variable */ g_string_append_printf (test_results, "\"%s\"", str); } } g_string_append (test_results, "\n"); } } else if (test_info->expect_query_error) { g_string_append (test_results, error->message); g_clear_error (&error); } if (comparer (results, test_results->str)) { /* print result difference */ gchar *quoted_results; gchar *command_line; gchar *quoted_command_line; gchar *shell; gchar *diff; quoted_results = g_shell_quote (test_results->str); command_line = g_strdup_printf ("echo -n %s | diff -u %s -", quoted_results, results_filename); quoted_command_line = g_shell_quote (command_line); shell = g_strdup_printf ("sh -c %s", quoted_command_line); g_spawn_command_line_sync (shell, &diff, NULL, NULL, &error); g_assert_no_error (error); g_error ("%s", diff); g_free (quoted_results); g_free (command_line); g_free (quoted_command_line); g_free (shell); g_free (diff); } g_string_free (test_results, TRUE); g_free (results); }