]> saetta.ns0.it Git - zakaudit/libzakaudit/commitdiff
Added function ZakAudit::get_user_insertion (closes #809).
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Wed, 18 Dec 2013 12:37:57 +0000 (13:37 +0100)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Wed, 18 Dec 2013 12:37:57 +0000 (13:37 +0100)
src/audit.c
src/libzakaudit.h
tests/test1.c

index 54ffd97a1b0187e3adf227e85b53c43887b3b43b..d3cceee2bcb6622a927fe10f362d423a14e97ea2 100644 (file)
@@ -1277,6 +1277,8 @@ GtkWidget
                        return NULL;
                }
 
+       sql_where = g_string_new ("");
+
        l = g_strv_length ((gchar **)key_values);
        for (strpart = 0; strpart < l; strpart++)
                {
@@ -1298,15 +1300,6 @@ GtkWidget
                                                field_value);
                }
 
-       if (sql_where->len == 0)
-               {
-                       return NULL;
-               }
-       else
-               {
-                       g_string_erase (sql_where, 0, 5);
-               }
-
        sql = g_strdup_printf ("(SELECT a.id, a.type, a.username, a.date,"
                               " v.value"
                               " FROM actions AS a"
@@ -1314,7 +1307,7 @@ GtkWidget
                               " INNER JOIN fields AS f ON v.id_fields = f.id"
                               " WHERE a.type = %d"
                               " AND a.id_tables = %d"
-                              " AND %s"
+                              "%s"
                               " AND f.is_key = TRUE)"
                               " UNION"
                               " (SELECT a.id, a.type, a.username, a.date,"
@@ -1324,7 +1317,7 @@ GtkWidget
                               " INNER JOIN fields AS f ON v.id_fields = f.id"
                               " WHERE a.type = %d"
                               " AND a.id_tables = %d"
-                              " AND %s"
+                              "%s"
                               " AND f.is_key = TRUE)"
                               " UNION",
                               " (SELECT a.id, a.type, a.username, a.date,"
@@ -1334,7 +1327,7 @@ GtkWidget
                               " INNER JOIN fields AS f ON v.id_fields = f.id"
                               " WHERE a.type = %d"
                               " AND a.id_tables = %d"
-                              " AND %s"
+                              "%s"
                               " AND f.is_key = TRUE)",
                               ZAK_AUDIT_ACTION_INSERT,
                               table->id,
@@ -1357,7 +1350,7 @@ GtkWidget
                                                                       "|", -1),
                                                           &error);
 
-                       tstore = gtk_builder_get_object (priv->gtkbuilder, "tstore_changes");
+                       tstore = (GtkTreeStore *)gtk_builder_get_object (priv->gtkbuilder, "tstore_changes");
 
                        /* TODO check if key field value is changed */
 
@@ -1374,6 +1367,99 @@ GtkWidget
        return ret;
 }
 
+/**
+ * zak_audit_get_user_insertion:
+ * @zak_audit: an #ZakAudit object.
+ * @datasource_name:
+ * @table_name:
+ * @key_values:
+ *
+ */
+const gchar
+*zak_audit_get_user_insertion (ZakAudit *zak_audit,
+                               const gchar *datasource_name,
+                               const gchar *table_name,
+                               const gchar **key_values)
+{
+       Datasource *datasource;
+       Table *table;
+       Field *field;
+
+       guint l;
+       guint strpart;
+
+       gchar *field_name;
+       gchar *field_value;
+
+       gchar *sql;
+       GString *sql_where;
+       GdaDataModel *dm;
+
+       const gchar *ret;
+
+       ZakAuditPrivate *priv = ZAK_AUDIT_GET_PRIVATE (zak_audit);
+
+       datasource = get_datasource_from_name (zak_audit, datasource_name);
+       if (datasource == NULL)
+               {
+                       return NULL;
+               }
+
+       table = get_table_from_name (zak_audit, datasource, table_name);
+       if (table == NULL)
+               {
+                       return NULL;
+               }
+
+       sql_where = g_string_new ("");
+
+       l = g_strv_length ((gchar **)key_values);
+       for (strpart = 0; strpart < l; strpart++)
+               {
+                       field_name = (gchar *)key_values[strpart];
+                       if (field_name == NULL) break;
+
+                       field = get_field_from_name (zak_audit, table, field_name);
+
+                       if (strpart + 1 >= l) break;
+
+                       field_value = (gchar *)key_values[++strpart];
+                       if (field_value == NULL) break;
+
+                       /* TODO the db field can be other type than string */
+                       g_string_append_printf (sql_where, " AND v.id_fields = %d"
+                                               " AND v.value = '%s'",
+                                               field->id,
+                                               field_value,
+                                               field_value);
+               }
+
+       sql = g_strdup_printf ("SELECT a.username"
+                              " FROM actions AS a"
+                              " INNER JOIN values AS v ON a.id = v.id_actions"
+                              " WHERE a.type = %d"
+                              " AND a.id_tables = %d"
+                              "%s",
+                              ZAK_AUDIT_ACTION_INSERT,
+                              table->id,
+                              sql_where->str);
+
+       dm = gdaex_query (priv->gdaex, sql);
+       g_free (sql);
+       if (dm != NULL && gda_data_model_get_n_rows (dm) > 0)
+               {
+                       ret = (const gchar *)gdaex_data_model_get_value_stringify_at (dm, 0, 0);
+               }
+       if (dm != NULL)
+               {
+                       g_object_unref (dm);
+               }
+
+       g_string_free (sql_where, TRUE);
+
+       return ret;
+}
+
 /**
  * zak_audit_destroy:
  * @zak_audit: an #ZakAudit object.
index 2680638f8abaf0a8a0636ffc79b9a062dd78d49b..9a6e9a472e196b43434bf4f720d7529e32a80d71 100644 (file)
@@ -93,6 +93,11 @@ GtkWidget *zak_audit_get_changes_widget (ZakAudit *zak_audit,
                                          const gchar *table_name,
                                          const gchar **key_values);
 
+const gchar *zak_audit_get_user_insertion (ZakAudit *zak_audit,
+                                           const gchar *datasource_name,
+                                           const gchar *table_name,
+                                           const gchar **key_values);
+
 void zak_audit_destroy (ZakAudit *zak_audit);
 
 
index 48e2fb824e76c1cf23ab3a8c09b8246ca2481cb3..4e31a2df3498259652ea5228dec011a11890fc38 100644 (file)
@@ -129,6 +129,9 @@ main (int argc, char *argv[])
        zak_audit_action_from_gdastatement (audit, ZAK_AUDIT_ACTION_DELETE, "I", "audit_test1", stmt);
        gda_connection_statement_execute_non_select (conn, stmt, NULL, NULL, NULL);
 
+       g_warning ("User insertion: %s",
+                  zak_audit_get_user_insertion (audit, "audit_test1", "test1", g_strsplit ("id|2", "|", -1)));
+
        zak_audit_destroy (audit);
 
        return 0;