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 <libgda/libgda.h>
20 #include <sql-parser/gda-sql-parser.h>
21 #include <libgda/gda-server-provider-extra.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "../test-errors.h"
25
26 static gboolean run_test (const gchar *sql, const gchar *empty_rs_serial, GError **error);
27
28 typedef struct {
29 gchar *sql;
30 gchar *empty_rs_serial;
31 } ATestData;
32
33 ATestData tests [] = {
34 {"SELECT * FROM table WHERE id=3", "{\"statement\":{\"sql\":null,\"stmt_type\":\"SELECT\",\"contents\":{\"distinct\":\"false\",\"fields\":[{\"expr\":{\"value\":\"*\"}}],\"from\":{\"targets\":[{\"expr\":{\"value\":\"table\"},\"table_name\":\"table\"}]},\"where\":{\"operation\":{\"operator\":\"=\",\"operand0\":{\"value\":\"0\"},\"operand1\":{\"value\":\"1\"}}}}}}"},
35 {"SELECT id, func1 (3, func2('rr'), ##aparam::string), NULL FROM a, b", "{\"statement\":{\"sql\":null,\"stmt_type\":\"SELECT\",\"contents\":{\"distinct\":\"false\",\"fields\":[{\"expr\":{\"value\":\"id\"},\"field_name\":\"id\"},{\"expr\":{\"func\":{\"function_name\":\"func1\",\"function_args\":[{\"value\":\"3\"},{\"func\":{\"function_name\":\"func2\",\"function_args\":[{\"value\":\"'rr'\"}]}},{\"value\":\"0\"}]}}},{\"expr\":{\"value\":null}}],\"from\":{\"targets\":[{\"expr\":{\"value\":\"a\"},\"table_name\":\"a\"},{\"expr\":{\"value\":\"b\"},\"table_name\":\"b\"}],\"joins\":[{\"join_type\":\"CROSS\",\"join_pos\":\"1\"}]},\"where\":{\"operation\":{\"operator\":\"=\",\"operand0\":{\"value\":\"0\"},\"operand1\":{\"value\":\"1\"}}}}}}"},
36 {"SELECT ##p1 + ##p2", "{\"statement\":{\"sql\":null,\"stmt_type\":\"SELECT\",\"contents\":{\"distinct\":\"false\",\"fields\":[{\"expr\":{\"operation\":{\"operator\":\"+\",\"operand0\":{\"value\":\"0\"},\"operand1\":{\"value\":\"0\"}}}}],\"where\":{\"operation\":{\"operator\":\"=\",\"operand0\":{\"value\":\"0\"},\"operand1\":{\"value\":\"1\"}}}}}}"},
37 {NULL, NULL}
38 };
39
40 int
main(int argc,char ** argv)41 main (int argc, char **argv)
42 {
43 GError *error = NULL;
44 gint i, nfailed;
45
46 gda_init ();
47 nfailed = 0;
48 for (i = 0; ; i++) {
49 ATestData *td = &(tests[i]);
50 if (!td->sql)
51 break;
52 if (!run_test (td->sql, td->empty_rs_serial, &error)) {
53 g_print ("Test %d failed: %s\n", i,
54 error && error->message ? error->message : "No detail");
55 nfailed++;
56 if (error)
57 g_error_free (error);
58 error = NULL;
59 }
60 }
61
62 if (nfailed == 0)
63 g_print ("Ok.\n");
64 return nfailed == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
65 }
66
67 static gboolean
run_test(const gchar * sql,const gchar * empty_rs_serial,GError ** error)68 run_test (const gchar *sql, const gchar *empty_rs_serial, GError **error)
69 {
70 static GdaSqlParser *parser = NULL;
71 GdaStatement *orig, *trans;
72 gboolean retval = FALSE;
73 gchar *tsql;
74
75 if (!parser)
76 parser = gda_sql_parser_new ();
77 orig = gda_sql_parser_parse_string (parser, sql, NULL, error);
78 if (!orig)
79 return FALSE;
80
81 trans = gda_select_alter_select_for_empty (orig, error);
82 if (!trans)
83 goto out;
84
85 tsql = gda_statement_to_sql_extended (trans, NULL, NULL, 0, NULL, error);
86 if (!tsql)
87 goto out;
88 g_print ("SQL: %s\nTRA: %s\n", sql, tsql);
89 g_free (tsql);
90
91 tsql = gda_statement_serialize (trans);
92 if (!empty_rs_serial)
93 g_print ("Missing test data!\n SQL: %s\n SER: %s\n", sql, tsql);
94 else if (strcmp (tsql, empty_rs_serial)) {
95 g_print ("Test failed!\n SQL: %s\n EXP: %s\n GOT: %s\n", sql, empty_rs_serial, tsql);
96 g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC, "%s",
97 "Failed serialized comparison");
98 g_free (tsql);
99 goto out;
100 }
101
102 retval = TRUE;
103 out:
104
105 if (trans)
106 g_object_unref (trans);
107 g_object_unref (orig);
108 return retval;
109 }
110