From bfee015c7dc017bed0feaea11318468a61a3a677 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 24 Nov 2013 16:45:30 +0000 Subject: [PATCH] Added functions ZakAudit::new_with_datasources and ZakAudit::new_from_string_with_datasources. closes #806. Added command line parameters to test1. --- .anjuta_sym_db.db | Bin 917504 -> 917504 bytes src/audit.c | 95 +++++++++++++++++++++++++++++++++++++++------- src/libzakaudit.h | 4 +- tests/test1.c | 43 +++++++++++++++++++-- 4 files changed, 125 insertions(+), 17 deletions(-) diff --git a/.anjuta_sym_db.db b/.anjuta_sym_db.db index 68380e9dea7a00077803a81742076ebe6d4ffc76..783ebc1281815b536b2c14cc73c1aebecd5d4074 100644 GIT binary patch delta 2963 zcmai0du&tJ8NcV=`}u_pNy!@mc>sYB5+FPhB1QwG%poPv)=6j~wi6pa6FYHY=f(LP zRV$_4gGi-k&@x(WpcTqQkXldOqzqbZLz>z@22(&{U1`g-iB$^F3PKP&=X#4&%p=nM za?W?Z^ZQ=Edp^hGRq=S$fjPu6GItYk3_Ct>kY8bz5%)`Y1FzvF{0z_H89a_};r`Ll zbAi!6oSMBP&8U~nXNu0Sa z7sb_8mFrfraa0QRYu2q=v2JiNO$kOytiTKUb`A(BDwy zn)K)UuKG^-g1(hr+1u$@;yLDc)kF6D*nQ58?nduz*Lhcs>t~*DyRFUJW_AuczOo;+ zE2E=VcUwG5(DVm~ zv@xp(w&%Ir(hMp2H=Ng$CJ`smGMevXbPvpXD#g{tMseJ47BO=a%Gh`>tWx`WB$e&+Lm3%MCh})LyhGj>>r6%i z&@8Lf$rlBk@Pa5^pRA-$YdS>P7BYJW)RC{*u@2ZnKAL`cmb@nlQp_L(o5>MTl_05N zU&Q!{&KM6v4nqSe6^b3~oiNnOavJ+G42|Sl*44*zZSI0KYSZrDnGnpEr_fJ!#i%_HefD*ipdyMPgxmza#e-wAt-LR9Ww&VIcYwz4(~i_`nx zmC~o{D;BI()Tn2O8)WQ-H&m`_8yT%5ZM@%!@hH5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.c @@ -63,7 +63,7 @@ static gboolean load_fields (ZakAudit *zak_audit, Table *table); static gboolean load_tables (ZakAudit *zak_audit, Datasource *datasource); -static gboolean load_datasources (ZakAudit *zak_audit); +static gboolean load_datasources (ZakAudit *zak_audit, va_list ap); static Field *get_field_from_name (ZakAudit *zak_audit, Table *table, const char *field); @@ -279,25 +279,57 @@ load_tables (ZakAudit *zak_audit, } static gboolean -load_datasources (ZakAudit *zak_audit) +load_datasources (ZakAudit *zak_audit, va_list ap) { GdaDataModel *dm; gchar *sql; - gint rows, row; + gint rows; + gint row; Datasource *datas; + GString *str_in; + char *datasource_name; + ZakAuditPrivate *priv = ZAK_AUDIT_GET_PRIVATE (zak_audit); + str_in = NULL; + + datasource_name = va_arg (ap, char *); + if (datasource_name != NULL) + { + str_in = g_string_new (""); + + while (datasource_name != NULL) + { + g_string_append_printf (str_in, ",'%s'", datasource_name); + + datasource_name = va_arg (ap, gchar *); + } + + g_string_erase (str_in, 0, 1); + g_string_prepend (str_in, " AND name IN ("); + g_string_append (str_in, ")"); + } + /* for each datasource on datasources table must be opened a connection */ - dm = gdaex_query (priv->gdaex, "SELECT id, name, cnc_string" - " FROM datasources" - " WHERE status <> 'D'"); + sql = g_strdup_printf ("SELECT id, name, cnc_string" + " FROM datasources" + " WHERE status <> 'D'" + "%s", + str_in == NULL ? "" : str_in->str); + dm = gdaex_query (priv->gdaex, sql); + g_free (sql); if (dm == NULL) { g_warning ("There are no datasources configured."); return FALSE; } + if (str_in != NULL) + { + g_string_free (str_in, TRUE); + } + rows = gda_data_model_get_n_rows (dm); if (rows < 1) { @@ -547,16 +579,18 @@ zak_audit_get_property (GObject *object, guint property_id, GValue *value, GPara } /* PUBLIC */ - /** - * zak_audit_new: + * zak_audit_new_with_datasources: * @gda_connection: a #GdaConnection object. + * @...: a #NULL terminated list of datasources to load. * * Returns: the newly #ZakAudit object. */ ZakAudit -*zak_audit_new (GdaConnection *gda_connection) +*zak_audit_new_with_datasources (GdaConnection *gda_connection, ...) { + va_list ap; + ZakAudit *zak_audit = zak_audit_new_ (); ZakAuditPrivate *priv = ZAK_AUDIT_GET_PRIVATE (zak_audit); @@ -570,26 +604,33 @@ ZakAudit return NULL; } + va_start (ap, gda_connection); + /* carico i datasource */ - if (!load_datasources (zak_audit)) + if (!load_datasources (zak_audit, ap)) { /* TODO */ g_warning ("Error loading datasources."); return NULL; } + va_end (ap); + return zak_audit; } /** - * zak_audit_new_from_string: + * zak_audit_new_from_string_with_datasources: * @cnc_string: the connection string to the datasource that contains library's tables. + * @...: a #NULL terminated list of datasources to load. * * Returns: the newly #ZakAudit object. */ ZakAudit -*zak_audit_new_from_string (const gchar *cnc_string) +*zak_audit_new_from_string_with_datasources (const gchar *cnc_string, ...) { + va_list ap; + ZakAudit *zak_audit = zak_audit_new_ (); ZakAuditPrivate *priv = ZAK_AUDIT_GET_PRIVATE (zak_audit); @@ -603,17 +644,45 @@ ZakAudit return NULL; } + va_start (ap, cnc_string); + /* carico i datasource */ - if (!load_datasources (zak_audit)) + if (!load_datasources (zak_audit, ap)) { /* TODO */ g_warning ("Error loading datasources."); return NULL; } + va_end (ap); + return zak_audit; } +/** + * zak_audit_new: + * @gda_connection: a #GdaConnection object. + * + * Returns: the newly #ZakAudit object. + */ +ZakAudit +*zak_audit_new (GdaConnection *gda_connection) +{ + return zak_audit_new_with_datasources (gda_connection, NULL); +} + +/** + * zak_audit_new_from_string: + * @cnc_string: the connection string to the datasource that contains library's tables. + * + * Returns: the newly #ZakAudit object. + */ +ZakAudit +*zak_audit_new_from_string (const gchar *cnc_string) +{ + return zak_audit_new_from_string_with_datasources (cnc_string, NULL); +} + /** * zak_audit_action: * @zak_audit: an #ZakAudit object. diff --git a/src/libzakaudit.h b/src/libzakaudit.h index 450d0b7..6ae1878 100644 --- a/src/libzakaudit.h +++ b/src/libzakaudit.h @@ -62,8 +62,10 @@ enum ZakAuditActions ZAK_AUDIT_ACTION_DELETE }; -ZakAudit *zak_audit_new (GdaConnection *gda_connection); +ZakAudit *zak_audit_new_with_datasources (GdaConnection *gda_connection, ...); +ZakAudit *zak_audit_new_from_string_with_datasources (const gchar *cnc_string, ...); +ZakAudit *zak_audit_new (GdaConnection *gda_connection); ZakAudit *zak_audit_new_from_string (const gchar *cnc_string); gboolean zak_audit_action (ZakAudit *zak_audit, diff --git a/tests/test1.c b/tests/test1.c index 58da167..9cd6226 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -1,7 +1,7 @@ /* * test1.c * - * Copyright (C) 2005-2010 Andrea Zagli + * Copyright (C) 2005-2013 Andrea Zagli * * This file is part of libaudit. * @@ -24,9 +24,23 @@ #include +static gchar *cnc_zakaudit; +static gchar *cnc_db; + +static GOptionEntry entries[] = +{ + { "cnc_string_zakaudit", 0, 0, G_OPTION_ARG_STRING, &cnc_zakaudit, "Connection string to zakaudit database", NULL }, + { "cnc_string_db", 0, 0, G_OPTION_ARG_STRING, &cnc_db, "Connection string to test database", NULL }, + { NULL } +}; + int main (int argc, char *argv[]) { + GError *error; + + GOptionContext *ctx; + GdaEx *gdaex; ZakAudit *audit; @@ -34,9 +48,32 @@ main (int argc, char *argv[]) GdaSqlParser *parser; GdaStatement *stmt; + cnc_zakaudit = NULL; + cnc_db = NULL; + g_type_init (); - audit = zak_audit_new_from_string ("PostgreSQL://postgres:postgres@HOST=localhost;DB_NAME=audit"); + ctx = g_option_context_new (""); + g_option_context_add_main_entries (ctx, entries, "audit_test1"); + if (!g_option_context_parse (ctx, &argc, &argv, &error)) + { + g_warning ("Errore nell'analisi della riga comandi: %s.", + error != NULL && error->message != NULL ? error->message : "nessun dettaglio"); + } + + if (cnc_zakaudit == NULL) + { + g_message ("You must provide the connection string to zakaudit database."); + return 0; + } + + if (cnc_db == NULL) + { + g_message ("You must provide the connection string to test database."); + return 0; + } + + audit = zak_audit_new_from_string_with_datasources (cnc_zakaudit, "audit_test1", NULL); if (audit == NULL) { @@ -44,7 +81,7 @@ main (int argc, char *argv[]) return 0; } - gdaex = gdaex_new_from_string ("PostgreSQL://postgres:postgres@HOST=localhost;DB_NAME=audit"); + gdaex = gdaex_new_from_string (cnc_db); conn = (GdaConnection *)gdaex_get_gdaconnection (gdaex); parser = gda_connection_create_parser (conn); if (parser == NULL) -- 2.49.0