1 /*
2  * Copyright (C) 2009 - 2011 Vivien Malerba <malerba@gnome-db.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 #include <libgda/libgda.h>
19 #include <libgda/thread-wrapper/gda-thread-wrapper.h>
20 
21 /*
22  * Display the contents of the 'data' data model
23  */
24 static void
list_table_columns(GdaDataModel * data)25 list_table_columns (GdaDataModel* data)
26 {
27 	gint nrows;
28 
29 	nrows = gda_data_model_get_n_rows (data);
30 	if (nrows == 0)
31 		g_print ("No column...\n");
32 	else {
33 		gint i;
34 		const GValue* col_name;
35 		const GValue* col_type;
36 
37 		g_print ("Tables' columns:\n");
38 		for (i = 0; i < nrows; ++ i) {
39 			col_name = gda_data_model_get_value_at (data, 0, i, NULL);
40 			g_assert (col_name);
41 
42 			col_type = gda_data_model_get_value_at (data, 2, i, NULL);
43 			g_assert (col_type);
44 
45 			printf("  %s: %s\n", g_value_get_string (col_name), g_value_get_string (col_type));
46 		}
47 	}
48 }
49 
50 /* this function is executed in a sub thread */
51 gboolean *
sub_thread_update_meta_store(GdaConnection * cnc,GError ** error)52 sub_thread_update_meta_store (GdaConnection *cnc, GError **error)
53 {
54 	gboolean *result;
55 	result = g_new (gboolean, 1);
56 	*result = gda_connection_update_meta_store (cnc, NULL, error);
57 	return result;
58 }
59 
60 int
main(int argc,char * argv[])61 main (int argc, char *argv[])
62 {
63 	GdaConnection* connection;
64 	GdaDataModel* data;
65 	GError* error = NULL;
66 	GValue *value;
67 
68 	gda_init();
69 	error = NULL;
70 
71 	/* open connection to the SalesTest data source, using the GDA_CONNECTION_OPTIONS_THREAD_SAFE flag
72 	 * to make sure we can use the same connection from multiple threads at once */
73 	connection = gda_connection_open_from_dsn ("SalesTest", NULL, GDA_CONNECTION_OPTIONS_THREAD_SAFE, &error);
74 	if (!connection) {
75 		fprintf (stderr, "%s\n", error->message);
76 		return -1;
77 	}
78 
79 	/* update meta data */
80 	GdaThreadWrapper *wrapper;
81 	guint job_id;
82 
83 	g_print ("Requesting a meta data update in the background.\n");
84 	wrapper = gda_thread_wrapper_new ();
85 	job_id = gda_thread_wrapper_execute (wrapper, (GdaThreadWrapperFunc) sub_thread_update_meta_store,
86 					     connection, NULL, &error);
87 	if (job_id == 0) {
88 		fprintf (stderr, "Can't use thread wrapper: %s\n", error->message);
89 		return -1;
90 	}
91 
92 	while (1) {
93 		gboolean *result;
94 		g_print ("Doing some business here...\n");
95 		g_usleep (100000);
96 		result = (gboolean *) gda_thread_wrapper_fetch_result (wrapper, FALSE, job_id, &error);
97 		if (result) {
98 			gboolean meta_ok;
99 
100 			g_object_unref (wrapper);
101 			meta_ok = *result;
102 			g_free (result);
103 			if (!meta_ok) {
104 				fprintf (stderr, "Could not update meta data: %s\n", error->message);
105 				return -1;
106 			}
107 			g_print ("Meta data has been updated!\n");
108 			break;
109 		}
110 	}
111 
112 	/*
113 	 * Get columns of the 'products' table
114 	 */
115 	g_value_set_string ((value = gda_value_new (G_TYPE_STRING)), "products");
116 	data = gda_connection_get_meta_store_data (connection, GDA_CONNECTION_META_FIELDS, &error, 1,
117 						   "name", value);
118 	if (!data)
119 		return -1;
120 	list_table_columns (data);
121 	g_object_unref (data);
122 
123 	return 0;
124 }
125