File: background.c

package info (click to toggle)
libgda5 5.2.10-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,168 kB
  • sloc: ansic: 495,319; xml: 10,486; yacc: 5,165; sh: 4,451; makefile: 4,095; php: 1,416; java: 1,300; javascript: 1,298; python: 896; sql: 879; perl: 116
file content (124 lines) | stat: -rw-r--r-- 3,516 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright (C) 2009 - 2011 Vivien Malerba <malerba@gnome-db.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <libgda/libgda.h>
#include <libgda/thread-wrapper/gda-thread-wrapper.h>

/*
 * Display the contents of the 'data' data model
 */
static void
list_table_columns (GdaDataModel* data)
{
	gint nrows;

	nrows = gda_data_model_get_n_rows (data);
	if (nrows == 0)
		g_print ("No column...\n");
	else {
		gint i;
		const GValue* col_name;
		const GValue* col_type;

		g_print ("Tables' columns:\n");
		for (i = 0; i < nrows; ++ i) {
			col_name = gda_data_model_get_value_at (data, 0, i, NULL);
			g_assert (col_name);
			
			col_type = gda_data_model_get_value_at (data, 2, i, NULL);
			g_assert (col_type);
			
			printf("  %s: %s\n", g_value_get_string (col_name), g_value_get_string (col_type));
		}
	}
}

/* this function is executed in a sub thread */
gboolean *
sub_thread_update_meta_store (GdaConnection *cnc, GError **error)
{
	gboolean *result;
	result = g_new (gboolean, 1);
	*result = gda_connection_update_meta_store (cnc, NULL, error);
	return result;
}

int
main (int argc, char *argv[])
{
	GdaConnection* connection;
	GdaDataModel* data;
	GError* error = NULL;
	GValue *value;

	gda_init();
	error = NULL;

	/* open connection to the SalesTest data source, using the GDA_CONNECTION_OPTIONS_THREAD_SAFE flag
	 * to make sure we can use the same connection from multiple threads at once */
	connection = gda_connection_open_from_dsn ("SalesTest", NULL, GDA_CONNECTION_OPTIONS_THREAD_SAFE, &error);
	if (!connection) {
		fprintf (stderr, "%s\n", error->message);
		return -1;
	}

	/* update meta data */
	GdaThreadWrapper *wrapper;
	guint job_id;

	g_print ("Requesting a meta data update in the background.\n");
	wrapper = gda_thread_wrapper_new ();
	job_id = gda_thread_wrapper_execute (wrapper, (GdaThreadWrapperFunc) sub_thread_update_meta_store,
					     connection, NULL, &error);
	if (job_id == 0) {
		fprintf (stderr, "Can't use thread wrapper: %s\n", error->message);
		return -1;
	}

	while (1) {
		gboolean *result;
		g_print ("Doing some business here...\n");
		g_usleep (100000);
		result = (gboolean *) gda_thread_wrapper_fetch_result (wrapper, FALSE, job_id, &error);
		if (result) {
			gboolean meta_ok;

			g_object_unref (wrapper);
			meta_ok = *result;
			g_free (result);
			if (!meta_ok) {
				fprintf (stderr, "Could not update meta data: %s\n", error->message);
				return -1;
			}
			g_print ("Meta data has been updated!\n");
			break;
		}
	}

	/*
	 * Get columns of the 'products' table
	 */
	g_value_set_string ((value = gda_value_new (G_TYPE_STRING)), "products");
	data = gda_connection_get_meta_store_data (connection, GDA_CONNECTION_META_FIELDS, &error, 1,
						   "name", value);
	if (!data)
		return -1;
	list_table_columns (data);
	g_object_unref (data);

	return 0;
}