-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.c
134 lines (112 loc) · 2.98 KB
/
database.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <string.h>
#include "csvparser.h"
typedef struct ColName{
char ColumnName[64];
int accesses;
struct ColName* next;
} ColName;
void push(ColName **head, int accesses,char ColumnName[])
{
ColName *temp = malloc(sizeof(ColName));
temp->accesses = accesses;
strcpy(temp->ColumnName,ColumnName);
temp->next = *head;
*head = temp;
}
void printList(ColName*head){
while(head->next!=NULL){
printf("Column Name: %s , Accesses: %d\n",head->ColumnName,head->accesses);
head=head->next;
}
}
void getColNameFromQueryAndInsertIt(ColName **head,char* query){
int i,equalPos,j,from,byEqual=0,till=0;
char column[64];
for(i=0;i<strlen(query);i++){
if(query[i]=='='){
equalPos = i;
}
}
if(query[equalPos-1]==' '){
equalPos--;
while(query[equalPos]==' '){
equalPos--;
}
till = equalPos;
}else{
till = equalPos-1;
}
while(query[equalPos]!=' '){
equalPos--;
from=equalPos+1;
}
i=0;
while(from < till+1){
column[i++]=query[from++];
}
column[i]='\0';
ColName* cur = *head;
while(cur){
if(!strcmp(cur->ColumnName,column)){
cur->accesses++;
return;
}
cur = cur->next;
}
push(head,1,column);
}
char *trim(char *str)
{
char *end;
while(isspace((unsigned char)*str)) str++;
if(*str == 0)
return str;
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
end[1] = '\0';
return str;
}
int countList(ColName *head){
int counter=0;
while(head->next){
counter++;
head=head->next;
}
return counter;
}
int main()
{
ColName *list = malloc(sizeof(ColName));
list->next = NULL;
int i = 0,counts=0;
char buf[1024],dbName[64],fileLog[64],filePath[1024];
printf("please enter database name: \n");
scanf("%s",dbName);
printf("please enter full log file path (csv format sperated by ,): \n");
scanf("%s",fileLog);
CsvParser *csvparser = CsvParser_new(fileLog,",", 1);
CsvRow *header;
CsvRow *row;
header = CsvParser_getHeader(csvparser);
if (header == NULL) {
printf("%s\n", CsvParser_getErrorMessage(csvparser));
return 1;
}
char **headerFields = CsvParser_getFields(header);
while ((row = CsvParser_getRow(csvparser)) ) {
char **rowFields = CsvParser_getFields(row);
for (i = 0 ; i < CsvParser_getNumFields(row) ; i++) {
if(i==27 && trim(rowFields[i])!= "" ){
getColNameFromQueryAndInsertIt(&list,rowFields[i]);
}
}
CsvParser_destroy_row(row);
}
CsvParser_destroy(csvparser);
sprintf(buf, ".\\createIndexesBatchFile.bat %d %s", countList(list),dbName); // puts string into buf
printf("Please Consider Adding Indexes On Columns Having More Than 100 Accesses:\n\n");
printList(list);
system(buf);
return 0;
}