From 92d41ac496d50e75829ce84e01e38cdececa30ba Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 4 May 2015 13:12:06 +0200 Subject: [PATCH] Function ZakAudit::action_from_where (for delete). --- src/audit.c | 78 ++++++++++++++++++++++++++++++++++++++++++++------- tests/test1.c | 6 ++++ 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/src/audit.c b/src/audit.c index 4c197c4..731dc43 100644 --- a/src/audit.c +++ b/src/audit.c @@ -893,7 +893,7 @@ zak_audit_action_v (ZakAudit *zak_audit, /* find action's timestamp */ gdt = g_date_time_new_now_local (); - /* trovo il datasource */ + /* find the datasource */ datasource = get_datasource_from_name (zak_audit, datasource_name); if (datasource == NULL) { @@ -1158,32 +1158,44 @@ zak_audit_action_from_where (ZakAudit *zak_audit, gboolean ret; Datasource *datasource; + Table *table; + GdaConnection *conn; GdaSqlParser *parser; GdaStatement *stmt; + GdaDataModel *dm; + GdaDataModelIter *dmiter; GdaSqlStatement *sql_stmt; GdaSqlStatementSelect *stmt_select; + GString *str; gchar *tmp; gchar **values; + guint cols; + guint col; + ZakAuditPrivate *priv = ZAK_AUDIT_GET_PRIVATE (zak_audit); ret = FALSE; + if (action == ZAK_AUDIT_ACTION_INSERT + || action == ZAK_AUDIT_ACTION_DELETE) + { + /* 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; + } + } + switch (action) { case ZAK_AUDIT_ACTION_INSERT: - /* trovo il 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; - } - conn = (GdaConnection *)gdaex_get_gdaconnection (datasource->gdaex); parser = gda_connection_create_parser (conn); if (parser == NULL) @@ -1223,6 +1235,52 @@ zak_audit_action_from_where (ZakAudit *zak_audit, g_free (tmp); break; + + case ZAK_AUDIT_ACTION_DELETE: + /* find the table */ + table = get_table_from_name (zak_audit, datasource, table_name); + if (table == NULL) + { + g_warning ("Unable to find the table «%s» on loaded tables.", + table_name); + return FALSE; + } + + /* 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) + { + values = g_strsplit (str->str, "|", -1); + ret = zak_audit_action_v (zak_audit, action, username, datasource_name, table_name, (const gchar **)values); + g_strfreev (values); + } + + g_string_free (str, TRUE); + } + g_object_unref (dm); + } + break; } return ret; diff --git a/tests/test1.c b/tests/test1.c index 55e0f05..669bdfa 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -142,6 +142,12 @@ main (int argc, char *argv[]) gdaex_execute (gdaex, "INSERT INTO test1 VALUES (3, 'Joe Black', 33, 2250.13)"); zak_audit_action_from_where (audit, ZAK_AUDIT_ACTION_INSERT, "I", "audit_test1", "test1", "id = 3"); + 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_DELETE, "I", "audit_test1", "test1", "id IN (3, 4)"); + gdaex_execute (gdaex, "DELETE FROM test1 WHERE id IN (3, 4)"); + g_warning ("User insertion: %s", zak_audit_get_user_insertion (audit, "audit_test1", "test1", (const gchar **)g_strsplit ("id|2", "|", -1))); -- 2.49.0