· 5 years ago · Mar 31, 2020, 09:54 PM
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <math.h>
5#include "datab.h"
6
7t_db *InitializareBazaDeDate(char *name)
8{
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 {
14 printf("Allocation failed");
15 exit(1);
16 }
17
18 strcpy(database->name, name);
19 database->tables = NULL;
20
21 return database;
22}
23t_table *AlocaTabel(char *name, size_t dataType)
24{
25 t_table *table = (t_table *)malloc(sizeof(t_table));
26 if (table == NULL)
27 {
28 printf("Allocation failed");
29 exit(1);
30 }
31
32 table->type = dataType;
33 strcpy(table->name, name);
34 table->lines = NULL;
35 table->columns = NULL;
36 table->next = NULL;
37
38 return table;
39}
40
41t_table *AdaugareTabel(t_db *database, char *name, size_t dataType)
42{
43 t_table *table = AlocaTabel(name, dataType);
44
45 if (database->tables == NULL)
46 {
47 database->tables = table;
48 database->tables->next = NULL;
49 }
50 else
51 {
52 // table->next = database->tables;
53 // database->tables = table;
54 t_table *p = database->tables;
55
56 while (p->next != NULL)
57 {
58 p = p->next;
59 }
60 p->next = table;
61 p->next->next = NULL;
62 }
63 return table;
64}
65
66t_column *AlocaColoana(t_table *table, char *name)
67{
68 t_column *column = (t_column *)malloc(sizeof(t_column));
69 if (column == NULL)
70 {
71 printf("Allocation Failed");
72 exit(1);
73 }
74
75 strcpy(column->name, name);
76 column->next = NULL;
77
78 return column;
79}
80
81void AdaugareColoana(t_table *table, char *name)
82{
83 t_column *column = AlocaColoana(table, name);
84
85 if (table->columns == NULL)
86 {
87 table->columns = column;
88 table->columns->next = NULL;
89 }
90 else
91 {
92 t_column *p = table->columns;
93
94 while (p->next != NULL)
95 {
96 p = p->next;
97 }
98 p->next = column;
99 p->next->next = NULL;
100 }
101}
102
103t_table *GasesteTabel(t_db *database, char *name)
104{
105 t_table *table = database->tables;
106 while (table != NULL)
107 {
108 if (strcmp(table->name, name) == 0)
109 {
110 return table;
111 }
112 table = table->next;
113 }
114
115 return NULL;
116}
117
118t_floatLine *AlocaLinieFLOAT()
119{
120 t_floatLine *line = (t_floatLine *)malloc(sizeof(t_floatLine));
121 line->cells = NULL;
122 line->next = NULL;
123
124 return line;
125}
126
127t_floatCell *AlocaCelulaFLOAT(float value)
128{
129 t_floatCell *cell = (t_floatCell *)malloc(sizeof(t_floatCell));
130 cell->value = value;
131 cell->next = NULL;
132
133 return cell;
134}
135t_floatLine *AdaugaLinieFLOAT(t_table *table)
136{
137 t_floatLine *line = (t_floatLine *)malloc(sizeof(t_floatLine));
138 t_floatLine *p;
139 line->cells = NULL;
140 p = table->lines;
141 if (table->lines == NULL)
142 {
143 table->lines = line;
144 //table->lines
145 line->next = NULL;
146 }
147 else
148 {
149 while (p->next != NULL)
150 {
151 p = p->next;
152 }
153 p->next = line;
154 p->next->next = NULL;
155 }
156 return line;
157}
158void AdaugaCelulaFLOAT(t_floatLine *line, float value)
159{
160 t_floatCell *cell = AlocaCelulaFLOAT(value);
161
162 t_floatCell *p = line->cells;
163 if (p == NULL)
164 {
165 line->cells = cell;
166 cell->next = NULL;
167 }
168 else
169 {
170 // cell->next = line->cells;
171 // line->cells = cell;
172 while (p->next != NULL)
173 {
174 p = p->next;
175 }
176 p->next = cell;
177 p->next->next = NULL;
178 }
179}
180void AfiseazaLinieFLOAT(t_floatLine *line)
181{
182 t_floatCell *cell = line->cells;
183 while (cell != NULL)
184 {
185 printf("%f", cell->value);
186 for (int i = 0; i < 23; i++)
187 printf(" ");
188 cell = cell->next;
189 }
190 printf("\n");
191}
192
193t_intCell *AlocaCelulaINT(int value)
194{
195 t_intCell *cell = (t_intCell *)malloc(sizeof(t_intCell));
196 cell->next = NULL;
197 cell->value = value;
198
199 return cell;
200}
201
202t_intLine *AlocaLinieINT()
203{
204 t_intLine *line = (t_intLine *)malloc(sizeof(t_intLine));
205 line->cells = NULL;
206 line->next = NULL;
207
208 return line;
209}
210
211t_intLine *AdaugaLinieINT(t_table *table)
212{
213 t_intLine *line = AlocaLinieINT();
214 t_intLine *p;
215 p = table->lines;
216 if (table->lines == NULL)
217 {
218 table->lines = line;
219 line->next = NULL;
220 }
221 else
222 {
223 while (p->next != NULL)
224 {
225 p = p->next;
226 }
227 p->next = line;
228 p->next->next = NULL;
229 }
230 return line;
231}
232
233void AdaugaCelulaInt(t_intLine *line, int value)
234{
235 t_intCell *cell = AlocaCelulaINT(value);
236
237 t_intCell *p = line->cells;
238 if (line->cells == NULL)
239 {
240 line->cells = cell;
241 cell->next = NULL;
242 }
243 else
244 {
245 while (p->next != NULL)
246 {
247 p = p->next;
248 }
249 p->next = cell;
250
251 p->next->next = NULL;
252 }
253}
254void AfiseazaLinieINT(t_intLine *line)
255{
256
257 t_intCell *cell = line->cells;
258 while (cell != NULL)
259 {
260 printf("%-30d ", cell->value);
261 cell = cell->next;
262 }
263 printf("\n");
264}
265
266t_stringCell *AlocaCelulaSTRING(char *value)
267{
268 t_stringCell *cell = (t_stringCell *)malloc(sizeof(t_stringCell));
269 cell->value = value;
270 cell->next = NULL;
271 return cell;
272}
273
274t_stringLine *AlocaLinieSTRING()
275{
276 t_stringLine *line = (t_stringLine *)malloc(sizeof(t_stringLine));
277 line->cells = NULL;
278 line->next = NULL;
279
280 return line;
281}
282t_stringLine *AdaugaLinieSTRING(t_table *table)
283{
284 t_stringLine *line = AlocaLinieSTRING();
285 t_stringLine *p;
286 p = table->lines;
287 if (table->lines == NULL)
288 {
289 table->lines = line;
290 line->next = NULL;
291 }
292 else
293 {
294 while (p->next != NULL)
295 {
296 p = p->next;
297 }
298 p->next = line;
299 p->next->next = NULL;
300 }
301
302 return line;
303}
304void AdaugaCelulaSTRING(t_stringLine *line, char *value)
305{
306
307 // t_stringCell *cell = AlocaCelulaSTRING(value);
308 t_stringCell *cell = (t_stringCell *)malloc(sizeof(t_stringCell));
309 cell->value = (char *)malloc(20 * sizeof(char));
310 strcpy(cell->value, value);
311 cell->next = NULL;
312 t_stringCell *p = line->cells;
313 if (line->cells == NULL)
314 {
315 line->cells = cell;
316 cell->next = NULL;
317 }
318 else
319 {
320 // cell->next = line->cells;
321 // line->cells = cell;
322 while (p->next != NULL)
323 {
324 p = p->next;
325 }
326 p->next = cell;
327 p->next->next = NULL;
328 }
329}
330void AfiseazaLinieSTRING(t_stringLine *line)
331{
332 t_stringCell *cell = line->cells;
333
334 while (cell != NULL)
335 {
336 printf("%s", cell->value);
337 for (unsigned int j = 0; j < 30 - strlen(cell->value) + 1; j++)
338 {
339 printf(" ");
340 }
341 cell = cell->next;
342 }
343 printf("\n");
344}
345
346int GasesteIndexColoana(t_table *table, char *name_column)
347{
348 t_column *column = table->columns;
349 int index = 0;
350 while (column != NULL)
351 {
352 index++;
353 if (strcmp(name_column, column->name) == 0)
354 {
355 return index;
356 }
357 column = column->next;
358 }
359 return 0;
360}
361
362t_intCell *GasesteCelulaINT(t_intLine *line, int index)
363{
364 t_intCell *cell = line->cells;
365 int index_cell = 0;
366 while (cell != NULL)
367 {
368 index_cell++;
369 if (index_cell == index)
370 {
371 return cell;
372 }
373 cell = cell->next;
374 }
375 return NULL;
376}
377t_floatCell *GasesteCelulaFLOAT(t_floatLine *line, int index)
378{
379 t_floatCell *cell = line->cells;
380 int index_cell = 0;
381 while (cell != NULL)
382 {
383 index_cell++;
384 if (index_cell == index)
385 {
386 return cell;
387 }
388 cell = cell->next;
389 }
390 return NULL;
391}
392t_stringCell *GasesteCelulaSTRING(t_stringLine *line, int index)
393{
394 t_stringCell *cell = line->cells;
395 int index_cell = 0;
396 while (cell != NULL)
397 {
398 index_cell++;
399 if (index_cell == index)
400 {
401 return cell;
402 }
403 cell = cell->next;
404 }
405 return NULL;
406}
407int VerificareConditieINT(t_intCell *cell, char *relation, char *value)
408{
409 int int_value = atoi(value);
410 if (strcmp(relation, "==") == 0)
411 {
412 if (int_value == cell->value)
413 {
414 return 1;
415 }
416 }
417 if (strcmp(relation, "<=") == 0)
418 {
419 if (cell->value <= int_value)
420 {
421 return 1;
422 }
423 }
424 if (strcmp(relation, ">=") == 0)
425 {
426 if (cell->value >= int_value)
427 {
428 return 1;
429 }
430 }
431 if (strcmp(relation, "<") == 0)
432 {
433 if (cell->value < int_value)
434 {
435 return 1;
436 }
437 }
438 if (strcmp(relation, ">") == 0)
439 {
440 if (cell->value > int_value)
441 {
442 return 1;
443 }
444 }
445 if (strcmp(relation, "!=") == 0)
446 {
447 if (cell->value != int_value)
448 {
449 return 1;
450 }
451 }
452 return 0;
453}
454float VerificareConditieFLOAT(t_floatCell *cell, char *relation, char *value)
455{
456 float float_value = atof(value);
457 if (strcmp(relation, "==") == 0)
458 {
459 if (float_value == cell->value)
460 {
461 return 1;
462 }
463 }
464 if (strcmp(relation, "<=") == 0)
465 {
466 if (cell->value <= float_value)
467 {
468 return 1;
469 }
470 }
471 if (strcmp(relation, ">=") == 0)
472 {
473 if (cell->value >= float_value)
474 {
475 return 1;
476 }
477 }
478 if (strcmp(relation, "<") == 0)
479 {
480 if (cell->value < float_value)
481 {
482 return 1;
483 }
484 }
485 if (strcmp(relation, ">") == 0)
486 {
487 if (cell->value > float_value)
488 {
489 return 1;
490 }
491 }
492 if (strcmp(relation, "!=") == 0)
493 {
494 if (cell->value != float_value)
495 {
496 return 1;
497 }
498 }
499 return 0;
500}
501int VerificareConditieSTRING(t_stringCell *cell, char *relation, char *value)
502{
503 if (strcmp(relation, "==") == 0)
504 {
505 if (strcmp(cell->value, value) == 0)
506 {
507 return 1;
508 }
509 }
510 if (strcmp(relation, "!=") == 0)
511 {
512 if (strcmp(cell->value, value) != 0)
513 {
514 return 1;
515 }
516 }
517 if (strcmp(relation, ">") == 0)
518 {
519 if (strcmp(cell->value, value) > 0)
520 {
521 return 1;
522 }
523 }
524 if (strcmp(relation, "<") == 0)
525 {
526 if (strcmp(cell->value, value) < 0)
527 {
528 return 1;
529 }
530 }
531 if (strcmp(relation, ">=") == 0)
532 {
533 if (strcmp(cell->value, value) >= 0)
534 {
535 return 1;
536 }
537 }
538 if (strcmp(relation, "<=") == 0)
539 {
540 if (strcmp(cell->value, value) <= 0)
541 {
542 return 1;
543 }
544 }
545 return 0;
546}
547void Verificare(t_table *table, char *name_column, char *relation, char *value)
548{
549 int index;
550
551 if (table->type == INT)
552 {
553 index = GasesteIndexColoana(table, name_column);
554 t_intLine *line = table->lines;
555 t_intCell *cell;
556 while (line != NULL)
557 {
558 cell = GasesteCelulaINT(line, index);
559 if (VerificareConditieINT(cell, relation, value) == 1)
560 {
561
562 AfiseazaLinieINT(line);
563 }
564 line = line->next;
565 }
566 }
567 if (table->type == FLOAT)
568 {
569 index = GasesteIndexColoana(table, name_column);
570 t_floatLine *line = table->lines;
571 t_floatCell *cell;
572 while (line != NULL)
573 {
574 cell = GasesteCelulaFLOAT(line, index);
575 if (VerificareConditieFLOAT(cell, relation, value) == 1)
576 {
577
578 AfiseazaLinieFLOAT(line);
579 }
580
581 line = line->next;
582 }
583 }
584 if (table->type == STRING)
585 {
586 index = GasesteIndexColoana(table, name_column);
587 t_stringLine *line = table->lines;
588 t_stringCell *cell;
589 while (line != NULL)
590 {
591 cell = GasesteCelulaSTRING(line, index);
592 if (VerificareConditieSTRING(cell, relation, value) == 1)
593 {
594 AfiseazaLinieSTRING(line);
595 }
596
597 line = line->next;
598 }
599 }
600}
601
602void ElibereazaLinieFLOAT(t_floatLine *line)
603{
604 t_floatCell *cell = line->cells;
605 t_floatCell *cell_next = cell->next;
606 while (cell != NULL && cell_next != NULL)
607 {
608 free(cell);
609 cell = cell_next;
610 cell_next = cell_next->next;
611 }
612 free(cell);
613
614 line->cells = NULL;
615}
616void ElibereazaLinieSTRING(t_stringLine *line)
617{
618 t_stringCell *cell = line->cells;
619 t_stringCell *cell_next = cell->next;
620 while (cell != NULL && cell_next != NULL)
621 {
622 free(cell->value);
623 free(cell);
624 cell = cell_next;
625 cell_next = cell_next->next;
626 }
627 free(cell->value);
628 free(cell);
629 line->cells = NULL;
630}
631void ElibereazaLinieINT(t_intLine *line)
632{
633 t_intCell *cell = line->cells;
634 t_intCell *cell_next = cell->next;
635 while (cell != NULL && cell_next != NULL)
636 {
637 free(cell);
638 cell = cell_next;
639 cell_next = cell_next->next;
640 }
641 free(cell);
642 line->cells = NULL;
643}
644void EliminareLinieINT(t_table *table, int index_line)
645{
646
647 if (index_line < 1)
648 {
649 return;
650 }
651 t_intLine *prev = NULL;
652 t_intLine *line = table->lines;
653 t_intLine *lineToBeDeleted = NULL;
654
655 int index = 1;
656 if (index_line == 1)
657 {
658 prev = NULL;
659 lineToBeDeleted = line;
660 line = lineToBeDeleted->next;
661 table->lines = lineToBeDeleted->next;
662 ElibereazaLinieINT(lineToBeDeleted);
663 free(lineToBeDeleted);
664 }
665 else
666 {
667 while (line != NULL && index != index_line)
668 {
669 index++;
670 prev = line;
671 line = line->next;
672 }
673
674 lineToBeDeleted = line;
675 line = lineToBeDeleted->next;
676
677 ElibereazaLinieINT(lineToBeDeleted);
678 free(lineToBeDeleted->cells);
679 free(lineToBeDeleted);
680 prev->next = line;
681 }
682}
683void EliminareLinieFLOAT(t_table *table, int index_line)
684{
685
686 if (index_line < 1)
687 {
688 return;
689 }
690 t_floatLine *prev = NULL;
691 t_floatLine *line = table->lines;
692 t_floatLine *lineToBeDeleted = NULL;
693
694 int index = 1;
695 if (index_line == 1)
696 {
697 prev = NULL;
698 lineToBeDeleted = line;
699 line = lineToBeDeleted->next;
700 table->lines = line;
701 ElibereazaLinieFLOAT(lineToBeDeleted);
702 free(lineToBeDeleted->cells);
703 free(lineToBeDeleted);
704 }
705 else
706 {
707 while (line != NULL && index != index_line)
708 {
709 index++;
710 prev = line;
711 line = line->next;
712 }
713
714 lineToBeDeleted = line;
715 line = lineToBeDeleted->next;
716
717 ElibereazaLinieFLOAT(lineToBeDeleted);
718 free(lineToBeDeleted->cells);
719 free(lineToBeDeleted);
720 prev->next = line;
721 }
722}
723void EliminareLinieSTRING(t_table *table, int index_line)
724{
725
726 if (index_line < 1)
727 {
728 return;
729 }
730 t_stringLine *prev = NULL;
731 t_stringLine *line = table->lines;
732 t_stringLine *lineToBeDeleted = NULL;
733
734 int index = 1;
735 if (index_line == 1)
736 {
737 prev = NULL;
738 lineToBeDeleted = line;
739 line = lineToBeDeleted->next;
740 table->lines = lineToBeDeleted->next;
741 ElibereazaLinieSTRING(lineToBeDeleted);
742 free(lineToBeDeleted);
743 }
744 else
745 {
746 while (line != NULL && index != index_line)
747 {
748 index++;
749 prev = line;
750 line = line->next;
751 }
752
753 lineToBeDeleted = line;
754 line = lineToBeDeleted->next;
755
756 ElibereazaLinieSTRING(lineToBeDeleted);
757 free(lineToBeDeleted->cells);
758 free(lineToBeDeleted);
759 prev->next = line;
760 }
761}
762
763void VerificareEliminare(t_table *table, char *name_column, char *relation, char *value)
764{
765 int index;
766 int index_line = 0;
767 index = GasesteIndexColoana(table, name_column);
768 if (table->type == INT)
769 {
770 t_intLine *line = table->lines;
771 t_intCell *cell;
772
773 while (line != NULL)
774 {
775 index_line++;
776 cell = GasesteCelulaINT(line, index);
777 if (VerificareConditieINT(cell, relation, value) == 1)
778 {
779 EliminareLinieINT(table, index_line);
780 index_line = index_line - 1;
781 }
782
783 line = line->next;
784 }
785 }
786 if (table->type == FLOAT)
787 {
788 t_floatLine *line = table->lines;
789 t_floatCell *cell;
790
791 while (line != NULL)
792 {
793 index_line++;
794 cell = GasesteCelulaFLOAT(line, index);
795 if (VerificareConditieFLOAT(cell, relation, value) == 1)
796 {
797
798 EliminareLinieFLOAT(table, index_line);
799 index_line = index_line - 1;
800 }
801
802 line = line->next;
803 }
804 }
805 if (table->type == STRING)
806 {
807 t_stringLine *line = table->lines;
808 t_stringCell *cell;
809
810 while (line != NULL)
811 {
812 index_line++;
813 cell = GasesteCelulaSTRING(line, index);
814 if (VerificareConditieSTRING(cell, relation, value) == 1)
815 {
816
817 EliminareLinieSTRING(table, index_line);
818 index_line = index_line - 1;
819 }
820
821 line = line->next;
822 }
823 }
824}
825void AfisareColoane(t_table *table)
826{
827 printf("TABLE: %s\n", table->name);
828 int count = 0;
829 t_column *column = table->columns;
830 while (column != NULL)
831 {
832 printf("%s", column->name);
833 for (unsigned int i = 0; i < 30 - strlen(column->name) + 1; i++)
834 {
835 printf(" ");
836 }
837 count++;
838 column = column->next;
839 }
840 printf("\n");
841 int i = 0;
842 while (i < count)
843 {
844 printf("------------------------------ ");
845 i++;
846 }
847 printf("\n");
848}
849
850void AfisareTabel(t_table *table)
851{
852
853 AfisareColoane(table);
854 if (table == NULL)
855 {
856 return;
857 }
858 if (table->type == INT)
859 {
860 int count_lines = 0;
861 t_intLine *line = table->lines;
862 while (line != NULL)
863 {
864 count_lines++;
865 AfiseazaLinieINT(line);
866 line = line->next;
867 }
868 // if(count_lines == 0)
869 // {
870 // printf("\n");
871 // }
872 }
873 if (table->type == FLOAT)
874 {
875 int count_lines = 0;
876 t_floatLine *line = table->lines;
877 while (line != NULL)
878 {
879 count_lines++;
880 AfiseazaLinieFLOAT(line);
881 line = line->next;
882 }
883 // if(count_lines == 0)
884 // {
885 // printf("\n");
886 // }
887 }
888 if (table->type == STRING)
889 {
890 int count_lines = 0;
891 t_stringLine *line = table->lines;
892 while (line != NULL)
893 {
894 count_lines++;
895 AfiseazaLinieSTRING(line);
896 line = line->next;
897 }
898 // if(count_lines == 0)
899 // {
900 // printf("\n");
901 // }
902 }
903 printf("\n");
904}
905void AfisareBazaDeDate(t_db *database)
906{
907 printf("DATABASE: ");
908 printf("%s\n\n", database->name);
909 t_table *p = database->tables;
910 while (p != NULL)
911 {
912 AfisareTabel(p);
913 p = p->next;
914 }
915}
916int VerificaExistentaColoana(t_table *table, char *name_column)
917{
918 t_column *column = table->columns;
919 int found = 0;
920 while (column != NULL)
921 {
922 if (strcmp(column->name, name_column) == 0)
923 {
924 found = 1;
925 }
926 column = column->next;
927 }
928 return found;
929}
930void EliberareLinii(t_table *table)
931{
932 if (table->type == INT)
933 {
934 t_intLine *line = table->lines;
935 t_intLine *lineToBeDeleted;
936 while (line != NULL)
937 {
938 lineToBeDeleted = line;
939 line = lineToBeDeleted->next;
940 ElibereazaLinieINT(lineToBeDeleted);
941 free(lineToBeDeleted);
942 }
943 table->lines = NULL;
944 }
945 if (table->type == FLOAT)
946 {
947 t_floatLine *line = table->lines;
948 t_floatLine *lineToBeDeleted;
949 while (line != NULL)
950 {
951 lineToBeDeleted = line;
952 line = lineToBeDeleted->next;
953 ElibereazaLinieFLOAT(lineToBeDeleted);
954 free(lineToBeDeleted);
955 }
956 table->lines = NULL;
957 }
958 if (table->type == STRING)
959 {
960 t_stringLine *line = table->lines;
961 t_stringLine *lineToBeDeleted;
962 while (line != NULL)
963 {
964 lineToBeDeleted = line;
965 line = lineToBeDeleted->next;
966 ElibereazaLinieSTRING(lineToBeDeleted);
967 free(lineToBeDeleted);
968 }
969 table->lines = NULL;
970 }
971}
972void EliberareColoane(t_table *table)
973{
974 t_column *column = table->columns;
975 t_column *column_next = column->next;
976 while (column != NULL && column_next != NULL)
977 {
978 //free(column->name);
979 free(column);
980 column = column_next;
981 column_next = column_next->next;
982 }
983 free(column);
984 table->columns = NULL;
985}
986void StergeTabel(t_db *database, char *name_table)
987{
988 t_table *table = database->tables;
989 t_table *prev = NULL;
990 t_table *tableToBeDeleted = NULL;
991
992 if (database->tables == NULL)
993 {
994 return;
995 }
996 if (strcmp(name_table, database->tables->name) == 0)
997 {
998 table = database->tables;
999 database->tables = database->tables->next;
1000 EliberareColoane(table);
1001 EliberareLinii(table);
1002 //free(table);
1003 return;
1004 }
1005 while (table != NULL)
1006 {
1007 if (strcmp(name_table, table->name) == 0)
1008 {
1009 tableToBeDeleted = table;
1010 table = tableToBeDeleted->next;
1011 EliberareColoane(tableToBeDeleted);
1012 EliberareLinii(tableToBeDeleted);
1013 free(tableToBeDeleted->lines);
1014 //free(tableToBeDeleted);
1015 prev->next = table;
1016 return;
1017 }
1018
1019 prev = table;
1020 table = table->next;
1021 }
1022}
1023void EliberareBazaDeDate(t_db *database)
1024{
1025 t_table *table = database->tables;
1026 t_table *tableToBeDeleted = NULL;
1027 while (table != NULL)
1028 {
1029 tableToBeDeleted = table;
1030 table = tableToBeDeleted->next;
1031 // EliberareColoane(tableToBeDeleted);
1032 //EliberareLinii(tableToBeDeleted);
1033
1034 StergeTabel(database, tableToBeDeleted->name);
1035 free(tableToBeDeleted);
1036 }
1037 free(database);
1038}
1039
1040int main()
1041{
1042 char c = '"';
1043 char *input = calloc(300, sizeof(char));
1044 char *name = calloc(30, sizeof(char));
1045 char *command = calloc(30, sizeof(char));
1046 char *dataType = calloc(30, sizeof(char));
1047 char *column = calloc(100, sizeof(char));
1048
1049 t_db *database;
1050 char *token;
1051 t_table *table;
1052 while (1)
1053 {
1054 fgets(input, 200, stdin);
1055 strtok(input, "\n");
1056 token = strtok(input, " ");
1057 strcpy(command, token);
1058 command[strlen(command)] = '\0';
1059 token = strtok(NULL, " ");
1060 if (token != NULL)
1061 {
1062 strcpy(name, token);
1063 name[strlen(name)] = '\0';
1064 }
1065
1066 if (strcmp(command, "INIT_DB") == 0)
1067 {
1068 database = InitializareBazaDeDate(name);
1069 }
1070 else if (strcmp(command, "CREATE") == 0)
1071 {
1072 token = strtok(NULL, " ");
1073 strcpy(dataType, token);
1074
1075 t_table *table_found = GasesteTabel(database, name);
1076 if (table_found == NULL)
1077 {
1078 if (strcmp(dataType, "INT") == 0)
1079 {
1080 size_t type = INT;
1081 table = AdaugareTabel(database, name, type);
1082 }
1083 else if (strcmp(dataType, "FLOAT") == 0)
1084 {
1085 size_t type = FLOAT;
1086 table = AdaugareTabel(database, name, type);
1087 }
1088 else if (strcmp(dataType, "STRING") == 0)
1089 {
1090 size_t type = STRING;
1091 table = AdaugareTabel(database, name, type);
1092 }
1093 else
1094 {
1095 printf("Unknown data type: %c%s%c\n", c, dataType, c);
1096 }
1097 token = strtok(NULL, " ");
1098 // strcpy(first_column, token);
1099
1100 while (token != NULL)
1101 {
1102 strcpy(column, token);
1103 AdaugareColoana(table, column);
1104 token = strtok(NULL, " ");
1105 }
1106 }
1107 else
1108 {
1109 printf("Table %c%s%c already exists.", c, name, c);
1110 printf("\n");
1111 }
1112 }
1113 else if (strcmp(command, "ADD") == 0)
1114 {
1115 t_table *table_found;
1116
1117 table_found = GasesteTabel(database, name);
1118 token = strtok(NULL, " ");
1119
1120 if (table_found != NULL)
1121 {
1122 if (table_found->type == STRING)
1123 {
1124 t_stringLine *line;
1125 line = AdaugaLinieSTRING(table_found);
1126
1127 while (token != NULL)
1128 {
1129 char *cell = token;
1130 cell[strlen(cell)] = '\0';
1131 AdaugaCelulaSTRING(line, cell);
1132 token = strtok(NULL, " ");
1133 }
1134 }
1135 if (table_found->type == INT)
1136 {
1137 t_intLine *line;
1138 line = AdaugaLinieINT(table_found);
1139
1140 while (token != NULL)
1141 {
1142 int cell = atoi(token);
1143 AdaugaCelulaInt(line, cell);
1144 token = strtok(NULL, " ");
1145 }
1146 }
1147 if (table_found->type == FLOAT)
1148 {
1149 t_floatLine *line;
1150 line = AdaugaLinieFLOAT(table_found);
1151
1152 while (token != NULL)
1153 {
1154 float cell = atof(token);
1155 AdaugaCelulaFLOAT(line, cell);
1156 token = strtok(NULL, " ");
1157 }
1158 }
1159 }
1160 else
1161 {
1162 printf("Table %c%s%c not found in database.\n", c, name, c);
1163 }
1164 }
1165 else if (strcmp(command, "SEARCH") == 0)
1166 {
1167 token = strtok(NULL, " ");
1168 char name_column[20];
1169 strcpy(name_column, token);
1170 token = strtok(NULL, " ");
1171 char relation[20];
1172 strcpy(relation, token);
1173 token = strtok(NULL, " ");
1174 char value[20];
1175 strcpy(value, token);
1176 int found;
1177 t_table *table_found = GasesteTabel(database, name);
1178 if (table_found != NULL)
1179 {
1180 found = VerificaExistentaColoana(table_found, name_column);
1181 if (found == 1)
1182 {
1183 AfisareColoane(table_found);
1184 Verificare(table_found, name_column, relation, value);
1185 printf("\n");
1186 }
1187 else
1188 {
1189 printf("Table %c%s%c does not contain column %c%s%c.\n", c, table_found->name, c, c, name_column, c);
1190 }
1191 }
1192 else
1193 {
1194 printf("Table %c%s%c not found in database.\n", c, name, c);
1195 }
1196 }
1197 else if (strcmp(command, "DELETE") == 0)
1198 {
1199 t_table *table_found = GasesteTabel(database, name);
1200 if (table_found == NULL)
1201 {
1202 printf("Table %c%s%c not found in database.\n", c, name, c);
1203 }
1204 else
1205 {
1206 token = strtok(NULL, " ");
1207 if (token == NULL)
1208 {
1209 //EliberareColoane(table_found);
1210 //EliberareLinii(table_found);
1211 StergeTabel(database, name);
1212 free(table_found);
1213 }
1214 else
1215 {
1216 char name_column[20];
1217 strcpy(name_column, token);
1218 token = strtok(NULL, " ");
1219 char relation[20];
1220 strcpy(relation, token);
1221 token = strtok(NULL, " ");
1222 char value[20];
1223 strcpy(value, token);
1224
1225 int found = VerificaExistentaColoana(table_found, name_column);
1226 if (found == 0)
1227 {
1228 printf("Table %c%s%c does not contain column %c%s%c.\n", c, table_found->name, c, c, name_column, c);
1229 }
1230 else
1231 {
1232
1233 VerificareEliminare(table_found, name_column, relation, value);
1234 }
1235 }
1236 }
1237 }
1238 else if (strcmp(command, "CLEAR") == 0)
1239 {
1240 t_table *table_found = GasesteTabel(database, name);
1241 EliberareLinii(table_found);
1242 }
1243 else if (strcmp(command, "PRINT") == 0)
1244 {
1245 t_table *table_found;
1246 table_found = GasesteTabel(database, name);
1247 if (table_found == NULL)
1248 {
1249 printf("Table %c%s%c not found in database.\n", c, name, c);
1250 }
1251 else
1252 {
1253 AfisareTabel(table_found);
1254 }
1255 }
1256 else if (strcmp(command, "PRINT_DB") == 0)
1257 {
1258 AfisareBazaDeDate(database);
1259 }
1260 else if (strcmp(command, "DELETE_DB") == 0)
1261 {
1262 EliberareBazaDeDate(database);
1263 break;
1264 }
1265 else
1266 {
1267 printf("Unknown command: %c%s%c\n", c, command, c);
1268 }
1269 }
1270 free(input);
1271 free(name);
1272 free(command);
1273 free(dataType);
1274 free(column);
1275}