-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselectionner_de_table.c
112 lines (101 loc) · 4 KB
/
selectionner_de_table.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "table.h"
// Fonction pour sélectionner et afficher des données d'une table
void selectionner_de_table(char *name, char *condition) {
Table *table = NULL;
for (int i = 0; i < table_count; i++) {
if (strcmp(tables[i].name, name) == 0) {
table = &tables[i];
break;
}
}
if (table == NULL) {
printf("Erreur : Table '%s' non trouvee.\n", name);
return;
}
printf("Table '%s':\n", name);
int col_index = -1;
char *col_name = NULL;
char *col_value = NULL;
char *operator = NULL;
if (condition != NULL) {
// Supprimer les guillemets simples autour du nom de la colonne
col_name = strtok(condition, " ");
if (col_name[0] == '\'' && col_name[strlen(col_name) - 1] == '\'') {
col_name = strdup(col_name + 1);
col_name[strlen(col_name) - 1] = '\0';
}
operator = strtok(NULL, " ");
col_value = strtok(NULL, " ");
// Trouver l'index de la colonne pour la condition
for (int j = 0; j < table->columns; j++) {
if (strcmp(table->column_defs[j].name, col_name) == 0) {
col_index = j;
break;
}
}
if (col_index == -1) {
printf("Erreur : Colonne '%s' non trouvee.\n", col_name);
return;
}
}
// Afficher les en-têtes de colonne
for (int c = 0; c < table->columns; c++) {
printf("%s\t", table->column_defs[c].name);
}
printf("\n");
// Afficher les lignes de la table
for (int r = 0; r < table->rows; r++) {
// Vérifier la condition (si elle est spécifiée)
if (condition != NULL) {
// Vérifier le type de données de la colonne
if (table->column_defs[col_index].type == ENTIER) {
int value1 = atoi(table->data[r][col_index]);
int value2 = atoi(col_value);
// Vérifier l'opérateur
if (
(strcmp(operator, "<=") == 0 && value1 <= value2) ||
(strcmp(operator, ">=") == 0 && value1 >= value2) ||
(strcmp(operator, "<") == 0 && value1 < value2) ||
(strcmp(operator, ">") == 0 && value1 > value2) ||
(strcmp(operator, "=") == 0 && value1 == value2)
) {
// Afficher la ligne si la condition est vraie
for (int c = 0; c < table->columns; c++) {
printf("%s\t", table->data[r][c]);
}
printf("\n");
}
} else if (table->column_defs[col_index].type == CHAINE) {
// Vérifier l'opérateur
if (
(strcmp(operator, "=") == 0 && strcmp(table->data[r][col_index], col_value) == 0) ||
(strcmp(operator, "LIKE") == 0 && strstr(table->data[r][col_index], col_value) != NULL)
) {
// Afficher la ligne si la condition est vraie
for (int c = 0; c < table->columns; c++) {
printf("%s\t", table->data[r][c]);
}
printf("\n");
}
} else if (table->column_defs[col_index].type == DATE) {
// Vérifier l'opérateur
if (strcmp(operator, "=") == 0 && strcmp(table->data[r][col_index], col_value) == 0) {
// Afficher la ligne si la condition est vraie
for (int c = 0; c < table->columns; c++) {
printf("%s\t", table->data[r][c]);
}
printf("\n");
}
}
} else {
// Afficher toutes les lignes si aucune condition n'est spécifiée
for (int c = 0; c < table->columns; c++) {
printf("%s\t", table->data[r][c]);
}
printf("\n");
}
}
}