From 783c392696ebf11d86ba7f755cbbd5e356fc15cf Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Tue, 5 May 2015 10:28:22 +0200 Subject: [PATCH] Function ZakAudit::action_from_where (for update). --- src/audit.c | 115 ++++++++++++++++++++++++++++++++++++++++++++------ tests/test1.c | 4 ++ 2 files changed, 107 insertions(+), 12 deletions(-) diff --git a/src/audit.c b/src/audit.c index 731dc43..b18bf49 100644 --- a/src/audit.c +++ b/src/audit.c @@ -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; diff --git a/tests/test1.c b/tests/test1.c index 669bdfa..207092b 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -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)"); -- 2.49.0