1 /*
2  * Copyright (C) 2007 - 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 <virtual/libgda-virtual.h>
20 #include "common.h"
21 #include <sql-parser/gda-sql-parser.h>
22 
23 gboolean copy_products (GdaConnection *virtual);
24 
25 int
main(int argc,char * argv[])26 main (int argc, char *argv[])
27 {
28         GdaConnection *s_cnc, *d_cnc, *virtual;
29 
30         gda_init ();
31 
32 	/* open "real" connections */
33 	s_cnc = open_source_connection ();
34         d_cnc = open_destination_connection ();
35 
36 	/* virtual connection settings */
37 	GdaVirtualProvider *provider;
38 	GError *error = NULL;
39 	provider = gda_vprovider_hub_new ();
40         virtual = gda_virtual_connection_open (provider, NULL);
41 
42 	/* adding connections to the virtual connection */
43         if (!gda_vconnection_hub_add (GDA_VCONNECTION_HUB (virtual), s_cnc, "source", &error)) {
44                 g_print ("Could not add connection to virtual connection: %s\n",
45                          error && error->message ? error->message : "No detail");
46                 exit (1);
47         }
48         if (!gda_vconnection_hub_add (GDA_VCONNECTION_HUB (virtual), d_cnc, "destination", &error)) {
49                 g_print ("Could not add connection to virtual connection: %s\n",
50                          error && error->message ? error->message : "No detail");
51                 exit (1);
52         }
53 
54 	/* copy some contents of the 'products' table into the 'products_copied', method 1 */
55 	if (! copy_products (virtual))
56 		exit (1);
57 
58         gda_connection_close (virtual);
59         gda_connection_close (s_cnc);
60         gda_connection_close (d_cnc);
61         return 0;
62 }
63 
64 /*
65  * Data copy
66  */
67 gboolean
copy_products(GdaConnection * virtual)68 copy_products (GdaConnection *virtual)
69 {
70 	GdaSqlParser *parser;
71 	GdaStatement *stmt;
72         gchar *sql;
73 	GError *error = NULL;
74 
75         /* DROP table if it exists */
76 	parser = gda_connection_create_parser (virtual);
77 	sql = "INSERT INTO destination.products_copied3 SELECT ref, name, price, wh_stored FROM source.products LIMIT 10";
78         stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
79 	g_assert (stmt);
80         g_object_unref (parser);
81 
82 	if (gda_connection_statement_execute_non_select (virtual, stmt, NULL, NULL, &error) == -1) {
83 		g_print ("Could not copy table's contents: %s\n",
84                          error && error->message ? error->message : "No detail");
85 		g_error_free (error);
86 		g_object_unref (stmt);
87 		return FALSE;
88 	}
89 	g_object_unref (stmt);
90 	return TRUE;
91 }
92