· 5 years ago · Apr 01, 2020, 12:26 PM
1#include <math.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#include "datab.h"
7
8t_db *InitializareBazaDeDate(char *name) {
9 // aloc memorie pentru o baza de date
10 t_db *database = (t_db *)calloc(1, sizeof(t_db));
11
12 if (database == NULL) {
13 printf("Allocation failed");
14 exit(1);
15 }
16
17 strcpy(database->name, name);
18 database->tables = NULL;
19
20 return database;
21}
22t_table *AlocaTabel(char *name, size_t dataType) {
23 t_table *table = (t_table *)malloc(sizeof(t_table));
24 if (table == NULL) {
25 printf("Allocation failed");
26 exit(1);
27 }
28
29 table->type = dataType;
30 strcpy(table->name, name);
31 table->lines = NULL;
32 table->columns = NULL;
33 table->next = NULL;
34
35 return table;
36}
37
38t_table *AdaugareTabel(t_db *database, char *name, size_t dataType) {
39 t_table *table = AlocaTabel(name, dataType);
40
41 if (database->tables == NULL) {
42 database->tables = table;
43 database->tables->next = NULL;
44 } else {
45 // table->next = database->tables;
46 // database->tables = table;
47 t_table *p = database->tables;
48
49 while (p->next != NULL) {
50 p = p->next;
51 }
52 p->next = table;
53 p->next->next = NULL;
54 }
55 return table;
56}
57
58t_column *AlocaColoana(char *name) {
59 t_column *column = (t_column *)malloc(sizeof(t_column));
60 if (column == NULL) {
61 printf("Allocation Failed");
62 exit(1);
63 }
64
65 strcpy(column->name, name);
66 column->next = NULL;
67
68 return column;
69}
70
71void AdaugareColoana(t_table *table, char *name) {
72 t_column *column = AlocaColoana(name);
73
74 if (table->columns == NULL) {
75 table->columns = column;
76 table->columns->next = NULL;
77 } else {
78 t_column *p = table->columns;
79
80 while (p->next != NULL) {
81 p = p->next;
82 }
83 p->next = column;
84 p->next->next = NULL;
85 }
86}
87
88t_table *GasesteTabel(t_db *database, char *name) {
89 t_table *table = database->tables;
90 while (table != NULL) {
91 if (strcmp(table->name, name) == 0) {
92 return table;
93 }
94 table = table->next;
95 }
96
97 return NULL;
98}
99
100t_floatLine *AlocaLinieFLOAT() {
101 t_floatLine *line = (t_floatLine *)malloc(sizeof(t_floatLine));
102 line->cells = NULL;
103 line->next = NULL;
104
105 return line;
106}
107
108t_floatCell *AlocaCelulaFLOAT(float value) {
109 t_floatCell *cell = (t_floatCell *)malloc(sizeof(t_floatCell));
110 cell->value = value;
111 cell->next = NULL;
112
113 return cell;
114}
115t_floatLine *AdaugaLinieFLOAT(t_table *table) {
116 t_floatLine *line = (t_floatLine *)malloc(sizeof(t_floatLine));
117 t_floatLine *p;
118 line->cells = NULL;
119 p = table->lines;
120 if (table->lines == NULL) {
121 table->lines = line;
122 line->next = NULL;
123 } else {
124 while (p->next != NULL) {
125 p = p->next;
126 }
127 p->next = line;
128 p->next->next = NULL;
129 }
130 return line;
131}
132void AdaugaCelulaFLOAT(t_floatLine *line, float value) {
133 t_floatCell *cell = AlocaCelulaFLOAT(value);
134
135 t_floatCell *p = line->cells;
136 if (p == NULL) {
137 line->cells = cell;
138 cell->next = NULL;
139 } else {
140 while (p->next != NULL) {
141 p = p->next;
142 }
143 p->next = cell;
144 p->next->next = NULL;
145 }
146}
147void AfiseazaLinieFLOAT(t_floatLine *line) {
148 t_floatCell *cell = line->cells;
149 while (cell != NULL) {
150 printf("%f", cell->value);
151 for (int i = 0; i < 23; i++) printf(" ");
152 cell = cell->next;
153 }
154 printf("\n");
155}
156
157t_intCell *AlocaCelulaINT(int value) {
158 t_intCell *cell = (t_intCell *)malloc(sizeof(t_intCell));
159 cell->next = NULL;
160 cell->value = value;
161
162 return cell;
163}
164
165t_intLine *AlocaLinieINT() {
166 t_intLine *line = (t_intLine *)malloc(sizeof(t_intLine));
167 line->cells = NULL;
168 line->next = NULL;
169
170 return line;
171}
172
173t_intLine *AdaugaLinieINT(t_table *table) {
174 t_intLine *line = AlocaLinieINT();
175 t_intLine *p;
176 p = table->lines;
177 if (table->lines == NULL) {
178 table->lines = line;
179 line->next = NULL;
180 } else {
181 while (p->next != NULL) {
182 p = p->next;
183 }
184 p->next = line;
185 p->next->next = NULL;
186 }
187 return line;
188}
189
190void AdaugaCelulaInt(t_intLine *line, int value) {
191 t_intCell *cell = AlocaCelulaINT(value);
192
193 t_intCell *p = line->cells;
194 if (line->cells == NULL) {
195 line->cells = cell;
196 cell->next = NULL;
197 } else {
198 while (p->next != NULL) {
199 p = p->next;
200 }
201 p->next = cell;
202
203 p->next->next = NULL;
204 }
205}
206void AfiseazaLinieINT(t_intLine *line) {
207 t_intCell *cell = line->cells;
208 while (cell != NULL) {
209 printf("%-30d ", cell->value);
210 cell = cell->next;
211 }
212 printf("\n");
213}
214
215t_stringCell *AlocaCelulaSTRING(char *value) {
216 t_stringCell *cell = (t_stringCell *)malloc(sizeof(t_stringCell));
217 cell->value = value;
218 cell->next = NULL;
219 return cell;
220}
221
222t_stringLine *AlocaLinieSTRING() {
223 t_stringLine *line = (t_stringLine *)malloc(sizeof(t_stringLine));
224 line->cells = NULL;
225 line->next = NULL;
226
227 return line;
228}
229t_stringLine *AdaugaLinieSTRING(t_table *table) {
230 t_stringLine *line = AlocaLinieSTRING();
231 t_stringLine *p;
232 p = table->lines;
233 if (table->lines == NULL) {
234 table->lines = line;
235 line->next = NULL;
236 } else {
237 while (p->next != NULL) {
238 p = p->next;
239 }
240 p->next = line;
241 p->next->next = NULL;
242 }
243
244 return line;
245}
246void AdaugaCelulaSTRING(t_stringLine *line, char *value) {
247 t_stringCell *cell = (t_stringCell *)malloc(sizeof(t_stringCell));
248 cell->value = (char *)malloc(20 * sizeof(char));
249 strcpy(cell->value, value);
250 cell->next = NULL;
251 t_stringCell *p = line->cells;
252 if (line->cells == NULL) {
253 line->cells = cell;
254 cell->next = NULL;
255 } else {
256 while (p->next != NULL) {
257 p = p->next;
258 }
259 p->next = cell;
260 p->next->next = NULL;
261 }
262}
263void AfiseazaLinieSTRING(t_stringLine *line) {
264 t_stringCell *cell = line->cells;
265
266 while (cell != NULL) {
267 printf("%s", cell->value);
268 for (unsigned int j = 0; j < 30 - strlen(cell->value) + 1; j++) {
269 printf(" ");
270 }
271 cell = cell->next;
272 }
273 printf("\n");
274}
275
276int GasesteIndexColoana(t_table *table, char *name_column) {
277 t_column *column = table->columns;
278 int index = 0;
279 while (column != NULL) {
280 index++;
281 if (strcmp(name_column, column->name) == 0) {
282 return index;
283 }
284 column = column->next;
285 }
286 return 0;
287}
288
289t_intCell *GasesteCelulaINT(t_intLine *line, int index) {
290 t_intCell *cell = line->cells;
291 int index_cell = 0;
292 while (cell != NULL) {
293 index_cell++;
294 if (index_cell == index) {
295 return cell;
296 }
297 cell = cell->next;
298 }
299 return NULL;
300}
301t_floatCell *GasesteCelulaFLOAT(t_floatLine *line, int index) {
302 t_floatCell *cell = line->cells;
303 int index_cell = 0;
304 while (cell != NULL) {
305 index_cell++;
306 if (index_cell == index) {
307 return cell;
308 }
309 cell = cell->next;
310 }
311 return NULL;
312}
313t_stringCell *GasesteCelulaSTRING(t_stringLine *line, int index) {
314 t_stringCell *cell = line->cells;
315 int index_cell = 0;
316 while (cell != NULL) {
317 index_cell++;
318 if (index_cell == index) {
319 return cell;
320 }
321 cell = cell->next;
322 }
323 return NULL;
324}
325int VerificareConditieINT(t_intCell *cell, char *relation, char *value) {
326 int int_value = atoi(value);
327 if (strcmp(relation, "==") == 0) {
328 if (int_value == cell->value) {
329 return 1;
330 }
331 }
332 if (strcmp(relation, "<=") == 0) {
333 if (cell->value <= int_value) {
334 return 1;
335 }
336 }
337 if (strcmp(relation, ">=") == 0) {
338 if (cell->value >= int_value) {
339 return 1;
340 }
341 }
342 if (strcmp(relation, "<") == 0) {
343 if (cell->value < int_value) {
344 return 1;
345 }
346 }
347 if (strcmp(relation, ">") == 0) {
348 if (cell->value > int_value) {
349 return 1;
350 }
351 }
352 if (strcmp(relation, "!=") == 0) {
353 if (cell->value != int_value) {
354 return 1;
355 }
356 }
357 return 0;
358}
359float VerificareConditieFLOAT(t_floatCell *cell, char *relation, char *value) {
360 float float_value = atof(value);
361 if (strcmp(relation, "==") == 0) {
362 if (float_value == cell->value) {
363 return 1;
364 }
365 }
366 if (strcmp(relation, "<=") == 0) {
367 if (cell->value <= float_value) {
368 return 1;
369 }
370 }
371 if (strcmp(relation, ">=") == 0) {
372 if (cell->value >= float_value) {
373 return 1;
374 }
375 }
376 if (strcmp(relation, "<") == 0) {
377 if (cell->value < float_value) {
378 return 1;
379 }
380 }
381 if (strcmp(relation, ">") == 0) {
382 if (cell->value > float_value) {
383 return 1;
384 }
385 }
386 if (strcmp(relation, "!=") == 0) {
387 if (cell->value != float_value) {
388 return 1;
389 }
390 }
391 return 0;
392}
393int VerificareConditieSTRING(t_stringCell *cell, char *relation, char *value) {
394 if (strcmp(relation, "==") == 0) {
395 if (strcmp(cell->value, value) == 0) {
396 return 1;
397 }
398 }
399 if (strcmp(relation, "!=") == 0) {
400 if (strcmp(cell->value, value) != 0) {
401 return 1;
402 }
403 }
404 if (strcmp(relation, ">") == 0) {
405 if (strcmp(cell->value, value) > 0) {
406 return 1;
407 }
408 }
409 if (strcmp(relation, "<") == 0) {
410 if (strcmp(cell->value, value) < 0) {
411 return 1;
412 }
413 }
414 if (strcmp(relation, ">=") == 0) {
415 if (strcmp(cell->value, value) >= 0) {
416 return 1;
417 }
418 }
419 if (strcmp(relation, "<=") == 0) {
420 if (strcmp(cell->value, value) <= 0) {
421 return 1;
422 }
423 }
424 return 0;
425}
426void Verificare(t_table *table, char *name_column, char *relation,
427 char *value) {
428 int index;
429
430 if (table->type == INT) {
431 index = GasesteIndexColoana(table, name_column);
432 t_intLine *line = table->lines;
433 t_intCell *cell;
434 while (line != NULL) {
435 cell = GasesteCelulaINT(line, index);
436 if (VerificareConditieINT(cell, relation, value) == 1) {
437 AfiseazaLinieINT(line);
438 }
439 line = line->next;
440 }
441 }
442 if (table->type == FLOAT) {
443 index = GasesteIndexColoana(table, name_column);
444 t_floatLine *line = table->lines;
445 t_floatCell *cell;
446 while (line != NULL) {
447 cell = GasesteCelulaFLOAT(line, index);
448 if (VerificareConditieFLOAT(cell, relation, value) == 1) {
449 AfiseazaLinieFLOAT(line);
450 }
451
452 line = line->next;
453 }
454 }
455 if (table->type == STRING) {
456 index = GasesteIndexColoana(table, name_column);
457 t_stringLine *line = table->lines;
458 t_stringCell *cell;
459 while (line != NULL) {
460 cell = GasesteCelulaSTRING(line, index);
461 if (VerificareConditieSTRING(cell, relation, value) == 1) {
462 AfiseazaLinieSTRING(line);
463 }
464
465 line = line->next;
466 }
467 }
468}
469
470void ElibereazaLinieFLOAT(t_floatLine *line) {
471 t_floatCell *cell = line->cells;
472 t_floatCell *cell_next = cell->next;
473 while (cell != NULL && cell_next != NULL) {
474 free(cell);
475 cell = cell_next;
476 cell_next = cell_next->next;
477 }
478 free(cell);
479
480 line->cells = NULL;
481}
482void ElibereazaLinieSTRING(t_stringLine *line) {
483 t_stringCell *cell = line->cells;
484 t_stringCell *cell_next = cell->next;
485 while (cell != NULL && cell_next != NULL) {
486 free(cell->value);
487 free(cell);
488 cell = cell_next;
489 cell_next = cell_next->next;
490 }
491 free(cell->value);
492 free(cell);
493 line->cells = NULL;
494}
495void ElibereazaLinieINT(t_intLine *line) {
496 t_intCell *cell = line->cells;
497 t_intCell *cell_next = cell->next;
498 while (cell != NULL && cell_next != NULL) {
499 free(cell);
500 cell = cell_next;
501 cell_next = cell_next->next;
502 }
503 free(cell);
504 line->cells = NULL;
505}
506void EliminareLinieINT(t_table *table, int index_line) {
507 if (index_line < 1) {
508 return;
509 }
510 t_intLine *prev = NULL;
511 t_intLine *line = table->lines;
512 t_intLine *lineToBeDeleted = NULL;
513
514 int index = 1;
515 if (index_line == 1) {
516 prev = NULL;
517 lineToBeDeleted = line;
518 line = lineToBeDeleted->next;
519 table->lines = lineToBeDeleted->next;
520 ElibereazaLinieINT(lineToBeDeleted);
521 free(lineToBeDeleted);
522 } else {
523 while (line != NULL && index != index_line) {
524 index++;
525 prev = line;
526 line = line->next;
527 }
528
529 lineToBeDeleted = line;
530 line = lineToBeDeleted->next;
531
532 ElibereazaLinieINT(lineToBeDeleted);
533 free(lineToBeDeleted);
534 prev->next = line;
535 }
536}
537void EliminareLinieFLOAT(t_table *table, int index_line) {
538 if (index_line < 1) {
539 return;
540 }
541 t_floatLine *prev = NULL;
542 t_floatLine *line = table->lines;
543 t_floatLine *lineToBeDeleted = NULL;
544
545 int index = 1;
546 if (index_line == 1) {
547 prev = NULL;
548 lineToBeDeleted = line;
549 line = lineToBeDeleted->next;
550 table->lines = line;
551 ElibereazaLinieFLOAT(lineToBeDeleted);
552 free(lineToBeDeleted);
553 } else {
554 while (line != NULL && index != index_line) {
555 index++;
556 prev = line;
557 line = line->next;
558 }
559
560 lineToBeDeleted = line;
561 line = lineToBeDeleted->next;
562
563 ElibereazaLinieFLOAT(lineToBeDeleted);
564 free(lineToBeDeleted);
565 prev->next = line;
566 }
567}
568void EliminareLinieSTRING(t_table *table, int index_line) {
569 if (index_line < 1) {
570 return;
571 }
572 t_stringLine *prev = NULL;
573 t_stringLine *line = table->lines;
574 t_stringLine *lineToBeDeleted = NULL;
575
576 int index = 1;
577 if (index_line == 1) {
578 prev = NULL;
579 lineToBeDeleted = line;
580 line = lineToBeDeleted->next;
581 table->lines = lineToBeDeleted->next;
582 ElibereazaLinieSTRING(lineToBeDeleted);
583 free(lineToBeDeleted);
584 } else {
585 while (line != NULL && index != index_line) {
586 index++;
587 prev = line;
588 line = line->next;
589 }
590
591 lineToBeDeleted = line;
592 line = lineToBeDeleted->next;
593
594 ElibereazaLinieSTRING(lineToBeDeleted);
595 free(lineToBeDeleted);
596 prev->next = line;
597 }
598}
599
600void VerificareEliminare(t_table *table, char *name_column, char *relation,
601 char *value) {
602 int index;
603 int index_line = 0;
604 index = GasesteIndexColoana(table, name_column);
605 if (table->type == INT) {
606 t_intLine *line = table->lines;
607 t_intCell *cell;
608
609 while (line != NULL) {
610 index_line++;
611 cell = GasesteCelulaINT(line, index);
612 if (VerificareConditieINT(cell, relation, value) == 1) {
613 line = line->next;
614 EliminareLinieINT(table, index_line);
615 index_line = index_line - 1;
616 } else {
617 line = line->next;
618 }
619 }
620 }
621 if (table->type == FLOAT) {
622 t_floatLine *line = table->lines;
623 t_floatCell *cell;
624
625 while (line != NULL) {
626 index_line++;
627 cell = GasesteCelulaFLOAT(line, index);
628 if (VerificareConditieFLOAT(cell, relation, value) == 1) {
629 line = line->next;
630 EliminareLinieFLOAT(table, index_line);
631 index_line = index_line - 1;
632 } else {
633 line = line->next;
634 }
635 }
636 }
637 if (table->type == STRING) {
638 t_stringLine *line = table->lines;
639 t_stringCell *cell;
640
641 while (line != NULL) {
642 index_line++;
643 cell = GasesteCelulaSTRING(line, index);
644 if (VerificareConditieSTRING(cell, relation, value) == 1) {
645 line = line->next;
646 EliminareLinieSTRING(table, index_line);
647 index_line = index_line - 1;
648 } else {
649 line = line->next;
650 }
651 }
652 }
653}
654void AfisareColoane(t_table *table) {
655 printf("TABLE: %s\n", table->name);
656 int count = 0;
657 t_column *column = table->columns;
658 while (column != NULL) {
659 printf("%s", column->name);
660 for (unsigned int i = 0; i < 30 - strlen(column->name) + 1; i++) {
661 printf(" ");
662 }
663 count++;
664 column = column->next;
665 }
666 printf("\n");
667 int i = 0;
668 while (i < count) {
669 printf("------------------------------ ");
670 i++;
671 }
672 printf("\n");
673}
674
675void AfisareTabel(t_table *table) {
676 AfisareColoane(table);
677 if (table == NULL) {
678 return;
679 }
680 if (table->type == INT) {
681 int count_lines = 0;
682 t_intLine *line = table->lines;
683 while (line != NULL) {
684 count_lines++;
685 AfiseazaLinieINT(line);
686 line = line->next;
687 }
688 }
689 if (table->type == FLOAT) {
690 int count_lines = 0;
691 t_floatLine *line = table->lines;
692 while (line != NULL) {
693 count_lines++;
694 AfiseazaLinieFLOAT(line);
695 line = line->next;
696 }
697 }
698 if (table->type == STRING) {
699 int count_lines = 0;
700 t_stringLine *line = table->lines;
701 while (line != NULL) {
702 count_lines++;
703 AfiseazaLinieSTRING(line);
704 line = line->next;
705 }
706 }
707 printf("\n");
708}
709void AfisareBazaDeDate(t_db *database) {
710 printf("DATABASE: ");
711 printf("%s\n\n", database->name);
712 t_table *p = database->tables;
713 while (p != NULL) {
714 AfisareTabel(p);
715 p = p->next;
716 }
717}
718int VerificaExistentaColoana(t_table *table, char *name_column) {
719 t_column *column = table->columns;
720 int found = 0;
721 while (column != NULL) {
722 if (strcmp(column->name, name_column) == 0) {
723 found = 1;
724 }
725 column = column->next;
726 }
727 return found;
728}
729void EliberareLinii(t_table *table) {
730 if (table->type == INT) {
731 t_intLine *line = table->lines;
732 t_intLine *lineToBeDeleted;
733 while (line != NULL) {
734 lineToBeDeleted = line;
735 line = lineToBeDeleted->next;
736 ElibereazaLinieINT(lineToBeDeleted);
737 free(lineToBeDeleted);
738 }
739 table->lines = NULL;
740 }
741 if (table->type == FLOAT) {
742 t_floatLine *line = table->lines;
743 t_floatLine *lineToBeDeleted;
744 while (line != NULL) {
745 lineToBeDeleted = line;
746 line = lineToBeDeleted->next;
747 ElibereazaLinieFLOAT(lineToBeDeleted);
748 free(lineToBeDeleted);
749 }
750 table->lines = NULL;
751 }
752 if (table->type == STRING) {
753 t_stringLine *line = table->lines;
754 t_stringLine *lineToBeDeleted;
755 while (line != NULL) {
756 lineToBeDeleted = line;
757 line = lineToBeDeleted->next;
758 ElibereazaLinieSTRING(lineToBeDeleted);
759 free(lineToBeDeleted);
760 }
761 table->lines = NULL;
762 }
763}
764void EliberareColoane(t_table *table) {
765 t_column *column = table->columns;
766 t_column *column_next = column->next;
767 while (column != NULL && column_next != NULL) {
768 free(column);
769 column = column_next;
770 column_next = column_next->next;
771 }
772 free(column);
773 table->columns = NULL;
774}
775void StergeTabel(t_db *database, char *name_table) {
776 t_table *table = database->tables;
777 t_table *prev = NULL;
778 t_table *tableToBeDeleted = NULL;
779
780 if (database->tables == NULL) {
781 return;
782 }
783 if (strcmp(name_table, database->tables->name) == 0) {
784 table = database->tables;
785 database->tables = database->tables->next;
786 EliberareColoane(table);
787 EliberareLinii(table);
788 return;
789 }
790 while (table != NULL) {
791 if (strcmp(name_table, table->name) == 0) {
792 tableToBeDeleted = table;
793 table = tableToBeDeleted->next;
794 EliberareColoane(tableToBeDeleted);
795 EliberareLinii(tableToBeDeleted);
796 free(tableToBeDeleted->lines);
797 prev->next = table;
798 return;
799 }
800
801 prev = table;
802 table = table->next;
803 }
804}
805void EliberareBazaDeDate(t_db *database) {
806 t_table *table = database->tables;
807 t_table *tableToBeDeleted = NULL;
808 while (table != NULL) {
809 tableToBeDeleted = table;
810 table = tableToBeDeleted->next;
811 StergeTabel(database, tableToBeDeleted->name);
812 free(tableToBeDeleted);
813 }
814 free(database);
815}
816
817int main() {
818 char c = '"';
819 char *input = calloc(300, sizeof(char));
820 char *name = calloc(30, sizeof(char));
821 char *command = calloc(30, sizeof(char));
822 char *dataType = calloc(30, sizeof(char));
823 char *column = calloc(100, sizeof(char));
824
825 t_db *database;
826 char *token;
827 t_table *table;
828 while (1) {
829 fgets(input, 200, stdin);
830 strtok(input, "\n");
831 token = strtok(input, " ");
832 strcpy(command, token);
833 command[strlen(command)] = '\0';
834 token = strtok(NULL, " ");
835 if (token != NULL) {
836 strcpy(name, token);
837 name[strlen(name)] = '\0';
838 }
839
840 if (strcmp(command, "INIT_DB") == 0) {
841 database = InitializareBazaDeDate(name);
842 } else if (strcmp(command, "CREATE") == 0) {
843 token = strtok(NULL, " ");
844 strcpy(dataType, token);
845
846 t_table *table_found = GasesteTabel(database, name);
847 if (table_found == NULL) {
848 if (strcmp(dataType, "INT") == 0) {
849 size_t type = INT;
850 table = AdaugareTabel(database, name, type);
851 } else if (strcmp(dataType, "FLOAT") == 0) {
852 size_t type = FLOAT;
853 table = AdaugareTabel(database, name, type);
854 } else if (strcmp(dataType, "STRING") == 0) {
855 size_t type = STRING;
856 table = AdaugareTabel(database, name, type);
857 } else {
858 printf("Unknown data type: %c%s%c\n", c, dataType, c);
859 }
860 token = strtok(NULL, " ");
861
862 while (token != NULL) {
863 strcpy(column, token);
864 AdaugareColoana(table, column);
865 token = strtok(NULL, " ");
866 }
867 } else {
868 printf("Table %c%s%c already exists.", c, name, c);
869 printf("\n");
870 }
871 } else if (strcmp(command, "ADD") == 0) {
872 t_table *table_found;
873
874 table_found = GasesteTabel(database, name);
875 token = strtok(NULL, " ");
876
877 if (table_found != NULL) {
878 if (table_found->type == STRING) {
879 t_stringLine *line;
880 line = AdaugaLinieSTRING(table_found);
881
882 while (token != NULL) {
883 char *cell = token;
884 cell[strlen(cell)] = '\0';
885 AdaugaCelulaSTRING(line, cell);
886 token = strtok(NULL, " ");
887 }
888 }
889 if (table_found->type == INT) {
890 t_intLine *line;
891 line = AdaugaLinieINT(table_found);
892
893 while (token != NULL) {
894 int cell = atoi(token);
895 AdaugaCelulaInt(line, cell);
896 token = strtok(NULL, " ");
897 }
898 }
899 if (table_found->type == FLOAT) {
900 t_floatLine *line;
901 line = AdaugaLinieFLOAT(table_found);
902
903 while (token != NULL) {
904 float cell = atof(token);
905 AdaugaCelulaFLOAT(line, cell);
906 token = strtok(NULL, " ");
907 }
908 }
909 } else {
910 printf("Table %c%s%c not found in database.\n", c, name, c);
911 }
912 } else if (strcmp(command, "SEARCH") == 0) {
913 token = strtok(NULL, " ");
914 char name_column[20];
915 strcpy(name_column, token);
916 token = strtok(NULL, " ");
917 char relation[20];
918 strcpy(relation, token);
919 token = strtok(NULL, " ");
920 char value[20];
921 strcpy(value, token);
922 int found;
923 t_table *table_found = GasesteTabel(database, name);
924 if (table_found != NULL) {
925 found = VerificaExistentaColoana(table_found, name_column);
926 if (found == 1) {
927 AfisareColoane(table_found);
928 Verificare(table_found, name_column, relation, value);
929 printf("\n");
930 } else {
931 printf("Table %c%s%c does not contain column %c%s%c.\n", c,
932 table_found->name, c, c, name_column, c);
933 }
934 } else {
935 printf("Table %c%s%c not found in database.\n", c, name, c);
936 }
937 } else if (strcmp(command, "DELETE") == 0) {
938 t_table *table_found = GasesteTabel(database, name);
939 if (table_found == NULL) {
940 printf("Table %c%s%c not found in database.\n", c, name, c);
941 } else {
942 token = strtok(NULL, " ");
943 if (token == NULL) {
944 StergeTabel(database, name);
945 free(table_found);
946 } else {
947 char name_column[20];
948 strcpy(name_column, token);
949 token = strtok(NULL, " ");
950 char relation[20];
951 strcpy(relation, token);
952 token = strtok(NULL, " ");
953 char value[20];
954 strcpy(value, token);
955
956 int found =
957 VerificaExistentaColoana(table_found, name_column);
958 if (found == 0) {
959 printf("Table %c%s%c does not contain column %c%s%c.\n",
960 c, table_found->name, c, c, name_column, c);
961 } else {
962 VerificareEliminare(table_found, name_column, relation,
963 value);
964 }
965 }
966 }
967 } else if (strcmp(command, "CLEAR") == 0) {
968 t_table *table_found = GasesteTabel(database, name);
969 EliberareLinii(table_found);
970 } else if (strcmp(command, "PRINT") == 0) {
971 t_table *table_found;
972 table_found = GasesteTabel(database, name);
973 if (table_found == NULL) {
974 printf("Table %c%s%c not found in database.\n", c, name, c);
975 } else {
976 AfisareTabel(table_found);
977 }
978 } else if (strcmp(command, "PRINT_DB") == 0) {
979 AfisareBazaDeDate(database);
980 } else if (strcmp(command, "DELETE_DB") == 0) {
981 EliberareBazaDeDate(database);
982 break;
983 } else {
984 printf("Unknown command: %c%s%c\n", c, command, c);
985 }
986 }
987 free(input);
988 free(name);
989 free(command);
990 free(dataType);
991 free(column);
992}