File: sql_display.c

package info (click to toggle)
qof 0.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,488 kB
  • sloc: ansic: 30,786; sh: 11,632; xml: 487; makefile: 431; yacc: 184; lex: 123; sed: 16
file content (317 lines) | stat: -rw-r--r-- 6,464 bytes parent folder | download | duplicates (8)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "sql_parser.h"

#define output(fmt,args...) fprintf (stdout, "%*s" fmt "\n" , indent * 2, "" , ##args)

static int sql_display_select (int indent, sql_select_statement * statement);
static int sql_display_field (int indent, sql_field * field);
static int sql_display_order_by (int indent, sql_order_field *order_by);

static int
sql_display_field_item (int indent, sql_field_item * item)
{
	GList *cur;

	switch (item->type) {
	case SQL_name:
		fprintf (stdout, "%*s", indent * 2, "");
		for (cur = item->d.name; cur != NULL; cur = cur->next)
			fprintf (stdout, "%s%s", (char *) cur->data, cur->next ? "." : "\n");
		break;

	case SQL_equation:
		output ("equation: %d", item->d.equation.op);
		output ("left:");
		sql_display_field_item (indent + 1, item->d.equation.left);
		output ("right");
		sql_display_field_item (indent + 1, item->d.equation.right);
		break;

	case SQL_inlineselect:
		output ("select:");
		sql_display_select (indent + 1, item->d.select);
		break;

	case SQL_function:
		output ("function: %s", item->d.function.funcname);
		for (cur = item->d.function.funcarglist; cur != NULL; cur = cur->next)
			sql_display_field (indent + 1, cur->data);
		break;

	}
	return 0;
}

static int
sql_display_field (int indent, sql_field * field)
{
	sql_display_field_item (indent, field->item);

	if (field->as)
		output ("as: %s", field->as);
	return 0;
}

static int
sql_display_condition (int indent, sql_condition * cond)
{
	char *condstr;

	if (!cond)
		return 0;

	switch (cond->op) {
	case SQL_eq:
		condstr = "=";
		break;
	case SQL_is:
		condstr = "IS";
		break;
	case SQL_isnot:
		condstr = "IS NOT";
		break;
	case SQL_in:
		condstr = "IN";
		break;
	case SQL_notin:
		condstr = "NOT IN";
		break;
	case SQL_like:
		condstr = "LIKE";
		break;
	case SQL_gt:
		condstr = ">";
		break;
	case SQL_lt:
		condstr = "<";
		break;
	case SQL_geq:
		condstr = ">=";
		break;
	case SQL_leq:
		condstr = "<=";
		break;
	case SQL_diff:
		condstr = "!=";
		break;
	case SQL_between:
		condstr = "BETWEEN";
		break;
	default:
		condstr = "UNKNOWN OPERATOR! (THIS IS AN ERROR)";
	}

	output ("op: %s", condstr);
	switch (cond->op) {
	case SQL_eq:
	case SQL_is:
	case SQL_isnot:
	case SQL_in:
	case SQL_notin:
	case SQL_like:
	case SQL_gt:
	case SQL_lt:
	case SQL_geq:
	case SQL_leq:
		output ("left:");
		sql_display_field (indent + 1, cond->d.pair.left);
		output ("right:");
		sql_display_field (indent + 1, cond->d.pair.right);
		break;

	case SQL_between:
		output ("field:");
		sql_display_field (indent + 1, cond->d.between.field);
		output ("lower:");
		sql_display_field (indent + 1, cond->d.between.lower);
		output ("upper:");
		sql_display_field (indent + 1, cond->d.between.upper);
		break;
	}

	return 0;
}

static int
sql_display_where (int indent, sql_where * where)
{
	switch (where->type) {
	case SQL_single:
		sql_display_condition (indent + 1, where->d.single);
		break;

	case SQL_negated:
		output ("negated:");
		sql_display_where (indent + 1, where->d.negated);
		break;

	case SQL_pair:
		output ("pair: %d", where->d.pair.op);
		output ("left:");
		sql_display_where (indent + 1, where->d.pair.left);
		output ("right:");
		sql_display_where (indent + 1, where->d.pair.right);
		break;
	}

	return 0;
}

static int
sql_display_table (int indent, sql_table * table)
{
	if (table->join_type != SQL_cross_join) {
		switch (table->join_type) {
		case SQL_inner_join:
			output ("inner join");
			break;
		case SQL_left_join:
			output ("left join");
			break;
		case SQL_right_join:
			output ("right join");
			break;
		case SQL_full_join:
			output ("full join");
			break;
		default:
			break;
		}
	}

	switch (table->type) {
	case SQL_simple:
		output ("table: %s", table->d.simple);
		break;

	case SQL_nestedselect:
		output ("table:");
		sql_display_select (indent + 1, table->d.select);
		break;
	}

	if (table->join_cond) {
		output ("cond:");
		sql_display_where (indent + 1, table->join_cond);
	}

	return 0;
}

static int
sql_display_select (int indent, sql_select_statement * statement)
{
	GList *cur;

	if (statement->distinct)
		output ("distinct");

	output ("fields:");

	for (cur = statement->fields; cur != NULL; cur = cur->next)
		sql_display_field (indent + 1, cur->data);

	output ("from:");

	for (cur = statement->from; cur != NULL; cur = cur->next)
		sql_display_table (indent + 1, cur->data);

	if (statement->where) {
		output ("where:");
		sql_display_where (indent + 1, statement->where);
	}

	if (statement->order)
		output ("order by:");
	for (cur = statement->order; cur != NULL; cur = cur->next)
		sql_display_order_by (indent + 1, cur->data);

	if (statement->group)
		output ("group by:");
	for (cur = statement->group; cur != NULL; cur = cur->next)
		sql_display_field (indent + 1, cur->data);

	return 0;
}

static int 
sql_display_order_by (int indent, sql_order_field *order_by)
{
	GList *walk;
	output ("order by %s", order_by->order_type == SQL_asc ? "ASC": "DESC");
	for (walk = order_by->name; walk; walk = walk->next)
		output ("%s", (gchar*) walk->data);
	return 0;
}

static int
sql_display_insert (int indent, sql_insert_statement * insert)
{
	GList *walk;

	output ("table");
	sql_display_table (indent + 1, insert->table);

	if (insert->fields) {
		output ("fields:");
		for (walk = insert->fields; walk != NULL; walk = walk->next)
			sql_display_field (indent + 1, walk->data);
	}

	output ("values:");
	for (walk = insert->values; walk != NULL; walk = walk->next)
		sql_display_field (indent + 1, walk->data);

	return 0;
}

static int
sql_display_update (int indent, sql_update_statement * update)
{
	GList *walk;

	output ("table:");
	sql_display_table (indent + 1, update->table);

	output ("set:");
	for (walk = update->set; walk != NULL; walk = walk->next) {
		sql_display_condition (indent + 1, walk->data);
	}

	if (update->where) {
		output ("where:");
		sql_display_where (indent + 1, update->where);
	}

	return 0;
	
}

int
sql_display (sql_statement * statement)
{
	int indent = 0;

	output ("query: %s", statement->full_query);
	switch (statement->type) {
	case SQL_select:
		sql_display_select (indent + 1, statement->statement);
		break;

	case SQL_insert:
		sql_display_insert (indent + 1, statement->statement);
		break;

	case SQL_update:
		sql_display_update (indent + 1, statement->statement);
		break;

	default:
		fprintf (stderr, "Unknown statement type: %d", statement->type);
	}

	return 0;
}