[libgda] Improved blobs example



commit 784615f2602dfcb71fbfb21cd5f47ccdd070ec99
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sat Dec 29 22:50:26 2012 +0100

    Improved blobs example

 samples/Blobs/Makefile.cross.win32 |   18 ++++++++++++
 samples/Blobs/README               |   18 ++++++++++--
 samples/Blobs/blobtest.c           |   55 +++++++++++++++++++++++-------------
 3 files changed, 68 insertions(+), 23 deletions(-)
---
diff --git a/samples/Blobs/Makefile.cross.win32 b/samples/Blobs/Makefile.cross.win32
new file mode 100644
index 0000000..d0381ab
--- /dev/null
+++ b/samples/Blobs/Makefile.cross.win32
@@ -0,0 +1,18 @@
+CC = /usr/bin/i686-w64-mingw32-gcc
+PKG_CONFIG = /usr/bin/mingw32-pkg-config
+PKG_CONFIG_PATH = /local/Win32/Libgda/lib/pkgconfig
+export PKG_CONFIG_PATH
+
+CFLAGS = -Wall -g -DGDA_DISABLE_DEPRECATED `${PKG_CONFIG} --cflags libgda-5.0`
+LDFLAGS = `${PKG_CONFIG} --libs libgda-5.0`
+
+all: blobtest.exe
+
+blobtest.exe: blobtest.c
+	$(CC) -o blobtest.exe blobtest.c $(CFLAGS) $(LDFLAGS)
+
+clean:
+	rm -f *~
+	rm -f *.o
+	rm -f blobtest.exe
+	rm -f fetched_*
diff --git a/samples/Blobs/README b/samples/Blobs/README
index d144424..bc7bb3a 100644
--- a/samples/Blobs/README
+++ b/samples/Blobs/README
@@ -15,9 +15,10 @@ The PostgreSQL SQL to create the test table is (the "WITH OIDS" clause is option
 but it's required to get the inserted row):
 CREATE TABLE blobstable (id serial PRIMARY KEY, data oid) WITH OIDS;
 
-This test program offers 2 operations which are to store the contents of a file to the
-database (which returns the ID of the stored data), and to fetch a stored data from
-the database from its ID (which creates a fetched_<ID> file).
+This test program offers 3 operations which are:
+* to store the contents of a file to the database (which returns the ID of the stored data)
+* to list stored blobs
+* to fetch a stored data from the database from its ID (which creates a fetched_<ID> file).
 
 This program also shows that transactions are created when reading BLOBs, and that these
 transactions need to be rolled back after the BLOB "handle" (data model and/or GValue) has been released.
@@ -27,6 +28,8 @@ Compiling and running:
 
 To compile (make sure Libgda is installed prior to this):
 > make
+or, if cross compiling for Windows from a Linux box (the Makefile.cross.win32 needs to be adapted to specify the path where the Windows binaries are):
+> make -f Makefile.cross.win32
 
 and to run (stores the blobtest executable file):
 > ./blobtest store blobtest
@@ -38,6 +41,15 @@ Inserted row is (for each numbered column in the table):
   [+1] = [\177ELF\001\001\001\000\000\000\000\000\000\000\000\000\002\000\003\000\001\000\000\000\020\216\004\0104\000\000\000\2542\000\000\000\000\000\000]
 After rolling back blob READ transaction: No transaction started
 Ok.
+
+> ./blobtest list
+id | data                                                                                                                                          
+---+-----------------------------------------------------------------------------------------------------------------------------------------------
+ 1 | \177ELF\001\001\001\000\000\000\000\000\000\000\000\000\002\000\003\000\001\000\000\000\320\216\004\0104\000\000\000<9\000\000\000\000\000\000
+(1 row)
+Ok.
+Still in a transaction, all modifications will be lost when connection is closed
+
 > ./blobtest fetch 1
 Before reading BLOB: No transaction started
 FETCHING BLOB with ID 1 to file 'fetched_1'
diff --git a/samples/Blobs/blobtest.c b/samples/Blobs/blobtest.c
index 9d65f4f..5494e8d 100644
--- a/samples/Blobs/blobtest.c
+++ b/samples/Blobs/blobtest.c
@@ -22,6 +22,7 @@
 GdaConnection *open_connection (void);
 static gboolean do_store (GdaConnection *cnc, const gchar *filename, GError **error);
 static gboolean do_fetch (GdaConnection *cnc, gint id, GError **error);
+static gboolean do_list (GdaConnection *cnc, GError **error);
 
 int
 main (int argc, char *argv[])
@@ -29,36 +30,33 @@ main (int argc, char *argv[])
         GdaConnection *cnc;
 	const gchar *filename = NULL;
 	gint id = 0;
-	gboolean store;
 	GError *error = NULL;
 	gboolean result;
 
 	/* parse arguments */
-	if (argc != 3)
-		goto help;
+        gda_init ();
+	cnc = open_connection ();
+
 	if (! g_ascii_strcasecmp (argv[1], "store")) {
+		if (argc != 3)
+			goto help;
 		filename = argv[2];
-		store = TRUE;
+		result = do_store (cnc, filename, &error);
 	}
 	else if (! g_ascii_strcasecmp (argv[1], "fetch")) {
+		if (argc != 3)
+			goto help;
 		id = atoi (argv[2]);
-		store = FALSE;
+		result = do_fetch (cnc, id, &error);
+	}
+	else if (! g_ascii_strcasecmp (argv[1], "list")) {
+		if (argc != 2)
+			goto help;
+		result = do_list (cnc, &error);
 	}
 	else
 		goto help;
 
-	/* do the job */
-        gda_init ();
-	cnc = open_connection ();
-	if (store)
-		result = do_store (cnc, filename, &error);
-	else
-		result = do_fetch (cnc, id, &error);
-
-	if (gda_connection_get_transaction_status (cnc))
-		g_print ("Still in a transaction, all modifications will be lost when connection is closed\n");
-        gda_connection_close (cnc);
-
 	if (!result) {
 		g_print ("ERROR: %s\n", error && error->message ? error->message : "No detail");
 		g_clear_error (&error);
@@ -66,10 +64,15 @@ main (int argc, char *argv[])
 	else
 		g_print ("Ok.\n");
 
+	if (gda_connection_get_transaction_status (cnc))
+		g_print ("Still in a transaction, all modifications will be lost when connection is closed\n");
+        gda_connection_close (cnc);
+
         return result ? 0 : 1;
 
  help:
-	g_print ("%s [store <filename> | fetch <ID>]\n", argv[0]);
+	gda_connection_close (cnc);
+	g_print ("%s [store <filename> | fetch <ID> | list]\n", argv[0]);
 	return 0;
 }
 
@@ -107,7 +110,6 @@ do_store (GdaConnection *cnc, const gchar *filename, GError **error)
 	GdaSet *params, *newrow;
 	GdaHolder *holder;
 	GValue *value;
-	GdaBlob *blob;
 	gint res;
 
 	parser = gda_sql_parser_new ();
@@ -125,7 +127,6 @@ do_store (GdaConnection *cnc, const gchar *filename, GError **error)
 
 	holder = gda_set_get_holder (params, "blob");
 	value = gda_value_new_blob_from_file (filename);
-	blob = (GdaBlob*) gda_value_get_blob (value);
 	g_assert (gda_holder_take_value (holder, value, NULL));
 
 	g_print ("Before writing BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
@@ -231,3 +232,17 @@ do_fetch (GdaConnection *cnc, gint id, GError **error)
 
 	return result;
 }
+
+static gboolean
+do_list (GdaConnection *cnc, GError **error)
+{
+	GdaDataModel *model;
+
+	model = gda_connection_execute_select_command (cnc, "SELECT * FROM blobstable", error);
+	if (model) {
+		gda_data_model_dump (model, stdout);
+		return TRUE;
+	}
+	else
+		return FALSE;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]