File: check_dml_comp.c

package info (click to toggle)
libgda5 5.2.10-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,168 kB
  • sloc: ansic: 495,319; xml: 10,486; yacc: 5,165; sh: 4,451; makefile: 4,095; php: 1,416; java: 1,300; javascript: 1,298; python: 896; sql: 879; perl: 116
file content (209 lines) | stat: -rw-r--r-- 6,083 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (C) 2008 - 2011 Vivien Malerba <malerba@gnome-db.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <stdio.h>
#include <glib.h>
#include <glib-object.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <gmodule.h>
#include <libgda/libgda.h>
#include <libgda/gda-util.h>
#include <sql-parser/gda-sql-parser.h>
#include <libgda/gda-debug-macros.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

GdaConnection *cnc;
static gint do_test (const xmlChar *id, const xmlChar *sql, 
		     const gchar *computed_type, const xmlChar *computed_exp, gboolean require_pk);

int 
main (int argc, char** argv)
{
	xmlDocPtr doc;
        xmlNodePtr root, node;
	gint failures = 0;
	gint ntests = 0;
	gchar *fname;

	gda_init ();

	/* open connection */
	gchar *cnc_string;
	fname = g_build_filename (ROOT_DIR, "data", NULL);
	cnc_string = g_strdup_printf ("DB_DIR=%s;DB_NAME=sales_test", fname);
	g_free (fname);
	cnc = gda_connection_open_from_string ("SQLite", cnc_string, NULL,
					       GDA_CONNECTION_OPTIONS_READ_ONLY, NULL);
	if (!cnc) {
		g_print ("Failed to open connection, cnc_string = %s\n", cnc_string);
		exit (1);
	}
	if (!gda_connection_update_meta_store (cnc, NULL, NULL)) {
		g_print ("Failed to update meta store, cnc_string = %s\n", cnc_string);
		exit (1);
	}
	g_free (cnc_string);

	/* load file */
	fname = g_build_filename (ROOT_DIR, "tests", "parser", "testvalid.xml", NULL);
	if (! g_file_test (fname, G_FILE_TEST_EXISTS)) {
                g_print ("File '%s' does not exist\n", fname);
                exit (1);
        }

	/* use test data */
	doc = xmlParseFile (fname);
	g_free (fname);
	g_assert (doc);
	root = xmlDocGetRootElement (doc);
	g_assert (!strcmp ((gchar*) root->name, "testdata"));
	for (node = root->children; node; node = node->next) {
		if (strcmp ((gchar*) node->name, "test"))
			continue;
		xmlNodePtr snode;
		xmlChar *sql = NULL;
		xmlChar *id;

		id = xmlGetProp (node, BAD_CAST "id");
		for (snode = node->children; snode; snode = snode->next) {
			if (!strcmp ((gchar*) snode->name, "sql")) 
				sql = xmlNodeGetContent (snode);
			else if (!strcmp ((gchar*) snode->name, "insert") ||
				 !strcmp ((gchar*) snode->name, "update") ||
				 !strcmp ((gchar*) snode->name, "delete")) {
				xmlChar *comp_exp;
				xmlChar *prop;
				gboolean require_pk = TRUE;
				comp_exp = xmlNodeGetContent (snode);
				prop = xmlGetProp (snode, "need_pk");
				if (prop) {
					if ((*prop == 'f') || (*prop == 'F') || (*prop == '0'))
						require_pk = FALSE;
					xmlFree (prop);
				}
				if (sql) {
					if (!do_test (id, sql, snode->name, comp_exp, require_pk))
						failures++;
					ntests++;
				}
			}
		}

		/* mem free */
		if (sql) xmlFree (sql);
		if (id)	xmlFree (id);
	}
	xmlFreeDoc (doc);

	g_print ("TESTS COUNT: %d\n", ntests);
	g_print ("FAILURES: %d\n", failures);
  
	return failures != 0 ? 1 : 0;
}

/*
 * Returns: the number of failures
 */
static gint
do_test (const xmlChar *id, const xmlChar *sql,
	 const gchar *computed_type, const xmlChar *computed_exp, gboolean require_pk) 
{
	static GdaSqlParser *parser = NULL;
	GdaStatement *stmt;
	GError *error = NULL;

	if (!parser) {
		parser = gda_connection_create_parser (cnc);
		if (!parser)
			parser = gda_sql_parser_new ();
	}

#ifdef GDA_DEBUG
	g_print ("===== TEST %s COMPUTING %s (%s), SQL: @%s@\n", id, computed_type, 
		 require_pk ? "PK fields" : "All fields", sql);
#endif

	stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
	if (!stmt) {
		g_print ("ERROR for test '%s': could not parse statement\n", id);
		return FALSE;
	}
	/*g_print ("EXP %d, got %d\n", valid_expected, is_valid);*/
	/*g_print ("PARSED: %s\n", gda_statement_serialize (stmt));*/

	if (computed_exp) {
		GdaStatement *cstmt = NULL;
		switch (*computed_type) {
		case 'i':
		case 'I':
			gda_compute_dml_statements (cnc, stmt, require_pk, &cstmt, NULL, NULL, &error);
			break;
		case 'u':
		case 'U':
			gda_compute_dml_statements (cnc, stmt, require_pk, NULL, &cstmt, NULL, &error);
			break;
		case 'd':
		case 'D':
			gda_compute_dml_statements (cnc, stmt, require_pk, NULL, NULL, &cstmt, &error);
			break;
		default:
			TO_IMPLEMENT;
		}

		if (!*computed_exp && cstmt) {
			g_object_unref (stmt);
			gchar *serial, *rend;
			serial = gda_statement_serialize (cstmt);
			rend = gda_statement_to_sql (cstmt, NULL, NULL);
			g_print ("ERROR for test '%s': %s statement created but none expected\n"
				 "\tgot: %s\n\tSQL: %s\n", id, computed_type, serial, rend);
			g_free (serial);
			g_free (rend);
			g_object_unref (cstmt);
			return FALSE;
		}
		if (*computed_exp && !cstmt) {
			g_print ("ERROR for test '%s': %s statement not created but expected: %s\n", id,
				 computed_type,
				 error && error->message ? error->message : "No detail");
			g_object_unref (stmt);
			return FALSE;
		}
		if (*computed_exp && cstmt) {
			gchar *serial;
			serial = gda_statement_serialize (cstmt);
			if (strcmp (serial, computed_exp)) {
				gchar *rend;
				rend = gda_statement_to_sql (cstmt, NULL, NULL);
				g_print ("ERROR for test '%s': computed %s statement is incorrect:\n"
					 "\texp: %s\n\tgot: %s\n\tSQL: %s\n", id, computed_type, computed_exp, serial,
					 rend);
				g_free (rend);
				g_object_unref (stmt);
				g_object_unref (cstmt);
				g_free (serial);
				return FALSE;
			}
			g_free (serial);
		}
	}
	g_object_unref (stmt);
	return TRUE;
}