]> saetta.ns0.it Git - zakaudit/libzakaudit/commitdiff
Function ZakAudit::action_from_where (for update).
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Tue, 5 May 2015 08:28:22 +0000 (10:28 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Tue, 5 May 2015 08:28:22 +0000 (10:28 +0200)
src/audit.c
tests/test1.c

index 731dc437c67cd1191a415dc3036ab5d155d47f4d..b18bf49d2ce80ef4205fe40655f600e1303854de 100644 (file)
@@ -73,6 +73,12 @@ typedef struct
        GList *tables;
 } Datasource;
 
+typedef struct
+{
+       gchar **values;
+       GHashTable *fields_updated;
+} RecordUpdated;
+
 static void zak_audit_class_init (ZakAuditClass *klass);
 static void zak_audit_init (ZakAudit *zak_audit);
 
@@ -116,6 +122,7 @@ struct _ZakAuditPrivate
        GdaEx *gdaex;
        GList *datasources;
 
+       GSList *records_updated;
        GHashTable *fields_updated;
 
        gchar *guidir;
@@ -143,6 +150,7 @@ zak_audit_init (ZakAudit *zak_audit)
        ZakAuditPrivate *priv = ZAK_AUDIT_GET_PRIVATE (zak_audit);
 
        priv->datasources = NULL;
+       priv->records_updated = NULL;
        priv->fields_updated = NULL;
 }
 
