From e81fdd526fd830ca370c3016c6479706c683c63c Mon Sep 17 00:00:00 2001
From: Andrea Zagli <azagli@libero.it>
Date: Sat, 5 Jun 2010 18:02:02 +0200
Subject: [PATCH] Added function Audit::action_from_gdastatement.

---
 src/audit.c    | 139 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/libaudit.h |   6 +++
 tests/test1.c  |  64 +++++++++++++++++++++++
 3 files changed, 209 insertions(+)

diff --git a/src/audit.c b/src/audit.c
index f195a3a..23d95e0 100644
--- a/src/audit.c
+++ b/src/audit.c
@@ -733,6 +733,145 @@ audit_action_v (Audit *audit,
 	return TRUE;
 }
 
+static void
+parse_cond (GdaSqlExpr *cond, gchar **str)
+{
+	if (cond->cond->operator_type == GDA_SQL_OPERATOR_TYPE_EQ)
+		{
+			GdaSqlExpr *op1 = (GdaSqlExpr *)cond->cond->operands->data;
+			GdaSqlExpr *op2 = (GdaSqlExpr *)cond->cond->operands->next->data;
+
+			*str = g_strconcat (*str, (g_strcmp0 (*str, "") != 0 ? "|" : ""),
+							    gda_value_stringify (op1->value),
+							    "|",
+							    gda_value_stringify (op2->value),
+							    NULL);
+		}
+	else
+		{
+			GSList *ops;
+
+			ops = cond->cond->operands;
+			while (ops != NULL)
+				{
+					parse_cond ((GdaSqlExpr *)ops->data, str);
+					ops = g_slist_next (ops);
+				}
+		}
+}
+
+/**
+ * audit_action_from_gdastatement:
+ * @audit: l'oggetto #Audit su cui effettuare l'azione.
+ * @action: il tipo di azione.
+ * @dn: lo username che effettua l'azione.
+ * @datasource_name: the datasource's name.
+ * @gda_statement:
+ *
+ * Returns: #TRUE se l'azione viene effettuata con successo.
+ */
+gboolean
+audit_action_from_gdastatement (Audit *audit,
+              enum AuditActions action,
+              const gchar *dn,
+              const gchar *datasource_name,
+              GdaStatement *gda_statement)
+{
+	gboolean ret;
+
+	GdaSqlStatement *sql_stmt;
+
+	gchar *str;
+	gchar *table_name;
+
+	ret = FALSE;
+	str = g_strdup ("");
+
+	g_object_get (gda_statement, "structure", &sql_stmt, NULL);
+	if (sql_stmt == NULL)
+		{
+			g_warning ("Error on getting GdaSqlStatement.");
+			return ret;
+		}
+
+	switch (sql_stmt->stmt_type)
+		{
+			case GDA_SQL_STATEMENT_INSERT:
+				{
+					GdaSqlStatementInsert *sql;
+					GSList *fields;
+					GSList *values;
+
+					gchar *field_name;
+					gchar *field_value;
+
+					sql = (GdaSqlStatementInsert *)sql_stmt->contents;
+
+					table_name = sql->table->table_name;
+
+					/* TODO reduntant */
+					fields = sql->fields_list;
+					values = ((GSList *)sql->values_list)->data;
+
+					while (fields != NULL && values != NULL)
+						{
+							field_name = ((GdaSqlField *)fields->data)->field_name;
+							field_value = (gchar *)gda_value_stringify (((GdaSqlExpr *)values->data)->value);
+
+							str = g_strconcat (str, (g_strcmp0 (str, "") != 0 ? "|" : ""),
+							                   field_name,
+							                   "|",
+							                   field_value,
+							                   NULL);
+
+							fields = g_slist_next (fields);
+							values = g_slist_next (values);
+						}
+
+					break;
+				}
+
+			case GDA_SQL_STATEMENT_UPDATE:
+				{
+					GdaSqlStatementUpdate *sql;
+
+					sql = (GdaSqlStatementUpdate *)sql_stmt->contents;
+
+					table_name = sql->table->table_name;
+
+					str = g_strdup ("");
+					parse_cond (sql->cond, &str);
+
+					break;
+				}
+
+			case GDA_SQL_STATEMENT_DELETE:
+				{
+					GdaSqlStatementDelete *sql;
+
+					sql = (GdaSqlStatementDelete *)sql_stmt->contents;
+
+					table_name = sql->table->table_name;
+
+					str = g_strdup ("");
+					parse_cond (sql->cond, &str);
+
+					break;
+				}
+
+			default:
+				g_warning ("GdaSqlStatementType %d not supported.", sql_stmt->stmt_type);
+				return ret;
+		}
+
+	if (g_strcmp0 (str, "") != 0)
+		{
+			ret = audit_action_v (audit, action, dn, datasource_name, table_name, (const gchar **)g_strsplit (str, "|", -1));
+		}
+
+	return ret;
+}
+
 /**
  * audit_destroy:
  *
diff --git a/src/libaudit.h b/src/libaudit.h
index 0b71bc1..3633949 100644
--- a/src/libaudit.h
+++ b/src/libaudit.h
@@ -80,6 +80,12 @@ gboolean audit_action_v (Audit *audit,
                        const gchar *table_name,
                        const gchar **fields_values);
 
+gboolean audit_action_from_gdastatement (Audit *audit,
+                       enum AuditActions action,
+                       const gchar *dn,
+                       const gchar *datasource_name,
+                       GdaStatement *gda_statement);
+
 void audit_destroy (Audit *audit);
 
 
diff --git a/tests/test1.c b/tests/test1.c
index aaaf273..09d8d8e 100644
--- a/tests/test1.c
+++ b/tests/test1.c
@@ -1,3 +1,27 @@
+/*
+ * test1.c
+ *
+ * Copyright (C) 2005-2010 Andrea Zagli <azagli@libero.it>
+ *
+ *  This file is part of libaudit.
+ *  
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <libgda/sql-parser/gda-sql-parser.h>
+
 #include <libaudit.h>
 
 int
@@ -6,6 +30,10 @@ main (int argc, char *argv[])
 	GdaEx *gdaex;
 	Audit *audit;
 
+	GdaConnection *conn;
+	GdaSqlParser *parser;
+	GdaStatement *stmt;
+
 	g_type_init ();
 
 	audit = audit_new_from_string ("PostgreSQL://postgres:postgres@HOST=localhost;DB_NAME=audit");
@@ -17,6 +45,13 @@ main (int argc, char *argv[])
 		}
 
 	gdaex = gdaex_new_from_string ("PostgreSQL://postgres:postgres@HOST=localhost;DB_NAME=audit");
+	conn = (GdaConnection *)gdaex_get_gdaconnection (gdaex);
+	parser = gda_connection_create_parser (conn);
+	if (parser == NULL)
+		{
+			g_warning ("Parser NULL");
+			return 0;
+		}
 
 	gdaex_execute (gdaex, "DELETE FROM test1");
 
@@ -27,6 +62,35 @@ main (int argc, char *argv[])
 	gdaex_execute (gdaex, "UPDATE test1 SET age=30 WHERE id = 1");
 	audit_action (audit, AUDIT_ACTION_AFTER_UPDATE, "I", "audit_test1", "test1", "id", "1", NULL);
 
+	stmt = gda_sql_parser_parse_string (parser, "INSERT INTO test1 (id, name, age, income) VALUES (2, 'Paul Green', 105, 35.33)", NULL, NULL);
+	if (stmt == NULL)
+		{
+			g_warning ("GdaStatement NULL");
+			return 0;
+		}
+	gda_connection_statement_execute_non_select (conn, stmt, NULL, NULL, NULL);
+	audit_action_from_gdastatement (audit, AUDIT_ACTION_INSERT, "I", "audit_test1", stmt);
+
+	stmt = gda_sql_parser_parse_string (parser, "UPDATE test1 SET age=130 WHERE id = 2", NULL, NULL);
+	if (stmt == NULL)
+		{
+			g_warning ("GdaStatement NULL");
+			return 0;
+		}
+	audit_action_from_gdastatement (audit, AUDIT_ACTION_BEFORE_UPDATE, "I", "audit_test1", stmt);
+	gda_connection_statement_execute_non_select (conn, stmt, NULL, NULL, NULL);
+	audit_action_from_gdastatement (audit, AUDIT_ACTION_AFTER_UPDATE, "I", "audit_test1", stmt);
+
+
+	stmt = gda_sql_parser_parse_string (parser, "DELETE FROM test1 WHERE id = 2", NULL, NULL);
+	if (stmt == NULL)
+		{
+			g_warning ("GdaStatement NULL");
+			return 0;
+		}
+	audit_action_from_gdastatement (audit, AUDIT_ACTION_DELETE, "I", "audit_test1", stmt);
+	gda_connection_statement_execute_non_select (conn, stmt, NULL, NULL, NULL);
+
 	audit_destroy (audit);
 
 	return 0;
-- 
2.49.0