1 /*
2 * Copyright (C) 2009 - 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
20 #undef GDA_DISABLE_DEPRECATED
21 #include <libgda/sql-parser/gda-sql-parser.h>
22
23 typedef struct {
24 gchar *sql_identifier;
25 gboolean need_quotes;
26 } ATest;
27
28 ATest tests[] = {
29 {"\"Solution\"", TRUE},
30 {"mytable", FALSE},
31 {"MYTABLE", FALSE},
32 {"MyTable", TRUE},
33 {"my.blob", TRUE},
34 {"_table", FALSE},
35 {"$table", FALSE},
36 {"#table", FALSE},
37 {"8table", TRUE},
38 {"t8ble", FALSE},
39 {"t8ble_1", FALSE},
40 {"t8ble_A", TRUE},
41 {"T8BLE_A", FALSE},
42 {"T8BLE_a", TRUE},
43 };
44
45 static gboolean
identifier_needs_quotes(const gchar * str)46 identifier_needs_quotes (const gchar *str)
47 {
48 const gchar *ptr;
49 gchar icase = 0;
50
51 g_return_val_if_fail (str, FALSE);
52 for (ptr = str; *ptr; ptr++) {
53 /* quote if 1st char is a number */
54 if ((*ptr <= '9') && (*ptr >= '0')) {
55 if (ptr == str)
56 return TRUE;
57 continue;
58 }
59 if ((*ptr >= 'A') && (*ptr <= 'Z')) {
60 if (icase == 0) /* first alpha char encountered */
61 icase = 'U';
62 else if (icase == 'L') /* @str has mixed case */
63 return TRUE;
64 continue;
65 }
66 if ((*ptr >= 'a') && (*ptr <= 'z')) {
67 if (icase == 0) /* first alpha char encountered */
68 icase = 'L';
69 else if (icase == 'U')
70 return TRUE; /* @str has mixed case */
71 continue;
72 }
73 if ((*ptr != '$') && (*ptr != '_') && (*ptr != '#'))
74 return TRUE;
75 }
76 return FALSE;
77 }
78
79 int
main(int argc,char ** argv)80 main (int argc, char** argv)
81 {
82 guint i, nfailed = 0;
83 for (i = 0; i < G_N_ELEMENTS (tests); i++) {
84 ATest *test = &(tests [i]);
85 if (identifier_needs_quotes (test->sql_identifier) != test->need_quotes) {
86 g_print ("Failed for %s: reported %s\n", test->sql_identifier,
87 test->need_quotes ? "no quotes needed" : "quotes needed");
88 nfailed++;
89 }
90 }
91
92 g_print ("%d tests executed, ", i);
93 if (nfailed > 0)
94 g_print ("%d failed\n", nfailed);
95 else
96 g_print ("Ok\n");
97 return EXIT_SUCCESS;
98 }
99