[tracker/wip/garnacho/sparql1.1: 20/23] libtracker-data: Add SparqlChecksum sqlite function



commit 2942e4e888b381b05f206544b711a9a44b29b747
Author: Carlos Garnacho <carlosg gnome org>
Date:   Thu Jul 30 00:06:13 2015 +0200

    libtracker-data: Add SparqlChecksum sqlite function
    
    This function will be used to back the various sparql checksum
    functions.

 src/libtracker-data/tracker-db-interface-sqlite.c |   41 +++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)
---
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c 
b/src/libtracker-data/tracker-db-interface-sqlite.c
index c72ef30..30c7678 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -1026,6 +1026,43 @@ function_sparql_rand (sqlite3_context *context,
        sqlite3_result_double (context, g_random_double ());
 }
 
+static void
+function_sparql_checksum (sqlite3_context *context,
+                         int              argc,
+                         sqlite3_value   *argv[])
+{
+       const gchar *str, *checksumstr;
+       GChecksumType checksum;
+       gchar *result;
+
+       if (argc != 2) {
+               sqlite3_result_error (context, "Invalid argument count", -1);
+               return;
+       }
+
+       str = sqlite3_value_text (argv[0]);
+       checksumstr = sqlite3_value_text (argv[1]);
+
+       if (!str)
+               return;
+
+       if (g_ascii_strcasecmp (checksumstr, "md5") == 0)
+               checksum = G_CHECKSUM_MD5;
+       else if (g_ascii_strcasecmp (checksumstr, "sha1") == 0)
+               checksum = G_CHECKSUM_SHA1;
+       else if (g_ascii_strcasecmp (checksumstr, "sha256") == 0)
+               checksum = G_CHECKSUM_SHA256;
+       else if (g_ascii_strcasecmp (checksumstr, "sha512") == 0)
+               checksum = G_CHECKSUM_SHA512;
+       else {
+               sqlite3_result_error (context, "Invalid checksum method specified", -1);
+               return;
+       }
+
+       result = g_compute_checksum_for_string (checksum, str, -1);
+       sqlite3_result_text (context, result, -1, g_free);
+}
+
 static inline int
 stmt_step (sqlite3_stmt *stmt)
 {
@@ -1171,6 +1208,10 @@ open_database (TrackerDBInterface  *db_interface,
                                 db_interface, &function_sparql_rand,
                                 NULL, NULL);
 
+       sqlite3_create_function (db_interface->db, "SparqlChecksum", 2, SQLITE_ANY,
+                                db_interface, &function_sparql_checksum,
+                                NULL, NULL);
+
        sqlite3_extended_result_codes (db_interface->db, 0);
        sqlite3_busy_timeout (db_interface->db, 100000);
 }


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