1 /*
2  * Copyright (C) 2008 Murray Cumming <murrayc@murrayc.com>
3  * Copyright (C) 2008 - 2011 Vivien Malerba <malerba@gnome-db.org>
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 "common.h"
20 #include "../test-errors.h"
21 #include <string.h>
22 #include <sql-parser/gda-sql-parser.h>
23 #include <glib/gstdio.h>
24 
25 /*
26  * Creates an SQLite .db file from the definitions in @sqlfile
27  */
28 gboolean
29 create_sqlite_db (const gchar *dir, const gchar *dbname, const gchar *sqlfile, GError **error)
30 {
31 	GdaBatch *batch;
32 	GdaSqlParser *parser;
33 	GdaServerProvider *prov;
34 	GdaConnection *cnc;
35 
36 	/* create batch */
37 	prov = gda_config_get_provider ("SQLite", NULL);
38 	if (!prov) {
39 		g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC, "%s",
40 			     "Cannot find the SQLite provider");
41 		return FALSE;
42 	}
43 	parser = gda_server_provider_create_parser (prov, NULL);
44 	if (!parser)
45 		parser = gda_sql_parser_new ();
46 
47 	batch = gda_sql_parser_parse_file_as_batch (parser, sqlfile, error);
48 	g_object_unref (parser);
49 	if (!batch)
50 		return FALSE;
51 
52 	/* clean any previous DB file */
53 	gchar *fname, *tmp;
54 	tmp = g_strdup_printf ("%s.db", dbname);
55 	fname = g_build_filename (dir, tmp, NULL);
56 	g_free (tmp);
57 	g_unlink (fname);
58 	g_free (fname);
59 
60 	/* open a connection */
61 	gchar *cnc_string;
62 	gchar *edir, *edbname;
63 
64 	edir = gda_rfc1738_encode (dir);
65 	edbname = gda_rfc1738_encode (dbname);
66 	cnc_string = g_strdup_printf ("DB_DIR=%s;DB_NAME=%s", edir, edbname);
67 	g_free (edir);
68 	g_free (edbname);
69 	cnc = gda_connection_open_from_string ("SQLite", cnc_string, NULL,
70 					       GDA_CONNECTION_OPTIONS_NONE, error);
71 	g_free (cnc_string);
72 	if (!cnc) {
73 		g_object_unref (batch);
74 		return FALSE;
75 	}
76 
77 	/* execute batch */
78 	GSList *list;
79 	const GSList *stmt_list;
80 	gboolean retval = TRUE;
81 	list = gda_connection_batch_execute (cnc, batch, NULL, GDA_STATEMENT_MODEL_RANDOM_ACCESS, error);
82 	stmt_list = gda_batch_get_statements (batch);
83 	if (g_slist_length (list) != g_slist_length ((GSList *) stmt_list))
84 		retval = FALSE;
85 
86 	g_slist_foreach (list, (GFunc) g_object_unref, NULL);
87 	g_slist_free (list);
88 
89 	gda_connection_close (cnc);
90 	g_object_unref (cnc);
91 	g_object_unref (batch);
92 	return retval;
93 }
94 
95 gboolean
96 run_sql_non_select (GdaConnection *cnc, const gchar *sql)
97 {
98         GdaStatement *stmt;
99         GError *error = NULL;
100         gint nrows;
101         GdaSqlParser *parser;
102 
103         parser = gda_connection_create_parser (cnc);
104 	if (!parser)
105 		parser = gda_sql_parser_new ();
106         stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
107         g_object_unref (parser);
108 
109         nrows = gda_connection_statement_execute_non_select (cnc, stmt, NULL, NULL, &error);
110         g_object_unref (stmt);
111         if (nrows == -1) {
112                 g_print ("NON SELECT error: %s\n", error && error->message ? error->message : "no detail");
113 		if (error)
114 			g_error_free (error);
115 		return FALSE;
116 	}
117 	return TRUE;
118 }
119 
120 GdaDataModel *
121 run_sql_select (GdaConnection *cnc, const gchar *sql)
122 {
123         GdaStatement *stmt;
124         GError *error = NULL;
125         GdaDataModel *res;
126         GdaSqlParser *parser;
127 
128         parser = gda_connection_create_parser (cnc);
129 	if (!parser)
130 		parser = gda_sql_parser_new ();
131 
132         stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
133         g_object_unref (parser);
134 
135         res = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
136         g_object_unref (stmt);
137         if (!res)
138                 g_print ("Could not execute query: %s\n",
139                          error && error->message ? error->message : "no detail");
140         return res;
141 }
142 
143 GdaDataModel *
144 run_sql_select_cursor (GdaConnection *cnc, const gchar *sql)
145 {
146 	GdaStatement *stmt;
147         GError *error = NULL;
148         GdaDataModel *res;
149         GdaSqlParser *parser;
150 
151         parser = gda_connection_create_parser (cnc);
152         stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
153         g_object_unref (parser);
154 
155         res = gda_connection_statement_execute_select_full (cnc, stmt, NULL, GDA_STATEMENT_MODEL_CURSOR_FORWARD,
156 							    NULL, &error);
157         g_object_unref (stmt);
158         if (!res)
159                 g_print ("Could not execute query in cursor mode: %s\n",
160                          error && error->message ? error->message : "no detail");
161         return res;
162 }
163 
164 gboolean
165 data_models_equal (GdaDataModel *m1, GdaDataModel *m2)
166 {
167 	GdaDataComparator *cmp;
168 	GError *error = NULL;
169 	cmp = (GdaDataComparator*) gda_data_comparator_new (m1, m2);
170 	if (! gda_data_comparator_compute_diff (cmp, &error)) {
171 		g_print ("Can't compare data models: %s\n", error && error->message ? error->message : "no detail");
172 		if (error)
173 			g_error_free (error);
174 		g_object_unref (cmp);
175 		return FALSE;
176 	}
177 	if (gda_data_comparator_get_n_diffs (cmp) != 0) {
178 		g_print ("Data models differ: %d differences\n", gda_data_comparator_get_n_diffs (cmp));
179 		g_object_unref (cmp);
180 		return FALSE;
181 	}
182 	g_object_unref (cmp);
183 	return TRUE;
184 }
185