From: Andrea Zagli Date: Tue, 15 Jun 2010 06:46:40 +0000 (+0200) Subject: Inverted INSERT and DELETE actions operations. X-Git-Tag: 0.5.0~10 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=463689deecf6b0b9767836cff9a64a5c1a5ec760;p=zakaudit%2Flibzakaudit Inverted INSERT and DELETE actions operations. --- diff --git a/.gitignore b/.gitignore index 3ff7f11..90465cc 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,5 @@ docs/reference/xml *.la *.bak test1 +*.exe libaudit*tar* diff --git a/src/audit.c b/src/audit.c index fee5ae9..ff7bd53 100644 --- a/src/audit.c +++ b/src/audit.c @@ -694,11 +694,8 @@ audit_action_v (Audit *audit, switch (action) { case AUDIT_ACTION_INSERT: - /* saving all fields */ - sql = g_strdup_printf ("SELECT * FROM %s", - table_name); - - sql_where = g_strdup (" WHERE TRUE"); + /* TODO for now it saves all the fields passed */ + /* saving just key's fields */ for (strpart = 0; strpart < l; strpart++) { field_name = (gchar *)fields_values[strpart]; @@ -709,24 +706,7 @@ audit_action_v (Audit *audit, 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); - } - sql = g_strconcat (sql, sql_where, NULL); - dm = gdaex_query (datasource->gdaex, sql); - - if (dm == NULL) return FALSE; - - if (gda_data_model_get_n_rows (dm) == 0 || - gda_data_model_get_n_rows (dm) > 1) return FALSE; - - cols = gda_data_model_get_n_columns (dm); - for (col = 0; col < cols; col++) - { - /* find the field */ - field_name = (gchar *)gda_data_model_get_column_title (dm, col); - insert_value (audit, id, table, field_name, - gdaex_data_model_get_value_stringify_at (dm, 0, col)); + insert_value (audit, id, table, field_name, value); } break; @@ -810,7 +790,11 @@ audit_action_v (Audit *audit, break; case AUDIT_ACTION_DELETE: - /* saving just key's fields */ + /* saving all fields */ + sql = g_strdup_printf ("SELECT * FROM %s", + table_name); + + sql_where = g_strdup (" WHERE TRUE"); for (strpart = 0; strpart < l; strpart++) { field_name = (gchar *)fields_values[strpart]; @@ -821,7 +805,24 @@ audit_action_v (Audit *audit, value = (gchar *)fields_values[++strpart]; if (value == NULL) break; - insert_value (audit, id, table, field_name, value); + /* TODO the db field can be other type than string */ + sql_where = g_strconcat (sql_where, " AND ", field_name, " = ", value, "", NULL); + } + sql = g_strconcat (sql, sql_where, NULL); + dm = gdaex_query (datasource->gdaex, sql); + + if (dm == NULL) return FALSE; + + if (gda_data_model_get_n_rows (dm) == 0 || + gda_data_model_get_n_rows (dm) > 1) return FALSE; + + cols = gda_data_model_get_n_columns (dm); + for (col = 0; col < cols; col++) + { + /* find the field */ + field_name = (gchar *)gda_data_model_get_column_title (dm, col); + insert_value (audit, id, table, field_name, + gdaex_data_model_get_value_stringify_at (dm, 0, col)); } break; diff --git a/tests/test1.c b/tests/test1.c index 09d8d8e..b5a3505 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -71,7 +71,7 @@ main (int argc, char *argv[]) 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); + stmt = gda_sql_parser_parse_string (parser, "UPDATE test1 SET income=99999999.99 WHERE id = 2", NULL, NULL); if (stmt == NULL) { g_warning ("GdaStatement NULL"); diff --git a/tests/test1.sql b/tests/test1.sql new file mode 100644 index 0000000..dd8a2c6 --- /dev/null +++ b/tests/test1.sql @@ -0,0 +1,8 @@ +CREATE TABLE test1 +( + id integer NOT NULL, + "name" character varying(100), + age integer, + income double precision, + CONSTRAINT test1_pkey PRIMARY KEY (id) +); \ No newline at end of file