]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
Aggiustamenti alla gestione allegati.
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 1 Feb 2013 07:51:05 +0000 (08:51 +0100)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 1 Feb 2013 07:51:05 +0000 (08:51 +0100)
src/allegato.c

index 8479b48f3f355b4ff3e28d7d6ce48f1a63d26828..fc9f40b39d6b001633b48108fcb883388c544424 100644 (file)
@@ -302,7 +302,7 @@ solipa_allegato_salva_in_db (GdaConnection *gda_con, const gchar *sql,
        GdaSet *plist;
        GdaHolder *param;
        GValue *value;
-       GObject *exec_res;
+       gint res;
 
        g_return_val_if_fail (GDA_IS_CONNECTION (gda_con), FALSE);
 
@@ -310,6 +310,7 @@ solipa_allegato_salva_in_db (GdaConnection *gda_con, const gchar *sql,
 
        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.",
@@ -321,8 +322,10 @@ solipa_allegato_salva_in_db (GdaConnection *gda_con, const gchar *sql,
        param = gda_set_get_holder (plist, blob_field_name);
        value = gda_value_new_blob_from_file (percorso);
        error = NULL;
-       if (!gda_holder_set_value (param, value, &error))
+       if (!gda_holder_take_value (param, value, &error))
                {
+                       g_object_unref (plist);
+
                        /* TODO error */
                        g_warning ("Errore nell'impostare il blob: %s.",
                                   error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
@@ -331,8 +334,12 @@ solipa_allegato_salva_in_db (GdaConnection *gda_con, const gchar *sql,
        else
                {
                        error = NULL;
-                       exec_res = gda_connection_statement_execute (gda_con, stmt, plist, GDA_STATEMENT_MODEL_RANDOM_ACCESS, NULL, &error);
-                       if (!exec_res)
+                       res = gda_connection_statement_execute_non_select (gda_con, stmt, plist, NULL, &error);
+
+                       g_object_unref (plist);
+                       g_object_unref (stmt);
+
+                       if (error != NULL)
                                {
                                        /* TODO error */
                                        g_warning ("Errore nell'esecuzione dello statement di aggiornamento del blob: %s.",
@@ -390,13 +397,20 @@ const gchar
                                        error = NULL;
                                        fin = g_file_open_tmp (g_strdup_printf ("solipa-XXXXXX-%s", filename_orig),
                                                               &filename_tmp, &error);
-                                       if (fin > 0)
+                                       if (fin > 0 && error == NULL)
                                                {
-                                                       if (gda_blob_op_read_all (blob->op, blob))
+                                                       close (fin);
+
+                                                       if (blob->op)
                                                                {
-                                                                       if (write (fin, blob->data.data, blob->data.binary_length) < 0)
+                                                                       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 scrittura del file temporaneo.");
+                                                                                       g_warning ("Errore nella lettura dell'allegato.");
                                                                                        g_free (ret);
                                                                                        ret = NULL;
                                                                                }
@@ -404,24 +418,28 @@ const gchar
                                                                                {
                                                                                        ret = g_strdup (filename_tmp);
                                                                                }
-#ifdef G_OS_WIN32
-                                                                       _commit (fin);
-#else
-                                                                       fsync (fin);
-#endif
+                                                                       gda_value_free (dest_value);
                                                                }
                                                        else
                                                                {
-                                                                       g_warning ("Errore nella lettura dell'allegato.");
-                                                                       g_free (ret);
-                                                                       ret = NULL;
+                                                                       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);
+                                                                               }
                                                                }
-
-                                                       close (fin);
                                                }
                                        else
                                                {
-                                                       g_warning ("Errore nella lettura dell'allegato: %s.", error->message);
+                                                       g_warning ("Errore nella lettura dell'allegato: %s.",
+                                                                  error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
                                                        g_free (ret);
                                                        ret = NULL;
                                                }
@@ -429,6 +447,8 @@ const gchar
                                        g_free (filename_tmp);
                                        g_free (filename_orig);
                                }
+
+                       g_object_unref (dm);
                }
 
        return ret;