1 /*
2  * Copyright (C) 2010 - 2011 Vivien Malerba <malerba@gnome-db.org>
3  * Copyright (C) 2011 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 #include <virtual/libgda-virtual.h>
21 #include <sql-parser/gda-sql-parser.h>
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 /* utility functions */
28 
29 /* Commented out because it is not used.
30 static void          run_sql_non_select (GdaConnection *cnc, const gchar *sql);
31 */
32 
33 static GdaDataModel *run_sql_select (GdaConnection *cnc, const gchar *sql);
34 static gint          run_and_show_sql_select (GdaConnection *cnc, const gchar *sql, const gchar *title);
35 int
main()36 main ()
37 {
38 	GError *error = NULL;
39 	GdaConnection *hub, *cnc;
40         GdaVirtualProvider *provider;
41 
42 	gda_init ();
43 
44 	/* open connection */
45 	cnc = gda_connection_open_from_dsn ("SalesTest", NULL,
46 					    GDA_CONNECTION_OPTIONS_READ_ONLY, &error);
47 	if (!cnc) {
48 		g_print ("Could not open connection to the SalesTest DSN: %s\n",
49 			 error && error->message ? error->message : "No detail");
50 		exit (1);
51 	}
52 
53 	/* Set up Connection hub */
54         provider = gda_vprovider_hub_new ();
55         hub = gda_virtual_connection_open (provider, NULL);
56         g_assert (hub);
57 
58 	if (!gda_vconnection_hub_add (GDA_VCONNECTION_HUB (hub), cnc, "sales", &error)) {
59 		g_print ("Could not add SalesTest connection to hub: %s\n",
60 			 error && error->message ? error->message : "No detail");
61 		exit (1);
62 	}
63 	g_object_unref (cnc);
64 
65 	/* some tests */
66 	run_and_show_sql_select (hub, "SELECT * FROM sales.customers where id=3", NULL);
67 	run_and_show_sql_select (hub, "SELECT * FROM sales.customers where name='Lew Bonito'", NULL);
68 	run_and_show_sql_select (hub, "SELECT * FROM sales.customers where id >= 4 order by name DESC", NULL);
69 	run_and_show_sql_select (hub, "SELECT * FROM sales.customers where id >= 5 order by name DESC", NULL);
70 	/*
71 	run_and_show_sql_select (hub, "SELECT * FROM sales.customers where id=3 OR id=4", NULL);
72 	run_and_show_sql_select (hub, "SELECT * FROM sales.customers where id=3 AND id=4", NULL);
73 	run_and_show_sql_select (hub, "SELECT * FROM sales.customers where id=3 AND id=4", NULL);
74 	*/
75 
76 	g_object_unref (hub);
77 
78 	return 0;
79 }
80 
81 static gint
run_and_show_sql_select(GdaConnection * cnc,const gchar * sql,const gchar * title)82 run_and_show_sql_select (GdaConnection *cnc, const gchar *sql, const gchar *title)
83 {
84 	GdaDataModel *model;
85 	gint nrows;
86 
87 	model = run_sql_select (cnc, sql);
88 	g_print ("\n====== %s ======\n", title ? title : sql);
89 	nrows = gda_data_model_get_n_rows (model);
90 	if (nrows != 0)
91 		gda_data_model_dump (model, stdout);
92 	else
93 		g_print ("None\n");
94 	g_object_unref (model);
95 	g_print ("\n");
96 	return nrows;
97 }
98 
99 static GdaDataModel *
run_sql_select(GdaConnection * cnc,const gchar * sql)100 run_sql_select (GdaConnection *cnc, const gchar *sql)
101 {
102 	GdaStatement *stmt;
103 	GError *error = NULL;
104 	GdaDataModel *res;
105 	GdaSqlParser *parser;
106 
107 	parser = gda_connection_create_parser (cnc);
108 	stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
109 	g_object_unref (parser);
110 
111         res = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
112         g_object_unref (stmt);
113 	if (!res)
114 		g_error ("Could not execute query: %s\n",
115 			 error && error->message ? error->message : "no detail");
116 	return res;
117 }
118 
119 /* Commented out because it is not used.
120 static void
121 run_sql_non_select (GdaConnection *cnc, const gchar *sql)
122 {
123 	GdaStatement *stmt;
124         GError *error = NULL;
125         gint nrows;
126 	GdaSqlParser *parser;
127 
128 	parser = gda_connection_create_parser (cnc);
129 	stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
130 	g_object_unref (parser);
131 
132 	nrows = gda_connection_statement_execute_non_select (cnc, stmt, NULL, NULL, &error);
133 	g_object_unref (stmt);
134         if (nrows == -1)
135                 g_error ("NON SELECT error: %s\n", error && error->message ? error->message : "no detail");
136 }
137 */
138