From c6016dde4ec0c4e43d24ebd354dc88126df1f466 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 15 Aug 2015 11:29:05 +0200 Subject: [PATCH] Spostate le funzioni Allegato::get_blob e ::salva_in_db in GdaEx. --- .gitignore | 1 + src/allegato.c | 171 ++----------------------------------------------- src/allegato.h | 8 ++- 3 files changed, 13 insertions(+), 167 deletions(-) diff --git a/.gitignore b/.gitignore index 1b4271e..32adc7e 100644 --- a/.gitignore +++ b/.gitignore @@ -67,5 +67,6 @@ tests/utils_gdatetime tests/utils_gstring_initial_capital tests/utils_gtktreemodel_copy tests/utils_infobar +tests/utils_message_dialog tests/utils_round *.csv diff --git a/src/allegato.c b/src/allegato.c index bbf3fbf..fe8af78 100644 --- a/src/allegato.c +++ b/src/allegato.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2012 Andrea Zagli + * Copyright (C) 2010-2015 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -22,8 +22,8 @@ #include -#include #include +#include #include #include @@ -294,181 +294,24 @@ solipa_allegato_apri_allegato (GtkWindow *transient, const gchar *filename) } } +G_DEPRECATED_FOR (gdaex_save_file_in_blob) gboolean -solipa_allegato_salva_in_db (GdaConnection *gda_con, const gchar *sql, +solipa_allegato_salva_in_db (GdaEx *gdaex, const gchar *sql, const gchar *blob_field_name, const gchar *percorso) { - GdaSqlParser *parser; - GdaStatement *stmt; - GError *error; - GdaSet *plist; - GdaHolder *param; - GValue *value; - gint res; - - g_return_val_if_fail (GDA_IS_CONNECTION (gda_con), FALSE); - - parser = gda_connection_create_parser (gda_con); - if (parser == NULL) - { - parser = gda_sql_parser_new (); - } - if (parser == NULL) - { - g_warning ("Errore nella creazione del parser sql."); - return FALSE; - } - - error = NULL; - stmt = gda_sql_parser_parse_string (parser, sql, NULL, &error); - g_object_unref (parser); - if (stmt == NULL || error != NULL) - { - g_warning ("Errore nell'analisi della stringa sql: %s.", - error != NULL && error->message != NULL ? error->message : "nessun dettaglio"); - return FALSE; - } - gda_statement_get_parameters (stmt, &plist, NULL); - - gda_connection_begin_transaction (gda_con, NULL, 0, NULL); - - param = gda_set_get_holder (plist, blob_field_name); - value = gda_value_new_blob_from_file (percorso); - error = NULL; - if (!gda_holder_take_value (param, value, &error)) - { - g_object_unref (plist); - - gda_connection_rollback_transaction (gda_con, NULL, NULL); - - /* TODO error */ - g_warning ("Errore nell'impostare il blob: %s.", - error != NULL && error->message != NULL ? error->message : "nessun dettaglio"); - return FALSE; - } - else - { - error = NULL; - res = gda_connection_statement_execute_non_select (gda_con, stmt, plist, NULL, &error); - - g_object_unref (plist); - g_object_unref (stmt); - - if (error != NULL) - { - gda_connection_rollback_transaction (gda_con, NULL, NULL); - - /* TODO error */ - g_warning ("Errore nell'esecuzione dello statement di aggiornamento del blob: %s.", - error != NULL && error->message != NULL ? error->message : "nessun dettaglio"); - return FALSE; - } - gda_connection_commit_transaction (gda_con, NULL, NULL); - } + gdaex_save_file_in_blob (gdaex, sql, blob_field_name, percorso); return TRUE; } +G_DEPRECATED_FOR (gdaex_get_blob) const gchar *solipa_allegato_get_blob (GdaEx *gdaex, const gchar *sql, const gchar *percorso_field_name, const gchar *blob_field_name) { - GdaDataModel *dm; - GError *error; - gint fin; - gchar *filename_tmp; - const GValue *value; - const gchar *percorso; - gchar *filename_orig; - const GdaBlob *blob; - gint i; - - gchar *ret; - - ret = NULL; - - dm = gdaex_query (gdaex, sql); - if (dm != NULL) - { - error = NULL; - value = gda_data_model_get_value_at (dm, - gda_data_model_get_column_index (dm, blob_field_name), - 0, &error); - if (!gda_value_is_null (value) && gda_value_isa (value, GDA_TYPE_BLOB)) - { - blob = gda_value_get_blob (value); - - filename_orig = g_strdup (""); - error = NULL; - value = gda_data_model_get_value_at (dm, - gda_data_model_get_column_index (dm, percorso_field_name), - 0, &error); - if (!gda_value_is_null (value)) - { - percorso = g_value_get_string (value); - filename_orig = g_path_get_basename (percorso); - } - - error = NULL; - fin = g_file_open_tmp (g_strdup_printf ("solipa-XXXXXX-%s", filename_orig), - &filename_tmp, &error); - if (fin > 0 && error == NULL) - { - close (fin); - - if (blob->op) - { - GValue *dest_value; - GdaBlob *dest_blob; - - dest_value = gda_value_new_blob_from_file (filename_tmp); - dest_blob = (GdaBlob *)gda_value_get_blob (dest_value); - if (!gda_blob_op_write_all (dest_blob->op, (GdaBlob *)blob)) - { - g_warning ("Errore nella lettura dell'allegato."); - g_free (ret); - ret = NULL; - } - else - { - ret = g_strdup (filename_tmp); - } - gda_value_free (dest_value); - } - else - { - error = NULL; - if (!g_file_set_contents (filename_tmp, (gchar *)((GdaBinary*)blob)->data, - ((GdaBinary*)blob)->binary_length, &error) || error != NULL) - { - g_warning ("Errore nella lettura dell'allegato."); - g_free (ret); - ret = NULL; - } - else - { - ret = g_strdup (filename_tmp); - } - } - } - else - { - g_warning ("Errore nella lettura dell'allegato: %s.", - error != NULL && error->message != NULL ? error->message : "nessun dettaglio"); - g_free (ret); - ret = NULL; - } - - g_free (filename_tmp); - g_free (filename_orig); - } - - g_object_unref (dm); - } - - return ret; + return gdaex_get_blob (gdaex, sql, percorso_field_name, blob_field_name); } void diff --git a/src/allegato.h b/src/allegato.h index 51f9e56..f9ba902 100644 --- a/src/allegato.h +++ b/src/allegato.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2012 Andrea Zagli + * Copyright (C) 2010-2015 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -22,7 +22,7 @@ #include #include -#include +#include #include #include "solipa.h" @@ -67,6 +67,7 @@ guint solipa_allegato_get_dimensione (SolipaAllegato *solipa_allegato); gboolean solipa_allegato_get_salva_in_db (SolipaAllegato *solipa_allegato); void solipa_allegato_set_salva_in_db (SolipaAllegato *solipa_allegato, gboolean salva); +G_DEPRECATED_FOR (gdaex_get_blob) const gchar *solipa_allegato_get_blob (GdaEx *gdaex, const gchar *sql, const gchar *percorso_field_name, @@ -79,7 +80,8 @@ void solipa_allegato_add_to_gtkform (SolipaAllegato *solipa_allegato, GtkForm *f void solipa_allegato_apri_allegato (GtkWindow *transient, const gchar *filename); -gboolean solipa_allegato_salva_in_db (GdaConnection *gda_con, const gchar *sql, +G_DEPRECATED_FOR (gdaex_save_file_in_blob) +gboolean solipa_allegato_salva_in_db (GdaEx *gdaexx, const gchar *sql, const gchar *blob_field_name, const gchar *percorso); -- 2.49.0