@@ -1180,15 +1188,24 @@ zak_audit_action_from_where (ZakAudit *zak_audit,
 
        ret = FALSE;
 
-       if (action == ZAK_AUDIT_ACTION_INSERT
+       /* find the datasource */
+       datasource = get_datasource_from_name (zak_audit, datasource_name);
+       if (datasource == NULL)
+               {
+                       g_warning ("Unable to find the datasource «%s» on loaded datasources.",
+                                  datasource_name);
+                       return FALSE;
+               }
+
+       if (action == ZAK_AUDIT_ACTION_BEFORE_UPDATE
            || action == ZAK_AUDIT_ACTION_DELETE)
                {
-                       /* find the datasource */
-                       datasource = get_datasource_from_name (zak_audit, datasource_name);
-                       if (datasource == NULL)
+                       /* find the table */
+                       table = get_table_from_name (zak_audit, datasource, table_name);
+                       if (table == NULL)
                                {
-                                       g_warning ("Unable to find the datasource «%s» on loaded datasources.",
-                                                  datasource_name);
+                                       g_warning ("Unable to find the table «%s» on loaded tables.",
+                                                  table_name);
                                        return FALSE;
                                }
                }
@@ -1236,16 +1253,89 @@ zak_audit_action_from_where (ZakAudit *zak_audit,
 
                                break;
 
-                       case ZAK_AUDIT_ACTION_DELETE:
-                               /* find the table */
-                               table = get_table_from_name (zak_audit, datasource, table_name);
-                               if (table == NULL)
+                       case ZAK_AUDIT_ACTION_BEFORE_UPDATE:
+                               if (priv->records_updated != NULL)
                                        {
-                                               g_warning ("Unable to find the table «%s» on loaded tables.",
-                                                          table_name);
+                                               g_slist_free (priv->records_updated);
+                                               priv->records_updated = NULL;
+                                       }
+
+                               /* for each record */
+                               tmp = g_strdup_printf ("SELECT %s FROM %s WHERE %s",
+                                                      table->keys_sql,
+                                                      table_name,
+                                                      where);
+                               dm = gdaex_query (datasource->gdaex, tmp);
+                               g_free (tmp);
+                               if (dm != NULL)
+                                       {
+                                               dmiter = gda_data_model_create_iter (dm);
+                                               while (gda_data_model_iter_move_next (dmiter))
+                                                       {
+                                                               str = g_string_new ("");
+
+                                                               cols = gda_data_model_get_n_columns (dm);
+                                                               for (col = 0; col < cols; col++)
+                                                                       {
+                                                                               g_string_append (str, (g_strcmp0 (str->str, "") != 0 ? "|" : ""));
+                                                                               g_string_append (str, gda_data_model_get_column_title (dm, col));
+                                                                               g_string_append (str, "|");
+                                                                               g_string_append (str, gdaex_data_model_iter_get_value_stringify_at (dmiter, col));
+                                                                       }
+
+                                                               if (g_strcmp0 (str->str, "") != 0)
+                                                                       {
+                                                                               RecordUpdated *record_updated;
+
+                                                                               record_updated = g_new0 (RecordUpdated, 1);
+                                                                               record_updated->values = g_strsplit (str->str, "|", -1);
+                                                                               ret = zak_audit_action_v (zak_audit, action, username, datasource_name, table_name, (const gchar **)record_updated->values);
+
+                                                                               if (priv->fields_updated != NULL)
+                                                                                       {
+                                                                                               record_updated->fields_updated = g_hash_table_ref (priv->fields_updated);
+                                                                                               priv->fields_updated = NULL;
+                                                                                               priv->records_updated = g_slist_append (priv->records_updated, record_updated);
+                                                                                       }
+                                                                               else
+                                                                                       {
+                                                                                               g_strfreev (record_updated->values);
+                                                                                               g_free (record_updated);
+                                                                                       }
+                                                                       }
+
+                                                               g_string_free (str, TRUE);
+                                                       }
+                                               g_object_unref (dmiter);
+                                               g_object_unref (dm);
+                                       }
+                               break;
+
+                       case ZAK_AUDIT_ACTION_AFTER_UPDATE:
+                               if (priv->records_updated == NULL)
+                                       {
+                                               g_warning ("You must call before an action of type ZAK_AUDIT_ACTION_AFTER_UPDATE.");
                                                return FALSE;
                                        }
 
+                               while (priv->records_updated != NULL)
+                                       {
+                                               RecordUpdated *record_updated;
+
+                                               record_updated = (RecordUpdated *)priv->records_updated->data;
+                                               priv->fields_updated = record_updated->fields_updated;
+                                               ret = zak_audit_action_v (zak_audit, action, username, datasource_name, table_name, (const gchar **)record_updated->values);
+
+                                               g_strfreev (record_updated->values);
+
+                                               priv->records_updated = g_slist_next (priv->records_updated);
+                                       }
+
+                               g_slist_free (priv->records_updated);
+                               priv->records_updated = NULL;
+                               break;
+
+                       case ZAK_AUDIT_ACTION_DELETE:
                                /* for each record */
                                tmp = g_strdup_printf ("SELECT %s FROM %s WHERE %s",
                                                       table->keys_sql,
@@ -1278,6 +1368,7 @@ zak_audit_action_from_where (ZakAudit *zak_audit,
 
                                                                g_string_free (str, TRUE);
                                                        }
+                                               g_object_unref (dmiter);
                                                g_object_unref (dm);
                                        }
                                break;
index 669bdfa2dd386f42759129113b4609e224d453f3..207092b9e578467cb226f472df680bdee0380465 100644 (file)
@@ -145,6 +145,10 @@ main (int argc, char *argv[])
        gdaex_execute (gdaex, "INSERT INTO test1 VALUES (4, 'John Doe', 54, 0.88)");
        zak_audit_action_from_where (audit, ZAK_AUDIT_ACTION_INSERT, "I", "audit_test1", "test1", "id = 4");
 
+       zak_audit_action_from_where (audit, ZAK_AUDIT_ACTION_BEFORE_UPDATE, "I", "audit_test1", "test1", "id IN (3, 4)");
+       gdaex_execute (gdaex, "UPDATE test1 SET age=99 WHERE id IN (3, 4)");
+       zak_audit_action_from_where (audit, ZAK_AUDIT_ACTION_AFTER_UPDATE, "I", "audit_test1", "test1", "id IN (3, 4)");
+
        zak_audit_action_from_where (audit, ZAK_AUDIT_ACTION_DELETE, "I", "audit_test1", "test1", "id IN (3, 4)");
        gdaex_execute (gdaex, "DELETE FROM test1 WHERE id IN (3, 4)");