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