]> saetta.ns0.it Git - zakaudit/libzakaudit/commitdiff
Added function ZakAudit::action_from_where (for now only insert into).
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Mon, 4 May 2015 10:36:45 +0000 (12:36 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Mon, 4 May 2015 10:36:45 +0000 (12:36 +0200)
src/audit.c
src/libzakaudit.h
tests/test1.c

index f37497e392d26e69e70f415852ddfff53db5e024..4c197c48cd70d27bf7e9c07c9652a44953af02ef 100644 (file)
@@ -26,6 +26,8 @@
 
 #include <string.h>
 
+#include <libgda/sql-parser/gda-sql-parser.h>
+
 #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 */
index 9a6e9a472e196b43434bf4f720d7529e32a80d71..061515043939be43425fd7d08ed44cab38517c48 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * libzakaudit.h
  *
- * Copyright (C) 2005-2013 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2015 Andrea Zagli <azagli@libero.it>
  *
  *  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,
index 445b8e06ca861e2c84a3c5d4e7ba0af6b7cde8c9..55e0f05aff986a0fde653ce24d10e1db206252a0 100644 (file)
@@ -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)));