From 47fefa87580d0b37421297cfd69ae3154d9d002e Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 4 May 2015 12:36:45 +0200 Subject: [PATCH] Added function ZakAudit::action_from_where (for now only insert into). --- src/audit.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++- src/libzakaudit.h | 9 ++++- tests/test1.c | 3 ++ 3 files changed, 107 insertions(+), 3 deletions(-) diff --git a/src/audit.c b/src/audit.c index f37497e..4c197c4 100644 --- a/src/audit.c +++ b/src/audit.c @@ -26,6 +26,8 @@ #include +#include + #include "libzakaudit.h" enum @@ -859,7 +861,7 @@ zak_audit_action_v (ZakAudit *zak_audit, gchar *value_new; gint l; gint strpart; - gint id = 0; + gint id; gint cols; gint col; GDateTime *gdt; @@ -875,6 +877,7 @@ zak_audit_action_v (ZakAudit *zak_audit, } /* find the new action's id */ + id = 0; dm = gdaex_query (priv->gdaex, "SELECT COALESCE (MAX (id), 0) FROM actions"); if (dm != NULL && gda_data_model_get_n_rows (dm) == 1) @@ -1134,6 +1137,97 @@ zak_audit_action_v (ZakAudit *zak_audit, return TRUE; } +/** + * zak_audit_action_from_where: + * @zak_audit: an #ZakAudit object. + * @action: the action's type. + * @username: the username that make the action. + * @datasource_name: the datasource's name. + * @where: the where part of the sql statemnt. + * + * Returns: #TRUE on success. + */ +gboolean +zak_audit_action_from_where (ZakAudit *zak_audit, + enum ZakAuditActions action, + const gchar *username, + const gchar *datasource_name, + const gchar *table_name, + const gchar *where) +{ + gboolean ret; + + Datasource *datasource; + GdaConnection *conn; + GdaSqlParser *parser; + GdaStatement *stmt; + + GdaSqlStatement *sql_stmt; + GdaSqlStatementSelect *stmt_select; + + gchar *tmp; + gchar **values; + + ZakAuditPrivate *priv = ZAK_AUDIT_GET_PRIVATE (zak_audit); + + ret = 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) + { + g_warning ("Parser NULL"); + return FALSE; + } + + tmp = g_strdup_printf ("SELECT * FROM %s WHERE %s", + table_name, + where); + stmt = gda_sql_parser_parse_string (parser, tmp, NULL, NULL); + g_free (tmp); + if (stmt == NULL) + { + g_warning ("GdaStatement NULL"); + return FALSE; + } + + g_object_get (stmt, "structure", &sql_stmt, NULL); + if (sql_stmt == NULL) + { + g_warning ("Error on getting GdaSqlStatement."); + return FALSE; + } + stmt_select = (GdaSqlStatementSelect *)sql_stmt->contents; + + tmp = g_strdup (""); + parse_cond (stmt_select->where_cond, &tmp); + + if (g_strcmp0 (tmp, "") != 0) + { + values = g_strsplit (tmp, "|", -1); + ret = zak_audit_action_v (zak_audit, action, username, datasource_name, table_name, (const gchar **)values); + g_strfreev (values); + } + g_free (tmp); + + break; + } + + return ret; +} + /** * zak_audit_action_from_gdastatement: * @zak_audit: an #ZakAudit object. @@ -1187,7 +1281,7 @@ zak_audit_action_from_gdastatement (ZakAudit *zak_audit, /* TODO reduntant */ fields = sql->fields_list; - + if (sql->values_list == NULL) { /* may be it is an INSERT with SELECT */ diff --git a/src/libzakaudit.h b/src/libzakaudit.h index 9a6e9a4..0615150 100644 --- a/src/libzakaudit.h +++ b/src/libzakaudit.h @@ -1,7 +1,7 @@ /* * libzakaudit.h * - * Copyright (C) 2005-2013 Andrea Zagli + * Copyright (C) 2005-2015 Andrea Zagli * * This file is part of libzak_audit. * @@ -82,6 +82,13 @@ gboolean zak_audit_action_v (ZakAudit *zak_audit, const gchar *table_name, const gchar **fields_values); +gboolean zak_audit_action_from_where (ZakAudit *zak_audit, + enum ZakAuditActions action, + const gchar *username, + const gchar *datasource_name, + const gchar *table_name, + const gchar *where); + gboolean zak_audit_action_from_gdastatement (ZakAudit *zak_audit, enum ZakAuditActions action, const gchar *username, diff --git a/tests/test1.c b/tests/test1.c index 445b8e0..55e0f05 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -139,6 +139,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); + 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"); + g_warning ("User insertion: %s", zak_audit_get_user_insertion (audit, "audit_test1", "test1", (const gchar **)g_strsplit ("id|2", "|", -1))); -- 2.49.0