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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include "sql_def.h"
#define NSIZE 32
#define VSIZE 1024
#define LSIZE 128
#define AND 0
#define EQ 1
#define LIST 2
#define NE 3
#define OR 4
#define PROD 5
#define PROJ 6
#define SEL 7
#define TABLE 8
#define VALUE 9
#define VALUE_OF 10
char display_type[][10] =
{"AND", "EQ", "LIST", "NE", "OR", "PROD",
"PROJ", "SEL", "TABLE", "VALUE", "VALUE_OF"};
typedef struct node NODE;
struct node
{
int type;
char text[VSIZE];
int number;
NODE *left;
NODE *right;
} node;
struct table
{
char file[12];
int nb_columns;
char columns[LSIZE][NSIZE];
};
struct table tables[3];
int stack[3], sp = 0;
char lines[2][LSIZE][VSIZE];
void init_tables(void)
{
int i;
strcpy(tables[0].file, "tmptab0.txt");
strcpy(tables[1].file, "tmptab1.txt");
strcpy(tables[2].file, "tmptab2.txt");
for (i = 0; i < 3; i++)
stack[i] = i;
sp = 2;
};
void display_line(int n, int nb_columns)
{
int i = 0;
for (i = 0; i < nb_columns - 1; i++)
{
printf("%s", lines[n][i]);
printf("\t");
}
printf("%s", lines[n][i]);
printf("\n");
return;
}
int read_line(FILE *source, int n)
{
int c, i = 0, j = 0;
c = fgetc(source);
if (c == EOF || c == '\n')
return i;
while (c != '\n')
{
if (c == '\t')
{
lines[n][i++][j] = '\0';
j = 0;
}
else
lines[n][i][j++] = c;
c = fgetc(source);
}
lines[n][i][j] = '\0';
return i + 1;
}
void write_line(FILE *target, int n, int nb_columns)
{
int i = 0;
for (i = 0; i < nb_columns - 1; i++)
{
fputs(lines[n][i], target);
fputs("\t", target);
}
fputs(lines[n][i], target);
fputs("\n", target);
return;
}
int is_table(char *table_name)
{
FILE *source;
int nb_columns;
source = fopen("table.txt", "r");
nb_columns = read_line(source, 0);
while (nb_columns && strcmp(lines[0][0], table_name))
nb_columns = read_line(source, 0);
fclose(source);
return nb_columns;
}
int is_column(char *table_name, char *column_name)
{
FILE *source;
int nb_columns;
source = fopen("column.txt", "r");
nb_columns = read_line(source, 0);
while (nb_columns &&
(strcmp(lines[0][0], table_name) || strcmp(lines[0][1], column_name)))
nb_columns = read_line(source, 0);
fclose(source);
return nb_columns;
}
int number_of_column(int stab, char *column_name)
{
int i, nb_columns;
i = 0;
nb_columns = tables[stab].nb_columns;
while (i < nb_columns && strcmp((tables[stab].columns)[i], column_name))
i++;
return i;
}
int number_table(char *table_name)
{
char column_name[NSIZE];
FILE *source;
int nb_columns, ttab, i;
source = fopen("column.txt", "r");
ttab = stack[sp--];
tables[ttab].nb_columns = 0;
nb_columns = read_line(source, 0);
while (nb_columns)
{
if (!strcmp(lines[0][0], table_name))
{
(tables[ttab].nb_columns)++;
strcpy(column_name, table_name);
strcat(column_name, ".");
strcat(column_name, lines[0][1]);
strcpy((tables[ttab].columns)[atoi(lines[0][2]) - 1], column_name);
}
nb_columns = read_line(source, 0);
}
fclose(source);
return ttab;
}
int number_product(int stab1, int stab2)
{
int ttab, i, j;
int nb_columns1, nb_columns2;
ttab = stack[sp--];
nb_columns1 = tables[stab1].nb_columns;
nb_columns2 = tables[stab2].nb_columns;
j = 0;
for (i = 0; i < nb_columns1; i++)
strcpy((tables[ttab].columns)[j++], (tables[stab1].columns)[i]);
for (i = 0; i < nb_columns2; i++)
strcpy((tables[ttab].columns)[j++], (tables[stab2].columns)[i]);
tables[ttab].nb_columns = j;
stack[++sp] = stab1;
stack[++sp] = stab2;
return ttab;
};
void number_term(int stab, NODE *tree)
{
switch (tree->type)
{
case VALUE_OF:
tree->number = number_of_column(stab, tree->text);
return;
case VALUE:
return;
}
}
void number_condition(int stab, NODE *tree)
{
switch (tree->type)
{
case AND:
case OR:
number_condition(stab, tree->left);
number_condition(stab, tree->right);
return;
case EQ:
case NE:
number_term(stab, tree->left);
number_term(stab, tree->right);
return;
}
}
int number_selection(int stab, NODE *condition)
{
number_condition(stab, condition);
return stab;
};
int number_projection(int stab, NODE *list)
{
int i;
while (list != NULL)
{
list->number = number_of_column(stab, list->text);
list = list->left;
}
return stab;
}
int load_table(char *table_name)
{
char file_name[NSIZE];
int ttab;
FILE *source, *target;
int nb_columns;
strcpy(file_name, table_name);
strcat(file_name, ".txt");
source = fopen(file_name, "r");
ttab = stack[sp--];
target = fopen(tables[ttab].file, "w");
nb_columns = read_line(source, 0);
while (nb_columns)
{
write_line(target, 0, nb_columns);
nb_columns = read_line(source, 0);
}
fclose(source);
fclose(target);
return ttab;
}
int product(int stab1, int stab2)
{
int ttab, i;
FILE *source1, *source2, *target;
int nb_columns1, nb_columns2;
ttab = stack[sp--];
source1 = fopen(tables[stab1].file, "r");
source2 = fopen(tables[stab2].file, "r");
target = fopen(tables[ttab].file, "w");
nb_columns1 = read_line(source1, 0);
while (nb_columns1)
{
for (i = 0; i < nb_columns1; i++)
strcpy(lines[1][i], lines[0][i]);
rewind(source2);
nb_columns2 = read_line(source2, 0);
while (nb_columns2)
{
for (i = 0; i < nb_columns2; i++)
strcpy(lines[1][nb_columns1 + i], lines[0][i]);
write_line(target, 1, nb_columns1 + nb_columns2);
nb_columns2 = read_line(source2, 0);
}
nb_columns1 = read_line(source1, 0);
}
stack[++sp] = stab1;
stack[++sp] = stab2;
fclose(source1);
fclose(source2);
fclose(target);
return ttab;
};
char *eval_term(NODE *tree)
{
switch (tree->type)
{
case VALUE_OF:
return lines[0][tree->number];
case VALUE:
return tree->text;
}
}
int eval_condition(NODE *tree)
{
switch (tree->type)
{
case AND:
return eval_condition(tree->left) && eval_condition(tree->right);
case OR:
return eval_condition(tree->left) || eval_condition(tree->right);
case EQ:
return !strcmp(eval_term(tree->left), eval_term(tree->right));
case NE:
return strcmp(eval_term(tree->left), eval_term(tree->right));
}
}
int selection(int stab, NODE *condition)
{
int ttab;
FILE *source, *target;
int nb_columns;
ttab = stack[sp--];
source = fopen(tables[stab].file, "r");
target = fopen(tables[ttab].file, "w");
nb_columns = read_line(source, 0);
while (nb_columns)
{
if (eval_condition(condition))
write_line(target, 0, nb_columns);
nb_columns = read_line(source, 0);
}
stack[++sp] = stab;
fclose(source);
fclose(target);
return ttab;
}
int projection(int stab, NODE *list)
{
int ttab, i;
NODE *tail;
FILE *source, *target;
int nb_columns;
ttab = stack[sp--];
source = fopen(tables[stab].file, "r");
target = fopen(tables[ttab].file, "w");
nb_columns = read_line(source, 0);
while (nb_columns)
{
i = 0;
tail = list;
while (tail != NULL)
{
strcpy(lines[1][i++], lines[0][tail->number]);
tail = tail->left;
}
write_line(target, 1, i);
nb_columns = read_line(source, 0);
}
stack[++sp] = stab;
fclose(source);
fclose(target);
return ttab;
}
void display_answer(int stab)
{
FILE *source;
int nb_columns;
source = fopen(tables[stab].file, "r");
nb_columns = read_line(source, 0);
while (nb_columns)
{
display_line(0, nb_columns);
nb_columns = read_line(source, 0);
}
fclose(source);
}
NODE *cons_tree(int type, char *text, int number, NODE *ltree, NODE *rtree)
{
NODE *tree;
tree = (NODE *) malloc(sizeof(NODE));
tree->type = type;
strcpy(tree->text, text);
tree->number = number;
tree->left = ltree;
tree->right = rtree;
return tree;
};
void print_indent(int indent)
{
int i;
for (i = 0; i < indent; i++)
printf(" ");
}
NODE *display_tree(NODE *tree, int indent)
{
print_indent(indent);
printf("%s", display_type[tree->type]);
switch (tree->type)
{
case AND:
case OR:
case EQ:
case NE:
case PROD:
case PROJ:
case SEL:
printf("\n");
display_tree(tree->left, indent + 1);
display_tree(tree->right, indent + 1);
break;
case LIST:
printf(" %s", tree->text);
printf(" %d", tree->number);
if (tree->left != NULL)
{
printf("\n");
display_tree(tree->left, indent + 1);
}
break;
case TABLE:
printf(" %s\n", tree->text);
break;
case VALUE_OF:
printf(" %s", tree->text);
printf(" %d\n", tree->number);
break;
case VALUE:
printf(" %s\n", tree->text);
break;
}
};
int number_tree(NODE *tree)
{
int stab1, stab2;
switch (tree->type)
{
case PROD:
stab1 = number_tree(tree->left);
stab2 = number_tree(tree->right);
return number_product(stab1, stab2);
case SEL:
return number_selection(number_tree(tree->left), tree->right);
case PROJ:
return number_projection(number_tree(tree->left), tree->right);
case TABLE:
return number_table(tree->text);
}
};
int eval_tree(NODE *tree)
{
int stab1, stab2;
switch (tree->type)
{
case PROD:
stab1 = eval_tree(tree->left);
stab2 = eval_tree(tree->right);
return product(stab1, stab2);
case SEL:
return selection(eval_tree(tree->left), tree->right);
case PROJ:
return projection(eval_tree(tree->left), tree->right);
case TABLE:
return load_table(tree->text);
}
};
void query(char *q, NODE *t)
{
printf("\nQUERY\n%s", q);
printf("TREE\n");
display_tree(t, 0);
printf("\n");
init_tables();
number_tree(t);
init_tables();
printf("ANSWER\n");
display_answer(eval_tree(t));
}
/*int main(void)
{
NODE *t;
t = cons_tree(
SEL,
"",
0,
cons_tree(TABLE, "livre", 0, NULL, NULL),
cons_tree(
EQ,
"",
0,
cons_tree(VALUE_OF, "livre.cote", 0, NULL, NULL),
cons_tree(VALUE, "L2", 0, NULL, NULL)));
query("select * \nfrom personne\nwhere personne.nom = 'Durand';\n", t);
t = cons_tree(
PROJ,
"",
0,
cons_tree(
SEL,
"",
0,
cons_tree(TABLE, "personne", 0, NULL, NULL),
cons_tree(
NE,
"",
0,
cons_tree(VALUE_OF, "personne.ville", 0, NULL, NULL),
cons_tree(VALUE, "Marseille", 0, NULL, NULL))),
cons_tree(
LIST,
"personne.nom",
0,
cons_tree(LIST, "personne.prenom", 0, NULL, NULL),
NULL));
query("select personne.nom, personne.prenom\nfrom personne\nwhere personne.ville <> 'Marseille'\n", t);
t = cons_tree(
PROJ,
"",
0,
cons_tree(
SEL,
"",
0,
cons_tree(
PROD,
"",
0,
cons_tree(TABLE, "personne", 0, NULL, NULL),
cons_tree(TABLE, "livre", 0, NULL, NULL)),
cons_tree(
AND,
"",
0,
cons_tree(
EQ,
"",
0,
cons_tree(VALUE_OF, "livre.auteur", 0, NULL, NULL),
cons_tree(VALUE_OF, "personne.nom", 0, NULL, NULL)),
cons_tree(
OR,
"",
0,
cons_tree(
EQ,
"",
0,
cons_tree(VALUE_OF, "personne.ville", 0, NULL, NULL),
cons_tree(VALUE, "Marseille", 0, NULL, NULL)),
cons_tree(
EQ,
"",
0,
cons_tree(VALUE_OF, "personne.ville", 0, NULL, NULL),
cons_tree(VALUE, "Toulon", 0, NULL, NULL))))),
cons_tree(
LIST,
"livre.titre",
0,
NULL,
NULL));
query("select livre.titre\nfrom personne, livre\nwhere livre.auteur = personne.nom\nand (personne.ville = 'Marseille' or personne.ville = 'Toulon')\n", t);
system("pause");
}
*/ |
Partager