From: Andrea Zagli Date: Sat, 5 Jun 2010 09:49:53 +0000 (+0200) Subject: Added function Audit::audit_action_v. X-Git-Tag: 0.5.0~14 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=402d97ea1c024d82d33d2c112ca8597ba735f128;p=zakaudit%2Flibzakaudit Added function Audit::audit_action_v. --- diff --git a/src/audit.c b/src/audit.c index ca35600..f195a3a 100644 --- a/src/audit.c +++ b/src/audit.c @@ -452,8 +452,9 @@ Audit * @audit: l'oggetto #Audit su cui effettuare l'azione. * @action: il tipo di azione. * @dn: lo username che effettua l'azione. - * @datasource: - * @tabella: + * @datasource_name: the datasource's name. + * @table_name: the table's name. + * @...: a #NULL terminated list of couples field-value. * * Returns: #TRUE se l'azione viene effettuata con successo. */ @@ -464,6 +465,60 @@ audit_action (Audit *audit, const gchar *datasource_name, const gchar *table_name, ...) +{ + va_list vargs; + gchar *str; + gchar **fields_values; + gchar *field_name; + gchar *field_value; + + gboolean ret; + + ret = FALSE; + str = g_strdup (""); + + va_start (vargs, table_name); + + field_name = va_arg (vargs, gchar *); + while (field_name != NULL) + { + field_value = va_arg (vargs, gchar *); + if (field_value == NULL) break; + + str = g_strconcat (str, (g_strcmp0 (str, "") != 0 ? "|" : ""), field_name, "|", field_value, NULL); + + field_name = va_arg (vargs, gchar *); + } + + va_end (vargs); + + 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_action_v: + * @audit: l'oggetto #Audit su cui effettuare l'azione. + * @action: the action's type. + * @dn: lo username che effettua l'azione. + * @datasource_name: the datasource's name. + * @table_name: the table's name. + * @fields_values: a #NULL-terminated array of strings in the form + * "...#NULL" + * + * Returns: #TRUE se l'azione viene effettuata con successo. + */ +gboolean +audit_action_v (Audit *audit, + enum AuditActions action, + const gchar *dn, + const gchar *datasource_name, + const gchar *table_name, + const gchar **fields_values) { GdaDataModel *dm; gchar *sql; @@ -471,12 +526,13 @@ audit_action (Audit *audit, gchar *field_name; gchar *value; gchar *value_new; + gint l; + gint strpart; gint id = 0; gint cols; gint col; time_t t; struct tm *tm_ora; - va_list vargs; Datasource *datasource; Table *table; @@ -533,9 +589,9 @@ audit_action (Audit *audit, gdaex_execute (priv->gdaex, sql); } - /* fields */ - va_start (vargs, table_name); + l = g_strv_length ((gchar **)fields_values); + /* fields */ switch (action) { case AUDIT_ACTION_INSERT: @@ -544,15 +600,18 @@ audit_action (Audit *audit, table_name); sql_where = g_strdup (" WHERE TRUE"); - field_name = va_arg (vargs, gchar *); - while (field_name != NULL) + for (strpart = 0; strpart < l; strpart++) { - value = va_arg (vargs, gchar *); + field_name = (gchar *)fields_values[strpart]; + if (field_name == NULL) break; + + if (strpart + 1 >= l) break; + + value = (gchar *)fields_values[++strpart]; if (value == NULL) break; /* TODO the db field can be other type than string */ sql_where = g_strconcat (sql_where, " AND ", field_name, " = ", value, "", NULL); - field_name = va_arg (vargs, gchar *); } sql = g_strconcat (sql, sql_where, NULL); dm = gdaex_query (datasource->gdaex, sql); @@ -578,15 +637,18 @@ audit_action (Audit *audit, table_name); sql_where = g_strdup (" WHERE TRUE"); - field_name = va_arg (vargs, gchar *); - while (field_name != NULL) + for (strpart = 0; strpart < l; strpart++) { - value = va_arg (vargs, gchar *); + field_name = (gchar *)fields_values[strpart]; + if (field_name == NULL) break; + + if (strpart + 1 >= l) break; + + value = (gchar *)fields_values[++strpart]; if (value == NULL) break; /* TODO the db field can be other type than string */ sql_where = g_strconcat (sql_where, " AND ", field_name, " = ", value, "", NULL); - field_name = va_arg (vargs, gchar *); } sql = g_strconcat (sql, sql_where, NULL); dm = gdaex_query (datasource->gdaex, sql); @@ -612,15 +674,18 @@ audit_action (Audit *audit, table_name); sql_where = g_strdup (" WHERE TRUE"); - field_name = va_arg (vargs, gchar *); - while (field_name != NULL) + for (strpart = 0; strpart < l; strpart++) { - value = va_arg (vargs, gchar *); + field_name = (gchar *)fields_values[strpart]; + if (field_name == NULL) break; + + if (strpart + 1 >= l) break; + + value = (gchar *)fields_values[++strpart]; if (value == NULL) break; /* TODO the db field can be other type than string */ sql_where = g_strconcat (sql_where, " AND ", field_name, " = ", value, "", NULL); - field_name = va_arg (vargs, gchar *); } sql = g_strconcat (sql, sql_where, NULL); dm = gdaex_query (datasource->gdaex, sql); @@ -647,15 +712,17 @@ audit_action (Audit *audit, case AUDIT_ACTION_DELETE: /* salvo solo i fields della chiave */ - field_name = va_arg (vargs, gchar *); - while (field_name != NULL) + for (strpart = 0; strpart < l; strpart++) { - value = va_arg (vargs, gchar *); + field_name = (gchar *)fields_values[strpart]; + if (field_name == NULL) break; + + if (strpart + 1 >= l) break; + + value = (gchar *)fields_values[++strpart]; if (value == NULL) break; insert_value (audit, id, table, field_name, value); - - field_name = va_arg (vargs, gchar *); } break; @@ -663,8 +730,6 @@ audit_action (Audit *audit, return FALSE; }; - va_end (vargs); - return TRUE; } diff --git a/src/libaudit.h b/src/libaudit.h index 9817b9c..0b71bc1 100644 --- a/src/libaudit.h +++ b/src/libaudit.h @@ -73,6 +73,13 @@ gboolean audit_action (Audit *audit, const gchar *table_name, ...); +gboolean audit_action_v (Audit *audit, + enum AuditActions action, + const gchar *dn, + const gchar *datasource_name, + const gchar *table_name, + const gchar **fields_values); + void audit_destroy (Audit *audit);