1 /*
2  * Copyright (C) 2008 - 2011 Vivien Malerba <malerba@gnome-db.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19 #ifdef USING_MINGW
20 #define _NO_OLDNAMES
21 #endif
22 #include <libgda/libgda.h>
23 #include <sql-parser/gda-sql-parser.h>
24 
25 #define PROVNAME "org.postgresql.Driver"
26 #define CNCSTRING "jdbc:postgresql:sales"
27 #define USERNAME "vmalerba"
28 
29 #define H2_PROVNAME "org.h2.Driver"
30 #define H2_CNCSTRING "jdbc:h2:h2data"
31 #define H2_USERNAME "h2"
32 
33 static void test_generic (const gchar *prov_name, const gchar *cnc_string, const gchar *username);
34 static void test_h2 (void);
35 
36 int
main(int argc,char ** argv)37 main (int argc, char **argv)
38 {
39 	const gchar *prov_name, *cnc_string, *username;
40 
41 	/* set up test environment */
42 	g_setenv ("GDA_TOP_BUILD_DIR", TOP_BUILD_DIR, 0);
43 	gda_init ();
44 
45 	/* generic test */
46 	if (argc != 1) {
47 		if (argc != 4) {
48 			g_print ("%s [<JDBC driver> <cnc string> <username>]\n", argv [0]);
49 			return 1;
50 		}
51 		prov_name = argv [1];
52 		cnc_string = argv [2];
53 		username = argv [3];
54 	}
55 	else {
56 		prov_name = PROVNAME;
57 		cnc_string = CNCSTRING;
58 		username = USERNAME;
59 	}
60 
61 	test_generic (prov_name, cnc_string, username);
62 	test_h2 ();
63 	g_print ("OK\n");
64 	return 0;
65 }
66 
67 static void
run_select(GdaConnection * cnc,const gchar * sql)68 run_select (GdaConnection *cnc, const gchar *sql)
69 {
70 	static GdaSqlParser *parser = NULL;
71 	GdaStatement *stmt;
72 	GdaDataModel *model;
73 	GError *error = NULL;
74 
75 	if (!parser)
76 		parser = gda_sql_parser_new ();
77 
78 	stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
79 	model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
80 	if (!model) {
81 		g_print ("Error executing statement: %s\n",
82 			 error && error->message ? error->message : "No detail");
83 		exit (1);
84 	}
85 	gda_data_model_dump (model, stdout);
86 	g_object_unref (stmt);
87 	g_object_unref (model);
88 }
89 
90 static void
test_h2(void)91 test_h2 (void)
92 {
93 	GError *error = NULL;
94 	GdaConnection *cnc;
95 	gchar *real_auth, *tmp;
96 
97 	tmp = gda_rfc1738_encode (H2_USERNAME);
98 	real_auth = g_strdup_printf ("USERNAME=%s", tmp);
99 	g_free (tmp);
100 	cnc = gda_connection_open_from_string (H2_PROVNAME, "URL=" H2_CNCSTRING, real_auth,
101 					       GDA_CONNECTION_OPTIONS_NONE, &error);
102 	g_free (real_auth);
103 	if (!cnc) {
104 		g_print ("Could open connection with the '%s' provider: %s\n", "org.h2.Driver",
105 			 error && error->message ? error->message : "No detail");
106 		exit (1);
107 	}
108 
109 	run_select (cnc, "SELECT * FROM t_string");
110 	run_select (cnc, "SELECT * FROM t_numbers");
111 	run_select (cnc, "SELECT * FROM t_misc");
112 	run_select (cnc, "SELECT * FROM t_bin");
113 	run_select (cnc, "SELECT * FROM t_others");
114 
115 
116 	g_object_unref (cnc);
117 }
118 
119 static void
test_generic(const gchar * prov_name,const gchar * cnc_string,const gchar * username)120 test_generic (const gchar *prov_name, const gchar *cnc_string, const gchar *username)
121 {
122 	GdaServerProvider *prov;
123 	GError *error = NULL;
124 	GdaConnection *cnc;
125 
126 
127 	/* pick up provider */
128 	prov = gda_config_get_provider (prov_name, &error);
129 	if (!prov) {
130 		g_print ("Could not get the '%s' provider: %s\n", prov_name,
131 			 error && error->message ? error->message : "No detail");
132 		return;
133 	}
134 
135 	/* open connection - failure */
136 	g_print ("TEST: Connection opening with failure...\n");
137 	cnc = gda_connection_open_from_string (prov_name, "URL=hello", NULL,
138 					       GDA_CONNECTION_OPTIONS_NONE, &error);
139 	if (cnc) {
140 		g_print ("Connection opening should have failed...\n");
141 		exit (1);
142 	}
143 	g_print ("Connection opening error: %s\n",
144 		 error && error->message ? error->message : "No detail");
145 	if (error) {
146 		g_error_free (error);
147 		error = NULL;
148 	}
149 
150 	/* open connection - success */
151 	g_print ("\nTEST: Connection opening with success...\n");
152 	gchar *real_cnc_string, *tmp;
153 	tmp = gda_rfc1738_encode (cnc_string);
154 	real_cnc_string = g_strdup_printf ("URL=%s", tmp);
155 	g_free (tmp);
156 
157 	gchar *real_auth;
158 	tmp = gda_rfc1738_encode (username);
159 	real_auth = g_strdup_printf ("USERNAME=%s", tmp);
160 	g_free (tmp);
161 
162 	g_print ("CNC STRING: %s\n", real_cnc_string);
163 	cnc = gda_connection_open_from_string (prov_name, real_cnc_string, real_auth,
164 					       GDA_CONNECTION_OPTIONS_NONE, &error);
165 	g_free (real_cnc_string);
166 	g_free (real_auth);
167 	if (!cnc) {
168 		g_print ("Could open connection with the '%s' provider: %s\n", prov_name,
169 			 error && error->message ? error->message : "No detail");
170 		exit (1);
171 	}
172 	gda_connection_close (cnc);
173 	if (! gda_connection_open (cnc, &error)) {
174 		g_print ("Connection re-opening error: %s\n",
175 			 error && error->message ? error->message : "No detail");
176 		exit (1);
177 	}
178 
179 	prov = gda_connection_get_provider (cnc);
180 	g_print ("Server version: %s\n", gda_server_provider_get_server_version (prov, cnc));
181 	g_print ("Server name   : %s\n", gda_server_provider_get_name (prov));
182 
183 	/* prepared statement */
184 	GdaSqlParser *parser;
185 	GdaStatement *stmt;
186 	g_print ("\nTEST: prepared statement\n");
187 	parser = gda_sql_parser_new ();
188 	stmt = gda_sql_parser_parse_string (parser, "SELECT * FROM customers", NULL, NULL);
189 	g_assert (stmt);
190 	if (! gda_connection_statement_prepare (cnc, stmt, &error)) {
191 		g_print ("Error preparing statement: %s\n",
192 			 error && error->message ? error->message : "No detail");
193 		exit (1);
194 	}
195 
196 	g_print ("\nTEST: execute SELECT statement\n");
197 	GdaDataModel *model;
198 	model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
199 	if (!model) {
200 		g_print ("Error executing statement: %s\n",
201 			 error && error->message ? error->message : "No detail");
202 		exit (1);
203 	}
204 	gda_data_model_dump (model, stdout);
205 	g_object_unref (stmt);
206 	g_object_unref (model);
207 
208 
209 	g_print ("\nTEST: execute SELECT statement - 2\n");
210 	stmt = gda_sql_parser_parse_string (parser, "SELECT * FROM orders", NULL, NULL);
211 	model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
212 	if (!model) {
213 		g_print ("Error executing statement: %s\n",
214 			 error && error->message ? error->message : "No detail");
215 		exit (1);
216 	}
217 	gda_data_model_dump (model, stdout);
218 	g_object_unref (model);
219 
220 	g_object_unref (stmt);
221 	g_object_unref (cnc);
222 }
223