1 /*
2  * Copyright (C) 2009 - 2011 Vivien Malerba <malerba@gnome-db.org>
3  * Copyright (C) 2012 Murray Cumming <murrayc@murrayc.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 #include <libgda/libgda.h>
20 
21 static const gchar* TABLE_NAME = "customers";
22 
23 /*
24  * Display the contents of the 'data' data model
25  */
26 static void
27 list_table_columns (GdaDataModel* data)
test_implicit()28 {
29 	gint nrows;
30 
31 	nrows = gda_data_model_get_n_rows (data);
32 	if (nrows == 0)
33 		g_print ("No column...\n");
34 	else {
35 		gint i;
36 		const GValue* col_name;
37 		const GValue* col_type;
38 
39 		g_print ("Tables' columns:\n");
40 		for (i = 0; i < nrows; ++ i) {
41 			col_name = gda_data_model_get_value_at (data, 0, i, NULL);
42 			g_assert (col_name);
43 
44 			col_type = gda_data_model_get_value_at (data, 2, i, NULL);
45 			g_assert (col_type);
46 
47 			printf("  %s: %s\n", g_value_get_string (col_name), g_value_get_string (col_type));
48 		}
49 	}
50 }
51 
52 int
53 main (int argc, char *argv[])
54 {
55 	GdaConnection* connection;
56 	GdaDataModel* data;
57 	GError* error = NULL;
58 	GValue *value;
59 
60 	gda_init();
61 	error = NULL;
62 
63 	/* open connection to the SalesTest data source */
64 	connection = gda_connection_open_from_dsn ("SalesTest", NULL, GDA_CONNECTION_OPTIONS_NONE, &error);
65 	if (!connection) {
66 		fprintf (stderr, "%s\n", error->message);
67 		return -1;
68 	}
69 
70 	/* Initial meta store, to show it has no information about the TABLE_NAME table */
71 	g_print ("Initial metastore state\n");
72 	g_value_set_string ((value = gda_value_new (G_TYPE_STRING)), TABLE_NAME);
73 	data = gda_connection_get_meta_store_data (connection, GDA_CONNECTION_META_FIELDS, &error, 1,
74 						   "name", value);
75 	if (!data)
76 		return -1;
77 	list_table_columns (data);
78 	g_object_unref (data);
79 
80 	/* Request partial update for the table we are interested in
81 	 * the GdaMetaContext specifies to update the "_tables" table, where the "table_name"
82 	 * attribute is TABLE_NAME */
83 	g_print ("\nPartial metastore update for table '%s'...\n", TABLE_NAME);
84 	GdaMetaContext mcontext = {"_tables", 1, NULL, NULL};
85 	mcontext.column_names = g_new (gchar *, 1);
86 	mcontext.column_names[0] = "table_name";
87 	mcontext.column_values = g_new (GValue *, 1);
88 	g_value_set_string ((mcontext.column_values[0] = gda_value_new (G_TYPE_STRING)), TABLE_NAME);
89 	if (!gda_connection_update_meta_store (connection, &mcontext, &error))
90 		return -1;
91 
92 	/* Query the same information about the TABLE_NAME table's columns
93 	 * this time there should be some information */
94 	data = gda_connection_get_meta_store_data (connection, GDA_CONNECTION_META_FIELDS, &error, 1,
95 						   "name", value);
96 	if (!data)
97 		return -1;
98 	list_table_columns (data);
99 	g_object_unref (data);
100 
101 	/*
102 	 * Request partial update for all the tables in the database
103 	 */
104 	g_print ("\nPartial metastore update for all the tables...\n");
105 	GdaMetaContext mcontext2 = {"_tables", 0, NULL, NULL};
106 	if (!gda_connection_update_meta_store (connection, &mcontext2, &error))
107 		return -1;
108 
109 	/*
110 	 * Get columns of the 'products' table
111 	 */
112 	gda_value_free (value);
113 	g_value_set_string ((value = gda_value_new (G_TYPE_STRING)), "products");
114 	data = gda_connection_get_meta_store_data (connection, GDA_CONNECTION_META_FIELDS, &error, 1,
115 						   "name", value);
116 	if (!data)
117 		return -1;
118 	list_table_columns (data);
119 	g_object_unref (data);
120 
121 	return 0;
122 }
123