[tracker/wip/carlosg/sparql-parser-ng: 283/306] libtracker-data: Add internal UUID generator SQL function



commit 62a17b041fc33fa90f330d95f41649a7eec94f98
Author: Carlos Garnacho <carlosg gnome org>
Date:   Fri Aug 31 11:43:01 2018 +0200

    libtracker-data: Add internal UUID generator SQL function

 src/libtracker-data/meson.build                   |  1 +
 src/libtracker-data/tracker-data.h                |  1 +
 src/libtracker-data/tracker-db-interface-sqlite.c | 47 +++++++++++++++++++++++
 src/libtracker-data/tracker-uuid.c                | 46 ++++++++++++++++++++++
 src/libtracker-data/tracker-uuid.h                | 28 ++++++++++++++
 5 files changed, 123 insertions(+)
---
diff --git a/src/libtracker-data/meson.build b/src/libtracker-data/meson.build
index 4c84ea5ec..3ae44ec28 100644
--- a/src/libtracker-data/meson.build
+++ b/src/libtracker-data/meson.build
@@ -61,6 +61,7 @@ libtracker_data = library('tracker-data',
     'tracker-sparql-parser.c',
     'tracker-sparql-types.c',
     'tracker-sparql.c',
+    'tracker-uuid.c',
     tracker_common_enum_header,
     tracker_data_enums[0],
     tracker_data_enums[1],
diff --git a/src/libtracker-data/tracker-data.h b/src/libtracker-data/tracker-data.h
index 5ed5a7c4b..7188774e0 100644
--- a/src/libtracker-data/tracker-data.h
+++ b/src/libtracker-data/tracker-data.h
@@ -42,6 +42,7 @@
 #include "tracker-property.h"
 #include "tracker-sparql-query.h"
 #include "tracker-sparql.h"
+#include "tracker-uuid.h"
 
 #undef __LIBTRACKER_DATA_INSIDE__
 
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c 
b/src/libtracker-data/tracker-db-interface-sqlite.c
index 6faeb79ed..f2ecb93e8 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -55,6 +55,7 @@
 #include "tracker-db-interface-sqlite.h"
 #include "tracker-db-manager.h"
 #include "tracker-data-enum-types.h"
+#include "tracker-uuid.h"
 
 typedef struct {
        TrackerDBStatement *head;
@@ -1366,6 +1367,50 @@ stmt_step (sqlite3_stmt *stmt)
        return result;
 }
 
+static void
+function_sparql_uuid (sqlite3_context *context,
+                      int              argc,
+                      sqlite3_value   *argv[])
+{
+       gchar *uuid = NULL;
+       sqlite3_stmt *stmt;
+       sqlite3 *db;
+       gint result;
+
+       if (argc > 1) {
+               sqlite3_result_error (context, "Invalid argument count", -1);
+               return;
+       }
+
+       db = sqlite3_context_db_handle (context);
+
+       result = sqlite3_prepare_v2 (db, "SELECT ID FROM Resource WHERE Uri=?",
+                                    -1, &stmt, NULL);
+       if (result != SQLITE_OK) {
+               sqlite3_result_error (context, sqlite3_errstr (result), -1);
+               return;
+       }
+
+       do {
+               g_clear_pointer (&uuid, g_free);
+               uuid = tracker_generate_uuid ();
+
+               sqlite3_reset (stmt);
+               sqlite3_bind_text (stmt, 1, uuid, -1, SQLITE_TRANSIENT);
+               result = stmt_step (stmt);
+       } while (result == SQLITE_ROW);
+
+       sqlite3_finalize (stmt);
+
+       if (result != SQLITE_DONE) {
+               sqlite3_result_error (context, sqlite3_errstr (result), -1);
+               g_free (uuid);
+               return;
+       }
+
+       sqlite3_result_text (context, uuid, -1, g_free);
+}
+
 static int
 check_interrupt (void *user_data)
 {
@@ -1430,6 +1475,8 @@ initialize_functions (TrackerDBInterface *db_interface)
                { "SparqlFloor", 1, SQLITE_ANY | SQLITE_DETERMINISTIC,
                  function_sparql_floor },
                { "SparqlRand", 0, SQLITE_ANY, function_sparql_rand },
+               /* UUID */
+               { "SparqlUUID", 0, SQLITE_ANY, function_sparql_uuid },
        };
 
        for (i = 0; i < G_N_ELEMENTS (functions); i++) {
diff --git a/src/libtracker-data/tracker-uuid.c b/src/libtracker-data/tracker-uuid.c
new file mode 100644
index 000000000..fec35efa4
--- /dev/null
+++ b/src/libtracker-data/tracker-uuid.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2008-2010, Nokia
+ * Copyright (C) 2018, Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include "config.h"
+#include "tracker-uuid.h"
+
+#if GLIB_CHECK_VERSION (2, 52, 0)
+#include <uuid/uuid.h>
+#endif
+
+gchar *
+tracker_generate_uuid (void)
+{
+       gchar *result;
+#if GLIB_CHECK_VERSION (2, 52, 0)
+       gchar *uuid = g_uuid_string_random ();
+       result = g_strdup_printf ("urn:uuid:%s", uuid);
+       g_free (uuid);
+#else
+       uuid_t base = { 0, };
+       gchar uuid[37];
+
+       uuid_generate (base);
+       uuid_unparse_lower (base, uuid);
+       result = g_strdup_printf ("urn:uuid:%s", uuid);
+#endif
+
+       return result;
+}
diff --git a/src/libtracker-data/tracker-uuid.h b/src/libtracker-data/tracker-uuid.h
new file mode 100644
index 000000000..744171e32
--- /dev/null
+++ b/src/libtracker-data/tracker-uuid.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2008-2010, Nokia
+ * Copyright (C) 2018, Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __TRACKER_UUID_H__
+#define __TRACKER_UUID_H__
+
+#include <glib.h>
+
+gchar * tracker_generate_uuid (void);
+
+#endif /* __TRACKER_UUID_H__ */


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